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