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