blob: 0282e0823831f5f25ad4ddc4aed7d12cfb68eb3c [file] [log] [blame]
Damien Millerf0e90602013-12-07 10:40:26 +11001/* $OpenBSD: key.c,v 1.107 2013/12/06 13:30:08 markus 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 Miller8a0268f2010-07-16 13:57:51 +100055#include "misc.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110056#include "ssh2.h"
57
Damien Millerf3747bf2013-01-18 11:44:04 +110058static int to_blob(const Key *, u_char **, u_int *, int);
Damien Miller4a3a9d42013-10-30 22:19:47 +110059static Key *key_from_blob2(const u_char *, u_int, int);
Damien Millerf3747bf2013-01-18 11:44:04 +110060
Damien Miller0a80ca12010-02-27 07:55:05 +110061static struct KeyCert *
62cert_new(void)
63{
64 struct KeyCert *cert;
65
66 cert = xcalloc(1, sizeof(*cert));
67 buffer_init(&cert->certblob);
Damien Miller4e270b02010-04-16 15:56:21 +100068 buffer_init(&cert->critical);
69 buffer_init(&cert->extensions);
Damien Miller0a80ca12010-02-27 07:55:05 +110070 cert->key_id = NULL;
71 cert->principals = NULL;
72 cert->signature_key = NULL;
73 return cert;
74}
Damien Miller450a7a12000-03-26 13:04:51 +100075
76Key *
77key_new(int type)
78{
79 Key *k;
80 RSA *rsa;
81 DSA *dsa;
Damien Miller07d86be2006-03-26 14:19:21 +110082 k = xcalloc(1, sizeof(*k));
Damien Miller450a7a12000-03-26 13:04:51 +100083 k->type = type;
Damien Millereb8b60e2010-08-31 22:41:14 +100084 k->ecdsa = NULL;
85 k->ecdsa_nid = -1;
Damien Millereba71ba2000-04-29 23:57:08 +100086 k->dsa = NULL;
87 k->rsa = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +110088 k->cert = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100089 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110090 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100091 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +100092 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +110093 case KEY_RSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +110094 if ((rsa = RSA_new()) == NULL)
95 fatal("key_new: RSA_new failed");
96 if ((rsa->n = BN_new()) == NULL)
97 fatal("key_new: BN_new failed");
98 if ((rsa->e = BN_new()) == NULL)
99 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +1000100 k->rsa = rsa;
101 break;
102 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000103 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100104 case KEY_DSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +1100105 if ((dsa = DSA_new()) == NULL)
106 fatal("key_new: DSA_new failed");
107 if ((dsa->p = BN_new()) == NULL)
108 fatal("key_new: BN_new failed");
109 if ((dsa->q = BN_new()) == NULL)
110 fatal("key_new: BN_new failed");
111 if ((dsa->g = BN_new()) == NULL)
112 fatal("key_new: BN_new failed");
113 if ((dsa->pub_key = BN_new()) == NULL)
114 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +1000115 k->dsa = dsa;
116 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000117#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000118 case KEY_ECDSA:
119 case KEY_ECDSA_CERT:
120 /* Cannot do anything until we know the group */
121 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000122#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +1000124 break;
125 default:
126 fatal("key_new: bad key type %d", k->type);
127 break;
128 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100129
130 if (key_is_cert(k))
131 k->cert = cert_new();
132
Damien Miller450a7a12000-03-26 13:04:51 +1000133 return k;
134}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000135
Damien Miller0a80ca12010-02-27 07:55:05 +1100136void
137key_add_private(Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100138{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100139 switch (k->type) {
140 case KEY_RSA1:
141 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000142 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100143 case KEY_RSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +1100144 if ((k->rsa->d = BN_new()) == NULL)
145 fatal("key_new_private: BN_new failed");
146 if ((k->rsa->iqmp = BN_new()) == NULL)
147 fatal("key_new_private: BN_new failed");
148 if ((k->rsa->q = BN_new()) == NULL)
149 fatal("key_new_private: BN_new failed");
150 if ((k->rsa->p = BN_new()) == NULL)
151 fatal("key_new_private: BN_new failed");
152 if ((k->rsa->dmq1 = BN_new()) == NULL)
153 fatal("key_new_private: BN_new failed");
154 if ((k->rsa->dmp1 = BN_new()) == NULL)
155 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100156 break;
157 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000158 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100159 case KEY_DSA_CERT:
Damien Millerda755162002-01-22 23:09:22 +1100160 if ((k->dsa->priv_key = BN_new()) == NULL)
161 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100162 break;
Damien Millereb8b60e2010-08-31 22:41:14 +1000163 case KEY_ECDSA:
164 case KEY_ECDSA_CERT:
165 /* Cannot do anything until we know the group */
166 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100167 case KEY_UNSPEC:
168 break;
169 default:
170 break;
171 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100172}
173
174Key *
175key_new_private(int type)
176{
177 Key *k = key_new(type);
178
179 key_add_private(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100180 return k;
181}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000182
Damien Miller0a80ca12010-02-27 07:55:05 +1100183static void
184cert_free(struct KeyCert *cert)
185{
186 u_int i;
187
188 buffer_free(&cert->certblob);
Damien Miller4e270b02010-04-16 15:56:21 +1000189 buffer_free(&cert->critical);
190 buffer_free(&cert->extensions);
Darren Tuckera627d422013-06-02 07:31:17 +1000191 free(cert->key_id);
Damien Miller0a80ca12010-02-27 07:55:05 +1100192 for (i = 0; i < cert->nprincipals; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000193 free(cert->principals[i]);
194 free(cert->principals);
Damien Miller0a80ca12010-02-27 07:55:05 +1100195 if (cert->signature_key != NULL)
196 key_free(cert->signature_key);
Darren Tuckera627d422013-06-02 07:31:17 +1000197 free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100198}
199
Damien Miller450a7a12000-03-26 13:04:51 +1000200void
201key_free(Key *k)
202{
Damien Miller429fcc22006-03-26 14:02:16 +1100203 if (k == NULL)
Damien Millerbbaad772006-03-26 14:03:03 +1100204 fatal("key_free: key is NULL");
Damien Miller450a7a12000-03-26 13:04:51 +1000205 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100206 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000207 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000208 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100209 case KEY_RSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000210 if (k->rsa != NULL)
211 RSA_free(k->rsa);
212 k->rsa = NULL;
213 break;
214 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000215 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100216 case KEY_DSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000217 if (k->dsa != NULL)
218 DSA_free(k->dsa);
219 k->dsa = NULL;
220 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000221#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000222 case KEY_ECDSA:
223 case KEY_ECDSA_CERT:
224 if (k->ecdsa != NULL)
225 EC_KEY_free(k->ecdsa);
226 k->ecdsa = NULL;
227 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000228#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100229 case KEY_UNSPEC:
230 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000231 default:
232 fatal("key_free: bad key type %d", k->type);
233 break;
234 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100235 if (key_is_cert(k)) {
236 if (k->cert != NULL)
237 cert_free(k->cert);
238 k->cert = NULL;
239 }
240
Darren Tuckera627d422013-06-02 07:31:17 +1000241 free(k);
Damien Miller450a7a12000-03-26 13:04:51 +1000242}
Damien Millerf58b58c2003-11-17 21:18:23 +1100243
Damien Miller0a80ca12010-02-27 07:55:05 +1100244static int
245cert_compare(struct KeyCert *a, struct KeyCert *b)
Damien Miller450a7a12000-03-26 13:04:51 +1000246{
Damien Miller0a80ca12010-02-27 07:55:05 +1100247 if (a == NULL && b == NULL)
248 return 1;
249 if (a == NULL || b == NULL)
Damien Miller450a7a12000-03-26 13:04:51 +1000250 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100251 if (buffer_len(&a->certblob) != buffer_len(&b->certblob))
252 return 0;
Damien Millerea1651c2010-07-16 13:58:37 +1000253 if (timingsafe_bcmp(buffer_ptr(&a->certblob), buffer_ptr(&b->certblob),
Damien Miller0a80ca12010-02-27 07:55:05 +1100254 buffer_len(&a->certblob)) != 0)
255 return 0;
256 return 1;
257}
258
259/*
260 * Compare public portions of key only, allowing comparisons between
261 * certificates and plain keys too.
262 */
263int
264key_equal_public(const Key *a, const Key *b)
265{
Darren Tucker8ccb7392010-09-10 12:28:24 +1000266#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000267 BN_CTX *bnctx;
Darren Tucker8ccb7392010-09-10 12:28:24 +1000268#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000269
Damien Miller0a80ca12010-02-27 07:55:05 +1100270 if (a == NULL || b == NULL ||
271 key_type_plain(a->type) != key_type_plain(b->type))
272 return 0;
273
Damien Miller450a7a12000-03-26 13:04:51 +1000274 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100275 case KEY_RSA1:
Damien Miller4e270b02010-04-16 15:56:21 +1000276 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100277 case KEY_RSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000278 case KEY_RSA:
279 return a->rsa != NULL && b->rsa != NULL &&
280 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
281 BN_cmp(a->rsa->n, b->rsa->n) == 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000282 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100283 case KEY_DSA_CERT:
Damien Miller450a7a12000-03-26 13:04:51 +1000284 case KEY_DSA:
285 return a->dsa != NULL && b->dsa != NULL &&
286 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
287 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
288 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
289 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
Damien Miller6af914a2010-09-10 11:39:26 +1000290#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000291 case KEY_ECDSA_CERT:
292 case KEY_ECDSA:
293 if (a->ecdsa == NULL || b->ecdsa == NULL ||
294 EC_KEY_get0_public_key(a->ecdsa) == NULL ||
295 EC_KEY_get0_public_key(b->ecdsa) == NULL)
296 return 0;
297 if ((bnctx = BN_CTX_new()) == NULL)
298 fatal("%s: BN_CTX_new failed", __func__);
299 if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa),
300 EC_KEY_get0_group(b->ecdsa), bnctx) != 0 ||
301 EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa),
302 EC_KEY_get0_public_key(a->ecdsa),
303 EC_KEY_get0_public_key(b->ecdsa), bnctx) != 0) {
304 BN_CTX_free(bnctx);
305 return 0;
306 }
307 BN_CTX_free(bnctx);
308 return 1;
Damien Miller6af914a2010-09-10 11:39:26 +1000309#endif /* OPENSSL_HAS_ECC */
Damien Miller450a7a12000-03-26 13:04:51 +1000310 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000311 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000312 }
Damien Miller87dd5f22008-07-11 17:35:09 +1000313 /* NOTREACHED */
Damien Miller450a7a12000-03-26 13:04:51 +1000314}
315
Damien Miller0a80ca12010-02-27 07:55:05 +1100316int
317key_equal(const Key *a, const Key *b)
318{
319 if (a == NULL || b == NULL || a->type != b->type)
320 return 0;
321 if (key_is_cert(a)) {
322 if (!cert_compare(a->cert, b->cert))
323 return 0;
324 }
325 return key_equal_public(a, b);
326}
327
Damien Miller37876e92003-05-15 10:19:46 +1000328u_char*
Damien Millerf3747bf2013-01-18 11:44:04 +1100329key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
330 u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000331{
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000332 const EVP_MD *md = NULL;
Ben Lindstromf0b48532001-03-12 02:59:31 +0000333 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000334 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000335 u_char *retval = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000336 u_int len = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100337 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000338
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000339 *dgst_raw_length = 0;
340
Ben Lindstromf0b48532001-03-12 02:59:31 +0000341 switch (dgst_type) {
342 case SSH_FP_MD5:
343 md = EVP_md5();
344 break;
345 case SSH_FP_SHA1:
346 md = EVP_sha1();
347 break;
Darren Tucker14a9d252012-06-30 20:05:02 +1000348#ifdef HAVE_EVP_SHA256
Damien Miller3bde12a2012-06-20 21:51:11 +1000349 case SSH_FP_SHA256:
350 md = EVP_sha256();
351 break;
Darren Tucker14a9d252012-06-30 20:05:02 +1000352#endif
Ben Lindstromf0b48532001-03-12 02:59:31 +0000353 default:
354 fatal("key_fingerprint_raw: bad digest type %d",
355 dgst_type);
356 }
Damien Miller450a7a12000-03-26 13:04:51 +1000357 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100358 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000359 nlen = BN_num_bytes(k->rsa->n);
360 elen = BN_num_bytes(k->rsa->e);
361 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000362 blob = xmalloc(len);
363 BN_bn2bin(k->rsa->n, blob);
364 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000365 break;
366 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000367 case KEY_ECDSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100368 case KEY_RSA:
369 key_to_blob(k, &blob, &len);
370 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000371 case KEY_DSA_CERT_V00:
372 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100373 case KEY_DSA_CERT:
Damien Millereb8b60e2010-08-31 22:41:14 +1000374 case KEY_ECDSA_CERT:
Damien Miller0a80ca12010-02-27 07:55:05 +1100375 case KEY_RSA_CERT:
376 /* We want a fingerprint of the _key_ not of the cert */
Damien Millerf3747bf2013-01-18 11:44:04 +1100377 to_blob(k, &blob, &len, 1);
Damien Miller0a80ca12010-02-27 07:55:05 +1100378 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100379 case KEY_UNSPEC:
380 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000381 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000382 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000383 break;
384 }
Damien Millereba71ba2000-04-29 23:57:08 +1000385 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000386 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000387 EVP_DigestInit(&ctx, md);
388 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100389 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000390 memset(blob, 0, len);
Darren Tuckera627d422013-06-02 07:31:17 +1000391 free(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000392 } else {
393 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000394 }
395 return retval;
396}
397
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000398static char *
399key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000400{
401 char *retval;
Damien Millereccb9de2005-06-17 12:59:34 +1000402 u_int i;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000403
Damien Miller07d86be2006-03-26 14:19:21 +1100404 retval = xcalloc(1, dgst_raw_len * 3 + 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100405 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000406 char hex[4];
407 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
Darren Tucker29588612003-07-14 17:28:34 +1000408 strlcat(retval, hex, dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000409 }
Darren Tucker29588612003-07-14 17:28:34 +1000410
411 /* Remove the trailing ':' character */
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000412 retval[(dgst_raw_len * 3) - 1] = '\0';
413 return retval;
414}
415
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000416static char *
417key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000418{
419 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
420 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
421 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000422 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000423 char *retval;
424
425 rounds = (dgst_raw_len / 2) + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100426 retval = xcalloc((rounds * 6), sizeof(char));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000427 retval[j++] = 'x';
428 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000429 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000430 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
431 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000432 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000433 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
434 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000435 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000436 retval[j++] = vowels[idx0];
437 retval[j++] = consonants[idx1];
438 retval[j++] = vowels[idx2];
439 if ((i + 1) < rounds) {
440 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
441 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
442 retval[j++] = consonants[idx3];
443 retval[j++] = '-';
444 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000445 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000446 ((((u_int)(dgst_raw[2 * i])) * 7) +
447 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000448 }
449 } else {
450 idx0 = seed % 6;
451 idx1 = 16;
452 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000453 retval[j++] = vowels[idx0];
454 retval[j++] = consonants[idx1];
455 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000456 }
457 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000458 retval[j++] = 'x';
459 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000460 return retval;
461}
462
Darren Tucker9c16ac92008-06-13 04:40:35 +1000463/*
464 * Draw an ASCII-Art representing the fingerprint so human brain can
465 * profit from its built-in pattern recognition ability.
466 * This technique is called "random art" and can be found in some
467 * scientific publications like this original paper:
468 *
469 * "Hash Visualization: a New Technique to improve Real-World Security",
470 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
471 * Techniques and E-Commerce (CrypTEC '99)
472 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
473 *
474 * The subject came up in a talk by Dan Kaminsky, too.
475 *
476 * If you see the picture is different, the key is different.
477 * If the picture looks the same, you still know nothing.
478 *
479 * The algorithm used here is a worm crawling over a discrete plane,
480 * leaving a trace (augmenting the field) everywhere it goes.
481 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
482 * makes the respective movement vector be ignored for this turn.
483 * Graphs are not unambiguous, because circles in graphs can be
484 * walked in either direction.
485 */
Darren Tucker987ac842008-06-13 04:54:40 +1000486
487/*
488 * Field sizes for the random art. Have to be odd, so the starting point
489 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
490 * Else pictures would be too dense, and drawing the frame would
491 * fail, too, because the key type would not fit in anymore.
492 */
493#define FLDBASE 8
494#define FLDSIZE_Y (FLDBASE + 1)
495#define FLDSIZE_X (FLDBASE * 2 + 1)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000496static char *
Darren Tucker987ac842008-06-13 04:54:40 +1000497key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000498{
499 /*
500 * Chars to be used after each other every time the worm
501 * intersects with itself. Matter of taste.
502 */
Darren Tucker4b3b9772008-06-13 04:55:10 +1000503 char *augmentation_string = " .o+=*BOX@%&#/^SE";
Darren Tucker9c16ac92008-06-13 04:40:35 +1000504 char *retval, *p;
Darren Tucker014d76f2008-06-13 04:43:51 +1000505 u_char field[FLDSIZE_X][FLDSIZE_Y];
Darren Tucker9c16ac92008-06-13 04:40:35 +1000506 u_int i, b;
507 int x, y;
Darren Tuckerd32b28a2008-06-13 04:45:50 +1000508 size_t len = strlen(augmentation_string) - 1;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000509
510 retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
511
512 /* initialize field */
Darren Tucker014d76f2008-06-13 04:43:51 +1000513 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
Darren Tucker9c16ac92008-06-13 04:40:35 +1000514 x = FLDSIZE_X / 2;
515 y = FLDSIZE_Y / 2;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000516
517 /* process raw key */
518 for (i = 0; i < dgst_raw_len; i++) {
519 int input;
520 /* each byte conveys four 2-bit move commands */
521 input = dgst_raw[i];
522 for (b = 0; b < 4; b++) {
523 /* evaluate 2 bit, rest is shifted later */
524 x += (input & 0x1) ? 1 : -1;
525 y += (input & 0x2) ? 1 : -1;
526
527 /* assure we are still in bounds */
528 x = MAX(x, 0);
529 y = MAX(y, 0);
530 x = MIN(x, FLDSIZE_X - 1);
531 y = MIN(y, FLDSIZE_Y - 1);
532
533 /* augment the field */
Damien Millerc6aadd92008-11-03 19:16:20 +1100534 if (field[x][y] < len - 2)
535 field[x][y]++;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000536 input = input >> 2;
537 }
538 }
Darren Tucker4b3b9772008-06-13 04:55:10 +1000539
540 /* mark starting point and end point*/
541 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
542 field[x][y] = len;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000543
544 /* fill in retval */
Damien Miller007132a2008-06-29 22:45:37 +1000545 snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k));
Darren Tucker987ac842008-06-13 04:54:40 +1000546 p = strchr(retval, '\0');
Darren Tucker9c16ac92008-06-13 04:40:35 +1000547
548 /* output upper border */
Damien Miller007132a2008-06-29 22:45:37 +1000549 for (i = p - retval - 1; i < FLDSIZE_X; i++)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000550 *p++ = '-';
551 *p++ = '+';
552 *p++ = '\n';
553
554 /* output content */
555 for (y = 0; y < FLDSIZE_Y; y++) {
556 *p++ = '|';
557 for (x = 0; x < FLDSIZE_X; x++)
Darren Tuckerd32b28a2008-06-13 04:45:50 +1000558 *p++ = augmentation_string[MIN(field[x][y], len)];
Darren Tucker9c16ac92008-06-13 04:40:35 +1000559 *p++ = '|';
560 *p++ = '\n';
561 }
562
563 /* output lower border */
564 *p++ = '+';
565 for (i = 0; i < FLDSIZE_X; i++)
566 *p++ = '-';
567 *p++ = '+';
568
569 return retval;
570}
571
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000572char *
Darren Tucker0acca372013-06-02 07:41:51 +1000573key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000574{
Ben Lindstroma3700052001-04-05 23:26:32 +0000575 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000576 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100577 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100578
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000579 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
580 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000581 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000582 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000583 case SSH_FP_HEX:
584 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
585 break;
586 case SSH_FP_BUBBLEBABBLE:
587 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
588 break;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000589 case SSH_FP_RANDOMART:
Darren Tucker987ac842008-06-13 04:54:40 +1000590 retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000591 break;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000592 default:
Damien Miller2f54ada2008-11-03 19:24:16 +1100593 fatal("key_fingerprint: bad digest representation %d",
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000594 dgst_rep);
595 break;
596 }
597 memset(dgst_raw, 0, dgst_raw_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000598 free(dgst_raw);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000599 return retval;
600}
601
Damien Miller450a7a12000-03-26 13:04:51 +1000602/*
603 * Reads a multiple-precision integer in decimal from the buffer, and advances
604 * the pointer. The integer must already be initialized. This function is
605 * permitted to modify the buffer. This leaves *cpp to point just beyond the
606 * last processed (and maybe modified) character. Note that this may modify
607 * the buffer containing the number.
608 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000609static int
Damien Miller450a7a12000-03-26 13:04:51 +1000610read_bignum(char **cpp, BIGNUM * value)
611{
612 char *cp = *cpp;
613 int old;
614
615 /* Skip any leading whitespace. */
616 for (; *cp == ' ' || *cp == '\t'; cp++)
617 ;
618
619 /* Check that it begins with a decimal digit. */
620 if (*cp < '0' || *cp > '9')
621 return 0;
622
623 /* Save starting position. */
624 *cpp = cp;
625
626 /* Move forward until all decimal digits skipped. */
627 for (; *cp >= '0' && *cp <= '9'; cp++)
628 ;
629
630 /* Save the old terminating character, and replace it by \0. */
631 old = *cp;
632 *cp = 0;
633
634 /* Parse the number. */
635 if (BN_dec2bn(&value, *cpp) == 0)
636 return 0;
637
638 /* Restore old terminating character. */
639 *cp = old;
640
641 /* Move beyond the number and return success. */
642 *cpp = cp;
643 return 1;
644}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000645
Ben Lindstrombba81212001-06-25 05:01:22 +0000646static int
Damien Miller450a7a12000-03-26 13:04:51 +1000647write_bignum(FILE *f, BIGNUM *num)
648{
649 char *buf = BN_bn2dec(num);
650 if (buf == NULL) {
651 error("write_bignum: BN_bn2dec() failed");
652 return 0;
653 }
654 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000655 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000656 return 1;
657}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100658
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000659/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100660int
Damien Millereba71ba2000-04-29 23:57:08 +1000661key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000662{
Damien Millereba71ba2000-04-29 23:57:08 +1000663 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100664 int success = -1;
665 char *cp, *space;
Darren Tucker8ccb7392010-09-10 12:28:24 +1000666 int len, n, type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100667 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000668 u_char *blob;
Darren Tucker8ccb7392010-09-10 12:28:24 +1000669#ifdef OPENSSL_HAS_ECC
670 int curve_nid = -1;
671#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000672
673 cp = *cpp;
674
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000675 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100676 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000677 /* Get number of bits. */
678 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100679 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000680 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
681 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000682 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100683 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000684 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000685 /* Get public exponent, public modulus. */
686 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100687 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000688 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100689 return -1;
Darren Tucker561724f2010-01-13 22:43:05 +1100690 /* validate the claimed number of bits */
691 if ((u_int)BN_num_bits(ret->rsa->n) != bits) {
692 verbose("key_read: claimed key size %d does not match "
693 "actual %d", bits, BN_num_bits(ret->rsa->n));
694 return -1;
695 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100696 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000697 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100698 case KEY_UNSPEC:
699 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000700 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000701 case KEY_ECDSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000702 case KEY_DSA_CERT_V00:
703 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100704 case KEY_DSA_CERT:
Damien Millereb8b60e2010-08-31 22:41:14 +1000705 case KEY_ECDSA_CERT:
Damien Miller0a80ca12010-02-27 07:55:05 +1100706 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100707 space = strchr(cp, ' ');
708 if (space == NULL) {
Damien Miller386f1f32003-02-24 11:54:57 +1100709 debug3("key_read: missing whitespace");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100710 return -1;
711 }
712 *space = '\0';
713 type = key_type_from_name(cp);
Damien Miller6af914a2010-09-10 11:39:26 +1000714#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000715 if (key_type_plain(type) == KEY_ECDSA &&
716 (curve_nid = key_ecdsa_nid_from_name(cp)) == -1) {
717 debug("key_read: invalid curve");
718 return -1;
719 }
Damien Miller6af914a2010-09-10 11:39:26 +1000720#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100721 *space = ' ';
722 if (type == KEY_UNSPEC) {
Damien Miller386f1f32003-02-24 11:54:57 +1100723 debug3("key_read: missing keytype");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100724 return -1;
725 }
726 cp = space+1;
727 if (*cp == '\0') {
728 debug3("key_read: short string");
729 return -1;
730 }
731 if (ret->type == KEY_UNSPEC) {
732 ret->type = type;
733 } else if (ret->type != type) {
734 /* is a key, but different type */
735 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000736 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100737 }
Damien Millereba71ba2000-04-29 23:57:08 +1000738 len = 2*strlen(cp);
739 blob = xmalloc(len);
740 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000741 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000742 error("key_read: uudecode %s failed", cp);
Darren Tuckera627d422013-06-02 07:31:17 +1000743 free(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100744 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000745 }
Darren Tucker502d3842003-06-28 12:38:01 +1000746 k = key_from_blob(blob, (u_int)n);
Darren Tuckera627d422013-06-02 07:31:17 +1000747 free(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000748 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100749 error("key_read: key_from_blob %s failed", cp);
750 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000751 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100752 if (k->type != type) {
753 error("key_read: type mismatch: encoding error");
754 key_free(k);
755 return -1;
756 }
Damien Miller6af914a2010-09-10 11:39:26 +1000757#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000758 if (key_type_plain(type) == KEY_ECDSA &&
759 curve_nid != k->ecdsa_nid) {
760 error("key_read: type mismatch: EC curve mismatch");
761 key_free(k);
762 return -1;
763 }
Damien Miller6af914a2010-09-10 11:39:26 +1000764#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +1100765/*XXXX*/
Damien Miller0a80ca12010-02-27 07:55:05 +1100766 if (key_is_cert(ret)) {
767 if (!key_is_cert(k)) {
768 error("key_read: loaded key is not a cert");
769 key_free(k);
770 return -1;
771 }
772 if (ret->cert != NULL)
773 cert_free(ret->cert);
774 ret->cert = k->cert;
775 k->cert = NULL;
776 }
777 if (key_type_plain(ret->type) == KEY_RSA) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100778 if (ret->rsa != NULL)
779 RSA_free(ret->rsa);
780 ret->rsa = k->rsa;
781 k->rsa = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100782#ifdef DEBUG_PK
783 RSA_print_fp(stderr, ret->rsa, 8);
784#endif
Damien Miller0a80ca12010-02-27 07:55:05 +1100785 }
786 if (key_type_plain(ret->type) == KEY_DSA) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100787 if (ret->dsa != NULL)
788 DSA_free(ret->dsa);
789 ret->dsa = k->dsa;
790 k->dsa = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100791#ifdef DEBUG_PK
792 DSA_print_fp(stderr, ret->dsa, 8);
793#endif
794 }
Damien Miller6af914a2010-09-10 11:39:26 +1000795#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000796 if (key_type_plain(ret->type) == KEY_ECDSA) {
797 if (ret->ecdsa != NULL)
798 EC_KEY_free(ret->ecdsa);
799 ret->ecdsa = k->ecdsa;
800 ret->ecdsa_nid = k->ecdsa_nid;
801 k->ecdsa = NULL;
802 k->ecdsa_nid = -1;
803#ifdef DEBUG_PK
804 key_dump_ec_key(ret->ecdsa);
805#endif
806 }
Damien Miller6af914a2010-09-10 11:39:26 +1000807#endif
Damien Miller0a80ca12010-02-27 07:55:05 +1100808 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100809/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000810 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100811 if (success != 1)
812 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000813 /* advance cp: skip whitespace and data */
814 while (*cp == ' ' || *cp == '\t')
815 cp++;
816 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
817 cp++;
818 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000819 break;
820 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000821 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000822 break;
823 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100824 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000825}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000826
Damien Miller450a7a12000-03-26 13:04:51 +1000827int
Damien Millerf58b58c2003-11-17 21:18:23 +1100828key_write(const Key *key, FILE *f)
Damien Miller450a7a12000-03-26 13:04:51 +1000829{
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000830 int n, success = 0;
831 u_int len, bits = 0;
Damien Millera10f5612002-09-12 09:49:15 +1000832 u_char *blob;
833 char *uu;
Damien Miller450a7a12000-03-26 13:04:51 +1000834
Damien Miller0a80ca12010-02-27 07:55:05 +1100835 if (key_is_cert(key)) {
836 if (key->cert == NULL) {
837 error("%s: no cert data", __func__);
838 return 0;
839 }
840 if (buffer_len(&key->cert->certblob) == 0) {
841 error("%s: no signed certificate blob", __func__);
842 return 0;
843 }
844 }
845
846 switch (key->type) {
847 case KEY_RSA1:
848 if (key->rsa == NULL)
849 return 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000850 /* size of modulus 'n' */
851 bits = BN_num_bits(key->rsa->n);
852 fprintf(f, "%u", bits);
853 if (write_bignum(f, key->rsa->e) &&
Damien Miller0a80ca12010-02-27 07:55:05 +1100854 write_bignum(f, key->rsa->n))
855 return 1;
856 error("key_write: failed for RSA key");
857 return 0;
858 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000859 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100860 case KEY_DSA_CERT:
861 if (key->dsa == NULL)
862 return 0;
863 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000864#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000865 case KEY_ECDSA:
866 case KEY_ECDSA_CERT:
867 if (key->ecdsa == NULL)
868 return 0;
869 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000870#endif
Damien Miller0a80ca12010-02-27 07:55:05 +1100871 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000872 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100873 case KEY_RSA_CERT:
874 if (key->rsa == NULL)
875 return 0;
876 break;
877 default:
878 return 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000879 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100880
881 key_to_blob(key, &blob, &len);
882 uu = xmalloc(2*len);
883 n = uuencode(blob, len, uu, 2*len);
884 if (n > 0) {
885 fprintf(f, "%s %s", key_ssh_name(key), uu);
886 success = 1;
887 }
Darren Tuckera627d422013-06-02 07:31:17 +1000888 free(blob);
889 free(uu);
Damien Miller0a80ca12010-02-27 07:55:05 +1100890
Damien Miller450a7a12000-03-26 13:04:51 +1000891 return success;
892}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000893
Damien Millerf58b58c2003-11-17 21:18:23 +1100894const char *
Damien Miller1cfbfaf2010-03-22 05:58:24 +1100895key_cert_type(const Key *k)
896{
897 switch (k->cert->type) {
898 case SSH2_CERT_TYPE_USER:
899 return "user";
900 case SSH2_CERT_TYPE_HOST:
901 return "host";
902 default:
903 return "unknown";
904 }
905}
906
Damien Millerea111192013-04-23 19:24:32 +1000907struct keytype {
908 char *name;
909 char *shortname;
910 int type;
911 int nid;
912 int cert;
913};
914static const struct keytype keytypes[] = {
915 { NULL, "RSA1", KEY_RSA1, 0, 0 },
916 { "ssh-rsa", "RSA", KEY_RSA, 0, 0 },
917 { "ssh-dss", "DSA", KEY_DSA, 0, 0 },
918#ifdef OPENSSL_HAS_ECC
919 { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA, NID_X9_62_prime256v1, 0 },
920 { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA, NID_secp384r1, 0 },
Darren Tucker37bcef52013-11-09 18:39:25 +1100921# ifdef OPENSSL_HAS_NISTP521
Damien Millerea111192013-04-23 19:24:32 +1000922 { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0 },
Darren Tucker37bcef52013-11-09 18:39:25 +1100923# endif
Damien Millerea111192013-04-23 19:24:32 +1000924#endif /* OPENSSL_HAS_ECC */
925 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT, 0, 1 },
926 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT, 0, 1 },
927#ifdef OPENSSL_HAS_ECC
928 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT",
929 KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1 },
930 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT",
931 KEY_ECDSA_CERT, NID_secp384r1, 1 },
Darren Tucker37bcef52013-11-09 18:39:25 +1100932# ifdef OPENSSL_HAS_NISTP521
Damien Millerea111192013-04-23 19:24:32 +1000933 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT",
934 KEY_ECDSA_CERT, NID_secp521r1, 1 },
Darren Tucker37bcef52013-11-09 18:39:25 +1100935# endif
Damien Millerea111192013-04-23 19:24:32 +1000936#endif /* OPENSSL_HAS_ECC */
937 { "ssh-rsa-cert-v00@openssh.com", "RSA-CERT-V00",
938 KEY_RSA_CERT_V00, 0, 1 },
939 { "ssh-dss-cert-v00@openssh.com", "DSA-CERT-V00",
940 KEY_DSA_CERT_V00, 0, 1 },
941 { NULL, NULL, -1, -1, 0 }
942};
943
944const char *
945key_type(const Key *k)
946{
947 const struct keytype *kt;
948
949 for (kt = keytypes; kt->type != -1; kt++) {
950 if (kt->type == k->type)
951 return kt->shortname;
952 }
953 return "unknown";
954}
955
Damien Millereb8b60e2010-08-31 22:41:14 +1000956static const char *
957key_ssh_name_from_type_nid(int type, int nid)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100958{
Damien Millerea111192013-04-23 19:24:32 +1000959 const struct keytype *kt;
960
961 for (kt = keytypes; kt->type != -1; kt++) {
962 if (kt->type == type && (kt->nid == 0 || kt->nid == nid))
963 return kt->name;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100964 }
965 return "ssh-unknown";
966}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000967
Damien Millereb8b60e2010-08-31 22:41:14 +1000968const char *
969key_ssh_name(const Key *k)
970{
971 return key_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
972}
973
974const char *
975key_ssh_name_plain(const Key *k)
976{
977 return key_ssh_name_from_type_nid(key_type_plain(k->type),
978 k->ecdsa_nid);
979}
980
Damien Millerea111192013-04-23 19:24:32 +1000981int
982key_type_from_name(char *name)
983{
984 const struct keytype *kt;
985
986 for (kt = keytypes; kt->type != -1; kt++) {
987 /* Only allow shortname matches for plain key types */
988 if ((kt->name != NULL && strcmp(name, kt->name) == 0) ||
989 (!kt->cert && strcasecmp(kt->shortname, name) == 0))
990 return kt->type;
991 }
992 debug2("key_type_from_name: unknown key type '%s'", name);
993 return KEY_UNSPEC;
994}
995
996int
997key_ecdsa_nid_from_name(const char *name)
998{
999 const struct keytype *kt;
1000
1001 for (kt = keytypes; kt->type != -1; kt++) {
1002 if (kt->type != KEY_ECDSA && kt->type != KEY_ECDSA_CERT)
1003 continue;
1004 if (kt->name != NULL && strcmp(name, kt->name) == 0)
1005 return kt->nid;
1006 }
1007 debug2("%s: unknown/non-ECDSA key type '%s'", __func__, name);
1008 return -1;
1009}
1010
1011char *
1012key_alg_list(void)
1013{
1014 char *ret = NULL;
1015 size_t nlen, rlen = 0;
1016 const struct keytype *kt;
1017
1018 for (kt = keytypes; kt->type != -1; kt++) {
1019 if (kt->name == NULL)
1020 continue;
1021 if (ret != NULL)
1022 ret[rlen++] = '\n';
1023 nlen = strlen(kt->name);
1024 ret = xrealloc(ret, 1, rlen + nlen + 2);
1025 memcpy(ret + rlen, kt->name, nlen + 1);
1026 rlen += nlen;
1027 }
1028 return ret;
1029}
1030
Damien Miller4a3a9d42013-10-30 22:19:47 +11001031int
1032key_type_is_cert(int type)
1033{
1034 const struct keytype *kt;
1035
1036 for (kt = keytypes; kt->type != -1; kt++) {
1037 if (kt->type == type)
1038 return kt->cert;
1039 }
1040 return 0;
1041}
1042
Damien Miller0bc1bd82000-11-13 22:57:25 +11001043u_int
Damien Millerf58b58c2003-11-17 21:18:23 +11001044key_size(const Key *k)
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001045{
Damien Millerad833b32000-08-23 10:46:23 +10001046 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001047 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +10001048 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +10001049 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001050 case KEY_RSA_CERT:
Damien Millerad833b32000-08-23 10:46:23 +10001051 return BN_num_bits(k->rsa->n);
Damien Millerad833b32000-08-23 10:46:23 +10001052 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +10001053 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001054 case KEY_DSA_CERT:
Damien Millerad833b32000-08-23 10:46:23 +10001055 return BN_num_bits(k->dsa->p);
Damien Miller6af914a2010-09-10 11:39:26 +10001056#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001057 case KEY_ECDSA:
1058 case KEY_ECDSA_CERT:
Damien Miller041ab7c2010-09-10 11:23:34 +10001059 return key_curve_nid_to_bits(k->ecdsa_nid);
Damien Miller6af914a2010-09-10 11:39:26 +10001060#endif
Damien Millerad833b32000-08-23 10:46:23 +10001061 }
1062 return 0;
1063}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001064
Ben Lindstrombba81212001-06-25 05:01:22 +00001065static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001066rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001067{
Damien Miller4499f4c2010-11-20 15:15:49 +11001068 RSA *private = RSA_new();
1069 BIGNUM *f4 = BN_new();
Damien Miller69b72032006-03-26 14:02:35 +11001070
Kevin Stevesef4eea92001-02-05 12:42:17 +00001071 if (private == NULL)
Damien Miller4499f4c2010-11-20 15:15:49 +11001072 fatal("%s: RSA_new failed", __func__);
1073 if (f4 == NULL)
1074 fatal("%s: BN_new failed", __func__);
1075 if (!BN_set_word(f4, RSA_F4))
1076 fatal("%s: BN_new failed", __func__);
1077 if (!RSA_generate_key_ex(private, bits, f4, NULL))
1078 fatal("%s: key generation failed.", __func__);
1079 BN_free(f4);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001080 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001081}
1082
Ben Lindstrombba81212001-06-25 05:01:22 +00001083static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +00001084dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001085{
Damien Miller4499f4c2010-11-20 15:15:49 +11001086 DSA *private = DSA_new();
Damien Miller69b72032006-03-26 14:02:35 +11001087
Damien Miller0bc1bd82000-11-13 22:57:25 +11001088 if (private == NULL)
Damien Miller4499f4c2010-11-20 15:15:49 +11001089 fatal("%s: DSA_new failed", __func__);
1090 if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL,
1091 NULL, NULL))
1092 fatal("%s: DSA_generate_parameters failed", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001093 if (!DSA_generate_key(private))
Damien Miller4499f4c2010-11-20 15:15:49 +11001094 fatal("%s: DSA_generate_key failed.", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001095 return private;
1096}
1097
Damien Millereb8b60e2010-08-31 22:41:14 +10001098int
1099key_ecdsa_bits_to_nid(int bits)
1100{
1101 switch (bits) {
Damien Miller6af914a2010-09-10 11:39:26 +10001102#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001103 case 256:
1104 return NID_X9_62_prime256v1;
1105 case 384:
1106 return NID_secp384r1;
Darren Tucker2c894302013-11-10 12:38:42 +11001107# ifdef OPENSSL_HAS_NISTP521
Damien Millereb8b60e2010-08-31 22:41:14 +10001108 case 521:
1109 return NID_secp521r1;
Darren Tucker37bcef52013-11-09 18:39:25 +11001110# endif
Damien Miller6af914a2010-09-10 11:39:26 +10001111#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001112 default:
1113 return -1;
1114 }
1115}
1116
Damien Miller6af914a2010-09-10 11:39:26 +10001117#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001118int
Damien Millerb472a902010-11-05 10:19:49 +11001119key_ecdsa_key_to_nid(EC_KEY *k)
Damien Millereb8b60e2010-08-31 22:41:14 +10001120{
1121 EC_GROUP *eg;
1122 int nids[] = {
1123 NID_X9_62_prime256v1,
1124 NID_secp384r1,
Darren Tucker37bcef52013-11-09 18:39:25 +11001125# ifdef OPENSSL_HAS_NISTP521
Damien Millereb8b60e2010-08-31 22:41:14 +10001126 NID_secp521r1,
Darren Tucker37bcef52013-11-09 18:39:25 +11001127# endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001128 -1
1129 };
Damien Millerb472a902010-11-05 10:19:49 +11001130 int nid;
Damien Millereb8b60e2010-08-31 22:41:14 +10001131 u_int i;
1132 BN_CTX *bnctx;
Damien Millerb472a902010-11-05 10:19:49 +11001133 const EC_GROUP *g = EC_KEY_get0_group(k);
Damien Millereb8b60e2010-08-31 22:41:14 +10001134
Damien Millerb472a902010-11-05 10:19:49 +11001135 /*
1136 * The group may be stored in a ASN.1 encoded private key in one of two
1137 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1138 * or explicit group parameters encoded into the key blob. Only the
1139 * "named group" case sets the group NID for us, but we can figure
1140 * it out for the other case by comparing against all the groups that
1141 * are supported.
1142 */
1143 if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1144 return nid;
Damien Millereb8b60e2010-08-31 22:41:14 +10001145 if ((bnctx = BN_CTX_new()) == NULL)
1146 fatal("%s: BN_CTX_new() failed", __func__);
1147 for (i = 0; nids[i] != -1; i++) {
1148 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
1149 fatal("%s: EC_GROUP_new_by_curve_name failed",
1150 __func__);
Damien Millerb472a902010-11-05 10:19:49 +11001151 if (EC_GROUP_cmp(g, eg, bnctx) == 0)
Damien Millereb8b60e2010-08-31 22:41:14 +10001152 break;
Damien Millereb8b60e2010-08-31 22:41:14 +10001153 EC_GROUP_free(eg);
1154 }
1155 BN_CTX_free(bnctx);
1156 debug3("%s: nid = %d", __func__, nids[i]);
Damien Millerb472a902010-11-05 10:19:49 +11001157 if (nids[i] != -1) {
1158 /* Use the group with the NID attached */
1159 EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1160 if (EC_KEY_set_group(k, eg) != 1)
1161 fatal("%s: EC_KEY_set_group", __func__);
1162 }
Damien Millereb8b60e2010-08-31 22:41:14 +10001163 return nids[i];
1164}
1165
1166static EC_KEY*
1167ecdsa_generate_private_key(u_int bits, int *nid)
1168{
1169 EC_KEY *private;
1170
1171 if ((*nid = key_ecdsa_bits_to_nid(bits)) == -1)
1172 fatal("%s: invalid key length", __func__);
1173 if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL)
1174 fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
1175 if (EC_KEY_generate_key(private) != 1)
1176 fatal("%s: EC_KEY_generate_key failed", __func__);
Damien Millerb472a902010-11-05 10:19:49 +11001177 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
Damien Millereb8b60e2010-08-31 22:41:14 +10001178 return private;
1179}
Damien Miller6af914a2010-09-10 11:39:26 +10001180#endif /* OPENSSL_HAS_ECC */
Damien Millereb8b60e2010-08-31 22:41:14 +10001181
Damien Miller0bc1bd82000-11-13 22:57:25 +11001182Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +00001183key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001184{
1185 Key *k = key_new(KEY_UNSPEC);
1186 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +00001187 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001188 k->dsa = dsa_generate_private_key(bits);
1189 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001190#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001191 case KEY_ECDSA:
1192 k->ecdsa = ecdsa_generate_private_key(bits, &k->ecdsa_nid);
1193 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001194#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001195 case KEY_RSA:
1196 case KEY_RSA1:
1197 k->rsa = rsa_generate_private_key(bits);
1198 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001199 case KEY_RSA_CERT_V00:
1200 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001201 case KEY_RSA_CERT:
1202 case KEY_DSA_CERT:
1203 fatal("key_generate: cert keys cannot be generated directly");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001204 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +00001205 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001206 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001207 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001208 return k;
1209}
1210
Damien Miller0a80ca12010-02-27 07:55:05 +11001211void
1212key_cert_copy(const Key *from_key, struct Key *to_key)
1213{
1214 u_int i;
1215 const struct KeyCert *from;
1216 struct KeyCert *to;
1217
1218 if (to_key->cert != NULL) {
1219 cert_free(to_key->cert);
1220 to_key->cert = NULL;
1221 }
1222
1223 if ((from = from_key->cert) == NULL)
1224 return;
1225
1226 to = to_key->cert = cert_new();
1227
1228 buffer_append(&to->certblob, buffer_ptr(&from->certblob),
1229 buffer_len(&from->certblob));
1230
Damien Miller4e270b02010-04-16 15:56:21 +10001231 buffer_append(&to->critical,
1232 buffer_ptr(&from->critical), buffer_len(&from->critical));
1233 buffer_append(&to->extensions,
1234 buffer_ptr(&from->extensions), buffer_len(&from->extensions));
Damien Miller0a80ca12010-02-27 07:55:05 +11001235
Damien Miller4e270b02010-04-16 15:56:21 +10001236 to->serial = from->serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001237 to->type = from->type;
1238 to->key_id = from->key_id == NULL ? NULL : xstrdup(from->key_id);
1239 to->valid_after = from->valid_after;
1240 to->valid_before = from->valid_before;
1241 to->signature_key = from->signature_key == NULL ?
1242 NULL : key_from_private(from->signature_key);
1243
1244 to->nprincipals = from->nprincipals;
1245 if (to->nprincipals > CERT_MAX_PRINCIPALS)
1246 fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)",
1247 __func__, to->nprincipals, CERT_MAX_PRINCIPALS);
1248 if (to->nprincipals > 0) {
1249 to->principals = xcalloc(from->nprincipals,
1250 sizeof(*to->principals));
1251 for (i = 0; i < to->nprincipals; i++)
1252 to->principals[i] = xstrdup(from->principals[i]);
1253 }
1254}
1255
Damien Miller0bc1bd82000-11-13 22:57:25 +11001256Key *
Damien Millerf58b58c2003-11-17 21:18:23 +11001257key_from_private(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001258{
1259 Key *n = NULL;
1260 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +00001261 case KEY_DSA:
Damien Miller4e270b02010-04-16 15:56:21 +10001262 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001263 case KEY_DSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001264 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +11001265 if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
1266 (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
1267 (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
1268 (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
1269 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001270 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001271#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001272 case KEY_ECDSA:
1273 case KEY_ECDSA_CERT:
1274 n = key_new(k->type);
1275 n->ecdsa_nid = k->ecdsa_nid;
1276 if ((n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid)) == NULL)
1277 fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
1278 if (EC_KEY_set_public_key(n->ecdsa,
1279 EC_KEY_get0_public_key(k->ecdsa)) != 1)
1280 fatal("%s: EC_KEY_set_public_key failed", __func__);
1281 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001282#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001283 case KEY_RSA:
1284 case KEY_RSA1:
Damien Miller4e270b02010-04-16 15:56:21 +10001285 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001286 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001287 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +11001288 if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
1289 (BN_copy(n->rsa->e, k->rsa->e) == NULL))
1290 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001291 break;
1292 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +00001293 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001294 break;
1295 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001296 if (key_is_cert(k))
1297 key_cert_copy(k, n);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001298 return n;
1299}
1300
1301int
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001302key_names_valid2(const char *names)
1303{
1304 char *s, *cp, *p;
1305
1306 if (names == NULL || strcmp(names, "") == 0)
1307 return 0;
1308 s = cp = xstrdup(names);
1309 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11001310 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001311 switch (key_type_from_name(p)) {
1312 case KEY_RSA1:
1313 case KEY_UNSPEC:
Darren Tuckera627d422013-06-02 07:31:17 +10001314 free(s);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001315 return 0;
1316 }
1317 }
1318 debug3("key names ok: [%s]", names);
Darren Tuckera627d422013-06-02 07:31:17 +10001319 free(s);
Ben Lindstrom982dbbc2001-04-17 18:11:36 +00001320 return 1;
1321}
1322
Damien Miller0a80ca12010-02-27 07:55:05 +11001323static int
1324cert_parse(Buffer *b, Key *key, const u_char *blob, u_int blen)
1325{
Damien Miller4e270b02010-04-16 15:56:21 +10001326 u_char *principals, *critical, *exts, *sig_key, *sig;
1327 u_int signed_len, plen, clen, sklen, slen, kidlen, elen;
Damien Miller0a80ca12010-02-27 07:55:05 +11001328 Buffer tmp;
1329 char *principal;
1330 int ret = -1;
Damien Miller4e270b02010-04-16 15:56:21 +10001331 int v00 = key->type == KEY_DSA_CERT_V00 ||
1332 key->type == KEY_RSA_CERT_V00;
Damien Miller0a80ca12010-02-27 07:55:05 +11001333
1334 buffer_init(&tmp);
1335
1336 /* Copy the entire key blob for verification and later serialisation */
1337 buffer_append(&key->cert->certblob, blob, blen);
1338
Damien Miller4e270b02010-04-16 15:56:21 +10001339 elen = 0; /* Not touched for v00 certs */
1340 principals = exts = critical = sig_key = sig = NULL;
1341 if ((!v00 && buffer_get_int64_ret(&key->cert->serial, b) != 0) ||
1342 buffer_get_int_ret(&key->cert->type, b) != 0 ||
Damien Millerda108ec2010-08-31 22:36:39 +10001343 (key->cert->key_id = buffer_get_cstring_ret(b, &kidlen)) == NULL ||
Damien Miller0a80ca12010-02-27 07:55:05 +11001344 (principals = buffer_get_string_ret(b, &plen)) == NULL ||
1345 buffer_get_int64_ret(&key->cert->valid_after, b) != 0 ||
1346 buffer_get_int64_ret(&key->cert->valid_before, b) != 0 ||
Damien Miller4e270b02010-04-16 15:56:21 +10001347 (critical = buffer_get_string_ret(b, &clen)) == NULL ||
1348 (!v00 && (exts = buffer_get_string_ret(b, &elen)) == NULL) ||
1349 (v00 && buffer_get_string_ptr_ret(b, NULL) == NULL) || /* nonce */
1350 buffer_get_string_ptr_ret(b, NULL) == NULL || /* reserved */
Damien Miller0a80ca12010-02-27 07:55:05 +11001351 (sig_key = buffer_get_string_ret(b, &sklen)) == NULL) {
1352 error("%s: parse error", __func__);
1353 goto out;
1354 }
1355
1356 /* Signature is left in the buffer so we can calculate this length */
1357 signed_len = buffer_len(&key->cert->certblob) - buffer_len(b);
1358
1359 if ((sig = buffer_get_string_ret(b, &slen)) == NULL) {
1360 error("%s: parse error", __func__);
1361 goto out;
1362 }
1363
1364 if (key->cert->type != SSH2_CERT_TYPE_USER &&
1365 key->cert->type != SSH2_CERT_TYPE_HOST) {
1366 error("Unknown certificate type %u", key->cert->type);
1367 goto out;
1368 }
1369
1370 buffer_append(&tmp, principals, plen);
1371 while (buffer_len(&tmp) > 0) {
1372 if (key->cert->nprincipals >= CERT_MAX_PRINCIPALS) {
Damien Miller41396572010-03-04 21:51:11 +11001373 error("%s: Too many principals", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001374 goto out;
1375 }
Damien Millerda108ec2010-08-31 22:36:39 +10001376 if ((principal = buffer_get_cstring_ret(&tmp, &plen)) == NULL) {
Damien Miller41396572010-03-04 21:51:11 +11001377 error("%s: Principals data invalid", __func__);
1378 goto out;
1379 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001380 key->cert->principals = xrealloc(key->cert->principals,
1381 key->cert->nprincipals + 1, sizeof(*key->cert->principals));
1382 key->cert->principals[key->cert->nprincipals++] = principal;
1383 }
1384
1385 buffer_clear(&tmp);
1386
Damien Miller4e270b02010-04-16 15:56:21 +10001387 buffer_append(&key->cert->critical, critical, clen);
1388 buffer_append(&tmp, critical, clen);
Damien Miller0a80ca12010-02-27 07:55:05 +11001389 /* validate structure */
1390 while (buffer_len(&tmp) != 0) {
Damien Miller2befbad2010-03-04 21:52:18 +11001391 if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL ||
1392 buffer_get_string_ptr_ret(&tmp, NULL) == NULL) {
Damien Miller4e270b02010-04-16 15:56:21 +10001393 error("%s: critical option data invalid", __func__);
1394 goto out;
1395 }
1396 }
1397 buffer_clear(&tmp);
1398
1399 buffer_append(&key->cert->extensions, exts, elen);
1400 buffer_append(&tmp, exts, elen);
1401 /* validate structure */
1402 while (buffer_len(&tmp) != 0) {
1403 if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL ||
1404 buffer_get_string_ptr_ret(&tmp, NULL) == NULL) {
1405 error("%s: extension data invalid", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001406 goto out;
1407 }
1408 }
1409 buffer_clear(&tmp);
1410
Damien Miller4a3a9d42013-10-30 22:19:47 +11001411 if ((key->cert->signature_key = key_from_blob2(sig_key, sklen, 0))
1412 == NULL) {
Damien Miller41396572010-03-04 21:51:11 +11001413 error("%s: Signature key invalid", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001414 goto out;
1415 }
1416 if (key->cert->signature_key->type != KEY_RSA &&
Damien Millereb8b60e2010-08-31 22:41:14 +10001417 key->cert->signature_key->type != KEY_DSA &&
1418 key->cert->signature_key->type != KEY_ECDSA) {
Damien Miller41396572010-03-04 21:51:11 +11001419 error("%s: Invalid signature key type %s (%d)", __func__,
Damien Miller0a80ca12010-02-27 07:55:05 +11001420 key_type(key->cert->signature_key),
1421 key->cert->signature_key->type);
1422 goto out;
1423 }
1424
1425 switch (key_verify(key->cert->signature_key, sig, slen,
1426 buffer_ptr(&key->cert->certblob), signed_len)) {
1427 case 1:
Damien Miller41396572010-03-04 21:51:11 +11001428 ret = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001429 break; /* Good signature */
1430 case 0:
Damien Miller41396572010-03-04 21:51:11 +11001431 error("%s: Invalid signature on certificate", __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001432 goto out;
1433 case -1:
Damien Miller41396572010-03-04 21:51:11 +11001434 error("%s: Certificate signature verification failed",
1435 __func__);
Damien Miller0a80ca12010-02-27 07:55:05 +11001436 goto out;
1437 }
1438
Damien Miller0a80ca12010-02-27 07:55:05 +11001439 out:
1440 buffer_free(&tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001441 free(principals);
1442 free(critical);
1443 free(exts);
1444 free(sig_key);
1445 free(sig);
Damien Miller0a80ca12010-02-27 07:55:05 +11001446 return ret;
1447}
1448
Damien Miller4a3a9d42013-10-30 22:19:47 +11001449static Key *
1450key_from_blob2(const u_char *blob, u_int blen, int allow_cert)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001451{
1452 Buffer b;
Darren Tucker8ccb7392010-09-10 12:28:24 +10001453 int rlen, type;
Damien Millereb8b60e2010-08-31 22:41:14 +10001454 char *ktype = NULL, *curve = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001455 Key *key = NULL;
Damien Miller6af914a2010-09-10 11:39:26 +10001456#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001457 EC_POINT *q = NULL;
Darren Tucker8ccb7392010-09-10 12:28:24 +10001458 int nid = -1;
Damien Miller6af914a2010-09-10 11:39:26 +10001459#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001460
1461#ifdef DEBUG_PK
1462 dump_base64(stderr, blob, blen);
1463#endif
1464 buffer_init(&b);
1465 buffer_append(&b, blob, blen);
Damien Millerda108ec2010-08-31 22:36:39 +10001466 if ((ktype = buffer_get_cstring_ret(&b, NULL)) == NULL) {
Darren Tucker08d04fa2004-11-05 20:42:28 +11001467 error("key_from_blob: can't read key type");
1468 goto out;
1469 }
1470
Damien Miller0bc1bd82000-11-13 22:57:25 +11001471 type = key_type_from_name(ktype);
Damien Miller6af914a2010-09-10 11:39:26 +10001472#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001473 if (key_type_plain(type) == KEY_ECDSA)
1474 nid = key_ecdsa_nid_from_name(ktype);
Damien Miller6af914a2010-09-10 11:39:26 +10001475#endif
Damien Miller4a3a9d42013-10-30 22:19:47 +11001476 if (!allow_cert && key_type_is_cert(type)) {
1477 error("key_from_blob: certificate not allowed in this context");
1478 goto out;
1479 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001480 switch (type) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001481 case KEY_RSA_CERT:
Damien Miller4e270b02010-04-16 15:56:21 +10001482 (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1483 /* FALLTHROUGH */
1484 case KEY_RSA:
1485 case KEY_RSA_CERT_V00:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001486 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001487 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
1488 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
1489 error("key_from_blob: can't read rsa key");
Damien Miller0a80ca12010-02-27 07:55:05 +11001490 badkey:
Darren Tucker08d04fa2004-11-05 20:42:28 +11001491 key_free(key);
1492 key = NULL;
1493 goto out;
1494 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001495#ifdef DEBUG_PK
1496 RSA_print_fp(stderr, key->rsa, 8);
1497#endif
1498 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001499 case KEY_DSA_CERT:
Damien Miller4e270b02010-04-16 15:56:21 +10001500 (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1501 /* FALLTHROUGH */
1502 case KEY_DSA:
1503 case KEY_DSA_CERT_V00:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001504 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001505 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
1506 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
1507 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
1508 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
1509 error("key_from_blob: can't read dsa key");
Damien Miller0a80ca12010-02-27 07:55:05 +11001510 goto badkey;
Darren Tucker08d04fa2004-11-05 20:42:28 +11001511 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001512#ifdef DEBUG_PK
1513 DSA_print_fp(stderr, key->dsa, 8);
1514#endif
1515 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001516#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001517 case KEY_ECDSA_CERT:
1518 (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */
1519 /* FALLTHROUGH */
1520 case KEY_ECDSA:
1521 key = key_new(type);
1522 key->ecdsa_nid = nid;
1523 if ((curve = buffer_get_string_ret(&b, NULL)) == NULL) {
1524 error("key_from_blob: can't read ecdsa curve");
1525 goto badkey;
1526 }
1527 if (key->ecdsa_nid != key_curve_name_to_nid(curve)) {
1528 error("key_from_blob: ecdsa curve doesn't match type");
1529 goto badkey;
1530 }
1531 if (key->ecdsa != NULL)
1532 EC_KEY_free(key->ecdsa);
1533 if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid))
1534 == NULL)
1535 fatal("key_from_blob: EC_KEY_new_by_curve_name failed");
1536 if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL)
1537 fatal("key_from_blob: EC_POINT_new failed");
1538 if (buffer_get_ecpoint_ret(&b, EC_KEY_get0_group(key->ecdsa),
1539 q) == -1) {
1540 error("key_from_blob: can't read ecdsa key point");
1541 goto badkey;
1542 }
1543 if (key_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
1544 q) != 0)
1545 goto badkey;
1546 if (EC_KEY_set_public_key(key->ecdsa, q) != 1)
1547 fatal("key_from_blob: EC_KEY_set_public_key failed");
1548#ifdef DEBUG_PK
1549 key_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q);
1550#endif
1551 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001552#endif /* OPENSSL_HAS_ECC */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001553 case KEY_UNSPEC:
1554 key = key_new(type);
1555 break;
1556 default:
1557 error("key_from_blob: cannot handle type %s", ktype);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001558 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001559 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001560 if (key_is_cert(key) && cert_parse(&b, key, blob, blen) == -1) {
1561 error("key_from_blob: can't parse cert data");
1562 goto badkey;
1563 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001564 rlen = buffer_len(&b);
1565 if (key != NULL && rlen != 0)
1566 error("key_from_blob: remaining bytes in key blob %d", rlen);
Darren Tucker08d04fa2004-11-05 20:42:28 +11001567 out:
Darren Tuckera627d422013-06-02 07:31:17 +10001568 free(ktype);
1569 free(curve);
Damien Miller6af914a2010-09-10 11:39:26 +10001570#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001571 if (q != NULL)
1572 EC_POINT_free(q);
Damien Miller6af914a2010-09-10 11:39:26 +10001573#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001574 buffer_free(&b);
1575 return key;
1576}
1577
Damien Miller4a3a9d42013-10-30 22:19:47 +11001578Key *
1579key_from_blob(const u_char *blob, u_int blen)
1580{
1581 return key_from_blob2(blob, blen, 1);
1582}
1583
Damien Millerf3747bf2013-01-18 11:44:04 +11001584static int
1585to_blob(const Key *key, u_char **blobp, u_int *lenp, int force_plain)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001586{
1587 Buffer b;
Damien Millerf3747bf2013-01-18 11:44:04 +11001588 int len, type;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001589
Damien Millerf7e8a872013-12-05 10:25:51 +11001590 if (blobp != NULL)
1591 *blobp = NULL;
1592 if (lenp != NULL)
1593 *lenp = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001594 if (key == NULL) {
1595 error("key_to_blob: key == NULL");
1596 return 0;
1597 }
1598 buffer_init(&b);
Damien Millerf3747bf2013-01-18 11:44:04 +11001599 type = force_plain ? key_type_plain(key->type) : key->type;
1600 switch (type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001601 case KEY_DSA_CERT_V00:
1602 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001603 case KEY_DSA_CERT:
Damien Millereb8b60e2010-08-31 22:41:14 +10001604 case KEY_ECDSA_CERT:
Damien Miller0a80ca12010-02-27 07:55:05 +11001605 case KEY_RSA_CERT:
1606 /* Use the existing blob */
1607 buffer_append(&b, buffer_ptr(&key->cert->certblob),
1608 buffer_len(&key->cert->certblob));
1609 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001610 case KEY_DSA:
Damien Millerf3747bf2013-01-18 11:44:04 +11001611 buffer_put_cstring(&b,
1612 key_ssh_name_from_type_nid(type, key->ecdsa_nid));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001613 buffer_put_bignum2(&b, key->dsa->p);
1614 buffer_put_bignum2(&b, key->dsa->q);
1615 buffer_put_bignum2(&b, key->dsa->g);
1616 buffer_put_bignum2(&b, key->dsa->pub_key);
1617 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001618#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001619 case KEY_ECDSA:
Damien Millerf3747bf2013-01-18 11:44:04 +11001620 buffer_put_cstring(&b,
1621 key_ssh_name_from_type_nid(type, key->ecdsa_nid));
Damien Millereb8b60e2010-08-31 22:41:14 +10001622 buffer_put_cstring(&b, key_curve_nid_to_name(key->ecdsa_nid));
1623 buffer_put_ecpoint(&b, EC_KEY_get0_group(key->ecdsa),
1624 EC_KEY_get0_public_key(key->ecdsa));
1625 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001626#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001627 case KEY_RSA:
Damien Millerf3747bf2013-01-18 11:44:04 +11001628 buffer_put_cstring(&b,
1629 key_ssh_name_from_type_nid(type, key->ecdsa_nid));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001630 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001631 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001632 break;
1633 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +00001634 error("key_to_blob: unsupported key type %d", key->type);
1635 buffer_free(&b);
1636 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001637 }
1638 len = buffer_len(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001639 if (lenp != NULL)
1640 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +00001641 if (blobp != NULL) {
1642 *blobp = xmalloc(len);
1643 memcpy(*blobp, buffer_ptr(&b), len);
1644 }
1645 memset(buffer_ptr(&b), 0, len);
1646 buffer_free(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001647 return len;
1648}
1649
1650int
Damien Millerf3747bf2013-01-18 11:44:04 +11001651key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1652{
1653 return to_blob(key, blobp, lenp, 0);
1654}
1655
1656int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001657key_sign(
Damien Millerf58b58c2003-11-17 21:18:23 +11001658 const Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001659 u_char **sigp, u_int *lenp,
Damien Millerf58b58c2003-11-17 21:18:23 +11001660 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001661{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001662 switch (key->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001663 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001664 case KEY_DSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001665 case KEY_DSA:
1666 return ssh_dss_sign(key, sigp, lenp, data, datalen);
Damien Miller6af914a2010-09-10 11:39:26 +10001667#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001668 case KEY_ECDSA_CERT:
1669 case KEY_ECDSA:
1670 return ssh_ecdsa_sign(key, sigp, lenp, data, datalen);
Damien Miller6af914a2010-09-10 11:39:26 +10001671#endif
Damien Miller4e270b02010-04-16 15:56:21 +10001672 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001673 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001674 case KEY_RSA:
1675 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001676 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +10001677 error("key_sign: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001678 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001679 }
1680}
1681
Ben Lindstrom01fff0c2002-06-06 20:54:07 +00001682/*
1683 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
1684 * and -1 on error.
1685 */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001686int
1687key_verify(
Damien Millerf58b58c2003-11-17 21:18:23 +11001688 const Key *key,
1689 const u_char *signature, u_int signaturelen,
1690 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001691{
Ben Lindstrom5363aee2001-06-25 04:42:20 +00001692 if (signaturelen == 0)
1693 return -1;
1694
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001695 switch (key->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001696 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001697 case KEY_DSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001698 case KEY_DSA:
1699 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
Damien Miller6af914a2010-09-10 11:39:26 +10001700#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001701 case KEY_ECDSA_CERT:
1702 case KEY_ECDSA:
1703 return ssh_ecdsa_verify(key, signature, signaturelen, data, datalen);
Damien Miller6af914a2010-09-10 11:39:26 +10001704#endif
Damien Miller4e270b02010-04-16 15:56:21 +10001705 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001706 case KEY_RSA_CERT:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001707 case KEY_RSA:
1708 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001709 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +10001710 error("key_verify: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001711 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001712 }
1713}
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001714
1715/* Converts a private to a public key */
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001716Key *
Damien Millerf58b58c2003-11-17 21:18:23 +11001717key_demote(const Key *k)
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001718{
1719 Key *pk;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001720
Damien Miller07d86be2006-03-26 14:19:21 +11001721 pk = xcalloc(1, sizeof(*pk));
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001722 pk->type = k->type;
1723 pk->flags = k->flags;
Damien Millereb8b60e2010-08-31 22:41:14 +10001724 pk->ecdsa_nid = k->ecdsa_nid;
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001725 pk->dsa = NULL;
Damien Millereb8b60e2010-08-31 22:41:14 +10001726 pk->ecdsa = NULL;
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001727 pk->rsa = NULL;
1728
1729 switch (k->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001730 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001731 case KEY_RSA_CERT:
1732 key_cert_copy(k, pk);
1733 /* FALLTHROUGH */
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001734 case KEY_RSA1:
1735 case KEY_RSA:
1736 if ((pk->rsa = RSA_new()) == NULL)
1737 fatal("key_demote: RSA_new failed");
1738 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
1739 fatal("key_demote: BN_dup failed");
1740 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
1741 fatal("key_demote: BN_dup failed");
1742 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001743 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001744 case KEY_DSA_CERT:
1745 key_cert_copy(k, pk);
1746 /* FALLTHROUGH */
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001747 case KEY_DSA:
1748 if ((pk->dsa = DSA_new()) == NULL)
1749 fatal("key_demote: DSA_new failed");
1750 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
1751 fatal("key_demote: BN_dup failed");
1752 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
1753 fatal("key_demote: BN_dup failed");
1754 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
1755 fatal("key_demote: BN_dup failed");
1756 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
1757 fatal("key_demote: BN_dup failed");
1758 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001759#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001760 case KEY_ECDSA_CERT:
1761 key_cert_copy(k, pk);
1762 /* FALLTHROUGH */
1763 case KEY_ECDSA:
1764 if ((pk->ecdsa = EC_KEY_new_by_curve_name(pk->ecdsa_nid)) == NULL)
1765 fatal("key_demote: EC_KEY_new_by_curve_name failed");
1766 if (EC_KEY_set_public_key(pk->ecdsa,
1767 EC_KEY_get0_public_key(k->ecdsa)) != 1)
1768 fatal("key_demote: EC_KEY_set_public_key failed");
1769 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001770#endif
Ben Lindstroma674e8d2002-03-22 01:45:53 +00001771 default:
1772 fatal("key_free: bad key type %d", k->type);
1773 break;
1774 }
1775
1776 return (pk);
1777}
Damien Miller0a80ca12010-02-27 07:55:05 +11001778
1779int
1780key_is_cert(const Key *k)
1781{
Damien Miller4e270b02010-04-16 15:56:21 +10001782 if (k == NULL)
1783 return 0;
Damien Miller4a3a9d42013-10-30 22:19:47 +11001784 return key_type_is_cert(k->type);
Damien Miller0a80ca12010-02-27 07:55:05 +11001785}
1786
1787/* Return the cert-less equivalent to a certified key type */
1788int
1789key_type_plain(int type)
1790{
1791 switch (type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001792 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001793 case KEY_RSA_CERT:
1794 return KEY_RSA;
Damien Miller4e270b02010-04-16 15:56:21 +10001795 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001796 case KEY_DSA_CERT:
1797 return KEY_DSA;
Damien Millereb8b60e2010-08-31 22:41:14 +10001798 case KEY_ECDSA_CERT:
1799 return KEY_ECDSA;
Damien Miller0a80ca12010-02-27 07:55:05 +11001800 default:
1801 return type;
1802 }
1803}
1804
1805/* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */
1806int
Damien Miller4e270b02010-04-16 15:56:21 +10001807key_to_certified(Key *k, int legacy)
Damien Miller0a80ca12010-02-27 07:55:05 +11001808{
1809 switch (k->type) {
1810 case KEY_RSA:
1811 k->cert = cert_new();
Damien Miller4e270b02010-04-16 15:56:21 +10001812 k->type = legacy ? KEY_RSA_CERT_V00 : KEY_RSA_CERT;
Damien Miller0a80ca12010-02-27 07:55:05 +11001813 return 0;
1814 case KEY_DSA:
1815 k->cert = cert_new();
Damien Miller4e270b02010-04-16 15:56:21 +10001816 k->type = legacy ? KEY_DSA_CERT_V00 : KEY_DSA_CERT;
Damien Miller0a80ca12010-02-27 07:55:05 +11001817 return 0;
Damien Millereb8b60e2010-08-31 22:41:14 +10001818 case KEY_ECDSA:
Damien Miller8f639fe2011-05-20 19:03:08 +10001819 if (legacy)
1820 fatal("%s: legacy ECDSA certificates are not supported",
1821 __func__);
Damien Millereb8b60e2010-08-31 22:41:14 +10001822 k->cert = cert_new();
1823 k->type = KEY_ECDSA_CERT;
1824 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001825 default:
1826 error("%s: key has incorrect type %s", __func__, key_type(k));
1827 return -1;
1828 }
1829}
1830
1831/* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */
1832int
1833key_drop_cert(Key *k)
1834{
1835 switch (k->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001836 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001837 case KEY_RSA_CERT:
1838 cert_free(k->cert);
1839 k->type = KEY_RSA;
1840 return 0;
Damien Miller4e270b02010-04-16 15:56:21 +10001841 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001842 case KEY_DSA_CERT:
1843 cert_free(k->cert);
1844 k->type = KEY_DSA;
1845 return 0;
Damien Millereb8b60e2010-08-31 22:41:14 +10001846 case KEY_ECDSA_CERT:
1847 cert_free(k->cert);
1848 k->type = KEY_ECDSA;
1849 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001850 default:
1851 error("%s: key has incorrect type %s", __func__, key_type(k));
1852 return -1;
1853 }
1854}
1855
Damien Millereb8b60e2010-08-31 22:41:14 +10001856/*
1857 * Sign a KEY_RSA_CERT, KEY_DSA_CERT or KEY_ECDSA_CERT, (re-)generating
1858 * the signed certblob
1859 */
Damien Miller0a80ca12010-02-27 07:55:05 +11001860int
1861key_certify(Key *k, Key *ca)
1862{
1863 Buffer principals;
1864 u_char *ca_blob, *sig_blob, nonce[32];
1865 u_int i, ca_len, sig_len;
1866
1867 if (k->cert == NULL) {
1868 error("%s: key lacks cert info", __func__);
1869 return -1;
1870 }
1871
1872 if (!key_is_cert(k)) {
1873 error("%s: certificate has unknown type %d", __func__,
1874 k->cert->type);
1875 return -1;
1876 }
1877
Damien Millereb8b60e2010-08-31 22:41:14 +10001878 if (ca->type != KEY_RSA && ca->type != KEY_DSA &&
1879 ca->type != KEY_ECDSA) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001880 error("%s: CA key has unsupported type %s", __func__,
1881 key_type(ca));
1882 return -1;
1883 }
1884
1885 key_to_blob(ca, &ca_blob, &ca_len);
1886
1887 buffer_clear(&k->cert->certblob);
1888 buffer_put_cstring(&k->cert->certblob, key_ssh_name(k));
1889
Damien Miller4e270b02010-04-16 15:56:21 +10001890 /* -v01 certs put nonce first */
Damien Miller0a5f0122011-02-04 11:47:01 +11001891 arc4random_buf(&nonce, sizeof(nonce));
1892 if (!key_cert_is_legacy(k))
Damien Miller4e270b02010-04-16 15:56:21 +10001893 buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce));
Damien Miller4e270b02010-04-16 15:56:21 +10001894
Damien Miller0a80ca12010-02-27 07:55:05 +11001895 switch (k->type) {
Damien Miller4e270b02010-04-16 15:56:21 +10001896 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001897 case KEY_DSA_CERT:
1898 buffer_put_bignum2(&k->cert->certblob, k->dsa->p);
1899 buffer_put_bignum2(&k->cert->certblob, k->dsa->q);
1900 buffer_put_bignum2(&k->cert->certblob, k->dsa->g);
1901 buffer_put_bignum2(&k->cert->certblob, k->dsa->pub_key);
1902 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001903#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001904 case KEY_ECDSA_CERT:
1905 buffer_put_cstring(&k->cert->certblob,
1906 key_curve_nid_to_name(k->ecdsa_nid));
1907 buffer_put_ecpoint(&k->cert->certblob,
1908 EC_KEY_get0_group(k->ecdsa),
1909 EC_KEY_get0_public_key(k->ecdsa));
1910 break;
Damien Miller6af914a2010-09-10 11:39:26 +10001911#endif
Damien Miller4e270b02010-04-16 15:56:21 +10001912 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +11001913 case KEY_RSA_CERT:
1914 buffer_put_bignum2(&k->cert->certblob, k->rsa->e);
1915 buffer_put_bignum2(&k->cert->certblob, k->rsa->n);
1916 break;
1917 default:
1918 error("%s: key has incorrect type %s", __func__, key_type(k));
1919 buffer_clear(&k->cert->certblob);
Darren Tuckera627d422013-06-02 07:31:17 +10001920 free(ca_blob);
Damien Miller0a80ca12010-02-27 07:55:05 +11001921 return -1;
1922 }
1923
Damien Miller4e270b02010-04-16 15:56:21 +10001924 /* -v01 certs have a serial number next */
Damien Millereb8b60e2010-08-31 22:41:14 +10001925 if (!key_cert_is_legacy(k))
Damien Miller4e270b02010-04-16 15:56:21 +10001926 buffer_put_int64(&k->cert->certblob, k->cert->serial);
1927
Damien Miller0a80ca12010-02-27 07:55:05 +11001928 buffer_put_int(&k->cert->certblob, k->cert->type);
1929 buffer_put_cstring(&k->cert->certblob, k->cert->key_id);
1930
1931 buffer_init(&principals);
1932 for (i = 0; i < k->cert->nprincipals; i++)
1933 buffer_put_cstring(&principals, k->cert->principals[i]);
1934 buffer_put_string(&k->cert->certblob, buffer_ptr(&principals),
1935 buffer_len(&principals));
1936 buffer_free(&principals);
1937
1938 buffer_put_int64(&k->cert->certblob, k->cert->valid_after);
1939 buffer_put_int64(&k->cert->certblob, k->cert->valid_before);
1940 buffer_put_string(&k->cert->certblob,
Damien Miller4e270b02010-04-16 15:56:21 +10001941 buffer_ptr(&k->cert->critical), buffer_len(&k->cert->critical));
Damien Miller0a80ca12010-02-27 07:55:05 +11001942
Damien Miller4e270b02010-04-16 15:56:21 +10001943 /* -v01 certs have non-critical options here */
Damien Millereb8b60e2010-08-31 22:41:14 +10001944 if (!key_cert_is_legacy(k)) {
Damien Miller4e270b02010-04-16 15:56:21 +10001945 buffer_put_string(&k->cert->certblob,
1946 buffer_ptr(&k->cert->extensions),
1947 buffer_len(&k->cert->extensions));
1948 }
1949
1950 /* -v00 certs put the nonce at the end */
Damien Millereb8b60e2010-08-31 22:41:14 +10001951 if (key_cert_is_legacy(k))
Damien Miller4e270b02010-04-16 15:56:21 +10001952 buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce));
1953
Damien Miller0a80ca12010-02-27 07:55:05 +11001954 buffer_put_string(&k->cert->certblob, NULL, 0); /* reserved */
1955 buffer_put_string(&k->cert->certblob, ca_blob, ca_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001956 free(ca_blob);
Damien Miller0a80ca12010-02-27 07:55:05 +11001957
1958 /* Sign the whole mess */
1959 if (key_sign(ca, &sig_blob, &sig_len, buffer_ptr(&k->cert->certblob),
1960 buffer_len(&k->cert->certblob)) != 0) {
1961 error("%s: signature operation failed", __func__);
1962 buffer_clear(&k->cert->certblob);
1963 return -1;
1964 }
1965 /* Append signature and we are done */
1966 buffer_put_string(&k->cert->certblob, sig_blob, sig_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001967 free(sig_blob);
Damien Miller0a80ca12010-02-27 07:55:05 +11001968
1969 return 0;
1970}
1971
1972int
1973key_cert_check_authority(const Key *k, int want_host, int require_principal,
1974 const char *name, const char **reason)
1975{
1976 u_int i, principal_matches;
1977 time_t now = time(NULL);
1978
1979 if (want_host) {
1980 if (k->cert->type != SSH2_CERT_TYPE_HOST) {
1981 *reason = "Certificate invalid: not a host certificate";
1982 return -1;
1983 }
1984 } else {
1985 if (k->cert->type != SSH2_CERT_TYPE_USER) {
1986 *reason = "Certificate invalid: not a user certificate";
1987 return -1;
1988 }
1989 }
1990 if (now < 0) {
1991 error("%s: system clock lies before epoch", __func__);
1992 *reason = "Certificate invalid: not yet valid";
1993 return -1;
1994 }
1995 if ((u_int64_t)now < k->cert->valid_after) {
1996 *reason = "Certificate invalid: not yet valid";
1997 return -1;
1998 }
1999 if ((u_int64_t)now >= k->cert->valid_before) {
2000 *reason = "Certificate invalid: expired";
2001 return -1;
2002 }
2003 if (k->cert->nprincipals == 0) {
2004 if (require_principal) {
2005 *reason = "Certificate lacks principal list";
2006 return -1;
2007 }
Damien Miller30da3442010-05-10 11:58:03 +10002008 } else if (name != NULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002009 principal_matches = 0;
2010 for (i = 0; i < k->cert->nprincipals; i++) {
2011 if (strcmp(name, k->cert->principals[i]) == 0) {
2012 principal_matches = 1;
2013 break;
2014 }
2015 }
2016 if (!principal_matches) {
2017 *reason = "Certificate invalid: name is not a listed "
2018 "principal";
2019 return -1;
2020 }
2021 }
2022 return 0;
2023}
Damien Miller4e270b02010-04-16 15:56:21 +10002024
2025int
Damien Millerf3747bf2013-01-18 11:44:04 +11002026key_cert_is_legacy(const Key *k)
Damien Miller4e270b02010-04-16 15:56:21 +10002027{
2028 switch (k->type) {
2029 case KEY_DSA_CERT_V00:
2030 case KEY_RSA_CERT_V00:
2031 return 1;
2032 default:
2033 return 0;
2034 }
2035}
Damien Millereb8b60e2010-08-31 22:41:14 +10002036
Damien Miller041ab7c2010-09-10 11:23:34 +10002037/* XXX: these are really begging for a table-driven approach */
Damien Millereb8b60e2010-08-31 22:41:14 +10002038int
2039key_curve_name_to_nid(const char *name)
2040{
Damien Miller6af914a2010-09-10 11:39:26 +10002041#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10002042 if (strcmp(name, "nistp256") == 0)
2043 return NID_X9_62_prime256v1;
2044 else if (strcmp(name, "nistp384") == 0)
2045 return NID_secp384r1;
Darren Tucker37bcef52013-11-09 18:39:25 +11002046# ifdef OPENSSL_HAS_NISTP521
Damien Millereb8b60e2010-08-31 22:41:14 +10002047 else if (strcmp(name, "nistp521") == 0)
2048 return NID_secp521r1;
Darren Tucker37bcef52013-11-09 18:39:25 +11002049# endif
Damien Miller6af914a2010-09-10 11:39:26 +10002050#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10002051
2052 debug("%s: unsupported EC curve name \"%.100s\"", __func__, name);
2053 return -1;
2054}
2055
Damien Miller041ab7c2010-09-10 11:23:34 +10002056u_int
2057key_curve_nid_to_bits(int nid)
2058{
2059 switch (nid) {
Damien Miller6af914a2010-09-10 11:39:26 +10002060#ifdef OPENSSL_HAS_ECC
Damien Miller041ab7c2010-09-10 11:23:34 +10002061 case NID_X9_62_prime256v1:
2062 return 256;
2063 case NID_secp384r1:
2064 return 384;
Darren Tucker2c894302013-11-10 12:38:42 +11002065# ifdef OPENSSL_HAS_NISTP521
Damien Miller041ab7c2010-09-10 11:23:34 +10002066 case NID_secp521r1:
2067 return 521;
Darren Tucker37bcef52013-11-09 18:39:25 +11002068# endif
Damien Miller6af914a2010-09-10 11:39:26 +10002069#endif
Damien Miller041ab7c2010-09-10 11:23:34 +10002070 default:
2071 error("%s: unsupported EC curve nid %d", __func__, nid);
2072 return 0;
2073 }
2074}
2075
Damien Millereb8b60e2010-08-31 22:41:14 +10002076const char *
2077key_curve_nid_to_name(int nid)
2078{
Damien Miller6af914a2010-09-10 11:39:26 +10002079#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10002080 if (nid == NID_X9_62_prime256v1)
2081 return "nistp256";
2082 else if (nid == NID_secp384r1)
2083 return "nistp384";
Darren Tucker37bcef52013-11-09 18:39:25 +11002084# ifdef OPENSSL_HAS_NISTP521
Damien Millereb8b60e2010-08-31 22:41:14 +10002085 else if (nid == NID_secp521r1)
2086 return "nistp521";
Darren Tucker37bcef52013-11-09 18:39:25 +11002087# endif
Damien Miller6af914a2010-09-10 11:39:26 +10002088#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10002089 error("%s: unsupported EC curve nid %d", __func__, nid);
2090 return NULL;
2091}
2092
Damien Miller6af914a2010-09-10 11:39:26 +10002093#ifdef OPENSSL_HAS_ECC
Damien Miller041ab7c2010-09-10 11:23:34 +10002094const EVP_MD *
2095key_ec_nid_to_evpmd(int nid)
2096{
2097 int kbits = key_curve_nid_to_bits(nid);
2098
2099 if (kbits == 0)
2100 fatal("%s: invalid nid %d", __func__, nid);
2101 /* RFC5656 section 6.2.1 */
2102 if (kbits <= 256)
2103 return EVP_sha256();
2104 else if (kbits <= 384)
2105 return EVP_sha384();
2106 else
2107 return EVP_sha512();
2108}
2109
Damien Millereb8b60e2010-08-31 22:41:14 +10002110int
2111key_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
2112{
2113 BN_CTX *bnctx;
2114 EC_POINT *nq = NULL;
2115 BIGNUM *order, *x, *y, *tmp;
2116 int ret = -1;
2117
2118 if ((bnctx = BN_CTX_new()) == NULL)
2119 fatal("%s: BN_CTX_new failed", __func__);
2120 BN_CTX_start(bnctx);
2121
2122 /*
2123 * We shouldn't ever hit this case because bignum_get_ecpoint()
2124 * refuses to load GF2m points.
2125 */
2126 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2127 NID_X9_62_prime_field) {
2128 error("%s: group is not a prime field", __func__);
2129 goto out;
2130 }
2131
2132 /* Q != infinity */
2133 if (EC_POINT_is_at_infinity(group, public)) {
2134 error("%s: received degenerate public key (infinity)",
2135 __func__);
2136 goto out;
2137 }
2138
2139 if ((x = BN_CTX_get(bnctx)) == NULL ||
2140 (y = BN_CTX_get(bnctx)) == NULL ||
2141 (order = BN_CTX_get(bnctx)) == NULL ||
2142 (tmp = BN_CTX_get(bnctx)) == NULL)
2143 fatal("%s: BN_CTX_get failed", __func__);
2144
2145 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2146 if (EC_GROUP_get_order(group, order, bnctx) != 1)
2147 fatal("%s: EC_GROUP_get_order failed", __func__);
2148 if (EC_POINT_get_affine_coordinates_GFp(group, public,
2149 x, y, bnctx) != 1)
2150 fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__);
2151 if (BN_num_bits(x) <= BN_num_bits(order) / 2) {
2152 error("%s: public key x coordinate too small: "
2153 "bits(x) = %d, bits(order)/2 = %d", __func__,
2154 BN_num_bits(x), BN_num_bits(order) / 2);
2155 goto out;
2156 }
2157 if (BN_num_bits(y) <= BN_num_bits(order) / 2) {
2158 error("%s: public key y coordinate too small: "
2159 "bits(y) = %d, bits(order)/2 = %d", __func__,
2160 BN_num_bits(x), BN_num_bits(order) / 2);
2161 goto out;
2162 }
2163
2164 /* nQ == infinity (n == order of subgroup) */
2165 if ((nq = EC_POINT_new(group)) == NULL)
2166 fatal("%s: BN_CTX_tmp failed", __func__);
2167 if (EC_POINT_mul(group, nq, NULL, public, order, bnctx) != 1)
2168 fatal("%s: EC_GROUP_mul failed", __func__);
2169 if (EC_POINT_is_at_infinity(group, nq) != 1) {
2170 error("%s: received degenerate public key (nQ != infinity)",
2171 __func__);
2172 goto out;
2173 }
2174
2175 /* x < order - 1, y < order - 1 */
2176 if (!BN_sub(tmp, order, BN_value_one()))
2177 fatal("%s: BN_sub failed", __func__);
2178 if (BN_cmp(x, tmp) >= 0) {
2179 error("%s: public key x coordinate >= group order - 1",
2180 __func__);
2181 goto out;
2182 }
2183 if (BN_cmp(y, tmp) >= 0) {
2184 error("%s: public key y coordinate >= group order - 1",
2185 __func__);
2186 goto out;
2187 }
2188 ret = 0;
2189 out:
2190 BN_CTX_free(bnctx);
2191 EC_POINT_free(nq);
2192 return ret;
2193}
2194
2195int
2196key_ec_validate_private(const EC_KEY *key)
2197{
2198 BN_CTX *bnctx;
2199 BIGNUM *order, *tmp;
2200 int ret = -1;
2201
2202 if ((bnctx = BN_CTX_new()) == NULL)
2203 fatal("%s: BN_CTX_new failed", __func__);
2204 BN_CTX_start(bnctx);
2205
2206 if ((order = BN_CTX_get(bnctx)) == NULL ||
2207 (tmp = BN_CTX_get(bnctx)) == NULL)
2208 fatal("%s: BN_CTX_get failed", __func__);
2209
2210 /* log2(private) > log2(order)/2 */
2211 if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, bnctx) != 1)
2212 fatal("%s: EC_GROUP_get_order failed", __func__);
2213 if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
2214 BN_num_bits(order) / 2) {
2215 error("%s: private key too small: "
2216 "bits(y) = %d, bits(order)/2 = %d", __func__,
2217 BN_num_bits(EC_KEY_get0_private_key(key)),
2218 BN_num_bits(order) / 2);
2219 goto out;
2220 }
2221
2222 /* private < order - 1 */
2223 if (!BN_sub(tmp, order, BN_value_one()))
2224 fatal("%s: BN_sub failed", __func__);
2225 if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0) {
2226 error("%s: private key >= group order - 1", __func__);
2227 goto out;
2228 }
2229 ret = 0;
2230 out:
2231 BN_CTX_free(bnctx);
2232 return ret;
2233}
2234
2235#if defined(DEBUG_KEXECDH) || defined(DEBUG_PK)
2236void
2237key_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
2238{
2239 BIGNUM *x, *y;
2240 BN_CTX *bnctx;
2241
2242 if (point == NULL) {
2243 fputs("point=(NULL)\n", stderr);
2244 return;
2245 }
2246 if ((bnctx = BN_CTX_new()) == NULL)
2247 fatal("%s: BN_CTX_new failed", __func__);
2248 BN_CTX_start(bnctx);
2249 if ((x = BN_CTX_get(bnctx)) == NULL || (y = BN_CTX_get(bnctx)) == NULL)
2250 fatal("%s: BN_CTX_get failed", __func__);
2251 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2252 NID_X9_62_prime_field)
2253 fatal("%s: group is not a prime field", __func__);
2254 if (EC_POINT_get_affine_coordinates_GFp(group, point, x, y, bnctx) != 1)
2255 fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__);
2256 fputs("x=", stderr);
2257 BN_print_fp(stderr, x);
2258 fputs("\ny=", stderr);
2259 BN_print_fp(stderr, y);
2260 fputs("\n", stderr);
2261 BN_CTX_free(bnctx);
2262}
2263
2264void
2265key_dump_ec_key(const EC_KEY *key)
2266{
2267 const BIGNUM *exponent;
2268
2269 key_dump_ec_point(EC_KEY_get0_group(key), EC_KEY_get0_public_key(key));
2270 fputs("exponent=", stderr);
2271 if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
2272 fputs("(NULL)", stderr);
2273 else
2274 BN_print_fp(stderr, EC_KEY_get0_private_key(key));
2275 fputs("\n", stderr);
2276}
2277#endif /* defined(DEBUG_KEXECDH) || defined(DEBUG_PK) */
Damien Miller6af914a2010-09-10 11:39:26 +10002278#endif /* OPENSSL_HAS_ECC */
Damien Millerf0e90602013-12-07 10:40:26 +11002279
2280void
2281key_private_serialize(const Key *key, Buffer *b)
2282{
2283 buffer_put_cstring(b, key_ssh_name(key));
2284 switch (key->type) {
2285 case KEY_RSA:
2286 buffer_put_bignum2(b, key->rsa->n);
2287 buffer_put_bignum2(b, key->rsa->e);
2288 buffer_put_bignum2(b, key->rsa->d);
2289 buffer_put_bignum2(b, key->rsa->iqmp);
2290 buffer_put_bignum2(b, key->rsa->p);
2291 buffer_put_bignum2(b, key->rsa->q);
2292 break;
2293 case KEY_RSA_CERT_V00:
2294 case KEY_RSA_CERT:
2295 if (key->cert == NULL || buffer_len(&key->cert->certblob) == 0)
2296 fatal("%s: no cert/certblob", __func__);
2297 buffer_put_string(b, buffer_ptr(&key->cert->certblob),
2298 buffer_len(&key->cert->certblob));
2299 buffer_put_bignum2(b, key->rsa->d);
2300 buffer_put_bignum2(b, key->rsa->iqmp);
2301 buffer_put_bignum2(b, key->rsa->p);
2302 buffer_put_bignum2(b, key->rsa->q);
2303 break;
2304 case KEY_DSA:
2305 buffer_put_bignum2(b, key->dsa->p);
2306 buffer_put_bignum2(b, key->dsa->q);
2307 buffer_put_bignum2(b, key->dsa->g);
2308 buffer_put_bignum2(b, key->dsa->pub_key);
2309 buffer_put_bignum2(b, key->dsa->priv_key);
2310 break;
2311 case KEY_DSA_CERT_V00:
2312 case KEY_DSA_CERT:
2313 if (key->cert == NULL || buffer_len(&key->cert->certblob) == 0)
2314 fatal("%s: no cert/certblob", __func__);
2315 buffer_put_string(b, buffer_ptr(&key->cert->certblob),
2316 buffer_len(&key->cert->certblob));
2317 buffer_put_bignum2(b, key->dsa->priv_key);
2318 break;
2319#ifdef OPENSSL_HAS_ECC
2320 case KEY_ECDSA:
2321 buffer_put_cstring(b, key_curve_nid_to_name(key->ecdsa_nid));
2322 buffer_put_ecpoint(b, EC_KEY_get0_group(key->ecdsa),
2323 EC_KEY_get0_public_key(key->ecdsa));
2324 buffer_put_bignum2(b, EC_KEY_get0_private_key(key->ecdsa));
2325 break;
2326 case KEY_ECDSA_CERT:
2327 if (key->cert == NULL || buffer_len(&key->cert->certblob) == 0)
2328 fatal("%s: no cert/certblob", __func__);
2329 buffer_put_string(b, buffer_ptr(&key->cert->certblob),
2330 buffer_len(&key->cert->certblob));
2331 buffer_put_bignum2(b, EC_KEY_get0_private_key(key->ecdsa));
2332 break;
2333#endif /* OPENSSL_HAS_ECC */
2334 }
2335}
2336
2337Key *
2338key_private_deserialize(Buffer *blob)
2339{
2340 char *type_name;
2341 Key *k = NULL;
2342 u_char *cert;
2343 u_int len;
2344 int type;
2345#ifdef OPENSSL_HAS_ECC
2346 char *curve;
2347 BIGNUM *exponent;
2348 EC_POINT *q;
2349#endif
2350
2351 type_name = buffer_get_string(blob, NULL);
2352 type = key_type_from_name(type_name);
2353 switch (type) {
2354 case KEY_DSA:
2355 k = key_new_private(type);
2356 buffer_get_bignum2(blob, k->dsa->p);
2357 buffer_get_bignum2(blob, k->dsa->q);
2358 buffer_get_bignum2(blob, k->dsa->g);
2359 buffer_get_bignum2(blob, k->dsa->pub_key);
2360 buffer_get_bignum2(blob, k->dsa->priv_key);
2361 break;
2362 case KEY_DSA_CERT_V00:
2363 case KEY_DSA_CERT:
2364 cert = buffer_get_string(blob, &len);
2365 if ((k = key_from_blob(cert, len)) == NULL)
2366 fatal("Certificate parse failed");
2367 free(cert);
2368 key_add_private(k);
2369 buffer_get_bignum2(blob, k->dsa->priv_key);
2370 break;
2371#ifdef OPENSSL_HAS_ECC
2372 case KEY_ECDSA:
2373 k = key_new_private(type);
2374 k->ecdsa_nid = key_ecdsa_nid_from_name(type_name);
2375 curve = buffer_get_string(blob, NULL);
2376 if (k->ecdsa_nid != key_curve_name_to_nid(curve))
2377 fatal("%s: curve names mismatch", __func__);
2378 free(curve);
2379 k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
2380 if (k->ecdsa == NULL)
2381 fatal("%s: EC_KEY_new_by_curve_name failed",
2382 __func__);
2383 q = EC_POINT_new(EC_KEY_get0_group(k->ecdsa));
2384 if (q == NULL)
2385 fatal("%s: BN_new failed", __func__);
2386 if ((exponent = BN_new()) == NULL)
2387 fatal("%s: BN_new failed", __func__);
2388 buffer_get_ecpoint(blob,
2389 EC_KEY_get0_group(k->ecdsa), q);
2390 buffer_get_bignum2(blob, exponent);
2391 if (EC_KEY_set_public_key(k->ecdsa, q) != 1)
2392 fatal("%s: EC_KEY_set_public_key failed",
2393 __func__);
2394 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1)
2395 fatal("%s: EC_KEY_set_private_key failed",
2396 __func__);
2397 if (key_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
2398 EC_KEY_get0_public_key(k->ecdsa)) != 0)
2399 fatal("%s: bad ECDSA public key", __func__);
2400 if (key_ec_validate_private(k->ecdsa) != 0)
2401 fatal("%s: bad ECDSA private key", __func__);
2402 BN_clear_free(exponent);
2403 EC_POINT_free(q);
2404 break;
2405 case KEY_ECDSA_CERT:
2406 cert = buffer_get_string(blob, &len);
2407 if ((k = key_from_blob(cert, len)) == NULL)
2408 fatal("Certificate parse failed");
2409 free(cert);
2410 key_add_private(k);
2411 if ((exponent = BN_new()) == NULL)
2412 fatal("%s: BN_new failed", __func__);
2413 buffer_get_bignum2(blob, exponent);
2414 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1)
2415 fatal("%s: EC_KEY_set_private_key failed",
2416 __func__);
2417 if (key_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
2418 EC_KEY_get0_public_key(k->ecdsa)) != 0 ||
2419 key_ec_validate_private(k->ecdsa) != 0)
2420 fatal("%s: bad ECDSA key", __func__);
2421 BN_clear_free(exponent);
2422 break;
2423#endif
2424 case KEY_RSA:
2425 k = key_new_private(type);
2426 buffer_get_bignum2(blob, k->rsa->n);
2427 buffer_get_bignum2(blob, k->rsa->e);
2428 buffer_get_bignum2(blob, k->rsa->d);
2429 buffer_get_bignum2(blob, k->rsa->iqmp);
2430 buffer_get_bignum2(blob, k->rsa->p);
2431 buffer_get_bignum2(blob, k->rsa->q);
2432
2433 /* Generate additional parameters */
2434 rsa_generate_additional_parameters(k->rsa);
2435 break;
2436 case KEY_RSA_CERT_V00:
2437 case KEY_RSA_CERT:
2438 cert = buffer_get_string(blob, &len);
2439 if ((k = key_from_blob(cert, len)) == NULL)
2440 fatal("Certificate parse failed");
2441 free(cert);
2442 key_add_private(k);
2443 buffer_get_bignum2(blob, k->rsa->d);
2444 buffer_get_bignum2(blob, k->rsa->iqmp);
2445 buffer_get_bignum2(blob, k->rsa->p);
2446 buffer_get_bignum2(blob, k->rsa->q);
2447 break;
2448 default:
2449 free(type_name);
2450 buffer_clear(blob);
2451 return NULL;
2452 }
2453 free(type_name);
2454
2455 /* enable blinding */
2456 switch (k->type) {
2457 case KEY_RSA:
2458 case KEY_RSA_CERT_V00:
2459 case KEY_RSA_CERT:
2460 case KEY_RSA1:
2461 if (RSA_blinding_on(k->rsa, NULL) != 1) {
2462 error("%s: RSA_blinding_on failed", __func__);
2463 key_free(k);
2464 return NULL;
2465 }
2466 break;
2467 }
2468 return k;
2469}