Annotation of src/usr.bin/ssh/key.c, Revision 1.86
1.86 ! stevesk 1: /* $OpenBSD: key.c,v 1.85 2010/03/04 01:44:57 djm 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.83 djm 51: #include "ssh2.h"
52:
53: static struct KeyCert *
54: cert_new(void)
55: {
56: struct KeyCert *cert;
57:
58: cert = xcalloc(1, sizeof(*cert));
59: buffer_init(&cert->certblob);
60: buffer_init(&cert->constraints);
61: cert->key_id = NULL;
62: cert->principals = NULL;
63: cert->signature_key = NULL;
64: return cert;
65: }
1.1 markus 66:
67: Key *
68: key_new(int type)
69: {
70: Key *k;
71: RSA *rsa;
72: DSA *dsa;
1.63 djm 73: k = xcalloc(1, sizeof(*k));
1.1 markus 74: k->type = type;
1.3 markus 75: k->dsa = NULL;
76: k->rsa = NULL;
1.83 djm 77: k->cert = NULL;
1.1 markus 78: switch (k->type) {
1.12 markus 79: case KEY_RSA1:
1.1 markus 80: case KEY_RSA:
1.83 djm 81: case KEY_RSA_CERT:
1.38 markus 82: if ((rsa = RSA_new()) == NULL)
83: fatal("key_new: RSA_new failed");
84: if ((rsa->n = BN_new()) == NULL)
85: fatal("key_new: BN_new failed");
86: if ((rsa->e = BN_new()) == NULL)
87: fatal("key_new: BN_new failed");
1.1 markus 88: k->rsa = rsa;
89: break;
90: case KEY_DSA:
1.83 djm 91: case KEY_DSA_CERT:
1.38 markus 92: if ((dsa = DSA_new()) == NULL)
93: fatal("key_new: DSA_new failed");
94: if ((dsa->p = BN_new()) == NULL)
95: fatal("key_new: BN_new failed");
96: if ((dsa->q = BN_new()) == NULL)
97: fatal("key_new: BN_new failed");
98: if ((dsa->g = BN_new()) == NULL)
99: fatal("key_new: BN_new failed");
100: if ((dsa->pub_key = BN_new()) == NULL)
101: fatal("key_new: BN_new failed");
1.1 markus 102: k->dsa = dsa;
103: break;
1.12 markus 104: case KEY_UNSPEC:
1.1 markus 105: break;
106: default:
107: fatal("key_new: bad key type %d", k->type);
108: break;
109: }
1.83 djm 110:
111: if (key_is_cert(k))
112: k->cert = cert_new();
113:
1.1 markus 114: return k;
115: }
1.45 deraadt 116:
1.83 djm 117: void
118: key_add_private(Key *k)
1.12 markus 119: {
120: switch (k->type) {
121: case KEY_RSA1:
122: case KEY_RSA:
1.83 djm 123: case KEY_RSA_CERT:
1.38 markus 124: if ((k->rsa->d = BN_new()) == NULL)
125: fatal("key_new_private: BN_new failed");
126: if ((k->rsa->iqmp = BN_new()) == NULL)
127: fatal("key_new_private: BN_new failed");
128: if ((k->rsa->q = BN_new()) == NULL)
129: fatal("key_new_private: BN_new failed");
130: if ((k->rsa->p = BN_new()) == NULL)
131: fatal("key_new_private: BN_new failed");
132: if ((k->rsa->dmq1 = BN_new()) == NULL)
133: fatal("key_new_private: BN_new failed");
134: if ((k->rsa->dmp1 = BN_new()) == NULL)
135: fatal("key_new_private: BN_new failed");
1.12 markus 136: break;
137: case KEY_DSA:
1.83 djm 138: case KEY_DSA_CERT:
1.38 markus 139: if ((k->dsa->priv_key = BN_new()) == NULL)
140: fatal("key_new_private: BN_new failed");
1.12 markus 141: break;
142: case KEY_UNSPEC:
143: break;
144: default:
145: break;
146: }
1.83 djm 147: }
148:
149: Key *
150: key_new_private(int type)
151: {
152: Key *k = key_new(type);
153:
154: key_add_private(k);
1.12 markus 155: return k;
156: }
1.45 deraadt 157:
1.83 djm 158: static void
159: cert_free(struct KeyCert *cert)
160: {
161: u_int i;
162:
163: buffer_free(&cert->certblob);
164: buffer_free(&cert->constraints);
165: if (cert->key_id != NULL)
166: xfree(cert->key_id);
167: for (i = 0; i < cert->nprincipals; i++)
168: xfree(cert->principals[i]);
169: if (cert->principals != NULL)
170: xfree(cert->principals);
171: if (cert->signature_key != NULL)
172: key_free(cert->signature_key);
173: }
174:
1.1 markus 175: void
176: key_free(Key *k)
177: {
1.60 djm 178: if (k == NULL)
1.62 deraadt 179: fatal("key_free: key is NULL");
1.1 markus 180: switch (k->type) {
1.12 markus 181: case KEY_RSA1:
1.1 markus 182: case KEY_RSA:
1.83 djm 183: case KEY_RSA_CERT:
1.1 markus 184: if (k->rsa != NULL)
185: RSA_free(k->rsa);
186: k->rsa = NULL;
187: break;
188: case KEY_DSA:
1.83 djm 189: case KEY_DSA_CERT:
1.1 markus 190: if (k->dsa != NULL)
191: DSA_free(k->dsa);
192: k->dsa = NULL;
193: break;
1.12 markus 194: case KEY_UNSPEC:
195: break;
1.1 markus 196: default:
197: fatal("key_free: bad key type %d", k->type);
198: break;
199: }
1.83 djm 200: if (key_is_cert(k)) {
201: if (k->cert != NULL)
202: cert_free(k->cert);
203: k->cert = NULL;
204: }
205:
1.1 markus 206: xfree(k);
207: }
1.55 jakob 208:
1.83 djm 209: static int
210: cert_compare(struct KeyCert *a, struct KeyCert *b)
211: {
212: if (a == NULL && b == NULL)
213: return 1;
214: if (a == NULL || b == NULL)
215: return 0;
216: if (buffer_len(&a->certblob) != buffer_len(&b->certblob))
217: return 0;
218: if (memcmp(buffer_ptr(&a->certblob), buffer_ptr(&b->certblob),
219: buffer_len(&a->certblob)) != 0)
220: return 0;
221: return 1;
222: }
223:
224: /*
225: * Compare public portions of key only, allowing comparisons between
226: * certificates and plain keys too.
227: */
1.1 markus 228: int
1.83 djm 229: key_equal_public(const Key *a, const Key *b)
1.1 markus 230: {
1.83 djm 231: if (a == NULL || b == NULL ||
232: key_type_plain(a->type) != key_type_plain(b->type))
1.1 markus 233: return 0;
1.83 djm 234:
1.1 markus 235: switch (a->type) {
1.12 markus 236: case KEY_RSA1:
1.83 djm 237: case KEY_RSA_CERT:
1.1 markus 238: case KEY_RSA:
239: return a->rsa != NULL && b->rsa != NULL &&
240: BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
241: BN_cmp(a->rsa->n, b->rsa->n) == 0;
1.83 djm 242: case KEY_DSA_CERT:
1.1 markus 243: case KEY_DSA:
244: return a->dsa != NULL && b->dsa != NULL &&
245: BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
246: BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
247: BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
248: BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
249: default:
1.3 markus 250: fatal("key_equal: bad key type %d", a->type);
1.1 markus 251: }
1.78 stevesk 252: /* NOTREACHED */
1.1 markus 253: }
254:
1.83 djm 255: int
256: key_equal(const Key *a, const Key *b)
257: {
258: if (a == NULL || b == NULL || a->type != b->type)
259: return 0;
260: if (key_is_cert(a)) {
261: if (!cert_compare(a->cert, b->cert))
262: return 0;
263: }
264: return key_equal_public(a, b);
265: }
266:
1.52 jakob 267: u_char*
1.83 djm 268: key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
1.1 markus 269: {
1.41 markus 270: const EVP_MD *md = NULL;
1.21 markus 271: EVP_MD_CTX ctx;
1.13 markus 272: u_char *blob = NULL;
1.19 jakob 273: u_char *retval = NULL;
1.40 markus 274: u_int len = 0;
1.83 djm 275: int nlen, elen, otype;
1.1 markus 276:
1.19 jakob 277: *dgst_raw_length = 0;
278:
1.21 markus 279: switch (dgst_type) {
280: case SSH_FP_MD5:
281: md = EVP_md5();
282: break;
283: case SSH_FP_SHA1:
284: md = EVP_sha1();
285: break;
286: default:
287: fatal("key_fingerprint_raw: bad digest type %d",
288: dgst_type);
289: }
1.1 markus 290: switch (k->type) {
1.12 markus 291: case KEY_RSA1:
1.1 markus 292: nlen = BN_num_bytes(k->rsa->n);
293: elen = BN_num_bytes(k->rsa->e);
294: len = nlen + elen;
1.3 markus 295: blob = xmalloc(len);
296: BN_bn2bin(k->rsa->n, blob);
297: BN_bn2bin(k->rsa->e, blob + nlen);
1.1 markus 298: break;
299: case KEY_DSA:
1.12 markus 300: case KEY_RSA:
301: key_to_blob(k, &blob, &len);
302: break;
1.83 djm 303: case KEY_DSA_CERT:
304: case KEY_RSA_CERT:
305: /* We want a fingerprint of the _key_ not of the cert */
306: otype = k->type;
307: k->type = key_type_plain(k->type);
308: key_to_blob(k, &blob, &len);
309: k->type = otype;
310: break;
1.12 markus 311: case KEY_UNSPEC:
312: return retval;
1.1 markus 313: default:
1.19 jakob 314: fatal("key_fingerprint_raw: bad key type %d", k->type);
1.1 markus 315: break;
316: }
1.3 markus 317: if (blob != NULL) {
1.19 jakob 318: retval = xmalloc(EVP_MAX_MD_SIZE);
1.8 markus 319: EVP_DigestInit(&ctx, md);
320: EVP_DigestUpdate(&ctx, blob, len);
1.39 markus 321: EVP_DigestFinal(&ctx, retval, dgst_raw_length);
1.3 markus 322: memset(blob, 0, len);
323: xfree(blob);
1.19 jakob 324: } else {
325: fatal("key_fingerprint_raw: blob is null");
1.1 markus 326: }
1.19 jakob 327: return retval;
328: }
329:
1.46 deraadt 330: static char *
331: key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
1.19 jakob 332: {
333: char *retval;
1.58 djm 334: u_int i;
1.19 jakob 335:
1.63 djm 336: retval = xcalloc(1, dgst_raw_len * 3 + 1);
1.36 deraadt 337: for (i = 0; i < dgst_raw_len; i++) {
1.19 jakob 338: char hex[4];
339: snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
1.54 avsm 340: strlcat(retval, hex, dgst_raw_len * 3 + 1);
1.19 jakob 341: }
1.54 avsm 342:
343: /* Remove the trailing ':' character */
1.19 jakob 344: retval[(dgst_raw_len * 3) - 1] = '\0';
345: return retval;
346: }
347:
1.46 deraadt 348: static char *
349: key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
1.19 jakob 350: {
351: char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
352: char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
353: 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
1.20 jakob 354: u_int i, j = 0, rounds, seed = 1;
1.19 jakob 355: char *retval;
356:
357: rounds = (dgst_raw_len / 2) + 1;
1.63 djm 358: retval = xcalloc((rounds * 6), sizeof(char));
1.20 jakob 359: retval[j++] = 'x';
360: for (i = 0; i < rounds; i++) {
1.19 jakob 361: u_int idx0, idx1, idx2, idx3, idx4;
1.20 jakob 362: if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
363: idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
1.19 jakob 364: seed) % 6;
1.20 jakob 365: idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
366: idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
1.19 jakob 367: (seed / 6)) % 6;
1.20 jakob 368: retval[j++] = vowels[idx0];
369: retval[j++] = consonants[idx1];
370: retval[j++] = vowels[idx2];
371: if ((i + 1) < rounds) {
372: idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
373: idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
374: retval[j++] = consonants[idx3];
375: retval[j++] = '-';
376: retval[j++] = consonants[idx4];
1.19 jakob 377: seed = ((seed * 5) +
1.20 jakob 378: ((((u_int)(dgst_raw[2 * i])) * 7) +
379: ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
1.19 jakob 380: }
381: } else {
382: idx0 = seed % 6;
383: idx1 = 16;
384: idx2 = seed / 6;
1.20 jakob 385: retval[j++] = vowels[idx0];
386: retval[j++] = consonants[idx1];
387: retval[j++] = vowels[idx2];
1.19 jakob 388: }
389: }
1.20 jakob 390: retval[j++] = 'x';
391: retval[j++] = '\0';
1.19 jakob 392: return retval;
393: }
394:
1.70 grunk 395: /*
396: * Draw an ASCII-Art representing the fingerprint so human brain can
397: * profit from its built-in pattern recognition ability.
398: * This technique is called "random art" and can be found in some
399: * scientific publications like this original paper:
400: *
401: * "Hash Visualization: a New Technique to improve Real-World Security",
402: * Perrig A. and Song D., 1999, International Workshop on Cryptographic
403: * Techniques and E-Commerce (CrypTEC '99)
404: * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
405: *
406: * The subject came up in a talk by Dan Kaminsky, too.
407: *
408: * If you see the picture is different, the key is different.
409: * If the picture looks the same, you still know nothing.
410: *
411: * The algorithm used here is a worm crawling over a discrete plane,
412: * leaving a trace (augmenting the field) everywhere it goes.
413: * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
414: * makes the respective movement vector be ignored for this turn.
415: * Graphs are not unambiguous, because circles in graphs can be
416: * walked in either direction.
417: */
1.74 grunk 418:
419: /*
420: * Field sizes for the random art. Have to be odd, so the starting point
421: * can be in the exact middle of the picture, and FLDBASE should be >=8 .
422: * Else pictures would be too dense, and drawing the frame would
423: * fail, too, because the key type would not fit in anymore.
424: */
425: #define FLDBASE 8
426: #define FLDSIZE_Y (FLDBASE + 1)
427: #define FLDSIZE_X (FLDBASE * 2 + 1)
1.70 grunk 428: static char *
1.74 grunk 429: key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k)
1.70 grunk 430: {
431: /*
432: * Chars to be used after each other every time the worm
433: * intersects with itself. Matter of taste.
434: */
1.75 grunk 435: char *augmentation_string = " .o+=*BOX@%&#/^SE";
1.70 grunk 436: char *retval, *p;
1.71 otto 437: u_char field[FLDSIZE_X][FLDSIZE_Y];
1.70 grunk 438: u_int i, b;
439: int x, y;
1.72 grunk 440: size_t len = strlen(augmentation_string) - 1;
1.70 grunk 441:
442: retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
443:
444: /* initialize field */
1.71 otto 445: memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1.70 grunk 446: x = FLDSIZE_X / 2;
447: y = FLDSIZE_Y / 2;
448:
449: /* process raw key */
450: for (i = 0; i < dgst_raw_len; i++) {
451: int input;
452: /* each byte conveys four 2-bit move commands */
453: input = dgst_raw[i];
454: for (b = 0; b < 4; b++) {
455: /* evaluate 2 bit, rest is shifted later */
456: x += (input & 0x1) ? 1 : -1;
457: y += (input & 0x2) ? 1 : -1;
458:
459: /* assure we are still in bounds */
460: x = MAX(x, 0);
461: y = MAX(y, 0);
462: x = MIN(x, FLDSIZE_X - 1);
463: y = MIN(y, FLDSIZE_Y - 1);
464:
465: /* augment the field */
1.79 grunk 466: if (field[x][y] < len - 2)
467: field[x][y]++;
1.70 grunk 468: input = input >> 2;
469: }
470: }
1.75 grunk 471:
472: /* mark starting point and end point*/
473: field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
474: field[x][y] = len;
1.70 grunk 475:
476: /* fill in retval */
1.77 otto 477: snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k));
1.74 grunk 478: p = strchr(retval, '\0');
1.70 grunk 479:
480: /* output upper border */
1.77 otto 481: for (i = p - retval - 1; i < FLDSIZE_X; i++)
1.70 grunk 482: *p++ = '-';
483: *p++ = '+';
484: *p++ = '\n';
485:
486: /* output content */
487: for (y = 0; y < FLDSIZE_Y; y++) {
488: *p++ = '|';
489: for (x = 0; x < FLDSIZE_X; x++)
1.72 grunk 490: *p++ = augmentation_string[MIN(field[x][y], len)];
1.70 grunk 491: *p++ = '|';
492: *p++ = '\n';
493: }
494:
495: /* output lower border */
496: *p++ = '+';
497: for (i = 0; i < FLDSIZE_X; i++)
498: *p++ = '-';
499: *p++ = '+';
500:
501: return retval;
502: }
503:
1.46 deraadt 504: char *
1.83 djm 505: key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
1.19 jakob 506: {
1.23 markus 507: char *retval = NULL;
1.19 jakob 508: u_char *dgst_raw;
1.39 markus 509: u_int dgst_raw_len;
1.36 deraadt 510:
1.19 jakob 511: dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
512: if (!dgst_raw)
1.22 markus 513: fatal("key_fingerprint: null from key_fingerprint_raw()");
1.35 deraadt 514: switch (dgst_rep) {
1.19 jakob 515: case SSH_FP_HEX:
516: retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
517: break;
518: case SSH_FP_BUBBLEBABBLE:
519: retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1.70 grunk 520: break;
521: case SSH_FP_RANDOMART:
1.74 grunk 522: retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k);
1.19 jakob 523: break;
524: default:
1.80 stevesk 525: fatal("key_fingerprint: bad digest representation %d",
1.19 jakob 526: dgst_rep);
527: break;
528: }
529: memset(dgst_raw, 0, dgst_raw_len);
530: xfree(dgst_raw);
1.1 markus 531: return retval;
532: }
533:
534: /*
535: * Reads a multiple-precision integer in decimal from the buffer, and advances
536: * the pointer. The integer must already be initialized. This function is
537: * permitted to modify the buffer. This leaves *cpp to point just beyond the
538: * last processed (and maybe modified) character. Note that this may modify
539: * the buffer containing the number.
540: */
1.27 itojun 541: static int
1.1 markus 542: read_bignum(char **cpp, BIGNUM * value)
543: {
544: char *cp = *cpp;
545: int old;
546:
547: /* Skip any leading whitespace. */
548: for (; *cp == ' ' || *cp == '\t'; cp++)
549: ;
550:
551: /* Check that it begins with a decimal digit. */
552: if (*cp < '0' || *cp > '9')
553: return 0;
554:
555: /* Save starting position. */
556: *cpp = cp;
557:
558: /* Move forward until all decimal digits skipped. */
559: for (; *cp >= '0' && *cp <= '9'; cp++)
560: ;
561:
562: /* Save the old terminating character, and replace it by \0. */
563: old = *cp;
564: *cp = 0;
565:
566: /* Parse the number. */
567: if (BN_dec2bn(&value, *cpp) == 0)
568: return 0;
569:
570: /* Restore old terminating character. */
571: *cp = old;
572:
573: /* Move beyond the number and return success. */
574: *cpp = cp;
575: return 1;
576: }
1.45 deraadt 577:
1.27 itojun 578: static int
1.1 markus 579: write_bignum(FILE *f, BIGNUM *num)
580: {
581: char *buf = BN_bn2dec(num);
582: if (buf == NULL) {
583: error("write_bignum: BN_bn2dec() failed");
584: return 0;
585: }
586: fprintf(f, " %s", buf);
1.33 markus 587: OPENSSL_free(buf);
1.1 markus 588: return 1;
589: }
1.12 markus 590:
1.32 markus 591: /* returns 1 ok, -1 error */
1.12 markus 592: int
1.3 markus 593: key_read(Key *ret, char **cpp)
1.1 markus 594: {
1.3 markus 595: Key *k;
1.12 markus 596: int success = -1;
597: char *cp, *space;
598: int len, n, type;
599: u_int bits;
1.13 markus 600: u_char *blob;
1.3 markus 601:
602: cp = *cpp;
603:
1.35 deraadt 604: switch (ret->type) {
1.12 markus 605: case KEY_RSA1:
1.3 markus 606: /* Get number of bits. */
607: if (*cp < '0' || *cp > '9')
1.12 markus 608: return -1; /* Bad bit count... */
1.3 markus 609: for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
610: bits = 10 * bits + *cp - '0';
1.1 markus 611: if (bits == 0)
1.12 markus 612: return -1;
1.3 markus 613: *cpp = cp;
1.1 markus 614: /* Get public exponent, public modulus. */
615: if (!read_bignum(cpp, ret->rsa->e))
1.12 markus 616: return -1;
1.1 markus 617: if (!read_bignum(cpp, ret->rsa->n))
1.12 markus 618: return -1;
1.82 dtucker 619: /* validate the claimed number of bits */
620: if ((u_int)BN_num_bits(ret->rsa->n) != bits) {
621: verbose("key_read: claimed key size %d does not match "
622: "actual %d", bits, BN_num_bits(ret->rsa->n));
623: return -1;
624: }
1.12 markus 625: success = 1;
1.1 markus 626: break;
1.12 markus 627: case KEY_UNSPEC:
628: case KEY_RSA:
1.1 markus 629: case KEY_DSA:
1.83 djm 630: case KEY_DSA_CERT:
631: case KEY_RSA_CERT:
1.12 markus 632: space = strchr(cp, ' ');
633: if (space == NULL) {
1.50 markus 634: debug3("key_read: missing whitespace");
1.12 markus 635: return -1;
636: }
637: *space = '\0';
638: type = key_type_from_name(cp);
639: *space = ' ';
640: if (type == KEY_UNSPEC) {
1.50 markus 641: debug3("key_read: missing keytype");
1.12 markus 642: return -1;
643: }
644: cp = space+1;
645: if (*cp == '\0') {
646: debug3("key_read: short string");
647: return -1;
648: }
649: if (ret->type == KEY_UNSPEC) {
650: ret->type = type;
651: } else if (ret->type != type) {
652: /* is a key, but different type */
653: debug3("key_read: type mismatch");
1.32 markus 654: return -1;
1.12 markus 655: }
1.3 markus 656: len = 2*strlen(cp);
657: blob = xmalloc(len);
658: n = uudecode(cp, blob, len);
1.6 markus 659: if (n < 0) {
1.7 markus 660: error("key_read: uudecode %s failed", cp);
1.34 markus 661: xfree(blob);
1.12 markus 662: return -1;
1.6 markus 663: }
1.53 markus 664: k = key_from_blob(blob, (u_int)n);
1.34 markus 665: xfree(blob);
1.7 markus 666: if (k == NULL) {
1.12 markus 667: error("key_read: key_from_blob %s failed", cp);
668: return -1;
1.7 markus 669: }
1.12 markus 670: if (k->type != type) {
671: error("key_read: type mismatch: encoding error");
672: key_free(k);
673: return -1;
674: }
675: /*XXXX*/
1.83 djm 676: if (key_is_cert(ret)) {
677: if (!key_is_cert(k)) {
678: error("key_read: loaded key is not a cert");
679: key_free(k);
680: return -1;
681: }
682: if (ret->cert != NULL)
683: cert_free(ret->cert);
684: ret->cert = k->cert;
685: k->cert = NULL;
686: }
687: if (key_type_plain(ret->type) == KEY_RSA) {
1.12 markus 688: if (ret->rsa != NULL)
689: RSA_free(ret->rsa);
690: ret->rsa = k->rsa;
691: k->rsa = NULL;
692: #ifdef DEBUG_PK
693: RSA_print_fp(stderr, ret->rsa, 8);
694: #endif
1.83 djm 695: }
696: if (key_type_plain(ret->type) == KEY_DSA) {
1.12 markus 697: if (ret->dsa != NULL)
698: DSA_free(ret->dsa);
699: ret->dsa = k->dsa;
700: k->dsa = NULL;
701: #ifdef DEBUG_PK
702: DSA_print_fp(stderr, ret->dsa, 8);
703: #endif
704: }
1.83 djm 705: success = 1;
1.12 markus 706: /*XXXX*/
1.34 markus 707: key_free(k);
1.12 markus 708: if (success != 1)
709: break;
1.7 markus 710: /* advance cp: skip whitespace and data */
711: while (*cp == ' ' || *cp == '\t')
712: cp++;
713: while (*cp != '\0' && *cp != ' ' && *cp != '\t')
714: cp++;
715: *cpp = cp;
1.1 markus 716: break;
717: default:
1.3 markus 718: fatal("key_read: bad key type: %d", ret->type);
1.1 markus 719: break;
720: }
1.12 markus 721: return success;
1.1 markus 722: }
1.45 deraadt 723:
1.1 markus 724: int
1.55 jakob 725: key_write(const Key *key, FILE *f)
1.1 markus 726: {
1.40 markus 727: int n, success = 0;
728: u_int len, bits = 0;
1.49 markus 729: u_char *blob;
730: char *uu;
1.1 markus 731:
1.83 djm 732: if (key_is_cert(key)) {
733: if (key->cert == NULL) {
734: error("%s: no cert data", __func__);
735: return 0;
736: }
737: if (buffer_len(&key->cert->certblob) == 0) {
738: error("%s: no signed certificate blob", __func__);
739: return 0;
740: }
741: }
742:
743: switch (key->type) {
744: case KEY_RSA1:
745: if (key->rsa == NULL)
746: return 0;
1.1 markus 747: /* size of modulus 'n' */
748: bits = BN_num_bits(key->rsa->n);
749: fprintf(f, "%u", bits);
750: if (write_bignum(f, key->rsa->e) &&
1.83 djm 751: write_bignum(f, key->rsa->n))
752: return 1;
753: error("key_write: failed for RSA key");
754: return 0;
755: case KEY_DSA:
756: case KEY_DSA_CERT:
757: if (key->dsa == NULL)
758: return 0;
759: break;
760: case KEY_RSA:
761: case KEY_RSA_CERT:
762: if (key->rsa == NULL)
763: return 0;
764: break;
765: default:
766: return 0;
767: }
768:
769: key_to_blob(key, &blob, &len);
770: uu = xmalloc(2*len);
771: n = uuencode(blob, len, uu, 2*len);
772: if (n > 0) {
773: fprintf(f, "%s %s", key_ssh_name(key), uu);
774: success = 1;
1.1 markus 775: }
1.83 djm 776: xfree(blob);
777: xfree(uu);
778:
1.1 markus 779: return success;
780: }
1.45 deraadt 781:
1.55 jakob 782: const char *
783: key_type(const Key *k)
1.4 markus 784: {
785: switch (k->type) {
1.12 markus 786: case KEY_RSA1:
787: return "RSA1";
1.4 markus 788: case KEY_RSA:
789: return "RSA";
790: case KEY_DSA:
791: return "DSA";
1.83 djm 792: case KEY_RSA_CERT:
793: return "RSA-CERT";
794: case KEY_DSA_CERT:
795: return "DSA-CERT";
1.4 markus 796: }
797: return "unknown";
1.86 ! stevesk 798: }
! 799:
! 800: const char *
! 801: key_cert_type(const Key *k)
! 802: {
! 803: switch (k->cert->type) {
! 804: case SSH2_CERT_TYPE_USER:
! 805: return "user";
! 806: case SSH2_CERT_TYPE_HOST:
! 807: return "host";
! 808: default:
! 809: return "unknown";
! 810: }
1.10 markus 811: }
1.45 deraadt 812:
1.55 jakob 813: const char *
814: key_ssh_name(const Key *k)
1.12 markus 815: {
816: switch (k->type) {
817: case KEY_RSA:
818: return "ssh-rsa";
819: case KEY_DSA:
820: return "ssh-dss";
1.83 djm 821: case KEY_RSA_CERT:
822: return "ssh-rsa-cert-v00@openssh.com";
823: case KEY_DSA_CERT:
824: return "ssh-dss-cert-v00@openssh.com";
1.12 markus 825: }
826: return "ssh-unknown";
827: }
1.45 deraadt 828:
1.12 markus 829: u_int
1.55 jakob 830: key_size(const Key *k)
1.35 deraadt 831: {
1.10 markus 832: switch (k->type) {
1.12 markus 833: case KEY_RSA1:
1.10 markus 834: case KEY_RSA:
1.83 djm 835: case KEY_RSA_CERT:
1.10 markus 836: return BN_num_bits(k->rsa->n);
837: case KEY_DSA:
1.83 djm 838: case KEY_DSA_CERT:
1.10 markus 839: return BN_num_bits(k->dsa->p);
840: }
841: return 0;
1.12 markus 842: }
843:
1.27 itojun 844: static RSA *
1.13 markus 845: rsa_generate_private_key(u_int bits)
1.12 markus 846: {
1.17 stevesk 847: RSA *private;
1.61 deraadt 848:
1.81 markus 849: private = RSA_generate_key(bits, RSA_F4, NULL, NULL);
1.17 stevesk 850: if (private == NULL)
851: fatal("rsa_generate_private_key: key generation failed.");
852: return private;
1.12 markus 853: }
854:
1.27 itojun 855: static DSA*
1.13 markus 856: dsa_generate_private_key(u_int bits)
1.12 markus 857: {
858: DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
1.61 deraadt 859:
1.12 markus 860: if (private == NULL)
861: fatal("dsa_generate_private_key: DSA_generate_parameters failed");
862: if (!DSA_generate_key(private))
1.17 stevesk 863: fatal("dsa_generate_private_key: DSA_generate_key failed.");
864: if (private == NULL)
865: fatal("dsa_generate_private_key: NULL.");
1.12 markus 866: return private;
867: }
868:
869: Key *
1.13 markus 870: key_generate(int type, u_int bits)
1.12 markus 871: {
872: Key *k = key_new(KEY_UNSPEC);
873: switch (type) {
1.17 stevesk 874: case KEY_DSA:
1.12 markus 875: k->dsa = dsa_generate_private_key(bits);
876: break;
877: case KEY_RSA:
878: case KEY_RSA1:
879: k->rsa = rsa_generate_private_key(bits);
880: break;
1.83 djm 881: case KEY_RSA_CERT:
882: case KEY_DSA_CERT:
883: fatal("key_generate: cert keys cannot be generated directly");
1.12 markus 884: default:
1.17 stevesk 885: fatal("key_generate: unknown type %d", type);
1.12 markus 886: }
1.17 stevesk 887: k->type = type;
1.12 markus 888: return k;
889: }
890:
1.83 djm 891: void
892: key_cert_copy(const Key *from_key, struct Key *to_key)
893: {
894: u_int i;
895: const struct KeyCert *from;
896: struct KeyCert *to;
897:
898: if (to_key->cert != NULL) {
899: cert_free(to_key->cert);
900: to_key->cert = NULL;
901: }
902:
903: if ((from = from_key->cert) == NULL)
904: return;
905:
906: to = to_key->cert = cert_new();
907:
908: buffer_append(&to->certblob, buffer_ptr(&from->certblob),
909: buffer_len(&from->certblob));
910:
911: buffer_append(&to->constraints, buffer_ptr(&from->constraints),
912: buffer_len(&from->constraints));
913:
914: to->type = from->type;
915: to->key_id = from->key_id == NULL ? NULL : xstrdup(from->key_id);
916: to->valid_after = from->valid_after;
917: to->valid_before = from->valid_before;
918: to->signature_key = from->signature_key == NULL ?
919: NULL : key_from_private(from->signature_key);
920:
921: to->nprincipals = from->nprincipals;
922: if (to->nprincipals > CERT_MAX_PRINCIPALS)
923: fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)",
924: __func__, to->nprincipals, CERT_MAX_PRINCIPALS);
925: if (to->nprincipals > 0) {
926: to->principals = xcalloc(from->nprincipals,
927: sizeof(*to->principals));
928: for (i = 0; i < to->nprincipals; i++)
929: to->principals[i] = xstrdup(from->principals[i]);
930: }
931: }
932:
1.12 markus 933: Key *
1.55 jakob 934: key_from_private(const Key *k)
1.12 markus 935: {
936: Key *n = NULL;
937: switch (k->type) {
1.17 stevesk 938: case KEY_DSA:
1.83 djm 939: case KEY_DSA_CERT:
1.12 markus 940: n = key_new(k->type);
1.68 markus 941: if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
942: (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
943: (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
944: (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
945: fatal("key_from_private: BN_copy failed");
1.12 markus 946: break;
947: case KEY_RSA:
948: case KEY_RSA1:
1.83 djm 949: case KEY_RSA_CERT:
1.12 markus 950: n = key_new(k->type);
1.68 markus 951: if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
952: (BN_copy(n->rsa->e, k->rsa->e) == NULL))
953: fatal("key_from_private: BN_copy failed");
1.12 markus 954: break;
955: default:
1.17 stevesk 956: fatal("key_from_private: unknown type %d", k->type);
1.12 markus 957: break;
958: }
1.83 djm 959: if (key_is_cert(k))
960: key_cert_copy(k, n);
1.12 markus 961: return n;
962: }
963:
964: int
965: key_type_from_name(char *name)
966: {
1.35 deraadt 967: if (strcmp(name, "rsa1") == 0) {
1.12 markus 968: return KEY_RSA1;
1.35 deraadt 969: } else if (strcmp(name, "rsa") == 0) {
1.12 markus 970: return KEY_RSA;
1.35 deraadt 971: } else if (strcmp(name, "dsa") == 0) {
1.12 markus 972: return KEY_DSA;
1.35 deraadt 973: } else if (strcmp(name, "ssh-rsa") == 0) {
1.12 markus 974: return KEY_RSA;
1.35 deraadt 975: } else if (strcmp(name, "ssh-dss") == 0) {
1.12 markus 976: return KEY_DSA;
1.83 djm 977: } else if (strcmp(name, "ssh-rsa-cert-v00@openssh.com") == 0) {
978: return KEY_RSA_CERT;
979: } else if (strcmp(name, "ssh-dss-cert-v00@openssh.com") == 0) {
980: return KEY_DSA_CERT;
1.12 markus 981: }
1.18 markus 982: debug2("key_type_from_name: unknown key type '%s'", name);
1.12 markus 983: return KEY_UNSPEC;
1.25 markus 984: }
985:
986: int
987: key_names_valid2(const char *names)
988: {
989: char *s, *cp, *p;
990:
991: if (names == NULL || strcmp(names, "") == 0)
992: return 0;
993: s = cp = xstrdup(names);
994: for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36 deraadt 995: (p = strsep(&cp, ","))) {
1.25 markus 996: switch (key_type_from_name(p)) {
997: case KEY_RSA1:
998: case KEY_UNSPEC:
999: xfree(s);
1000: return 0;
1001: }
1002: }
1003: debug3("key names ok: [%s]", names);
1004: xfree(s);
1005: return 1;
1.12 markus 1006: }
1007:
1.83 djm 1008: static int
1009: cert_parse(Buffer *b, Key *key, const u_char *blob, u_int blen)
1010: {
1011: u_char *principals, *constraints, *sig_key, *sig;
1.84 djm 1012: u_int signed_len, plen, clen, sklen, slen, kidlen;
1.83 djm 1013: Buffer tmp;
1014: char *principal;
1015: int ret = -1;
1016:
1017: buffer_init(&tmp);
1018:
1019: /* Copy the entire key blob for verification and later serialisation */
1020: buffer_append(&key->cert->certblob, blob, blen);
1021:
1022: principals = constraints = sig_key = sig = NULL;
1023: if (buffer_get_int_ret(&key->cert->type, b) != 0 ||
1.84 djm 1024: (key->cert->key_id = buffer_get_string_ret(b, &kidlen)) == NULL ||
1.83 djm 1025: (principals = buffer_get_string_ret(b, &plen)) == NULL ||
1026: buffer_get_int64_ret(&key->cert->valid_after, b) != 0 ||
1027: buffer_get_int64_ret(&key->cert->valid_before, b) != 0 ||
1028: (constraints = buffer_get_string_ret(b, &clen)) == NULL ||
1029: /* skip nonce */ buffer_get_string_ptr_ret(b, NULL) == NULL ||
1030: /* skip reserved */ buffer_get_string_ptr_ret(b, NULL) == NULL ||
1031: (sig_key = buffer_get_string_ret(b, &sklen)) == NULL) {
1032: error("%s: parse error", __func__);
1033: goto out;
1034: }
1035:
1.84 djm 1036: if (kidlen != strlen(key->cert->key_id)) {
1037: error("%s: key ID contains \\0 character", __func__);
1038: goto out;
1039: }
1040:
1.83 djm 1041: /* Signature is left in the buffer so we can calculate this length */
1042: signed_len = buffer_len(&key->cert->certblob) - buffer_len(b);
1043:
1044: if ((sig = buffer_get_string_ret(b, &slen)) == NULL) {
1045: error("%s: parse error", __func__);
1046: goto out;
1047: }
1048:
1049: if (key->cert->type != SSH2_CERT_TYPE_USER &&
1050: key->cert->type != SSH2_CERT_TYPE_HOST) {
1051: error("Unknown certificate type %u", key->cert->type);
1052: goto out;
1053: }
1054:
1055: buffer_append(&tmp, principals, plen);
1056: while (buffer_len(&tmp) > 0) {
1057: if (key->cert->nprincipals >= CERT_MAX_PRINCIPALS) {
1.84 djm 1058: error("%s: Too many principals", __func__);
1.83 djm 1059: goto out;
1060: }
1.84 djm 1061: if ((principal = buffer_get_string_ret(&tmp, &plen)) == NULL) {
1062: error("%s: Principals data invalid", __func__);
1063: goto out;
1064: }
1065: if (strlen(principal) != plen) {
1066: error("%s: Principal contains \\0 character",
1067: __func__);
1.83 djm 1068: goto out;
1069: }
1070: key->cert->principals = xrealloc(key->cert->principals,
1071: key->cert->nprincipals + 1, sizeof(*key->cert->principals));
1072: key->cert->principals[key->cert->nprincipals++] = principal;
1073: }
1074:
1075: buffer_clear(&tmp);
1076:
1077: buffer_append(&key->cert->constraints, constraints, clen);
1078: buffer_append(&tmp, constraints, clen);
1079: /* validate structure */
1080: while (buffer_len(&tmp) != 0) {
1.85 djm 1081: if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL ||
1082: buffer_get_string_ptr_ret(&tmp, NULL) == NULL) {
1.84 djm 1083: error("%s: Constraints data invalid", __func__);
1.83 djm 1084: goto out;
1085: }
1086: }
1087: buffer_clear(&tmp);
1088:
1089: if ((key->cert->signature_key = key_from_blob(sig_key,
1090: sklen)) == NULL) {
1.84 djm 1091: error("%s: Signature key invalid", __func__);
1.83 djm 1092: goto out;
1093: }
1094: if (key->cert->signature_key->type != KEY_RSA &&
1095: key->cert->signature_key->type != KEY_DSA) {
1.84 djm 1096: error("%s: Invalid signature key type %s (%d)", __func__,
1.83 djm 1097: key_type(key->cert->signature_key),
1098: key->cert->signature_key->type);
1099: goto out;
1100: }
1101:
1102: switch (key_verify(key->cert->signature_key, sig, slen,
1103: buffer_ptr(&key->cert->certblob), signed_len)) {
1104: case 1:
1.84 djm 1105: ret = 0;
1.83 djm 1106: break; /* Good signature */
1107: case 0:
1.84 djm 1108: error("%s: Invalid signature on certificate", __func__);
1.83 djm 1109: goto out;
1110: case -1:
1.84 djm 1111: error("%s: Certificate signature verification failed",
1112: __func__);
1.83 djm 1113: goto out;
1114: }
1115:
1116: out:
1117: buffer_free(&tmp);
1118: if (principals != NULL)
1119: xfree(principals);
1120: if (constraints != NULL)
1121: xfree(constraints);
1122: if (sig_key != NULL)
1123: xfree(sig_key);
1124: if (sig != NULL)
1125: xfree(sig);
1126: return ret;
1127: }
1128:
1.12 markus 1129: Key *
1.55 jakob 1130: key_from_blob(const u_char *blob, u_int blen)
1.12 markus 1131: {
1132: Buffer b;
1133: int rlen, type;
1.57 djm 1134: char *ktype = NULL;
1.12 markus 1135: Key *key = NULL;
1136:
1137: #ifdef DEBUG_PK
1138: dump_base64(stderr, blob, blen);
1139: #endif
1140: buffer_init(&b);
1141: buffer_append(&b, blob, blen);
1.57 djm 1142: if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
1143: error("key_from_blob: can't read key type");
1144: goto out;
1145: }
1146:
1.12 markus 1147: type = key_type_from_name(ktype);
1148:
1.35 deraadt 1149: switch (type) {
1.12 markus 1150: case KEY_RSA:
1.83 djm 1151: case KEY_RSA_CERT:
1.12 markus 1152: key = key_new(type);
1.57 djm 1153: if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
1154: buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
1155: error("key_from_blob: can't read rsa key");
1.83 djm 1156: badkey:
1.57 djm 1157: key_free(key);
1158: key = NULL;
1159: goto out;
1160: }
1.12 markus 1161: #ifdef DEBUG_PK
1162: RSA_print_fp(stderr, key->rsa, 8);
1163: #endif
1164: break;
1165: case KEY_DSA:
1.83 djm 1166: case KEY_DSA_CERT:
1.12 markus 1167: key = key_new(type);
1.57 djm 1168: if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
1169: buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
1170: buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
1171: buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
1172: error("key_from_blob: can't read dsa key");
1.83 djm 1173: goto badkey;
1.57 djm 1174: }
1.12 markus 1175: #ifdef DEBUG_PK
1176: DSA_print_fp(stderr, key->dsa, 8);
1177: #endif
1178: break;
1179: case KEY_UNSPEC:
1180: key = key_new(type);
1181: break;
1182: default:
1183: error("key_from_blob: cannot handle type %s", ktype);
1.57 djm 1184: goto out;
1.12 markus 1185: }
1.83 djm 1186: if (key_is_cert(key) && cert_parse(&b, key, blob, blen) == -1) {
1187: error("key_from_blob: can't parse cert data");
1188: goto badkey;
1189: }
1.12 markus 1190: rlen = buffer_len(&b);
1191: if (key != NULL && rlen != 0)
1192: error("key_from_blob: remaining bytes in key blob %d", rlen);
1.57 djm 1193: out:
1194: if (ktype != NULL)
1195: xfree(ktype);
1.12 markus 1196: buffer_free(&b);
1197: return key;
1198: }
1199:
1200: int
1.55 jakob 1201: key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1.12 markus 1202: {
1203: Buffer b;
1204: int len;
1205:
1206: if (key == NULL) {
1207: error("key_to_blob: key == NULL");
1208: return 0;
1209: }
1210: buffer_init(&b);
1.35 deraadt 1211: switch (key->type) {
1.83 djm 1212: case KEY_DSA_CERT:
1213: case KEY_RSA_CERT:
1214: /* Use the existing blob */
1215: buffer_append(&b, buffer_ptr(&key->cert->certblob),
1216: buffer_len(&key->cert->certblob));
1217: break;
1.12 markus 1218: case KEY_DSA:
1219: buffer_put_cstring(&b, key_ssh_name(key));
1220: buffer_put_bignum2(&b, key->dsa->p);
1221: buffer_put_bignum2(&b, key->dsa->q);
1222: buffer_put_bignum2(&b, key->dsa->g);
1223: buffer_put_bignum2(&b, key->dsa->pub_key);
1224: break;
1225: case KEY_RSA:
1226: buffer_put_cstring(&b, key_ssh_name(key));
1.14 markus 1227: buffer_put_bignum2(&b, key->rsa->e);
1.12 markus 1228: buffer_put_bignum2(&b, key->rsa->n);
1229: break;
1230: default:
1.31 markus 1231: error("key_to_blob: unsupported key type %d", key->type);
1232: buffer_free(&b);
1233: return 0;
1.12 markus 1234: }
1235: len = buffer_len(&b);
1.48 markus 1236: if (lenp != NULL)
1237: *lenp = len;
1238: if (blobp != NULL) {
1239: *blobp = xmalloc(len);
1240: memcpy(*blobp, buffer_ptr(&b), len);
1241: }
1.12 markus 1242: memset(buffer_ptr(&b), 0, len);
1243: buffer_free(&b);
1244: return len;
1245: }
1246:
1247: int
1248: key_sign(
1.55 jakob 1249: const Key *key,
1.40 markus 1250: u_char **sigp, u_int *lenp,
1.55 jakob 1251: const u_char *data, u_int datalen)
1.12 markus 1252: {
1.35 deraadt 1253: switch (key->type) {
1.83 djm 1254: case KEY_DSA_CERT:
1.12 markus 1255: case KEY_DSA:
1256: return ssh_dss_sign(key, sigp, lenp, data, datalen);
1.83 djm 1257: case KEY_RSA_CERT:
1.12 markus 1258: case KEY_RSA:
1259: return ssh_rsa_sign(key, sigp, lenp, data, datalen);
1260: default:
1.56 markus 1261: error("key_sign: invalid key type %d", key->type);
1.12 markus 1262: return -1;
1263: }
1264: }
1265:
1.44 markus 1266: /*
1267: * key_verify returns 1 for a correct signature, 0 for an incorrect signature
1268: * and -1 on error.
1269: */
1.12 markus 1270: int
1271: key_verify(
1.55 jakob 1272: const Key *key,
1273: const u_char *signature, u_int signaturelen,
1274: const u_char *data, u_int datalen)
1.12 markus 1275: {
1.26 markus 1276: if (signaturelen == 0)
1277: return -1;
1278:
1.35 deraadt 1279: switch (key->type) {
1.83 djm 1280: case KEY_DSA_CERT:
1.12 markus 1281: case KEY_DSA:
1282: return ssh_dss_verify(key, signature, signaturelen, data, datalen);
1.83 djm 1283: case KEY_RSA_CERT:
1.12 markus 1284: case KEY_RSA:
1285: return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
1286: default:
1.56 markus 1287: error("key_verify: invalid key type %d", key->type);
1.12 markus 1288: return -1;
1289: }
1.42 markus 1290: }
1291:
1292: /* Converts a private to a public key */
1293: Key *
1.55 jakob 1294: key_demote(const Key *k)
1.42 markus 1295: {
1296: Key *pk;
1.43 markus 1297:
1.63 djm 1298: pk = xcalloc(1, sizeof(*pk));
1.42 markus 1299: pk->type = k->type;
1300: pk->flags = k->flags;
1301: pk->dsa = NULL;
1302: pk->rsa = NULL;
1303:
1304: switch (k->type) {
1.83 djm 1305: case KEY_RSA_CERT:
1306: key_cert_copy(k, pk);
1307: /* FALLTHROUGH */
1.42 markus 1308: case KEY_RSA1:
1309: case KEY_RSA:
1310: if ((pk->rsa = RSA_new()) == NULL)
1311: fatal("key_demote: RSA_new failed");
1312: if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
1313: fatal("key_demote: BN_dup failed");
1314: if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
1315: fatal("key_demote: BN_dup failed");
1316: break;
1.83 djm 1317: case KEY_DSA_CERT:
1318: key_cert_copy(k, pk);
1319: /* FALLTHROUGH */
1.42 markus 1320: case KEY_DSA:
1321: if ((pk->dsa = DSA_new()) == NULL)
1322: fatal("key_demote: DSA_new failed");
1323: if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
1324: fatal("key_demote: BN_dup failed");
1325: if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
1326: fatal("key_demote: BN_dup failed");
1327: if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
1328: fatal("key_demote: BN_dup failed");
1329: if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
1330: fatal("key_demote: BN_dup failed");
1331: break;
1332: default:
1333: fatal("key_free: bad key type %d", k->type);
1334: break;
1335: }
1336:
1337: return (pk);
1.83 djm 1338: }
1339:
1340: int
1341: key_is_cert(const Key *k)
1342: {
1343: return k != NULL &&
1344: (k->type == KEY_RSA_CERT || k->type == KEY_DSA_CERT);
1345: }
1346:
1347: /* Return the cert-less equivalent to a certified key type */
1348: int
1349: key_type_plain(int type)
1350: {
1351: switch (type) {
1352: case KEY_RSA_CERT:
1353: return KEY_RSA;
1354: case KEY_DSA_CERT:
1355: return KEY_DSA;
1356: default:
1357: return type;
1358: }
1359: }
1360:
1361: /* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */
1362: int
1363: key_to_certified(Key *k)
1364: {
1365: switch (k->type) {
1366: case KEY_RSA:
1367: k->cert = cert_new();
1368: k->type = KEY_RSA_CERT;
1369: return 0;
1370: case KEY_DSA:
1371: k->cert = cert_new();
1372: k->type = KEY_DSA_CERT;
1373: return 0;
1374: default:
1375: error("%s: key has incorrect type %s", __func__, key_type(k));
1376: return -1;
1377: }
1378: }
1379:
1380: /* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */
1381: int
1382: key_drop_cert(Key *k)
1383: {
1384: switch (k->type) {
1385: case KEY_RSA_CERT:
1386: cert_free(k->cert);
1387: k->type = KEY_RSA;
1388: return 0;
1389: case KEY_DSA_CERT:
1390: cert_free(k->cert);
1391: k->type = KEY_DSA;
1392: return 0;
1393: default:
1394: error("%s: key has incorrect type %s", __func__, key_type(k));
1395: return -1;
1396: }
1397: }
1398:
1399: /* Sign a KEY_RSA_CERT or KEY_DSA_CERT, (re-)generating the signed certblob */
1400: int
1401: key_certify(Key *k, Key *ca)
1402: {
1403: Buffer principals;
1404: u_char *ca_blob, *sig_blob, nonce[32];
1405: u_int i, ca_len, sig_len;
1406:
1407: if (k->cert == NULL) {
1408: error("%s: key lacks cert info", __func__);
1409: return -1;
1410: }
1411:
1412: if (!key_is_cert(k)) {
1413: error("%s: certificate has unknown type %d", __func__,
1414: k->cert->type);
1415: return -1;
1416: }
1417:
1418: if (ca->type != KEY_RSA && ca->type != KEY_DSA) {
1419: error("%s: CA key has unsupported type %s", __func__,
1420: key_type(ca));
1421: return -1;
1422: }
1423:
1424: key_to_blob(ca, &ca_blob, &ca_len);
1425:
1426: buffer_clear(&k->cert->certblob);
1427: buffer_put_cstring(&k->cert->certblob, key_ssh_name(k));
1428:
1429: switch (k->type) {
1430: case KEY_DSA_CERT:
1431: buffer_put_bignum2(&k->cert->certblob, k->dsa->p);
1432: buffer_put_bignum2(&k->cert->certblob, k->dsa->q);
1433: buffer_put_bignum2(&k->cert->certblob, k->dsa->g);
1434: buffer_put_bignum2(&k->cert->certblob, k->dsa->pub_key);
1435: break;
1436: case KEY_RSA_CERT:
1437: buffer_put_bignum2(&k->cert->certblob, k->rsa->e);
1438: buffer_put_bignum2(&k->cert->certblob, k->rsa->n);
1439: break;
1440: default:
1441: error("%s: key has incorrect type %s", __func__, key_type(k));
1442: buffer_clear(&k->cert->certblob);
1443: xfree(ca_blob);
1444: return -1;
1445: }
1446:
1447: buffer_put_int(&k->cert->certblob, k->cert->type);
1448: buffer_put_cstring(&k->cert->certblob, k->cert->key_id);
1449:
1450: buffer_init(&principals);
1451: for (i = 0; i < k->cert->nprincipals; i++)
1452: buffer_put_cstring(&principals, k->cert->principals[i]);
1453: buffer_put_string(&k->cert->certblob, buffer_ptr(&principals),
1454: buffer_len(&principals));
1455: buffer_free(&principals);
1456:
1457: buffer_put_int64(&k->cert->certblob, k->cert->valid_after);
1458: buffer_put_int64(&k->cert->certblob, k->cert->valid_before);
1459: buffer_put_string(&k->cert->certblob,
1460: buffer_ptr(&k->cert->constraints),
1461: buffer_len(&k->cert->constraints));
1462:
1463: arc4random_buf(&nonce, sizeof(nonce));
1464: buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce));
1465: buffer_put_string(&k->cert->certblob, NULL, 0); /* reserved */
1466: buffer_put_string(&k->cert->certblob, ca_blob, ca_len);
1467: xfree(ca_blob);
1468:
1469: /* Sign the whole mess */
1470: if (key_sign(ca, &sig_blob, &sig_len, buffer_ptr(&k->cert->certblob),
1471: buffer_len(&k->cert->certblob)) != 0) {
1472: error("%s: signature operation failed", __func__);
1473: buffer_clear(&k->cert->certblob);
1474: return -1;
1475: }
1476: /* Append signature and we are done */
1477: buffer_put_string(&k->cert->certblob, sig_blob, sig_len);
1478: xfree(sig_blob);
1479:
1480: return 0;
1481: }
1482:
1483: int
1484: key_cert_check_authority(const Key *k, int want_host, int require_principal,
1485: const char *name, const char **reason)
1486: {
1487: u_int i, principal_matches;
1488: time_t now = time(NULL);
1489:
1490: if (want_host) {
1491: if (k->cert->type != SSH2_CERT_TYPE_HOST) {
1492: *reason = "Certificate invalid: not a host certificate";
1493: return -1;
1494: }
1495: } else {
1496: if (k->cert->type != SSH2_CERT_TYPE_USER) {
1497: *reason = "Certificate invalid: not a user certificate";
1498: return -1;
1499: }
1500: }
1501: if (now < 0) {
1502: error("%s: system clock lies before epoch", __func__);
1503: *reason = "Certificate invalid: not yet valid";
1504: return -1;
1505: }
1506: if ((u_int64_t)now < k->cert->valid_after) {
1507: *reason = "Certificate invalid: not yet valid";
1508: return -1;
1509: }
1510: if ((u_int64_t)now >= k->cert->valid_before) {
1511: *reason = "Certificate invalid: expired";
1512: return -1;
1513: }
1514: if (k->cert->nprincipals == 0) {
1515: if (require_principal) {
1516: *reason = "Certificate lacks principal list";
1517: return -1;
1518: }
1519: } else {
1520: principal_matches = 0;
1521: for (i = 0; i < k->cert->nprincipals; i++) {
1522: if (strcmp(name, k->cert->principals[i]) == 0) {
1523: principal_matches = 1;
1524: break;
1525: }
1526: }
1527: if (!principal_matches) {
1528: *reason = "Certificate invalid: name is not a listed "
1529: "principal";
1530: return -1;
1531: }
1532: }
1533: return 0;
1.4 markus 1534: }