Annotation of src/usr.bin/ssh/key.c, Revision 1.106
1.106 ! djm 1: /* $OpenBSD: key.c,v 1.105 2013/10/29 09:42:11 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.89 djm 51: #include "misc.h"
1.83 djm 52: #include "ssh2.h"
53:
1.100 djm 54: static int to_blob(const Key *, u_char **, u_int *, int);
1.105 djm 55: static Key *key_from_blob2(const u_char *, u_int, int);
1.100 djm 56:
1.83 djm 57: static struct KeyCert *
58: cert_new(void)
59: {
60: struct KeyCert *cert;
61:
62: cert = xcalloc(1, sizeof(*cert));
63: buffer_init(&cert->certblob);
1.87 djm 64: buffer_init(&cert->critical);
65: buffer_init(&cert->extensions);
1.83 djm 66: cert->key_id = NULL;
67: cert->principals = NULL;
68: cert->signature_key = NULL;
69: return cert;
70: }
1.1 markus 71:
72: Key *
73: key_new(int type)
74: {
75: Key *k;
76: RSA *rsa;
77: DSA *dsa;
1.63 djm 78: k = xcalloc(1, sizeof(*k));
1.1 markus 79: k->type = type;
1.92 djm 80: k->ecdsa = NULL;
81: k->ecdsa_nid = -1;
1.3 markus 82: k->dsa = NULL;
83: k->rsa = NULL;
1.83 djm 84: k->cert = NULL;
1.1 markus 85: switch (k->type) {
1.12 markus 86: case KEY_RSA1:
1.1 markus 87: case KEY_RSA:
1.87 djm 88: case KEY_RSA_CERT_V00:
1.83 djm 89: case KEY_RSA_CERT:
1.38 markus 90: if ((rsa = RSA_new()) == NULL)
91: fatal("key_new: RSA_new failed");
92: if ((rsa->n = BN_new()) == NULL)
93: fatal("key_new: BN_new failed");
94: if ((rsa->e = BN_new()) == NULL)
95: fatal("key_new: BN_new failed");
1.1 markus 96: k->rsa = rsa;
97: break;
98: case KEY_DSA:
1.87 djm 99: case KEY_DSA_CERT_V00:
1.83 djm 100: case KEY_DSA_CERT:
1.38 markus 101: if ((dsa = DSA_new()) == NULL)
102: fatal("key_new: DSA_new failed");
103: if ((dsa->p = BN_new()) == NULL)
104: fatal("key_new: BN_new failed");
105: if ((dsa->q = BN_new()) == NULL)
106: fatal("key_new: BN_new failed");
107: if ((dsa->g = BN_new()) == NULL)
108: fatal("key_new: BN_new failed");
109: if ((dsa->pub_key = BN_new()) == NULL)
110: fatal("key_new: BN_new failed");
1.1 markus 111: k->dsa = dsa;
112: break;
1.92 djm 113: case KEY_ECDSA:
114: case KEY_ECDSA_CERT:
115: /* Cannot do anything until we know the group */
116: break;
1.12 markus 117: case KEY_UNSPEC:
1.1 markus 118: break;
119: default:
120: fatal("key_new: bad key type %d", k->type);
121: break;
122: }
1.83 djm 123:
124: if (key_is_cert(k))
125: k->cert = cert_new();
126:
1.1 markus 127: return k;
128: }
1.45 deraadt 129:
1.83 djm 130: void
131: key_add_private(Key *k)
1.12 markus 132: {
133: switch (k->type) {
134: case KEY_RSA1:
135: case KEY_RSA:
1.87 djm 136: case KEY_RSA_CERT_V00:
1.83 djm 137: case KEY_RSA_CERT:
1.38 markus 138: if ((k->rsa->d = BN_new()) == NULL)
139: fatal("key_new_private: BN_new failed");
140: if ((k->rsa->iqmp = BN_new()) == NULL)
141: fatal("key_new_private: BN_new failed");
142: if ((k->rsa->q = BN_new()) == NULL)
143: fatal("key_new_private: BN_new failed");
144: if ((k->rsa->p = BN_new()) == NULL)
145: fatal("key_new_private: BN_new failed");
146: if ((k->rsa->dmq1 = BN_new()) == NULL)
147: fatal("key_new_private: BN_new failed");
148: if ((k->rsa->dmp1 = BN_new()) == NULL)
149: fatal("key_new_private: BN_new failed");
1.12 markus 150: break;
151: case KEY_DSA:
1.87 djm 152: case KEY_DSA_CERT_V00:
1.83 djm 153: case KEY_DSA_CERT:
1.38 markus 154: if ((k->dsa->priv_key = BN_new()) == NULL)
155: fatal("key_new_private: BN_new failed");
1.12 markus 156: break;
1.92 djm 157: case KEY_ECDSA:
158: case KEY_ECDSA_CERT:
159: /* Cannot do anything until we know the group */
160: break;
1.12 markus 161: case KEY_UNSPEC:
162: break;
163: default:
164: break;
165: }
1.83 djm 166: }
167:
168: Key *
169: key_new_private(int type)
170: {
171: Key *k = key_new(type);
172:
173: key_add_private(k);
1.12 markus 174: return k;
175: }
1.45 deraadt 176:
1.83 djm 177: static void
178: cert_free(struct KeyCert *cert)
179: {
180: u_int i;
181:
182: buffer_free(&cert->certblob);
1.87 djm 183: buffer_free(&cert->critical);
184: buffer_free(&cert->extensions);
1.103 djm 185: free(cert->key_id);
1.83 djm 186: for (i = 0; i < cert->nprincipals; i++)
1.103 djm 187: free(cert->principals[i]);
188: free(cert->principals);
1.83 djm 189: if (cert->signature_key != NULL)
190: key_free(cert->signature_key);
1.103 djm 191: free(cert);
1.83 djm 192: }
193:
1.1 markus 194: void
195: key_free(Key *k)
196: {
1.60 djm 197: if (k == NULL)
1.62 deraadt 198: fatal("key_free: key is NULL");
1.1 markus 199: switch (k->type) {
1.12 markus 200: case KEY_RSA1:
1.1 markus 201: case KEY_RSA:
1.87 djm 202: case KEY_RSA_CERT_V00:
1.83 djm 203: case KEY_RSA_CERT:
1.1 markus 204: if (k->rsa != NULL)
205: RSA_free(k->rsa);
206: k->rsa = NULL;
207: break;
208: case KEY_DSA:
1.87 djm 209: case KEY_DSA_CERT_V00:
1.83 djm 210: case KEY_DSA_CERT:
1.1 markus 211: if (k->dsa != NULL)
212: DSA_free(k->dsa);
213: k->dsa = NULL;
214: break;
1.92 djm 215: case KEY_ECDSA:
216: case KEY_ECDSA_CERT:
217: if (k->ecdsa != NULL)
218: EC_KEY_free(k->ecdsa);
219: k->ecdsa = NULL;
220: break;
1.12 markus 221: case KEY_UNSPEC:
222: break;
1.1 markus 223: default:
224: fatal("key_free: bad key type %d", k->type);
225: break;
226: }
1.83 djm 227: if (key_is_cert(k)) {
228: if (k->cert != NULL)
229: cert_free(k->cert);
230: k->cert = NULL;
231: }
232:
1.103 djm 233: free(k);
1.1 markus 234: }
1.55 jakob 235:
1.83 djm 236: static int
237: cert_compare(struct KeyCert *a, struct KeyCert *b)
238: {
239: if (a == NULL && b == NULL)
240: return 1;
241: if (a == NULL || b == NULL)
242: return 0;
243: if (buffer_len(&a->certblob) != buffer_len(&b->certblob))
244: return 0;
1.90 djm 245: if (timingsafe_bcmp(buffer_ptr(&a->certblob), buffer_ptr(&b->certblob),
1.83 djm 246: buffer_len(&a->certblob)) != 0)
247: return 0;
248: return 1;
249: }
250:
251: /*
252: * Compare public portions of key only, allowing comparisons between
253: * certificates and plain keys too.
254: */
1.1 markus 255: int
1.83 djm 256: key_equal_public(const Key *a, const Key *b)
1.1 markus 257: {
1.92 djm 258: BN_CTX *bnctx;
259:
1.83 djm 260: if (a == NULL || b == NULL ||
261: key_type_plain(a->type) != key_type_plain(b->type))
1.1 markus 262: return 0;
1.83 djm 263:
1.1 markus 264: switch (a->type) {
1.12 markus 265: case KEY_RSA1:
1.87 djm 266: case KEY_RSA_CERT_V00:
1.83 djm 267: case KEY_RSA_CERT:
1.1 markus 268: case KEY_RSA:
269: return a->rsa != NULL && b->rsa != NULL &&
270: BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
271: BN_cmp(a->rsa->n, b->rsa->n) == 0;
1.87 djm 272: case KEY_DSA_CERT_V00:
1.83 djm 273: case KEY_DSA_CERT:
1.1 markus 274: case KEY_DSA:
275: return a->dsa != NULL && b->dsa != NULL &&
276: BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
277: BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
278: BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
279: BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
1.92 djm 280: case KEY_ECDSA_CERT:
281: case KEY_ECDSA:
282: if (a->ecdsa == NULL || b->ecdsa == NULL ||
283: EC_KEY_get0_public_key(a->ecdsa) == NULL ||
284: EC_KEY_get0_public_key(b->ecdsa) == NULL)
285: return 0;
286: if ((bnctx = BN_CTX_new()) == NULL)
287: fatal("%s: BN_CTX_new failed", __func__);
288: if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa),
289: EC_KEY_get0_group(b->ecdsa), bnctx) != 0 ||
290: EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa),
291: EC_KEY_get0_public_key(a->ecdsa),
292: EC_KEY_get0_public_key(b->ecdsa), bnctx) != 0) {
293: BN_CTX_free(bnctx);
294: return 0;
295: }
296: BN_CTX_free(bnctx);
297: return 1;
1.1 markus 298: default:
1.3 markus 299: fatal("key_equal: bad key type %d", a->type);
1.1 markus 300: }
1.78 stevesk 301: /* NOTREACHED */
1.1 markus 302: }
303:
1.83 djm 304: int
305: key_equal(const Key *a, const Key *b)
306: {
307: if (a == NULL || b == NULL || a->type != b->type)
308: return 0;
309: if (key_is_cert(a)) {
310: if (!cert_compare(a->cert, b->cert))
311: return 0;
312: }
313: return key_equal_public(a, b);
314: }
315:
1.52 jakob 316: u_char*
1.100 djm 317: key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
318: u_int *dgst_raw_length)
1.1 markus 319: {
1.41 markus 320: const EVP_MD *md = NULL;
1.21 markus 321: EVP_MD_CTX ctx;
1.13 markus 322: u_char *blob = NULL;
1.19 jakob 323: u_char *retval = NULL;
1.40 markus 324: u_int len = 0;
1.100 djm 325: int nlen, elen;
1.1 markus 326:
1.19 jakob 327: *dgst_raw_length = 0;
328:
1.21 markus 329: switch (dgst_type) {
330: case SSH_FP_MD5:
331: md = EVP_md5();
332: break;
333: case SSH_FP_SHA1:
334: md = EVP_sha1();
1.99 djm 335: break;
336: case SSH_FP_SHA256:
337: md = EVP_sha256();
1.21 markus 338: break;
339: default:
340: fatal("key_fingerprint_raw: bad digest type %d",
341: dgst_type);
342: }
1.1 markus 343: switch (k->type) {
1.12 markus 344: case KEY_RSA1:
1.1 markus 345: nlen = BN_num_bytes(k->rsa->n);
346: elen = BN_num_bytes(k->rsa->e);
347: len = nlen + elen;
1.3 markus 348: blob = xmalloc(len);
349: BN_bn2bin(k->rsa->n, blob);
350: BN_bn2bin(k->rsa->e, blob + nlen);
1.1 markus 351: break;
352: case KEY_DSA:
1.92 djm 353: case KEY_ECDSA:
1.12 markus 354: case KEY_RSA:
355: key_to_blob(k, &blob, &len);
356: break;
1.87 djm 357: case KEY_DSA_CERT_V00:
358: case KEY_RSA_CERT_V00:
1.83 djm 359: case KEY_DSA_CERT:
1.92 djm 360: case KEY_ECDSA_CERT:
1.83 djm 361: case KEY_RSA_CERT:
362: /* We want a fingerprint of the _key_ not of the cert */
1.100 djm 363: to_blob(k, &blob, &len, 1);
1.83 djm 364: break;
1.12 markus 365: case KEY_UNSPEC:
366: return retval;
1.1 markus 367: default:
1.19 jakob 368: fatal("key_fingerprint_raw: bad key type %d", k->type);
1.1 markus 369: break;
370: }
1.3 markus 371: if (blob != NULL) {
1.19 jakob 372: retval = xmalloc(EVP_MAX_MD_SIZE);
1.8 markus 373: EVP_DigestInit(&ctx, md);
374: EVP_DigestUpdate(&ctx, blob, len);
1.39 markus 375: EVP_DigestFinal(&ctx, retval, dgst_raw_length);
1.3 markus 376: memset(blob, 0, len);
1.103 djm 377: free(blob);
1.19 jakob 378: } else {
379: fatal("key_fingerprint_raw: blob is null");
1.1 markus 380: }
1.19 jakob 381: return retval;
382: }
383:
1.46 deraadt 384: static char *
385: key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
1.19 jakob 386: {
387: char *retval;
1.58 djm 388: u_int i;
1.19 jakob 389:
1.63 djm 390: retval = xcalloc(1, dgst_raw_len * 3 + 1);
1.36 deraadt 391: for (i = 0; i < dgst_raw_len; i++) {
1.19 jakob 392: char hex[4];
393: snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
1.54 avsm 394: strlcat(retval, hex, dgst_raw_len * 3 + 1);
1.19 jakob 395: }
1.54 avsm 396:
397: /* Remove the trailing ':' character */
1.19 jakob 398: retval[(dgst_raw_len * 3) - 1] = '\0';
399: return retval;
400: }
401:
1.46 deraadt 402: static char *
403: key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
1.19 jakob 404: {
405: char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
406: char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
407: 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
1.20 jakob 408: u_int i, j = 0, rounds, seed = 1;
1.19 jakob 409: char *retval;
410:
411: rounds = (dgst_raw_len / 2) + 1;
1.63 djm 412: retval = xcalloc((rounds * 6), sizeof(char));
1.20 jakob 413: retval[j++] = 'x';
414: for (i = 0; i < rounds; i++) {
1.19 jakob 415: u_int idx0, idx1, idx2, idx3, idx4;
1.20 jakob 416: if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
417: idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
1.19 jakob 418: seed) % 6;
1.20 jakob 419: idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
420: idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
1.19 jakob 421: (seed / 6)) % 6;
1.20 jakob 422: retval[j++] = vowels[idx0];
423: retval[j++] = consonants[idx1];
424: retval[j++] = vowels[idx2];
425: if ((i + 1) < rounds) {
426: idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
427: idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
428: retval[j++] = consonants[idx3];
429: retval[j++] = '-';
430: retval[j++] = consonants[idx4];
1.19 jakob 431: seed = ((seed * 5) +
1.20 jakob 432: ((((u_int)(dgst_raw[2 * i])) * 7) +
433: ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
1.19 jakob 434: }
435: } else {
436: idx0 = seed % 6;
437: idx1 = 16;
438: idx2 = seed / 6;
1.20 jakob 439: retval[j++] = vowels[idx0];
440: retval[j++] = consonants[idx1];
441: retval[j++] = vowels[idx2];
1.19 jakob 442: }
443: }
1.20 jakob 444: retval[j++] = 'x';
445: retval[j++] = '\0';
1.19 jakob 446: return retval;
447: }
448:
1.70 grunk 449: /*
450: * Draw an ASCII-Art representing the fingerprint so human brain can
451: * profit from its built-in pattern recognition ability.
452: * This technique is called "random art" and can be found in some
453: * scientific publications like this original paper:
454: *
455: * "Hash Visualization: a New Technique to improve Real-World Security",
456: * Perrig A. and Song D., 1999, International Workshop on Cryptographic
457: * Techniques and E-Commerce (CrypTEC '99)
458: * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
459: *
460: * The subject came up in a talk by Dan Kaminsky, too.
461: *
462: * If you see the picture is different, the key is different.
463: * If the picture looks the same, you still know nothing.
464: *
465: * The algorithm used here is a worm crawling over a discrete plane,
466: * leaving a trace (augmenting the field) everywhere it goes.
467: * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
468: * makes the respective movement vector be ignored for this turn.
469: * Graphs are not unambiguous, because circles in graphs can be
470: * walked in either direction.
471: */
1.74 grunk 472:
473: /*
474: * Field sizes for the random art. Have to be odd, so the starting point
475: * can be in the exact middle of the picture, and FLDBASE should be >=8 .
476: * Else pictures would be too dense, and drawing the frame would
477: * fail, too, because the key type would not fit in anymore.
478: */
479: #define FLDBASE 8
480: #define FLDSIZE_Y (FLDBASE + 1)
481: #define FLDSIZE_X (FLDBASE * 2 + 1)
1.70 grunk 482: static char *
1.74 grunk 483: key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k)
1.70 grunk 484: {
485: /*
486: * Chars to be used after each other every time the worm
487: * intersects with itself. Matter of taste.
488: */
1.75 grunk 489: char *augmentation_string = " .o+=*BOX@%&#/^SE";
1.70 grunk 490: char *retval, *p;
1.71 otto 491: u_char field[FLDSIZE_X][FLDSIZE_Y];
1.70 grunk 492: u_int i, b;
493: int x, y;
1.72 grunk 494: size_t len = strlen(augmentation_string) - 1;
1.70 grunk 495:
496: retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
497:
498: /* initialize field */
1.71 otto 499: memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1.70 grunk 500: x = FLDSIZE_X / 2;
501: y = FLDSIZE_Y / 2;
502:
503: /* process raw key */
504: for (i = 0; i < dgst_raw_len; i++) {
505: int input;
506: /* each byte conveys four 2-bit move commands */
507: input = dgst_raw[i];
508: for (b = 0; b < 4; b++) {
509: /* evaluate 2 bit, rest is shifted later */
510: x += (input & 0x1) ? 1 : -1;
511: y += (input & 0x2) ? 1 : -1;
512:
513: /* assure we are still in bounds */
514: x = MAX(x, 0);
515: y = MAX(y, 0);
516: x = MIN(x, FLDSIZE_X - 1);
517: y = MIN(y, FLDSIZE_Y - 1);
518:
519: /* augment the field */
1.79 grunk 520: if (field[x][y] < len - 2)
521: field[x][y]++;
1.70 grunk 522: input = input >> 2;
523: }
524: }
1.75 grunk 525:
526: /* mark starting point and end point*/
527: field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
528: field[x][y] = len;
1.70 grunk 529:
530: /* fill in retval */
1.77 otto 531: snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k));
1.74 grunk 532: p = strchr(retval, '\0');
1.70 grunk 533:
534: /* output upper border */
1.77 otto 535: for (i = p - retval - 1; i < FLDSIZE_X; i++)
1.70 grunk 536: *p++ = '-';
537: *p++ = '+';
538: *p++ = '\n';
539:
540: /* output content */
541: for (y = 0; y < FLDSIZE_Y; y++) {
542: *p++ = '|';
543: for (x = 0; x < FLDSIZE_X; x++)
1.72 grunk 544: *p++ = augmentation_string[MIN(field[x][y], len)];
1.70 grunk 545: *p++ = '|';
546: *p++ = '\n';
547: }
548:
549: /* output lower border */
550: *p++ = '+';
551: for (i = 0; i < FLDSIZE_X; i++)
552: *p++ = '-';
553: *p++ = '+';
554:
555: return retval;
556: }
557:
1.46 deraadt 558: char *
1.104 djm 559: key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
1.19 jakob 560: {
1.23 markus 561: char *retval = NULL;
1.19 jakob 562: u_char *dgst_raw;
1.39 markus 563: u_int dgst_raw_len;
1.36 deraadt 564:
1.19 jakob 565: dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
566: if (!dgst_raw)
1.22 markus 567: fatal("key_fingerprint: null from key_fingerprint_raw()");
1.35 deraadt 568: switch (dgst_rep) {
1.19 jakob 569: case SSH_FP_HEX:
570: retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
571: break;
572: case SSH_FP_BUBBLEBABBLE:
573: retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1.70 grunk 574: break;
575: case SSH_FP_RANDOMART:
1.74 grunk 576: retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k);
1.19 jakob 577: break;
578: default:
1.80 stevesk 579: fatal("key_fingerprint: bad digest representation %d",
1.19 jakob 580: dgst_rep);
581: break;
582: }
583: memset(dgst_raw, 0, dgst_raw_len);
1.103 djm 584: free(dgst_raw);
1.1 markus 585: return retval;
586: }
587:
588: /*
589: * Reads a multiple-precision integer in decimal from the buffer, and advances
590: * the pointer. The integer must already be initialized. This function is
591: * permitted to modify the buffer. This leaves *cpp to point just beyond the
592: * last processed (and maybe modified) character. Note that this may modify
593: * the buffer containing the number.
594: */
1.27 itojun 595: static int
1.1 markus 596: read_bignum(char **cpp, BIGNUM * value)
597: {
598: char *cp = *cpp;
599: int old;
600:
601: /* Skip any leading whitespace. */
602: for (; *cp == ' ' || *cp == '\t'; cp++)
603: ;
604:
605: /* Check that it begins with a decimal digit. */
606: if (*cp < '0' || *cp > '9')
607: return 0;
608:
609: /* Save starting position. */
610: *cpp = cp;
611:
612: /* Move forward until all decimal digits skipped. */
613: for (; *cp >= '0' && *cp <= '9'; cp++)
614: ;
615:
616: /* Save the old terminating character, and replace it by \0. */
617: old = *cp;
618: *cp = 0;
619:
620: /* Parse the number. */
621: if (BN_dec2bn(&value, *cpp) == 0)
622: return 0;
623:
624: /* Restore old terminating character. */
625: *cp = old;
626:
627: /* Move beyond the number and return success. */
628: *cpp = cp;
629: return 1;
630: }
1.45 deraadt 631:
1.27 itojun 632: static int
1.1 markus 633: write_bignum(FILE *f, BIGNUM *num)
634: {
635: char *buf = BN_bn2dec(num);
636: if (buf == NULL) {
637: error("write_bignum: BN_bn2dec() failed");
638: return 0;
639: }
640: fprintf(f, " %s", buf);
1.33 markus 641: OPENSSL_free(buf);
1.1 markus 642: return 1;
643: }
1.12 markus 644:
1.32 markus 645: /* returns 1 ok, -1 error */
1.12 markus 646: int
1.3 markus 647: key_read(Key *ret, char **cpp)
1.1 markus 648: {
1.3 markus 649: Key *k;
1.12 markus 650: int success = -1;
651: char *cp, *space;
1.92 djm 652: int len, n, type, curve_nid = -1;
1.12 markus 653: u_int bits;
1.13 markus 654: u_char *blob;
1.3 markus 655:
656: cp = *cpp;
657:
1.35 deraadt 658: switch (ret->type) {
1.12 markus 659: case KEY_RSA1:
1.3 markus 660: /* Get number of bits. */
661: if (*cp < '0' || *cp > '9')
1.12 markus 662: return -1; /* Bad bit count... */
1.3 markus 663: for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
664: bits = 10 * bits + *cp - '0';
1.1 markus 665: if (bits == 0)
1.12 markus 666: return -1;
1.3 markus 667: *cpp = cp;
1.1 markus 668: /* Get public exponent, public modulus. */
669: if (!read_bignum(cpp, ret->rsa->e))
1.12 markus 670: return -1;
1.1 markus 671: if (!read_bignum(cpp, ret->rsa->n))
1.12 markus 672: return -1;
1.82 dtucker 673: /* validate the claimed number of bits */
674: if ((u_int)BN_num_bits(ret->rsa->n) != bits) {
675: verbose("key_read: claimed key size %d does not match "
676: "actual %d", bits, BN_num_bits(ret->rsa->n));
677: return -1;
678: }
1.12 markus 679: success = 1;
1.1 markus 680: break;
1.12 markus 681: case KEY_UNSPEC:
682: case KEY_RSA:
1.1 markus 683: case KEY_DSA:
1.92 djm 684: case KEY_ECDSA:
1.87 djm 685: case KEY_DSA_CERT_V00:
686: case KEY_RSA_CERT_V00:
1.83 djm 687: case KEY_DSA_CERT:
1.92 djm 688: case KEY_ECDSA_CERT:
1.83 djm 689: case KEY_RSA_CERT:
1.12 markus 690: space = strchr(cp, ' ');
691: if (space == NULL) {
1.50 markus 692: debug3("key_read: missing whitespace");
1.12 markus 693: return -1;
694: }
695: *space = '\0';
696: type = key_type_from_name(cp);
1.92 djm 697: if (key_type_plain(type) == KEY_ECDSA &&
698: (curve_nid = key_ecdsa_nid_from_name(cp)) == -1) {
699: debug("key_read: invalid curve");
700: return -1;
701: }
1.12 markus 702: *space = ' ';
703: if (type == KEY_UNSPEC) {
1.50 markus 704: debug3("key_read: missing keytype");
1.12 markus 705: return -1;
706: }
707: cp = space+1;
708: if (*cp == '\0') {
709: debug3("key_read: short string");
710: return -1;
711: }
712: if (ret->type == KEY_UNSPEC) {
713: ret->type = type;
714: } else if (ret->type != type) {
715: /* is a key, but different type */
716: debug3("key_read: type mismatch");
1.32 markus 717: return -1;
1.12 markus 718: }
1.3 markus 719: len = 2*strlen(cp);
720: blob = xmalloc(len);
721: n = uudecode(cp, blob, len);
1.6 markus 722: if (n < 0) {
1.7 markus 723: error("key_read: uudecode %s failed", cp);
1.103 djm 724: free(blob);
1.12 markus 725: return -1;
1.6 markus 726: }
1.53 markus 727: k = key_from_blob(blob, (u_int)n);
1.103 djm 728: free(blob);
1.7 markus 729: if (k == NULL) {
1.12 markus 730: error("key_read: key_from_blob %s failed", cp);
731: return -1;
1.7 markus 732: }
1.12 markus 733: if (k->type != type) {
734: error("key_read: type mismatch: encoding error");
735: key_free(k);
736: return -1;
737: }
1.92 djm 738: if (key_type_plain(type) == KEY_ECDSA &&
739: curve_nid != k->ecdsa_nid) {
740: error("key_read: type mismatch: EC curve mismatch");
741: key_free(k);
742: return -1;
743: }
1.12 markus 744: /*XXXX*/
1.83 djm 745: if (key_is_cert(ret)) {
746: if (!key_is_cert(k)) {
747: error("key_read: loaded key is not a cert");
748: key_free(k);
749: return -1;
750: }
751: if (ret->cert != NULL)
752: cert_free(ret->cert);
753: ret->cert = k->cert;
754: k->cert = NULL;
755: }
756: if (key_type_plain(ret->type) == KEY_RSA) {
1.12 markus 757: if (ret->rsa != NULL)
758: RSA_free(ret->rsa);
759: ret->rsa = k->rsa;
760: k->rsa = NULL;
761: #ifdef DEBUG_PK
762: RSA_print_fp(stderr, ret->rsa, 8);
763: #endif
1.83 djm 764: }
765: if (key_type_plain(ret->type) == KEY_DSA) {
1.12 markus 766: if (ret->dsa != NULL)
767: DSA_free(ret->dsa);
768: ret->dsa = k->dsa;
769: k->dsa = NULL;
770: #ifdef DEBUG_PK
771: DSA_print_fp(stderr, ret->dsa, 8);
772: #endif
773: }
1.92 djm 774: if (key_type_plain(ret->type) == KEY_ECDSA) {
775: if (ret->ecdsa != NULL)
776: EC_KEY_free(ret->ecdsa);
777: ret->ecdsa = k->ecdsa;
778: ret->ecdsa_nid = k->ecdsa_nid;
779: k->ecdsa = NULL;
780: k->ecdsa_nid = -1;
781: #ifdef DEBUG_PK
782: key_dump_ec_key(ret->ecdsa);
783: #endif
784: }
1.83 djm 785: success = 1;
1.12 markus 786: /*XXXX*/
1.34 markus 787: key_free(k);
1.12 markus 788: if (success != 1)
789: break;
1.7 markus 790: /* advance cp: skip whitespace and data */
791: while (*cp == ' ' || *cp == '\t')
792: cp++;
793: while (*cp != '\0' && *cp != ' ' && *cp != '\t')
794: cp++;
795: *cpp = cp;
1.1 markus 796: break;
797: default:
1.3 markus 798: fatal("key_read: bad key type: %d", ret->type);
1.1 markus 799: break;
800: }
1.12 markus 801: return success;
1.1 markus 802: }
1.45 deraadt 803:
1.1 markus 804: int
1.55 jakob 805: key_write(const Key *key, FILE *f)
1.1 markus 806: {
1.40 markus 807: int n, success = 0;
808: u_int len, bits = 0;
1.49 markus 809: u_char *blob;
810: char *uu;
1.1 markus 811:
1.83 djm 812: if (key_is_cert(key)) {
813: if (key->cert == NULL) {
814: error("%s: no cert data", __func__);
815: return 0;
816: }
817: if (buffer_len(&key->cert->certblob) == 0) {
818: error("%s: no signed certificate blob", __func__);
819: return 0;
820: }
821: }
822:
823: switch (key->type) {
824: case KEY_RSA1:
825: if (key->rsa == NULL)
826: return 0;
1.1 markus 827: /* size of modulus 'n' */
828: bits = BN_num_bits(key->rsa->n);
829: fprintf(f, "%u", bits);
830: if (write_bignum(f, key->rsa->e) &&
1.83 djm 831: write_bignum(f, key->rsa->n))
832: return 1;
833: error("key_write: failed for RSA key");
834: return 0;
835: case KEY_DSA:
1.87 djm 836: case KEY_DSA_CERT_V00:
1.83 djm 837: case KEY_DSA_CERT:
838: if (key->dsa == NULL)
839: return 0;
840: break;
1.92 djm 841: case KEY_ECDSA:
842: case KEY_ECDSA_CERT:
843: if (key->ecdsa == NULL)
844: return 0;
845: break;
1.83 djm 846: case KEY_RSA:
1.87 djm 847: case KEY_RSA_CERT_V00:
1.83 djm 848: case KEY_RSA_CERT:
849: if (key->rsa == NULL)
850: return 0;
851: break;
852: default:
853: return 0;
854: }
855:
856: key_to_blob(key, &blob, &len);
857: uu = xmalloc(2*len);
858: n = uuencode(blob, len, uu, 2*len);
859: if (n > 0) {
860: fprintf(f, "%s %s", key_ssh_name(key), uu);
861: success = 1;
1.1 markus 862: }
1.103 djm 863: free(blob);
864: free(uu);
1.83 djm 865:
1.1 markus 866: return success;
867: }
1.45 deraadt 868:
1.55 jakob 869: const char *
1.86 stevesk 870: key_cert_type(const Key *k)
871: {
872: switch (k->cert->type) {
873: case SSH2_CERT_TYPE_USER:
874: return "user";
875: case SSH2_CERT_TYPE_HOST:
876: return "host";
877: default:
878: return "unknown";
879: }
1.10 markus 880: }
1.45 deraadt 881:
1.101 djm 882: struct keytype {
883: char *name;
884: char *shortname;
885: int type;
886: int nid;
887: int cert;
888: };
889: static const struct keytype keytypes[] = {
890: { NULL, "RSA1", KEY_RSA1, 0, 0 },
891: { "ssh-rsa", "RSA", KEY_RSA, 0, 0 },
892: { "ssh-dss", "DSA", KEY_DSA, 0, 0 },
893: { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA, NID_X9_62_prime256v1, 0 },
894: { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA, NID_secp384r1, 0 },
895: { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0 },
896: { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT, 0, 1 },
897: { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT, 0, 1 },
898: { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT",
899: KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1 },
900: { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT",
901: KEY_ECDSA_CERT, NID_secp384r1, 1 },
902: { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT",
903: KEY_ECDSA_CERT, NID_secp521r1, 1 },
904: { "ssh-rsa-cert-v00@openssh.com", "RSA-CERT-V00",
905: KEY_RSA_CERT_V00, 0, 1 },
906: { "ssh-dss-cert-v00@openssh.com", "DSA-CERT-V00",
907: KEY_DSA_CERT_V00, 0, 1 },
908: { NULL, NULL, -1, -1, 0 }
909: };
910:
911: const char *
912: key_type(const Key *k)
913: {
914: const struct keytype *kt;
915:
916: for (kt = keytypes; kt->type != -1; kt++) {
917: if (kt->type == k->type)
918: return kt->shortname;
919: }
920: return "unknown";
921: }
922:
1.92 djm 923: static const char *
924: key_ssh_name_from_type_nid(int type, int nid)
1.12 markus 925: {
1.101 djm 926: const struct keytype *kt;
927:
928: for (kt = keytypes; kt->type != -1; kt++) {
929: if (kt->type == type && (kt->nid == 0 || kt->nid == nid))
930: return kt->name;
1.12 markus 931: }
932: return "ssh-unknown";
933: }
1.45 deraadt 934:
1.92 djm 935: const char *
936: key_ssh_name(const Key *k)
937: {
938: return key_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
939: }
940:
941: const char *
942: key_ssh_name_plain(const Key *k)
943: {
944: return key_ssh_name_from_type_nid(key_type_plain(k->type),
945: k->ecdsa_nid);
946: }
947:
1.101 djm 948: int
949: key_type_from_name(char *name)
950: {
951: const struct keytype *kt;
952:
953: for (kt = keytypes; kt->type != -1; kt++) {
954: /* Only allow shortname matches for plain key types */
955: if ((kt->name != NULL && strcmp(name, kt->name) == 0) ||
956: (!kt->cert && strcasecmp(kt->shortname, name) == 0))
957: return kt->type;
958: }
959: debug2("key_type_from_name: unknown key type '%s'", name);
960: return KEY_UNSPEC;
961: }
962:
963: int
964: key_ecdsa_nid_from_name(const char *name)
965: {
966: const struct keytype *kt;
967:
968: for (kt = keytypes; kt->type != -1; kt++) {
969: if (kt->type != KEY_ECDSA && kt->type != KEY_ECDSA_CERT)
970: continue;
971: if (kt->name != NULL && strcmp(name, kt->name) == 0)
972: return kt->nid;
973: }
974: debug2("%s: unknown/non-ECDSA key type '%s'", __func__, name);
975: return -1;
976: }
977:
978: char *
979: key_alg_list(void)
980: {
981: char *ret = NULL;
982: size_t nlen, rlen = 0;
983: const struct keytype *kt;
984:
985: for (kt = keytypes; kt->type != -1; kt++) {
986: if (kt->name == NULL)
987: continue;
988: if (ret != NULL)
989: ret[rlen++] = '\n';
990: nlen = strlen(kt->name);
991: ret = xrealloc(ret, 1, rlen + nlen + 2);
992: memcpy(ret + rlen, kt->name, nlen + 1);
993: rlen += nlen;
994: }
995: return ret;
996: }
997:
1.105 djm 998: int
999: key_type_is_cert(int type)
1000: {
1001: const struct keytype *kt;
1002:
1003: for (kt = keytypes; kt->type != -1; kt++) {
1004: if (kt->type == type)
1005: return kt->cert;
1006: }
1007: return 0;
1008: }
1009:
1.12 markus 1010: u_int
1.55 jakob 1011: key_size(const Key *k)
1.35 deraadt 1012: {
1.10 markus 1013: switch (k->type) {
1.12 markus 1014: case KEY_RSA1:
1.10 markus 1015: case KEY_RSA:
1.87 djm 1016: case KEY_RSA_CERT_V00:
1.83 djm 1017: case KEY_RSA_CERT:
1.10 markus 1018: return BN_num_bits(k->rsa->n);
1019: case KEY_DSA:
1.87 djm 1020: case KEY_DSA_CERT_V00:
1.83 djm 1021: case KEY_DSA_CERT:
1.10 markus 1022: return BN_num_bits(k->dsa->p);
1.92 djm 1023: case KEY_ECDSA:
1024: case KEY_ECDSA_CERT:
1.93 djm 1025: return key_curve_nid_to_bits(k->ecdsa_nid);
1.10 markus 1026: }
1027: return 0;
1.12 markus 1028: }
1029:
1.27 itojun 1030: static RSA *
1.13 markus 1031: rsa_generate_private_key(u_int bits)
1.12 markus 1032: {
1.95 djm 1033: RSA *private = RSA_new();
1034: BIGNUM *f4 = BN_new();
1.61 deraadt 1035:
1.17 stevesk 1036: if (private == NULL)
1.95 djm 1037: fatal("%s: RSA_new failed", __func__);
1038: if (f4 == NULL)
1039: fatal("%s: BN_new failed", __func__);
1040: if (!BN_set_word(f4, RSA_F4))
1041: fatal("%s: BN_new failed", __func__);
1042: if (!RSA_generate_key_ex(private, bits, f4, NULL))
1043: fatal("%s: key generation failed.", __func__);
1044: BN_free(f4);
1.17 stevesk 1045: return private;
1.12 markus 1046: }
1047:
1.27 itojun 1048: static DSA*
1.13 markus 1049: dsa_generate_private_key(u_int bits)
1.12 markus 1050: {
1.95 djm 1051: DSA *private = DSA_new();
1.61 deraadt 1052:
1.12 markus 1053: if (private == NULL)
1.95 djm 1054: fatal("%s: DSA_new failed", __func__);
1055: if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL,
1056: NULL, NULL))
1057: fatal("%s: DSA_generate_parameters failed", __func__);
1.12 markus 1058: if (!DSA_generate_key(private))
1.95 djm 1059: fatal("%s: DSA_generate_key failed.", __func__);
1.12 markus 1060: return private;
1061: }
1062:
1.92 djm 1063: int
1064: key_ecdsa_bits_to_nid(int bits)
1065: {
1066: switch (bits) {
1067: case 256:
1068: return NID_X9_62_prime256v1;
1069: case 384:
1070: return NID_secp384r1;
1071: case 521:
1072: return NID_secp521r1;
1073: default:
1074: return -1;
1075: }
1076: }
1077:
1078: int
1.94 djm 1079: key_ecdsa_key_to_nid(EC_KEY *k)
1.92 djm 1080: {
1081: EC_GROUP *eg;
1082: int nids[] = {
1083: NID_X9_62_prime256v1,
1084: NID_secp384r1,
1085: NID_secp521r1,
1086: -1
1087: };
1.94 djm 1088: int nid;
1.92 djm 1089: u_int i;
1090: BN_CTX *bnctx;
1.94 djm 1091: const EC_GROUP *g = EC_KEY_get0_group(k);
1.92 djm 1092:
1.94 djm 1093: /*
1094: * The group may be stored in a ASN.1 encoded private key in one of two
1095: * ways: as a "named group", which is reconstituted by ASN.1 object ID
1096: * or explicit group parameters encoded into the key blob. Only the
1097: * "named group" case sets the group NID for us, but we can figure
1098: * it out for the other case by comparing against all the groups that
1099: * are supported.
1100: */
1101: if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1102: return nid;
1.92 djm 1103: if ((bnctx = BN_CTX_new()) == NULL)
1104: fatal("%s: BN_CTX_new() failed", __func__);
1105: for (i = 0; nids[i] != -1; i++) {
1106: if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
1107: fatal("%s: EC_GROUP_new_by_curve_name failed",
1108: __func__);
1.94 djm 1109: if (EC_GROUP_cmp(g, eg, bnctx) == 0)
1.92 djm 1110: break;
1111: EC_GROUP_free(eg);
1112: }
1113: BN_CTX_free(bnctx);
1114: debug3("%s: nid = %d", __func__, nids[i]);
1.94 djm 1115: if (nids[i] != -1) {
1116: /* Use the group with the NID attached */
1117: EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1118: if (EC_KEY_set_group(k, eg) != 1)
1119: fatal("%s: EC_KEY_set_group", __func__);
1120: }
1.92 djm 1121: return nids[i];
1122: }
1123:
1124: static EC_KEY*
1125: ecdsa_generate_private_key(u_int bits, int *nid)
1126: {
1127: EC_KEY *private;
1128:
1129: if ((*nid = key_ecdsa_bits_to_nid(bits)) == -1)
1130: fatal("%s: invalid key length", __func__);
1131: if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL)
1132: fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
1133: if (EC_KEY_generate_key(private) != 1)
1134: fatal("%s: EC_KEY_generate_key failed", __func__);
1.94 djm 1135: EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
1.92 djm 1136: return private;
1137: }
1138:
1.12 markus 1139: Key *
1.13 markus 1140: key_generate(int type, u_int bits)
1.12 markus 1141: {
1142: Key *k = key_new(KEY_UNSPEC);
1143: switch (type) {
1.17 stevesk 1144: case KEY_DSA:
1.12 markus 1145: k->dsa = dsa_generate_private_key(bits);
1146: break;
1.92 djm 1147: case KEY_ECDSA:
1148: k->ecdsa = ecdsa_generate_private_key(bits, &k->ecdsa_nid);
1149: break;
1.12 markus 1150: case KEY_RSA:
1151: case KEY_RSA1:
1152: k->rsa = rsa_generate_private_key(bits);
1153: break;
1.87 djm 1154: case KEY_RSA_CERT_V00:
1155: case KEY_DSA_CERT_V00:
1.83 djm 1156: case KEY_RSA_CERT:
1157: case KEY_DSA_CERT:
1158: fatal("key_generate: cert keys cannot be generated directly");
1.12 markus 1159: default:
1.17 stevesk 1160: fatal("key_generate: unknown type %d", type);
1.12 markus 1161: }
1.17 stevesk 1162: k->type = type;
1.12 markus 1163: return k;
1164: }
1165:
1.83 djm 1166: void
1167: key_cert_copy(const Key *from_key, struct Key *to_key)
1168: {
1169: u_int i;
1170: const struct KeyCert *from;
1171: struct KeyCert *to;
1172:
1173: if (to_key->cert != NULL) {
1174: cert_free(to_key->cert);
1175: to_key->cert = NULL;
1176: }
1177:
1178: if ((from = from_key->cert) == NULL)
1179: return;
1180:
1181: to = to_key->cert = cert_new();
1182:
1183: buffer_append(&to->certblob, buffer_ptr(&from->certblob),
1184: buffer_len(&from->certblob));
1185:
1.87 djm 1186: buffer_append(&to->critical,
1187: buffer_ptr(&from->critical), buffer_len(&from->critical));
1188: buffer_append(&to->extensions,
1189: buffer_ptr(&from->extensions), buffer_len(&from->extensions));
1.83 djm 1190:
1.87 djm 1191: to->serial = from->serial;
1.83 djm 1192: to->type = from->type;
1193: to->key_id = from->key_id == NULL ? NULL : xstrdup(from->key_id);
1194: to->valid_after = from->valid_after;
1195: to->valid_before = from->valid_before;
1196: to->signature_key = from->signature_key == NULL ?
1197: NULL : key_from_private(from->signature_key);
1198:
1199: to->nprincipals = from->nprincipals;
1200: if (to->nprincipals > CERT_MAX_PRINCIPALS)
1201: fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)",
1202: __func__, to->nprincipals, CERT_MAX_PRINCIPALS);
1203: if (to->nprincipals > 0) {
1204: to->principals = xcalloc(from->nprincipals,
1205: sizeof(*to->principals));
1206: for (i = 0; i < to->nprincipals; i++)
1207: to->principals[i] = xstrdup(from->principals[i]);
1208: }
1209: }
1210:
1.12 markus 1211: Key *
1.55 jakob 1212: key_from_private(const Key *k)
1.12 markus 1213: {
1214: Key *n = NULL;
1215: switch (k->type) {
1.17 stevesk 1216: case KEY_DSA:
1.87 djm 1217: case KEY_DSA_CERT_V00:
1.83 djm 1218: case KEY_DSA_CERT:
1.12 markus 1219: n = key_new(k->type);
1.68 markus 1220: if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
1221: (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
1222: (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
1223: (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
1224: fatal("key_from_private: BN_copy failed");
1.12 markus 1225: break;
1.92 djm 1226: case KEY_ECDSA:
1227: case KEY_ECDSA_CERT:
1228: n = key_new(k->type);
1229: n->ecdsa_nid = k->ecdsa_nid;
1230: if ((n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid)) == NULL)
1231: fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
1232: if (EC_KEY_set_public_key(n->ecdsa,
1233: EC_KEY_get0_public_key(k->ecdsa)) != 1)
1234: fatal("%s: EC_KEY_set_public_key failed", __func__);
1235: break;
1.12 markus 1236: case KEY_RSA:
1237: case KEY_RSA1:
1.87 djm 1238: case KEY_RSA_CERT_V00:
1.83 djm 1239: case KEY_RSA_CERT:
1.12 markus 1240: n = key_new(k->type);
1.68 markus 1241: if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
1242: (BN_copy(n->rsa->e, k->rsa->e) == NULL))
1243: fatal("key_from_private: BN_copy failed");
1.12 markus 1244: break;
1245: default:
1.17 stevesk 1246: fatal("key_from_private: unknown type %d", k->type);
1.12 markus 1247: break;
1248: }
1.83 djm 1249: if (key_is_cert(k))
1250: key_cert_copy(k, n);
1.12 markus 1251: return n;
1.92 djm 1252: }
1253:
1254: int
1.25 markus 1255: key_names_valid2(const char *names)
1256: {
1257: char *s, *cp, *p;
1258:
1259: if (names == NULL || strcmp(names, "") == 0)
1260: return 0;
1261: s = cp = xstrdup(names);
1262: for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36 deraadt 1263: (p = strsep(&cp, ","))) {
1.25 markus 1264: switch (key_type_from_name(p)) {
1265: case KEY_RSA1:
1266: case KEY_UNSPEC:
1.103 djm 1267: free(s);
1.25 markus 1268: return 0;
1269: }
1270: }
1271: debug3("key names ok: [%s]", names);
1.103 djm 1272: free(s);
1.25 markus 1273: return 1;
1.12 markus 1274: }
1275:
1.83 djm 1276: static int
1277: cert_parse(Buffer *b, Key *key, const u_char *blob, u_int blen)
1278: {
1.87 djm 1279: u_char *principals, *critical, *exts, *sig_key, *sig;
1280: u_int signed_len, plen, clen, sklen, slen, kidlen, elen;
1.83 djm 1281: Buffer tmp;
1282: char *principal;
1283: int ret = -1;
1.87 djm 1284: int v00 = key->type == KEY_DSA_CERT_V00 ||
1285: key->type == KEY_RSA_CERT_V00;
1.83 djm 1286:
1287: buffer_init(&tmp);
1288:
1289: /* Copy the entire key blob for verification and later serialisation */
1290: buffer_append(&key->cert->certblob, blob, blen);
1291:
1.87 djm 1292: elen = 0; /* Not touched for v00 certs */
1293: principals = exts = critical = sig_key = sig = NULL;
1294: if ((!v00 && buffer_get_int64_ret(&key->cert->serial, b) != 0) ||
1295: buffer_get_int_ret(&key->cert->type, b) != 0 ||
1.91 djm 1296: (key->cert->key_id = buffer_get_cstring_ret(b, &kidlen)) == NULL ||
1.83 djm 1297: (principals = buffer_get_string_ret(b, &plen)) == NULL ||
1298: buffer_get_int64_ret(&key->cert->valid_after, b) != 0 ||
1299: buffer_get_int64_ret(&key->cert->valid_before, b) != 0 ||
1.87 djm 1300: (critical = buffer_get_string_ret(b, &clen)) == NULL ||
1301: (!v00 && (exts = buffer_get_string_ret(b, &elen)) == NULL) ||
1302: (v00 && buffer_get_string_ptr_ret(b, NULL) == NULL) || /* nonce */
1303: buffer_get_string_ptr_ret(b, NULL) == NULL || /* reserved */
1.83 djm 1304: (sig_key = buffer_get_string_ret(b, &sklen)) == NULL) {
1305: error("%s: parse error", __func__);
1.84 djm 1306: goto out;
1307: }
1308:
1.83 djm 1309: /* Signature is left in the buffer so we can calculate this length */
1310: signed_len = buffer_len(&key->cert->certblob) - buffer_len(b);
1311:
1312: if ((sig = buffer_get_string_ret(b, &slen)) == NULL) {
1313: error("%s: parse error", __func__);
1314: goto out;
1315: }
1316:
1317: if (key->cert->type != SSH2_CERT_TYPE_USER &&
1318: key->cert->type != SSH2_CERT_TYPE_HOST) {
1319: error("Unknown certificate type %u", key->cert->type);
1320: goto out;
1321: }
1322:
1323: buffer_append(&tmp, principals, plen);
1324: while (buffer_len(&tmp) > 0) {
1325: if (key->cert->nprincipals >= CERT_MAX_PRINCIPALS) {
1.84 djm 1326: error("%s: Too many principals", __func__);
1.83 djm 1327: goto out;
1328: }
1.91 djm 1329: if ((principal = buffer_get_cstring_ret(&tmp, &plen)) == NULL) {
1.84 djm 1330: error("%s: Principals data invalid", __func__);
1331: goto out;
1332: }
1.83 djm 1333: key->cert->principals = xrealloc(key->cert->principals,
1334: key->cert->nprincipals + 1, sizeof(*key->cert->principals));
1335: key->cert->principals[key->cert->nprincipals++] = principal;
1336: }
1337:
1338: buffer_clear(&tmp);
1339:
1.87 djm 1340: buffer_append(&key->cert->critical, critical, clen);
1341: buffer_append(&tmp, critical, clen);
1.83 djm 1342: /* validate structure */
1343: while (buffer_len(&tmp) != 0) {
1.85 djm 1344: if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL ||
1345: buffer_get_string_ptr_ret(&tmp, NULL) == NULL) {
1.87 djm 1346: error("%s: critical option data invalid", __func__);
1347: goto out;
1348: }
1349: }
1350: buffer_clear(&tmp);
1351:
1352: buffer_append(&key->cert->extensions, exts, elen);
1353: buffer_append(&tmp, exts, elen);
1354: /* validate structure */
1355: while (buffer_len(&tmp) != 0) {
1356: if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL ||
1357: buffer_get_string_ptr_ret(&tmp, NULL) == NULL) {
1358: error("%s: extension data invalid", __func__);
1.83 djm 1359: goto out;
1360: }
1361: }
1362: buffer_clear(&tmp);
1363:
1.105 djm 1364: if ((key->cert->signature_key = key_from_blob2(sig_key, sklen, 0))
1365: == NULL) {
1.84 djm 1366: error("%s: Signature key invalid", __func__);
1.83 djm 1367: goto out;
1368: }
1369: if (key->cert->signature_key->type != KEY_RSA &&
1.92 djm 1370: key->cert->signature_key->type != KEY_DSA &&
1371: key->cert->signature_key->type != KEY_ECDSA) {
1.84 djm 1372: error("%s: Invalid signature key type %s (%d)", __func__,
1.83 djm 1373: key_type(key->cert->signature_key),
1374: key->cert->signature_key->type);
1375: goto out;
1376: }
1377:
1378: switch (key_verify(key->cert->signature_key, sig, slen,
1379: buffer_ptr(&key->cert->certblob), signed_len)) {
1380: case 1:
1.84 djm 1381: ret = 0;
1.83 djm 1382: break; /* Good signature */
1383: case 0:
1.84 djm 1384: error("%s: Invalid signature on certificate", __func__);
1.83 djm 1385: goto out;
1386: case -1:
1.84 djm 1387: error("%s: Certificate signature verification failed",
1388: __func__);
1.83 djm 1389: goto out;
1390: }
1391:
1392: out:
1393: buffer_free(&tmp);
1.103 djm 1394: free(principals);
1395: free(critical);
1396: free(exts);
1397: free(sig_key);
1398: free(sig);
1.83 djm 1399: return ret;
1400: }
1401:
1.105 djm 1402: static Key *
1403: key_from_blob2(const u_char *blob, u_int blen, int allow_cert)
1.12 markus 1404: {
1405: Buffer b;
1.92 djm 1406: int rlen, type, nid = -1;
1407: char *ktype = NULL, *curve = NULL;
1.12 markus 1408: Key *key = NULL;
1.92 djm 1409: EC_POINT *q = NULL;
1.12 markus 1410:
1411: #ifdef DEBUG_PK
1412: dump_base64(stderr, blob, blen);
1413: #endif
1414: buffer_init(&b);
1415: buffer_append(&b, blob, blen);
1.91 djm 1416: if ((ktype = buffer_get_cstring_ret(&b, NULL)) == NULL) {
1.57 djm 1417: error("key_from_blob: can't read key type");
1418: goto out;
1419: }
1420:
1.12 markus 1421: type = key_type_from_name(ktype);
1.92 djm 1422: if (key_type_plain(type) == KEY_ECDSA)
1423: nid = key_ecdsa_nid_from_name(ktype);
1.105 djm 1424: if (!allow_cert && key_type_is_cert(type)) {
1425: error("key_from_blob: certificate not allowed in this context");
1426: goto out;
1427: }
1.35 deraadt 1428: switch (type) {
1.87 djm 1429: case KEY_RSA_CERT:
1430: (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1431: /* FALLTHROUGH */
1.12 markus 1432: case KEY_RSA:
1.87 djm 1433: case KEY_RSA_CERT_V00:
1.12 markus 1434: key = key_new(type);
1.57 djm 1435: if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
1436: buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
1437: error("key_from_blob: can't read rsa key");
1.83 djm 1438: badkey:
1.57 djm 1439: key_free(key);
1440: key = NULL;
1441: goto out;
1442: }
1.12 markus 1443: #ifdef DEBUG_PK
1444: RSA_print_fp(stderr, key->rsa, 8);
1445: #endif
1446: break;
1.87 djm 1447: case KEY_DSA_CERT:
1448: (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1449: /* FALLTHROUGH */
1.12 markus 1450: case KEY_DSA:
1.87 djm 1451: case KEY_DSA_CERT_V00:
1.12 markus 1452: key = key_new(type);
1.57 djm 1453: if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
1454: buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
1455: buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
1456: buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
1457: error("key_from_blob: can't read dsa key");
1.83 djm 1458: goto badkey;
1.57 djm 1459: }
1.12 markus 1460: #ifdef DEBUG_PK
1461: DSA_print_fp(stderr, key->dsa, 8);
1462: #endif
1463: break;
1.92 djm 1464: case KEY_ECDSA_CERT:
1465: (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1466: /* FALLTHROUGH */
1467: case KEY_ECDSA:
1468: key = key_new(type);
1469: key->ecdsa_nid = nid;
1470: if ((curve = buffer_get_string_ret(&b, NULL)) == NULL) {
1471: error("key_from_blob: can't read ecdsa curve");
1472: goto badkey;
1473: }
1474: if (key->ecdsa_nid != key_curve_name_to_nid(curve)) {
1475: error("key_from_blob: ecdsa curve doesn't match type");
1476: goto badkey;
1477: }
1478: if (key->ecdsa != NULL)
1479: EC_KEY_free(key->ecdsa);
1480: if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid))
1481: == NULL)
1482: fatal("key_from_blob: EC_KEY_new_by_curve_name failed");
1483: if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL)
1484: fatal("key_from_blob: EC_POINT_new failed");
1485: if (buffer_get_ecpoint_ret(&b, EC_KEY_get0_group(key->ecdsa),
1486: q) == -1) {
1487: error("key_from_blob: can't read ecdsa key point");
1488: goto badkey;
1489: }
1490: if (key_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
1491: q) != 0)
1492: goto badkey;
1493: if (EC_KEY_set_public_key(key->ecdsa, q) != 1)
1494: fatal("key_from_blob: EC_KEY_set_public_key failed");
1495: #ifdef DEBUG_PK
1496: key_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q);
1497: #endif
1498: break;
1.12 markus 1499: case KEY_UNSPEC:
1500: key = key_new(type);
1501: break;
1502: default:
1503: error("key_from_blob: cannot handle type %s", ktype);
1.57 djm 1504: goto out;
1.12 markus 1505: }
1.83 djm 1506: if (key_is_cert(key) && cert_parse(&b, key, blob, blen) == -1) {
1507: error("key_from_blob: can't parse cert data");
1508: goto badkey;
1509: }
1.12 markus 1510: rlen = buffer_len(&b);
1511: if (key != NULL && rlen != 0)
1512: error("key_from_blob: remaining bytes in key blob %d", rlen);
1.57 djm 1513: out:
1.103 djm 1514: free(ktype);
1515: free(curve);
1.92 djm 1516: if (q != NULL)
1517: EC_POINT_free(q);
1.12 markus 1518: buffer_free(&b);
1519: return key;
1520: }
1521:
1.105 djm 1522: Key *
1523: key_from_blob(const u_char *blob, u_int blen)
1524: {
1525: return key_from_blob2(blob, blen, 1);
1526: }
1527:
1.100 djm 1528: static int
1529: to_blob(const Key *key, u_char **blobp, u_int *lenp, int force_plain)
1.12 markus 1530: {
1531: Buffer b;
1.100 djm 1532: int len, type;
1.12 markus 1533:
1.106 ! djm 1534: if (blobp != NULL)
! 1535: *blobp = NULL;
! 1536: if (lenp != NULL)
! 1537: *lenp = 0;
1.12 markus 1538: if (key == NULL) {
1539: error("key_to_blob: key == NULL");
1540: return 0;
1541: }
1542: buffer_init(&b);
1.100 djm 1543: type = force_plain ? key_type_plain(key->type) : key->type;
1544: switch (type) {
1.87 djm 1545: case KEY_DSA_CERT_V00:
1546: case KEY_RSA_CERT_V00:
1.83 djm 1547: case KEY_DSA_CERT:
1.92 djm 1548: case KEY_ECDSA_CERT:
1.83 djm 1549: case KEY_RSA_CERT:
1550: /* Use the existing blob */
1551: buffer_append(&b, buffer_ptr(&key->cert->certblob),
1552: buffer_len(&key->cert->certblob));
1553: break;
1.12 markus 1554: case KEY_DSA:
1.100 djm 1555: buffer_put_cstring(&b,
1556: key_ssh_name_from_type_nid(type, key->ecdsa_nid));
1.12 markus 1557: buffer_put_bignum2(&b, key->dsa->p);
1558: buffer_put_bignum2(&b, key->dsa->q);
1559: buffer_put_bignum2(&b, key->dsa->g);
1560: buffer_put_bignum2(&b, key->dsa->pub_key);
1561: break;
1.92 djm 1562: case KEY_ECDSA:
1.100 djm 1563: buffer_put_cstring(&b,
1564: key_ssh_name_from_type_nid(type, key->ecdsa_nid));
1.92 djm 1565: buffer_put_cstring(&b, key_curve_nid_to_name(key->ecdsa_nid));
1566: buffer_put_ecpoint(&b, EC_KEY_get0_group(key->ecdsa),
1567: EC_KEY_get0_public_key(key->ecdsa));
1568: break;
1.12 markus 1569: case KEY_RSA:
1.100 djm 1570: buffer_put_cstring(&b,
1571: key_ssh_name_from_type_nid(type, key->ecdsa_nid));
1.14 markus 1572: buffer_put_bignum2(&b, key->rsa->e);
1.12 markus 1573: buffer_put_bignum2(&b, key->rsa->n);
1574: break;
1575: default:
1.31 markus 1576: error("key_to_blob: unsupported key type %d", key->type);
1577: buffer_free(&b);
1578: return 0;
1.12 markus 1579: }
1580: len = buffer_len(&b);
1.48 markus 1581: if (lenp != NULL)
1582: *lenp = len;
1583: if (blobp != NULL) {
1584: *blobp = xmalloc(len);
1585: memcpy(*blobp, buffer_ptr(&b), len);
1586: }
1.12 markus 1587: memset(buffer_ptr(&b), 0, len);
1588: buffer_free(&b);
1589: return len;
1590: }
1591:
1592: int
1.100 djm 1593: key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1594: {
1595: return to_blob(key, blobp, lenp, 0);
1596: }
1597:
1598: int
1.12 markus 1599: key_sign(
1.55 jakob 1600: const Key *key,
1.40 markus 1601: u_char **sigp, u_int *lenp,
1.55 jakob 1602: const u_char *data, u_int datalen)
1.12 markus 1603: {
1.35 deraadt 1604: switch (key->type) {
1.87 djm 1605: case KEY_DSA_CERT_V00:
1.83 djm 1606: case KEY_DSA_CERT:
1.12 markus 1607: case KEY_DSA:
1608: return ssh_dss_sign(key, sigp, lenp, data, datalen);
1.92 djm 1609: case KEY_ECDSA_CERT:
1610: case KEY_ECDSA:
1611: return ssh_ecdsa_sign(key, sigp, lenp, data, datalen);
1.87 djm 1612: case KEY_RSA_CERT_V00:
1.83 djm 1613: case KEY_RSA_CERT:
1.12 markus 1614: case KEY_RSA:
1615: return ssh_rsa_sign(key, sigp, lenp, data, datalen);
1616: default:
1.56 markus 1617: error("key_sign: invalid key type %d", key->type);
1.12 markus 1618: return -1;
1619: }
1620: }
1621:
1.44 markus 1622: /*
1623: * key_verify returns 1 for a correct signature, 0 for an incorrect signature
1624: * and -1 on error.
1625: */
1.12 markus 1626: int
1627: key_verify(
1.55 jakob 1628: const Key *key,
1629: const u_char *signature, u_int signaturelen,
1630: const u_char *data, u_int datalen)
1.12 markus 1631: {
1.26 markus 1632: if (signaturelen == 0)
1633: return -1;
1634:
1.35 deraadt 1635: switch (key->type) {
1.87 djm 1636: case KEY_DSA_CERT_V00:
1.83 djm 1637: case KEY_DSA_CERT:
1.12 markus 1638: case KEY_DSA:
1639: return ssh_dss_verify(key, signature, signaturelen, data, datalen);
1.92 djm 1640: case KEY_ECDSA_CERT:
1641: case KEY_ECDSA:
1642: return ssh_ecdsa_verify(key, signature, signaturelen, data, datalen);
1.87 djm 1643: case KEY_RSA_CERT_V00:
1.83 djm 1644: case KEY_RSA_CERT:
1.12 markus 1645: case KEY_RSA:
1646: return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
1647: default:
1.56 markus 1648: error("key_verify: invalid key type %d", key->type);
1.12 markus 1649: return -1;
1650: }
1.42 markus 1651: }
1652:
1653: /* Converts a private to a public key */
1654: Key *
1.55 jakob 1655: key_demote(const Key *k)
1.42 markus 1656: {
1657: Key *pk;
1.43 markus 1658:
1.63 djm 1659: pk = xcalloc(1, sizeof(*pk));
1.42 markus 1660: pk->type = k->type;
1661: pk->flags = k->flags;
1.92 djm 1662: pk->ecdsa_nid = k->ecdsa_nid;
1.42 markus 1663: pk->dsa = NULL;
1.92 djm 1664: pk->ecdsa = NULL;
1.42 markus 1665: pk->rsa = NULL;
1666:
1667: switch (k->type) {
1.87 djm 1668: case KEY_RSA_CERT_V00:
1.83 djm 1669: case KEY_RSA_CERT:
1670: key_cert_copy(k, pk);
1671: /* FALLTHROUGH */
1.42 markus 1672: case KEY_RSA1:
1673: case KEY_RSA:
1674: if ((pk->rsa = RSA_new()) == NULL)
1675: fatal("key_demote: RSA_new failed");
1676: if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
1677: fatal("key_demote: BN_dup failed");
1678: if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
1679: fatal("key_demote: BN_dup failed");
1680: break;
1.87 djm 1681: case KEY_DSA_CERT_V00:
1.83 djm 1682: case KEY_DSA_CERT:
1683: key_cert_copy(k, pk);
1684: /* FALLTHROUGH */
1.42 markus 1685: case KEY_DSA:
1686: if ((pk->dsa = DSA_new()) == NULL)
1687: fatal("key_demote: DSA_new failed");
1688: if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
1689: fatal("key_demote: BN_dup failed");
1690: if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
1691: fatal("key_demote: BN_dup failed");
1692: if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
1693: fatal("key_demote: BN_dup failed");
1694: if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
1695: fatal("key_demote: BN_dup failed");
1696: break;
1.92 djm 1697: case KEY_ECDSA_CERT:
1698: key_cert_copy(k, pk);
1699: /* FALLTHROUGH */
1700: case KEY_ECDSA:
1701: if ((pk->ecdsa = EC_KEY_new_by_curve_name(pk->ecdsa_nid)) == NULL)
1702: fatal("key_demote: EC_KEY_new_by_curve_name failed");
1703: if (EC_KEY_set_public_key(pk->ecdsa,
1704: EC_KEY_get0_public_key(k->ecdsa)) != 1)
1705: fatal("key_demote: EC_KEY_set_public_key failed");
1706: break;
1.42 markus 1707: default:
1708: fatal("key_free: bad key type %d", k->type);
1709: break;
1710: }
1711:
1712: return (pk);
1.83 djm 1713: }
1714:
1715: int
1716: key_is_cert(const Key *k)
1717: {
1.87 djm 1718: if (k == NULL)
1719: return 0;
1.105 djm 1720: return key_type_is_cert(k->type);
1.83 djm 1721: }
1722:
1723: /* Return the cert-less equivalent to a certified key type */
1724: int
1725: key_type_plain(int type)
1726: {
1727: switch (type) {
1.87 djm 1728: case KEY_RSA_CERT_V00:
1.83 djm 1729: case KEY_RSA_CERT:
1730: return KEY_RSA;
1.87 djm 1731: case KEY_DSA_CERT_V00:
1.83 djm 1732: case KEY_DSA_CERT:
1733: return KEY_DSA;
1.92 djm 1734: case KEY_ECDSA_CERT:
1735: return KEY_ECDSA;
1.83 djm 1736: default:
1737: return type;
1738: }
1739: }
1740:
1741: /* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */
1742: int
1.87 djm 1743: key_to_certified(Key *k, int legacy)
1.83 djm 1744: {
1745: switch (k->type) {
1746: case KEY_RSA:
1747: k->cert = cert_new();
1.87 djm 1748: k->type = legacy ? KEY_RSA_CERT_V00 : KEY_RSA_CERT;
1.83 djm 1749: return 0;
1750: case KEY_DSA:
1751: k->cert = cert_new();
1.87 djm 1752: k->type = legacy ? KEY_DSA_CERT_V00 : KEY_DSA_CERT;
1.83 djm 1753: return 0;
1.92 djm 1754: case KEY_ECDSA:
1.97 djm 1755: if (legacy)
1756: fatal("%s: legacy ECDSA certificates are not supported",
1757: __func__);
1.92 djm 1758: k->cert = cert_new();
1759: k->type = KEY_ECDSA_CERT;
1760: return 0;
1.83 djm 1761: default:
1762: error("%s: key has incorrect type %s", __func__, key_type(k));
1763: return -1;
1764: }
1765: }
1766:
1767: /* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */
1768: int
1769: key_drop_cert(Key *k)
1770: {
1771: switch (k->type) {
1.87 djm 1772: case KEY_RSA_CERT_V00:
1.83 djm 1773: case KEY_RSA_CERT:
1774: cert_free(k->cert);
1775: k->type = KEY_RSA;
1776: return 0;
1.87 djm 1777: case KEY_DSA_CERT_V00:
1.83 djm 1778: case KEY_DSA_CERT:
1779: cert_free(k->cert);
1780: k->type = KEY_DSA;
1781: return 0;
1.92 djm 1782: case KEY_ECDSA_CERT:
1783: cert_free(k->cert);
1784: k->type = KEY_ECDSA;
1785: return 0;
1.83 djm 1786: default:
1787: error("%s: key has incorrect type %s", __func__, key_type(k));
1788: return -1;
1789: }
1790: }
1791:
1.92 djm 1792: /*
1793: * Sign a KEY_RSA_CERT, KEY_DSA_CERT or KEY_ECDSA_CERT, (re-)generating
1794: * the signed certblob
1795: */
1.83 djm 1796: int
1797: key_certify(Key *k, Key *ca)
1798: {
1799: Buffer principals;
1800: u_char *ca_blob, *sig_blob, nonce[32];
1801: u_int i, ca_len, sig_len;
1802:
1803: if (k->cert == NULL) {
1804: error("%s: key lacks cert info", __func__);
1805: return -1;
1806: }
1807:
1808: if (!key_is_cert(k)) {
1809: error("%s: certificate has unknown type %d", __func__,
1810: k->cert->type);
1811: return -1;
1812: }
1813:
1.92 djm 1814: if (ca->type != KEY_RSA && ca->type != KEY_DSA &&
1815: ca->type != KEY_ECDSA) {
1.83 djm 1816: error("%s: CA key has unsupported type %s", __func__,
1817: key_type(ca));
1818: return -1;
1819: }
1820:
1821: key_to_blob(ca, &ca_blob, &ca_len);
1822:
1823: buffer_clear(&k->cert->certblob);
1824: buffer_put_cstring(&k->cert->certblob, key_ssh_name(k));
1825:
1.87 djm 1826: /* -v01 certs put nonce first */
1.96 djm 1827: arc4random_buf(&nonce, sizeof(nonce));
1828: if (!key_cert_is_legacy(k))
1.87 djm 1829: buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce));
1830:
1.83 djm 1831: switch (k->type) {
1.87 djm 1832: case KEY_DSA_CERT_V00:
1.83 djm 1833: case KEY_DSA_CERT:
1834: buffer_put_bignum2(&k->cert->certblob, k->dsa->p);
1835: buffer_put_bignum2(&k->cert->certblob, k->dsa->q);
1836: buffer_put_bignum2(&k->cert->certblob, k->dsa->g);
1837: buffer_put_bignum2(&k->cert->certblob, k->dsa->pub_key);
1838: break;
1.92 djm 1839: case KEY_ECDSA_CERT:
1840: buffer_put_cstring(&k->cert->certblob,
1841: key_curve_nid_to_name(k->ecdsa_nid));
1842: buffer_put_ecpoint(&k->cert->certblob,
1843: EC_KEY_get0_group(k->ecdsa),
1844: EC_KEY_get0_public_key(k->ecdsa));
1845: break;
1.87 djm 1846: case KEY_RSA_CERT_V00:
1.83 djm 1847: case KEY_RSA_CERT:
1848: buffer_put_bignum2(&k->cert->certblob, k->rsa->e);
1849: buffer_put_bignum2(&k->cert->certblob, k->rsa->n);
1850: break;
1851: default:
1852: error("%s: key has incorrect type %s", __func__, key_type(k));
1853: buffer_clear(&k->cert->certblob);
1.103 djm 1854: free(ca_blob);
1.83 djm 1855: return -1;
1856: }
1857:
1.87 djm 1858: /* -v01 certs have a serial number next */
1.92 djm 1859: if (!key_cert_is_legacy(k))
1.87 djm 1860: buffer_put_int64(&k->cert->certblob, k->cert->serial);
1861:
1.83 djm 1862: buffer_put_int(&k->cert->certblob, k->cert->type);
1863: buffer_put_cstring(&k->cert->certblob, k->cert->key_id);
1864:
1865: buffer_init(&principals);
1866: for (i = 0; i < k->cert->nprincipals; i++)
1867: buffer_put_cstring(&principals, k->cert->principals[i]);
1868: buffer_put_string(&k->cert->certblob, buffer_ptr(&principals),
1869: buffer_len(&principals));
1870: buffer_free(&principals);
1871:
1872: buffer_put_int64(&k->cert->certblob, k->cert->valid_after);
1873: buffer_put_int64(&k->cert->certblob, k->cert->valid_before);
1874: buffer_put_string(&k->cert->certblob,
1.87 djm 1875: buffer_ptr(&k->cert->critical), buffer_len(&k->cert->critical));
1876:
1877: /* -v01 certs have non-critical options here */
1.92 djm 1878: if (!key_cert_is_legacy(k)) {
1.87 djm 1879: buffer_put_string(&k->cert->certblob,
1880: buffer_ptr(&k->cert->extensions),
1881: buffer_len(&k->cert->extensions));
1882: }
1883:
1884: /* -v00 certs put the nonce at the end */
1.92 djm 1885: if (key_cert_is_legacy(k))
1.87 djm 1886: buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce));
1.83 djm 1887:
1888: buffer_put_string(&k->cert->certblob, NULL, 0); /* reserved */
1889: buffer_put_string(&k->cert->certblob, ca_blob, ca_len);
1.103 djm 1890: free(ca_blob);
1.83 djm 1891:
1892: /* Sign the whole mess */
1893: if (key_sign(ca, &sig_blob, &sig_len, buffer_ptr(&k->cert->certblob),
1894: buffer_len(&k->cert->certblob)) != 0) {
1895: error("%s: signature operation failed", __func__);
1896: buffer_clear(&k->cert->certblob);
1897: return -1;
1898: }
1899: /* Append signature and we are done */
1900: buffer_put_string(&k->cert->certblob, sig_blob, sig_len);
1.103 djm 1901: free(sig_blob);
1.83 djm 1902:
1903: return 0;
1904: }
1905:
1906: int
1907: key_cert_check_authority(const Key *k, int want_host, int require_principal,
1908: const char *name, const char **reason)
1909: {
1910: u_int i, principal_matches;
1911: time_t now = time(NULL);
1912:
1913: if (want_host) {
1914: if (k->cert->type != SSH2_CERT_TYPE_HOST) {
1915: *reason = "Certificate invalid: not a host certificate";
1916: return -1;
1917: }
1918: } else {
1919: if (k->cert->type != SSH2_CERT_TYPE_USER) {
1920: *reason = "Certificate invalid: not a user certificate";
1921: return -1;
1922: }
1923: }
1924: if (now < 0) {
1925: error("%s: system clock lies before epoch", __func__);
1926: *reason = "Certificate invalid: not yet valid";
1927: return -1;
1928: }
1929: if ((u_int64_t)now < k->cert->valid_after) {
1930: *reason = "Certificate invalid: not yet valid";
1931: return -1;
1932: }
1933: if ((u_int64_t)now >= k->cert->valid_before) {
1934: *reason = "Certificate invalid: expired";
1935: return -1;
1936: }
1937: if (k->cert->nprincipals == 0) {
1938: if (require_principal) {
1939: *reason = "Certificate lacks principal list";
1940: return -1;
1941: }
1.88 djm 1942: } else if (name != NULL) {
1.83 djm 1943: principal_matches = 0;
1944: for (i = 0; i < k->cert->nprincipals; i++) {
1945: if (strcmp(name, k->cert->principals[i]) == 0) {
1946: principal_matches = 1;
1947: break;
1948: }
1949: }
1950: if (!principal_matches) {
1951: *reason = "Certificate invalid: name is not a listed "
1952: "principal";
1953: return -1;
1954: }
1955: }
1956: return 0;
1.87 djm 1957: }
1958:
1959: int
1.100 djm 1960: key_cert_is_legacy(const Key *k)
1.87 djm 1961: {
1962: switch (k->type) {
1963: case KEY_DSA_CERT_V00:
1964: case KEY_RSA_CERT_V00:
1965: return 1;
1966: default:
1967: return 0;
1968: }
1.4 markus 1969: }
1.92 djm 1970:
1.93 djm 1971: /* XXX: these are really begging for a table-driven approach */
1.92 djm 1972: int
1973: key_curve_name_to_nid(const char *name)
1974: {
1975: if (strcmp(name, "nistp256") == 0)
1976: return NID_X9_62_prime256v1;
1977: else if (strcmp(name, "nistp384") == 0)
1978: return NID_secp384r1;
1979: else if (strcmp(name, "nistp521") == 0)
1980: return NID_secp521r1;
1981:
1982: debug("%s: unsupported EC curve name \"%.100s\"", __func__, name);
1983: return -1;
1984: }
1985:
1.93 djm 1986: u_int
1987: key_curve_nid_to_bits(int nid)
1988: {
1989: switch (nid) {
1990: case NID_X9_62_prime256v1:
1991: return 256;
1992: case NID_secp384r1:
1993: return 384;
1994: case NID_secp521r1:
1995: return 521;
1996: default:
1997: error("%s: unsupported EC curve nid %d", __func__, nid);
1998: return 0;
1999: }
2000: }
2001:
1.92 djm 2002: const char *
2003: key_curve_nid_to_name(int nid)
2004: {
2005: if (nid == NID_X9_62_prime256v1)
2006: return "nistp256";
2007: else if (nid == NID_secp384r1)
2008: return "nistp384";
2009: else if (nid == NID_secp521r1)
2010: return "nistp521";
2011:
2012: error("%s: unsupported EC curve nid %d", __func__, nid);
2013: return NULL;
1.93 djm 2014: }
2015:
2016: const EVP_MD *
2017: key_ec_nid_to_evpmd(int nid)
2018: {
2019: int kbits = key_curve_nid_to_bits(nid);
2020:
2021: if (kbits == 0)
2022: fatal("%s: invalid nid %d", __func__, nid);
2023: /* RFC5656 section 6.2.1 */
2024: if (kbits <= 256)
2025: return EVP_sha256();
2026: else if (kbits <= 384)
2027: return EVP_sha384();
2028: else
2029: return EVP_sha512();
1.92 djm 2030: }
2031:
2032: int
2033: key_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
2034: {
2035: BN_CTX *bnctx;
2036: EC_POINT *nq = NULL;
2037: BIGNUM *order, *x, *y, *tmp;
2038: int ret = -1;
2039:
2040: if ((bnctx = BN_CTX_new()) == NULL)
2041: fatal("%s: BN_CTX_new failed", __func__);
2042: BN_CTX_start(bnctx);
2043:
2044: /*
2045: * We shouldn't ever hit this case because bignum_get_ecpoint()
2046: * refuses to load GF2m points.
2047: */
2048: if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2049: NID_X9_62_prime_field) {
2050: error("%s: group is not a prime field", __func__);
2051: goto out;
2052: }
2053:
2054: /* Q != infinity */
2055: if (EC_POINT_is_at_infinity(group, public)) {
2056: error("%s: received degenerate public key (infinity)",
2057: __func__);
2058: goto out;
2059: }
2060:
2061: if ((x = BN_CTX_get(bnctx)) == NULL ||
2062: (y = BN_CTX_get(bnctx)) == NULL ||
2063: (order = BN_CTX_get(bnctx)) == NULL ||
2064: (tmp = BN_CTX_get(bnctx)) == NULL)
2065: fatal("%s: BN_CTX_get failed", __func__);
2066:
2067: /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2068: if (EC_GROUP_get_order(group, order, bnctx) != 1)
2069: fatal("%s: EC_GROUP_get_order failed", __func__);
2070: if (EC_POINT_get_affine_coordinates_GFp(group, public,
2071: x, y, bnctx) != 1)
2072: fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__);
2073: if (BN_num_bits(x) <= BN_num_bits(order) / 2) {
2074: error("%s: public key x coordinate too small: "
2075: "bits(x) = %d, bits(order)/2 = %d", __func__,
2076: BN_num_bits(x), BN_num_bits(order) / 2);
2077: goto out;
2078: }
2079: if (BN_num_bits(y) <= BN_num_bits(order) / 2) {
2080: error("%s: public key y coordinate too small: "
2081: "bits(y) = %d, bits(order)/2 = %d", __func__,
2082: BN_num_bits(x), BN_num_bits(order) / 2);
2083: goto out;
2084: }
2085:
2086: /* nQ == infinity (n == order of subgroup) */
2087: if ((nq = EC_POINT_new(group)) == NULL)
2088: fatal("%s: BN_CTX_tmp failed", __func__);
2089: if (EC_POINT_mul(group, nq, NULL, public, order, bnctx) != 1)
2090: fatal("%s: EC_GROUP_mul failed", __func__);
2091: if (EC_POINT_is_at_infinity(group, nq) != 1) {
2092: error("%s: received degenerate public key (nQ != infinity)",
2093: __func__);
2094: goto out;
2095: }
2096:
2097: /* x < order - 1, y < order - 1 */
2098: if (!BN_sub(tmp, order, BN_value_one()))
2099: fatal("%s: BN_sub failed", __func__);
2100: if (BN_cmp(x, tmp) >= 0) {
2101: error("%s: public key x coordinate >= group order - 1",
2102: __func__);
2103: goto out;
2104: }
2105: if (BN_cmp(y, tmp) >= 0) {
2106: error("%s: public key y coordinate >= group order - 1",
2107: __func__);
2108: goto out;
2109: }
2110: ret = 0;
2111: out:
2112: BN_CTX_free(bnctx);
2113: EC_POINT_free(nq);
2114: return ret;
2115: }
2116:
2117: int
2118: key_ec_validate_private(const EC_KEY *key)
2119: {
2120: BN_CTX *bnctx;
2121: BIGNUM *order, *tmp;
2122: int ret = -1;
2123:
2124: if ((bnctx = BN_CTX_new()) == NULL)
2125: fatal("%s: BN_CTX_new failed", __func__);
2126: BN_CTX_start(bnctx);
2127:
2128: if ((order = BN_CTX_get(bnctx)) == NULL ||
2129: (tmp = BN_CTX_get(bnctx)) == NULL)
2130: fatal("%s: BN_CTX_get failed", __func__);
2131:
2132: /* log2(private) > log2(order)/2 */
2133: if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, bnctx) != 1)
2134: fatal("%s: EC_GROUP_get_order failed", __func__);
2135: if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
2136: BN_num_bits(order) / 2) {
2137: error("%s: private key too small: "
2138: "bits(y) = %d, bits(order)/2 = %d", __func__,
2139: BN_num_bits(EC_KEY_get0_private_key(key)),
2140: BN_num_bits(order) / 2);
2141: goto out;
2142: }
2143:
2144: /* private < order - 1 */
2145: if (!BN_sub(tmp, order, BN_value_one()))
2146: fatal("%s: BN_sub failed", __func__);
2147: if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0) {
2148: error("%s: private key >= group order - 1", __func__);
2149: goto out;
2150: }
2151: ret = 0;
2152: out:
2153: BN_CTX_free(bnctx);
2154: return ret;
2155: }
2156:
2157: #if defined(DEBUG_KEXECDH) || defined(DEBUG_PK)
2158: void
2159: key_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
2160: {
2161: BIGNUM *x, *y;
2162: BN_CTX *bnctx;
2163:
2164: if (point == NULL) {
2165: fputs("point=(NULL)\n", stderr);
2166: return;
2167: }
2168: if ((bnctx = BN_CTX_new()) == NULL)
2169: fatal("%s: BN_CTX_new failed", __func__);
2170: BN_CTX_start(bnctx);
2171: if ((x = BN_CTX_get(bnctx)) == NULL || (y = BN_CTX_get(bnctx)) == NULL)
2172: fatal("%s: BN_CTX_get failed", __func__);
2173: if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2174: NID_X9_62_prime_field)
2175: fatal("%s: group is not a prime field", __func__);
2176: if (EC_POINT_get_affine_coordinates_GFp(group, point, x, y, bnctx) != 1)
2177: fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__);
2178: fputs("x=", stderr);
2179: BN_print_fp(stderr, x);
2180: fputs("\ny=", stderr);
2181: BN_print_fp(stderr, y);
2182: fputs("\n", stderr);
2183: BN_CTX_free(bnctx);
2184: }
2185:
2186: void
2187: key_dump_ec_key(const EC_KEY *key)
2188: {
2189: const BIGNUM *exponent;
2190:
2191: key_dump_ec_point(EC_KEY_get0_group(key), EC_KEY_get0_public_key(key));
2192: fputs("exponent=", stderr);
2193: if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
2194: fputs("(NULL)", stderr);
2195: else
2196: BN_print_fp(stderr, EC_KEY_get0_private_key(key));
2197: fputs("\n", stderr);
2198: }
2199: #endif /* defined(DEBUG_KEXECDH) || defined(DEBUG_PK) */
2200: