Annotation of src/usr.bin/ssh/key.c, Revision 1.71
1.71 ! otto 1: /* $OpenBSD: key.c,v 1.70 2008/06/11 21:01:35 grunk Exp $ */
1.1 markus 2: /*
1.11 deraadt 3: * read_bignum():
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: *
6: * As far as I am concerned, the code I have written for this software
7: * can be used freely for any purpose. Any derived versions of this
8: * software must be clearly marked as such, and if the derived work is
9: * incompatible with the protocol description in the RFC file, it must be
10: * called by a name other than "ssh" or "Secure Shell".
11: *
12: *
1.28 markus 13: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.1 markus 14: *
15: * Redistribution and use in source and binary forms, with or without
16: * modification, are permitted provided that the following conditions
17: * are met:
18: * 1. Redistributions of source code must retain the above copyright
19: * notice, this list of conditions and the following disclaimer.
20: * 2. Redistributions in binary form must reproduce the above copyright
21: * notice, this list of conditions and the following disclaimer in the
22: * documentation and/or other materials provided with the distribution.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: */
1.67 deraadt 35:
1.70 grunk 36: #include <sys/param.h>
1.67 deraadt 37: #include <sys/types.h>
1.1 markus 38:
1.2 markus 39: #include <openssl/evp.h>
1.65 stevesk 40:
1.66 stevesk 41: #include <stdio.h>
1.65 stevesk 42: #include <string.h>
1.15 markus 43:
1.1 markus 44: #include "xmalloc.h"
45: #include "key.h"
1.12 markus 46: #include "rsa.h"
1.3 markus 47: #include "uuencode.h"
1.12 markus 48: #include "buffer.h"
1.15 markus 49: #include "log.h"
1.1 markus 50:
51: Key *
52: key_new(int type)
53: {
54: Key *k;
55: RSA *rsa;
56: DSA *dsa;
1.63 djm 57: k = xcalloc(1, sizeof(*k));
1.1 markus 58: k->type = type;
1.3 markus 59: k->dsa = NULL;
60: k->rsa = NULL;
1.1 markus 61: switch (k->type) {
1.12 markus 62: case KEY_RSA1:
1.1 markus 63: case KEY_RSA:
1.38 markus 64: if ((rsa = RSA_new()) == NULL)
65: fatal("key_new: RSA_new failed");
66: if ((rsa->n = BN_new()) == NULL)
67: fatal("key_new: BN_new failed");
68: if ((rsa->e = BN_new()) == NULL)
69: fatal("key_new: BN_new failed");
1.1 markus 70: k->rsa = rsa;
71: break;
72: case KEY_DSA:
1.38 markus 73: if ((dsa = DSA_new()) == NULL)
74: fatal("key_new: DSA_new failed");
75: if ((dsa->p = BN_new()) == NULL)
76: fatal("key_new: BN_new failed");
77: if ((dsa->q = BN_new()) == NULL)
78: fatal("key_new: BN_new failed");
79: if ((dsa->g = BN_new()) == NULL)
80: fatal("key_new: BN_new failed");
81: if ((dsa->pub_key = BN_new()) == NULL)
82: fatal("key_new: BN_new failed");
1.1 markus 83: k->dsa = dsa;
84: break;
1.12 markus 85: case KEY_UNSPEC:
1.1 markus 86: break;
87: default:
88: fatal("key_new: bad key type %d", k->type);
89: break;
90: }
91: return k;
92: }
1.45 deraadt 93:
1.12 markus 94: Key *
95: key_new_private(int type)
96: {
97: Key *k = key_new(type);
98: switch (k->type) {
99: case KEY_RSA1:
100: case KEY_RSA:
1.38 markus 101: if ((k->rsa->d = BN_new()) == NULL)
102: fatal("key_new_private: BN_new failed");
103: if ((k->rsa->iqmp = BN_new()) == NULL)
104: fatal("key_new_private: BN_new failed");
105: if ((k->rsa->q = BN_new()) == NULL)
106: fatal("key_new_private: BN_new failed");
107: if ((k->rsa->p = BN_new()) == NULL)
108: fatal("key_new_private: BN_new failed");
109: if ((k->rsa->dmq1 = BN_new()) == NULL)
110: fatal("key_new_private: BN_new failed");
111: if ((k->rsa->dmp1 = BN_new()) == NULL)
112: fatal("key_new_private: BN_new failed");
1.12 markus 113: break;
114: case KEY_DSA:
1.38 markus 115: if ((k->dsa->priv_key = BN_new()) == NULL)
116: fatal("key_new_private: BN_new failed");
1.12 markus 117: break;
118: case KEY_UNSPEC:
119: break;
120: default:
121: break;
122: }
123: return k;
124: }
1.45 deraadt 125:
1.1 markus 126: void
127: key_free(Key *k)
128: {
1.60 djm 129: if (k == NULL)
1.62 deraadt 130: fatal("key_free: key is NULL");
1.1 markus 131: switch (k->type) {
1.12 markus 132: case KEY_RSA1:
1.1 markus 133: case KEY_RSA:
134: if (k->rsa != NULL)
135: RSA_free(k->rsa);
136: k->rsa = NULL;
137: break;
138: case KEY_DSA:
139: if (k->dsa != NULL)
140: DSA_free(k->dsa);
141: k->dsa = NULL;
142: break;
1.12 markus 143: case KEY_UNSPEC:
144: break;
1.1 markus 145: default:
146: fatal("key_free: bad key type %d", k->type);
147: break;
148: }
149: xfree(k);
150: }
1.55 jakob 151:
1.1 markus 152: int
1.55 jakob 153: key_equal(const Key *a, const Key *b)
1.1 markus 154: {
155: if (a == NULL || b == NULL || a->type != b->type)
156: return 0;
157: switch (a->type) {
1.12 markus 158: case KEY_RSA1:
1.1 markus 159: case KEY_RSA:
160: return a->rsa != NULL && b->rsa != NULL &&
161: BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
162: BN_cmp(a->rsa->n, b->rsa->n) == 0;
163: case KEY_DSA:
164: return a->dsa != NULL && b->dsa != NULL &&
165: BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
166: BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
167: BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
168: BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
169: default:
1.3 markus 170: fatal("key_equal: bad key type %d", a->type);
1.1 markus 171: }
172: }
173:
1.52 jakob 174: u_char*
1.55 jakob 175: key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
176: u_int *dgst_raw_length)
1.1 markus 177: {
1.41 markus 178: const EVP_MD *md = NULL;
1.21 markus 179: EVP_MD_CTX ctx;
1.13 markus 180: u_char *blob = NULL;
1.19 jakob 181: u_char *retval = NULL;
1.40 markus 182: u_int len = 0;
1.3 markus 183: int nlen, elen;
1.1 markus 184:
1.19 jakob 185: *dgst_raw_length = 0;
186:
1.21 markus 187: switch (dgst_type) {
188: case SSH_FP_MD5:
189: md = EVP_md5();
190: break;
191: case SSH_FP_SHA1:
192: md = EVP_sha1();
193: break;
194: default:
195: fatal("key_fingerprint_raw: bad digest type %d",
196: dgst_type);
197: }
1.1 markus 198: switch (k->type) {
1.12 markus 199: case KEY_RSA1:
1.1 markus 200: nlen = BN_num_bytes(k->rsa->n);
201: elen = BN_num_bytes(k->rsa->e);
202: len = nlen + elen;
1.3 markus 203: blob = xmalloc(len);
204: BN_bn2bin(k->rsa->n, blob);
205: BN_bn2bin(k->rsa->e, blob + nlen);
1.1 markus 206: break;
207: case KEY_DSA:
1.12 markus 208: case KEY_RSA:
209: key_to_blob(k, &blob, &len);
210: break;
211: case KEY_UNSPEC:
212: return retval;
1.1 markus 213: default:
1.19 jakob 214: fatal("key_fingerprint_raw: bad key type %d", k->type);
1.1 markus 215: break;
216: }
1.3 markus 217: if (blob != NULL) {
1.19 jakob 218: retval = xmalloc(EVP_MAX_MD_SIZE);
1.8 markus 219: EVP_DigestInit(&ctx, md);
220: EVP_DigestUpdate(&ctx, blob, len);
1.39 markus 221: EVP_DigestFinal(&ctx, retval, dgst_raw_length);
1.3 markus 222: memset(blob, 0, len);
223: xfree(blob);
1.19 jakob 224: } else {
225: fatal("key_fingerprint_raw: blob is null");
1.1 markus 226: }
1.19 jakob 227: return retval;
228: }
229:
1.46 deraadt 230: static char *
231: key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
1.19 jakob 232: {
233: char *retval;
1.58 djm 234: u_int i;
1.19 jakob 235:
1.63 djm 236: retval = xcalloc(1, dgst_raw_len * 3 + 1);
1.36 deraadt 237: for (i = 0; i < dgst_raw_len; i++) {
1.19 jakob 238: char hex[4];
239: snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
1.54 avsm 240: strlcat(retval, hex, dgst_raw_len * 3 + 1);
1.19 jakob 241: }
1.54 avsm 242:
243: /* Remove the trailing ':' character */
1.19 jakob 244: retval[(dgst_raw_len * 3) - 1] = '\0';
245: return retval;
246: }
247:
1.46 deraadt 248: static char *
249: key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
1.19 jakob 250: {
251: char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
252: char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
253: 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
1.20 jakob 254: u_int i, j = 0, rounds, seed = 1;
1.19 jakob 255: char *retval;
256:
257: rounds = (dgst_raw_len / 2) + 1;
1.63 djm 258: retval = xcalloc((rounds * 6), sizeof(char));
1.20 jakob 259: retval[j++] = 'x';
260: for (i = 0; i < rounds; i++) {
1.19 jakob 261: u_int idx0, idx1, idx2, idx3, idx4;
1.20 jakob 262: if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
263: idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
1.19 jakob 264: seed) % 6;
1.20 jakob 265: idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
266: idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
1.19 jakob 267: (seed / 6)) % 6;
1.20 jakob 268: retval[j++] = vowels[idx0];
269: retval[j++] = consonants[idx1];
270: retval[j++] = vowels[idx2];
271: if ((i + 1) < rounds) {
272: idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
273: idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
274: retval[j++] = consonants[idx3];
275: retval[j++] = '-';
276: retval[j++] = consonants[idx4];
1.19 jakob 277: seed = ((seed * 5) +
1.20 jakob 278: ((((u_int)(dgst_raw[2 * i])) * 7) +
279: ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
1.19 jakob 280: }
281: } else {
282: idx0 = seed % 6;
283: idx1 = 16;
284: idx2 = seed / 6;
1.20 jakob 285: retval[j++] = vowels[idx0];
286: retval[j++] = consonants[idx1];
287: retval[j++] = vowels[idx2];
1.19 jakob 288: }
289: }
1.20 jakob 290: retval[j++] = 'x';
291: retval[j++] = '\0';
1.19 jakob 292: return retval;
293: }
294:
1.70 grunk 295: /*
296: * Draw an ASCII-Art representing the fingerprint so human brain can
297: * profit from its built-in pattern recognition ability.
298: * This technique is called "random art" and can be found in some
299: * scientific publications like this original paper:
300: *
301: * "Hash Visualization: a New Technique to improve Real-World Security",
302: * Perrig A. and Song D., 1999, International Workshop on Cryptographic
303: * Techniques and E-Commerce (CrypTEC '99)
304: * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
305: *
306: * The subject came up in a talk by Dan Kaminsky, too.
307: *
308: * If you see the picture is different, the key is different.
309: * If the picture looks the same, you still know nothing.
310: *
311: * The algorithm used here is a worm crawling over a discrete plane,
312: * leaving a trace (augmenting the field) everywhere it goes.
313: * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
314: * makes the respective movement vector be ignored for this turn.
315: * Graphs are not unambiguous, because circles in graphs can be
316: * walked in either direction.
317: */
318: #define FLDSIZE_Y 8
319: #define FLDSIZE_X FLDSIZE_Y * 2
320: static char *
321: key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len)
322: {
323: /*
324: * Chars to be used after each other every time the worm
325: * intersects with itself. Matter of taste.
326: */
327: char *augmentation_string = " .o+=*BOX@%&#/^";
328: char *retval, *p;
1.71 ! otto 329: u_char field[FLDSIZE_X][FLDSIZE_Y];
1.70 grunk 330: u_int i, b;
331: int x, y;
1.71 ! otto 332: size_t len = strlen(augmentation_string);
1.70 grunk 333:
334: retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
335:
336: /* initialize field */
1.71 ! otto 337: memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1.70 grunk 338: x = FLDSIZE_X / 2;
339: y = FLDSIZE_Y / 2;
1.71 ! otto 340: field[x][y] = 1;
1.70 grunk 341:
342: /* process raw key */
343: for (i = 0; i < dgst_raw_len; i++) {
344: int input;
345: /* each byte conveys four 2-bit move commands */
346: input = dgst_raw[i];
347: for (b = 0; b < 4; b++) {
348: /* evaluate 2 bit, rest is shifted later */
349: x += (input & 0x1) ? 1 : -1;
350: y += (input & 0x2) ? 1 : -1;
351:
352: /* assure we are still in bounds */
353: x = MAX(x, 0);
354: y = MAX(y, 0);
355: x = MIN(x, FLDSIZE_X - 1);
356: y = MIN(y, FLDSIZE_Y - 1);
357:
358: /* augment the field */
1.71 ! otto 359: field[x][y]++;
1.70 grunk 360: input = input >> 2;
361: }
362: }
363:
364: /* fill in retval */
365: p = retval;
366:
367: /* output upper border */
368: *p++ = '+';
369: for (i = 0; i < FLDSIZE_X; i++)
370: *p++ = '-';
371: *p++ = '+';
372: *p++ = '\n';
373:
374: /* output content */
375: for (y = 0; y < FLDSIZE_Y; y++) {
376: *p++ = '|';
377: for (x = 0; x < FLDSIZE_X; x++)
1.71 ! otto 378: *p++ = augmentation_string[MIN(field[x][y], len - 1)];
1.70 grunk 379: *p++ = '|';
380: *p++ = '\n';
381: }
382:
383: /* output lower border */
384: *p++ = '+';
385: for (i = 0; i < FLDSIZE_X; i++)
386: *p++ = '-';
387: *p++ = '+';
388:
389: return retval;
390: }
391:
1.46 deraadt 392: char *
1.55 jakob 393: key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
1.19 jakob 394: {
1.23 markus 395: char *retval = NULL;
1.19 jakob 396: u_char *dgst_raw;
1.39 markus 397: u_int dgst_raw_len;
1.36 deraadt 398:
1.19 jakob 399: dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
400: if (!dgst_raw)
1.22 markus 401: fatal("key_fingerprint: null from key_fingerprint_raw()");
1.35 deraadt 402: switch (dgst_rep) {
1.19 jakob 403: case SSH_FP_HEX:
404: retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
405: break;
406: case SSH_FP_BUBBLEBABBLE:
407: retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1.70 grunk 408: break;
409: case SSH_FP_RANDOMART:
410: retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len);
1.19 jakob 411: break;
412: default:
413: fatal("key_fingerprint_ex: bad digest representation %d",
414: dgst_rep);
415: break;
416: }
417: memset(dgst_raw, 0, dgst_raw_len);
418: xfree(dgst_raw);
1.1 markus 419: return retval;
420: }
421:
422: /*
423: * Reads a multiple-precision integer in decimal from the buffer, and advances
424: * the pointer. The integer must already be initialized. This function is
425: * permitted to modify the buffer. This leaves *cpp to point just beyond the
426: * last processed (and maybe modified) character. Note that this may modify
427: * the buffer containing the number.
428: */
1.27 itojun 429: static int
1.1 markus 430: read_bignum(char **cpp, BIGNUM * value)
431: {
432: char *cp = *cpp;
433: int old;
434:
435: /* Skip any leading whitespace. */
436: for (; *cp == ' ' || *cp == '\t'; cp++)
437: ;
438:
439: /* Check that it begins with a decimal digit. */
440: if (*cp < '0' || *cp > '9')
441: return 0;
442:
443: /* Save starting position. */
444: *cpp = cp;
445:
446: /* Move forward until all decimal digits skipped. */
447: for (; *cp >= '0' && *cp <= '9'; cp++)
448: ;
449:
450: /* Save the old terminating character, and replace it by \0. */
451: old = *cp;
452: *cp = 0;
453:
454: /* Parse the number. */
455: if (BN_dec2bn(&value, *cpp) == 0)
456: return 0;
457:
458: /* Restore old terminating character. */
459: *cp = old;
460:
461: /* Move beyond the number and return success. */
462: *cpp = cp;
463: return 1;
464: }
1.45 deraadt 465:
1.27 itojun 466: static int
1.1 markus 467: write_bignum(FILE *f, BIGNUM *num)
468: {
469: char *buf = BN_bn2dec(num);
470: if (buf == NULL) {
471: error("write_bignum: BN_bn2dec() failed");
472: return 0;
473: }
474: fprintf(f, " %s", buf);
1.33 markus 475: OPENSSL_free(buf);
1.1 markus 476: return 1;
477: }
1.12 markus 478:
1.32 markus 479: /* returns 1 ok, -1 error */
1.12 markus 480: int
1.3 markus 481: key_read(Key *ret, char **cpp)
1.1 markus 482: {
1.3 markus 483: Key *k;
1.12 markus 484: int success = -1;
485: char *cp, *space;
486: int len, n, type;
487: u_int bits;
1.13 markus 488: u_char *blob;
1.3 markus 489:
490: cp = *cpp;
491:
1.35 deraadt 492: switch (ret->type) {
1.12 markus 493: case KEY_RSA1:
1.3 markus 494: /* Get number of bits. */
495: if (*cp < '0' || *cp > '9')
1.12 markus 496: return -1; /* Bad bit count... */
1.3 markus 497: for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
498: bits = 10 * bits + *cp - '0';
1.1 markus 499: if (bits == 0)
1.12 markus 500: return -1;
1.3 markus 501: *cpp = cp;
1.1 markus 502: /* Get public exponent, public modulus. */
503: if (!read_bignum(cpp, ret->rsa->e))
1.12 markus 504: return -1;
1.1 markus 505: if (!read_bignum(cpp, ret->rsa->n))
1.12 markus 506: return -1;
507: success = 1;
1.1 markus 508: break;
1.12 markus 509: case KEY_UNSPEC:
510: case KEY_RSA:
1.1 markus 511: case KEY_DSA:
1.12 markus 512: space = strchr(cp, ' ');
513: if (space == NULL) {
1.50 markus 514: debug3("key_read: missing whitespace");
1.12 markus 515: return -1;
516: }
517: *space = '\0';
518: type = key_type_from_name(cp);
519: *space = ' ';
520: if (type == KEY_UNSPEC) {
1.50 markus 521: debug3("key_read: missing keytype");
1.12 markus 522: return -1;
523: }
524: cp = space+1;
525: if (*cp == '\0') {
526: debug3("key_read: short string");
527: return -1;
528: }
529: if (ret->type == KEY_UNSPEC) {
530: ret->type = type;
531: } else if (ret->type != type) {
532: /* is a key, but different type */
533: debug3("key_read: type mismatch");
1.32 markus 534: return -1;
1.12 markus 535: }
1.3 markus 536: len = 2*strlen(cp);
537: blob = xmalloc(len);
538: n = uudecode(cp, blob, len);
1.6 markus 539: if (n < 0) {
1.7 markus 540: error("key_read: uudecode %s failed", cp);
1.34 markus 541: xfree(blob);
1.12 markus 542: return -1;
1.6 markus 543: }
1.53 markus 544: k = key_from_blob(blob, (u_int)n);
1.34 markus 545: xfree(blob);
1.7 markus 546: if (k == NULL) {
1.12 markus 547: error("key_read: key_from_blob %s failed", cp);
548: return -1;
1.7 markus 549: }
1.12 markus 550: if (k->type != type) {
551: error("key_read: type mismatch: encoding error");
552: key_free(k);
553: return -1;
554: }
555: /*XXXX*/
556: if (ret->type == KEY_RSA) {
557: if (ret->rsa != NULL)
558: RSA_free(ret->rsa);
559: ret->rsa = k->rsa;
560: k->rsa = NULL;
561: success = 1;
562: #ifdef DEBUG_PK
563: RSA_print_fp(stderr, ret->rsa, 8);
564: #endif
565: } else {
566: if (ret->dsa != NULL)
567: DSA_free(ret->dsa);
568: ret->dsa = k->dsa;
569: k->dsa = NULL;
570: success = 1;
571: #ifdef DEBUG_PK
572: DSA_print_fp(stderr, ret->dsa, 8);
573: #endif
574: }
575: /*XXXX*/
1.34 markus 576: key_free(k);
1.12 markus 577: if (success != 1)
578: break;
1.7 markus 579: /* advance cp: skip whitespace and data */
580: while (*cp == ' ' || *cp == '\t')
581: cp++;
582: while (*cp != '\0' && *cp != ' ' && *cp != '\t')
583: cp++;
584: *cpp = cp;
1.1 markus 585: break;
586: default:
1.3 markus 587: fatal("key_read: bad key type: %d", ret->type);
1.1 markus 588: break;
589: }
1.12 markus 590: return success;
1.1 markus 591: }
1.45 deraadt 592:
1.1 markus 593: int
1.55 jakob 594: key_write(const Key *key, FILE *f)
1.1 markus 595: {
1.40 markus 596: int n, success = 0;
597: u_int len, bits = 0;
1.49 markus 598: u_char *blob;
599: char *uu;
1.1 markus 600:
1.12 markus 601: if (key->type == KEY_RSA1 && key->rsa != NULL) {
1.1 markus 602: /* size of modulus 'n' */
603: bits = BN_num_bits(key->rsa->n);
604: fprintf(f, "%u", bits);
605: if (write_bignum(f, key->rsa->e) &&
606: write_bignum(f, key->rsa->n)) {
607: success = 1;
608: } else {
609: error("key_write: failed for RSA key");
610: }
1.12 markus 611: } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
612: (key->type == KEY_RSA && key->rsa != NULL)) {
613: key_to_blob(key, &blob, &len);
1.3 markus 614: uu = xmalloc(2*len);
1.5 markus 615: n = uuencode(blob, len, uu, 2*len);
616: if (n > 0) {
1.12 markus 617: fprintf(f, "%s %s", key_ssh_name(key), uu);
1.5 markus 618: success = 1;
619: }
1.3 markus 620: xfree(blob);
621: xfree(uu);
1.1 markus 622: }
623: return success;
624: }
1.45 deraadt 625:
1.55 jakob 626: const char *
627: key_type(const Key *k)
1.4 markus 628: {
629: switch (k->type) {
1.12 markus 630: case KEY_RSA1:
631: return "RSA1";
1.4 markus 632: case KEY_RSA:
633: return "RSA";
634: case KEY_DSA:
635: return "DSA";
636: }
637: return "unknown";
1.10 markus 638: }
1.45 deraadt 639:
1.55 jakob 640: const char *
641: key_ssh_name(const Key *k)
1.12 markus 642: {
643: switch (k->type) {
644: case KEY_RSA:
645: return "ssh-rsa";
646: case KEY_DSA:
647: return "ssh-dss";
648: }
649: return "ssh-unknown";
650: }
1.45 deraadt 651:
1.12 markus 652: u_int
1.55 jakob 653: key_size(const Key *k)
1.35 deraadt 654: {
1.10 markus 655: switch (k->type) {
1.12 markus 656: case KEY_RSA1:
1.10 markus 657: case KEY_RSA:
658: return BN_num_bits(k->rsa->n);
659: case KEY_DSA:
660: return BN_num_bits(k->dsa->p);
661: }
662: return 0;
1.12 markus 663: }
664:
1.27 itojun 665: static RSA *
1.13 markus 666: rsa_generate_private_key(u_int bits)
1.12 markus 667: {
1.17 stevesk 668: RSA *private;
1.61 deraadt 669:
1.17 stevesk 670: private = RSA_generate_key(bits, 35, NULL, NULL);
671: if (private == NULL)
672: fatal("rsa_generate_private_key: key generation failed.");
673: return private;
1.12 markus 674: }
675:
1.27 itojun 676: static DSA*
1.13 markus 677: dsa_generate_private_key(u_int bits)
1.12 markus 678: {
679: DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
1.61 deraadt 680:
1.12 markus 681: if (private == NULL)
682: fatal("dsa_generate_private_key: DSA_generate_parameters failed");
683: if (!DSA_generate_key(private))
1.17 stevesk 684: fatal("dsa_generate_private_key: DSA_generate_key failed.");
685: if (private == NULL)
686: fatal("dsa_generate_private_key: NULL.");
1.12 markus 687: return private;
688: }
689:
690: Key *
1.13 markus 691: key_generate(int type, u_int bits)
1.12 markus 692: {
693: Key *k = key_new(KEY_UNSPEC);
694: switch (type) {
1.17 stevesk 695: case KEY_DSA:
1.12 markus 696: k->dsa = dsa_generate_private_key(bits);
697: break;
698: case KEY_RSA:
699: case KEY_RSA1:
700: k->rsa = rsa_generate_private_key(bits);
701: break;
702: default:
1.17 stevesk 703: fatal("key_generate: unknown type %d", type);
1.12 markus 704: }
1.17 stevesk 705: k->type = type;
1.12 markus 706: return k;
707: }
708:
709: Key *
1.55 jakob 710: key_from_private(const Key *k)
1.12 markus 711: {
712: Key *n = NULL;
713: switch (k->type) {
1.17 stevesk 714: case KEY_DSA:
1.12 markus 715: n = key_new(k->type);
1.68 markus 716: if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
717: (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
718: (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
719: (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
720: fatal("key_from_private: BN_copy failed");
1.12 markus 721: break;
722: case KEY_RSA:
723: case KEY_RSA1:
724: n = key_new(k->type);
1.68 markus 725: if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
726: (BN_copy(n->rsa->e, k->rsa->e) == NULL))
727: fatal("key_from_private: BN_copy failed");
1.12 markus 728: break;
729: default:
1.17 stevesk 730: fatal("key_from_private: unknown type %d", k->type);
1.12 markus 731: break;
732: }
733: return n;
734: }
735:
736: int
737: key_type_from_name(char *name)
738: {
1.35 deraadt 739: if (strcmp(name, "rsa1") == 0) {
1.12 markus 740: return KEY_RSA1;
1.35 deraadt 741: } else if (strcmp(name, "rsa") == 0) {
1.12 markus 742: return KEY_RSA;
1.35 deraadt 743: } else if (strcmp(name, "dsa") == 0) {
1.12 markus 744: return KEY_DSA;
1.35 deraadt 745: } else if (strcmp(name, "ssh-rsa") == 0) {
1.12 markus 746: return KEY_RSA;
1.35 deraadt 747: } else if (strcmp(name, "ssh-dss") == 0) {
1.12 markus 748: return KEY_DSA;
749: }
1.18 markus 750: debug2("key_type_from_name: unknown key type '%s'", name);
1.12 markus 751: return KEY_UNSPEC;
1.25 markus 752: }
753:
754: int
755: key_names_valid2(const char *names)
756: {
757: char *s, *cp, *p;
758:
759: if (names == NULL || strcmp(names, "") == 0)
760: return 0;
761: s = cp = xstrdup(names);
762: for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36 deraadt 763: (p = strsep(&cp, ","))) {
1.25 markus 764: switch (key_type_from_name(p)) {
765: case KEY_RSA1:
766: case KEY_UNSPEC:
767: xfree(s);
768: return 0;
769: }
770: }
771: debug3("key names ok: [%s]", names);
772: xfree(s);
773: return 1;
1.12 markus 774: }
775:
776: Key *
1.55 jakob 777: key_from_blob(const u_char *blob, u_int blen)
1.12 markus 778: {
779: Buffer b;
780: int rlen, type;
1.57 djm 781: char *ktype = NULL;
1.12 markus 782: Key *key = NULL;
783:
784: #ifdef DEBUG_PK
785: dump_base64(stderr, blob, blen);
786: #endif
787: buffer_init(&b);
788: buffer_append(&b, blob, blen);
1.57 djm 789: if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
790: error("key_from_blob: can't read key type");
791: goto out;
792: }
793:
1.12 markus 794: type = key_type_from_name(ktype);
795:
1.35 deraadt 796: switch (type) {
1.12 markus 797: case KEY_RSA:
798: key = key_new(type);
1.57 djm 799: if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
800: buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
801: error("key_from_blob: can't read rsa key");
802: key_free(key);
803: key = NULL;
804: goto out;
805: }
1.12 markus 806: #ifdef DEBUG_PK
807: RSA_print_fp(stderr, key->rsa, 8);
808: #endif
809: break;
810: case KEY_DSA:
811: key = key_new(type);
1.57 djm 812: if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
813: buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
814: buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
815: buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
816: error("key_from_blob: can't read dsa key");
817: key_free(key);
818: key = NULL;
819: goto out;
820: }
1.12 markus 821: #ifdef DEBUG_PK
822: DSA_print_fp(stderr, key->dsa, 8);
823: #endif
824: break;
825: case KEY_UNSPEC:
826: key = key_new(type);
827: break;
828: default:
829: error("key_from_blob: cannot handle type %s", ktype);
1.57 djm 830: goto out;
1.12 markus 831: }
832: rlen = buffer_len(&b);
833: if (key != NULL && rlen != 0)
834: error("key_from_blob: remaining bytes in key blob %d", rlen);
1.57 djm 835: out:
836: if (ktype != NULL)
837: xfree(ktype);
1.12 markus 838: buffer_free(&b);
839: return key;
840: }
841:
842: int
1.55 jakob 843: key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1.12 markus 844: {
845: Buffer b;
846: int len;
847:
848: if (key == NULL) {
849: error("key_to_blob: key == NULL");
850: return 0;
851: }
852: buffer_init(&b);
1.35 deraadt 853: switch (key->type) {
1.12 markus 854: case KEY_DSA:
855: buffer_put_cstring(&b, key_ssh_name(key));
856: buffer_put_bignum2(&b, key->dsa->p);
857: buffer_put_bignum2(&b, key->dsa->q);
858: buffer_put_bignum2(&b, key->dsa->g);
859: buffer_put_bignum2(&b, key->dsa->pub_key);
860: break;
861: case KEY_RSA:
862: buffer_put_cstring(&b, key_ssh_name(key));
1.14 markus 863: buffer_put_bignum2(&b, key->rsa->e);
1.12 markus 864: buffer_put_bignum2(&b, key->rsa->n);
865: break;
866: default:
1.31 markus 867: error("key_to_blob: unsupported key type %d", key->type);
868: buffer_free(&b);
869: return 0;
1.12 markus 870: }
871: len = buffer_len(&b);
1.48 markus 872: if (lenp != NULL)
873: *lenp = len;
874: if (blobp != NULL) {
875: *blobp = xmalloc(len);
876: memcpy(*blobp, buffer_ptr(&b), len);
877: }
1.12 markus 878: memset(buffer_ptr(&b), 0, len);
879: buffer_free(&b);
880: return len;
881: }
882:
883: int
884: key_sign(
1.55 jakob 885: const Key *key,
1.40 markus 886: u_char **sigp, u_int *lenp,
1.55 jakob 887: const u_char *data, u_int datalen)
1.12 markus 888: {
1.35 deraadt 889: switch (key->type) {
1.12 markus 890: case KEY_DSA:
891: return ssh_dss_sign(key, sigp, lenp, data, datalen);
892: case KEY_RSA:
893: return ssh_rsa_sign(key, sigp, lenp, data, datalen);
894: default:
1.56 markus 895: error("key_sign: invalid key type %d", key->type);
1.12 markus 896: return -1;
897: }
898: }
899:
1.44 markus 900: /*
901: * key_verify returns 1 for a correct signature, 0 for an incorrect signature
902: * and -1 on error.
903: */
1.12 markus 904: int
905: key_verify(
1.55 jakob 906: const Key *key,
907: const u_char *signature, u_int signaturelen,
908: const u_char *data, u_int datalen)
1.12 markus 909: {
1.26 markus 910: if (signaturelen == 0)
911: return -1;
912:
1.35 deraadt 913: switch (key->type) {
1.12 markus 914: case KEY_DSA:
915: return ssh_dss_verify(key, signature, signaturelen, data, datalen);
916: case KEY_RSA:
917: return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
918: default:
1.56 markus 919: error("key_verify: invalid key type %d", key->type);
1.12 markus 920: return -1;
921: }
1.42 markus 922: }
923:
924: /* Converts a private to a public key */
925: Key *
1.55 jakob 926: key_demote(const Key *k)
1.42 markus 927: {
928: Key *pk;
1.43 markus 929:
1.63 djm 930: pk = xcalloc(1, sizeof(*pk));
1.42 markus 931: pk->type = k->type;
932: pk->flags = k->flags;
933: pk->dsa = NULL;
934: pk->rsa = NULL;
935:
936: switch (k->type) {
937: case KEY_RSA1:
938: case KEY_RSA:
939: if ((pk->rsa = RSA_new()) == NULL)
940: fatal("key_demote: RSA_new failed");
941: if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
942: fatal("key_demote: BN_dup failed");
943: if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
944: fatal("key_demote: BN_dup failed");
945: break;
946: case KEY_DSA:
947: if ((pk->dsa = DSA_new()) == NULL)
948: fatal("key_demote: DSA_new failed");
949: if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
950: fatal("key_demote: BN_dup failed");
951: if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
952: fatal("key_demote: BN_dup failed");
953: if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
954: fatal("key_demote: BN_dup failed");
955: if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
956: fatal("key_demote: BN_dup failed");
957: break;
958: default:
959: fatal("key_free: bad key type %d", k->type);
960: break;
961: }
962:
963: return (pk);
1.4 markus 964: }