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