Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 1 | /* $OpenBSD: key.c,v 1.105 2013/10/29 09:42:11 djm Exp $ */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 3 | * read_bignum(): |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * |
| 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
| 11 | * |
| 12 | * |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 13 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Darren Tucker | 0f0ef0a | 2008-06-13 08:58:05 +1000 | [diff] [blame] | 14 | * Copyright (c) 2008 Alexander von Gernler. All rights reserved. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * 1. Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 36 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 37 | #include "includes.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 38 | |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 39 | #include <sys/param.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 42 | #include <openssl/evp.h> |
Darren Tucker | 3d295a6 | 2008-02-28 19:22:04 +1100 | [diff] [blame] | 43 | #include <openbsd-compat/openssl-compat.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 44 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 45 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 46 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 47 | #include <string.h> |
| 48 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 49 | #include "xmalloc.h" |
| 50 | #include "key.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 51 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | #include "uuencode.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 53 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 54 | #include "log.h" |
Damien Miller | 8a0268f | 2010-07-16 13:57:51 +1000 | [diff] [blame] | 55 | #include "misc.h" |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 56 | #include "ssh2.h" |
| 57 | |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 58 | static int to_blob(const Key *, u_char **, u_int *, int); |
Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 59 | static Key *key_from_blob2(const u_char *, u_int, int); |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 60 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 61 | static struct KeyCert * |
| 62 | cert_new(void) |
| 63 | { |
| 64 | struct KeyCert *cert; |
| 65 | |
| 66 | cert = xcalloc(1, sizeof(*cert)); |
| 67 | buffer_init(&cert->certblob); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 68 | buffer_init(&cert->critical); |
| 69 | buffer_init(&cert->extensions); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 70 | cert->key_id = NULL; |
| 71 | cert->principals = NULL; |
| 72 | cert->signature_key = NULL; |
| 73 | return cert; |
| 74 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 75 | |
| 76 | Key * |
| 77 | key_new(int type) |
| 78 | { |
| 79 | Key *k; |
| 80 | RSA *rsa; |
| 81 | DSA *dsa; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 82 | k = xcalloc(1, sizeof(*k)); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 83 | k->type = type; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 84 | k->ecdsa = NULL; |
| 85 | k->ecdsa_nid = -1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 86 | k->dsa = NULL; |
| 87 | k->rsa = NULL; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 88 | k->cert = NULL; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 89 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 90 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 91 | case KEY_RSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 92 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 93 | case KEY_RSA_CERT: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 94 | 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 Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 100 | k->rsa = rsa; |
| 101 | break; |
| 102 | case KEY_DSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 103 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 104 | case KEY_DSA_CERT: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 105 | 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 Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 115 | k->dsa = dsa; |
| 116 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 117 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 118 | case KEY_ECDSA: |
| 119 | case KEY_ECDSA_CERT: |
| 120 | /* Cannot do anything until we know the group */ |
| 121 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 122 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 123 | case KEY_UNSPEC: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 124 | break; |
| 125 | default: |
| 126 | fatal("key_new: bad key type %d", k->type); |
| 127 | break; |
| 128 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 129 | |
| 130 | if (key_is_cert(k)) |
| 131 | k->cert = cert_new(); |
| 132 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 133 | return k; |
| 134 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 135 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 136 | void |
| 137 | key_add_private(Key *k) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 138 | { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 139 | switch (k->type) { |
| 140 | case KEY_RSA1: |
| 141 | case KEY_RSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 142 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 143 | case KEY_RSA_CERT: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 144 | 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 Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 156 | break; |
| 157 | case KEY_DSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 158 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 159 | case KEY_DSA_CERT: |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 160 | if ((k->dsa->priv_key = BN_new()) == NULL) |
| 161 | fatal("key_new_private: BN_new failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 162 | break; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 163 | case KEY_ECDSA: |
| 164 | case KEY_ECDSA_CERT: |
| 165 | /* Cannot do anything until we know the group */ |
| 166 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 167 | case KEY_UNSPEC: |
| 168 | break; |
| 169 | default: |
| 170 | break; |
| 171 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Key * |
| 175 | key_new_private(int type) |
| 176 | { |
| 177 | Key *k = key_new(type); |
| 178 | |
| 179 | key_add_private(k); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 180 | return k; |
| 181 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 182 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 183 | static void |
| 184 | cert_free(struct KeyCert *cert) |
| 185 | { |
| 186 | u_int i; |
| 187 | |
| 188 | buffer_free(&cert->certblob); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 189 | buffer_free(&cert->critical); |
| 190 | buffer_free(&cert->extensions); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 191 | free(cert->key_id); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 192 | for (i = 0; i < cert->nprincipals; i++) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 193 | free(cert->principals[i]); |
| 194 | free(cert->principals); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 195 | if (cert->signature_key != NULL) |
| 196 | key_free(cert->signature_key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 197 | free(cert); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 198 | } |
| 199 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 200 | void |
| 201 | key_free(Key *k) |
| 202 | { |
Damien Miller | 429fcc2 | 2006-03-26 14:02:16 +1100 | [diff] [blame] | 203 | if (k == NULL) |
Damien Miller | bbaad77 | 2006-03-26 14:03:03 +1100 | [diff] [blame] | 204 | fatal("key_free: key is NULL"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 205 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 206 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 207 | case KEY_RSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 208 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 209 | case KEY_RSA_CERT: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 210 | if (k->rsa != NULL) |
| 211 | RSA_free(k->rsa); |
| 212 | k->rsa = NULL; |
| 213 | break; |
| 214 | case KEY_DSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 215 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 216 | case KEY_DSA_CERT: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 217 | if (k->dsa != NULL) |
| 218 | DSA_free(k->dsa); |
| 219 | k->dsa = NULL; |
| 220 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 221 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 222 | 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 Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 228 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 229 | case KEY_UNSPEC: |
| 230 | break; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 231 | default: |
| 232 | fatal("key_free: bad key type %d", k->type); |
| 233 | break; |
| 234 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 235 | if (key_is_cert(k)) { |
| 236 | if (k->cert != NULL) |
| 237 | cert_free(k->cert); |
| 238 | k->cert = NULL; |
| 239 | } |
| 240 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 241 | free(k); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 242 | } |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 243 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 244 | static int |
| 245 | cert_compare(struct KeyCert *a, struct KeyCert *b) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 246 | { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 247 | if (a == NULL && b == NULL) |
| 248 | return 1; |
| 249 | if (a == NULL || b == NULL) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 250 | return 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 251 | if (buffer_len(&a->certblob) != buffer_len(&b->certblob)) |
| 252 | return 0; |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 253 | if (timingsafe_bcmp(buffer_ptr(&a->certblob), buffer_ptr(&b->certblob), |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 254 | 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 | */ |
| 263 | int |
| 264 | key_equal_public(const Key *a, const Key *b) |
| 265 | { |
Darren Tucker | 8ccb739 | 2010-09-10 12:28:24 +1000 | [diff] [blame] | 266 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 267 | BN_CTX *bnctx; |
Darren Tucker | 8ccb739 | 2010-09-10 12:28:24 +1000 | [diff] [blame] | 268 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 269 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 270 | if (a == NULL || b == NULL || |
| 271 | key_type_plain(a->type) != key_type_plain(b->type)) |
| 272 | return 0; |
| 273 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 274 | switch (a->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 275 | case KEY_RSA1: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 276 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 277 | case KEY_RSA_CERT: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 278 | 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 Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 282 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 283 | case KEY_DSA_CERT: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 284 | 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 Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 290 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 291 | 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 Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 309 | #endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 310 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 311 | fatal("key_equal: bad key type %d", a->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 312 | } |
Damien Miller | 87dd5f2 | 2008-07-11 17:35:09 +1000 | [diff] [blame] | 313 | /* NOTREACHED */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 314 | } |
| 315 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 316 | int |
| 317 | key_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 Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 328 | u_char* |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 329 | key_fingerprint_raw(const Key *k, enum fp_type dgst_type, |
| 330 | u_int *dgst_raw_length) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 331 | { |
Ben Lindstrom | 80cb27d | 2002-03-05 01:33:36 +0000 | [diff] [blame] | 332 | const EVP_MD *md = NULL; |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 333 | EVP_MD_CTX ctx; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 334 | u_char *blob = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 335 | u_char *retval = NULL; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 336 | u_int len = 0; |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 337 | int nlen, elen; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 338 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 339 | *dgst_raw_length = 0; |
| 340 | |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 341 | 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 Tucker | 14a9d25 | 2012-06-30 20:05:02 +1000 | [diff] [blame] | 348 | #ifdef HAVE_EVP_SHA256 |
Damien Miller | 3bde12a | 2012-06-20 21:51:11 +1000 | [diff] [blame] | 349 | case SSH_FP_SHA256: |
| 350 | md = EVP_sha256(); |
| 351 | break; |
Darren Tucker | 14a9d25 | 2012-06-30 20:05:02 +1000 | [diff] [blame] | 352 | #endif |
Ben Lindstrom | f0b4853 | 2001-03-12 02:59:31 +0000 | [diff] [blame] | 353 | default: |
| 354 | fatal("key_fingerprint_raw: bad digest type %d", |
| 355 | dgst_type); |
| 356 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 357 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 358 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 359 | nlen = BN_num_bytes(k->rsa->n); |
| 360 | elen = BN_num_bytes(k->rsa->e); |
| 361 | len = nlen + elen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 362 | blob = xmalloc(len); |
| 363 | BN_bn2bin(k->rsa->n, blob); |
| 364 | BN_bn2bin(k->rsa->e, blob + nlen); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 365 | break; |
| 366 | case KEY_DSA: |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 367 | case KEY_ECDSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 368 | case KEY_RSA: |
| 369 | key_to_blob(k, &blob, &len); |
| 370 | break; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 371 | case KEY_DSA_CERT_V00: |
| 372 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 373 | case KEY_DSA_CERT: |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 374 | case KEY_ECDSA_CERT: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 375 | case KEY_RSA_CERT: |
| 376 | /* We want a fingerprint of the _key_ not of the cert */ |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 377 | to_blob(k, &blob, &len, 1); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 378 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 379 | case KEY_UNSPEC: |
| 380 | return retval; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 381 | default: |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 382 | fatal("key_fingerprint_raw: bad key type %d", k->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 383 | break; |
| 384 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 385 | if (blob != NULL) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 386 | retval = xmalloc(EVP_MAX_MD_SIZE); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 387 | EVP_DigestInit(&ctx, md); |
| 388 | EVP_DigestUpdate(&ctx, blob, len); |
Damien Miller | 3672e4b | 2002-02-05 11:54:07 +1100 | [diff] [blame] | 389 | EVP_DigestFinal(&ctx, retval, dgst_raw_length); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 390 | memset(blob, 0, len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 391 | free(blob); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 392 | } else { |
| 393 | fatal("key_fingerprint_raw: blob is null"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 394 | } |
| 395 | return retval; |
| 396 | } |
| 397 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 398 | static char * |
| 399 | key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 400 | { |
| 401 | char *retval; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 402 | u_int i; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 403 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 404 | retval = xcalloc(1, dgst_raw_len * 3 + 1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 405 | for (i = 0; i < dgst_raw_len; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 406 | char hex[4]; |
| 407 | snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]); |
Darren Tucker | 2958861 | 2003-07-14 17:28:34 +1000 | [diff] [blame] | 408 | strlcat(retval, hex, dgst_raw_len * 3 + 1); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 409 | } |
Darren Tucker | 2958861 | 2003-07-14 17:28:34 +1000 | [diff] [blame] | 410 | |
| 411 | /* Remove the trailing ':' character */ |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 412 | retval[(dgst_raw_len * 3) - 1] = '\0'; |
| 413 | return retval; |
| 414 | } |
| 415 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 416 | static char * |
| 417 | key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 418 | { |
| 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 Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 422 | u_int i, j = 0, rounds, seed = 1; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 423 | char *retval; |
| 424 | |
| 425 | rounds = (dgst_raw_len / 2) + 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 426 | retval = xcalloc((rounds * 6), sizeof(char)); |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 427 | retval[j++] = 'x'; |
| 428 | for (i = 0; i < rounds; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 429 | u_int idx0, idx1, idx2, idx3, idx4; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 430 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { |
| 431 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 432 | seed) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 433 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; |
| 434 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 435 | (seed / 6)) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 436 | 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 Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 445 | seed = ((seed * 5) + |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 446 | ((((u_int)(dgst_raw[2 * i])) * 7) + |
| 447 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 448 | } |
| 449 | } else { |
| 450 | idx0 = seed % 6; |
| 451 | idx1 = 16; |
| 452 | idx2 = seed / 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 453 | retval[j++] = vowels[idx0]; |
| 454 | retval[j++] = consonants[idx1]; |
| 455 | retval[j++] = vowels[idx2]; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame] | 458 | retval[j++] = 'x'; |
| 459 | retval[j++] = '\0'; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 460 | return retval; |
| 461 | } |
| 462 | |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 463 | /* |
| 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 Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 486 | |
| 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 Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 496 | static char * |
Darren Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 497 | key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k) |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 498 | { |
| 499 | /* |
| 500 | * Chars to be used after each other every time the worm |
| 501 | * intersects with itself. Matter of taste. |
| 502 | */ |
Darren Tucker | 4b3b977 | 2008-06-13 04:55:10 +1000 | [diff] [blame] | 503 | char *augmentation_string = " .o+=*BOX@%&#/^SE"; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 504 | char *retval, *p; |
Darren Tucker | 014d76f | 2008-06-13 04:43:51 +1000 | [diff] [blame] | 505 | u_char field[FLDSIZE_X][FLDSIZE_Y]; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 506 | u_int i, b; |
| 507 | int x, y; |
Darren Tucker | d32b28a | 2008-06-13 04:45:50 +1000 | [diff] [blame] | 508 | size_t len = strlen(augmentation_string) - 1; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 509 | |
| 510 | retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2)); |
| 511 | |
| 512 | /* initialize field */ |
Darren Tucker | 014d76f | 2008-06-13 04:43:51 +1000 | [diff] [blame] | 513 | memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char)); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 514 | x = FLDSIZE_X / 2; |
| 515 | y = FLDSIZE_Y / 2; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 516 | |
| 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 Miller | c6aadd9 | 2008-11-03 19:16:20 +1100 | [diff] [blame] | 534 | if (field[x][y] < len - 2) |
| 535 | field[x][y]++; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 536 | input = input >> 2; |
| 537 | } |
| 538 | } |
Darren Tucker | 4b3b977 | 2008-06-13 04:55:10 +1000 | [diff] [blame] | 539 | |
| 540 | /* mark starting point and end point*/ |
| 541 | field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1; |
| 542 | field[x][y] = len; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 543 | |
| 544 | /* fill in retval */ |
Damien Miller | 007132a | 2008-06-29 22:45:37 +1000 | [diff] [blame] | 545 | snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k)); |
Darren Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 546 | p = strchr(retval, '\0'); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 547 | |
| 548 | /* output upper border */ |
Damien Miller | 007132a | 2008-06-29 22:45:37 +1000 | [diff] [blame] | 549 | for (i = p - retval - 1; i < FLDSIZE_X; i++) |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 550 | *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 Tucker | d32b28a | 2008-06-13 04:45:50 +1000 | [diff] [blame] | 558 | *p++ = augmentation_string[MIN(field[x][y], len)]; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 559 | *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 Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 572 | char * |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 573 | key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep) |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 574 | { |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 575 | char *retval = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 576 | u_char *dgst_raw; |
Damien Miller | 3672e4b | 2002-02-05 11:54:07 +1100 | [diff] [blame] | 577 | u_int dgst_raw_len; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 578 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 579 | dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len); |
| 580 | if (!dgst_raw) |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 581 | fatal("key_fingerprint: null from key_fingerprint_raw()"); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 582 | switch (dgst_rep) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 583 | 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 Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 589 | case SSH_FP_RANDOMART: |
Darren Tucker | 987ac84 | 2008-06-13 04:54:40 +1000 | [diff] [blame] | 590 | retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 591 | break; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 592 | default: |
Damien Miller | 2f54ada | 2008-11-03 19:24:16 +1100 | [diff] [blame] | 593 | fatal("key_fingerprint: bad digest representation %d", |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 594 | dgst_rep); |
| 595 | break; |
| 596 | } |
| 597 | memset(dgst_raw, 0, dgst_raw_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 598 | free(dgst_raw); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 599 | return retval; |
| 600 | } |
| 601 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 602 | /* |
| 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 Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 609 | static int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 610 | read_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 Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 645 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 646 | static int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 647 | write_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 Miller | af3030f | 2001-10-10 15:00:49 +1000 | [diff] [blame] | 655 | OPENSSL_free(buf); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 656 | return 1; |
| 657 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 658 | |
Ben Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 659 | /* returns 1 ok, -1 error */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 660 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 661 | key_read(Key *ret, char **cpp) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 662 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 663 | Key *k; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 664 | int success = -1; |
| 665 | char *cp, *space; |
Darren Tucker | 8ccb739 | 2010-09-10 12:28:24 +1000 | [diff] [blame] | 666 | int len, n, type; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 667 | u_int bits; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 668 | u_char *blob; |
Darren Tucker | 8ccb739 | 2010-09-10 12:28:24 +1000 | [diff] [blame] | 669 | #ifdef OPENSSL_HAS_ECC |
| 670 | int curve_nid = -1; |
| 671 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 672 | |
| 673 | cp = *cpp; |
| 674 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 675 | switch (ret->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 676 | case KEY_RSA1: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 677 | /* Get number of bits. */ |
| 678 | if (*cp < '0' || *cp > '9') |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 679 | return -1; /* Bad bit count... */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 680 | for (bits = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 681 | bits = 10 * bits + *cp - '0'; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 682 | if (bits == 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 683 | return -1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 684 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 685 | /* Get public exponent, public modulus. */ |
| 686 | if (!read_bignum(cpp, ret->rsa->e)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 687 | return -1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 688 | if (!read_bignum(cpp, ret->rsa->n)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 689 | return -1; |
Darren Tucker | 561724f | 2010-01-13 22:43:05 +1100 | [diff] [blame] | 690 | /* 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 Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 696 | success = 1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 697 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 698 | case KEY_UNSPEC: |
| 699 | case KEY_RSA: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 700 | case KEY_DSA: |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 701 | case KEY_ECDSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 702 | case KEY_DSA_CERT_V00: |
| 703 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 704 | case KEY_DSA_CERT: |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 705 | case KEY_ECDSA_CERT: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 706 | case KEY_RSA_CERT: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 707 | space = strchr(cp, ' '); |
| 708 | if (space == NULL) { |
Damien Miller | 386f1f3 | 2003-02-24 11:54:57 +1100 | [diff] [blame] | 709 | debug3("key_read: missing whitespace"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 710 | return -1; |
| 711 | } |
| 712 | *space = '\0'; |
| 713 | type = key_type_from_name(cp); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 714 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 715 | 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 Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 720 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 721 | *space = ' '; |
| 722 | if (type == KEY_UNSPEC) { |
Damien Miller | 386f1f3 | 2003-02-24 11:54:57 +1100 | [diff] [blame] | 723 | debug3("key_read: missing keytype"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 724 | 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 Lindstrom | 309f3d1 | 2001-09-20 00:55:53 +0000 | [diff] [blame] | 736 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 737 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 738 | len = 2*strlen(cp); |
| 739 | blob = xmalloc(len); |
| 740 | n = uudecode(cp, blob, len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 741 | if (n < 0) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 742 | error("key_read: uudecode %s failed", cp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 743 | free(blob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 744 | return -1; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 745 | } |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 746 | k = key_from_blob(blob, (u_int)n); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 747 | free(blob); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 748 | if (k == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 749 | error("key_read: key_from_blob %s failed", cp); |
| 750 | return -1; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 751 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 752 | if (k->type != type) { |
| 753 | error("key_read: type mismatch: encoding error"); |
| 754 | key_free(k); |
| 755 | return -1; |
| 756 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 757 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 758 | 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 Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 764 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 765 | /*XXXX*/ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 766 | 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 Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 778 | if (ret->rsa != NULL) |
| 779 | RSA_free(ret->rsa); |
| 780 | ret->rsa = k->rsa; |
| 781 | k->rsa = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 782 | #ifdef DEBUG_PK |
| 783 | RSA_print_fp(stderr, ret->rsa, 8); |
| 784 | #endif |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 785 | } |
| 786 | if (key_type_plain(ret->type) == KEY_DSA) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 787 | if (ret->dsa != NULL) |
| 788 | DSA_free(ret->dsa); |
| 789 | ret->dsa = k->dsa; |
| 790 | k->dsa = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 791 | #ifdef DEBUG_PK |
| 792 | DSA_print_fp(stderr, ret->dsa, 8); |
| 793 | #endif |
| 794 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 795 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 796 | 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 Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 807 | #endif |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 808 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 809 | /*XXXX*/ |
Ben Lindstrom | 4cbc181 | 2001-12-06 16:41:41 +0000 | [diff] [blame] | 810 | key_free(k); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 811 | if (success != 1) |
| 812 | break; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 813 | /* 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 Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 819 | break; |
| 820 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 821 | fatal("key_read: bad key type: %d", ret->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 822 | break; |
| 823 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 824 | return success; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 825 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 826 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 827 | int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 828 | key_write(const Key *key, FILE *f) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 829 | { |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 830 | int n, success = 0; |
| 831 | u_int len, bits = 0; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 832 | u_char *blob; |
| 833 | char *uu; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 834 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 835 | 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 Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 850 | /* 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 Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 854 | 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 Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 859 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 860 | case KEY_DSA_CERT: |
| 861 | if (key->dsa == NULL) |
| 862 | return 0; |
| 863 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 864 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 865 | case KEY_ECDSA: |
| 866 | case KEY_ECDSA_CERT: |
| 867 | if (key->ecdsa == NULL) |
| 868 | return 0; |
| 869 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 870 | #endif |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 871 | case KEY_RSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 872 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 873 | case KEY_RSA_CERT: |
| 874 | if (key->rsa == NULL) |
| 875 | return 0; |
| 876 | break; |
| 877 | default: |
| 878 | return 0; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 879 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 880 | |
| 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 Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 888 | free(blob); |
| 889 | free(uu); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 890 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 891 | return success; |
| 892 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 893 | |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 894 | const char * |
Damien Miller | 1cfbfaf | 2010-03-22 05:58:24 +1100 | [diff] [blame] | 895 | key_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 Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 907 | struct keytype { |
| 908 | char *name; |
| 909 | char *shortname; |
| 910 | int type; |
| 911 | int nid; |
| 912 | int cert; |
| 913 | }; |
| 914 | static 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 }, |
| 921 | { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0 }, |
| 922 | #endif /* OPENSSL_HAS_ECC */ |
| 923 | { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT, 0, 1 }, |
| 924 | { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT, 0, 1 }, |
| 925 | #ifdef OPENSSL_HAS_ECC |
| 926 | { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT", |
| 927 | KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1 }, |
| 928 | { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT", |
| 929 | KEY_ECDSA_CERT, NID_secp384r1, 1 }, |
| 930 | { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT", |
| 931 | KEY_ECDSA_CERT, NID_secp521r1, 1 }, |
| 932 | #endif /* OPENSSL_HAS_ECC */ |
| 933 | { "ssh-rsa-cert-v00@openssh.com", "RSA-CERT-V00", |
| 934 | KEY_RSA_CERT_V00, 0, 1 }, |
| 935 | { "ssh-dss-cert-v00@openssh.com", "DSA-CERT-V00", |
| 936 | KEY_DSA_CERT_V00, 0, 1 }, |
| 937 | { NULL, NULL, -1, -1, 0 } |
| 938 | }; |
| 939 | |
| 940 | const char * |
| 941 | key_type(const Key *k) |
| 942 | { |
| 943 | const struct keytype *kt; |
| 944 | |
| 945 | for (kt = keytypes; kt->type != -1; kt++) { |
| 946 | if (kt->type == k->type) |
| 947 | return kt->shortname; |
| 948 | } |
| 949 | return "unknown"; |
| 950 | } |
| 951 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 952 | static const char * |
| 953 | key_ssh_name_from_type_nid(int type, int nid) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 954 | { |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 955 | const struct keytype *kt; |
| 956 | |
| 957 | for (kt = keytypes; kt->type != -1; kt++) { |
| 958 | if (kt->type == type && (kt->nid == 0 || kt->nid == nid)) |
| 959 | return kt->name; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 960 | } |
| 961 | return "ssh-unknown"; |
| 962 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 963 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 964 | const char * |
| 965 | key_ssh_name(const Key *k) |
| 966 | { |
| 967 | return key_ssh_name_from_type_nid(k->type, k->ecdsa_nid); |
| 968 | } |
| 969 | |
| 970 | const char * |
| 971 | key_ssh_name_plain(const Key *k) |
| 972 | { |
| 973 | return key_ssh_name_from_type_nid(key_type_plain(k->type), |
| 974 | k->ecdsa_nid); |
| 975 | } |
| 976 | |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 977 | int |
| 978 | key_type_from_name(char *name) |
| 979 | { |
| 980 | const struct keytype *kt; |
| 981 | |
| 982 | for (kt = keytypes; kt->type != -1; kt++) { |
| 983 | /* Only allow shortname matches for plain key types */ |
| 984 | if ((kt->name != NULL && strcmp(name, kt->name) == 0) || |
| 985 | (!kt->cert && strcasecmp(kt->shortname, name) == 0)) |
| 986 | return kt->type; |
| 987 | } |
| 988 | debug2("key_type_from_name: unknown key type '%s'", name); |
| 989 | return KEY_UNSPEC; |
| 990 | } |
| 991 | |
| 992 | int |
| 993 | key_ecdsa_nid_from_name(const char *name) |
| 994 | { |
| 995 | const struct keytype *kt; |
| 996 | |
| 997 | for (kt = keytypes; kt->type != -1; kt++) { |
| 998 | if (kt->type != KEY_ECDSA && kt->type != KEY_ECDSA_CERT) |
| 999 | continue; |
| 1000 | if (kt->name != NULL && strcmp(name, kt->name) == 0) |
| 1001 | return kt->nid; |
| 1002 | } |
| 1003 | debug2("%s: unknown/non-ECDSA key type '%s'", __func__, name); |
| 1004 | return -1; |
| 1005 | } |
| 1006 | |
| 1007 | char * |
| 1008 | key_alg_list(void) |
| 1009 | { |
| 1010 | char *ret = NULL; |
| 1011 | size_t nlen, rlen = 0; |
| 1012 | const struct keytype *kt; |
| 1013 | |
| 1014 | for (kt = keytypes; kt->type != -1; kt++) { |
| 1015 | if (kt->name == NULL) |
| 1016 | continue; |
| 1017 | if (ret != NULL) |
| 1018 | ret[rlen++] = '\n'; |
| 1019 | nlen = strlen(kt->name); |
| 1020 | ret = xrealloc(ret, 1, rlen + nlen + 2); |
| 1021 | memcpy(ret + rlen, kt->name, nlen + 1); |
| 1022 | rlen += nlen; |
| 1023 | } |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 1027 | int |
| 1028 | key_type_is_cert(int type) |
| 1029 | { |
| 1030 | const struct keytype *kt; |
| 1031 | |
| 1032 | for (kt = keytypes; kt->type != -1; kt++) { |
| 1033 | if (kt->type == type) |
| 1034 | return kt->cert; |
| 1035 | } |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1039 | u_int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 1040 | key_size(const Key *k) |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1041 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1042 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1043 | case KEY_RSA1: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1044 | case KEY_RSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1045 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1046 | case KEY_RSA_CERT: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1047 | return BN_num_bits(k->rsa->n); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1048 | case KEY_DSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1049 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1050 | case KEY_DSA_CERT: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1051 | return BN_num_bits(k->dsa->p); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1052 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1053 | case KEY_ECDSA: |
| 1054 | case KEY_ECDSA_CERT: |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 1055 | return key_curve_nid_to_bits(k->ecdsa_nid); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1056 | #endif |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1057 | } |
| 1058 | return 0; |
| 1059 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1060 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1061 | static RSA * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1062 | rsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1063 | { |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 1064 | RSA *private = RSA_new(); |
| 1065 | BIGNUM *f4 = BN_new(); |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 1066 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1067 | if (private == NULL) |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 1068 | fatal("%s: RSA_new failed", __func__); |
| 1069 | if (f4 == NULL) |
| 1070 | fatal("%s: BN_new failed", __func__); |
| 1071 | if (!BN_set_word(f4, RSA_F4)) |
| 1072 | fatal("%s: BN_new failed", __func__); |
| 1073 | if (!RSA_generate_key_ex(private, bits, f4, NULL)) |
| 1074 | fatal("%s: key generation failed.", __func__); |
| 1075 | BN_free(f4); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1076 | return private; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1077 | } |
| 1078 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1079 | static DSA* |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1080 | dsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1081 | { |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 1082 | DSA *private = DSA_new(); |
Damien Miller | 69b7203 | 2006-03-26 14:02:35 +1100 | [diff] [blame] | 1083 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1084 | if (private == NULL) |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 1085 | fatal("%s: DSA_new failed", __func__); |
| 1086 | if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL, |
| 1087 | NULL, NULL)) |
| 1088 | fatal("%s: DSA_generate_parameters failed", __func__); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1089 | if (!DSA_generate_key(private)) |
Damien Miller | 4499f4c | 2010-11-20 15:15:49 +1100 | [diff] [blame] | 1090 | fatal("%s: DSA_generate_key failed.", __func__); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1091 | return private; |
| 1092 | } |
| 1093 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1094 | int |
| 1095 | key_ecdsa_bits_to_nid(int bits) |
| 1096 | { |
| 1097 | switch (bits) { |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1098 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1099 | case 256: |
| 1100 | return NID_X9_62_prime256v1; |
| 1101 | case 384: |
| 1102 | return NID_secp384r1; |
| 1103 | case 521: |
| 1104 | return NID_secp521r1; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1105 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1106 | default: |
| 1107 | return -1; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1111 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1112 | int |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 1113 | key_ecdsa_key_to_nid(EC_KEY *k) |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1114 | { |
| 1115 | EC_GROUP *eg; |
| 1116 | int nids[] = { |
| 1117 | NID_X9_62_prime256v1, |
| 1118 | NID_secp384r1, |
| 1119 | NID_secp521r1, |
| 1120 | -1 |
| 1121 | }; |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 1122 | int nid; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1123 | u_int i; |
| 1124 | BN_CTX *bnctx; |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 1125 | const EC_GROUP *g = EC_KEY_get0_group(k); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1126 | |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 1127 | /* |
| 1128 | * The group may be stored in a ASN.1 encoded private key in one of two |
| 1129 | * ways: as a "named group", which is reconstituted by ASN.1 object ID |
| 1130 | * or explicit group parameters encoded into the key blob. Only the |
| 1131 | * "named group" case sets the group NID for us, but we can figure |
| 1132 | * it out for the other case by comparing against all the groups that |
| 1133 | * are supported. |
| 1134 | */ |
| 1135 | if ((nid = EC_GROUP_get_curve_name(g)) > 0) |
| 1136 | return nid; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1137 | if ((bnctx = BN_CTX_new()) == NULL) |
| 1138 | fatal("%s: BN_CTX_new() failed", __func__); |
| 1139 | for (i = 0; nids[i] != -1; i++) { |
| 1140 | if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL) |
| 1141 | fatal("%s: EC_GROUP_new_by_curve_name failed", |
| 1142 | __func__); |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 1143 | if (EC_GROUP_cmp(g, eg, bnctx) == 0) |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1144 | break; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1145 | EC_GROUP_free(eg); |
| 1146 | } |
| 1147 | BN_CTX_free(bnctx); |
| 1148 | debug3("%s: nid = %d", __func__, nids[i]); |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 1149 | if (nids[i] != -1) { |
| 1150 | /* Use the group with the NID attached */ |
| 1151 | EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE); |
| 1152 | if (EC_KEY_set_group(k, eg) != 1) |
| 1153 | fatal("%s: EC_KEY_set_group", __func__); |
| 1154 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1155 | return nids[i]; |
| 1156 | } |
| 1157 | |
| 1158 | static EC_KEY* |
| 1159 | ecdsa_generate_private_key(u_int bits, int *nid) |
| 1160 | { |
| 1161 | EC_KEY *private; |
| 1162 | |
| 1163 | if ((*nid = key_ecdsa_bits_to_nid(bits)) == -1) |
| 1164 | fatal("%s: invalid key length", __func__); |
| 1165 | if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) |
| 1166 | fatal("%s: EC_KEY_new_by_curve_name failed", __func__); |
| 1167 | if (EC_KEY_generate_key(private) != 1) |
| 1168 | fatal("%s: EC_KEY_generate_key failed", __func__); |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 1169 | EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1170 | return private; |
| 1171 | } |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1172 | #endif /* OPENSSL_HAS_ECC */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1173 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1174 | Key * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1175 | key_generate(int type, u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1176 | { |
| 1177 | Key *k = key_new(KEY_UNSPEC); |
| 1178 | switch (type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1179 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1180 | k->dsa = dsa_generate_private_key(bits); |
| 1181 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1182 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1183 | case KEY_ECDSA: |
| 1184 | k->ecdsa = ecdsa_generate_private_key(bits, &k->ecdsa_nid); |
| 1185 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1186 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1187 | case KEY_RSA: |
| 1188 | case KEY_RSA1: |
| 1189 | k->rsa = rsa_generate_private_key(bits); |
| 1190 | break; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1191 | case KEY_RSA_CERT_V00: |
| 1192 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1193 | case KEY_RSA_CERT: |
| 1194 | case KEY_DSA_CERT: |
| 1195 | fatal("key_generate: cert keys cannot be generated directly"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1196 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1197 | fatal("key_generate: unknown type %d", type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1198 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1199 | k->type = type; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1200 | return k; |
| 1201 | } |
| 1202 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1203 | void |
| 1204 | key_cert_copy(const Key *from_key, struct Key *to_key) |
| 1205 | { |
| 1206 | u_int i; |
| 1207 | const struct KeyCert *from; |
| 1208 | struct KeyCert *to; |
| 1209 | |
| 1210 | if (to_key->cert != NULL) { |
| 1211 | cert_free(to_key->cert); |
| 1212 | to_key->cert = NULL; |
| 1213 | } |
| 1214 | |
| 1215 | if ((from = from_key->cert) == NULL) |
| 1216 | return; |
| 1217 | |
| 1218 | to = to_key->cert = cert_new(); |
| 1219 | |
| 1220 | buffer_append(&to->certblob, buffer_ptr(&from->certblob), |
| 1221 | buffer_len(&from->certblob)); |
| 1222 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1223 | buffer_append(&to->critical, |
| 1224 | buffer_ptr(&from->critical), buffer_len(&from->critical)); |
| 1225 | buffer_append(&to->extensions, |
| 1226 | buffer_ptr(&from->extensions), buffer_len(&from->extensions)); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1227 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1228 | to->serial = from->serial; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1229 | to->type = from->type; |
| 1230 | to->key_id = from->key_id == NULL ? NULL : xstrdup(from->key_id); |
| 1231 | to->valid_after = from->valid_after; |
| 1232 | to->valid_before = from->valid_before; |
| 1233 | to->signature_key = from->signature_key == NULL ? |
| 1234 | NULL : key_from_private(from->signature_key); |
| 1235 | |
| 1236 | to->nprincipals = from->nprincipals; |
| 1237 | if (to->nprincipals > CERT_MAX_PRINCIPALS) |
| 1238 | fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)", |
| 1239 | __func__, to->nprincipals, CERT_MAX_PRINCIPALS); |
| 1240 | if (to->nprincipals > 0) { |
| 1241 | to->principals = xcalloc(from->nprincipals, |
| 1242 | sizeof(*to->principals)); |
| 1243 | for (i = 0; i < to->nprincipals; i++) |
| 1244 | to->principals[i] = xstrdup(from->principals[i]); |
| 1245 | } |
| 1246 | } |
| 1247 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1248 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 1249 | key_from_private(const Key *k) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1250 | { |
| 1251 | Key *n = NULL; |
| 1252 | switch (k->type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1253 | case KEY_DSA: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1254 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1255 | case KEY_DSA_CERT: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1256 | n = key_new(k->type); |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 1257 | if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) || |
| 1258 | (BN_copy(n->dsa->q, k->dsa->q) == NULL) || |
| 1259 | (BN_copy(n->dsa->g, k->dsa->g) == NULL) || |
| 1260 | (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) |
| 1261 | fatal("key_from_private: BN_copy failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1262 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1263 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1264 | case KEY_ECDSA: |
| 1265 | case KEY_ECDSA_CERT: |
| 1266 | n = key_new(k->type); |
| 1267 | n->ecdsa_nid = k->ecdsa_nid; |
| 1268 | if ((n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid)) == NULL) |
| 1269 | fatal("%s: EC_KEY_new_by_curve_name failed", __func__); |
| 1270 | if (EC_KEY_set_public_key(n->ecdsa, |
| 1271 | EC_KEY_get0_public_key(k->ecdsa)) != 1) |
| 1272 | fatal("%s: EC_KEY_set_public_key failed", __func__); |
| 1273 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1274 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1275 | case KEY_RSA: |
| 1276 | case KEY_RSA1: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1277 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1278 | case KEY_RSA_CERT: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1279 | n = key_new(k->type); |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 1280 | if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) || |
| 1281 | (BN_copy(n->rsa->e, k->rsa->e) == NULL)) |
| 1282 | fatal("key_from_private: BN_copy failed"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1283 | break; |
| 1284 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1285 | fatal("key_from_private: unknown type %d", k->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1286 | break; |
| 1287 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1288 | if (key_is_cert(k)) |
| 1289 | key_cert_copy(k, n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1290 | return n; |
| 1291 | } |
| 1292 | |
| 1293 | int |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1294 | key_names_valid2(const char *names) |
| 1295 | { |
| 1296 | char *s, *cp, *p; |
| 1297 | |
| 1298 | if (names == NULL || strcmp(names, "") == 0) |
| 1299 | return 0; |
| 1300 | s = cp = xstrdup(names); |
| 1301 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1302 | (p = strsep(&cp, ","))) { |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1303 | switch (key_type_from_name(p)) { |
| 1304 | case KEY_RSA1: |
| 1305 | case KEY_UNSPEC: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1306 | free(s); |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1307 | return 0; |
| 1308 | } |
| 1309 | } |
| 1310 | debug3("key names ok: [%s]", names); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1311 | free(s); |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 1312 | return 1; |
| 1313 | } |
| 1314 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1315 | static int |
| 1316 | cert_parse(Buffer *b, Key *key, const u_char *blob, u_int blen) |
| 1317 | { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1318 | u_char *principals, *critical, *exts, *sig_key, *sig; |
| 1319 | u_int signed_len, plen, clen, sklen, slen, kidlen, elen; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1320 | Buffer tmp; |
| 1321 | char *principal; |
| 1322 | int ret = -1; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1323 | int v00 = key->type == KEY_DSA_CERT_V00 || |
| 1324 | key->type == KEY_RSA_CERT_V00; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1325 | |
| 1326 | buffer_init(&tmp); |
| 1327 | |
| 1328 | /* Copy the entire key blob for verification and later serialisation */ |
| 1329 | buffer_append(&key->cert->certblob, blob, blen); |
| 1330 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1331 | elen = 0; /* Not touched for v00 certs */ |
| 1332 | principals = exts = critical = sig_key = sig = NULL; |
| 1333 | if ((!v00 && buffer_get_int64_ret(&key->cert->serial, b) != 0) || |
| 1334 | buffer_get_int_ret(&key->cert->type, b) != 0 || |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 1335 | (key->cert->key_id = buffer_get_cstring_ret(b, &kidlen)) == NULL || |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1336 | (principals = buffer_get_string_ret(b, &plen)) == NULL || |
| 1337 | buffer_get_int64_ret(&key->cert->valid_after, b) != 0 || |
| 1338 | buffer_get_int64_ret(&key->cert->valid_before, b) != 0 || |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1339 | (critical = buffer_get_string_ret(b, &clen)) == NULL || |
| 1340 | (!v00 && (exts = buffer_get_string_ret(b, &elen)) == NULL) || |
| 1341 | (v00 && buffer_get_string_ptr_ret(b, NULL) == NULL) || /* nonce */ |
| 1342 | buffer_get_string_ptr_ret(b, NULL) == NULL || /* reserved */ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1343 | (sig_key = buffer_get_string_ret(b, &sklen)) == NULL) { |
| 1344 | error("%s: parse error", __func__); |
| 1345 | goto out; |
| 1346 | } |
| 1347 | |
| 1348 | /* Signature is left in the buffer so we can calculate this length */ |
| 1349 | signed_len = buffer_len(&key->cert->certblob) - buffer_len(b); |
| 1350 | |
| 1351 | if ((sig = buffer_get_string_ret(b, &slen)) == NULL) { |
| 1352 | error("%s: parse error", __func__); |
| 1353 | goto out; |
| 1354 | } |
| 1355 | |
| 1356 | if (key->cert->type != SSH2_CERT_TYPE_USER && |
| 1357 | key->cert->type != SSH2_CERT_TYPE_HOST) { |
| 1358 | error("Unknown certificate type %u", key->cert->type); |
| 1359 | goto out; |
| 1360 | } |
| 1361 | |
| 1362 | buffer_append(&tmp, principals, plen); |
| 1363 | while (buffer_len(&tmp) > 0) { |
| 1364 | if (key->cert->nprincipals >= CERT_MAX_PRINCIPALS) { |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 1365 | error("%s: Too many principals", __func__); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1366 | goto out; |
| 1367 | } |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 1368 | if ((principal = buffer_get_cstring_ret(&tmp, &plen)) == NULL) { |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 1369 | error("%s: Principals data invalid", __func__); |
| 1370 | goto out; |
| 1371 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1372 | key->cert->principals = xrealloc(key->cert->principals, |
| 1373 | key->cert->nprincipals + 1, sizeof(*key->cert->principals)); |
| 1374 | key->cert->principals[key->cert->nprincipals++] = principal; |
| 1375 | } |
| 1376 | |
| 1377 | buffer_clear(&tmp); |
| 1378 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1379 | buffer_append(&key->cert->critical, critical, clen); |
| 1380 | buffer_append(&tmp, critical, clen); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1381 | /* validate structure */ |
| 1382 | while (buffer_len(&tmp) != 0) { |
Damien Miller | 2befbad | 2010-03-04 21:52:18 +1100 | [diff] [blame] | 1383 | if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL || |
| 1384 | buffer_get_string_ptr_ret(&tmp, NULL) == NULL) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1385 | error("%s: critical option data invalid", __func__); |
| 1386 | goto out; |
| 1387 | } |
| 1388 | } |
| 1389 | buffer_clear(&tmp); |
| 1390 | |
| 1391 | buffer_append(&key->cert->extensions, exts, elen); |
| 1392 | buffer_append(&tmp, exts, elen); |
| 1393 | /* validate structure */ |
| 1394 | while (buffer_len(&tmp) != 0) { |
| 1395 | if (buffer_get_string_ptr_ret(&tmp, NULL) == NULL || |
| 1396 | buffer_get_string_ptr_ret(&tmp, NULL) == NULL) { |
| 1397 | error("%s: extension data invalid", __func__); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1398 | goto out; |
| 1399 | } |
| 1400 | } |
| 1401 | buffer_clear(&tmp); |
| 1402 | |
Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 1403 | if ((key->cert->signature_key = key_from_blob2(sig_key, sklen, 0)) |
| 1404 | == NULL) { |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 1405 | error("%s: Signature key invalid", __func__); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1406 | goto out; |
| 1407 | } |
| 1408 | if (key->cert->signature_key->type != KEY_RSA && |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1409 | key->cert->signature_key->type != KEY_DSA && |
| 1410 | key->cert->signature_key->type != KEY_ECDSA) { |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 1411 | error("%s: Invalid signature key type %s (%d)", __func__, |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1412 | key_type(key->cert->signature_key), |
| 1413 | key->cert->signature_key->type); |
| 1414 | goto out; |
| 1415 | } |
| 1416 | |
| 1417 | switch (key_verify(key->cert->signature_key, sig, slen, |
| 1418 | buffer_ptr(&key->cert->certblob), signed_len)) { |
| 1419 | case 1: |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 1420 | ret = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1421 | break; /* Good signature */ |
| 1422 | case 0: |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 1423 | error("%s: Invalid signature on certificate", __func__); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1424 | goto out; |
| 1425 | case -1: |
Damien Miller | 4139657 | 2010-03-04 21:51:11 +1100 | [diff] [blame] | 1426 | error("%s: Certificate signature verification failed", |
| 1427 | __func__); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1428 | goto out; |
| 1429 | } |
| 1430 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1431 | out: |
| 1432 | buffer_free(&tmp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1433 | free(principals); |
| 1434 | free(critical); |
| 1435 | free(exts); |
| 1436 | free(sig_key); |
| 1437 | free(sig); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1438 | return ret; |
| 1439 | } |
| 1440 | |
Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 1441 | static Key * |
| 1442 | key_from_blob2(const u_char *blob, u_int blen, int allow_cert) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1443 | { |
| 1444 | Buffer b; |
Darren Tucker | 8ccb739 | 2010-09-10 12:28:24 +1000 | [diff] [blame] | 1445 | int rlen, type; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1446 | char *ktype = NULL, *curve = NULL; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1447 | Key *key = NULL; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1448 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1449 | EC_POINT *q = NULL; |
Darren Tucker | 8ccb739 | 2010-09-10 12:28:24 +1000 | [diff] [blame] | 1450 | int nid = -1; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1451 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1452 | |
| 1453 | #ifdef DEBUG_PK |
| 1454 | dump_base64(stderr, blob, blen); |
| 1455 | #endif |
| 1456 | buffer_init(&b); |
| 1457 | buffer_append(&b, blob, blen); |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 1458 | if ((ktype = buffer_get_cstring_ret(&b, NULL)) == NULL) { |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 1459 | error("key_from_blob: can't read key type"); |
| 1460 | goto out; |
| 1461 | } |
| 1462 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1463 | type = key_type_from_name(ktype); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1464 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1465 | if (key_type_plain(type) == KEY_ECDSA) |
| 1466 | nid = key_ecdsa_nid_from_name(ktype); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1467 | #endif |
Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 1468 | if (!allow_cert && key_type_is_cert(type)) { |
| 1469 | error("key_from_blob: certificate not allowed in this context"); |
| 1470 | goto out; |
| 1471 | } |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1472 | switch (type) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1473 | case KEY_RSA_CERT: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1474 | (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */ |
| 1475 | /* FALLTHROUGH */ |
| 1476 | case KEY_RSA: |
| 1477 | case KEY_RSA_CERT_V00: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1478 | key = key_new(type); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 1479 | if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 || |
| 1480 | buffer_get_bignum2_ret(&b, key->rsa->n) == -1) { |
| 1481 | error("key_from_blob: can't read rsa key"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1482 | badkey: |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 1483 | key_free(key); |
| 1484 | key = NULL; |
| 1485 | goto out; |
| 1486 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1487 | #ifdef DEBUG_PK |
| 1488 | RSA_print_fp(stderr, key->rsa, 8); |
| 1489 | #endif |
| 1490 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1491 | case KEY_DSA_CERT: |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1492 | (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */ |
| 1493 | /* FALLTHROUGH */ |
| 1494 | case KEY_DSA: |
| 1495 | case KEY_DSA_CERT_V00: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1496 | key = key_new(type); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 1497 | if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 || |
| 1498 | buffer_get_bignum2_ret(&b, key->dsa->q) == -1 || |
| 1499 | buffer_get_bignum2_ret(&b, key->dsa->g) == -1 || |
| 1500 | buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) { |
| 1501 | error("key_from_blob: can't read dsa key"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1502 | goto badkey; |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 1503 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1504 | #ifdef DEBUG_PK |
| 1505 | DSA_print_fp(stderr, key->dsa, 8); |
| 1506 | #endif |
| 1507 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1508 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1509 | case KEY_ECDSA_CERT: |
| 1510 | (void)buffer_get_string_ptr_ret(&b, NULL); /* Skip nonce */ |
| 1511 | /* FALLTHROUGH */ |
| 1512 | case KEY_ECDSA: |
| 1513 | key = key_new(type); |
| 1514 | key->ecdsa_nid = nid; |
| 1515 | if ((curve = buffer_get_string_ret(&b, NULL)) == NULL) { |
| 1516 | error("key_from_blob: can't read ecdsa curve"); |
| 1517 | goto badkey; |
| 1518 | } |
| 1519 | if (key->ecdsa_nid != key_curve_name_to_nid(curve)) { |
| 1520 | error("key_from_blob: ecdsa curve doesn't match type"); |
| 1521 | goto badkey; |
| 1522 | } |
| 1523 | if (key->ecdsa != NULL) |
| 1524 | EC_KEY_free(key->ecdsa); |
| 1525 | if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) |
| 1526 | == NULL) |
| 1527 | fatal("key_from_blob: EC_KEY_new_by_curve_name failed"); |
| 1528 | if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL) |
| 1529 | fatal("key_from_blob: EC_POINT_new failed"); |
| 1530 | if (buffer_get_ecpoint_ret(&b, EC_KEY_get0_group(key->ecdsa), |
| 1531 | q) == -1) { |
| 1532 | error("key_from_blob: can't read ecdsa key point"); |
| 1533 | goto badkey; |
| 1534 | } |
| 1535 | if (key_ec_validate_public(EC_KEY_get0_group(key->ecdsa), |
| 1536 | q) != 0) |
| 1537 | goto badkey; |
| 1538 | if (EC_KEY_set_public_key(key->ecdsa, q) != 1) |
| 1539 | fatal("key_from_blob: EC_KEY_set_public_key failed"); |
| 1540 | #ifdef DEBUG_PK |
| 1541 | key_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q); |
| 1542 | #endif |
| 1543 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1544 | #endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1545 | case KEY_UNSPEC: |
| 1546 | key = key_new(type); |
| 1547 | break; |
| 1548 | default: |
| 1549 | error("key_from_blob: cannot handle type %s", ktype); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 1550 | goto out; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1551 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1552 | if (key_is_cert(key) && cert_parse(&b, key, blob, blen) == -1) { |
| 1553 | error("key_from_blob: can't parse cert data"); |
| 1554 | goto badkey; |
| 1555 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1556 | rlen = buffer_len(&b); |
| 1557 | if (key != NULL && rlen != 0) |
| 1558 | error("key_from_blob: remaining bytes in key blob %d", rlen); |
Darren Tucker | 08d04fa | 2004-11-05 20:42:28 +1100 | [diff] [blame] | 1559 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1560 | free(ktype); |
| 1561 | free(curve); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1562 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1563 | if (q != NULL) |
| 1564 | EC_POINT_free(q); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1565 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1566 | buffer_free(&b); |
| 1567 | return key; |
| 1568 | } |
| 1569 | |
Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 1570 | Key * |
| 1571 | key_from_blob(const u_char *blob, u_int blen) |
| 1572 | { |
| 1573 | return key_from_blob2(blob, blen, 1); |
| 1574 | } |
| 1575 | |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1576 | static int |
| 1577 | to_blob(const Key *key, u_char **blobp, u_int *lenp, int force_plain) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1578 | { |
| 1579 | Buffer b; |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1580 | int len, type; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1581 | |
| 1582 | if (key == NULL) { |
| 1583 | error("key_to_blob: key == NULL"); |
| 1584 | return 0; |
| 1585 | } |
| 1586 | buffer_init(&b); |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1587 | type = force_plain ? key_type_plain(key->type) : key->type; |
| 1588 | switch (type) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1589 | case KEY_DSA_CERT_V00: |
| 1590 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1591 | case KEY_DSA_CERT: |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1592 | case KEY_ECDSA_CERT: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1593 | case KEY_RSA_CERT: |
| 1594 | /* Use the existing blob */ |
| 1595 | buffer_append(&b, buffer_ptr(&key->cert->certblob), |
| 1596 | buffer_len(&key->cert->certblob)); |
| 1597 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1598 | case KEY_DSA: |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1599 | buffer_put_cstring(&b, |
| 1600 | key_ssh_name_from_type_nid(type, key->ecdsa_nid)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1601 | buffer_put_bignum2(&b, key->dsa->p); |
| 1602 | buffer_put_bignum2(&b, key->dsa->q); |
| 1603 | buffer_put_bignum2(&b, key->dsa->g); |
| 1604 | buffer_put_bignum2(&b, key->dsa->pub_key); |
| 1605 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1606 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1607 | case KEY_ECDSA: |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1608 | buffer_put_cstring(&b, |
| 1609 | key_ssh_name_from_type_nid(type, key->ecdsa_nid)); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1610 | buffer_put_cstring(&b, key_curve_nid_to_name(key->ecdsa_nid)); |
| 1611 | buffer_put_ecpoint(&b, EC_KEY_get0_group(key->ecdsa), |
| 1612 | EC_KEY_get0_public_key(key->ecdsa)); |
| 1613 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1614 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1615 | case KEY_RSA: |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1616 | buffer_put_cstring(&b, |
| 1617 | key_ssh_name_from_type_nid(type, key->ecdsa_nid)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1618 | buffer_put_bignum2(&b, key->rsa->e); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1619 | buffer_put_bignum2(&b, key->rsa->n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1620 | break; |
| 1621 | default: |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 1622 | error("key_to_blob: unsupported key type %d", key->type); |
| 1623 | buffer_free(&b); |
| 1624 | return 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1625 | } |
| 1626 | len = buffer_len(&b); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1627 | if (lenp != NULL) |
| 1628 | *lenp = len; |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 1629 | if (blobp != NULL) { |
| 1630 | *blobp = xmalloc(len); |
| 1631 | memcpy(*blobp, buffer_ptr(&b), len); |
| 1632 | } |
| 1633 | memset(buffer_ptr(&b), 0, len); |
| 1634 | buffer_free(&b); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1635 | return len; |
| 1636 | } |
| 1637 | |
| 1638 | int |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1639 | key_to_blob(const Key *key, u_char **blobp, u_int *lenp) |
| 1640 | { |
| 1641 | return to_blob(key, blobp, lenp, 0); |
| 1642 | } |
| 1643 | |
| 1644 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1645 | key_sign( |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 1646 | const Key *key, |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 1647 | u_char **sigp, u_int *lenp, |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 1648 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1649 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1650 | switch (key->type) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1651 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1652 | case KEY_DSA_CERT: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1653 | case KEY_DSA: |
| 1654 | return ssh_dss_sign(key, sigp, lenp, data, datalen); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1655 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1656 | case KEY_ECDSA_CERT: |
| 1657 | case KEY_ECDSA: |
| 1658 | return ssh_ecdsa_sign(key, sigp, lenp, data, datalen); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1659 | #endif |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1660 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1661 | case KEY_RSA_CERT: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1662 | case KEY_RSA: |
| 1663 | return ssh_rsa_sign(key, sigp, lenp, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1664 | default: |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 1665 | error("key_sign: invalid key type %d", key->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1666 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | |
Ben Lindstrom | 01fff0c | 2002-06-06 20:54:07 +0000 | [diff] [blame] | 1670 | /* |
| 1671 | * key_verify returns 1 for a correct signature, 0 for an incorrect signature |
| 1672 | * and -1 on error. |
| 1673 | */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1674 | int |
| 1675 | key_verify( |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 1676 | const Key *key, |
| 1677 | const u_char *signature, u_int signaturelen, |
| 1678 | const u_char *data, u_int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1679 | { |
Ben Lindstrom | 5363aee | 2001-06-25 04:42:20 +0000 | [diff] [blame] | 1680 | if (signaturelen == 0) |
| 1681 | return -1; |
| 1682 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1683 | switch (key->type) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1684 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1685 | case KEY_DSA_CERT: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1686 | case KEY_DSA: |
| 1687 | return ssh_dss_verify(key, signature, signaturelen, data, datalen); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1688 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1689 | case KEY_ECDSA_CERT: |
| 1690 | case KEY_ECDSA: |
| 1691 | return ssh_ecdsa_verify(key, signature, signaturelen, data, datalen); |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1692 | #endif |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1693 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1694 | case KEY_RSA_CERT: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1695 | case KEY_RSA: |
| 1696 | return ssh_rsa_verify(key, signature, signaturelen, data, datalen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1697 | default: |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 1698 | error("key_verify: invalid key type %d", key->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1699 | return -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1700 | } |
| 1701 | } |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1702 | |
| 1703 | /* Converts a private to a public key */ |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1704 | Key * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 1705 | key_demote(const Key *k) |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1706 | { |
| 1707 | Key *pk; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1708 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1709 | pk = xcalloc(1, sizeof(*pk)); |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1710 | pk->type = k->type; |
| 1711 | pk->flags = k->flags; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1712 | pk->ecdsa_nid = k->ecdsa_nid; |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1713 | pk->dsa = NULL; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1714 | pk->ecdsa = NULL; |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1715 | pk->rsa = NULL; |
| 1716 | |
| 1717 | switch (k->type) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1718 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1719 | case KEY_RSA_CERT: |
| 1720 | key_cert_copy(k, pk); |
| 1721 | /* FALLTHROUGH */ |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1722 | case KEY_RSA1: |
| 1723 | case KEY_RSA: |
| 1724 | if ((pk->rsa = RSA_new()) == NULL) |
| 1725 | fatal("key_demote: RSA_new failed"); |
| 1726 | if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL) |
| 1727 | fatal("key_demote: BN_dup failed"); |
| 1728 | if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL) |
| 1729 | fatal("key_demote: BN_dup failed"); |
| 1730 | break; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1731 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1732 | case KEY_DSA_CERT: |
| 1733 | key_cert_copy(k, pk); |
| 1734 | /* FALLTHROUGH */ |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1735 | case KEY_DSA: |
| 1736 | if ((pk->dsa = DSA_new()) == NULL) |
| 1737 | fatal("key_demote: DSA_new failed"); |
| 1738 | if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL) |
| 1739 | fatal("key_demote: BN_dup failed"); |
| 1740 | if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL) |
| 1741 | fatal("key_demote: BN_dup failed"); |
| 1742 | if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL) |
| 1743 | fatal("key_demote: BN_dup failed"); |
| 1744 | if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) |
| 1745 | fatal("key_demote: BN_dup failed"); |
| 1746 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1747 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1748 | case KEY_ECDSA_CERT: |
| 1749 | key_cert_copy(k, pk); |
| 1750 | /* FALLTHROUGH */ |
| 1751 | case KEY_ECDSA: |
| 1752 | if ((pk->ecdsa = EC_KEY_new_by_curve_name(pk->ecdsa_nid)) == NULL) |
| 1753 | fatal("key_demote: EC_KEY_new_by_curve_name failed"); |
| 1754 | if (EC_KEY_set_public_key(pk->ecdsa, |
| 1755 | EC_KEY_get0_public_key(k->ecdsa)) != 1) |
| 1756 | fatal("key_demote: EC_KEY_set_public_key failed"); |
| 1757 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1758 | #endif |
Ben Lindstrom | a674e8d | 2002-03-22 01:45:53 +0000 | [diff] [blame] | 1759 | default: |
| 1760 | fatal("key_free: bad key type %d", k->type); |
| 1761 | break; |
| 1762 | } |
| 1763 | |
| 1764 | return (pk); |
| 1765 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1766 | |
| 1767 | int |
| 1768 | key_is_cert(const Key *k) |
| 1769 | { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1770 | if (k == NULL) |
| 1771 | return 0; |
Damien Miller | 4a3a9d4 | 2013-10-30 22:19:47 +1100 | [diff] [blame] | 1772 | return key_type_is_cert(k->type); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /* Return the cert-less equivalent to a certified key type */ |
| 1776 | int |
| 1777 | key_type_plain(int type) |
| 1778 | { |
| 1779 | switch (type) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1780 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1781 | case KEY_RSA_CERT: |
| 1782 | return KEY_RSA; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1783 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1784 | case KEY_DSA_CERT: |
| 1785 | return KEY_DSA; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1786 | case KEY_ECDSA_CERT: |
| 1787 | return KEY_ECDSA; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1788 | default: |
| 1789 | return type; |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | /* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */ |
| 1794 | int |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1795 | key_to_certified(Key *k, int legacy) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1796 | { |
| 1797 | switch (k->type) { |
| 1798 | case KEY_RSA: |
| 1799 | k->cert = cert_new(); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1800 | k->type = legacy ? KEY_RSA_CERT_V00 : KEY_RSA_CERT; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1801 | return 0; |
| 1802 | case KEY_DSA: |
| 1803 | k->cert = cert_new(); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1804 | k->type = legacy ? KEY_DSA_CERT_V00 : KEY_DSA_CERT; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1805 | return 0; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1806 | case KEY_ECDSA: |
Damien Miller | 8f639fe | 2011-05-20 19:03:08 +1000 | [diff] [blame] | 1807 | if (legacy) |
| 1808 | fatal("%s: legacy ECDSA certificates are not supported", |
| 1809 | __func__); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1810 | k->cert = cert_new(); |
| 1811 | k->type = KEY_ECDSA_CERT; |
| 1812 | return 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1813 | default: |
| 1814 | error("%s: key has incorrect type %s", __func__, key_type(k)); |
| 1815 | return -1; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | /* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */ |
| 1820 | int |
| 1821 | key_drop_cert(Key *k) |
| 1822 | { |
| 1823 | switch (k->type) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1824 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1825 | case KEY_RSA_CERT: |
| 1826 | cert_free(k->cert); |
| 1827 | k->type = KEY_RSA; |
| 1828 | return 0; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1829 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1830 | case KEY_DSA_CERT: |
| 1831 | cert_free(k->cert); |
| 1832 | k->type = KEY_DSA; |
| 1833 | return 0; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1834 | case KEY_ECDSA_CERT: |
| 1835 | cert_free(k->cert); |
| 1836 | k->type = KEY_ECDSA; |
| 1837 | return 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1838 | default: |
| 1839 | error("%s: key has incorrect type %s", __func__, key_type(k)); |
| 1840 | return -1; |
| 1841 | } |
| 1842 | } |
| 1843 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1844 | /* |
| 1845 | * Sign a KEY_RSA_CERT, KEY_DSA_CERT or KEY_ECDSA_CERT, (re-)generating |
| 1846 | * the signed certblob |
| 1847 | */ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1848 | int |
| 1849 | key_certify(Key *k, Key *ca) |
| 1850 | { |
| 1851 | Buffer principals; |
| 1852 | u_char *ca_blob, *sig_blob, nonce[32]; |
| 1853 | u_int i, ca_len, sig_len; |
| 1854 | |
| 1855 | if (k->cert == NULL) { |
| 1856 | error("%s: key lacks cert info", __func__); |
| 1857 | return -1; |
| 1858 | } |
| 1859 | |
| 1860 | if (!key_is_cert(k)) { |
| 1861 | error("%s: certificate has unknown type %d", __func__, |
| 1862 | k->cert->type); |
| 1863 | return -1; |
| 1864 | } |
| 1865 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1866 | if (ca->type != KEY_RSA && ca->type != KEY_DSA && |
| 1867 | ca->type != KEY_ECDSA) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1868 | error("%s: CA key has unsupported type %s", __func__, |
| 1869 | key_type(ca)); |
| 1870 | return -1; |
| 1871 | } |
| 1872 | |
| 1873 | key_to_blob(ca, &ca_blob, &ca_len); |
| 1874 | |
| 1875 | buffer_clear(&k->cert->certblob); |
| 1876 | buffer_put_cstring(&k->cert->certblob, key_ssh_name(k)); |
| 1877 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1878 | /* -v01 certs put nonce first */ |
Damien Miller | 0a5f012 | 2011-02-04 11:47:01 +1100 | [diff] [blame] | 1879 | arc4random_buf(&nonce, sizeof(nonce)); |
| 1880 | if (!key_cert_is_legacy(k)) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1881 | buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce)); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1882 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1883 | switch (k->type) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1884 | case KEY_DSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1885 | case KEY_DSA_CERT: |
| 1886 | buffer_put_bignum2(&k->cert->certblob, k->dsa->p); |
| 1887 | buffer_put_bignum2(&k->cert->certblob, k->dsa->q); |
| 1888 | buffer_put_bignum2(&k->cert->certblob, k->dsa->g); |
| 1889 | buffer_put_bignum2(&k->cert->certblob, k->dsa->pub_key); |
| 1890 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1891 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1892 | case KEY_ECDSA_CERT: |
| 1893 | buffer_put_cstring(&k->cert->certblob, |
| 1894 | key_curve_nid_to_name(k->ecdsa_nid)); |
| 1895 | buffer_put_ecpoint(&k->cert->certblob, |
| 1896 | EC_KEY_get0_group(k->ecdsa), |
| 1897 | EC_KEY_get0_public_key(k->ecdsa)); |
| 1898 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 1899 | #endif |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1900 | case KEY_RSA_CERT_V00: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1901 | case KEY_RSA_CERT: |
| 1902 | buffer_put_bignum2(&k->cert->certblob, k->rsa->e); |
| 1903 | buffer_put_bignum2(&k->cert->certblob, k->rsa->n); |
| 1904 | break; |
| 1905 | default: |
| 1906 | error("%s: key has incorrect type %s", __func__, key_type(k)); |
| 1907 | buffer_clear(&k->cert->certblob); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1908 | free(ca_blob); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1909 | return -1; |
| 1910 | } |
| 1911 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1912 | /* -v01 certs have a serial number next */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1913 | if (!key_cert_is_legacy(k)) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1914 | buffer_put_int64(&k->cert->certblob, k->cert->serial); |
| 1915 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1916 | buffer_put_int(&k->cert->certblob, k->cert->type); |
| 1917 | buffer_put_cstring(&k->cert->certblob, k->cert->key_id); |
| 1918 | |
| 1919 | buffer_init(&principals); |
| 1920 | for (i = 0; i < k->cert->nprincipals; i++) |
| 1921 | buffer_put_cstring(&principals, k->cert->principals[i]); |
| 1922 | buffer_put_string(&k->cert->certblob, buffer_ptr(&principals), |
| 1923 | buffer_len(&principals)); |
| 1924 | buffer_free(&principals); |
| 1925 | |
| 1926 | buffer_put_int64(&k->cert->certblob, k->cert->valid_after); |
| 1927 | buffer_put_int64(&k->cert->certblob, k->cert->valid_before); |
| 1928 | buffer_put_string(&k->cert->certblob, |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1929 | buffer_ptr(&k->cert->critical), buffer_len(&k->cert->critical)); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1930 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1931 | /* -v01 certs have non-critical options here */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1932 | if (!key_cert_is_legacy(k)) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1933 | buffer_put_string(&k->cert->certblob, |
| 1934 | buffer_ptr(&k->cert->extensions), |
| 1935 | buffer_len(&k->cert->extensions)); |
| 1936 | } |
| 1937 | |
| 1938 | /* -v00 certs put the nonce at the end */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1939 | if (key_cert_is_legacy(k)) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1940 | buffer_put_string(&k->cert->certblob, nonce, sizeof(nonce)); |
| 1941 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1942 | buffer_put_string(&k->cert->certblob, NULL, 0); /* reserved */ |
| 1943 | buffer_put_string(&k->cert->certblob, ca_blob, ca_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1944 | free(ca_blob); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1945 | |
| 1946 | /* Sign the whole mess */ |
| 1947 | if (key_sign(ca, &sig_blob, &sig_len, buffer_ptr(&k->cert->certblob), |
| 1948 | buffer_len(&k->cert->certblob)) != 0) { |
| 1949 | error("%s: signature operation failed", __func__); |
| 1950 | buffer_clear(&k->cert->certblob); |
| 1951 | return -1; |
| 1952 | } |
| 1953 | /* Append signature and we are done */ |
| 1954 | buffer_put_string(&k->cert->certblob, sig_blob, sig_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1955 | free(sig_blob); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1956 | |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | int |
| 1961 | key_cert_check_authority(const Key *k, int want_host, int require_principal, |
| 1962 | const char *name, const char **reason) |
| 1963 | { |
| 1964 | u_int i, principal_matches; |
| 1965 | time_t now = time(NULL); |
| 1966 | |
| 1967 | if (want_host) { |
| 1968 | if (k->cert->type != SSH2_CERT_TYPE_HOST) { |
| 1969 | *reason = "Certificate invalid: not a host certificate"; |
| 1970 | return -1; |
| 1971 | } |
| 1972 | } else { |
| 1973 | if (k->cert->type != SSH2_CERT_TYPE_USER) { |
| 1974 | *reason = "Certificate invalid: not a user certificate"; |
| 1975 | return -1; |
| 1976 | } |
| 1977 | } |
| 1978 | if (now < 0) { |
| 1979 | error("%s: system clock lies before epoch", __func__); |
| 1980 | *reason = "Certificate invalid: not yet valid"; |
| 1981 | return -1; |
| 1982 | } |
| 1983 | if ((u_int64_t)now < k->cert->valid_after) { |
| 1984 | *reason = "Certificate invalid: not yet valid"; |
| 1985 | return -1; |
| 1986 | } |
| 1987 | if ((u_int64_t)now >= k->cert->valid_before) { |
| 1988 | *reason = "Certificate invalid: expired"; |
| 1989 | return -1; |
| 1990 | } |
| 1991 | if (k->cert->nprincipals == 0) { |
| 1992 | if (require_principal) { |
| 1993 | *reason = "Certificate lacks principal list"; |
| 1994 | return -1; |
| 1995 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 1996 | } else if (name != NULL) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1997 | principal_matches = 0; |
| 1998 | for (i = 0; i < k->cert->nprincipals; i++) { |
| 1999 | if (strcmp(name, k->cert->principals[i]) == 0) { |
| 2000 | principal_matches = 1; |
| 2001 | break; |
| 2002 | } |
| 2003 | } |
| 2004 | if (!principal_matches) { |
| 2005 | *reason = "Certificate invalid: name is not a listed " |
| 2006 | "principal"; |
| 2007 | return -1; |
| 2008 | } |
| 2009 | } |
| 2010 | return 0; |
| 2011 | } |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 2012 | |
| 2013 | int |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2014 | key_cert_is_legacy(const Key *k) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 2015 | { |
| 2016 | switch (k->type) { |
| 2017 | case KEY_DSA_CERT_V00: |
| 2018 | case KEY_RSA_CERT_V00: |
| 2019 | return 1; |
| 2020 | default: |
| 2021 | return 0; |
| 2022 | } |
| 2023 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2024 | |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 2025 | /* XXX: these are really begging for a table-driven approach */ |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2026 | int |
| 2027 | key_curve_name_to_nid(const char *name) |
| 2028 | { |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2029 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2030 | if (strcmp(name, "nistp256") == 0) |
| 2031 | return NID_X9_62_prime256v1; |
| 2032 | else if (strcmp(name, "nistp384") == 0) |
| 2033 | return NID_secp384r1; |
| 2034 | else if (strcmp(name, "nistp521") == 0) |
| 2035 | return NID_secp521r1; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2036 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2037 | |
| 2038 | debug("%s: unsupported EC curve name \"%.100s\"", __func__, name); |
| 2039 | return -1; |
| 2040 | } |
| 2041 | |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 2042 | u_int |
| 2043 | key_curve_nid_to_bits(int nid) |
| 2044 | { |
| 2045 | switch (nid) { |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2046 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 2047 | case NID_X9_62_prime256v1: |
| 2048 | return 256; |
| 2049 | case NID_secp384r1: |
| 2050 | return 384; |
| 2051 | case NID_secp521r1: |
| 2052 | return 521; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2053 | #endif |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 2054 | default: |
| 2055 | error("%s: unsupported EC curve nid %d", __func__, nid); |
| 2056 | return 0; |
| 2057 | } |
| 2058 | } |
| 2059 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2060 | const char * |
| 2061 | key_curve_nid_to_name(int nid) |
| 2062 | { |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2063 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2064 | if (nid == NID_X9_62_prime256v1) |
| 2065 | return "nistp256"; |
| 2066 | else if (nid == NID_secp384r1) |
| 2067 | return "nistp384"; |
| 2068 | else if (nid == NID_secp521r1) |
| 2069 | return "nistp521"; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2070 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2071 | error("%s: unsupported EC curve nid %d", __func__, nid); |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2075 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 2076 | const EVP_MD * |
| 2077 | key_ec_nid_to_evpmd(int nid) |
| 2078 | { |
| 2079 | int kbits = key_curve_nid_to_bits(nid); |
| 2080 | |
| 2081 | if (kbits == 0) |
| 2082 | fatal("%s: invalid nid %d", __func__, nid); |
| 2083 | /* RFC5656 section 6.2.1 */ |
| 2084 | if (kbits <= 256) |
| 2085 | return EVP_sha256(); |
| 2086 | else if (kbits <= 384) |
| 2087 | return EVP_sha384(); |
| 2088 | else |
| 2089 | return EVP_sha512(); |
| 2090 | } |
| 2091 | |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 2092 | int |
| 2093 | key_ec_validate_public(const EC_GROUP *group, const EC_POINT *public) |
| 2094 | { |
| 2095 | BN_CTX *bnctx; |
| 2096 | EC_POINT *nq = NULL; |
| 2097 | BIGNUM *order, *x, *y, *tmp; |
| 2098 | int ret = -1; |
| 2099 | |
| 2100 | if ((bnctx = BN_CTX_new()) == NULL) |
| 2101 | fatal("%s: BN_CTX_new failed", __func__); |
| 2102 | BN_CTX_start(bnctx); |
| 2103 | |
| 2104 | /* |
| 2105 | * We shouldn't ever hit this case because bignum_get_ecpoint() |
| 2106 | * refuses to load GF2m points. |
| 2107 | */ |
| 2108 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
| 2109 | NID_X9_62_prime_field) { |
| 2110 | error("%s: group is not a prime field", __func__); |
| 2111 | goto out; |
| 2112 | } |
| 2113 | |
| 2114 | /* Q != infinity */ |
| 2115 | if (EC_POINT_is_at_infinity(group, public)) { |
| 2116 | error("%s: received degenerate public key (infinity)", |
| 2117 | __func__); |
| 2118 | goto out; |
| 2119 | } |
| 2120 | |
| 2121 | if ((x = BN_CTX_get(bnctx)) == NULL || |
| 2122 | (y = BN_CTX_get(bnctx)) == NULL || |
| 2123 | (order = BN_CTX_get(bnctx)) == NULL || |
| 2124 | (tmp = BN_CTX_get(bnctx)) == NULL) |
| 2125 | fatal("%s: BN_CTX_get failed", __func__); |
| 2126 | |
| 2127 | /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */ |
| 2128 | if (EC_GROUP_get_order(group, order, bnctx) != 1) |
| 2129 | fatal("%s: EC_GROUP_get_order failed", __func__); |
| 2130 | if (EC_POINT_get_affine_coordinates_GFp(group, public, |
| 2131 | x, y, bnctx) != 1) |
| 2132 | fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__); |
| 2133 | if (BN_num_bits(x) <= BN_num_bits(order) / 2) { |
| 2134 | error("%s: public key x coordinate too small: " |
| 2135 | "bits(x) = %d, bits(order)/2 = %d", __func__, |
| 2136 | BN_num_bits(x), BN_num_bits(order) / 2); |
| 2137 | goto out; |
| 2138 | } |
| 2139 | if (BN_num_bits(y) <= BN_num_bits(order) / 2) { |
| 2140 | error("%s: public key y coordinate too small: " |
| 2141 | "bits(y) = %d, bits(order)/2 = %d", __func__, |
| 2142 | BN_num_bits(x), BN_num_bits(order) / 2); |
| 2143 | goto out; |
| 2144 | } |
| 2145 | |
| 2146 | /* nQ == infinity (n == order of subgroup) */ |
| 2147 | if ((nq = EC_POINT_new(group)) == NULL) |
| 2148 | fatal("%s: BN_CTX_tmp failed", __func__); |
| 2149 | if (EC_POINT_mul(group, nq, NULL, public, order, bnctx) != 1) |
| 2150 | fatal("%s: EC_GROUP_mul failed", __func__); |
| 2151 | if (EC_POINT_is_at_infinity(group, nq) != 1) { |
| 2152 | error("%s: received degenerate public key (nQ != infinity)", |
| 2153 | __func__); |
| 2154 | goto out; |
| 2155 | } |
| 2156 | |
| 2157 | /* x < order - 1, y < order - 1 */ |
| 2158 | if (!BN_sub(tmp, order, BN_value_one())) |
| 2159 | fatal("%s: BN_sub failed", __func__); |
| 2160 | if (BN_cmp(x, tmp) >= 0) { |
| 2161 | error("%s: public key x coordinate >= group order - 1", |
| 2162 | __func__); |
| 2163 | goto out; |
| 2164 | } |
| 2165 | if (BN_cmp(y, tmp) >= 0) { |
| 2166 | error("%s: public key y coordinate >= group order - 1", |
| 2167 | __func__); |
| 2168 | goto out; |
| 2169 | } |
| 2170 | ret = 0; |
| 2171 | out: |
| 2172 | BN_CTX_free(bnctx); |
| 2173 | EC_POINT_free(nq); |
| 2174 | return ret; |
| 2175 | } |
| 2176 | |
| 2177 | int |
| 2178 | key_ec_validate_private(const EC_KEY *key) |
| 2179 | { |
| 2180 | BN_CTX *bnctx; |
| 2181 | BIGNUM *order, *tmp; |
| 2182 | int ret = -1; |
| 2183 | |
| 2184 | if ((bnctx = BN_CTX_new()) == NULL) |
| 2185 | fatal("%s: BN_CTX_new failed", __func__); |
| 2186 | BN_CTX_start(bnctx); |
| 2187 | |
| 2188 | if ((order = BN_CTX_get(bnctx)) == NULL || |
| 2189 | (tmp = BN_CTX_get(bnctx)) == NULL) |
| 2190 | fatal("%s: BN_CTX_get failed", __func__); |
| 2191 | |
| 2192 | /* log2(private) > log2(order)/2 */ |
| 2193 | if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, bnctx) != 1) |
| 2194 | fatal("%s: EC_GROUP_get_order failed", __func__); |
| 2195 | if (BN_num_bits(EC_KEY_get0_private_key(key)) <= |
| 2196 | BN_num_bits(order) / 2) { |
| 2197 | error("%s: private key too small: " |
| 2198 | "bits(y) = %d, bits(order)/2 = %d", __func__, |
| 2199 | BN_num_bits(EC_KEY_get0_private_key(key)), |
| 2200 | BN_num_bits(order) / 2); |
| 2201 | goto out; |
| 2202 | } |
| 2203 | |
| 2204 | /* private < order - 1 */ |
| 2205 | if (!BN_sub(tmp, order, BN_value_one())) |
| 2206 | fatal("%s: BN_sub failed", __func__); |
| 2207 | if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0) { |
| 2208 | error("%s: private key >= group order - 1", __func__); |
| 2209 | goto out; |
| 2210 | } |
| 2211 | ret = 0; |
| 2212 | out: |
| 2213 | BN_CTX_free(bnctx); |
| 2214 | return ret; |
| 2215 | } |
| 2216 | |
| 2217 | #if defined(DEBUG_KEXECDH) || defined(DEBUG_PK) |
| 2218 | void |
| 2219 | key_dump_ec_point(const EC_GROUP *group, const EC_POINT *point) |
| 2220 | { |
| 2221 | BIGNUM *x, *y; |
| 2222 | BN_CTX *bnctx; |
| 2223 | |
| 2224 | if (point == NULL) { |
| 2225 | fputs("point=(NULL)\n", stderr); |
| 2226 | return; |
| 2227 | } |
| 2228 | if ((bnctx = BN_CTX_new()) == NULL) |
| 2229 | fatal("%s: BN_CTX_new failed", __func__); |
| 2230 | BN_CTX_start(bnctx); |
| 2231 | if ((x = BN_CTX_get(bnctx)) == NULL || (y = BN_CTX_get(bnctx)) == NULL) |
| 2232 | fatal("%s: BN_CTX_get failed", __func__); |
| 2233 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
| 2234 | NID_X9_62_prime_field) |
| 2235 | fatal("%s: group is not a prime field", __func__); |
| 2236 | if (EC_POINT_get_affine_coordinates_GFp(group, point, x, y, bnctx) != 1) |
| 2237 | fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__); |
| 2238 | fputs("x=", stderr); |
| 2239 | BN_print_fp(stderr, x); |
| 2240 | fputs("\ny=", stderr); |
| 2241 | BN_print_fp(stderr, y); |
| 2242 | fputs("\n", stderr); |
| 2243 | BN_CTX_free(bnctx); |
| 2244 | } |
| 2245 | |
| 2246 | void |
| 2247 | key_dump_ec_key(const EC_KEY *key) |
| 2248 | { |
| 2249 | const BIGNUM *exponent; |
| 2250 | |
| 2251 | key_dump_ec_point(EC_KEY_get0_group(key), EC_KEY_get0_public_key(key)); |
| 2252 | fputs("exponent=", stderr); |
| 2253 | if ((exponent = EC_KEY_get0_private_key(key)) == NULL) |
| 2254 | fputs("(NULL)", stderr); |
| 2255 | else |
| 2256 | BN_print_fp(stderr, EC_KEY_get0_private_key(key)); |
| 2257 | fputs("\n", stderr); |
| 2258 | } |
| 2259 | #endif /* defined(DEBUG_KEXECDH) || defined(DEBUG_PK) */ |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 2260 | #endif /* OPENSSL_HAS_ECC */ |