Annotation of src/usr.bin/ssh/key.c, Revision 1.70
1.70 ! grunk 1: /* $OpenBSD: key.c,v 1.69 2007/07/12 05:48:05 ray 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;
! 329: char field[FLDSIZE_X][FLDSIZE_Y];
! 330: u_int i, b;
! 331: int x, y;
! 332:
! 333: retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
! 334:
! 335: /* initialize field */
! 336: memset(field, ' ', FLDSIZE_X * FLDSIZE_Y * sizeof(char));
! 337: x = FLDSIZE_X / 2;
! 338: y = FLDSIZE_Y / 2;
! 339: field[x][y] = '.';
! 340:
! 341: /* process raw key */
! 342: for (i = 0; i < dgst_raw_len; i++) {
! 343: int input;
! 344: /* each byte conveys four 2-bit move commands */
! 345: input = dgst_raw[i];
! 346: for (b = 0; b < 4; b++) {
! 347: /* evaluate 2 bit, rest is shifted later */
! 348: x += (input & 0x1) ? 1 : -1;
! 349: y += (input & 0x2) ? 1 : -1;
! 350:
! 351: /* assure we are still in bounds */
! 352: x = MAX(x, 0);
! 353: y = MAX(y, 0);
! 354: x = MIN(x, FLDSIZE_X - 1);
! 355: y = MIN(y, FLDSIZE_Y - 1);
! 356:
! 357: /* augment the field */
! 358: p = strchr(augmentation_string, field[x][y]);
! 359: if (*++p != '\0')
! 360: field[x][y] = *p;
! 361:
! 362: input = input >> 2;
! 363: }
! 364: }
! 365:
! 366: /* fill in retval */
! 367: p = retval;
! 368:
! 369: /* output upper border */
! 370: *p++ = '+';
! 371: for (i = 0; i < FLDSIZE_X; i++)
! 372: *p++ = '-';
! 373: *p++ = '+';
! 374: *p++ = '\n';
! 375:
! 376: /* output content */
! 377: for (y = 0; y < FLDSIZE_Y; y++) {
! 378: *p++ = '|';
! 379: for (x = 0; x < FLDSIZE_X; x++)
! 380: *p++ = field[x][y];
! 381: *p++ = '|';
! 382: *p++ = '\n';
! 383: }
! 384:
! 385: /* output lower border */
! 386: *p++ = '+';
! 387: for (i = 0; i < FLDSIZE_X; i++)
! 388: *p++ = '-';
! 389: *p++ = '+';
! 390:
! 391: return retval;
! 392: }
! 393:
1.46 deraadt 394: char *
1.55 jakob 395: key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
1.19 jakob 396: {
1.23 markus 397: char *retval = NULL;
1.19 jakob 398: u_char *dgst_raw;
1.39 markus 399: u_int dgst_raw_len;
1.36 deraadt 400:
1.19 jakob 401: dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
402: if (!dgst_raw)
1.22 markus 403: fatal("key_fingerprint: null from key_fingerprint_raw()");
1.35 deraadt 404: switch (dgst_rep) {
1.19 jakob 405: case SSH_FP_HEX:
406: retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
407: break;
408: case SSH_FP_BUBBLEBABBLE:
409: retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1.70 ! grunk 410: break;
! 411: case SSH_FP_RANDOMART:
! 412: retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len);
1.19 jakob 413: break;
414: default:
415: fatal("key_fingerprint_ex: bad digest representation %d",
416: dgst_rep);
417: break;
418: }
419: memset(dgst_raw, 0, dgst_raw_len);
420: xfree(dgst_raw);
1.1 markus 421: return retval;
422: }
423:
424: /*
425: * Reads a multiple-precision integer in decimal from the buffer, and advances
426: * the pointer. The integer must already be initialized. This function is
427: * permitted to modify the buffer. This leaves *cpp to point just beyond the
428: * last processed (and maybe modified) character. Note that this may modify
429: * the buffer containing the number.
430: */
1.27 itojun 431: static int
1.1 markus 432: read_bignum(char **cpp, BIGNUM * value)
433: {
434: char *cp = *cpp;
435: int old;
436:
437: /* Skip any leading whitespace. */
438: for (; *cp == ' ' || *cp == '\t'; cp++)
439: ;
440:
441: /* Check that it begins with a decimal digit. */
442: if (*cp < '0' || *cp > '9')
443: return 0;
444:
445: /* Save starting position. */
446: *cpp = cp;
447:
448: /* Move forward until all decimal digits skipped. */
449: for (; *cp >= '0' && *cp <= '9'; cp++)
450: ;
451:
452: /* Save the old terminating character, and replace it by \0. */
453: old = *cp;
454: *cp = 0;
455:
456: /* Parse the number. */
457: if (BN_dec2bn(&value, *cpp) == 0)
458: return 0;
459:
460: /* Restore old terminating character. */
461: *cp = old;
462:
463: /* Move beyond the number and return success. */
464: *cpp = cp;
465: return 1;
466: }
1.45 deraadt 467:
1.27 itojun 468: static int
1.1 markus 469: write_bignum(FILE *f, BIGNUM *num)
470: {
471: char *buf = BN_bn2dec(num);
472: if (buf == NULL) {
473: error("write_bignum: BN_bn2dec() failed");
474: return 0;
475: }
476: fprintf(f, " %s", buf);
1.33 markus 477: OPENSSL_free(buf);
1.1 markus 478: return 1;
479: }
1.12 markus 480:
1.32 markus 481: /* returns 1 ok, -1 error */
1.12 markus 482: int
1.3 markus 483: key_read(Key *ret, char **cpp)
1.1 markus 484: {
1.3 markus 485: Key *k;
1.12 markus 486: int success = -1;
487: char *cp, *space;
488: int len, n, type;
489: u_int bits;
1.13 markus 490: u_char *blob;
1.3 markus 491:
492: cp = *cpp;
493:
1.35 deraadt 494: switch (ret->type) {
1.12 markus 495: case KEY_RSA1:
1.3 markus 496: /* Get number of bits. */
497: if (*cp < '0' || *cp > '9')
1.12 markus 498: return -1; /* Bad bit count... */
1.3 markus 499: for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
500: bits = 10 * bits + *cp - '0';
1.1 markus 501: if (bits == 0)
1.12 markus 502: return -1;
1.3 markus 503: *cpp = cp;
1.1 markus 504: /* Get public exponent, public modulus. */
505: if (!read_bignum(cpp, ret->rsa->e))
1.12 markus 506: return -1;
1.1 markus 507: if (!read_bignum(cpp, ret->rsa->n))
1.12 markus 508: return -1;
509: success = 1;
1.1 markus 510: break;
1.12 markus 511: case KEY_UNSPEC:
512: case KEY_RSA:
1.1 markus 513: case KEY_DSA:
1.12 markus 514: space = strchr(cp, ' ');
515: if (space == NULL) {
1.50 markus 516: debug3("key_read: missing whitespace");
1.12 markus 517: return -1;
518: }
519: *space = '\0';
520: type = key_type_from_name(cp);
521: *space = ' ';
522: if (type == KEY_UNSPEC) {
1.50 markus 523: debug3("key_read: missing keytype");
1.12 markus 524: return -1;
525: }
526: cp = space+1;
527: if (*cp == '\0') {
528: debug3("key_read: short string");
529: return -1;
530: }
531: if (ret->type == KEY_UNSPEC) {
532: ret->type = type;
533: } else if (ret->type != type) {
534: /* is a key, but different type */
535: debug3("key_read: type mismatch");
1.32 markus 536: return -1;
1.12 markus 537: }
1.3 markus 538: len = 2*strlen(cp);
539: blob = xmalloc(len);
540: n = uudecode(cp, blob, len);
1.6 markus 541: if (n < 0) {
1.7 markus 542: error("key_read: uudecode %s failed", cp);
1.34 markus 543: xfree(blob);
1.12 markus 544: return -1;
1.6 markus 545: }
1.53 markus 546: k = key_from_blob(blob, (u_int)n);
1.34 markus 547: xfree(blob);
1.7 markus 548: if (k == NULL) {
1.12 markus 549: error("key_read: key_from_blob %s failed", cp);
550: return -1;
1.7 markus 551: }
1.12 markus 552: if (k->type != type) {
553: error("key_read: type mismatch: encoding error");
554: key_free(k);
555: return -1;
556: }
557: /*XXXX*/
558: if (ret->type == KEY_RSA) {
559: if (ret->rsa != NULL)
560: RSA_free(ret->rsa);
561: ret->rsa = k->rsa;
562: k->rsa = NULL;
563: success = 1;
564: #ifdef DEBUG_PK
565: RSA_print_fp(stderr, ret->rsa, 8);
566: #endif
567: } else {
568: if (ret->dsa != NULL)
569: DSA_free(ret->dsa);
570: ret->dsa = k->dsa;
571: k->dsa = NULL;
572: success = 1;
573: #ifdef DEBUG_PK
574: DSA_print_fp(stderr, ret->dsa, 8);
575: #endif
576: }
577: /*XXXX*/
1.34 markus 578: key_free(k);
1.12 markus 579: if (success != 1)
580: break;
1.7 markus 581: /* advance cp: skip whitespace and data */
582: while (*cp == ' ' || *cp == '\t')
583: cp++;
584: while (*cp != '\0' && *cp != ' ' && *cp != '\t')
585: cp++;
586: *cpp = cp;
1.1 markus 587: break;
588: default:
1.3 markus 589: fatal("key_read: bad key type: %d", ret->type);
1.1 markus 590: break;
591: }
1.12 markus 592: return success;
1.1 markus 593: }
1.45 deraadt 594:
1.1 markus 595: int
1.55 jakob 596: key_write(const Key *key, FILE *f)
1.1 markus 597: {
1.40 markus 598: int n, success = 0;
599: u_int len, bits = 0;
1.49 markus 600: u_char *blob;
601: char *uu;
1.1 markus 602:
1.12 markus 603: if (key->type == KEY_RSA1 && key->rsa != NULL) {
1.1 markus 604: /* size of modulus 'n' */
605: bits = BN_num_bits(key->rsa->n);
606: fprintf(f, "%u", bits);
607: if (write_bignum(f, key->rsa->e) &&
608: write_bignum(f, key->rsa->n)) {
609: success = 1;
610: } else {
611: error("key_write: failed for RSA key");
612: }
1.12 markus 613: } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
614: (key->type == KEY_RSA && key->rsa != NULL)) {
615: key_to_blob(key, &blob, &len);
1.3 markus 616: uu = xmalloc(2*len);
1.5 markus 617: n = uuencode(blob, len, uu, 2*len);
618: if (n > 0) {
1.12 markus 619: fprintf(f, "%s %s", key_ssh_name(key), uu);
1.5 markus 620: success = 1;
621: }
1.3 markus 622: xfree(blob);
623: xfree(uu);
1.1 markus 624: }
625: return success;
626: }
1.45 deraadt 627:
1.55 jakob 628: const char *
629: key_type(const Key *k)
1.4 markus 630: {
631: switch (k->type) {
1.12 markus 632: case KEY_RSA1:
633: return "RSA1";
1.4 markus 634: case KEY_RSA:
635: return "RSA";
636: case KEY_DSA:
637: return "DSA";
638: }
639: return "unknown";
1.10 markus 640: }
1.45 deraadt 641:
1.55 jakob 642: const char *
643: key_ssh_name(const Key *k)
1.12 markus 644: {
645: switch (k->type) {
646: case KEY_RSA:
647: return "ssh-rsa";
648: case KEY_DSA:
649: return "ssh-dss";
650: }
651: return "ssh-unknown";
652: }
1.45 deraadt 653:
1.12 markus 654: u_int
1.55 jakob 655: key_size(const Key *k)
1.35 deraadt 656: {
1.10 markus 657: switch (k->type) {
1.12 markus 658: case KEY_RSA1:
1.10 markus 659: case KEY_RSA:
660: return BN_num_bits(k->rsa->n);
661: case KEY_DSA:
662: return BN_num_bits(k->dsa->p);
663: }
664: return 0;
1.12 markus 665: }
666:
1.27 itojun 667: static RSA *
1.13 markus 668: rsa_generate_private_key(u_int bits)
1.12 markus 669: {
1.17 stevesk 670: RSA *private;
1.61 deraadt 671:
1.17 stevesk 672: private = RSA_generate_key(bits, 35, NULL, NULL);
673: if (private == NULL)
674: fatal("rsa_generate_private_key: key generation failed.");
675: return private;
1.12 markus 676: }
677:
1.27 itojun 678: static DSA*
1.13 markus 679: dsa_generate_private_key(u_int bits)
1.12 markus 680: {
681: DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
1.61 deraadt 682:
1.12 markus 683: if (private == NULL)
684: fatal("dsa_generate_private_key: DSA_generate_parameters failed");
685: if (!DSA_generate_key(private))
1.17 stevesk 686: fatal("dsa_generate_private_key: DSA_generate_key failed.");
687: if (private == NULL)
688: fatal("dsa_generate_private_key: NULL.");
1.12 markus 689: return private;
690: }
691:
692: Key *
1.13 markus 693: key_generate(int type, u_int bits)
1.12 markus 694: {
695: Key *k = key_new(KEY_UNSPEC);
696: switch (type) {
1.17 stevesk 697: case KEY_DSA:
1.12 markus 698: k->dsa = dsa_generate_private_key(bits);
699: break;
700: case KEY_RSA:
701: case KEY_RSA1:
702: k->rsa = rsa_generate_private_key(bits);
703: break;
704: default:
1.17 stevesk 705: fatal("key_generate: unknown type %d", type);
1.12 markus 706: }
1.17 stevesk 707: k->type = type;
1.12 markus 708: return k;
709: }
710:
711: Key *
1.55 jakob 712: key_from_private(const Key *k)
1.12 markus 713: {
714: Key *n = NULL;
715: switch (k->type) {
1.17 stevesk 716: case KEY_DSA:
1.12 markus 717: n = key_new(k->type);
1.68 markus 718: if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
719: (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
720: (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
721: (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
722: fatal("key_from_private: BN_copy failed");
1.12 markus 723: break;
724: case KEY_RSA:
725: case KEY_RSA1:
726: n = key_new(k->type);
1.68 markus 727: if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
728: (BN_copy(n->rsa->e, k->rsa->e) == NULL))
729: fatal("key_from_private: BN_copy failed");
1.12 markus 730: break;
731: default:
1.17 stevesk 732: fatal("key_from_private: unknown type %d", k->type);
1.12 markus 733: break;
734: }
735: return n;
736: }
737:
738: int
739: key_type_from_name(char *name)
740: {
1.35 deraadt 741: if (strcmp(name, "rsa1") == 0) {
1.12 markus 742: return KEY_RSA1;
1.35 deraadt 743: } else if (strcmp(name, "rsa") == 0) {
1.12 markus 744: return KEY_RSA;
1.35 deraadt 745: } else if (strcmp(name, "dsa") == 0) {
1.12 markus 746: return KEY_DSA;
1.35 deraadt 747: } else if (strcmp(name, "ssh-rsa") == 0) {
1.12 markus 748: return KEY_RSA;
1.35 deraadt 749: } else if (strcmp(name, "ssh-dss") == 0) {
1.12 markus 750: return KEY_DSA;
751: }
1.18 markus 752: debug2("key_type_from_name: unknown key type '%s'", name);
1.12 markus 753: return KEY_UNSPEC;
1.25 markus 754: }
755:
756: int
757: key_names_valid2(const char *names)
758: {
759: char *s, *cp, *p;
760:
761: if (names == NULL || strcmp(names, "") == 0)
762: return 0;
763: s = cp = xstrdup(names);
764: for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36 deraadt 765: (p = strsep(&cp, ","))) {
1.25 markus 766: switch (key_type_from_name(p)) {
767: case KEY_RSA1:
768: case KEY_UNSPEC:
769: xfree(s);
770: return 0;
771: }
772: }
773: debug3("key names ok: [%s]", names);
774: xfree(s);
775: return 1;
1.12 markus 776: }
777:
778: Key *
1.55 jakob 779: key_from_blob(const u_char *blob, u_int blen)
1.12 markus 780: {
781: Buffer b;
782: int rlen, type;
1.57 djm 783: char *ktype = NULL;
1.12 markus 784: Key *key = NULL;
785:
786: #ifdef DEBUG_PK
787: dump_base64(stderr, blob, blen);
788: #endif
789: buffer_init(&b);
790: buffer_append(&b, blob, blen);
1.57 djm 791: if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
792: error("key_from_blob: can't read key type");
793: goto out;
794: }
795:
1.12 markus 796: type = key_type_from_name(ktype);
797:
1.35 deraadt 798: switch (type) {
1.12 markus 799: case KEY_RSA:
800: key = key_new(type);
1.57 djm 801: if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
802: buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
803: error("key_from_blob: can't read rsa key");
804: key_free(key);
805: key = NULL;
806: goto out;
807: }
1.12 markus 808: #ifdef DEBUG_PK
809: RSA_print_fp(stderr, key->rsa, 8);
810: #endif
811: break;
812: case KEY_DSA:
813: key = key_new(type);
1.57 djm 814: if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
815: buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
816: buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
817: buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
818: error("key_from_blob: can't read dsa key");
819: key_free(key);
820: key = NULL;
821: goto out;
822: }
1.12 markus 823: #ifdef DEBUG_PK
824: DSA_print_fp(stderr, key->dsa, 8);
825: #endif
826: break;
827: case KEY_UNSPEC:
828: key = key_new(type);
829: break;
830: default:
831: error("key_from_blob: cannot handle type %s", ktype);
1.57 djm 832: goto out;
1.12 markus 833: }
834: rlen = buffer_len(&b);
835: if (key != NULL && rlen != 0)
836: error("key_from_blob: remaining bytes in key blob %d", rlen);
1.57 djm 837: out:
838: if (ktype != NULL)
839: xfree(ktype);
1.12 markus 840: buffer_free(&b);
841: return key;
842: }
843:
844: int
1.55 jakob 845: key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1.12 markus 846: {
847: Buffer b;
848: int len;
849:
850: if (key == NULL) {
851: error("key_to_blob: key == NULL");
852: return 0;
853: }
854: buffer_init(&b);
1.35 deraadt 855: switch (key->type) {
1.12 markus 856: case KEY_DSA:
857: buffer_put_cstring(&b, key_ssh_name(key));
858: buffer_put_bignum2(&b, key->dsa->p);
859: buffer_put_bignum2(&b, key->dsa->q);
860: buffer_put_bignum2(&b, key->dsa->g);
861: buffer_put_bignum2(&b, key->dsa->pub_key);
862: break;
863: case KEY_RSA:
864: buffer_put_cstring(&b, key_ssh_name(key));
1.14 markus 865: buffer_put_bignum2(&b, key->rsa->e);
1.12 markus 866: buffer_put_bignum2(&b, key->rsa->n);
867: break;
868: default:
1.31 markus 869: error("key_to_blob: unsupported key type %d", key->type);
870: buffer_free(&b);
871: return 0;
1.12 markus 872: }
873: len = buffer_len(&b);
1.48 markus 874: if (lenp != NULL)
875: *lenp = len;
876: if (blobp != NULL) {
877: *blobp = xmalloc(len);
878: memcpy(*blobp, buffer_ptr(&b), len);
879: }
1.12 markus 880: memset(buffer_ptr(&b), 0, len);
881: buffer_free(&b);
882: return len;
883: }
884:
885: int
886: key_sign(
1.55 jakob 887: const Key *key,
1.40 markus 888: u_char **sigp, u_int *lenp,
1.55 jakob 889: const u_char *data, u_int datalen)
1.12 markus 890: {
1.35 deraadt 891: switch (key->type) {
1.12 markus 892: case KEY_DSA:
893: return ssh_dss_sign(key, sigp, lenp, data, datalen);
894: case KEY_RSA:
895: return ssh_rsa_sign(key, sigp, lenp, data, datalen);
896: default:
1.56 markus 897: error("key_sign: invalid key type %d", key->type);
1.12 markus 898: return -1;
899: }
900: }
901:
1.44 markus 902: /*
903: * key_verify returns 1 for a correct signature, 0 for an incorrect signature
904: * and -1 on error.
905: */
1.12 markus 906: int
907: key_verify(
1.55 jakob 908: const Key *key,
909: const u_char *signature, u_int signaturelen,
910: const u_char *data, u_int datalen)
1.12 markus 911: {
1.26 markus 912: if (signaturelen == 0)
913: return -1;
914:
1.35 deraadt 915: switch (key->type) {
1.12 markus 916: case KEY_DSA:
917: return ssh_dss_verify(key, signature, signaturelen, data, datalen);
918: case KEY_RSA:
919: return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
920: default:
1.56 markus 921: error("key_verify: invalid key type %d", key->type);
1.12 markus 922: return -1;
923: }
1.42 markus 924: }
925:
926: /* Converts a private to a public key */
927: Key *
1.55 jakob 928: key_demote(const Key *k)
1.42 markus 929: {
930: Key *pk;
1.43 markus 931:
1.63 djm 932: pk = xcalloc(1, sizeof(*pk));
1.42 markus 933: pk->type = k->type;
934: pk->flags = k->flags;
935: pk->dsa = NULL;
936: pk->rsa = NULL;
937:
938: switch (k->type) {
939: case KEY_RSA1:
940: case KEY_RSA:
941: if ((pk->rsa = RSA_new()) == NULL)
942: fatal("key_demote: RSA_new failed");
943: if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
944: fatal("key_demote: BN_dup failed");
945: if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
946: fatal("key_demote: BN_dup failed");
947: break;
948: case KEY_DSA:
949: if ((pk->dsa = DSA_new()) == NULL)
950: fatal("key_demote: DSA_new failed");
951: if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
952: fatal("key_demote: BN_dup failed");
953: if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
954: fatal("key_demote: BN_dup failed");
955: if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
956: fatal("key_demote: BN_dup failed");
957: if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
958: fatal("key_demote: BN_dup failed");
959: break;
960: default:
961: fatal("key_free: bad key type %d", k->type);
962: break;
963: }
964:
965: return (pk);
1.4 markus 966: }