-
Notifications
You must be signed in to change notification settings - Fork 8
/
t_pw.c
133 lines (111 loc) · 3.6 KB
/
t_pw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 1997-2007 The Stanford SRP Authentication Project
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Redistributions in source or binary form must retain an intact copy
* of this copyright notice.
*/
#include "t_defines.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "t_pwd.h"
#include "t_read.h"
#include "t_sha.h"
#include "srp_aux.h"
static struct t_pw * syspw = NULL;
static struct t_passwd tpass;
_TYPE( struct t_pw * )
t_newpw()
{
struct t_pw * tpw;
if((tpw = malloc(sizeof(struct t_pw))) == NULL)
return NULL;
tpw->pwbuf = cstr_new();
return tpw;
}
_TYPE( void )
t_closepw(tpw)
struct t_pw * tpw;
{
if(tpw->pwbuf)
cstr_clear_free(tpw->pwbuf);
free(tpw);
}
_TYPE( struct t_pwent * )
t_makepwent(tpw, user, pass, salt, confent)
struct t_pw * tpw;
const char * user;
const char * pass;
const struct t_num * salt;
const struct t_confent * confent;
{
BigInteger x, v, n, g;
unsigned char dig[SHA_DIGESTSIZE];
SHA1_CTX ctxt;
int i = 0;
tpw->pebuf.name = tpw->userbuf;
tpw->pebuf.salt.data = tpw->saltbuf;
strncpy(tpw->pebuf.name, user, MAXUSERLEN);
tpw->pebuf.index = confent->index;
if(salt) {
tpw->pebuf.salt.len = salt->len;
memcpy(tpw->pebuf.salt.data, salt->data, salt->len);
}
else {
memset(dig, 0, SALTLEN); /* salt is 80 bits */
tpw->pebuf.salt.len = SALTLEN;
do {
t_random(tpw->pebuf.salt.data, SALTLEN);
} while(memcmp(tpw->pebuf.salt.data, dig, SALTLEN) == 0);
if(tpw->pebuf.salt.data[0] == 0)
tpw->pebuf.salt.data[0] = 0xff;
}
n = BigIntegerFromBytes(confent->modulus.data, confent->modulus.len);
g = BigIntegerFromBytes(confent->generator.data, confent->generator.len);
v = BigIntegerFromInt(0);
SHA1Init(&ctxt);
SHA1Update(&ctxt, user, strlen(user));
SHA1Update(&ctxt, ":", 1);
SHA1Update(&ctxt, pass, strlen(pass));
SHA1Final(dig, &ctxt);
SHA1Init(&ctxt);
SHA1Update(&ctxt, tpw->pebuf.salt.data, tpw->pebuf.salt.len);
SHA1Update(&ctxt, dig, sizeof(dig));
SHA1Final(dig, &ctxt);
/* x = H(s, H(u, ':', p)) */
x = BigIntegerFromBytes(dig, sizeof(dig));
BigIntegerModExp(v, g, x, n, NULL, NULL);
BigIntegerToCstr(v, tpw->pwbuf);
tpw->pebuf.password.data = tpw->pwbuf->data;
tpw->pebuf.password.len = tpw->pwbuf->length;
BigIntegerFree(v);
BigIntegerFree(x);
BigIntegerFree(g);
BigIntegerFree(n);
return &tpw->pebuf;
}