summaryrefslogtreecommitdiffstats
path: root/contrib/opie/opiekey.c
blob: 698834968b89cc26227f3910bd756cc54f6c4a99 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* opiekey.c: Stand-alone program for computing responses to OTP challenges.

 Takes a sequence number and seed (presumably from an OPIE challenge)
 as command line arguments, prompts for the user's secret pass phrase,
 and outputs a response.

%%% portions-copyright-cmetz-96
Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
Reserved. The Inner Net License Version 2 applies to these portions of
the 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>.

Portions of this software are Copyright 1995 by Randall Atkinson and Dan
McDonald, All Rights Reserved. All Rights under this copyright are assigned
to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
License Agreement applies to this software.

	History:

	Modified by cmetz for OPIE 2.4. Use struct opie_key for key blocks.
	Modified by cmetz for OPIE 2.31. Renamed "init" and RESPONSE_INIT
	        to "init-hex" and RESPONSE_INIT_HEX. Removed active attack
		protection support.
	Modified by cmetz for OPIE 2.3. OPIE_PASS_MAX changed to
		OPIE_SECRET_MAX. Added extended responses, which created
		lots of changes. Eliminated extra variable. Added -x and
		-t to help. Added -f flag. Added SHA support.
	Modified by cmetz for OPIE 2.22. Print newline after seed too long
	        message. Check for minimum seed length. Correct a grammar
		error.
        Modified at NRL for OPIE 2.2. Check opiereadpass() return.
                Change opiereadpass() calls to add echo arg. Use FUNCTION
                definition et al. Check seed length here, too. Added back
		hex output. Reworked final output function.
	Modified at NRL for OPIE 2.0.
	Written at Bellcore for the S/Key Version 1 software distribution
		(skey.c).

$FreeBSD$

*/
#include "opie_cfg.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "opie.h"

#ifdef	__MSDOS__
#include <dos.h>
#endif

#if HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */

extern char *optarg;
extern int optind, opterr;

int aflag = 0;

char *algnames[] = { NULL, NULL, NULL, "SHA-1", "MD4", "MD5" };
char *algids[] = { NULL, NULL, NULL, "sha1", "md4", "md5" };

/******** Begin real source code ***************/

static VOIDRET usage FUNCTION((s), char *s)
{
  fprintf(stderr, "usage: %s [-v] [-h] [-f] [-x] [-t type] [-4 | -5 | -s] [-a] [-n count] sequence_number seed\n", s);
  exit(1);
}

#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_STANDARD, "standard" },
  { RESPONSE_STANDARD, "otp" },
  { RESPONSE_UNKNOWN, NULL }
};

static void getsecret FUNCTION((secret, promptextra, retype), char *secret AND char *promptextra AND int flags)
{
  fprintf(stderr, "Enter %ssecret pass phrase: ", promptextra);
  if (!opiereadpass(secret, OPIE_SECRET_MAX, 0)) {
    fprintf(stderr, "Error reading %ssecret pass phrase!\n", promptextra);
    exit(1);
  }
  if (secret[0] && (flags & 1)) {
    char verify[OPIE_SECRET_MAX + 1];

    fprintf(stderr, "Again %ssecret pass phrase: ", promptextra);
    if (!opiereadpass(verify, OPIE_SECRET_MAX, 0)) {
      fprintf(stderr, "Error reading %ssecret pass phrase!\n", promptextra);
      memset(verify, 0, sizeof(verify));
      memset(secret, 0, OPIE_SECRET_MAX + 1);
      exit(1);
    }
    if (verify[0] && strcmp(verify, secret)) {
      fprintf(stderr, "They don't match. Try again.\n");
      memset(verify, 0, sizeof(verify));
      memset(secret, 0, OPIE_SECRET_MAX + 1);
      exit(1);
    }
    memset(verify, 0, sizeof(verify));
  }
  if (!(flags & 2) && !aflag && opiepasscheck(secret)) {
    memset(secret, 0, OPIE_SECRET_MAX + 1);
    fprintf(stderr, "Secret pass phrases must be between %d and %d characters long.\n", OPIE_SECRET_MIN, OPIE_SECRET_MAX);
    exit(1);
  };
}

int main FUNCTION((argc, argv), int argc AND char *argv[])
{
  /* variable declarations */
  unsigned algorithm = MDX;	/* default algorithm per Makefile's MDX
				   symbol */
  int keynum = 0;
  int i;
  int count = 1;
  char secret[OPIE_SECRET_MAX + 1], newsecret[OPIE_SECRET_MAX + 1];
  struct opie_otpkey key, newkey;
  char *seed, newseed[OPIE_SEED_MAX + 1];
  char response[OPIE_RESPONSE_MAX + 1];
  char *slash;
  int hex = 0;
  int type = RESPONSE_STANDARD;
  int force = 0;

  if (slash = strchr(argv[0], '/'))
    slash++;
  else
    slash = argv[0];

  if (!strcmp(slash, "key") || strstr(slash, "md4"))
    algorithm = 4;

  if (strstr(slash, "md5"))
    algorithm = 5;

  if (strstr(slash, "sha"))
    algorithm = 3;

  while ((i = getopt(argc, argv, "fhvn:x45at:s")) != EOF) {
    switch (i) {
    case 'v':
      opieversion();

    case 'n':
      count = atoi(optarg);
      break;

    case 'x':
      hex = 1;
      break;

    case 'f':
#if INSECURE_OVERRIDE
      force = 1;
#else /* INSECURE_OVERRIDE */
      fprintf(stderr, "Sorry, but the -f option is not supported by this build of OPIE.\n");
#endif /* INSECURE_OVERRIDE */
      break;

    case '4':
      /* use MD4 algorithm */
      algorithm = 4;
      break;

    case '5':
      /* use MD5 algorithm */
      algorithm = 5;
      break;

    case 'a':
      aflag = 1;
      break;

    case 't':
      {
	struct _rtrans *r;
	for (r = rtrans; r->name && strcmp(r->name, optarg); r++);
	if (!r->name) {
	  fprintf(stderr, "%s: %s: unknown response type.\n", argv[0], optarg);
	  exit(1);
	}
	type = r->type;
      }
      break;

    case 's':
      algorithm = 3;
      break;

    default:
      usage(argv[0]);
    }
  }

  if ((argc - optind) < 2)
    usage(argv[0]);

  fprintf(stderr, "Using the %s algorithm to compute response.\n", algnames[algorithm]);

  /* get sequence number, which is next-to-last parameter */
  keynum = atoi(argv[optind]);
  if (keynum < 1) {
    fprintf(stderr, "Sequence number %s is not positive.\n", argv[optind]);
    exit(1);
  }
  /* get seed string, which is last parameter */
  seed = argv[optind + 1];
  {
    i = strlen(seed);

    if (i > OPIE_SEED_MAX) {
      fprintf(stderr, "Seeds must be less than %d characters long.\n", OPIE_SEED_MAX);
      exit(1);
    }
    if (i < OPIE_SEED_MIN) {
      fprintf(stderr, "Seeds must be greater than %d characters long.\n", OPIE_SEED_MIN);
      exit(1);
    }
  }

  fprintf(stderr, "Reminder: Don't use opiekey from telnet or dial-in sessions.\n");

  if (opieinsecure()) {
    fprintf(stderr, "Sorry, but you don't seem to be on the console or a secure terminal.\n");
#if INSECURE_OVERRIDE
    if (force)
      fprintf(stderr, "Warning: Continuing could disclose your secret pass phrase to an attacker!\n");
    else
#endif /* INSECURE_OVERRIDE */
      exit(1);
  }

  if ((type == RESPONSE_INIT_HEX) || (type == RESPONSE_INIT_WORD)) {
#if RETYPE
    getsecret(secret, "old ", 1);
#else /* RETYPE */
    getsecret(secret, "old ", 0);
#endif /* RETYPE */
    getsecret(newsecret, "new ", 1);
    if (!newsecret[0])
      strcpy(newsecret, secret);

    if (opienewseed(strcpy(newseed, seed)) < 0) {
      fprintf(stderr, "Error updating seed.\n");
      goto error;
    }

    if (opiekeycrunch(algorithm, &newkey, newseed, newsecret)) {
      fprintf(stderr, "%s: key crunch failed (1)\n", argv[0]);
      goto error;
    }

    for (i = 0; i < 499; i++)
      opiehash(&newkey, algorithm);
  } else
#if RETYPE
    getsecret(secret, "", 1);
#else /* RETYPE */
    getsecret(secret, "", 0);
#endif /* RETYPE */

  /* Crunch seed and secret password into starting key normally */
  if (opiekeycrunch(algorithm, &key, seed, secret)) {
    fprintf(stderr, "%s: key crunch failed\n", argv[0]);
    goto error;
  }

  for (i = 0; i <= (keynum - count); i++)
    opiehash(&key, algorithm);

  {
    char buf[OPIE_SEED_MAX + 48 + 1];
    char *c;

    for (; i <= keynum; i++) {
      if (count > 1)
	printf("%d: %s", i, (type == RESPONSE_STANDARD) ? "" : "\n");
      
      switch(type) {
      case RESPONSE_STANDARD:
	if (hex)
	  opiebtoh(response, &key);
	else
	  opiebtoe(response, &key);
	break;
      case RESPONSE_WORD:
	strcpy(response, "word:");
	strcat(response, opiebtoe(buf, &key));
	break;
      case RESPONSE_HEX:
	strcpy(response, "hex:");
	strcat(response, opiebtoh(buf, &key));
	break;
      case RESPONSE_INIT_HEX:
      case RESPONSE_INIT_WORD:
	if (type == RESPONSE_INIT_HEX) {
	  strcpy(response, "init-hex:");
	  strcat(response, opiebtoh(buf, &key));
	  sprintf(buf, ":%s 499 %s:", algids[algorithm], newseed);
	  strcat(response, buf);
	  strcat(response, opiebtoh(buf, &newkey));
	} else {
	  strcpy(response, "init-word:");
	  strcat(response, opiebtoe(buf, &key));
	  sprintf(buf, ":%s 499 %s:", algids[algorithm], newseed);
	  strcat(response, buf);
	  strcat(response, opiebtoe(buf, &newkey));
	}
	break;
      }
      puts(response);
      opiehash(&key, algorithm);
    }
  }

  memset(secret, 0, sizeof(secret));
  memset(newsecret, 0, sizeof(newsecret));
  return 0;

error:
  memset(secret, 0, sizeof(secret));
  memset(newsecret, 0, sizeof(newsecret));
  return 1;
}
OpenPOWER on IntegriCloud