Annotation of src/usr.bin/ssh/key.c, Revision 1.41
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.41 ! markus 35: RCSID("$OpenBSD: key.c,v 1.40 2002/02/24 19:14:59 markus Exp $");
1.1 markus 36:
1.2 markus 37: #include <openssl/evp.h>
1.15 markus 38:
1.1 markus 39: #include "xmalloc.h"
40: #include "key.h"
1.12 markus 41: #include "rsa.h"
42: #include "ssh-dss.h"
43: #include "ssh-rsa.h"
1.3 markus 44: #include "uuencode.h"
1.12 markus 45: #include "buffer.h"
46: #include "bufaux.h"
1.15 markus 47: #include "log.h"
1.1 markus 48:
49: Key *
50: key_new(int type)
51: {
52: Key *k;
53: RSA *rsa;
54: DSA *dsa;
55: k = xmalloc(sizeof(*k));
56: k->type = type;
1.29 markus 57: k->flags = 0;
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.12 markus 92: Key *
93: key_new_private(int type)
94: {
95: Key *k = key_new(type);
96: switch (k->type) {
97: case KEY_RSA1:
98: case KEY_RSA:
1.38 markus 99: if ((k->rsa->d = BN_new()) == NULL)
100: fatal("key_new_private: BN_new failed");
101: if ((k->rsa->iqmp = BN_new()) == NULL)
102: fatal("key_new_private: BN_new failed");
103: if ((k->rsa->q = BN_new()) == NULL)
104: fatal("key_new_private: BN_new failed");
105: if ((k->rsa->p = BN_new()) == NULL)
106: fatal("key_new_private: BN_new failed");
107: if ((k->rsa->dmq1 = BN_new()) == NULL)
108: fatal("key_new_private: BN_new failed");
109: if ((k->rsa->dmp1 = BN_new()) == NULL)
110: fatal("key_new_private: BN_new failed");
1.12 markus 111: break;
112: case KEY_DSA:
1.38 markus 113: if ((k->dsa->priv_key = BN_new()) == NULL)
114: fatal("key_new_private: BN_new failed");
1.12 markus 115: break;
116: case KEY_UNSPEC:
117: break;
118: default:
119: break;
120: }
121: return k;
122: }
1.1 markus 123: void
124: key_free(Key *k)
125: {
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: }
146: int
147: key_equal(Key *a, Key *b)
148: {
149: if (a == NULL || b == NULL || a->type != b->type)
150: return 0;
151: switch (a->type) {
1.12 markus 152: case KEY_RSA1:
1.1 markus 153: case KEY_RSA:
154: return a->rsa != NULL && b->rsa != NULL &&
155: BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
156: BN_cmp(a->rsa->n, b->rsa->n) == 0;
157: break;
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: break;
165: default:
1.3 markus 166: fatal("key_equal: bad key type %d", a->type);
1.1 markus 167: break;
168: }
169: return 0;
170: }
171:
1.27 itojun 172: static u_char*
1.39 markus 173: key_fingerprint_raw(Key *k, enum fp_type dgst_type, 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: break;
211: default:
1.19 jakob 212: fatal("key_fingerprint_raw: bad key type %d", k->type);
1.1 markus 213: break;
214: }
1.3 markus 215: if (blob != NULL) {
1.19 jakob 216: retval = xmalloc(EVP_MAX_MD_SIZE);
1.8 markus 217: EVP_DigestInit(&ctx, md);
218: EVP_DigestUpdate(&ctx, blob, len);
1.39 markus 219: EVP_DigestFinal(&ctx, retval, dgst_raw_length);
1.3 markus 220: memset(blob, 0, len);
221: xfree(blob);
1.19 jakob 222: } else {
223: fatal("key_fingerprint_raw: blob is null");
1.1 markus 224: }
1.19 jakob 225: return retval;
226: }
227:
1.27 itojun 228: static char*
1.39 markus 229: key_fingerprint_hex(u_char* dgst_raw, u_int dgst_raw_len)
1.19 jakob 230: {
231: char *retval;
232: int i;
233:
1.24 deraadt 234: retval = xmalloc(dgst_raw_len * 3 + 1);
1.19 jakob 235: retval[0] = '\0';
1.36 deraadt 236: for (i = 0; i < dgst_raw_len; i++) {
1.19 jakob 237: char hex[4];
238: snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
239: strlcat(retval, hex, dgst_raw_len * 3);
240: }
241: retval[(dgst_raw_len * 3) - 1] = '\0';
242: return retval;
243: }
244:
1.27 itojun 245: static char*
1.39 markus 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;
255: retval = xmalloc(sizeof(char) * (rounds*6));
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:
292: char*
1.22 markus 293: key_fingerprint(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.27 itojun 362: static int
1.1 markus 363: write_bignum(FILE *f, BIGNUM *num)
364: {
365: char *buf = BN_bn2dec(num);
366: if (buf == NULL) {
367: error("write_bignum: BN_bn2dec() failed");
368: return 0;
369: }
370: fprintf(f, " %s", buf);
1.33 markus 371: OPENSSL_free(buf);
1.1 markus 372: return 1;
373: }
1.12 markus 374:
1.32 markus 375: /* returns 1 ok, -1 error */
1.12 markus 376: int
1.3 markus 377: key_read(Key *ret, char **cpp)
1.1 markus 378: {
1.3 markus 379: Key *k;
1.12 markus 380: int success = -1;
381: char *cp, *space;
382: int len, n, type;
383: u_int bits;
1.13 markus 384: u_char *blob;
1.3 markus 385:
386: cp = *cpp;
387:
1.35 deraadt 388: switch (ret->type) {
1.12 markus 389: case KEY_RSA1:
1.3 markus 390: /* Get number of bits. */
391: if (*cp < '0' || *cp > '9')
1.12 markus 392: return -1; /* Bad bit count... */
1.3 markus 393: for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
394: bits = 10 * bits + *cp - '0';
1.1 markus 395: if (bits == 0)
1.12 markus 396: return -1;
1.3 markus 397: *cpp = cp;
1.1 markus 398: /* Get public exponent, public modulus. */
399: if (!read_bignum(cpp, ret->rsa->e))
1.12 markus 400: return -1;
1.1 markus 401: if (!read_bignum(cpp, ret->rsa->n))
1.12 markus 402: return -1;
403: success = 1;
1.1 markus 404: break;
1.12 markus 405: case KEY_UNSPEC:
406: case KEY_RSA:
1.1 markus 407: case KEY_DSA:
1.12 markus 408: space = strchr(cp, ' ');
409: if (space == NULL) {
410: debug3("key_read: no space");
411: return -1;
412: }
413: *space = '\0';
414: type = key_type_from_name(cp);
415: *space = ' ';
416: if (type == KEY_UNSPEC) {
417: debug3("key_read: no key found");
418: return -1;
419: }
420: cp = space+1;
421: if (*cp == '\0') {
422: debug3("key_read: short string");
423: return -1;
424: }
425: if (ret->type == KEY_UNSPEC) {
426: ret->type = type;
427: } else if (ret->type != type) {
428: /* is a key, but different type */
429: debug3("key_read: type mismatch");
1.32 markus 430: return -1;
1.12 markus 431: }
1.3 markus 432: len = 2*strlen(cp);
433: blob = xmalloc(len);
434: n = uudecode(cp, blob, len);
1.6 markus 435: if (n < 0) {
1.7 markus 436: error("key_read: uudecode %s failed", cp);
1.34 markus 437: xfree(blob);
1.12 markus 438: return -1;
1.6 markus 439: }
1.12 markus 440: k = key_from_blob(blob, n);
1.34 markus 441: xfree(blob);
1.7 markus 442: if (k == NULL) {
1.12 markus 443: error("key_read: key_from_blob %s failed", cp);
444: return -1;
1.7 markus 445: }
1.12 markus 446: if (k->type != type) {
447: error("key_read: type mismatch: encoding error");
448: key_free(k);
449: return -1;
450: }
451: /*XXXX*/
452: if (ret->type == KEY_RSA) {
453: if (ret->rsa != NULL)
454: RSA_free(ret->rsa);
455: ret->rsa = k->rsa;
456: k->rsa = NULL;
457: success = 1;
458: #ifdef DEBUG_PK
459: RSA_print_fp(stderr, ret->rsa, 8);
460: #endif
461: } else {
462: if (ret->dsa != NULL)
463: DSA_free(ret->dsa);
464: ret->dsa = k->dsa;
465: k->dsa = NULL;
466: success = 1;
467: #ifdef DEBUG_PK
468: DSA_print_fp(stderr, ret->dsa, 8);
469: #endif
470: }
471: /*XXXX*/
1.34 markus 472: key_free(k);
1.12 markus 473: if (success != 1)
474: break;
1.7 markus 475: /* advance cp: skip whitespace and data */
476: while (*cp == ' ' || *cp == '\t')
477: cp++;
478: while (*cp != '\0' && *cp != ' ' && *cp != '\t')
479: cp++;
480: *cpp = cp;
1.1 markus 481: break;
482: default:
1.3 markus 483: fatal("key_read: bad key type: %d", ret->type);
1.1 markus 484: break;
485: }
1.12 markus 486: return success;
1.1 markus 487: }
488: int
489: key_write(Key *key, FILE *f)
490: {
1.40 markus 491: int n, success = 0;
492: u_int len, bits = 0;
493: u_char *blob, *uu;
1.1 markus 494:
1.12 markus 495: if (key->type == KEY_RSA1 && key->rsa != NULL) {
1.1 markus 496: /* size of modulus 'n' */
497: bits = BN_num_bits(key->rsa->n);
498: fprintf(f, "%u", bits);
499: if (write_bignum(f, key->rsa->e) &&
500: write_bignum(f, key->rsa->n)) {
501: success = 1;
502: } else {
503: error("key_write: failed for RSA key");
504: }
1.12 markus 505: } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
506: (key->type == KEY_RSA && key->rsa != NULL)) {
507: key_to_blob(key, &blob, &len);
1.3 markus 508: uu = xmalloc(2*len);
1.5 markus 509: n = uuencode(blob, len, uu, 2*len);
510: if (n > 0) {
1.12 markus 511: fprintf(f, "%s %s", key_ssh_name(key), uu);
1.5 markus 512: success = 1;
513: }
1.3 markus 514: xfree(blob);
515: xfree(uu);
1.1 markus 516: }
517: return success;
518: }
1.4 markus 519: char *
520: key_type(Key *k)
521: {
522: switch (k->type) {
1.12 markus 523: case KEY_RSA1:
524: return "RSA1";
525: break;
1.4 markus 526: case KEY_RSA:
527: return "RSA";
528: break;
529: case KEY_DSA:
530: return "DSA";
531: break;
532: }
533: return "unknown";
1.10 markus 534: }
1.12 markus 535: char *
536: key_ssh_name(Key *k)
537: {
538: switch (k->type) {
539: case KEY_RSA:
540: return "ssh-rsa";
541: break;
542: case KEY_DSA:
543: return "ssh-dss";
544: break;
545: }
546: return "ssh-unknown";
547: }
548: u_int
1.35 deraadt 549: key_size(Key *k)
550: {
1.10 markus 551: switch (k->type) {
1.12 markus 552: case KEY_RSA1:
1.10 markus 553: case KEY_RSA:
554: return BN_num_bits(k->rsa->n);
555: break;
556: case KEY_DSA:
557: return BN_num_bits(k->dsa->p);
558: break;
559: }
560: return 0;
1.12 markus 561: }
562:
1.27 itojun 563: static RSA *
1.13 markus 564: rsa_generate_private_key(u_int bits)
1.12 markus 565: {
1.17 stevesk 566: RSA *private;
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);
577: if (private == NULL)
578: fatal("dsa_generate_private_key: DSA_generate_parameters failed");
579: if (!DSA_generate_key(private))
1.17 stevesk 580: fatal("dsa_generate_private_key: DSA_generate_key failed.");
581: if (private == NULL)
582: fatal("dsa_generate_private_key: NULL.");
1.12 markus 583: return private;
584: }
585:
586: Key *
1.13 markus 587: key_generate(int type, u_int bits)
1.12 markus 588: {
589: Key *k = key_new(KEY_UNSPEC);
590: switch (type) {
1.17 stevesk 591: case KEY_DSA:
1.12 markus 592: k->dsa = dsa_generate_private_key(bits);
593: break;
594: case KEY_RSA:
595: case KEY_RSA1:
596: k->rsa = rsa_generate_private_key(bits);
597: break;
598: default:
1.17 stevesk 599: fatal("key_generate: unknown type %d", type);
1.12 markus 600: }
1.17 stevesk 601: k->type = type;
1.12 markus 602: return k;
603: }
604:
605: Key *
606: key_from_private(Key *k)
607: {
608: Key *n = NULL;
609: switch (k->type) {
1.17 stevesk 610: case KEY_DSA:
1.12 markus 611: n = key_new(k->type);
612: BN_copy(n->dsa->p, k->dsa->p);
613: BN_copy(n->dsa->q, k->dsa->q);
614: BN_copy(n->dsa->g, k->dsa->g);
615: BN_copy(n->dsa->pub_key, k->dsa->pub_key);
616: break;
617: case KEY_RSA:
618: case KEY_RSA1:
619: n = key_new(k->type);
620: BN_copy(n->rsa->n, k->rsa->n);
621: BN_copy(n->rsa->e, k->rsa->e);
622: break;
623: default:
1.17 stevesk 624: fatal("key_from_private: unknown type %d", k->type);
1.12 markus 625: break;
626: }
627: return n;
628: }
629:
630: int
631: key_type_from_name(char *name)
632: {
1.35 deraadt 633: if (strcmp(name, "rsa1") == 0) {
1.12 markus 634: return KEY_RSA1;
1.35 deraadt 635: } else if (strcmp(name, "rsa") == 0) {
1.12 markus 636: return KEY_RSA;
1.35 deraadt 637: } else if (strcmp(name, "dsa") == 0) {
1.12 markus 638: return KEY_DSA;
1.35 deraadt 639: } else if (strcmp(name, "ssh-rsa") == 0) {
1.12 markus 640: return KEY_RSA;
1.35 deraadt 641: } else if (strcmp(name, "ssh-dss") == 0) {
1.12 markus 642: return KEY_DSA;
643: }
1.18 markus 644: debug2("key_type_from_name: unknown key type '%s'", name);
1.12 markus 645: return KEY_UNSPEC;
1.25 markus 646: }
647:
648: int
649: key_names_valid2(const char *names)
650: {
651: char *s, *cp, *p;
652:
653: if (names == NULL || strcmp(names, "") == 0)
654: return 0;
655: s = cp = xstrdup(names);
656: for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36 deraadt 657: (p = strsep(&cp, ","))) {
1.25 markus 658: switch (key_type_from_name(p)) {
659: case KEY_RSA1:
660: case KEY_UNSPEC:
661: xfree(s);
662: return 0;
663: }
664: }
665: debug3("key names ok: [%s]", names);
666: xfree(s);
667: return 1;
1.12 markus 668: }
669:
670: Key *
1.30 stevesk 671: key_from_blob(u_char *blob, int blen)
1.12 markus 672: {
673: Buffer b;
674: char *ktype;
675: int rlen, type;
676: Key *key = NULL;
677:
678: #ifdef DEBUG_PK
679: dump_base64(stderr, blob, blen);
680: #endif
681: buffer_init(&b);
682: buffer_append(&b, blob, blen);
683: ktype = buffer_get_string(&b, NULL);
684: type = key_type_from_name(ktype);
685:
1.35 deraadt 686: switch (type) {
1.12 markus 687: case KEY_RSA:
688: key = key_new(type);
1.14 markus 689: buffer_get_bignum2(&b, key->rsa->e);
1.12 markus 690: buffer_get_bignum2(&b, key->rsa->n);
691: #ifdef DEBUG_PK
692: RSA_print_fp(stderr, key->rsa, 8);
693: #endif
694: break;
695: case KEY_DSA:
696: key = key_new(type);
697: buffer_get_bignum2(&b, key->dsa->p);
698: buffer_get_bignum2(&b, key->dsa->q);
699: buffer_get_bignum2(&b, key->dsa->g);
700: buffer_get_bignum2(&b, key->dsa->pub_key);
701: #ifdef DEBUG_PK
702: DSA_print_fp(stderr, key->dsa, 8);
703: #endif
704: break;
705: case KEY_UNSPEC:
706: key = key_new(type);
707: break;
708: default:
709: error("key_from_blob: cannot handle type %s", ktype);
710: break;
711: }
712: rlen = buffer_len(&b);
713: if (key != NULL && rlen != 0)
714: error("key_from_blob: remaining bytes in key blob %d", rlen);
715: xfree(ktype);
716: buffer_free(&b);
717: return key;
718: }
719:
720: int
1.13 markus 721: key_to_blob(Key *key, u_char **blobp, u_int *lenp)
1.12 markus 722: {
723: Buffer b;
724: int len;
1.13 markus 725: u_char *buf;
1.12 markus 726:
727: if (key == NULL) {
728: error("key_to_blob: key == NULL");
729: return 0;
730: }
731: buffer_init(&b);
1.35 deraadt 732: switch (key->type) {
1.12 markus 733: case KEY_DSA:
734: buffer_put_cstring(&b, key_ssh_name(key));
735: buffer_put_bignum2(&b, key->dsa->p);
736: buffer_put_bignum2(&b, key->dsa->q);
737: buffer_put_bignum2(&b, key->dsa->g);
738: buffer_put_bignum2(&b, key->dsa->pub_key);
739: break;
740: case KEY_RSA:
741: buffer_put_cstring(&b, key_ssh_name(key));
1.14 markus 742: buffer_put_bignum2(&b, key->rsa->e);
1.12 markus 743: buffer_put_bignum2(&b, key->rsa->n);
744: break;
745: default:
1.31 markus 746: error("key_to_blob: unsupported key type %d", key->type);
747: buffer_free(&b);
748: return 0;
1.12 markus 749: }
750: len = buffer_len(&b);
751: buf = xmalloc(len);
752: memcpy(buf, buffer_ptr(&b), len);
753: memset(buffer_ptr(&b), 0, len);
754: buffer_free(&b);
755: if (lenp != NULL)
756: *lenp = len;
757: if (blobp != NULL)
758: *blobp = buf;
759: return len;
760: }
761:
762: int
763: key_sign(
764: Key *key,
1.40 markus 765: u_char **sigp, u_int *lenp,
766: u_char *data, u_int datalen)
1.12 markus 767: {
1.35 deraadt 768: switch (key->type) {
1.12 markus 769: case KEY_DSA:
770: return ssh_dss_sign(key, sigp, lenp, data, datalen);
771: break;
772: case KEY_RSA:
773: return ssh_rsa_sign(key, sigp, lenp, data, datalen);
774: break;
775: default:
776: error("key_sign: illegal key type %d", key->type);
777: return -1;
778: break;
779: }
780: }
781:
782: int
783: key_verify(
784: Key *key,
1.40 markus 785: u_char *signature, u_int signaturelen,
786: u_char *data, u_int datalen)
1.12 markus 787: {
1.26 markus 788: if (signaturelen == 0)
789: return -1;
790:
1.35 deraadt 791: switch (key->type) {
1.12 markus 792: case KEY_DSA:
793: return ssh_dss_verify(key, signature, signaturelen, data, datalen);
794: break;
795: case KEY_RSA:
796: return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
797: break;
798: default:
799: error("key_verify: illegal key type %d", key->type);
800: return -1;
801: break;
802: }
1.4 markus 803: }