summaryrefslogtreecommitdiffstats
path: root/contrib/opie/libopie/verify.c
blob: e6abec4d217a2a05250436f28b47651e82212a71 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* verify.c: The opieverify() library function.

%%% copyright-cmetz-96
This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
The Inner Net License Version 3 applies to this software.
You should have received a copy of the license with this software. If
you didn't get a copy, you may request one from <license@inner.net>.

	History:

	Modified by cmetz for OPIE 2.4. Use struct opie_otpkey for keys.
		Check that seed and sequence number are valid.
	Modified by cmetz for OPIE 2.32. Renamed _opieparsechallenge() to
		__opieparsechallenge() and handle new argument. Fixed init
		response parsing bug.
	Modified by cmetz for OPIE 2.31. Renamed "init" to "init-hex".
	Modified by cmetz for OPIE 2.31. Renamed "init" and "RESPONSE_INIT"
		to "init-hex" and "RESPONSE_INIT_HEX". Removed active attack
		protection support.
	Created by cmetz for OPIE 2.3 using the old verify.c as a guide.
*/

#include "opie_cfg.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#include "opie.h"

#define RESPONSE_STANDARD  0
#define RESPONSE_WORD      1
#define RESPONSE_HEX       2
#define RESPONSE_INIT_HEX  3
#define RESPONSE_INIT_WORD 4
#define RESPONSE_UNKNOWN   5

struct _rtrans {
  int type;
  char *name;
};

static struct _rtrans rtrans[] = {
  { RESPONSE_WORD, "word" },
  { RESPONSE_HEX, "hex" },
  { RESPONSE_INIT_HEX, "init-hex" },
  { RESPONSE_INIT_WORD, "init-word" },
  { RESPONSE_STANDARD, "" },
  { RESPONSE_UNKNOWN, NULL }
};

static char *algids[] = { NULL, NULL, NULL, "sha1", "md4", "md5" };

static int changed FUNCTION((opie), struct opie *opie)
{
  struct opie opie2;

  memset(&opie2, 0, sizeof(struct opie));
  opie2.opie_principal = opie->opie_principal;
  if (__opiereadrec(&opie2))
    return 1;

  if ((opie2.opie_n != opie->opie_n) || strcmp(opie2.opie_val, opie->opie_val) || strcmp(opie2.opie_seed, opie->opie_seed))
    return 1;

  memset(&opie2, 0, sizeof(struct opie));
  return 0;
}

int opieverify FUNCTION((opie, response), struct opie *opie AND char *response)
{
  int i, rval = -1;
  char *c;
  struct opie_otpkey key, fkey, lastkey;
  struct opie nopie;

  if (!opie || !response)
    goto verret;

  if (!opie->opie_principal)
#if DEBUG
    abort();
#else /* DEBUG */
    goto verret;
#endif /* DEBUG */

  if (!opieatob8(&lastkey, opie->opie_val))
    goto verret;

  for (c = opie->opie_seed; *c; c++)
    if (!isalnum(*c))
      goto verret;

  if (opie->opie_n <= 0)
    goto verret;

  if (c = strchr(response, ':')) {
    *(c++) = 0;
    {
      struct _rtrans *r;
      for (r = rtrans; r->name && strcmp(r->name, response); r++);
      i = r->type;
    }
  } else
    i = RESPONSE_STANDARD;

  switch(i) {
  case RESPONSE_STANDARD:
    i = 1;
    
    if (opieetob(&key, response) == 1) {
      memcpy(&fkey, &key, sizeof(struct opie_otpkey));
      opiehash(&fkey, MDX);
      i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
    }
    if (i && opieatob8(&key, response)) {
      memcpy(&fkey, &key, sizeof(struct opie_otpkey));
      opiehash(&fkey, MDX);
      i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
    }
    break;
  case RESPONSE_WORD:
    i = 1;

    if (opieetob(&key, c) == 1) {
      memcpy(&fkey, &key, sizeof(struct opie_otpkey));
      opiehash(&fkey, MDX);
      i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
    }
    break;
  case RESPONSE_HEX:
    i = 1;

    if (opieatob8(&key, c)) {
      memcpy(&fkey, &key, sizeof(struct opie_otpkey));
      opiehash(&fkey, MDX);
      i = memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey));
    }
    break;
  case RESPONSE_INIT_HEX:
  case RESPONSE_INIT_WORD:
    {
      char *c2;

      if (!(c2 = strchr(c, ':')))
	goto verret;

      *(c2++) = 0;

      if (i == RESPONSE_INIT_HEX) {
	if (!opieatob8(&key, c))
	  goto verret;
      } else {
	if (opieetob(&key, c) != 1)
	  goto verret;
      }

      memcpy(&fkey, &key, sizeof(struct opie_otpkey));
      opiehash(&fkey, MDX);

      if (memcmp(&fkey, &lastkey, sizeof(struct opie_otpkey)))
	goto verret;

      if (changed(opie))
	goto verret;
      
      opie->opie_n--;

      if (!opiebtoa8(opie->opie_val, &key))
	goto verret;

      if (__opiewriterec(opie))
	goto verret;

      if (!(c2 = strchr(c = c2, ':')))
	goto verret;

      *(c2++) = 0;

      {
	int j, k;

	if (__opieparsechallenge(c, &j, &(opie->opie_n), &(opie->opie_seed), &k) || (j != MDX) || k)
	  goto verret;
      }

      if (i == RESPONSE_INIT_HEX) {
	if (!opieatob8(&key, c2))
	  goto verret;
      } else {
	if (opieetob(&key, c2) != 1)
	  goto verret;
      }
    }
    goto verwrt;
  case RESPONSE_UNKNOWN:
    rval = 1;
    goto verret;
  default:
    rval = -1;
    goto verret;
  }

  if (i) {
    rval = 1;
    goto verret;
  }

  if (changed(opie))
    goto verret;
  
  opie->opie_n--;

verwrt:
  if (!opiebtoa8(opie->opie_val, &key))
    goto verret;
  rval = __opiewriterec(opie);

verret:
  opieunlock();
  memset(opie, 0, sizeof(struct opie));
  return rval;
}
OpenPOWER on IntegriCloud