Annotation of src/usr.bin/ssh/key.c, Revision 1.66
1.66 ! stevesk 1: /* $OpenBSD: key.c,v 1.65 2006/07/22 20:48:23 stevesk 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.15 markus 35: #include "includes.h"
1.1 markus 36:
1.2 markus 37: #include <openssl/evp.h>
1.65 stevesk 38:
1.66 ! stevesk 39: #include <stdio.h>
1.65 stevesk 40: #include <string.h>
1.15 markus 41:
1.1 markus 42: #include "xmalloc.h"
43: #include "key.h"
1.12 markus 44: #include "rsa.h"
1.3 markus 45: #include "uuencode.h"
1.12 markus 46: #include "buffer.h"
47: #include "bufaux.h"
1.15 markus 48: #include "log.h"
1.1 markus 49:
50: Key *
51: key_new(int type)
52: {
53: Key *k;
54: RSA *rsa;
55: DSA *dsa;
1.63 djm 56: k = xcalloc(1, sizeof(*k));
1.1 markus 57: k->type = type;
1.3 markus 58: k->dsa = NULL;
59: k->rsa = NULL;
1.1 markus 60: switch (k->type) {
1.12 markus 61: case KEY_RSA1:
1.1 markus 62: case KEY_RSA:
1.38 markus 63: if ((rsa = RSA_new()) == NULL)
64: fatal("key_new: RSA_new failed");
65: if ((rsa->n = BN_new()) == NULL)
66: fatal("key_new: BN_new failed");
67: if ((rsa->e = BN_new()) == NULL)
68: fatal("key_new: BN_new failed");
1.1 markus 69: k->rsa = rsa;
70: break;
71: case KEY_DSA:
1.38 markus 72: if ((dsa = DSA_new()) == NULL)
73: fatal("key_new: DSA_new failed");
74: if ((dsa->p = BN_new()) == NULL)
75: fatal("key_new: BN_new failed");
76: if ((dsa->q = BN_new()) == NULL)
77: fatal("key_new: BN_new failed");
78: if ((dsa->g = BN_new()) == NULL)
79: fatal("key_new: BN_new failed");
80: if ((dsa->pub_key = BN_new()) == NULL)
81: fatal("key_new: BN_new failed");
1.1 markus 82: k->dsa = dsa;
83: break;
1.12 markus 84: case KEY_UNSPEC:
1.1 markus 85: break;
86: default:
87: fatal("key_new: bad key type %d", k->type);
88: break;
89: }
90: return k;
91: }
1.45 deraadt 92:
1.12 markus 93: Key *
94: key_new_private(int type)
95: {
96: Key *k = key_new(type);
97: switch (k->type) {
98: case KEY_RSA1:
99: case KEY_RSA:
1.38 markus 100: if ((k->rsa->d = BN_new()) == NULL)
101: fatal("key_new_private: BN_new failed");
102: if ((k->rsa->iqmp = BN_new()) == NULL)
103: fatal("key_new_private: BN_new failed");
104: if ((k->rsa->q = BN_new()) == NULL)
105: fatal("key_new_private: BN_new failed");
106: if ((k->rsa->p = BN_new()) == NULL)
107: fatal("key_new_private: BN_new failed");
108: if ((k->rsa->dmq1 = BN_new()) == NULL)
109: fatal("key_new_private: BN_new failed");
110: if ((k->rsa->dmp1 = BN_new()) == NULL)
111: fatal("key_new_private: BN_new failed");
1.12 markus 112: break;
113: case KEY_DSA:
1.38 markus 114: if ((k->dsa->priv_key = BN_new()) == NULL)
115: fatal("key_new_private: BN_new failed");
1.12 markus 116: break;
117: case KEY_UNSPEC:
118: break;
119: default:
120: break;
121: }
122: return k;
123: }
1.45 deraadt 124:
1.1 markus 125: void
126: key_free(Key *k)
127: {
1.60 djm 128: if (k == NULL)
1.62 deraadt 129: fatal("key_free: key is NULL");
1.1 markus 130: switch (k->type) {
1.12 markus 131: case KEY_RSA1:
1.1 markus 132: case KEY_RSA:
133: if (k->rsa != NULL)
134: RSA_free(k->rsa);
135: k->rsa = NULL;
136: break;
137: case KEY_DSA:
138: if (k->dsa != NULL)
139: DSA_free(k->dsa);
140: k->dsa = NULL;
141: break;
1.12 markus 142: case KEY_UNSPEC:
143: break;
1.1 markus 144: default:
145: fatal("key_free: bad key type %d", k->type);
146: break;
147: }
148: xfree(k);
149: }
1.55 jakob 150:
1.1 markus 151: int
1.55 jakob 152: key_equal(const Key *a, const Key *b)
1.1 markus 153: {
154: if (a == NULL || b == NULL || a->type != b->type)
155: return 0;
156: switch (a->type) {
1.12 markus 157: case KEY_RSA1:
1.1 markus 158: case KEY_RSA:
159: return a->rsa != NULL && b->rsa != NULL &&
160: BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
161: BN_cmp(a->rsa->n, b->rsa->n) == 0;
162: case KEY_DSA:
163: return a->dsa != NULL && b->dsa != NULL &&
164: BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
165: BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
166: BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
167: BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
168: default:
1.3 markus 169: fatal("key_equal: bad key type %d", a->type);
1.1 markus 170: break;
171: }
172: return 0;
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.46 deraadt 296: char *
1.55 jakob 297: key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
1.19 jakob 298: {
1.23 markus 299: char *retval = NULL;
1.19 jakob 300: u_char *dgst_raw;
1.39 markus 301: u_int dgst_raw_len;
1.36 deraadt 302:
1.19 jakob 303: dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
304: if (!dgst_raw)
1.22 markus 305: fatal("key_fingerprint: null from key_fingerprint_raw()");
1.35 deraadt 306: switch (dgst_rep) {
1.19 jakob 307: case SSH_FP_HEX:
308: retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
309: break;
310: case SSH_FP_BUBBLEBABBLE:
311: retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
312: break;
313: default:
314: fatal("key_fingerprint_ex: bad digest representation %d",
315: dgst_rep);
316: break;
317: }
318: memset(dgst_raw, 0, dgst_raw_len);
319: xfree(dgst_raw);
1.1 markus 320: return retval;
321: }
322:
323: /*
324: * Reads a multiple-precision integer in decimal from the buffer, and advances
325: * the pointer. The integer must already be initialized. This function is
326: * permitted to modify the buffer. This leaves *cpp to point just beyond the
327: * last processed (and maybe modified) character. Note that this may modify
328: * the buffer containing the number.
329: */
1.27 itojun 330: static int
1.1 markus 331: read_bignum(char **cpp, BIGNUM * value)
332: {
333: char *cp = *cpp;
334: int old;
335:
336: /* Skip any leading whitespace. */
337: for (; *cp == ' ' || *cp == '\t'; cp++)
338: ;
339:
340: /* Check that it begins with a decimal digit. */
341: if (*cp < '0' || *cp > '9')
342: return 0;
343:
344: /* Save starting position. */
345: *cpp = cp;
346:
347: /* Move forward until all decimal digits skipped. */
348: for (; *cp >= '0' && *cp <= '9'; cp++)
349: ;
350:
351: /* Save the old terminating character, and replace it by \0. */
352: old = *cp;
353: *cp = 0;
354:
355: /* Parse the number. */
356: if (BN_dec2bn(&value, *cpp) == 0)
357: return 0;
358:
359: /* Restore old terminating character. */
360: *cp = old;
361:
362: /* Move beyond the number and return success. */
363: *cpp = cp;
364: return 1;
365: }
1.45 deraadt 366:
1.27 itojun 367: static int
1.1 markus 368: write_bignum(FILE *f, BIGNUM *num)
369: {
370: char *buf = BN_bn2dec(num);
371: if (buf == NULL) {
372: error("write_bignum: BN_bn2dec() failed");
373: return 0;
374: }
375: fprintf(f, " %s", buf);
1.33 markus 376: OPENSSL_free(buf);
1.1 markus 377: return 1;
378: }
1.12 markus 379:
1.32 markus 380: /* returns 1 ok, -1 error */
1.12 markus 381: int
1.3 markus 382: key_read(Key *ret, char **cpp)
1.1 markus 383: {
1.3 markus 384: Key *k;
1.12 markus 385: int success = -1;
386: char *cp, *space;
387: int len, n, type;
388: u_int bits;
1.13 markus 389: u_char *blob;
1.3 markus 390:
391: cp = *cpp;
392:
1.35 deraadt 393: switch (ret->type) {
1.12 markus 394: case KEY_RSA1:
1.3 markus 395: /* Get number of bits. */
396: if (*cp < '0' || *cp > '9')
1.12 markus 397: return -1; /* Bad bit count... */
1.3 markus 398: for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
399: bits = 10 * bits + *cp - '0';
1.1 markus 400: if (bits == 0)
1.12 markus 401: return -1;
1.3 markus 402: *cpp = cp;
1.1 markus 403: /* Get public exponent, public modulus. */
404: if (!read_bignum(cpp, ret->rsa->e))
1.12 markus 405: return -1;
1.1 markus 406: if (!read_bignum(cpp, ret->rsa->n))
1.12 markus 407: return -1;
408: success = 1;
1.1 markus 409: break;
1.12 markus 410: case KEY_UNSPEC:
411: case KEY_RSA:
1.1 markus 412: case KEY_DSA:
1.12 markus 413: space = strchr(cp, ' ');
414: if (space == NULL) {
1.50 markus 415: debug3("key_read: missing whitespace");
1.12 markus 416: return -1;
417: }
418: *space = '\0';
419: type = key_type_from_name(cp);
420: *space = ' ';
421: if (type == KEY_UNSPEC) {
1.50 markus 422: debug3("key_read: missing keytype");
1.12 markus 423: return -1;
424: }
425: cp = space+1;
426: if (*cp == '\0') {
427: debug3("key_read: short string");
428: return -1;
429: }
430: if (ret->type == KEY_UNSPEC) {
431: ret->type = type;
432: } else if (ret->type != type) {
433: /* is a key, but different type */
434: debug3("key_read: type mismatch");
1.32 markus 435: return -1;
1.12 markus 436: }
1.3 markus 437: len = 2*strlen(cp);
438: blob = xmalloc(len);
439: n = uudecode(cp, blob, len);
1.6 markus 440: if (n < 0) {
1.7 markus 441: error("key_read: uudecode %s failed", cp);
1.34 markus 442: xfree(blob);
1.12 markus 443: return -1;
1.6 markus 444: }
1.53 markus 445: k = key_from_blob(blob, (u_int)n);
1.34 markus 446: xfree(blob);
1.7 markus 447: if (k == NULL) {
1.12 markus 448: error("key_read: key_from_blob %s failed", cp);
449: return -1;
1.7 markus 450: }
1.12 markus 451: if (k->type != type) {
452: error("key_read: type mismatch: encoding error");
453: key_free(k);
454: return -1;
455: }
456: /*XXXX*/
457: if (ret->type == KEY_RSA) {
458: if (ret->rsa != NULL)
459: RSA_free(ret->rsa);
460: ret->rsa = k->rsa;
461: k->rsa = NULL;
462: success = 1;
463: #ifdef DEBUG_PK
464: RSA_print_fp(stderr, ret->rsa, 8);
465: #endif
466: } else {
467: if (ret->dsa != NULL)
468: DSA_free(ret->dsa);
469: ret->dsa = k->dsa;
470: k->dsa = NULL;
471: success = 1;
472: #ifdef DEBUG_PK
473: DSA_print_fp(stderr, ret->dsa, 8);
474: #endif
475: }
476: /*XXXX*/
1.34 markus 477: key_free(k);
1.12 markus 478: if (success != 1)
479: break;
1.7 markus 480: /* advance cp: skip whitespace and data */
481: while (*cp == ' ' || *cp == '\t')
482: cp++;
483: while (*cp != '\0' && *cp != ' ' && *cp != '\t')
484: cp++;
485: *cpp = cp;
1.1 markus 486: break;
487: default:
1.3 markus 488: fatal("key_read: bad key type: %d", ret->type);
1.1 markus 489: break;
490: }
1.12 markus 491: return success;
1.1 markus 492: }
1.45 deraadt 493:
1.1 markus 494: int
1.55 jakob 495: key_write(const Key *key, FILE *f)
1.1 markus 496: {
1.40 markus 497: int n, success = 0;
498: u_int len, bits = 0;
1.49 markus 499: u_char *blob;
500: char *uu;
1.1 markus 501:
1.12 markus 502: if (key->type == KEY_RSA1 && key->rsa != NULL) {
1.1 markus 503: /* size of modulus 'n' */
504: bits = BN_num_bits(key->rsa->n);
505: fprintf(f, "%u", bits);
506: if (write_bignum(f, key->rsa->e) &&
507: write_bignum(f, key->rsa->n)) {
508: success = 1;
509: } else {
510: error("key_write: failed for RSA key");
511: }
1.12 markus 512: } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
513: (key->type == KEY_RSA && key->rsa != NULL)) {
514: key_to_blob(key, &blob, &len);
1.3 markus 515: uu = xmalloc(2*len);
1.5 markus 516: n = uuencode(blob, len, uu, 2*len);
517: if (n > 0) {
1.12 markus 518: fprintf(f, "%s %s", key_ssh_name(key), uu);
1.5 markus 519: success = 1;
520: }
1.3 markus 521: xfree(blob);
522: xfree(uu);
1.1 markus 523: }
524: return success;
525: }
1.45 deraadt 526:
1.55 jakob 527: const char *
528: key_type(const Key *k)
1.4 markus 529: {
530: switch (k->type) {
1.12 markus 531: case KEY_RSA1:
532: return "RSA1";
1.4 markus 533: case KEY_RSA:
534: return "RSA";
535: case KEY_DSA:
536: return "DSA";
537: }
538: return "unknown";
1.10 markus 539: }
1.45 deraadt 540:
1.55 jakob 541: const char *
542: key_ssh_name(const Key *k)
1.12 markus 543: {
544: switch (k->type) {
545: case KEY_RSA:
546: return "ssh-rsa";
547: case KEY_DSA:
548: return "ssh-dss";
549: }
550: return "ssh-unknown";
551: }
1.45 deraadt 552:
1.12 markus 553: u_int
1.55 jakob 554: key_size(const Key *k)
1.35 deraadt 555: {
1.10 markus 556: switch (k->type) {
1.12 markus 557: case KEY_RSA1:
1.10 markus 558: case KEY_RSA:
559: return BN_num_bits(k->rsa->n);
560: case KEY_DSA:
561: return BN_num_bits(k->dsa->p);
562: }
563: return 0;
1.12 markus 564: }
565:
1.27 itojun 566: static RSA *
1.13 markus 567: rsa_generate_private_key(u_int bits)
1.12 markus 568: {
1.17 stevesk 569: RSA *private;
1.61 deraadt 570:
1.17 stevesk 571: private = RSA_generate_key(bits, 35, NULL, NULL);
572: if (private == NULL)
573: fatal("rsa_generate_private_key: key generation failed.");
574: return private;
1.12 markus 575: }
576:
1.27 itojun 577: static DSA*
1.13 markus 578: dsa_generate_private_key(u_int bits)
1.12 markus 579: {
580: DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
1.61 deraadt 581:
1.12 markus 582: if (private == NULL)
583: fatal("dsa_generate_private_key: DSA_generate_parameters failed");
584: if (!DSA_generate_key(private))
1.17 stevesk 585: fatal("dsa_generate_private_key: DSA_generate_key failed.");
586: if (private == NULL)
587: fatal("dsa_generate_private_key: NULL.");
1.12 markus 588: return private;
589: }
590:
591: Key *
1.13 markus 592: key_generate(int type, u_int bits)
1.12 markus 593: {
594: Key *k = key_new(KEY_UNSPEC);
595: switch (type) {
1.17 stevesk 596: case KEY_DSA:
1.12 markus 597: k->dsa = dsa_generate_private_key(bits);
598: break;
599: case KEY_RSA:
600: case KEY_RSA1:
601: k->rsa = rsa_generate_private_key(bits);
602: break;
603: default:
1.17 stevesk 604: fatal("key_generate: unknown type %d", type);
1.12 markus 605: }
1.17 stevesk 606: k->type = type;
1.12 markus 607: return k;
608: }
609:
610: Key *
1.55 jakob 611: key_from_private(const Key *k)
1.12 markus 612: {
613: Key *n = NULL;
614: switch (k->type) {
1.17 stevesk 615: case KEY_DSA:
1.12 markus 616: n = key_new(k->type);
617: BN_copy(n->dsa->p, k->dsa->p);
618: BN_copy(n->dsa->q, k->dsa->q);
619: BN_copy(n->dsa->g, k->dsa->g);
620: BN_copy(n->dsa->pub_key, k->dsa->pub_key);
621: break;
622: case KEY_RSA:
623: case KEY_RSA1:
624: n = key_new(k->type);
625: BN_copy(n->rsa->n, k->rsa->n);
626: BN_copy(n->rsa->e, k->rsa->e);
627: break;
628: default:
1.17 stevesk 629: fatal("key_from_private: unknown type %d", k->type);
1.12 markus 630: break;
631: }
632: return n;
633: }
634:
635: int
636: key_type_from_name(char *name)
637: {
1.35 deraadt 638: if (strcmp(name, "rsa1") == 0) {
1.12 markus 639: return KEY_RSA1;
1.35 deraadt 640: } else if (strcmp(name, "rsa") == 0) {
1.12 markus 641: return KEY_RSA;
1.35 deraadt 642: } else if (strcmp(name, "dsa") == 0) {
1.12 markus 643: return KEY_DSA;
1.35 deraadt 644: } else if (strcmp(name, "ssh-rsa") == 0) {
1.12 markus 645: return KEY_RSA;
1.35 deraadt 646: } else if (strcmp(name, "ssh-dss") == 0) {
1.12 markus 647: return KEY_DSA;
648: }
1.18 markus 649: debug2("key_type_from_name: unknown key type '%s'", name);
1.12 markus 650: return KEY_UNSPEC;
1.25 markus 651: }
652:
653: int
654: key_names_valid2(const char *names)
655: {
656: char *s, *cp, *p;
657:
658: if (names == NULL || strcmp(names, "") == 0)
659: return 0;
660: s = cp = xstrdup(names);
661: for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36 deraadt 662: (p = strsep(&cp, ","))) {
1.25 markus 663: switch (key_type_from_name(p)) {
664: case KEY_RSA1:
665: case KEY_UNSPEC:
666: xfree(s);
667: return 0;
668: }
669: }
670: debug3("key names ok: [%s]", names);
671: xfree(s);
672: return 1;
1.12 markus 673: }
674:
675: Key *
1.55 jakob 676: key_from_blob(const u_char *blob, u_int blen)
1.12 markus 677: {
678: Buffer b;
679: int rlen, type;
1.57 djm 680: char *ktype = NULL;
1.12 markus 681: Key *key = NULL;
682:
683: #ifdef DEBUG_PK
684: dump_base64(stderr, blob, blen);
685: #endif
686: buffer_init(&b);
687: buffer_append(&b, blob, blen);
1.57 djm 688: if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
689: error("key_from_blob: can't read key type");
690: goto out;
691: }
692:
1.12 markus 693: type = key_type_from_name(ktype);
694:
1.35 deraadt 695: switch (type) {
1.12 markus 696: case KEY_RSA:
697: key = key_new(type);
1.57 djm 698: if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
699: buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
700: error("key_from_blob: can't read rsa key");
701: key_free(key);
702: key = NULL;
703: goto out;
704: }
1.12 markus 705: #ifdef DEBUG_PK
706: RSA_print_fp(stderr, key->rsa, 8);
707: #endif
708: break;
709: case KEY_DSA:
710: key = key_new(type);
1.57 djm 711: if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
712: buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
713: buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
714: buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
715: error("key_from_blob: can't read dsa key");
716: key_free(key);
717: key = NULL;
718: goto out;
719: }
1.12 markus 720: #ifdef DEBUG_PK
721: DSA_print_fp(stderr, key->dsa, 8);
722: #endif
723: break;
724: case KEY_UNSPEC:
725: key = key_new(type);
726: break;
727: default:
728: error("key_from_blob: cannot handle type %s", ktype);
1.57 djm 729: goto out;
1.12 markus 730: }
731: rlen = buffer_len(&b);
732: if (key != NULL && rlen != 0)
733: error("key_from_blob: remaining bytes in key blob %d", rlen);
1.57 djm 734: out:
735: if (ktype != NULL)
736: xfree(ktype);
1.12 markus 737: buffer_free(&b);
738: return key;
739: }
740:
741: int
1.55 jakob 742: key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1.12 markus 743: {
744: Buffer b;
745: int len;
746:
747: if (key == NULL) {
748: error("key_to_blob: key == NULL");
749: return 0;
750: }
751: buffer_init(&b);
1.35 deraadt 752: switch (key->type) {
1.12 markus 753: case KEY_DSA:
754: buffer_put_cstring(&b, key_ssh_name(key));
755: buffer_put_bignum2(&b, key->dsa->p);
756: buffer_put_bignum2(&b, key->dsa->q);
757: buffer_put_bignum2(&b, key->dsa->g);
758: buffer_put_bignum2(&b, key->dsa->pub_key);
759: break;
760: case KEY_RSA:
761: buffer_put_cstring(&b, key_ssh_name(key));
1.14 markus 762: buffer_put_bignum2(&b, key->rsa->e);
1.12 markus 763: buffer_put_bignum2(&b, key->rsa->n);
764: break;
765: default:
1.31 markus 766: error("key_to_blob: unsupported key type %d", key->type);
767: buffer_free(&b);
768: return 0;
1.12 markus 769: }
770: len = buffer_len(&b);
1.48 markus 771: if (lenp != NULL)
772: *lenp = len;
773: if (blobp != NULL) {
774: *blobp = xmalloc(len);
775: memcpy(*blobp, buffer_ptr(&b), len);
776: }
1.12 markus 777: memset(buffer_ptr(&b), 0, len);
778: buffer_free(&b);
779: return len;
780: }
781:
782: int
783: key_sign(
1.55 jakob 784: const Key *key,
1.40 markus 785: u_char **sigp, u_int *lenp,
1.55 jakob 786: const u_char *data, u_int datalen)
1.12 markus 787: {
1.35 deraadt 788: switch (key->type) {
1.12 markus 789: case KEY_DSA:
790: return ssh_dss_sign(key, sigp, lenp, data, datalen);
791: case KEY_RSA:
792: return ssh_rsa_sign(key, sigp, lenp, data, datalen);
793: default:
1.56 markus 794: error("key_sign: invalid key type %d", key->type);
1.12 markus 795: return -1;
796: }
797: }
798:
1.44 markus 799: /*
800: * key_verify returns 1 for a correct signature, 0 for an incorrect signature
801: * and -1 on error.
802: */
1.12 markus 803: int
804: key_verify(
1.55 jakob 805: const Key *key,
806: const u_char *signature, u_int signaturelen,
807: const u_char *data, u_int datalen)
1.12 markus 808: {
1.26 markus 809: if (signaturelen == 0)
810: return -1;
811:
1.35 deraadt 812: switch (key->type) {
1.12 markus 813: case KEY_DSA:
814: return ssh_dss_verify(key, signature, signaturelen, data, datalen);
815: case KEY_RSA:
816: return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
817: default:
1.56 markus 818: error("key_verify: invalid key type %d", key->type);
1.12 markus 819: return -1;
820: }
1.42 markus 821: }
822:
823: /* Converts a private to a public key */
824: Key *
1.55 jakob 825: key_demote(const Key *k)
1.42 markus 826: {
827: Key *pk;
1.43 markus 828:
1.63 djm 829: pk = xcalloc(1, sizeof(*pk));
1.42 markus 830: pk->type = k->type;
831: pk->flags = k->flags;
832: pk->dsa = NULL;
833: pk->rsa = NULL;
834:
835: switch (k->type) {
836: case KEY_RSA1:
837: case KEY_RSA:
838: if ((pk->rsa = RSA_new()) == NULL)
839: fatal("key_demote: RSA_new failed");
840: if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
841: fatal("key_demote: BN_dup failed");
842: if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
843: fatal("key_demote: BN_dup failed");
844: break;
845: case KEY_DSA:
846: if ((pk->dsa = DSA_new()) == NULL)
847: fatal("key_demote: DSA_new failed");
848: if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
849: fatal("key_demote: BN_dup failed");
850: if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
851: fatal("key_demote: BN_dup failed");
852: if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
853: fatal("key_demote: BN_dup failed");
854: if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
855: fatal("key_demote: BN_dup failed");
856: break;
857: default:
858: fatal("key_free: bad key type %d", k->type);
859: break;
860: }
861:
862: return (pk);
1.4 markus 863: }