blob: c4d9d577175d198aa23f258ba08c64c67e3b3755 [file] [log] [blame]
Damien Miller30da3442010-05-10 11:58:03 +10001/* $OpenBSD: key.c,v 1.88 2010/05/07 11:30:29 djm Exp $ */
Damien Miller450a7a12000-03-26 13:04:51 +10002/*
Damien Millere4340be2000-09-16 13:29:08 +11003 * 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 *
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Darren Tucker0f0ef0a2008-06-13 08:58:05 +100014 * Copyright (c) 2008 Alexander von Gernler. All rights reserved.
Damien Miller450a7a12000-03-26 13:04:51 +100015 *
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.
Damien Miller450a7a12000-03-26 13:04:51 +100024 *
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 */
Damien Millerd7834352006-08-05 12:39:39 +100036
Damien Miller450a7a12000-03-26 13:04:51 +100037#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038
Darren Tucker9c16ac92008-06-13 04:40:35 +100039#include <sys/param.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <sys/types.h>
41
Damien Miller450a7a12000-03-26 13:04:51 +100042#include <openssl/evp.h>
Darren Tucker3d295a62008-02-28 19:22:04 +110043#include <openbsd-compat/openssl-compat.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
Damien Millerded319c2006-09-01 15:38:36 +100045#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100046#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
48
Damien Miller450a7a12000-03-26 13:04:51 +100049#include "xmalloc.h"
50#include "key.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110051#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "uuencode.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110053#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "log.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110055#include "ssh2.h"
56
57static struct KeyCert *
58cert_new(void)
59{
60 struct KeyCert *cert;
61
62 cert = xcalloc(1, sizeof(*cert));
63 buffer_init(&cert->certblob);
Damien Miller4e270b02010-04-16 15:56:21 +100064 buffer_init(&cert->critical);
65 buffer_init(&cert->extensions);
Damien Miller0a80ca12010-02-27 07:55:05 +110066 cert->key_id = NULL;
67 cert->principals = NULL;
68 cert->signature_key = NULL;
69 return cert;
70}
Damien Miller450a7a12000-03-26 13:04:51 +100071
72Key *
73key_new(int type)
74{
75 Key *k;
76 RSA *rsa;
77 DSA *dsa;
Damien Miller07d86be2006-03-26 14:19:21 +110078 k = xcalloc(1, sizeof(*k));
Damien Miller450a7a12000-03-26 13:04:51 +100079 k->type = type;
Damien Millereba71ba2000-04-29 23:57:08 +100080 k->dsa = NULL;
81 k->rsa = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +110082 k->cert = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100083 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110084 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100085 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +100086 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +110087 case KEY_RSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +110088 if ((rsa = RSA_new()) == NULL)
89 fatal("key_new: RSA_new failed");
90 if ((rsa->n = BN_new()) == NULL)
91 fatal("key_new: BN_new failed");
92 if ((rsa->e = BN_new()) == NULL)
93 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100094 k->rsa = rsa;
95 break;
96 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +100097 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +110098 case KEY_DSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +110099 if ((dsa = DSA_new()) == NULL)
100 fatal("key_new: DSA_new failed");
101 if ((dsa->p = BN_new()) == NULL)
102 fatal("key_new: BN_new failed");
103 if ((dsa->q = BN_new()) == NULL)
104 fatal("key_new: BN_new failed");
105 if ((dsa->g = BN_new()) == NULL)
106 fatal("key_new: BN_new failed");
107 if ((dsa->pub_key = BN_new()) == NULL)
108 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +1000109 k->dsa = dsa;
110 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100111 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +1000112 break;
113 default:
114 fatal("key_new: bad key type %d", k->type);
115 break;
116 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100117
118 if (key_is_cert(k))
119 k->cert = cert_new();
120
Damien Miller450a7a12000-03-26 13:04:51 +1000121 return k;
122}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000123
Damien Miller0a80ca12010-02-27 07:55:05 +1100124void
125key_add_private(Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100126{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100127 switch (k->type) {
128 case KEY_RSA1:
129 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000130 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100131 case KEY_RSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +1100132 if ((k->rsa->d = BN_new()) == NULL)
133 fatal("key_new_private: BN_new failed");
134 if ((k->rsa->iqmp = BN_new()) == NULL)
135 fatal("key_new_private: BN_new failed");
136 if ((k->rsa->q = BN_new()) == NULL)
137 fatal("key_new_private: BN_new failed");
138 if ((k->rsa->p = BN_new()) == NULL)
139 fatal("key_new_private: BN_new failed");
140 if ((k->rsa->dmq1 = BN_new()) == NULL)
141 fatal("key_new_private: BN_new failed");
142 if ((k->rsa->dmp1 = BN_new()) == NULL)
143 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100144 break;
145 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000146 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100147 case KEY_DSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +1100148 if ((k->dsa->priv_key = BN_new()) == NULL)
149 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100150 break;
151 case KEY_UNSPEC:
152 break;
153 default:
154 break;
155 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100156}
157
158Key *
159key_new_private(int type)
160{
161 Key *k = key_new(type);
162
163 key_add_private(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164 return k;
165}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000166
Damien Miller0a80ca12010-02-27 07:55:05 +1100167static void
168cert_free(struct KeyCert *cert)
169{
170 u_int i;
171
172 buffer_free(&cert->certblob);
Damien Miller4e270b02010-04-16 15:56:21 +1000173 buffer_free(&cert->critical);
174 buffer_free(&cert->extensions);
Damien Miller0a80ca12010-02-27 07:55:05 +1100175 if (cert->key_id != NULL)
176 xfree(cert->key_id);
177 for (i = 0; i < cert->nprincipals; i++)
178 xfree(cert->principals[i]);
179 if (cert->principals != NULL)
180 xfree(cert->principals);
181 if (cert->signature_key != NULL)
182 key_free(cert->signature_key);
183}
184
Damien Miller450a7a12000-03-26 13:04:51 +1000185void
186key_free(Key *k)
187{
Damien Miller429fcc22006-03-26 14:02:16 +1100188 if (k == NULL)
Damien Millerbbaad772006-03-26 14:03:03 +1100189 fatal("key_free: key is NULL");
Damien Miller450a7a12000-03-26 13:04:51 +1000190 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100191 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000192 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000193 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100194 case KEY_RSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000195 if (k->rsa != NULL)
196 RSA_free(k->rsa);
197 k->rsa = NULL;
198 break;
199 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000200 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100201 case KEY_DSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000202 if (k->dsa != NULL)
203 DSA_free(k->dsa);
204 k->dsa = NULL;
205 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100206 case KEY_UNSPEC:
207 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000208 default:
209 fatal("key_free: bad key type %d", k->type);
210 break;
211 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100212 if (key_is_cert(k)) {
213 if (k->cert != NULL)
214 cert_free(k->cert);
215 k->cert = NULL;
216 }
217
Damien Miller450a7a12000-03-26 13:04:51 +1000218 xfree(k);
219}
Damien Millerf58b58c2003-11-17 21:18:23 +1100220
Damien Miller0a80ca12010-02-27 07:55:05 +1100221static int
222cert_compare(struct KeyCert *a, struct KeyCert *b)
Damien Miller450a7a12000-03-26 13:04:51 +1000223{
Damien Miller0a80ca12010-02-27 07:55:05 +1100224 if (a == NULL && b == NULL)
225 return 1;
226 if (a == NULL || b == NULL)
Damien Miller450a7a12000-03-26 13:04:51 +1000227 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100228 if (buffer_len(&a->certblob) != buffer_len(&b->certblob))
229 return 0;
230 if (memcmp(buffer_ptr(&a->certblob), buffer_ptr(&b->certblob),
231 buffer_len(&a->certblob)) != 0)
232 return 0;
233 return 1;
234}
235
236/*
237 * Compare public portions of key only, allowing comparisons between
238 * certificates and plain keys too.
239 */
240int
241key_equal_public(const Key *a, const Key *b)
242{
243 if (a == NULL || b == NULL ||
244 key_type_plain(a->type) != key_type_plain(b->type))
245 return 0;
246
Damien Miller450a7a12000-03-26 13:04:51 +1000247 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100248 case KEY_RSA1:
Damien Miller4e270b02010-04-16 15:56:21 +1000249 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100250 case KEY_RSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000251 case KEY_RSA:
252 return a->rsa != NULL && b->rsa != NULL &&
253 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
254 BN_cmp(a->rsa->n, b->rsa->n) == 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000255 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100256 case KEY_DSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000257 case KEY_DSA:
258 return a->dsa != NULL && b->dsa != NULL &&
259 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
260 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
261 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
262 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000263 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000264 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000265 }
Damien Miller87dd5f22008-07-11 17:35:09 +1000266 /* NOTREACHED */
Damien Miller450a7a12000-03-26 13:04:51 +1000267}
268
Damien Miller0a80ca12010-02-27 07:55:05 +1100269int
270key_equal(const Key *a, const Key *b)
271{
272 if (a == NULL || b == NULL || a->type != b->type)
273 return 0;
274 if (key_is_cert(a)) {
275 if (!cert_compare(a->cert, b->cert))
276 return 0;
277 }
278 return key_equal_public(a, b);
279}
280
Damien Miller37876e92003-05-15 10:19:46 +1000281u_char*
Damien Miller0a80ca12010-02-27 07:55:05 +1100282key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000283{
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000284 const EVP_MD *md = NULL;
Ben Lindstromf0b48532001-03-12 02:59:31 +0000285 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000286 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000287 u_char *retval = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000288 u_int len = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100289 int nlen, elen, otype;
Damien Miller450a7a12000-03-26 13:04:51 +1000290
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000291 *dgst_raw_length = 0;
292
Ben Lindstromf0b48532001-03-12 02:59:31 +0000293 switch (dgst_type) {
294 case SSH_FP_MD5:
295 md = EVP_md5();
296 break;
297 case SSH_FP_SHA1:
298 md = EVP_sha1();
299 break;
300 default:
301 fatal("key_fingerprint_raw: bad digest type %d",
302 dgst_type);
303 }
Damien Miller450a7a12000-03-26 13:04:51 +1000304 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100305 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000306 nlen = BN_num_bytes(k->rsa->n);
307 elen = BN_num_bytes(k->rsa->e);
308 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000309 blob = xmalloc(len);
310 BN_bn2bin(k->rsa->n, blob);
311 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000312 break;
313 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100314 case KEY_RSA:
315 key_to_blob(k, &blob, &len);
316 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000317 case KEY_DSA_CERT_V00:
318 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100319 case KEY_DSA_CERT:
320 case KEY_RSA_CERT:
321 /* We want a fingerprint of the _key_ not of the cert */
322 otype = k->type;
323 k->type = key_type_plain(k->type);
324 key_to_blob(k, &blob, &len);
325 k->type = otype;
326 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100327 case KEY_UNSPEC:
328 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000329 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000330 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000331 break;
332 }
Damien Millereba71ba2000-04-29 23:57:08 +1000333 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000334 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000335 EVP_DigestInit(&ctx, md);
336 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100337 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000338 memset(blob, 0, len);
339 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000340 } else {
341 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000342 }
343 return retval;
344}
345
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000346static char *
347key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000348{
349 char *retval;
Damien Millereccb9de2005-06-17 12:59:34 +1000350 u_int i;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000351
Damien Miller07d86be2006-03-26 14:19:21 +1100352 retval = xcalloc(1, dgst_raw_len * 3 + 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100353 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000354 char hex[4];
355 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
Darren Tucker29588612003-07-14 17:28:34 +1000356 strlcat(retval, hex, dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000357 }
Darren Tucker29588612003-07-14 17:28:34 +1000358
359 /* Remove the trailing ':' character */
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000360 retval[(dgst_raw_len * 3) - 1] = '\0';
361 return retval;
362}
363
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000364static char *
365key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000366{
367 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
368 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
369 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000370 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000371 char *retval;
372
373 rounds = (dgst_raw_len / 2) + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100374 retval = xcalloc((rounds * 6), sizeof(char));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000375 retval[j++] = 'x';
376 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000377 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000378 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
379 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000380 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000381 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
382 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000383 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000384 retval[j++] = vowels[idx0];
385 retval[j++] = consonants[idx1];
386 retval[j++] = vowels[idx2];
387 if ((i + 1) < rounds) {
388 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
389 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
390 retval[j++] = consonants[idx3];
391 retval[j++] = '-';
392 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000393 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000394 ((((u_int)(dgst_raw[2 * i])) * 7) +
395 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000396 }
397 } else {
398 idx0 = seed % 6;
399 idx1 = 16;
400 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000401 retval[j++] = vowels[idx0];
402 retval[j++] = consonants[idx1];
403 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000404 }
405 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000406 retval[j++] = 'x';
407 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000408 return retval;
409}
410
Darren Tucker9c16ac92008-06-13 04:40:35 +1000411/*
412 * Draw an ASCII-Art representing the fingerprint so human brain can
413 * profit from its built-in pattern recognition ability.
414 * This technique is called "random art" and can be found in some
415 * scientific publications like this original paper:
416 *
417 * "Hash Visualization: a New Technique to improve Real-World Security",
418 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
419 * Techniques and E-Commerce (CrypTEC '99)
420 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
421 *
422 * The subject came up in a talk by Dan Kaminsky, too.
423 *
424 * If you see the picture is different, the key is different.
425 * If the picture looks the same, you still know nothing.
426 *
427 * The algorithm used here is a worm crawling over a discrete plane,
428 * leaving a trace (augmenting the field) everywhere it goes.
429 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
430 * makes the respective movement vector be ignored for this turn.
431 * Graphs are not unambiguous, because circles in graphs can be
432 * walked in either direction.
433 */
Darren Tucker987ac842008-06-13 04:54:40 +1000434
435/*
436 * Field sizes for the random art. Have to be odd, so the starting point
437 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
438 * Else pictures would be too dense, and drawing the frame would
439 * fail, too, because the key type would not fit in anymore.
440 */
441#define FLDBASE 8
442#define FLDSIZE_Y (FLDBASE + 1)
443#define FLDSIZE_X (FLDBASE * 2 + 1)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000444static char *
Darren Tucker987ac842008-06-13 04:54:40 +1000445key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000446{
447 /*
448 * Chars to be used after each other every time the worm
449 * intersects with itself. Matter of taste.
450 */
Darren Tucker4b3b9772008-06-13 04:55:10 +1000451 char *augmentation_string = " .o+=*BOX@%&#/^SE";
Darren Tucker9c16ac92008-06-13 04:40:35 +1000452 char *retval, *p;
Darren Tucker014d76f2008-06-13 04:43:51 +1000453 u_char field[FLDSIZE_X][FLDSIZE_Y];
Darren Tucker9c16ac92008-06-13 04:40:35 +1000454 u_int i, b;
455 int x, y;
Darren Tuckerd32b28a2008-06-13 04:45:50 +1000456 size_t len = strlen(augmentation_string) - 1;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000457
458 retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
459
460 /* initialize field */
Darren Tucker014d76f2008-06-13 04:43:51 +1000461 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
Darren Tucker9c16ac92008-06-13 04:40:35 +1000462 x = FLDSIZE_X / 2;
463 y = FLDSIZE_Y / 2;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000464
465 /* process raw key */
466 for (i = 0; i < dgst_raw_len; i++) {
467 int input;
468 /* each byte conveys four 2-bit move commands */
469 input = dgst_raw[i];
470 for (b = 0; b < 4; b++) {
471 /* evaluate 2 bit, rest is shifted later */
472 x += (input & 0x1) ? 1 : -1;
473 y += (input & 0x2) ? 1 : -1;
474
475 /* assure we are still in bounds */
476 x = MAX(x, 0);
477 y = MAX(y, 0);
478 x = MIN(x, FLDSIZE_X - 1);
479 y = MIN(y, FLDSIZE_Y - 1);
480
481 /* augment the field */
Damien Millerc6aadd92008-11-03 19:16:20 +1100482 if (field[x][y] < len - 2)
483 field[x][y]++;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000484 input = input >> 2;
485 }
486 }
Darren Tucker4b3b9772008-06-13 04:55:10 +1000487
488 /* mark starting point and end point*/
489 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
490 field[x][y] = len;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000491
492 /* fill in retval */
Damien Miller007132a2008-06-29 22:45:37 +1000493 snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k));
Darren Tucker987ac842008-06-13 04:54:40 +1000494 p = strchr(retval, '\0');
Darren Tucker9c16ac92008-06-13 04:40:35 +1000495
496 /* output upper border */
Damien Miller007132a2008-06-29 22:45:37 +1000497 for (i = p - retval - 1; i < FLDSIZE_X; i++)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000498 *p++ = '-';
499 *p++ = '+';
500 *p++ = '\n';
501
502 /* output content */
503 for (y = 0; y < FLDSIZE_Y; y++) {
504 *p++ = '|';
505 for (x = 0; x < FLDSIZE_X; x++)
Darren Tuckerd32b28a2008-06-13 04:45:50 +1000506 *p++ = augmentation_string[MIN(field[x][y], len)];
Darren Tucker9c16ac92008-06-13 04:40:35 +1000507 *p++ = '|';
508 *p++ = '\n';
509 }
510
511 /* output lower border */
512 *p++ = '+';
513 for (i = 0; i < FLDSIZE_X; i++)
514 *p++ = '-';
515 *p++ = '+';
516
517 return retval;
518}
519
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000520char *
Damien Miller0a80ca12010-02-27 07:55:05 +1100521key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000522{
Ben Lindstroma3700052001-04-05 23:26:32 +0000523 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000524 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100525 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100526
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000527 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
528 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000529 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000530 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000531 case SSH_FP_HEX:
532 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
533 break;
534 case SSH_FP_BUBBLEBABBLE:
535 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
536 break;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000537 case SSH_FP_RANDOMART:
Darren Tucker987ac842008-06-13 04:54:40 +1000538 retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000539 break;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000540 default:
Damien Miller2f54ada2008-11-03 19:24:16 +1100541 fatal("key_fingerprint: bad digest representation %d",
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000542 dgst_rep);
543 break;
544 }
545 memset(dgst_raw, 0, dgst_raw_len);
546 xfree(dgst_raw);
547 return retval;
548}
549
Damien Miller450a7a12000-03-26 13:04:51 +1000550/*
551 * Reads a multiple-precision integer in decimal from the buffer, and advances
552 * the pointer. The integer must already be initialized. This function is
553 * permitted to modify the buffer. This leaves *cpp to point just beyond the
554 * last processed (and maybe modified) character. Note that this may modify
555 * the buffer containing the number.
556 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000557static int
Damien Miller450a7a12000-03-26 13:04:51 +1000558read_bignum(char **cpp, BIGNUM * value)
559{
560 char *cp = *cpp;
561 int old;
562
563 /* Skip any leading whitespace. */
564 for (; *cp == ' ' || *cp == '\t'; cp++)
565 ;
566
567 /* Check that it begins with a decimal digit. */
568 if (*cp < '0' || *cp > '9')
569 return 0;
570
571 /* Save starting position. */
572 *cpp = cp;
573
574 /* Move forward until all decimal digits skipped. */
575 for (; *cp >= '0' && *cp <= '9'; cp++)
576 ;
577
578 /* Save the old terminating character, and replace it by \0. */
579 old = *cp;
580 *cp = 0;
581
582 /* Parse the number. */
583 if (BN_dec2bn(&value, *cpp) == 0)
584 return 0;
585
586 /* Restore old terminating character. */
587 *cp = old;
588
589 /* Move beyond the number and return success. */
590 *cpp = cp;
591 return 1;
592}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000593
Ben Lindstrombba81212001-06-25 05:01:22 +0000594static int
Damien Miller450a7a12000-03-26 13:04:51 +1000595write_bignum(FILE *f, BIGNUM *num)
596{
597 char *buf = BN_bn2dec(num);
598 if (buf == NULL) {
599 error("write_bignum: BN_bn2dec() failed");
600 return 0;
601 }
602 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000603 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000604 return 1;
605}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100606
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000607/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100608int
Damien Millereba71ba2000-04-29 23:57:08 +1000609key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000610{
Damien Millereba71ba2000-04-29 23:57:08 +1000611 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100612 int success = -1;
613 char *cp, *space;
614 int len, n, type;
615 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000616 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000617
618 cp = *cpp;
619
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000620 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100621 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000622 /* Get number of bits. */
623 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100624 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000625 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
626 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000627 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100628 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000629 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000630 /* Get public exponent, public modulus. */
631 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100632 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000633 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100634 return -1;
Darren Tucker561724f2010-01-13 22:43:05 +1100635 /* validate the claimed number of bits */
636 if ((u_int)BN_num_bits(ret->rsa->n) != bits) {
637 verbose("key_read: claimed key size %d does not match "
638 "actual %d", bits, BN_num_bits(ret->rsa->n));
639 return -1;
640 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100641 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000642 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100643 case KEY_UNSPEC:
644 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000645 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000646 case KEY_DSA_CERT_V00:
647 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100648 case KEY_DSA_CERT:
649 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100650 space = strchr(cp, ' ');
651 if (space == NULL) {
Damien Miller386f1f32003-02-24 11:54:57 +1100652 debug3("key_read: missing whitespace");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100653 return -1;
654 }
655 *space = '\0';
656 type = key_type_from_name(cp);
657 *space = ' ';
658 if (type == KEY_UNSPEC) {
Damien Miller386f1f32003-02-24 11:54:57 +1100659 debug3("key_read: missing keytype");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100660 return -1;
661 }
662 cp = space+1;
663 if (*cp == '\0') {
664 debug3("key_read: short string");
665 return -1;
666 }
667 if (ret->type == KEY_UNSPEC) {
668 ret->type = type;
669 } else if (ret->type != type) {
670 /* is a key, but different type */
671 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000672 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100673 }
Damien Millereba71ba2000-04-29 23:57:08 +1000674 len = 2*strlen(cp);
675 blob = xmalloc(len);
676 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000677 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000678 error("key_read: uudecode %s failed", cp);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000679 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100680 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000681 }
Darren Tucker502d3842003-06-28 12:38:01 +1000682 k = key_from_blob(blob, (u_int)n);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000683 xfree(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000684 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100685 error("key_read: key_from_blob %s failed", cp);
686 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000687 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100688 if (k->type != type) {
689 error("key_read: type mismatch: encoding error");
690 key_free(k);
691 return -1;
692 }
693/*XXXX*/
Damien Miller0a80ca12010-02-27 07:55:05 +1100694 if (key_is_cert(ret)) {
695 if (!key_is_cert(k)) {
696 error("key_read: loaded key is not a cert");
697 key_free(k);
698 return -1;
699 }
700 if (ret->cert != NULL)
701 cert_free(ret->cert);
702 ret->cert = k->cert;
703 k->cert = NULL;
704 }
705 if (key_type_plain(ret->type) == KEY_RSA) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100706 if (ret->rsa != NULL)
707 RSA_free(ret->rsa);
708 ret->rsa = k->rsa;
709 k->rsa = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100710#ifdef DEBUG_PK
711 RSA_print_fp(stderr, ret->rsa, 8);
712#endif
Damien Miller0a80ca12010-02-27 07:55:05 +1100713 }
714 if (key_type_plain(ret->type) == KEY_DSA) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100715 if (ret->dsa != NULL)
716 DSA_free(ret->dsa);
717 ret->dsa = k->dsa;
718 k->dsa = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100719#ifdef DEBUG_PK
720 DSA_print_fp(stderr, ret->dsa, 8);
721#endif
722 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100723 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100724/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000725 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100726 if (success != 1)
727 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000728 /* advance cp: skip whitespace and data */
729 while (*cp == ' ' || *cp == '\t')
730 cp++;
731 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
732 cp++;
733 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000734 break;
735 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000736 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000737 break;
738 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000740}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000741
Damien Miller450a7a12000-03-26 13:04:51 +1000742int
Damien Millerf58b58c2003-11-17 21:18:23 +1100743key_write(const Key *key, FILE *f)
Damien Miller450a7a12000-03-26 13:04:51 +1000744{
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000745 int n, success = 0;
746 u_int len, bits = 0;
Damien Millera10f5612002-09-12 09:49:15 +1000747 u_char *blob;
748 char *uu;
Damien Miller450a7a12000-03-26 13:04:51 +1000749
Damien Miller0a80ca12010-02-27 07:55:05 +1100750 if (key_is_cert(key)) {
751 if (key->cert == NULL) {
752 error("%s: no cert data", __func__);
753 return 0;
754 }
755 if (buffer_len(&key->cert->certblob) == 0) {
756 error("%s: no signed certificate blob", __func__);
757 return 0;
758 }
759 }
760
761 switch (key->type) {
762 case KEY_RSA1:
763 if (key->rsa == NULL)
764 return 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000765 /* size of modulus 'n' */
766 bits = BN_num_bits(key->rsa->n);
767 fprintf(f, "%u", bits);
768 if (write_bignum(f, key->rsa->e) &&
Damien Miller0a80ca12010-02-27 07:55:05 +1100769 write_bignum(f, key->rsa->n))
770 return 1;
771 error("key_write: failed for RSA key");
772 return 0;
773 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000774 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100775 case KEY_DSA_CERT:
776 if (key->dsa == NULL)
777 return 0;
778 break;
779 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000780 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100781 case KEY_RSA_CERT:
782 if (key->rsa == NULL)
783 return 0;
784 break;
785 default:
786 return 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000787 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100788
789 key_to_blob(key, &blob, &len);
790 uu = xmalloc(2*len);
791 n = uuencode(blob, len, uu, 2*len);
792 if (n > 0) {
793 fprintf(f, "%s %s", key_ssh_name(key), uu);
794 success = 1;
795 }
796 xfree(blob);
797 xfree(uu);
798
Damien Miller450a7a12000-03-26 13:04:51 +1000799 return success;
800}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000801
Damien Millerf58b58c2003-11-17 21:18:23 +1100802const char *
803key_type(const Key *k)
Damien Millere247cc42000-05-07 12:03:14 +1000804{
805 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100806 case KEY_RSA1:
807 return "RSA1";
Damien Millere247cc42000-05-07 12:03:14 +1000808 case KEY_RSA:
809 return "RSA";
Damien Millere247cc42000-05-07 12:03:14 +1000810 case KEY_DSA:
811 return "DSA";
Damien Miller4e270b02010-04-16 15:56:21 +1000812 case KEY_RSA_CERT_V00:
813 return "RSA-CERT-V00";
814 case KEY_DSA_CERT_V00:
815 return "DSA-CERT-V00";
Damien Miller0a80ca12010-02-27 07:55:05 +1100816 case KEY_RSA_CERT:
817 return "RSA-CERT";
818 case KEY_DSA_CERT:
819 return "DSA-CERT";
Damien Millere247cc42000-05-07 12:03:14 +1000820 }
821 return "unknown";
822}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000823
Damien Millerf58b58c2003-11-17 21:18:23 +1100824const char *
Damien Miller1cfbfaf2010-03-22 05:58:24 +1100825key_cert_type(const Key *k)
826{
827 switch (k->cert->type) {
828 case SSH2_CERT_TYPE_USER:
829 return "user";
830 case SSH2_CERT_TYPE_HOST:
831 return "host";
832 default:
833 return "unknown";
834 }
835}
836
837const char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100838key_ssh_name(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100839{
840 switch (k->type) {
841 case KEY_RSA:
842 return "ssh-rsa";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100843 case KEY_DSA:
844 return "ssh-dss";
Damien Miller4e270b02010-04-16 15:56:21 +1000845 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100846 return "ssh-rsa-cert-v00@openssh.com";
Damien Miller4e270b02010-04-16 15:56:21 +1000847 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100848 return "ssh-dss-cert-v00@openssh.com";
Damien Miller4e270b02010-04-16 15:56:21 +1000849 case KEY_RSA_CERT:
850 return "ssh-rsa-cert-v01@openssh.com";
851 case KEY_DSA_CERT:
852 return "ssh-dss-cert-v01@openssh.com";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100853 }
854 return "ssh-unknown";
855}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000856
Damien Miller0bc1bd82000-11-13 22:57:25 +1100857u_int
Damien Millerf58b58c2003-11-17 21:18:23 +1100858key_size(const Key *k)
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000859{
Damien Millerad833b32000-08-23 10:46:23 +1000860 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100861 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000862 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000863 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100864 case KEY_RSA_CERT:
Damien Millerad833b32000-08-23 10:46:23 +1000865 return BN_num_bits(k->rsa->n);
Damien Millerad833b32000-08-23 10:46:23 +1000866 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000867 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100868 case KEY_DSA_CERT:
Damien Millerad833b32000-08-23 10:46:23 +1000869 return BN_num_bits(k->dsa->p);
Damien Millerad833b32000-08-23 10:46:23 +1000870 }
871 return 0;
872}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100873
Ben Lindstrombba81212001-06-25 05:01:22 +0000874static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000875rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100876{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000877 RSA *private;
Damien Miller69b72032006-03-26 14:02:35 +1100878
Darren Tucker57e0d012010-01-08 18:52:27 +1100879 private = RSA_generate_key(bits, RSA_F4, NULL, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000880 if (private == NULL)
881 fatal("rsa_generate_private_key: key generation failed.");
882 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100883}
884
Ben Lindstrombba81212001-06-25 05:01:22 +0000885static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000886dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100887{
888 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
Damien Miller69b72032006-03-26 14:02:35 +1100889
Damien Miller0bc1bd82000-11-13 22:57:25 +1100890 if (private == NULL)
891 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
892 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000893 fatal("dsa_generate_private_key: DSA_generate_key failed.");
894 if (private == NULL)
895 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100896 return private;
897}
898
899Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000900key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100901{
902 Key *k = key_new(KEY_UNSPEC);
903 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000904 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100905 k->dsa = dsa_generate_private_key(bits);
906 break;
907 case KEY_RSA:
908 case KEY_RSA1:
909 k->rsa = rsa_generate_private_key(bits);
910 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000911 case KEY_RSA_CERT_V00:
912 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100913 case KEY_RSA_CERT:
914 case KEY_DSA_CERT:
915 fatal("key_generate: cert keys cannot be generated directly");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100916 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000917 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100918 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000919 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100920 return k;
921}
922
Damien Miller0a80ca12010-02-27 07:55:05 +1100923void
924key_cert_copy(const Key *from_key, struct Key *to_key)
925{
926 u_int i;
927 const struct KeyCert *from;
928 struct KeyCert *to;
929
930 if (to_key->cert != NULL) {
931 cert_free(to_key->cert);
932 to_key->cert = NULL;
933 }
934
935 if ((from = from_key->cert) == NULL)
936 return;
937
938 to = to_key->cert = cert_new();
939
940 buffer_append(&to->certblob, buffer_ptr(&from->certblob),
941 buffer_len(&from->certblob));
942
Damien Miller4e270b02010-04-16 15:56:21 +1000943 buffer_append(&to->critical,
944 buffer_ptr(&from->critical), buffer_len(&from->critical));
945 buffer_append(&to->extensions,
946 buffer_ptr(&from->extensions), buffer_len(&from->extensions));
Damien Miller0a80ca12010-02-27 07:55:05 +1100947
Damien Miller4e270b02010-04-16 15:56:21 +1000948 to->serial = from->serial;
Damien Miller0a80ca12010-02-27 07:55:05 +1100949 to->type = from->type;
950 to->key_id = from->key_id == NULL ? NULL : xstrdup(from->key_id);
951 to->valid_after = from->valid_after;
952 to->valid_before = from->valid_before;
953 to->signature_key = from->signature_key == NULL ?
954 NULL : key_from_private(from->signature_key);
955
956 to->nprincipals = from->nprincipals;
957 if (to->nprincipals > CERT_MAX_PRINCIPALS)
958 fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)",
959 __func__, to->nprincipals, CERT_MAX_PRINCIPALS);
960 if (to->nprincipals > 0) {
961 to->principals = xcalloc(from->nprincipals,
962 sizeof(*to->principals));
963 for (i = 0; i < to->nprincipals; i++)
964 to->principals[i] = xstrdup(from->principals[i]);
965 }
966}
967
Damien Miller0bc1bd82000-11-13 22:57:25 +1100968Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100969key_from_private(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100970{
971 Key *n = NULL;
972 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000973 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000974 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100975 case KEY_DSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100976 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +1100977 if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
978 (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
979 (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
980 (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
981 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100982 break;
983 case KEY_RSA:
984 case KEY_RSA1:
Damien Miller4e270b02010-04-16 15:56:21 +1000985 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100986 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100987 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +1100988 if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
989 (BN_copy(n->rsa->e, k->rsa->e) == NULL))
990 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100991 break;
992 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000993 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100994 break;
995 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100996 if (key_is_cert(k))
997 key_cert_copy(k, n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100998 return n;
999}
1000
1001int
1002key_type_from_name(char *name)
1003{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001004 if (strcmp(name, "rsa1") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001005 return KEY_RSA1;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001006 } else if (strcmp(name, "rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001007 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001008 } else if (strcmp(name, "dsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001009 return KEY_DSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001010 } else if (strcmp(name, "ssh-rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001011 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001012 } else if (strcmp(name, "ssh-dss") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001013 return KEY_DSA;
Damien Miller0a80ca12010-02-27 07:55:05 +11001014 } else if (strcmp(name, "ssh-rsa-cert-v00@openssh.com") == 0) {
Damien Miller4e270b02010-04-16 15:56:21 +10001015 return KEY_RSA_CERT_V00;
Damien Miller0a80ca12010-02-27 07:55:05 +11001016 } else if (strcmp(name, "ssh-dss-cert-v00@openssh.com") == 0) {
Damien Miller4e270b02010-04-16 15:56:21 +10001017 return KEY_DSA_CERT_V00;
1018 } else if (strcmp(name, "ssh-rsa-cert-v01@openssh.com") == 0) {
1019 return KEY_RSA_CERT;
1020 } else if (strcmp(name, "ssh-dss-cert-v01@openssh.com") == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001021 return KEY_DSA_CERT;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001022 }
Ben Lindstromb54873a2001-03-11 20:01:55 +00001023 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001024 return KEY_UNSPEC;
1025}
1026
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001027int
1028key_names_valid2(const char *names)
1029{
1030 char *s, *cp, *p;
1031
1032 if (names == NULL || strcmp(names, "") == 0)
1033 return 0;
1034 s = cp = xstrdup(names);
1035 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11001036 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001037 switch (key_type_from_name(p)) {
1038 case KEY_RSA1:
1039 case KEY_UNSPEC:
1040 xfree(s);
1041 return 0;
1042 }
1043 }
1044 debug3("key names ok: [%s]", names);
1045 xfree(s);
1046 return 1;
1047}
1048
Damien Miller0a80ca12010-02-27 07:55:05 +11001049static int
1050cert_parse(Buffer *b, Key *key, const u_char *blob, u_int blen)
1051{
Damien Miller4e270b02010-04-16 15:56:21 +10001052 u_char *principals, *critical, *exts, *sig_key, *sig;
1053 u_int signed_len, plen, clen, sklen, slen, kidlen, elen;
Damien Miller0a80ca12010-02-27 07:55:05 +11001054 Buffer tmp;
1055 char *principal;
1056 int ret = -1;
Damien Miller4e270b02010-04-16 15:56:21 +10001057 int v00 = key->type == KEY_DSA_CERT_V00 ||
1058 key->type == KEY_RSA_CERT_V00;
Damien Miller0a80ca12010-02-27 07:55:05 +11001059
1060 buffer_init(&tmp);
1061
1062 /* Copy the entire key blob for verification and later serialisation */
1063 buffer_append(&key->cert->certblob, blob, blen);
1064
Damien Miller4e270b02010-04-16 15:56:21 +10001065 elen = 0; /* Not touched for v00 certs */
1066 principals = exts = critical = sig_key = sig = NULL;
1067 if ((!v00 && buffer_get_int64_ret(&key->cert->serial, b) != 0) ||
1068 buffer_get_int_ret(&key->cert->type, b) != 0 ||
Damien Miller41396572010-03-04 21:51:11 +11001069 (key->cert->key_id = buffer_get_string_ret(b, &kidlen)) == NULL ||
Damien Miller0a80ca12010-02-27 07:55:05 +11001070 (principals = buffer_get_string_ret(b, &plen)) == NULL ||
1071 buffer_get_int64_ret(&key->cert->valid_after, b) != 0 ||
1072 buffer_get_int64_ret(&key->cert->valid_before, b) != 0 ||
Damien Miller4e270b02010-04-16 15:56:21 +10001073 (critical = buffer_get_string_ret(b, &clen)) == NULL ||
1074 (!v00 && (exts = buffer_get_string_ret(b, &elen)) == NULL) ||
1075 (v00 && buffer_get_string_ptr_ret(b, NULL) == NULL) || /* nonce */
1076 buffer_get_string_ptr_ret(b, NULL) == NULL || /* reserved */
Damien Miller0a80ca12010-02-27 07:55:05 +11001077 (sig_key = buffer_get_string_ret(b, &sklen)) == NULL) {
1078 error("%s: parse error", __func__);
1079 goto out;
1080 }
1081
Damien Miller41396572010-03-04 21:51:11 +11001082 if (kidlen != strlen(key->cert->key_id)) {
1083 error("%s: key ID contains \\0 character", __func__);
1084 goto out;
1085 }
1086
Damien Miller0a80ca12010-02-27 07:55:05 +11001087 /* Signature is left in the buffer so we can calculate this length */
1088 signed_len = buffer_len(&key->cert->certblob) - buffer_len(b);
1089
1090 if ((sig = buffer_get_string_ret(b, &slen)) == NULL) {
1091 error("%s: parse error", __func__);
1092 goto out;
1093 }
1094
1095 if (key->cert->type != SSH2_CERT_TYPE_USER &&
1096 key->cert->type != SSH2_CERT_TYPE_HOST) {
1097 error("Unknown certificate type %u", key->cert->type);
1098 goto out;
1099 }
1100
1101 buffer_append(&tmp, principals, plen);
1102 while (buffer_len(&tmp) > 0) {
1103 if (key->cert->nprincipals >= CERT_MAX_PRINCIPALS) {
Damien Miller41396572010-03-04 21:51:11 +11001104 error("%s: Too many principals", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001105 goto out;
1106 }
Damien Miller41396572010-03-04 21:51:11 +11001107 if ((principal = buffer_get_string_ret(&tmp, &plen)) == NULL) {
1108 error("%s: Principals data invalid", __func__);
1109 goto out;
1110 }
1111 if (strlen(principal) != plen) {
1112 error("%s: Principal contains \\0 character",
1113 __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001114 goto out;
1115 }
1116 key->cert->principals = xrealloc(key->cert->principals,
1117 key->cert->nprincipals + 1, sizeof(*key->cert->principals));
1118 key->cert->principals[key->cert->nprincipals++] = principal;
1119 }
1120
1121 buffer_clear(&tmp);
1122
Damien Miller4e270b02010-04-16 15:56:21 +10001123 buffer_append(&key->cert->critical, critical, clen);
1124 buffer_append(&tmp, critical, clen);
Damien Miller0a80ca12010-02-27 07:55:05 +11001125 /* validate structure */
1126 while (buffer_len(&tmp) != 0) {
Damien Miller2befbad2010-03-04 21:52:18 +11001127 if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL ||
1128 buffer_get_string_ptr_ret(&tmp, NULL) == NULL) {
Damien Miller4e270b02010-04-16 15:56:21 +10001129 error("%s: critical option data invalid", __func__);
1130 goto out;
1131 }
1132 }
1133 buffer_clear(&tmp);
1134
1135 buffer_append(&key->cert->extensions, exts, elen);
1136 buffer_append(&tmp, exts, elen);
1137 /* validate structure */
1138 while (buffer_len(&tmp) != 0) {
1139 if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL ||
1140 buffer_get_string_ptr_ret(&tmp, NULL) == NULL) {
1141 error("%s: extension data invalid", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001142 goto out;
1143 }
1144 }
1145 buffer_clear(&tmp);
1146
1147 if ((key->cert->signature_key = key_from_blob(sig_key,
1148 sklen)) == NULL) {
Damien Miller41396572010-03-04 21:51:11 +11001149 error("%s: Signature key invalid", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001150 goto out;
1151 }
1152 if (key->cert->signature_key->type != KEY_RSA &&
1153 key->cert->signature_key->type != KEY_DSA) {
Damien Miller41396572010-03-04 21:51:11 +11001154 error("%s: Invalid signature key type %s (%d)", __func__,
Damien Miller0a80ca12010-02-27 07:55:05 +11001155 key_type(key->cert->signature_key),
1156 key->cert->signature_key->type);
1157 goto out;
1158 }
1159
1160 switch (key_verify(key->cert->signature_key, sig, slen,
1161 buffer_ptr(&key->cert->certblob), signed_len)) {
1162 case 1:
Damien Miller41396572010-03-04 21:51:11 +11001163 ret = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001164 break; /* Good signature */
1165 case 0:
Damien Miller41396572010-03-04 21:51:11 +11001166 error("%s: Invalid signature on certificate", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001167 goto out;
1168 case -1:
Damien Miller41396572010-03-04 21:51:11 +11001169 error("%s: Certificate signature verification failed",
1170 __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001171 goto out;
1172 }
1173
Damien Miller0a80ca12010-02-27 07:55:05 +11001174 out:
1175 buffer_free(&tmp);
1176 if (principals != NULL)
1177 xfree(principals);
Damien Miller4e270b02010-04-16 15:56:21 +10001178 if (critical != NULL)
1179 xfree(critical);
1180 if (exts != NULL)
1181 xfree(exts);
Damien Miller0a80ca12010-02-27 07:55:05 +11001182 if (sig_key != NULL)
1183 xfree(sig_key);
1184 if (sig != NULL)
1185 xfree(sig);
1186 return ret;
1187}
1188
Damien Miller0bc1bd82000-11-13 22:57:25 +11001189Key *
Damien Millerf58b58c2003-11-17 21:18:23 +11001190key_from_blob(const u_char *blob, u_int blen)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001191{
1192 Buffer b;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001193 int rlen, type;
Darren Tucker08d04fa2004-11-05 20:42:28 +11001194 char *ktype = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001195 Key *key = NULL;
1196
1197#ifdef DEBUG_PK
1198 dump_base64(stderr, blob, blen);
1199#endif
1200 buffer_init(&b);
1201 buffer_append(&b, blob, blen);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001202 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
1203 error("key_from_blob: can't read key type");
1204 goto out;
1205 }
1206
Damien Miller0bc1bd82000-11-13 22:57:25 +11001207 type = key_type_from_name(ktype);
1208
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001209 switch (type) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001210 case KEY_RSA_CERT:
Damien Miller4e270b02010-04-16 15:56:21 +10001211 (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1212 /* FALLTHROUGH */
1213 case KEY_RSA:
1214 case KEY_RSA_CERT_V00:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001215 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001216 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
1217 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
1218 error("key_from_blob: can't read rsa key");
Damien Miller0a80ca12010-02-27 07:55:05 +11001219 badkey:
Darren Tucker08d04fa2004-11-05 20:42:28 +11001220 key_free(key);
1221 key = NULL;
1222 goto out;
1223 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001224#ifdef DEBUG_PK
1225 RSA_print_fp(stderr, key->rsa, 8);
1226#endif
1227 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001228 case KEY_DSA_CERT:
Damien Miller4e270b02010-04-16 15:56:21 +10001229 (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1230 /* FALLTHROUGH */
1231 case KEY_DSA:
1232 case KEY_DSA_CERT_V00:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001233 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001234 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
1235 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
1236 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
1237 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
1238 error("key_from_blob: can't read dsa key");
Damien Miller0a80ca12010-02-27 07:55:05 +11001239 goto badkey;
Darren Tucker08d04fa2004-11-05 20:42:28 +11001240 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001241#ifdef DEBUG_PK
1242 DSA_print_fp(stderr, key->dsa, 8);
1243#endif
1244 break;
1245 case KEY_UNSPEC:
1246 key = key_new(type);
1247 break;
1248 default:
1249 error("key_from_blob: cannot handle type %s", ktype);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001250 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001251 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001252 if (key_is_cert(key) && cert_parse(&b, key, blob, blen) == -1) {
1253 error("key_from_blob: can't parse cert data");
1254 goto badkey;
1255 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001256 rlen = buffer_len(&b);
1257 if (key != NULL && rlen != 0)
1258 error("key_from_blob: remaining bytes in key blob %d", rlen);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001259 out:
1260 if (ktype != NULL)
1261 xfree(ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001262 buffer_free(&b);
1263 return key;
1264}
1265
1266int
Damien Millerf58b58c2003-11-17 21:18:23 +11001267key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001268{
1269 Buffer b;
1270 int len;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001271
1272 if (key == NULL) {
1273 error("key_to_blob: key == NULL");
1274 return 0;
1275 }
1276 buffer_init(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001277 switch (key->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001278 case KEY_DSA_CERT_V00:
1279 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001280 case KEY_DSA_CERT:
1281 case KEY_RSA_CERT:
1282 /* Use the existing blob */
1283 buffer_append(&b, buffer_ptr(&key->cert->certblob),
1284 buffer_len(&key->cert->certblob));
1285 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001286 case KEY_DSA:
1287 buffer_put_cstring(&b, key_ssh_name(key));
1288 buffer_put_bignum2(&b, key->dsa->p);
1289 buffer_put_bignum2(&b, key->dsa->q);
1290 buffer_put_bignum2(&b, key->dsa->g);
1291 buffer_put_bignum2(&b, key->dsa->pub_key);
1292 break;
1293 case KEY_RSA:
1294 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001295 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001296 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001297 break;
1298 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +00001299 error("key_to_blob: unsupported key type %d", key->type);
1300 buffer_free(&b);
1301 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001302 }
1303 len = buffer_len(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001304 if (lenp != NULL)
1305 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +00001306 if (blobp != NULL) {
1307 *blobp = xmalloc(len);
1308 memcpy(*blobp, buffer_ptr(&b), len);
1309 }
1310 memset(buffer_ptr(&b), 0, len);
1311 buffer_free(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001312 return len;
1313}
1314
1315int
1316key_sign(
Damien Millerf58b58c2003-11-17 21:18:23 +11001317 const Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001318 u_char **sigp, u_int *lenp,
Damien Millerf58b58c2003-11-17 21:18:23 +11001319 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001320{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001321 switch (key->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001322 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001323 case KEY_DSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001324 case KEY_DSA:
1325 return ssh_dss_sign(key, sigp, lenp, data, datalen);
Damien Miller4e270b02010-04-16 15:56:21 +10001326 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001327 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001328 case KEY_RSA:
1329 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001330 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +10001331 error("key_sign: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001332 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001333 }
1334}
1335
Ben Lindstrom01fff0c2002-06-06 20:54:07 +00001336/*
1337 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
1338 * and -1 on error.
1339 */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001340int
1341key_verify(
Damien Millerf58b58c2003-11-17 21:18:23 +11001342 const Key *key,
1343 const u_char *signature, u_int signaturelen,
1344 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001345{
Ben Lindstrom5363aee2001-06-25 04:42:20 +00001346 if (signaturelen == 0)
1347 return -1;
1348
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001349 switch (key->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001350 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001351 case KEY_DSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001352 case KEY_DSA:
1353 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
Damien Miller4e270b02010-04-16 15:56:21 +10001354 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001355 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001356 case KEY_RSA:
1357 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001358 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +10001359 error("key_verify: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001360 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001361 }
1362}
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001363
1364/* Converts a private to a public key */
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001365Key *
Damien Millerf58b58c2003-11-17 21:18:23 +11001366key_demote(const Key *k)
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001367{
1368 Key *pk;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001369
Damien Miller07d86be2006-03-26 14:19:21 +11001370 pk = xcalloc(1, sizeof(*pk));
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001371 pk->type = k->type;
1372 pk->flags = k->flags;
1373 pk->dsa = NULL;
1374 pk->rsa = NULL;
1375
1376 switch (k->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001377 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001378 case KEY_RSA_CERT:
1379 key_cert_copy(k, pk);
1380 /* FALLTHROUGH */
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001381 case KEY_RSA1:
1382 case KEY_RSA:
1383 if ((pk->rsa = RSA_new()) == NULL)
1384 fatal("key_demote: RSA_new failed");
1385 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
1386 fatal("key_demote: BN_dup failed");
1387 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
1388 fatal("key_demote: BN_dup failed");
1389 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001390 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001391 case KEY_DSA_CERT:
1392 key_cert_copy(k, pk);
1393 /* FALLTHROUGH */
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001394 case KEY_DSA:
1395 if ((pk->dsa = DSA_new()) == NULL)
1396 fatal("key_demote: DSA_new failed");
1397 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
1398 fatal("key_demote: BN_dup failed");
1399 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
1400 fatal("key_demote: BN_dup failed");
1401 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
1402 fatal("key_demote: BN_dup failed");
1403 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
1404 fatal("key_demote: BN_dup failed");
1405 break;
1406 default:
1407 fatal("key_free: bad key type %d", k->type);
1408 break;
1409 }
1410
1411 return (pk);
1412}
Damien Miller0a80ca12010-02-27 07:55:05 +11001413
1414int
1415key_is_cert(const Key *k)
1416{
Damien Miller4e270b02010-04-16 15:56:21 +10001417 if (k == NULL)
1418 return 0;
1419 switch (k->type) {
1420 case KEY_RSA_CERT_V00:
1421 case KEY_DSA_CERT_V00:
1422 case KEY_RSA_CERT:
1423 case KEY_DSA_CERT:
1424 return 1;
1425 default:
1426 return 0;
1427 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001428}
1429
1430/* Return the cert-less equivalent to a certified key type */
1431int
1432key_type_plain(int type)
1433{
1434 switch (type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001435 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001436 case KEY_RSA_CERT:
1437 return KEY_RSA;
Damien Miller4e270b02010-04-16 15:56:21 +10001438 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001439 case KEY_DSA_CERT:
1440 return KEY_DSA;
1441 default:
1442 return type;
1443 }
1444}
1445
1446/* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */
1447int
Damien Miller4e270b02010-04-16 15:56:21 +10001448key_to_certified(Key *k, int legacy)
Damien Miller0a80ca12010-02-27 07:55:05 +11001449{
1450 switch (k->type) {
1451 case KEY_RSA:
1452 k->cert = cert_new();
Damien Miller4e270b02010-04-16 15:56:21 +10001453 k->type = legacy ? KEY_RSA_CERT_V00 : KEY_RSA_CERT;
Damien Miller0a80ca12010-02-27 07:55:05 +11001454 return 0;
1455 case KEY_DSA:
1456 k->cert = cert_new();
Damien Miller4e270b02010-04-16 15:56:21 +10001457 k->type = legacy ? KEY_DSA_CERT_V00 : KEY_DSA_CERT;
Damien Miller0a80ca12010-02-27 07:55:05 +11001458 return 0;
1459 default:
1460 error("%s: key has incorrect type %s", __func__, key_type(k));
1461 return -1;
1462 }
1463}
1464
1465/* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */
1466int
1467key_drop_cert(Key *k)
1468{
1469 switch (k->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001470 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001471 case KEY_RSA_CERT:
1472 cert_free(k->cert);
1473 k->type = KEY_RSA;
1474 return 0;
Damien Miller4e270b02010-04-16 15:56:21 +10001475 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001476 case KEY_DSA_CERT:
1477 cert_free(k->cert);
1478 k->type = KEY_DSA;
1479 return 0;
1480 default:
1481 error("%s: key has incorrect type %s", __func__, key_type(k));
1482 return -1;
1483 }
1484}
1485
1486/* Sign a KEY_RSA_CERT or KEY_DSA_CERT, (re-)generating the signed certblob */
1487int
1488key_certify(Key *k, Key *ca)
1489{
1490 Buffer principals;
1491 u_char *ca_blob, *sig_blob, nonce[32];
1492 u_int i, ca_len, sig_len;
1493
1494 if (k->cert == NULL) {
1495 error("%s: key lacks cert info", __func__);
1496 return -1;
1497 }
1498
1499 if (!key_is_cert(k)) {
1500 error("%s: certificate has unknown type %d", __func__,
1501 k->cert->type);
1502 return -1;
1503 }
1504
1505 if (ca->type != KEY_RSA && ca->type != KEY_DSA) {
1506 error("%s: CA key has unsupported type %s", __func__,
1507 key_type(ca));
1508 return -1;
1509 }
1510
1511 key_to_blob(ca, &ca_blob, &ca_len);
1512
1513 buffer_clear(&k->cert->certblob);
1514 buffer_put_cstring(&k->cert->certblob, key_ssh_name(k));
1515
Damien Miller4e270b02010-04-16 15:56:21 +10001516 /* -v01 certs put nonce first */
1517 if (k->type == KEY_DSA_CERT || k->type == KEY_RSA_CERT) {
1518 arc4random_buf(&nonce, sizeof(nonce));
1519 buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce));
1520 }
1521
Damien Miller0a80ca12010-02-27 07:55:05 +11001522 switch (k->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001523 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001524 case KEY_DSA_CERT:
1525 buffer_put_bignum2(&k->cert->certblob, k->dsa->p);
1526 buffer_put_bignum2(&k->cert->certblob, k->dsa->q);
1527 buffer_put_bignum2(&k->cert->certblob, k->dsa->g);
1528 buffer_put_bignum2(&k->cert->certblob, k->dsa->pub_key);
1529 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001530 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001531 case KEY_RSA_CERT:
1532 buffer_put_bignum2(&k->cert->certblob, k->rsa->e);
1533 buffer_put_bignum2(&k->cert->certblob, k->rsa->n);
1534 break;
1535 default:
1536 error("%s: key has incorrect type %s", __func__, key_type(k));
1537 buffer_clear(&k->cert->certblob);
1538 xfree(ca_blob);
1539 return -1;
1540 }
1541
Damien Miller4e270b02010-04-16 15:56:21 +10001542 /* -v01 certs have a serial number next */
1543 if (k->type == KEY_DSA_CERT || k->type == KEY_RSA_CERT)
1544 buffer_put_int64(&k->cert->certblob, k->cert->serial);
1545
Damien Miller0a80ca12010-02-27 07:55:05 +11001546 buffer_put_int(&k->cert->certblob, k->cert->type);
1547 buffer_put_cstring(&k->cert->certblob, k->cert->key_id);
1548
1549 buffer_init(&principals);
1550 for (i = 0; i < k->cert->nprincipals; i++)
1551 buffer_put_cstring(&principals, k->cert->principals[i]);
1552 buffer_put_string(&k->cert->certblob, buffer_ptr(&principals),
1553 buffer_len(&principals));
1554 buffer_free(&principals);
1555
1556 buffer_put_int64(&k->cert->certblob, k->cert->valid_after);
1557 buffer_put_int64(&k->cert->certblob, k->cert->valid_before);
1558 buffer_put_string(&k->cert->certblob,
Damien Miller4e270b02010-04-16 15:56:21 +10001559 buffer_ptr(&k->cert->critical), buffer_len(&k->cert->critical));
Damien Miller0a80ca12010-02-27 07:55:05 +11001560
Damien Miller4e270b02010-04-16 15:56:21 +10001561 /* -v01 certs have non-critical options here */
1562 if (k->type == KEY_DSA_CERT || k->type == KEY_RSA_CERT) {
1563 buffer_put_string(&k->cert->certblob,
1564 buffer_ptr(&k->cert->extensions),
1565 buffer_len(&k->cert->extensions));
1566 }
1567
1568 /* -v00 certs put the nonce at the end */
1569 if (k->type == KEY_DSA_CERT_V00 || k->type == KEY_RSA_CERT_V00)
1570 buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce));
1571
Damien Miller0a80ca12010-02-27 07:55:05 +11001572 buffer_put_string(&k->cert->certblob, NULL, 0); /* reserved */
1573 buffer_put_string(&k->cert->certblob, ca_blob, ca_len);
1574 xfree(ca_blob);
1575
1576 /* Sign the whole mess */
1577 if (key_sign(ca, &sig_blob, &sig_len, buffer_ptr(&k->cert->certblob),
1578 buffer_len(&k->cert->certblob)) != 0) {
1579 error("%s: signature operation failed", __func__);
1580 buffer_clear(&k->cert->certblob);
1581 return -1;
1582 }
1583 /* Append signature and we are done */
1584 buffer_put_string(&k->cert->certblob, sig_blob, sig_len);
1585 xfree(sig_blob);
1586
1587 return 0;
1588}
1589
1590int
1591key_cert_check_authority(const Key *k, int want_host, int require_principal,
1592 const char *name, const char **reason)
1593{
1594 u_int i, principal_matches;
1595 time_t now = time(NULL);
1596
1597 if (want_host) {
1598 if (k->cert->type != SSH2_CERT_TYPE_HOST) {
1599 *reason = "Certificate invalid: not a host certificate";
1600 return -1;
1601 }
1602 } else {
1603 if (k->cert->type != SSH2_CERT_TYPE_USER) {
1604 *reason = "Certificate invalid: not a user certificate";
1605 return -1;
1606 }
1607 }
1608 if (now < 0) {
1609 error("%s: system clock lies before epoch", __func__);
1610 *reason = "Certificate invalid: not yet valid";
1611 return -1;
1612 }
1613 if ((u_int64_t)now < k->cert->valid_after) {
1614 *reason = "Certificate invalid: not yet valid";
1615 return -1;
1616 }
1617 if ((u_int64_t)now >= k->cert->valid_before) {
1618 *reason = "Certificate invalid: expired";
1619 return -1;
1620 }
1621 if (k->cert->nprincipals == 0) {
1622 if (require_principal) {
1623 *reason = "Certificate lacks principal list";
1624 return -1;
1625 }
Damien Miller30da3442010-05-10 11:58:03 +10001626 } else if (name != NULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001627 principal_matches = 0;
1628 for (i = 0; i < k->cert->nprincipals; i++) {
1629 if (strcmp(name, k->cert->principals[i]) == 0) {
1630 principal_matches = 1;
1631 break;
1632 }
1633 }
1634 if (!principal_matches) {
1635 *reason = "Certificate invalid: name is not a listed "
1636 "principal";
1637 return -1;
1638 }
1639 }
1640 return 0;
1641}
Damien Miller4e270b02010-04-16 15:56:21 +10001642
1643int
1644key_cert_is_legacy(Key *k)
1645{
1646 switch (k->type) {
1647 case KEY_DSA_CERT_V00:
1648 case KEY_RSA_CERT_V00:
1649 return 1;
1650 default:
1651 return 0;
1652 }
1653}