Raise some WARNS in usr.bin.
[dragonfly.git] / contrib / opie / libopie / verify.c
blobe6abec4d217a2a05250436f28b47651e82212a71
1 /* verify.c: The opieverify() library function.
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you didn't get a copy, you may request one from <license@inner.net>.
9 History:
11 Modified by cmetz for OPIE 2.4. Use struct opie_otpkey for keys.
12 Check that seed and sequence number are valid.
13 Modified by cmetz for OPIE 2.32. Renamed _opieparsechallenge() to
14 __opieparsechallenge() and handle new argument. Fixed init
15 response parsing bug.
16 Modified by cmetz for OPIE 2.31. Renamed "init" to "init-hex".
17 Modified by cmetz for OPIE 2.31. Renamed "init" and "RESPONSE_INIT"
18 to "init-hex" and "RESPONSE_INIT_HEX". Removed active attack
19 protection support.
20 Created by cmetz for OPIE 2.3 using the old verify.c as a guide.
23 #include "opie_cfg.h"
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif /* HAVE_STRING_H */
27 #include "opie.h"
29 #define RESPONSE_STANDARD 0
30 #define RESPONSE_WORD 1
31 #define RESPONSE_HEX 2
32 #define RESPONSE_INIT_HEX 3
33 #define RESPONSE_INIT_WORD 4
34 #define RESPONSE_UNKNOWN 5
36 struct _rtrans {
37 int type;
38 char *name;
41 static struct _rtrans rtrans[] = {
42 { RESPONSE_WORD, "word" },
43 { RESPONSE_HEX, "hex" },
44 { RESPONSE_INIT_HEX, "init-hex" },
45 { RESPONSE_INIT_WORD, "init-word" },
46 { RESPONSE_STANDARD, "" },
47 { RESPONSE_UNKNOWN, NULL }
50 static char *algids[] = { NULL, NULL, NULL, "sha1", "md4", "md5" };
52 static int changed FUNCTION((opie), struct opie *opie)
54 struct opie opie2;
56 memset(&opie2, 0, sizeof(struct opie));
57 opie2.opie_principal = opie->opie_principal;
58 if (__opiereadrec(&opie2))
59 return 1;
61 if ((opie2.opie_n != opie->opie_n) || strcmp(opie2.opie_val, opie->opie_val) || strcmp(opie2.opie_seed, opie->opie_seed))
62 return 1;
64 memset(&opie2, 0, sizeof(struct opie));
65 return 0;
68 int opieverify FUNCTION((opie, response), struct opie *opie AND char *response)
70 int i, rval = -1;
71 char *c;
72 struct opie_otpkey key, fkey, lastkey;
73 struct opie nopie;
75 if (!opie || !response)
76 goto verret;
78 if (!opie->opie_principal)
79 #if DEBUG
80 abort();
81 #else /* DEBUG */
82 goto verret;
83 #endif /* DEBUG */
85 if (!opieatob8(&lastkey, opie->opie_val))
86 goto verret;
88 for (c = opie->opie_seed; *c; c++)
89 if (!isalnum(*c))
90 goto verret;
92 if (opie->opie_n <= 0)
93 goto verret;
95 if (c = strchr(response, ':')) {
96 *(c++) = 0;
98 struct _rtrans *r;
99 for (r = rtrans; r->name && strcmp(r->name, response); r++);
100 i = r->type;
102 } else
103 i = RESPONSE_STANDARD;
105 switch(i) {
106 case RESPONSE_STANDARD:
107 i = 1;
109 if (opieetob(&key, response) == 1) {
110 memcpy(&fkey, &key, sizeof(struct opie_otpkey));
111 opiehash(&fkey, MDX);
112 i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
114 if (i && opieatob8(&key, response)) {
115 memcpy(&fkey, &key, sizeof(struct opie_otpkey));
116 opiehash(&fkey, MDX);
117 i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
119 break;
120 case RESPONSE_WORD:
121 i = 1;
123 if (opieetob(&key, c) == 1) {
124 memcpy(&fkey, &key, sizeof(struct opie_otpkey));
125 opiehash(&fkey, MDX);
126 i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
128 break;
129 case RESPONSE_HEX:
130 i = 1;
132 if (opieatob8(&key, c)) {
133 memcpy(&fkey, &key, sizeof(struct opie_otpkey));
134 opiehash(&fkey, MDX);
135 i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
137 break;
138 case RESPONSE_INIT_HEX:
139 case RESPONSE_INIT_WORD:
141 char *c2;
143 if (!(c2 = strchr(c, ':')))
144 goto verret;
146 *(c2++) = 0;
148 if (i == RESPONSE_INIT_HEX) {
149 if (!opieatob8(&key, c))
150 goto verret;
151 } else {
152 if (opieetob(&key, c) != 1)
153 goto verret;
156 memcpy(&fkey, &key, sizeof(struct opie_otpkey));
157 opiehash(&fkey, MDX);
159 if (memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey)))
160 goto verret;
162 if (changed(opie))
163 goto verret;
165 opie->opie_n--;
167 if (!opiebtoa8(opie->opie_val, &key))
168 goto verret;
170 if (__opiewriterec(opie))
171 goto verret;
173 if (!(c2 = strchr(c = c2, ':')))
174 goto verret;
176 *(c2++) = 0;
179 int j, k;
181 if (__opieparsechallenge(c, &j, &(opie->opie_n), &(opie->opie_seed), &k) || (j != MDX) || k)
182 goto verret;
185 if (i == RESPONSE_INIT_HEX) {
186 if (!opieatob8(&key, c2))
187 goto verret;
188 } else {
189 if (opieetob(&key, c2) != 1)
190 goto verret;
193 goto verwrt;
194 case RESPONSE_UNKNOWN:
195 rval = 1;
196 goto verret;
197 default:
198 rval = -1;
199 goto verret;
202 if (i) {
203 rval = 1;
204 goto verret;
207 if (changed(opie))
208 goto verret;
210 opie->opie_n--;
212 verwrt:
213 if (!opiebtoa8(opie->opie_val, &key))
214 goto verret;
215 rval = __opiewriterec(opie);
217 verret:
218 opieunlock();
219 memset(opie, 0, sizeof(struct opie));
220 return rval;