djm@openbsd.org | a571dbc | 2016-10-04 21:34:40 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: sshkey.c,v 1.40 2016/10/04 21:34:40 djm Exp $ */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
| 4 | * Copyright (c) 2008 Alexander von Gernler. All rights reserved. |
| 5 | * Copyright (c) 2010,2011 Damien Miller. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "includes.h" |
| 29 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 30 | #include <sys/types.h> |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 31 | #include <netinet/in.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 32 | |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 33 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 34 | #include <openssl/evp.h> |
| 35 | #include <openssl/err.h> |
| 36 | #include <openssl/pem.h> |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 37 | #endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 38 | |
| 39 | #include "crypto_api.h" |
| 40 | |
| 41 | #include <errno.h> |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 42 | #include <limits.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <string.h> |
Damien Miller | d16bdd8 | 2014-12-22 10:18:09 +1100 | [diff] [blame] | 45 | #include <resolv.h> |
Damien Miller | 82b2482 | 2014-07-02 17:43:41 +1000 | [diff] [blame] | 46 | #ifdef HAVE_UTIL_H |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 47 | #include <util.h> |
Damien Miller | 82b2482 | 2014-07-02 17:43:41 +1000 | [diff] [blame] | 48 | #endif /* HAVE_UTIL_H */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 49 | |
| 50 | #include "ssh2.h" |
| 51 | #include "ssherr.h" |
| 52 | #include "misc.h" |
| 53 | #include "sshbuf.h" |
| 54 | #include "rsa.h" |
| 55 | #include "cipher.h" |
| 56 | #include "digest.h" |
| 57 | #define SSHKEY_INTERNAL |
| 58 | #include "sshkey.h" |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 59 | #include "match.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 60 | |
| 61 | /* openssh private key file format */ |
| 62 | #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n" |
| 63 | #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n" |
| 64 | #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1) |
| 65 | #define MARK_END_LEN (sizeof(MARK_END) - 1) |
| 66 | #define KDFNAME "bcrypt" |
| 67 | #define AUTH_MAGIC "openssh-key-v1" |
| 68 | #define SALT_LEN 16 |
| 69 | #define DEFAULT_CIPHERNAME "aes256-cbc" |
| 70 | #define DEFAULT_ROUNDS 16 |
| 71 | |
| 72 | /* Version identification string for SSH v1 identity files. */ |
| 73 | #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n" |
| 74 | |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 75 | static int sshkey_from_blob_internal(struct sshbuf *buf, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 76 | struct sshkey **keyp, int allow_cert); |
| 77 | |
| 78 | /* Supported key types */ |
| 79 | struct keytype { |
| 80 | const char *name; |
| 81 | const char *shortname; |
| 82 | int type; |
| 83 | int nid; |
| 84 | int cert; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 85 | int sigonly; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 86 | }; |
| 87 | static const struct keytype keytypes[] = { |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 88 | { "ssh-ed25519", "ED25519", KEY_ED25519, 0, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 89 | { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT", |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 90 | KEY_ED25519_CERT, 0, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 91 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 92 | { NULL, "RSA1", KEY_RSA1, 0, 0, 0 }, |
| 93 | { "ssh-rsa", "RSA", KEY_RSA, 0, 0, 0 }, |
| 94 | { "rsa-sha2-256", "RSA", KEY_RSA, 0, 0, 1 }, |
| 95 | { "rsa-sha2-512", "RSA", KEY_RSA, 0, 0, 1 }, |
| 96 | { "ssh-dss", "DSA", KEY_DSA, 0, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 97 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 98 | { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA, NID_X9_62_prime256v1, 0, 0 }, |
| 99 | { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA, NID_secp384r1, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 100 | # ifdef OPENSSL_HAS_NISTP521 |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 101 | { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 102 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 103 | # endif /* OPENSSL_HAS_ECC */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 104 | { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT, 0, 1, 0 }, |
| 105 | { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT, 0, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 106 | # ifdef OPENSSL_HAS_ECC |
| 107 | { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT", |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 108 | KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 109 | { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT", |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 110 | KEY_ECDSA_CERT, NID_secp384r1, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 111 | # ifdef OPENSSL_HAS_NISTP521 |
| 112 | { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT", |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 113 | KEY_ECDSA_CERT, NID_secp521r1, 1, 0 }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 114 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 115 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 116 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 117 | { NULL, NULL, -1, -1, 0, 0 } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | const char * |
| 121 | sshkey_type(const struct sshkey *k) |
| 122 | { |
| 123 | const struct keytype *kt; |
| 124 | |
| 125 | for (kt = keytypes; kt->type != -1; kt++) { |
| 126 | if (kt->type == k->type) |
| 127 | return kt->shortname; |
| 128 | } |
| 129 | return "unknown"; |
| 130 | } |
| 131 | |
| 132 | static const char * |
| 133 | sshkey_ssh_name_from_type_nid(int type, int nid) |
| 134 | { |
| 135 | const struct keytype *kt; |
| 136 | |
| 137 | for (kt = keytypes; kt->type != -1; kt++) { |
| 138 | if (kt->type == type && (kt->nid == 0 || kt->nid == nid)) |
| 139 | return kt->name; |
| 140 | } |
| 141 | return "ssh-unknown"; |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | sshkey_type_is_cert(int type) |
| 146 | { |
| 147 | const struct keytype *kt; |
| 148 | |
| 149 | for (kt = keytypes; kt->type != -1; kt++) { |
| 150 | if (kt->type == type) |
| 151 | return kt->cert; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | const char * |
| 157 | sshkey_ssh_name(const struct sshkey *k) |
| 158 | { |
| 159 | return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid); |
| 160 | } |
| 161 | |
| 162 | const char * |
| 163 | sshkey_ssh_name_plain(const struct sshkey *k) |
| 164 | { |
| 165 | return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type), |
| 166 | k->ecdsa_nid); |
| 167 | } |
| 168 | |
| 169 | int |
| 170 | sshkey_type_from_name(const char *name) |
| 171 | { |
| 172 | const struct keytype *kt; |
| 173 | |
| 174 | for (kt = keytypes; kt->type != -1; kt++) { |
| 175 | /* Only allow shortname matches for plain key types */ |
| 176 | if ((kt->name != NULL && strcmp(name, kt->name) == 0) || |
| 177 | (!kt->cert && strcasecmp(kt->shortname, name) == 0)) |
| 178 | return kt->type; |
| 179 | } |
| 180 | return KEY_UNSPEC; |
| 181 | } |
| 182 | |
| 183 | int |
| 184 | sshkey_ecdsa_nid_from_name(const char *name) |
| 185 | { |
| 186 | const struct keytype *kt; |
| 187 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 188 | for (kt = keytypes; kt->type != -1; kt++) { |
| 189 | if (kt->type != KEY_ECDSA && kt->type != KEY_ECDSA_CERT) |
| 190 | continue; |
| 191 | if (kt->name != NULL && strcmp(name, kt->name) == 0) |
| 192 | return kt->nid; |
| 193 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | char * |
djm@openbsd.org | 130f5df | 2016-09-12 23:31:27 +0000 | [diff] [blame] | 198 | sshkey_alg_list(int certs_only, int plain_only, char sep) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 199 | { |
| 200 | char *tmp, *ret = NULL; |
| 201 | size_t nlen, rlen = 0; |
| 202 | const struct keytype *kt; |
| 203 | |
| 204 | for (kt = keytypes; kt->type != -1; kt++) { |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 205 | if (kt->name == NULL || kt->sigonly) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 206 | continue; |
| 207 | if ((certs_only && !kt->cert) || (plain_only && kt->cert)) |
| 208 | continue; |
| 209 | if (ret != NULL) |
djm@openbsd.org | 130f5df | 2016-09-12 23:31:27 +0000 | [diff] [blame] | 210 | ret[rlen++] = sep; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 211 | nlen = strlen(kt->name); |
| 212 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
| 213 | free(ret); |
| 214 | return NULL; |
| 215 | } |
| 216 | ret = tmp; |
| 217 | memcpy(ret + rlen, kt->name, nlen + 1); |
| 218 | rlen += nlen; |
| 219 | } |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | int |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 224 | sshkey_names_valid2(const char *names, int allow_wildcard) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 225 | { |
| 226 | char *s, *cp, *p; |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 227 | const struct keytype *kt; |
| 228 | int type; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 229 | |
| 230 | if (names == NULL || strcmp(names, "") == 0) |
| 231 | return 0; |
| 232 | if ((s = cp = strdup(names)) == NULL) |
| 233 | return 0; |
| 234 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
| 235 | (p = strsep(&cp, ","))) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 236 | type = sshkey_type_from_name(p); |
| 237 | if (type == KEY_RSA1) { |
| 238 | free(s); |
| 239 | return 0; |
| 240 | } |
| 241 | if (type == KEY_UNSPEC) { |
| 242 | if (allow_wildcard) { |
| 243 | /* |
| 244 | * Try matching key types against the string. |
| 245 | * If any has a positive or negative match then |
| 246 | * the component is accepted. |
| 247 | */ |
| 248 | for (kt = keytypes; kt->type != -1; kt++) { |
| 249 | if (kt->type == KEY_RSA1) |
| 250 | continue; |
| 251 | if (match_pattern_list(kt->name, |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 252 | p, 0) != 0) |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | if (kt->type != -1) |
| 256 | continue; |
| 257 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 258 | free(s); |
| 259 | return 0; |
| 260 | } |
| 261 | } |
| 262 | free(s); |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | u_int |
| 267 | sshkey_size(const struct sshkey *k) |
| 268 | { |
| 269 | switch (k->type) { |
| 270 | #ifdef WITH_OPENSSL |
| 271 | case KEY_RSA1: |
| 272 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 273 | case KEY_RSA_CERT: |
| 274 | return BN_num_bits(k->rsa->n); |
| 275 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 276 | case KEY_DSA_CERT: |
| 277 | return BN_num_bits(k->dsa->p); |
| 278 | case KEY_ECDSA: |
| 279 | case KEY_ECDSA_CERT: |
| 280 | return sshkey_curve_nid_to_bits(k->ecdsa_nid); |
| 281 | #endif /* WITH_OPENSSL */ |
| 282 | case KEY_ED25519: |
| 283 | case KEY_ED25519_CERT: |
| 284 | return 256; /* XXX */ |
| 285 | } |
| 286 | return 0; |
| 287 | } |
| 288 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 289 | static int |
| 290 | sshkey_type_is_valid_ca(int type) |
| 291 | { |
| 292 | switch (type) { |
| 293 | case KEY_RSA: |
| 294 | case KEY_DSA: |
| 295 | case KEY_ECDSA: |
| 296 | case KEY_ED25519: |
| 297 | return 1; |
| 298 | default: |
| 299 | return 0; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | int |
| 304 | sshkey_is_cert(const struct sshkey *k) |
| 305 | { |
| 306 | if (k == NULL) |
| 307 | return 0; |
| 308 | return sshkey_type_is_cert(k->type); |
| 309 | } |
| 310 | |
| 311 | /* Return the cert-less equivalent to a certified key type */ |
| 312 | int |
| 313 | sshkey_type_plain(int type) |
| 314 | { |
| 315 | switch (type) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 316 | case KEY_RSA_CERT: |
| 317 | return KEY_RSA; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 318 | case KEY_DSA_CERT: |
| 319 | return KEY_DSA; |
| 320 | case KEY_ECDSA_CERT: |
| 321 | return KEY_ECDSA; |
| 322 | case KEY_ED25519_CERT: |
| 323 | return KEY_ED25519; |
| 324 | default: |
| 325 | return type; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | #ifdef WITH_OPENSSL |
| 330 | /* XXX: these are really begging for a table-driven approach */ |
| 331 | int |
| 332 | sshkey_curve_name_to_nid(const char *name) |
| 333 | { |
| 334 | if (strcmp(name, "nistp256") == 0) |
| 335 | return NID_X9_62_prime256v1; |
| 336 | else if (strcmp(name, "nistp384") == 0) |
| 337 | return NID_secp384r1; |
| 338 | # ifdef OPENSSL_HAS_NISTP521 |
| 339 | else if (strcmp(name, "nistp521") == 0) |
| 340 | return NID_secp521r1; |
| 341 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 342 | else |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | u_int |
| 347 | sshkey_curve_nid_to_bits(int nid) |
| 348 | { |
| 349 | switch (nid) { |
| 350 | case NID_X9_62_prime256v1: |
| 351 | return 256; |
| 352 | case NID_secp384r1: |
| 353 | return 384; |
| 354 | # ifdef OPENSSL_HAS_NISTP521 |
| 355 | case NID_secp521r1: |
| 356 | return 521; |
| 357 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 358 | default: |
| 359 | return 0; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | int |
| 364 | sshkey_ecdsa_bits_to_nid(int bits) |
| 365 | { |
| 366 | switch (bits) { |
| 367 | case 256: |
| 368 | return NID_X9_62_prime256v1; |
| 369 | case 384: |
| 370 | return NID_secp384r1; |
| 371 | # ifdef OPENSSL_HAS_NISTP521 |
| 372 | case 521: |
| 373 | return NID_secp521r1; |
| 374 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 375 | default: |
| 376 | return -1; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | const char * |
| 381 | sshkey_curve_nid_to_name(int nid) |
| 382 | { |
| 383 | switch (nid) { |
| 384 | case NID_X9_62_prime256v1: |
| 385 | return "nistp256"; |
| 386 | case NID_secp384r1: |
| 387 | return "nistp384"; |
| 388 | # ifdef OPENSSL_HAS_NISTP521 |
| 389 | case NID_secp521r1: |
| 390 | return "nistp521"; |
| 391 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 392 | default: |
| 393 | return NULL; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | int |
| 398 | sshkey_ec_nid_to_hash_alg(int nid) |
| 399 | { |
| 400 | int kbits = sshkey_curve_nid_to_bits(nid); |
| 401 | |
| 402 | if (kbits <= 0) |
| 403 | return -1; |
| 404 | |
| 405 | /* RFC5656 section 6.2.1 */ |
| 406 | if (kbits <= 256) |
| 407 | return SSH_DIGEST_SHA256; |
| 408 | else if (kbits <= 384) |
| 409 | return SSH_DIGEST_SHA384; |
| 410 | else |
| 411 | return SSH_DIGEST_SHA512; |
| 412 | } |
| 413 | #endif /* WITH_OPENSSL */ |
| 414 | |
| 415 | static void |
| 416 | cert_free(struct sshkey_cert *cert) |
| 417 | { |
| 418 | u_int i; |
| 419 | |
| 420 | if (cert == NULL) |
| 421 | return; |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 422 | sshbuf_free(cert->certblob); |
| 423 | sshbuf_free(cert->critical); |
| 424 | sshbuf_free(cert->extensions); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 425 | free(cert->key_id); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 426 | for (i = 0; i < cert->nprincipals; i++) |
| 427 | free(cert->principals[i]); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 428 | free(cert->principals); |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 429 | sshkey_free(cert->signature_key); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 430 | explicit_bzero(cert, sizeof(*cert)); |
| 431 | free(cert); |
| 432 | } |
| 433 | |
| 434 | static struct sshkey_cert * |
| 435 | cert_new(void) |
| 436 | { |
| 437 | struct sshkey_cert *cert; |
| 438 | |
| 439 | if ((cert = calloc(1, sizeof(*cert))) == NULL) |
| 440 | return NULL; |
| 441 | if ((cert->certblob = sshbuf_new()) == NULL || |
| 442 | (cert->critical = sshbuf_new()) == NULL || |
| 443 | (cert->extensions = sshbuf_new()) == NULL) { |
| 444 | cert_free(cert); |
| 445 | return NULL; |
| 446 | } |
| 447 | cert->key_id = NULL; |
| 448 | cert->principals = NULL; |
| 449 | cert->signature_key = NULL; |
| 450 | return cert; |
| 451 | } |
| 452 | |
| 453 | struct sshkey * |
| 454 | sshkey_new(int type) |
| 455 | { |
| 456 | struct sshkey *k; |
| 457 | #ifdef WITH_OPENSSL |
| 458 | RSA *rsa; |
| 459 | DSA *dsa; |
| 460 | #endif /* WITH_OPENSSL */ |
| 461 | |
| 462 | if ((k = calloc(1, sizeof(*k))) == NULL) |
| 463 | return NULL; |
| 464 | k->type = type; |
| 465 | k->ecdsa = NULL; |
| 466 | k->ecdsa_nid = -1; |
| 467 | k->dsa = NULL; |
| 468 | k->rsa = NULL; |
| 469 | k->cert = NULL; |
| 470 | k->ed25519_sk = NULL; |
| 471 | k->ed25519_pk = NULL; |
| 472 | switch (k->type) { |
| 473 | #ifdef WITH_OPENSSL |
| 474 | case KEY_RSA1: |
| 475 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 476 | case KEY_RSA_CERT: |
| 477 | if ((rsa = RSA_new()) == NULL || |
| 478 | (rsa->n = BN_new()) == NULL || |
| 479 | (rsa->e = BN_new()) == NULL) { |
| 480 | if (rsa != NULL) |
| 481 | RSA_free(rsa); |
| 482 | free(k); |
| 483 | return NULL; |
| 484 | } |
| 485 | k->rsa = rsa; |
| 486 | break; |
| 487 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 488 | case KEY_DSA_CERT: |
| 489 | if ((dsa = DSA_new()) == NULL || |
| 490 | (dsa->p = BN_new()) == NULL || |
| 491 | (dsa->q = BN_new()) == NULL || |
| 492 | (dsa->g = BN_new()) == NULL || |
| 493 | (dsa->pub_key = BN_new()) == NULL) { |
| 494 | if (dsa != NULL) |
| 495 | DSA_free(dsa); |
| 496 | free(k); |
| 497 | return NULL; |
| 498 | } |
| 499 | k->dsa = dsa; |
| 500 | break; |
| 501 | case KEY_ECDSA: |
| 502 | case KEY_ECDSA_CERT: |
| 503 | /* Cannot do anything until we know the group */ |
| 504 | break; |
| 505 | #endif /* WITH_OPENSSL */ |
| 506 | case KEY_ED25519: |
| 507 | case KEY_ED25519_CERT: |
| 508 | /* no need to prealloc */ |
| 509 | break; |
| 510 | case KEY_UNSPEC: |
| 511 | break; |
| 512 | default: |
| 513 | free(k); |
| 514 | return NULL; |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | if (sshkey_is_cert(k)) { |
| 519 | if ((k->cert = cert_new()) == NULL) { |
| 520 | sshkey_free(k); |
| 521 | return NULL; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return k; |
| 526 | } |
| 527 | |
| 528 | int |
| 529 | sshkey_add_private(struct sshkey *k) |
| 530 | { |
| 531 | switch (k->type) { |
| 532 | #ifdef WITH_OPENSSL |
| 533 | case KEY_RSA1: |
| 534 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 535 | case KEY_RSA_CERT: |
| 536 | #define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL) |
| 537 | if (bn_maybe_alloc_failed(k->rsa->d) || |
| 538 | bn_maybe_alloc_failed(k->rsa->iqmp) || |
| 539 | bn_maybe_alloc_failed(k->rsa->q) || |
| 540 | bn_maybe_alloc_failed(k->rsa->p) || |
| 541 | bn_maybe_alloc_failed(k->rsa->dmq1) || |
| 542 | bn_maybe_alloc_failed(k->rsa->dmp1)) |
| 543 | return SSH_ERR_ALLOC_FAIL; |
| 544 | break; |
| 545 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 546 | case KEY_DSA_CERT: |
| 547 | if (bn_maybe_alloc_failed(k->dsa->priv_key)) |
| 548 | return SSH_ERR_ALLOC_FAIL; |
| 549 | break; |
| 550 | #undef bn_maybe_alloc_failed |
| 551 | case KEY_ECDSA: |
| 552 | case KEY_ECDSA_CERT: |
| 553 | /* Cannot do anything until we know the group */ |
| 554 | break; |
| 555 | #endif /* WITH_OPENSSL */ |
| 556 | case KEY_ED25519: |
| 557 | case KEY_ED25519_CERT: |
| 558 | /* no need to prealloc */ |
| 559 | break; |
| 560 | case KEY_UNSPEC: |
| 561 | break; |
| 562 | default: |
| 563 | return SSH_ERR_INVALID_ARGUMENT; |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | struct sshkey * |
| 569 | sshkey_new_private(int type) |
| 570 | { |
| 571 | struct sshkey *k = sshkey_new(type); |
| 572 | |
| 573 | if (k == NULL) |
| 574 | return NULL; |
| 575 | if (sshkey_add_private(k) != 0) { |
| 576 | sshkey_free(k); |
| 577 | return NULL; |
| 578 | } |
| 579 | return k; |
| 580 | } |
| 581 | |
| 582 | void |
| 583 | sshkey_free(struct sshkey *k) |
| 584 | { |
| 585 | if (k == NULL) |
| 586 | return; |
| 587 | switch (k->type) { |
| 588 | #ifdef WITH_OPENSSL |
| 589 | case KEY_RSA1: |
| 590 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 591 | case KEY_RSA_CERT: |
| 592 | if (k->rsa != NULL) |
| 593 | RSA_free(k->rsa); |
| 594 | k->rsa = NULL; |
| 595 | break; |
| 596 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 597 | case KEY_DSA_CERT: |
| 598 | if (k->dsa != NULL) |
| 599 | DSA_free(k->dsa); |
| 600 | k->dsa = NULL; |
| 601 | break; |
| 602 | # ifdef OPENSSL_HAS_ECC |
| 603 | case KEY_ECDSA: |
| 604 | case KEY_ECDSA_CERT: |
| 605 | if (k->ecdsa != NULL) |
| 606 | EC_KEY_free(k->ecdsa); |
| 607 | k->ecdsa = NULL; |
| 608 | break; |
| 609 | # endif /* OPENSSL_HAS_ECC */ |
| 610 | #endif /* WITH_OPENSSL */ |
| 611 | case KEY_ED25519: |
| 612 | case KEY_ED25519_CERT: |
| 613 | if (k->ed25519_pk) { |
| 614 | explicit_bzero(k->ed25519_pk, ED25519_PK_SZ); |
| 615 | free(k->ed25519_pk); |
| 616 | k->ed25519_pk = NULL; |
| 617 | } |
| 618 | if (k->ed25519_sk) { |
| 619 | explicit_bzero(k->ed25519_sk, ED25519_SK_SZ); |
| 620 | free(k->ed25519_sk); |
| 621 | k->ed25519_sk = NULL; |
| 622 | } |
| 623 | break; |
| 624 | case KEY_UNSPEC: |
| 625 | break; |
| 626 | default: |
| 627 | break; |
| 628 | } |
| 629 | if (sshkey_is_cert(k)) |
| 630 | cert_free(k->cert); |
| 631 | explicit_bzero(k, sizeof(*k)); |
| 632 | free(k); |
| 633 | } |
| 634 | |
| 635 | static int |
| 636 | cert_compare(struct sshkey_cert *a, struct sshkey_cert *b) |
| 637 | { |
| 638 | if (a == NULL && b == NULL) |
| 639 | return 1; |
| 640 | if (a == NULL || b == NULL) |
| 641 | return 0; |
| 642 | if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob)) |
| 643 | return 0; |
| 644 | if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob), |
| 645 | sshbuf_len(a->certblob)) != 0) |
| 646 | return 0; |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Compare public portions of key only, allowing comparisons between |
| 652 | * certificates and plain keys too. |
| 653 | */ |
| 654 | int |
| 655 | sshkey_equal_public(const struct sshkey *a, const struct sshkey *b) |
| 656 | { |
Darren Tucker | 948a177 | 2014-07-22 01:07:11 +1000 | [diff] [blame] | 657 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 658 | BN_CTX *bnctx; |
Darren Tucker | 948a177 | 2014-07-22 01:07:11 +1000 | [diff] [blame] | 659 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 660 | |
| 661 | if (a == NULL || b == NULL || |
| 662 | sshkey_type_plain(a->type) != sshkey_type_plain(b->type)) |
| 663 | return 0; |
| 664 | |
| 665 | switch (a->type) { |
| 666 | #ifdef WITH_OPENSSL |
| 667 | case KEY_RSA1: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 668 | case KEY_RSA_CERT: |
| 669 | case KEY_RSA: |
| 670 | return a->rsa != NULL && b->rsa != NULL && |
| 671 | BN_cmp(a->rsa->e, b->rsa->e) == 0 && |
| 672 | BN_cmp(a->rsa->n, b->rsa->n) == 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 673 | case KEY_DSA_CERT: |
| 674 | case KEY_DSA: |
| 675 | return a->dsa != NULL && b->dsa != NULL && |
| 676 | BN_cmp(a->dsa->p, b->dsa->p) == 0 && |
| 677 | BN_cmp(a->dsa->q, b->dsa->q) == 0 && |
| 678 | BN_cmp(a->dsa->g, b->dsa->g) == 0 && |
| 679 | BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0; |
| 680 | # ifdef OPENSSL_HAS_ECC |
| 681 | case KEY_ECDSA_CERT: |
| 682 | case KEY_ECDSA: |
| 683 | if (a->ecdsa == NULL || b->ecdsa == NULL || |
| 684 | EC_KEY_get0_public_key(a->ecdsa) == NULL || |
| 685 | EC_KEY_get0_public_key(b->ecdsa) == NULL) |
| 686 | return 0; |
| 687 | if ((bnctx = BN_CTX_new()) == NULL) |
| 688 | return 0; |
| 689 | if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa), |
| 690 | EC_KEY_get0_group(b->ecdsa), bnctx) != 0 || |
| 691 | EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa), |
| 692 | EC_KEY_get0_public_key(a->ecdsa), |
| 693 | EC_KEY_get0_public_key(b->ecdsa), bnctx) != 0) { |
| 694 | BN_CTX_free(bnctx); |
| 695 | return 0; |
| 696 | } |
| 697 | BN_CTX_free(bnctx); |
| 698 | return 1; |
| 699 | # endif /* OPENSSL_HAS_ECC */ |
| 700 | #endif /* WITH_OPENSSL */ |
| 701 | case KEY_ED25519: |
| 702 | case KEY_ED25519_CERT: |
| 703 | return a->ed25519_pk != NULL && b->ed25519_pk != NULL && |
| 704 | memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) == 0; |
| 705 | default: |
| 706 | return 0; |
| 707 | } |
| 708 | /* NOTREACHED */ |
| 709 | } |
| 710 | |
| 711 | int |
| 712 | sshkey_equal(const struct sshkey *a, const struct sshkey *b) |
| 713 | { |
| 714 | if (a == NULL || b == NULL || a->type != b->type) |
| 715 | return 0; |
| 716 | if (sshkey_is_cert(a)) { |
| 717 | if (!cert_compare(a->cert, b->cert)) |
| 718 | return 0; |
| 719 | } |
| 720 | return sshkey_equal_public(a, b); |
| 721 | } |
| 722 | |
| 723 | static int |
| 724 | to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain) |
| 725 | { |
| 726 | int type, ret = SSH_ERR_INTERNAL_ERROR; |
| 727 | const char *typename; |
| 728 | |
| 729 | if (key == NULL) |
| 730 | return SSH_ERR_INVALID_ARGUMENT; |
| 731 | |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 732 | if (sshkey_is_cert(key)) { |
| 733 | if (key->cert == NULL) |
| 734 | return SSH_ERR_EXPECTED_CERT; |
| 735 | if (sshbuf_len(key->cert->certblob) == 0) |
| 736 | return SSH_ERR_KEY_LACKS_CERTBLOB; |
| 737 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 738 | type = force_plain ? sshkey_type_plain(key->type) : key->type; |
| 739 | typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid); |
| 740 | |
| 741 | switch (type) { |
| 742 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 743 | case KEY_DSA_CERT: |
| 744 | case KEY_ECDSA_CERT: |
| 745 | case KEY_RSA_CERT: |
| 746 | #endif /* WITH_OPENSSL */ |
| 747 | case KEY_ED25519_CERT: |
| 748 | /* Use the existing blob */ |
| 749 | /* XXX modified flag? */ |
| 750 | if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0) |
| 751 | return ret; |
| 752 | break; |
| 753 | #ifdef WITH_OPENSSL |
| 754 | case KEY_DSA: |
| 755 | if (key->dsa == NULL) |
| 756 | return SSH_ERR_INVALID_ARGUMENT; |
| 757 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
| 758 | (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 || |
| 759 | (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 || |
| 760 | (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 || |
| 761 | (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0) |
| 762 | return ret; |
| 763 | break; |
Darren Tucker | d1a0421 | 2014-07-19 07:23:55 +1000 | [diff] [blame] | 764 | # ifdef OPENSSL_HAS_ECC |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 765 | case KEY_ECDSA: |
| 766 | if (key->ecdsa == NULL) |
| 767 | return SSH_ERR_INVALID_ARGUMENT; |
| 768 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
| 769 | (ret = sshbuf_put_cstring(b, |
| 770 | sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 || |
| 771 | (ret = sshbuf_put_eckey(b, key->ecdsa)) != 0) |
| 772 | return ret; |
| 773 | break; |
Darren Tucker | d1a0421 | 2014-07-19 07:23:55 +1000 | [diff] [blame] | 774 | # endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 775 | case KEY_RSA: |
| 776 | if (key->rsa == NULL) |
| 777 | return SSH_ERR_INVALID_ARGUMENT; |
| 778 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
| 779 | (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 || |
| 780 | (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0) |
| 781 | return ret; |
| 782 | break; |
| 783 | #endif /* WITH_OPENSSL */ |
| 784 | case KEY_ED25519: |
| 785 | if (key->ed25519_pk == NULL) |
| 786 | return SSH_ERR_INVALID_ARGUMENT; |
| 787 | if ((ret = sshbuf_put_cstring(b, typename)) != 0 || |
| 788 | (ret = sshbuf_put_string(b, |
| 789 | key->ed25519_pk, ED25519_PK_SZ)) != 0) |
| 790 | return ret; |
| 791 | break; |
| 792 | default: |
| 793 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 794 | } |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 799 | sshkey_putb(const struct sshkey *key, struct sshbuf *b) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 800 | { |
| 801 | return to_blob_buf(key, b, 0); |
| 802 | } |
| 803 | |
| 804 | int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 805 | sshkey_puts(const struct sshkey *key, struct sshbuf *b) |
| 806 | { |
| 807 | struct sshbuf *tmp; |
| 808 | int r; |
| 809 | |
| 810 | if ((tmp = sshbuf_new()) == NULL) |
| 811 | return SSH_ERR_ALLOC_FAIL; |
| 812 | r = to_blob_buf(key, tmp, 0); |
| 813 | if (r == 0) |
| 814 | r = sshbuf_put_stringb(b, tmp); |
| 815 | sshbuf_free(tmp); |
| 816 | return r; |
| 817 | } |
| 818 | |
| 819 | int |
| 820 | sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 821 | { |
| 822 | return to_blob_buf(key, b, 1); |
| 823 | } |
| 824 | |
| 825 | static int |
| 826 | to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain) |
| 827 | { |
| 828 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 829 | size_t len; |
| 830 | struct sshbuf *b = NULL; |
| 831 | |
| 832 | if (lenp != NULL) |
| 833 | *lenp = 0; |
| 834 | if (blobp != NULL) |
| 835 | *blobp = NULL; |
| 836 | if ((b = sshbuf_new()) == NULL) |
| 837 | return SSH_ERR_ALLOC_FAIL; |
| 838 | if ((ret = to_blob_buf(key, b, force_plain)) != 0) |
| 839 | goto out; |
| 840 | len = sshbuf_len(b); |
| 841 | if (lenp != NULL) |
| 842 | *lenp = len; |
| 843 | if (blobp != NULL) { |
| 844 | if ((*blobp = malloc(len)) == NULL) { |
| 845 | ret = SSH_ERR_ALLOC_FAIL; |
| 846 | goto out; |
| 847 | } |
| 848 | memcpy(*blobp, sshbuf_ptr(b), len); |
| 849 | } |
| 850 | ret = 0; |
| 851 | out: |
| 852 | sshbuf_free(b); |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | int |
| 857 | sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp) |
| 858 | { |
| 859 | return to_blob(key, blobp, lenp, 0); |
| 860 | } |
| 861 | |
| 862 | int |
| 863 | sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp) |
| 864 | { |
| 865 | return to_blob(key, blobp, lenp, 1); |
| 866 | } |
| 867 | |
| 868 | int |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 869 | sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 870 | u_char **retp, size_t *lenp) |
| 871 | { |
| 872 | u_char *blob = NULL, *ret = NULL; |
| 873 | size_t blob_len = 0; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 874 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 875 | |
| 876 | if (retp != NULL) |
| 877 | *retp = NULL; |
| 878 | if (lenp != NULL) |
| 879 | *lenp = 0; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 880 | if (ssh_digest_bytes(dgst_alg) == 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 881 | r = SSH_ERR_INVALID_ARGUMENT; |
| 882 | goto out; |
| 883 | } |
| 884 | |
| 885 | if (k->type == KEY_RSA1) { |
| 886 | #ifdef WITH_OPENSSL |
| 887 | int nlen = BN_num_bytes(k->rsa->n); |
| 888 | int elen = BN_num_bytes(k->rsa->e); |
| 889 | |
djm@openbsd.org | 27c3a9c | 2016-09-26 21:16:11 +0000 | [diff] [blame] | 890 | if (nlen < 0 || elen < 0 || nlen >= INT_MAX - elen) { |
| 891 | r = SSH_ERR_INVALID_FORMAT; |
| 892 | goto out; |
| 893 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 894 | blob_len = nlen + elen; |
djm@openbsd.org | 27c3a9c | 2016-09-26 21:16:11 +0000 | [diff] [blame] | 895 | if ((blob = malloc(blob_len)) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 896 | r = SSH_ERR_ALLOC_FAIL; |
| 897 | goto out; |
| 898 | } |
| 899 | BN_bn2bin(k->rsa->n, blob); |
| 900 | BN_bn2bin(k->rsa->e, blob + nlen); |
| 901 | #endif /* WITH_OPENSSL */ |
| 902 | } else if ((r = to_blob(k, &blob, &blob_len, 1)) != 0) |
| 903 | goto out; |
| 904 | if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) { |
| 905 | r = SSH_ERR_ALLOC_FAIL; |
| 906 | goto out; |
| 907 | } |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 908 | if ((r = ssh_digest_memory(dgst_alg, blob, blob_len, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 909 | ret, SSH_DIGEST_MAX_LENGTH)) != 0) |
| 910 | goto out; |
| 911 | /* success */ |
| 912 | if (retp != NULL) { |
| 913 | *retp = ret; |
| 914 | ret = NULL; |
| 915 | } |
| 916 | if (lenp != NULL) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 917 | *lenp = ssh_digest_bytes(dgst_alg); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 918 | r = 0; |
| 919 | out: |
| 920 | free(ret); |
| 921 | if (blob != NULL) { |
| 922 | explicit_bzero(blob, blob_len); |
| 923 | free(blob); |
| 924 | } |
| 925 | return r; |
| 926 | } |
| 927 | |
| 928 | static char * |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 929 | fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 930 | { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 931 | char *ret; |
| 932 | size_t plen = strlen(alg) + 1; |
| 933 | size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1; |
| 934 | int r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 935 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 936 | if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 937 | return NULL; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 938 | strlcpy(ret, alg, rlen); |
| 939 | strlcat(ret, ":", rlen); |
| 940 | if (dgst_raw_len == 0) |
| 941 | return ret; |
| 942 | if ((r = b64_ntop(dgst_raw, dgst_raw_len, |
| 943 | ret + plen, rlen - plen)) == -1) { |
| 944 | explicit_bzero(ret, rlen); |
| 945 | free(ret); |
| 946 | return NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 947 | } |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 948 | /* Trim padding characters from end */ |
| 949 | ret[strcspn(ret, "=")] = '\0'; |
| 950 | return ret; |
| 951 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 952 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 953 | static char * |
| 954 | fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len) |
| 955 | { |
| 956 | char *retval, hex[5]; |
| 957 | size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2; |
| 958 | |
| 959 | if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL) |
| 960 | return NULL; |
| 961 | strlcpy(retval, alg, rlen); |
| 962 | strlcat(retval, ":", rlen); |
| 963 | for (i = 0; i < dgst_raw_len; i++) { |
| 964 | snprintf(hex, sizeof(hex), "%s%02x", |
| 965 | i > 0 ? ":" : "", dgst_raw[i]); |
| 966 | strlcat(retval, hex, rlen); |
| 967 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 968 | return retval; |
| 969 | } |
| 970 | |
| 971 | static char * |
| 972 | fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len) |
| 973 | { |
| 974 | char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; |
| 975 | char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', |
| 976 | 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' }; |
| 977 | u_int i, j = 0, rounds, seed = 1; |
| 978 | char *retval; |
| 979 | |
| 980 | rounds = (dgst_raw_len / 2) + 1; |
| 981 | if ((retval = calloc(rounds, 6)) == NULL) |
| 982 | return NULL; |
| 983 | retval[j++] = 'x'; |
| 984 | for (i = 0; i < rounds; i++) { |
| 985 | u_int idx0, idx1, idx2, idx3, idx4; |
| 986 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { |
| 987 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + |
| 988 | seed) % 6; |
| 989 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; |
| 990 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + |
| 991 | (seed / 6)) % 6; |
| 992 | retval[j++] = vowels[idx0]; |
| 993 | retval[j++] = consonants[idx1]; |
| 994 | retval[j++] = vowels[idx2]; |
| 995 | if ((i + 1) < rounds) { |
| 996 | idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15; |
| 997 | idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15; |
| 998 | retval[j++] = consonants[idx3]; |
| 999 | retval[j++] = '-'; |
| 1000 | retval[j++] = consonants[idx4]; |
| 1001 | seed = ((seed * 5) + |
| 1002 | ((((u_int)(dgst_raw[2 * i])) * 7) + |
| 1003 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; |
| 1004 | } |
| 1005 | } else { |
| 1006 | idx0 = seed % 6; |
| 1007 | idx1 = 16; |
| 1008 | idx2 = seed / 6; |
| 1009 | retval[j++] = vowels[idx0]; |
| 1010 | retval[j++] = consonants[idx1]; |
| 1011 | retval[j++] = vowels[idx2]; |
| 1012 | } |
| 1013 | } |
| 1014 | retval[j++] = 'x'; |
| 1015 | retval[j++] = '\0'; |
| 1016 | return retval; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Draw an ASCII-Art representing the fingerprint so human brain can |
| 1021 | * profit from its built-in pattern recognition ability. |
| 1022 | * This technique is called "random art" and can be found in some |
| 1023 | * scientific publications like this original paper: |
| 1024 | * |
| 1025 | * "Hash Visualization: a New Technique to improve Real-World Security", |
| 1026 | * Perrig A. and Song D., 1999, International Workshop on Cryptographic |
| 1027 | * Techniques and E-Commerce (CrypTEC '99) |
| 1028 | * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf |
| 1029 | * |
| 1030 | * The subject came up in a talk by Dan Kaminsky, too. |
| 1031 | * |
| 1032 | * If you see the picture is different, the key is different. |
| 1033 | * If the picture looks the same, you still know nothing. |
| 1034 | * |
| 1035 | * The algorithm used here is a worm crawling over a discrete plane, |
| 1036 | * leaving a trace (augmenting the field) everywhere it goes. |
| 1037 | * Movement is taken from dgst_raw 2bit-wise. Bumping into walls |
| 1038 | * makes the respective movement vector be ignored for this turn. |
| 1039 | * Graphs are not unambiguous, because circles in graphs can be |
| 1040 | * walked in either direction. |
| 1041 | */ |
| 1042 | |
| 1043 | /* |
| 1044 | * Field sizes for the random art. Have to be odd, so the starting point |
| 1045 | * can be in the exact middle of the picture, and FLDBASE should be >=8 . |
| 1046 | * Else pictures would be too dense, and drawing the frame would |
| 1047 | * fail, too, because the key type would not fit in anymore. |
| 1048 | */ |
| 1049 | #define FLDBASE 8 |
| 1050 | #define FLDSIZE_Y (FLDBASE + 1) |
| 1051 | #define FLDSIZE_X (FLDBASE * 2 + 1) |
| 1052 | static char * |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1053 | fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1054 | const struct sshkey *k) |
| 1055 | { |
| 1056 | /* |
| 1057 | * Chars to be used after each other every time the worm |
| 1058 | * intersects with itself. Matter of taste. |
| 1059 | */ |
| 1060 | char *augmentation_string = " .o+=*BOX@%&#/^SE"; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1061 | char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1062 | u_char field[FLDSIZE_X][FLDSIZE_Y]; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1063 | size_t i, tlen, hlen; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1064 | u_int b; |
Damien Miller | 61e28e5 | 2014-07-03 21:22:22 +1000 | [diff] [blame] | 1065 | int x, y, r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1066 | size_t len = strlen(augmentation_string) - 1; |
| 1067 | |
| 1068 | if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL) |
| 1069 | return NULL; |
| 1070 | |
| 1071 | /* initialize field */ |
| 1072 | memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char)); |
| 1073 | x = FLDSIZE_X / 2; |
| 1074 | y = FLDSIZE_Y / 2; |
| 1075 | |
| 1076 | /* process raw key */ |
| 1077 | for (i = 0; i < dgst_raw_len; i++) { |
| 1078 | int input; |
| 1079 | /* each byte conveys four 2-bit move commands */ |
| 1080 | input = dgst_raw[i]; |
| 1081 | for (b = 0; b < 4; b++) { |
| 1082 | /* evaluate 2 bit, rest is shifted later */ |
| 1083 | x += (input & 0x1) ? 1 : -1; |
| 1084 | y += (input & 0x2) ? 1 : -1; |
| 1085 | |
| 1086 | /* assure we are still in bounds */ |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1087 | x = MAXIMUM(x, 0); |
| 1088 | y = MAXIMUM(y, 0); |
| 1089 | x = MINIMUM(x, FLDSIZE_X - 1); |
| 1090 | y = MINIMUM(y, FLDSIZE_Y - 1); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1091 | |
| 1092 | /* augment the field */ |
| 1093 | if (field[x][y] < len - 2) |
| 1094 | field[x][y]++; |
| 1095 | input = input >> 2; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /* mark starting point and end point*/ |
| 1100 | field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1; |
| 1101 | field[x][y] = len; |
| 1102 | |
Damien Miller | 61e28e5 | 2014-07-03 21:22:22 +1000 | [diff] [blame] | 1103 | /* assemble title */ |
| 1104 | r = snprintf(title, sizeof(title), "[%s %u]", |
| 1105 | sshkey_type(k), sshkey_size(k)); |
| 1106 | /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */ |
| 1107 | if (r < 0 || r > (int)sizeof(title)) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1108 | r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k)); |
| 1109 | tlen = (r <= 0) ? 0 : strlen(title); |
| 1110 | |
| 1111 | /* assemble hash ID. */ |
| 1112 | r = snprintf(hash, sizeof(hash), "[%s]", alg); |
| 1113 | hlen = (r <= 0) ? 0 : strlen(hash); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1114 | |
| 1115 | /* output upper border */ |
Damien Miller | 61e28e5 | 2014-07-03 21:22:22 +1000 | [diff] [blame] | 1116 | p = retval; |
| 1117 | *p++ = '+'; |
| 1118 | for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++) |
| 1119 | *p++ = '-'; |
| 1120 | memcpy(p, title, tlen); |
| 1121 | p += tlen; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1122 | for (i += tlen; i < FLDSIZE_X; i++) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1123 | *p++ = '-'; |
| 1124 | *p++ = '+'; |
| 1125 | *p++ = '\n'; |
| 1126 | |
| 1127 | /* output content */ |
| 1128 | for (y = 0; y < FLDSIZE_Y; y++) { |
| 1129 | *p++ = '|'; |
| 1130 | for (x = 0; x < FLDSIZE_X; x++) |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1131 | *p++ = augmentation_string[MINIMUM(field[x][y], len)]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1132 | *p++ = '|'; |
| 1133 | *p++ = '\n'; |
| 1134 | } |
| 1135 | |
| 1136 | /* output lower border */ |
| 1137 | *p++ = '+'; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1138 | for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++) |
| 1139 | *p++ = '-'; |
| 1140 | memcpy(p, hash, hlen); |
| 1141 | p += hlen; |
| 1142 | for (i += hlen; i < FLDSIZE_X; i++) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1143 | *p++ = '-'; |
| 1144 | *p++ = '+'; |
| 1145 | |
| 1146 | return retval; |
| 1147 | } |
| 1148 | |
| 1149 | char * |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1150 | sshkey_fingerprint(const struct sshkey *k, int dgst_alg, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1151 | enum sshkey_fp_rep dgst_rep) |
| 1152 | { |
| 1153 | char *retval = NULL; |
| 1154 | u_char *dgst_raw; |
| 1155 | size_t dgst_raw_len; |
| 1156 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1157 | if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1158 | return NULL; |
| 1159 | switch (dgst_rep) { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1160 | case SSH_FP_DEFAULT: |
| 1161 | if (dgst_alg == SSH_DIGEST_MD5) { |
| 1162 | retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg), |
| 1163 | dgst_raw, dgst_raw_len); |
| 1164 | } else { |
| 1165 | retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg), |
| 1166 | dgst_raw, dgst_raw_len); |
| 1167 | } |
| 1168 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1169 | case SSH_FP_HEX: |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1170 | retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg), |
| 1171 | dgst_raw, dgst_raw_len); |
| 1172 | break; |
| 1173 | case SSH_FP_BASE64: |
| 1174 | retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg), |
| 1175 | dgst_raw, dgst_raw_len); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1176 | break; |
| 1177 | case SSH_FP_BUBBLEBABBLE: |
| 1178 | retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len); |
| 1179 | break; |
| 1180 | case SSH_FP_RANDOMART: |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1181 | retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg), |
| 1182 | dgst_raw, dgst_raw_len, k); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1183 | break; |
| 1184 | default: |
| 1185 | explicit_bzero(dgst_raw, dgst_raw_len); |
| 1186 | free(dgst_raw); |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | explicit_bzero(dgst_raw, dgst_raw_len); |
| 1190 | free(dgst_raw); |
| 1191 | return retval; |
| 1192 | } |
| 1193 | |
| 1194 | #ifdef WITH_SSH1 |
| 1195 | /* |
| 1196 | * Reads a multiple-precision integer in decimal from the buffer, and advances |
| 1197 | * the pointer. The integer must already be initialized. This function is |
| 1198 | * permitted to modify the buffer. This leaves *cpp to point just beyond the |
| 1199 | * last processed character. |
| 1200 | */ |
| 1201 | static int |
| 1202 | read_decimal_bignum(char **cpp, BIGNUM *v) |
| 1203 | { |
| 1204 | char *cp; |
| 1205 | size_t e; |
| 1206 | int skip = 1; /* skip white space */ |
| 1207 | |
| 1208 | cp = *cpp; |
| 1209 | while (*cp == ' ' || *cp == '\t') |
| 1210 | cp++; |
| 1211 | e = strspn(cp, "0123456789"); |
| 1212 | if (e == 0) |
| 1213 | return SSH_ERR_INVALID_FORMAT; |
| 1214 | if (e > SSHBUF_MAX_BIGNUM * 3) |
| 1215 | return SSH_ERR_BIGNUM_TOO_LARGE; |
| 1216 | if (cp[e] == '\0') |
| 1217 | skip = 0; |
millert@openbsd.org | 259adb6 | 2015-11-16 23:47:52 +0000 | [diff] [blame] | 1218 | else if (strchr(" \t\r\n", cp[e]) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1219 | return SSH_ERR_INVALID_FORMAT; |
| 1220 | cp[e] = '\0'; |
| 1221 | if (BN_dec2bn(&v, cp) <= 0) |
| 1222 | return SSH_ERR_INVALID_FORMAT; |
| 1223 | *cpp = cp + e + skip; |
| 1224 | return 0; |
| 1225 | } |
| 1226 | #endif /* WITH_SSH1 */ |
| 1227 | |
| 1228 | /* returns 0 ok, and < 0 error */ |
| 1229 | int |
| 1230 | sshkey_read(struct sshkey *ret, char **cpp) |
| 1231 | { |
| 1232 | struct sshkey *k; |
| 1233 | int retval = SSH_ERR_INVALID_FORMAT; |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1234 | char *ep, *cp, *space; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1235 | int r, type, curve_nid = -1; |
| 1236 | struct sshbuf *blob; |
| 1237 | #ifdef WITH_SSH1 |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1238 | u_long bits; |
| 1239 | #endif /* WITH_SSH1 */ |
| 1240 | |
| 1241 | cp = *cpp; |
| 1242 | |
| 1243 | switch (ret->type) { |
| 1244 | case KEY_RSA1: |
| 1245 | #ifdef WITH_SSH1 |
| 1246 | /* Get number of bits. */ |
| 1247 | bits = strtoul(cp, &ep, 10); |
millert@openbsd.org | 259adb6 | 2015-11-16 23:47:52 +0000 | [diff] [blame] | 1248 | if (*cp == '\0' || strchr(" \t\r\n", *ep) == NULL || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1249 | bits == 0 || bits > SSHBUF_MAX_BIGNUM * 8) |
| 1250 | return SSH_ERR_INVALID_FORMAT; /* Bad bit count... */ |
| 1251 | /* Get public exponent, public modulus. */ |
| 1252 | if ((r = read_decimal_bignum(&ep, ret->rsa->e)) < 0) |
| 1253 | return r; |
| 1254 | if ((r = read_decimal_bignum(&ep, ret->rsa->n)) < 0) |
| 1255 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1256 | /* validate the claimed number of bits */ |
| 1257 | if (BN_num_bits(ret->rsa->n) != (int)bits) |
| 1258 | return SSH_ERR_KEY_BITS_MISMATCH; |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1259 | *cpp = ep; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1260 | retval = 0; |
| 1261 | #endif /* WITH_SSH1 */ |
| 1262 | break; |
| 1263 | case KEY_UNSPEC: |
| 1264 | case KEY_RSA: |
| 1265 | case KEY_DSA: |
| 1266 | case KEY_ECDSA: |
| 1267 | case KEY_ED25519: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1268 | case KEY_DSA_CERT: |
| 1269 | case KEY_ECDSA_CERT: |
| 1270 | case KEY_RSA_CERT: |
| 1271 | case KEY_ED25519_CERT: |
| 1272 | space = strchr(cp, ' '); |
| 1273 | if (space == NULL) |
| 1274 | return SSH_ERR_INVALID_FORMAT; |
| 1275 | *space = '\0'; |
| 1276 | type = sshkey_type_from_name(cp); |
| 1277 | if (sshkey_type_plain(type) == KEY_ECDSA && |
| 1278 | (curve_nid = sshkey_ecdsa_nid_from_name(cp)) == -1) |
| 1279 | return SSH_ERR_EC_CURVE_INVALID; |
| 1280 | *space = ' '; |
| 1281 | if (type == KEY_UNSPEC) |
| 1282 | return SSH_ERR_INVALID_FORMAT; |
| 1283 | cp = space+1; |
| 1284 | if (*cp == '\0') |
| 1285 | return SSH_ERR_INVALID_FORMAT; |
djm@openbsd.org | d2d5100 | 2014-11-18 01:02:25 +0000 | [diff] [blame] | 1286 | if (ret->type != KEY_UNSPEC && ret->type != type) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1287 | return SSH_ERR_KEY_TYPE_MISMATCH; |
| 1288 | if ((blob = sshbuf_new()) == NULL) |
| 1289 | return SSH_ERR_ALLOC_FAIL; |
| 1290 | /* trim comment */ |
| 1291 | space = strchr(cp, ' '); |
markus@openbsd.org | 816d153 | 2015-01-12 20:13:27 +0000 | [diff] [blame] | 1292 | if (space) { |
| 1293 | /* advance 'space': skip whitespace */ |
| 1294 | *space++ = '\0'; |
| 1295 | while (*space == ' ' || *space == '\t') |
| 1296 | space++; |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1297 | ep = space; |
markus@openbsd.org | 816d153 | 2015-01-12 20:13:27 +0000 | [diff] [blame] | 1298 | } else |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1299 | ep = cp + strlen(cp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1300 | if ((r = sshbuf_b64tod(blob, cp)) != 0) { |
| 1301 | sshbuf_free(blob); |
| 1302 | return r; |
| 1303 | } |
| 1304 | if ((r = sshkey_from_blob(sshbuf_ptr(blob), |
| 1305 | sshbuf_len(blob), &k)) != 0) { |
| 1306 | sshbuf_free(blob); |
| 1307 | return r; |
| 1308 | } |
| 1309 | sshbuf_free(blob); |
| 1310 | if (k->type != type) { |
| 1311 | sshkey_free(k); |
| 1312 | return SSH_ERR_KEY_TYPE_MISMATCH; |
| 1313 | } |
| 1314 | if (sshkey_type_plain(type) == KEY_ECDSA && |
| 1315 | curve_nid != k->ecdsa_nid) { |
| 1316 | sshkey_free(k); |
| 1317 | return SSH_ERR_EC_CURVE_MISMATCH; |
| 1318 | } |
djm@openbsd.org | d2d5100 | 2014-11-18 01:02:25 +0000 | [diff] [blame] | 1319 | ret->type = type; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1320 | if (sshkey_is_cert(ret)) { |
| 1321 | if (!sshkey_is_cert(k)) { |
| 1322 | sshkey_free(k); |
| 1323 | return SSH_ERR_EXPECTED_CERT; |
| 1324 | } |
| 1325 | if (ret->cert != NULL) |
| 1326 | cert_free(ret->cert); |
| 1327 | ret->cert = k->cert; |
| 1328 | k->cert = NULL; |
| 1329 | } |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1330 | switch (sshkey_type_plain(ret->type)) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1331 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1332 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1333 | if (ret->rsa != NULL) |
| 1334 | RSA_free(ret->rsa); |
| 1335 | ret->rsa = k->rsa; |
| 1336 | k->rsa = NULL; |
| 1337 | #ifdef DEBUG_PK |
| 1338 | RSA_print_fp(stderr, ret->rsa, 8); |
| 1339 | #endif |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1342 | if (ret->dsa != NULL) |
| 1343 | DSA_free(ret->dsa); |
| 1344 | ret->dsa = k->dsa; |
| 1345 | k->dsa = NULL; |
| 1346 | #ifdef DEBUG_PK |
| 1347 | DSA_print_fp(stderr, ret->dsa, 8); |
| 1348 | #endif |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1349 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1350 | # ifdef OPENSSL_HAS_ECC |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1351 | case KEY_ECDSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1352 | if (ret->ecdsa != NULL) |
| 1353 | EC_KEY_free(ret->ecdsa); |
| 1354 | ret->ecdsa = k->ecdsa; |
| 1355 | ret->ecdsa_nid = k->ecdsa_nid; |
| 1356 | k->ecdsa = NULL; |
| 1357 | k->ecdsa_nid = -1; |
| 1358 | #ifdef DEBUG_PK |
| 1359 | sshkey_dump_ec_key(ret->ecdsa); |
| 1360 | #endif |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1361 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1362 | # endif /* OPENSSL_HAS_ECC */ |
| 1363 | #endif /* WITH_OPENSSL */ |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1364 | case KEY_ED25519: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1365 | free(ret->ed25519_pk); |
| 1366 | ret->ed25519_pk = k->ed25519_pk; |
| 1367 | k->ed25519_pk = NULL; |
| 1368 | #ifdef DEBUG_PK |
| 1369 | /* XXX */ |
| 1370 | #endif |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1371 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1372 | } |
djm@openbsd.org | 3a9f84b | 2015-11-16 22:50:01 +0000 | [diff] [blame] | 1373 | *cpp = ep; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1374 | retval = 0; |
| 1375 | /*XXXX*/ |
| 1376 | sshkey_free(k); |
| 1377 | if (retval != 0) |
| 1378 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1379 | break; |
| 1380 | default: |
| 1381 | return SSH_ERR_INVALID_ARGUMENT; |
| 1382 | } |
| 1383 | return retval; |
| 1384 | } |
| 1385 | |
| 1386 | int |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1387 | sshkey_to_base64(const struct sshkey *key, char **b64p) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1388 | { |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1389 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1390 | struct sshbuf *b = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1391 | char *uu = NULL; |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (b64p != NULL) |
| 1394 | *b64p = NULL; |
| 1395 | if ((b = sshbuf_new()) == NULL) |
| 1396 | return SSH_ERR_ALLOC_FAIL; |
| 1397 | if ((r = sshkey_putb(key, b)) != 0) |
| 1398 | goto out; |
| 1399 | if ((uu = sshbuf_dtob64(b)) == NULL) { |
| 1400 | r = SSH_ERR_ALLOC_FAIL; |
| 1401 | goto out; |
| 1402 | } |
| 1403 | /* Success */ |
| 1404 | if (b64p != NULL) { |
| 1405 | *b64p = uu; |
| 1406 | uu = NULL; |
| 1407 | } |
| 1408 | r = 0; |
| 1409 | out: |
| 1410 | sshbuf_free(b); |
| 1411 | free(uu); |
| 1412 | return r; |
| 1413 | } |
| 1414 | |
| 1415 | static int |
| 1416 | sshkey_format_rsa1(const struct sshkey *key, struct sshbuf *b) |
| 1417 | { |
| 1418 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1419 | #ifdef WITH_SSH1 |
| 1420 | u_int bits = 0; |
| 1421 | char *dec_e = NULL, *dec_n = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1422 | |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1423 | if (key->rsa == NULL || key->rsa->e == NULL || |
| 1424 | key->rsa->n == NULL) { |
| 1425 | r = SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1426 | goto out; |
| 1427 | } |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1428 | if ((dec_e = BN_bn2dec(key->rsa->e)) == NULL || |
| 1429 | (dec_n = BN_bn2dec(key->rsa->n)) == NULL) { |
| 1430 | r = SSH_ERR_ALLOC_FAIL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1431 | goto out; |
| 1432 | } |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1433 | /* size of modulus 'n' */ |
| 1434 | if ((bits = BN_num_bits(key->rsa->n)) <= 0) { |
| 1435 | r = SSH_ERR_INVALID_ARGUMENT; |
| 1436 | goto out; |
| 1437 | } |
| 1438 | if ((r = sshbuf_putf(b, "%u %s %s", bits, dec_e, dec_n)) != 0) |
| 1439 | goto out; |
| 1440 | |
| 1441 | /* Success */ |
| 1442 | r = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1443 | out: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1444 | if (dec_e != NULL) |
| 1445 | OPENSSL_free(dec_e); |
| 1446 | if (dec_n != NULL) |
| 1447 | OPENSSL_free(dec_n); |
| 1448 | #endif /* WITH_SSH1 */ |
djm@openbsd.org | d80fbe4 | 2015-05-21 04:55:51 +0000 | [diff] [blame] | 1449 | |
| 1450 | return r; |
| 1451 | } |
| 1452 | |
| 1453 | static int |
| 1454 | sshkey_format_text(const struct sshkey *key, struct sshbuf *b) |
| 1455 | { |
| 1456 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1457 | char *uu = NULL; |
| 1458 | |
| 1459 | if (key->type == KEY_RSA1) { |
| 1460 | if ((r = sshkey_format_rsa1(key, b)) != 0) |
| 1461 | goto out; |
| 1462 | } else { |
| 1463 | /* Unsupported key types handled in sshkey_to_base64() */ |
| 1464 | if ((r = sshkey_to_base64(key, &uu)) != 0) |
| 1465 | goto out; |
| 1466 | if ((r = sshbuf_putf(b, "%s %s", |
| 1467 | sshkey_ssh_name(key), uu)) != 0) |
| 1468 | goto out; |
| 1469 | } |
| 1470 | r = 0; |
| 1471 | out: |
| 1472 | free(uu); |
| 1473 | return r; |
| 1474 | } |
| 1475 | |
| 1476 | int |
| 1477 | sshkey_write(const struct sshkey *key, FILE *f) |
| 1478 | { |
| 1479 | struct sshbuf *b = NULL; |
| 1480 | int r = SSH_ERR_INTERNAL_ERROR; |
| 1481 | |
| 1482 | if ((b = sshbuf_new()) == NULL) |
| 1483 | return SSH_ERR_ALLOC_FAIL; |
| 1484 | if ((r = sshkey_format_text(key, b)) != 0) |
| 1485 | goto out; |
| 1486 | if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) { |
| 1487 | if (feof(f)) |
| 1488 | errno = EPIPE; |
| 1489 | r = SSH_ERR_SYSTEM_ERROR; |
| 1490 | goto out; |
| 1491 | } |
| 1492 | /* Success */ |
| 1493 | r = 0; |
| 1494 | out: |
| 1495 | sshbuf_free(b); |
| 1496 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | const char * |
| 1500 | sshkey_cert_type(const struct sshkey *k) |
| 1501 | { |
| 1502 | switch (k->cert->type) { |
| 1503 | case SSH2_CERT_TYPE_USER: |
| 1504 | return "user"; |
| 1505 | case SSH2_CERT_TYPE_HOST: |
| 1506 | return "host"; |
| 1507 | default: |
| 1508 | return "unknown"; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | #ifdef WITH_OPENSSL |
| 1513 | static int |
| 1514 | rsa_generate_private_key(u_int bits, RSA **rsap) |
| 1515 | { |
| 1516 | RSA *private = NULL; |
| 1517 | BIGNUM *f4 = NULL; |
| 1518 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1519 | |
| 1520 | if (rsap == NULL || |
| 1521 | bits < SSH_RSA_MINIMUM_MODULUS_SIZE || |
| 1522 | bits > SSHBUF_MAX_BIGNUM * 8) |
| 1523 | return SSH_ERR_INVALID_ARGUMENT; |
| 1524 | *rsap = NULL; |
| 1525 | if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) { |
| 1526 | ret = SSH_ERR_ALLOC_FAIL; |
| 1527 | goto out; |
| 1528 | } |
| 1529 | if (!BN_set_word(f4, RSA_F4) || |
| 1530 | !RSA_generate_key_ex(private, bits, f4, NULL)) { |
| 1531 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 1532 | goto out; |
| 1533 | } |
| 1534 | *rsap = private; |
| 1535 | private = NULL; |
| 1536 | ret = 0; |
| 1537 | out: |
| 1538 | if (private != NULL) |
| 1539 | RSA_free(private); |
| 1540 | if (f4 != NULL) |
| 1541 | BN_free(f4); |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
| 1545 | static int |
| 1546 | dsa_generate_private_key(u_int bits, DSA **dsap) |
| 1547 | { |
| 1548 | DSA *private; |
| 1549 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1550 | |
| 1551 | if (dsap == NULL || bits != 1024) |
| 1552 | return SSH_ERR_INVALID_ARGUMENT; |
| 1553 | if ((private = DSA_new()) == NULL) { |
| 1554 | ret = SSH_ERR_ALLOC_FAIL; |
| 1555 | goto out; |
| 1556 | } |
| 1557 | *dsap = NULL; |
| 1558 | if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL, |
| 1559 | NULL, NULL) || !DSA_generate_key(private)) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1560 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 1561 | goto out; |
| 1562 | } |
| 1563 | *dsap = private; |
| 1564 | private = NULL; |
| 1565 | ret = 0; |
| 1566 | out: |
| 1567 | if (private != NULL) |
| 1568 | DSA_free(private); |
| 1569 | return ret; |
| 1570 | } |
| 1571 | |
| 1572 | # ifdef OPENSSL_HAS_ECC |
| 1573 | int |
| 1574 | sshkey_ecdsa_key_to_nid(EC_KEY *k) |
| 1575 | { |
| 1576 | EC_GROUP *eg; |
| 1577 | int nids[] = { |
| 1578 | NID_X9_62_prime256v1, |
| 1579 | NID_secp384r1, |
| 1580 | # ifdef OPENSSL_HAS_NISTP521 |
| 1581 | NID_secp521r1, |
| 1582 | # endif /* OPENSSL_HAS_NISTP521 */ |
| 1583 | -1 |
| 1584 | }; |
| 1585 | int nid; |
| 1586 | u_int i; |
| 1587 | BN_CTX *bnctx; |
| 1588 | const EC_GROUP *g = EC_KEY_get0_group(k); |
| 1589 | |
| 1590 | /* |
| 1591 | * The group may be stored in a ASN.1 encoded private key in one of two |
| 1592 | * ways: as a "named group", which is reconstituted by ASN.1 object ID |
| 1593 | * or explicit group parameters encoded into the key blob. Only the |
| 1594 | * "named group" case sets the group NID for us, but we can figure |
| 1595 | * it out for the other case by comparing against all the groups that |
| 1596 | * are supported. |
| 1597 | */ |
| 1598 | if ((nid = EC_GROUP_get_curve_name(g)) > 0) |
| 1599 | return nid; |
| 1600 | if ((bnctx = BN_CTX_new()) == NULL) |
| 1601 | return -1; |
| 1602 | for (i = 0; nids[i] != -1; i++) { |
| 1603 | if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL) { |
| 1604 | BN_CTX_free(bnctx); |
| 1605 | return -1; |
| 1606 | } |
| 1607 | if (EC_GROUP_cmp(g, eg, bnctx) == 0) |
| 1608 | break; |
| 1609 | EC_GROUP_free(eg); |
| 1610 | } |
| 1611 | BN_CTX_free(bnctx); |
| 1612 | if (nids[i] != -1) { |
| 1613 | /* Use the group with the NID attached */ |
| 1614 | EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE); |
| 1615 | if (EC_KEY_set_group(k, eg) != 1) { |
| 1616 | EC_GROUP_free(eg); |
| 1617 | return -1; |
| 1618 | } |
| 1619 | } |
| 1620 | return nids[i]; |
| 1621 | } |
| 1622 | |
| 1623 | static int |
| 1624 | ecdsa_generate_private_key(u_int bits, int *nid, EC_KEY **ecdsap) |
| 1625 | { |
| 1626 | EC_KEY *private; |
| 1627 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1628 | |
| 1629 | if (nid == NULL || ecdsap == NULL || |
| 1630 | (*nid = sshkey_ecdsa_bits_to_nid(bits)) == -1) |
| 1631 | return SSH_ERR_INVALID_ARGUMENT; |
| 1632 | *ecdsap = NULL; |
| 1633 | if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) { |
| 1634 | ret = SSH_ERR_ALLOC_FAIL; |
| 1635 | goto out; |
| 1636 | } |
| 1637 | if (EC_KEY_generate_key(private) != 1) { |
| 1638 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 1639 | goto out; |
| 1640 | } |
| 1641 | EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE); |
| 1642 | *ecdsap = private; |
| 1643 | private = NULL; |
| 1644 | ret = 0; |
| 1645 | out: |
| 1646 | if (private != NULL) |
| 1647 | EC_KEY_free(private); |
| 1648 | return ret; |
| 1649 | } |
| 1650 | # endif /* OPENSSL_HAS_ECC */ |
| 1651 | #endif /* WITH_OPENSSL */ |
| 1652 | |
| 1653 | int |
| 1654 | sshkey_generate(int type, u_int bits, struct sshkey **keyp) |
| 1655 | { |
| 1656 | struct sshkey *k; |
| 1657 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1658 | |
| 1659 | if (keyp == NULL) |
| 1660 | return SSH_ERR_INVALID_ARGUMENT; |
| 1661 | *keyp = NULL; |
| 1662 | if ((k = sshkey_new(KEY_UNSPEC)) == NULL) |
| 1663 | return SSH_ERR_ALLOC_FAIL; |
| 1664 | switch (type) { |
| 1665 | case KEY_ED25519: |
| 1666 | if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL || |
| 1667 | (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL) { |
| 1668 | ret = SSH_ERR_ALLOC_FAIL; |
| 1669 | break; |
| 1670 | } |
| 1671 | crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk); |
| 1672 | ret = 0; |
| 1673 | break; |
| 1674 | #ifdef WITH_OPENSSL |
| 1675 | case KEY_DSA: |
| 1676 | ret = dsa_generate_private_key(bits, &k->dsa); |
| 1677 | break; |
| 1678 | # ifdef OPENSSL_HAS_ECC |
| 1679 | case KEY_ECDSA: |
| 1680 | ret = ecdsa_generate_private_key(bits, &k->ecdsa_nid, |
| 1681 | &k->ecdsa); |
| 1682 | break; |
| 1683 | # endif /* OPENSSL_HAS_ECC */ |
| 1684 | case KEY_RSA: |
| 1685 | case KEY_RSA1: |
| 1686 | ret = rsa_generate_private_key(bits, &k->rsa); |
| 1687 | break; |
| 1688 | #endif /* WITH_OPENSSL */ |
| 1689 | default: |
| 1690 | ret = SSH_ERR_INVALID_ARGUMENT; |
| 1691 | } |
| 1692 | if (ret == 0) { |
| 1693 | k->type = type; |
| 1694 | *keyp = k; |
| 1695 | } else |
| 1696 | sshkey_free(k); |
| 1697 | return ret; |
| 1698 | } |
| 1699 | |
| 1700 | int |
| 1701 | sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key) |
| 1702 | { |
| 1703 | u_int i; |
| 1704 | const struct sshkey_cert *from; |
| 1705 | struct sshkey_cert *to; |
| 1706 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1707 | |
| 1708 | if (to_key->cert != NULL) { |
| 1709 | cert_free(to_key->cert); |
| 1710 | to_key->cert = NULL; |
| 1711 | } |
| 1712 | |
| 1713 | if ((from = from_key->cert) == NULL) |
| 1714 | return SSH_ERR_INVALID_ARGUMENT; |
| 1715 | |
| 1716 | if ((to = to_key->cert = cert_new()) == NULL) |
| 1717 | return SSH_ERR_ALLOC_FAIL; |
| 1718 | |
| 1719 | if ((ret = sshbuf_putb(to->certblob, from->certblob)) != 0 || |
| 1720 | (ret = sshbuf_putb(to->critical, from->critical)) != 0 || |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 1721 | (ret = sshbuf_putb(to->extensions, from->extensions)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1722 | return ret; |
| 1723 | |
| 1724 | to->serial = from->serial; |
| 1725 | to->type = from->type; |
| 1726 | if (from->key_id == NULL) |
| 1727 | to->key_id = NULL; |
| 1728 | else if ((to->key_id = strdup(from->key_id)) == NULL) |
| 1729 | return SSH_ERR_ALLOC_FAIL; |
| 1730 | to->valid_after = from->valid_after; |
| 1731 | to->valid_before = from->valid_before; |
| 1732 | if (from->signature_key == NULL) |
| 1733 | to->signature_key = NULL; |
| 1734 | else if ((ret = sshkey_from_private(from->signature_key, |
| 1735 | &to->signature_key)) != 0) |
| 1736 | return ret; |
| 1737 | |
| 1738 | if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) |
| 1739 | return SSH_ERR_INVALID_ARGUMENT; |
| 1740 | if (from->nprincipals > 0) { |
| 1741 | if ((to->principals = calloc(from->nprincipals, |
| 1742 | sizeof(*to->principals))) == NULL) |
| 1743 | return SSH_ERR_ALLOC_FAIL; |
| 1744 | for (i = 0; i < from->nprincipals; i++) { |
| 1745 | to->principals[i] = strdup(from->principals[i]); |
| 1746 | if (to->principals[i] == NULL) { |
| 1747 | to->nprincipals = i; |
| 1748 | return SSH_ERR_ALLOC_FAIL; |
| 1749 | } |
| 1750 | } |
| 1751 | } |
| 1752 | to->nprincipals = from->nprincipals; |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | int |
| 1757 | sshkey_from_private(const struct sshkey *k, struct sshkey **pkp) |
| 1758 | { |
| 1759 | struct sshkey *n = NULL; |
| 1760 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 1761 | |
djm@openbsd.org | 1a2663a | 2015-10-15 23:08:23 +0000 | [diff] [blame] | 1762 | *pkp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1763 | switch (k->type) { |
| 1764 | #ifdef WITH_OPENSSL |
| 1765 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1766 | case KEY_DSA_CERT: |
| 1767 | if ((n = sshkey_new(k->type)) == NULL) |
| 1768 | return SSH_ERR_ALLOC_FAIL; |
| 1769 | if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) || |
| 1770 | (BN_copy(n->dsa->q, k->dsa->q) == NULL) || |
| 1771 | (BN_copy(n->dsa->g, k->dsa->g) == NULL) || |
| 1772 | (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) { |
| 1773 | sshkey_free(n); |
| 1774 | return SSH_ERR_ALLOC_FAIL; |
| 1775 | } |
| 1776 | break; |
| 1777 | # ifdef OPENSSL_HAS_ECC |
| 1778 | case KEY_ECDSA: |
| 1779 | case KEY_ECDSA_CERT: |
| 1780 | if ((n = sshkey_new(k->type)) == NULL) |
| 1781 | return SSH_ERR_ALLOC_FAIL; |
| 1782 | n->ecdsa_nid = k->ecdsa_nid; |
| 1783 | n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid); |
| 1784 | if (n->ecdsa == NULL) { |
| 1785 | sshkey_free(n); |
| 1786 | return SSH_ERR_ALLOC_FAIL; |
| 1787 | } |
| 1788 | if (EC_KEY_set_public_key(n->ecdsa, |
| 1789 | EC_KEY_get0_public_key(k->ecdsa)) != 1) { |
| 1790 | sshkey_free(n); |
| 1791 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 1792 | } |
| 1793 | break; |
| 1794 | # endif /* OPENSSL_HAS_ECC */ |
| 1795 | case KEY_RSA: |
| 1796 | case KEY_RSA1: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1797 | case KEY_RSA_CERT: |
| 1798 | if ((n = sshkey_new(k->type)) == NULL) |
| 1799 | return SSH_ERR_ALLOC_FAIL; |
| 1800 | if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) || |
| 1801 | (BN_copy(n->rsa->e, k->rsa->e) == NULL)) { |
| 1802 | sshkey_free(n); |
| 1803 | return SSH_ERR_ALLOC_FAIL; |
| 1804 | } |
| 1805 | break; |
| 1806 | #endif /* WITH_OPENSSL */ |
| 1807 | case KEY_ED25519: |
| 1808 | case KEY_ED25519_CERT: |
| 1809 | if ((n = sshkey_new(k->type)) == NULL) |
| 1810 | return SSH_ERR_ALLOC_FAIL; |
| 1811 | if (k->ed25519_pk != NULL) { |
| 1812 | if ((n->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) { |
| 1813 | sshkey_free(n); |
| 1814 | return SSH_ERR_ALLOC_FAIL; |
| 1815 | } |
| 1816 | memcpy(n->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ); |
| 1817 | } |
| 1818 | break; |
| 1819 | default: |
| 1820 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 1821 | } |
| 1822 | if (sshkey_is_cert(k)) { |
| 1823 | if ((ret = sshkey_cert_copy(k, n)) != 0) { |
| 1824 | sshkey_free(n); |
| 1825 | return ret; |
| 1826 | } |
| 1827 | } |
| 1828 | *pkp = n; |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | static int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1833 | cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1834 | { |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1835 | struct sshbuf *principals = NULL, *crit = NULL; |
| 1836 | struct sshbuf *exts = NULL, *ca = NULL; |
| 1837 | u_char *sig = NULL; |
| 1838 | size_t signed_len = 0, slen = 0, kidlen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1839 | int ret = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1840 | |
| 1841 | /* Copy the entire key blob for verification and later serialisation */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1842 | if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1843 | return ret; |
| 1844 | |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 1845 | /* Parse body of certificate up to signature */ |
| 1846 | if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1847 | (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 || |
| 1848 | (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 || |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1849 | (ret = sshbuf_froms(b, &principals)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1850 | (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 || |
| 1851 | (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 || |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1852 | (ret = sshbuf_froms(b, &crit)) != 0 || |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 1853 | (ret = sshbuf_froms(b, &exts)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1854 | (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 || |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1855 | (ret = sshbuf_froms(b, &ca)) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1856 | /* XXX debug print error for ret */ |
| 1857 | ret = SSH_ERR_INVALID_FORMAT; |
| 1858 | goto out; |
| 1859 | } |
| 1860 | |
| 1861 | /* Signature is left in the buffer so we can calculate this length */ |
| 1862 | signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b); |
| 1863 | |
| 1864 | if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) { |
| 1865 | ret = SSH_ERR_INVALID_FORMAT; |
| 1866 | goto out; |
| 1867 | } |
| 1868 | |
| 1869 | if (key->cert->type != SSH2_CERT_TYPE_USER && |
| 1870 | key->cert->type != SSH2_CERT_TYPE_HOST) { |
| 1871 | ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE; |
| 1872 | goto out; |
| 1873 | } |
| 1874 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1875 | /* Parse principals section */ |
| 1876 | while (sshbuf_len(principals) > 0) { |
| 1877 | char *principal = NULL; |
| 1878 | char **oprincipals = NULL; |
| 1879 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1880 | if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) { |
| 1881 | ret = SSH_ERR_INVALID_FORMAT; |
| 1882 | goto out; |
| 1883 | } |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1884 | if ((ret = sshbuf_get_cstring(principals, &principal, |
| 1885 | NULL)) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1886 | ret = SSH_ERR_INVALID_FORMAT; |
| 1887 | goto out; |
| 1888 | } |
| 1889 | oprincipals = key->cert->principals; |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 1890 | key->cert->principals = reallocarray(key->cert->principals, |
| 1891 | key->cert->nprincipals + 1, sizeof(*key->cert->principals)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1892 | if (key->cert->principals == NULL) { |
| 1893 | free(principal); |
| 1894 | key->cert->principals = oprincipals; |
| 1895 | ret = SSH_ERR_ALLOC_FAIL; |
| 1896 | goto out; |
| 1897 | } |
| 1898 | key->cert->principals[key->cert->nprincipals++] = principal; |
| 1899 | } |
| 1900 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1901 | /* |
| 1902 | * Stash a copies of the critical options and extensions sections |
| 1903 | * for later use. |
| 1904 | */ |
| 1905 | if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 || |
| 1906 | (exts != NULL && |
| 1907 | (ret = sshbuf_putb(key->cert->extensions, exts)) != 0)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1908 | goto out; |
| 1909 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1910 | /* |
| 1911 | * Validate critical options and extensions sections format. |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1912 | */ |
| 1913 | while (sshbuf_len(crit) != 0) { |
| 1914 | if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 || |
| 1915 | (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) { |
| 1916 | sshbuf_reset(key->cert->critical); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1917 | ret = SSH_ERR_INVALID_FORMAT; |
| 1918 | goto out; |
| 1919 | } |
| 1920 | } |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1921 | while (exts != NULL && sshbuf_len(exts) != 0) { |
| 1922 | if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 || |
| 1923 | (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) { |
| 1924 | sshbuf_reset(key->cert->extensions); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1925 | ret = SSH_ERR_INVALID_FORMAT; |
| 1926 | goto out; |
| 1927 | } |
| 1928 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1929 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1930 | /* Parse CA key and check signature */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1931 | if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1932 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 1933 | goto out; |
| 1934 | } |
| 1935 | if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) { |
| 1936 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 1937 | goto out; |
| 1938 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1939 | if ((ret = sshkey_verify(key->cert->signature_key, sig, slen, |
| 1940 | sshbuf_ptr(key->cert->certblob), signed_len, 0)) != 0) |
| 1941 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1942 | |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1943 | /* Success */ |
| 1944 | ret = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1945 | out: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1946 | sshbuf_free(ca); |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 1947 | sshbuf_free(crit); |
| 1948 | sshbuf_free(exts); |
| 1949 | sshbuf_free(principals); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1950 | free(sig); |
| 1951 | return ret; |
| 1952 | } |
| 1953 | |
| 1954 | static int |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1955 | sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp, |
| 1956 | int allow_cert) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1957 | { |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 1958 | int type, ret = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1959 | char *ktype = NULL, *curve = NULL; |
| 1960 | struct sshkey *key = NULL; |
| 1961 | size_t len; |
| 1962 | u_char *pk = NULL; |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1963 | struct sshbuf *copy; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1964 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
| 1965 | EC_POINT *q = NULL; |
| 1966 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
| 1967 | |
| 1968 | #ifdef DEBUG_PK /* XXX */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1969 | sshbuf_dump(b, stderr); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1970 | #endif |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 1971 | if (keyp != NULL) |
| 1972 | *keyp = NULL; |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1973 | if ((copy = sshbuf_fromb(b)) == NULL) { |
| 1974 | ret = SSH_ERR_ALLOC_FAIL; |
| 1975 | goto out; |
| 1976 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1977 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0) { |
| 1978 | ret = SSH_ERR_INVALID_FORMAT; |
| 1979 | goto out; |
| 1980 | } |
| 1981 | |
| 1982 | type = sshkey_type_from_name(ktype); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1983 | if (!allow_cert && sshkey_type_is_cert(type)) { |
| 1984 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 1985 | goto out; |
| 1986 | } |
| 1987 | switch (type) { |
| 1988 | #ifdef WITH_OPENSSL |
| 1989 | case KEY_RSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 1990 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1991 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 1992 | ret = SSH_ERR_INVALID_FORMAT; |
| 1993 | goto out; |
| 1994 | } |
| 1995 | /* FALLTHROUGH */ |
| 1996 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1997 | if ((key = sshkey_new(type)) == NULL) { |
| 1998 | ret = SSH_ERR_ALLOC_FAIL; |
| 1999 | goto out; |
| 2000 | } |
djm@openbsd.org | 3f4ea3c | 2015-04-03 22:17:27 +0000 | [diff] [blame] | 2001 | if (sshbuf_get_bignum2(b, key->rsa->e) != 0 || |
| 2002 | sshbuf_get_bignum2(b, key->rsa->n) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2003 | ret = SSH_ERR_INVALID_FORMAT; |
| 2004 | goto out; |
| 2005 | } |
| 2006 | #ifdef DEBUG_PK |
| 2007 | RSA_print_fp(stderr, key->rsa, 8); |
| 2008 | #endif |
| 2009 | break; |
| 2010 | case KEY_DSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2011 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2012 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2013 | ret = SSH_ERR_INVALID_FORMAT; |
| 2014 | goto out; |
| 2015 | } |
| 2016 | /* FALLTHROUGH */ |
| 2017 | case KEY_DSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2018 | if ((key = sshkey_new(type)) == NULL) { |
| 2019 | ret = SSH_ERR_ALLOC_FAIL; |
| 2020 | goto out; |
| 2021 | } |
djm@openbsd.org | 3f4ea3c | 2015-04-03 22:17:27 +0000 | [diff] [blame] | 2022 | if (sshbuf_get_bignum2(b, key->dsa->p) != 0 || |
| 2023 | sshbuf_get_bignum2(b, key->dsa->q) != 0 || |
| 2024 | sshbuf_get_bignum2(b, key->dsa->g) != 0 || |
| 2025 | sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2026 | ret = SSH_ERR_INVALID_FORMAT; |
| 2027 | goto out; |
| 2028 | } |
| 2029 | #ifdef DEBUG_PK |
| 2030 | DSA_print_fp(stderr, key->dsa, 8); |
| 2031 | #endif |
| 2032 | break; |
| 2033 | case KEY_ECDSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2034 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2035 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2036 | ret = SSH_ERR_INVALID_FORMAT; |
| 2037 | goto out; |
| 2038 | } |
| 2039 | /* FALLTHROUGH */ |
| 2040 | # ifdef OPENSSL_HAS_ECC |
| 2041 | case KEY_ECDSA: |
| 2042 | if ((key = sshkey_new(type)) == NULL) { |
| 2043 | ret = SSH_ERR_ALLOC_FAIL; |
| 2044 | goto out; |
| 2045 | } |
djm@openbsd.org | 54924b5 | 2015-01-14 10:46:28 +0000 | [diff] [blame] | 2046 | key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2047 | if (sshbuf_get_cstring(b, &curve, NULL) != 0) { |
| 2048 | ret = SSH_ERR_INVALID_FORMAT; |
| 2049 | goto out; |
| 2050 | } |
| 2051 | if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) { |
| 2052 | ret = SSH_ERR_EC_CURVE_MISMATCH; |
| 2053 | goto out; |
| 2054 | } |
| 2055 | if (key->ecdsa != NULL) |
| 2056 | EC_KEY_free(key->ecdsa); |
| 2057 | if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid)) |
| 2058 | == NULL) { |
| 2059 | ret = SSH_ERR_EC_CURVE_INVALID; |
| 2060 | goto out; |
| 2061 | } |
| 2062 | if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL) { |
| 2063 | ret = SSH_ERR_ALLOC_FAIL; |
| 2064 | goto out; |
| 2065 | } |
| 2066 | if (sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa)) != 0) { |
| 2067 | ret = SSH_ERR_INVALID_FORMAT; |
| 2068 | goto out; |
| 2069 | } |
| 2070 | if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa), |
| 2071 | q) != 0) { |
| 2072 | ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
| 2073 | goto out; |
| 2074 | } |
| 2075 | if (EC_KEY_set_public_key(key->ecdsa, q) != 1) { |
| 2076 | /* XXX assume it is a allocation error */ |
| 2077 | ret = SSH_ERR_ALLOC_FAIL; |
| 2078 | goto out; |
| 2079 | } |
| 2080 | #ifdef DEBUG_PK |
| 2081 | sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q); |
| 2082 | #endif |
| 2083 | break; |
| 2084 | # endif /* OPENSSL_HAS_ECC */ |
| 2085 | #endif /* WITH_OPENSSL */ |
| 2086 | case KEY_ED25519_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2087 | /* Skip nonce */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2088 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
| 2089 | ret = SSH_ERR_INVALID_FORMAT; |
| 2090 | goto out; |
| 2091 | } |
| 2092 | /* FALLTHROUGH */ |
| 2093 | case KEY_ED25519: |
| 2094 | if ((ret = sshbuf_get_string(b, &pk, &len)) != 0) |
| 2095 | goto out; |
| 2096 | if (len != ED25519_PK_SZ) { |
| 2097 | ret = SSH_ERR_INVALID_FORMAT; |
| 2098 | goto out; |
| 2099 | } |
| 2100 | if ((key = sshkey_new(type)) == NULL) { |
| 2101 | ret = SSH_ERR_ALLOC_FAIL; |
| 2102 | goto out; |
| 2103 | } |
| 2104 | key->ed25519_pk = pk; |
| 2105 | pk = NULL; |
| 2106 | break; |
| 2107 | case KEY_UNSPEC: |
| 2108 | if ((key = sshkey_new(type)) == NULL) { |
| 2109 | ret = SSH_ERR_ALLOC_FAIL; |
| 2110 | goto out; |
| 2111 | } |
| 2112 | break; |
| 2113 | default: |
| 2114 | ret = SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2115 | goto out; |
| 2116 | } |
| 2117 | |
| 2118 | /* Parse certificate potion */ |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2119 | if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2120 | goto out; |
| 2121 | |
| 2122 | if (key != NULL && sshbuf_len(b) != 0) { |
| 2123 | ret = SSH_ERR_INVALID_FORMAT; |
| 2124 | goto out; |
| 2125 | } |
| 2126 | ret = 0; |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 2127 | if (keyp != NULL) { |
| 2128 | *keyp = key; |
| 2129 | key = NULL; |
| 2130 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2131 | out: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2132 | sshbuf_free(copy); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2133 | sshkey_free(key); |
| 2134 | free(ktype); |
| 2135 | free(curve); |
| 2136 | free(pk); |
| 2137 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
| 2138 | if (q != NULL) |
| 2139 | EC_POINT_free(q); |
| 2140 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
| 2141 | return ret; |
| 2142 | } |
| 2143 | |
| 2144 | int |
| 2145 | sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp) |
| 2146 | { |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2147 | struct sshbuf *b; |
| 2148 | int r; |
| 2149 | |
| 2150 | if ((b = sshbuf_from(blob, blen)) == NULL) |
| 2151 | return SSH_ERR_ALLOC_FAIL; |
| 2152 | r = sshkey_from_blob_internal(b, keyp, 1); |
| 2153 | sshbuf_free(b); |
| 2154 | return r; |
| 2155 | } |
| 2156 | |
| 2157 | int |
| 2158 | sshkey_fromb(struct sshbuf *b, struct sshkey **keyp) |
| 2159 | { |
| 2160 | return sshkey_from_blob_internal(b, keyp, 1); |
| 2161 | } |
| 2162 | |
| 2163 | int |
| 2164 | sshkey_froms(struct sshbuf *buf, struct sshkey **keyp) |
| 2165 | { |
| 2166 | struct sshbuf *b; |
| 2167 | int r; |
| 2168 | |
| 2169 | if ((r = sshbuf_froms(buf, &b)) != 0) |
| 2170 | return r; |
| 2171 | r = sshkey_from_blob_internal(b, keyp, 1); |
| 2172 | sshbuf_free(b); |
| 2173 | return r; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | int |
| 2177 | sshkey_sign(const struct sshkey *key, |
| 2178 | u_char **sigp, size_t *lenp, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 2179 | const u_char *data, size_t datalen, const char *alg, u_int compat) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2180 | { |
| 2181 | if (sigp != NULL) |
| 2182 | *sigp = NULL; |
| 2183 | if (lenp != NULL) |
| 2184 | *lenp = 0; |
| 2185 | if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE) |
| 2186 | return SSH_ERR_INVALID_ARGUMENT; |
| 2187 | switch (key->type) { |
| 2188 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2189 | case KEY_DSA_CERT: |
| 2190 | case KEY_DSA: |
| 2191 | return ssh_dss_sign(key, sigp, lenp, data, datalen, compat); |
| 2192 | # ifdef OPENSSL_HAS_ECC |
| 2193 | case KEY_ECDSA_CERT: |
| 2194 | case KEY_ECDSA: |
| 2195 | return ssh_ecdsa_sign(key, sigp, lenp, data, datalen, compat); |
| 2196 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2197 | case KEY_RSA_CERT: |
| 2198 | case KEY_RSA: |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 2199 | return ssh_rsa_sign(key, sigp, lenp, data, datalen, alg); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2200 | #endif /* WITH_OPENSSL */ |
| 2201 | case KEY_ED25519: |
| 2202 | case KEY_ED25519_CERT: |
| 2203 | return ssh_ed25519_sign(key, sigp, lenp, data, datalen, compat); |
| 2204 | default: |
| 2205 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * ssh_key_verify returns 0 for a correct signature and < 0 on error. |
| 2211 | */ |
| 2212 | int |
| 2213 | sshkey_verify(const struct sshkey *key, |
| 2214 | const u_char *sig, size_t siglen, |
| 2215 | const u_char *data, size_t dlen, u_int compat) |
| 2216 | { |
djm@openbsd.org | 4cf87f4 | 2014-12-10 01:24:09 +0000 | [diff] [blame] | 2217 | if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2218 | return SSH_ERR_INVALID_ARGUMENT; |
| 2219 | switch (key->type) { |
| 2220 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2221 | case KEY_DSA_CERT: |
| 2222 | case KEY_DSA: |
| 2223 | return ssh_dss_verify(key, sig, siglen, data, dlen, compat); |
| 2224 | # ifdef OPENSSL_HAS_ECC |
| 2225 | case KEY_ECDSA_CERT: |
| 2226 | case KEY_ECDSA: |
| 2227 | return ssh_ecdsa_verify(key, sig, siglen, data, dlen, compat); |
| 2228 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2229 | case KEY_RSA_CERT: |
| 2230 | case KEY_RSA: |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 2231 | return ssh_rsa_verify(key, sig, siglen, data, dlen); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2232 | #endif /* WITH_OPENSSL */ |
| 2233 | case KEY_ED25519: |
| 2234 | case KEY_ED25519_CERT: |
| 2235 | return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat); |
| 2236 | default: |
| 2237 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | /* Converts a private to a public key */ |
| 2242 | int |
| 2243 | sshkey_demote(const struct sshkey *k, struct sshkey **dkp) |
| 2244 | { |
| 2245 | struct sshkey *pk; |
| 2246 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 2247 | |
djm@openbsd.org | 1a2663a | 2015-10-15 23:08:23 +0000 | [diff] [blame] | 2248 | *dkp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2249 | if ((pk = calloc(1, sizeof(*pk))) == NULL) |
| 2250 | return SSH_ERR_ALLOC_FAIL; |
| 2251 | pk->type = k->type; |
| 2252 | pk->flags = k->flags; |
| 2253 | pk->ecdsa_nid = k->ecdsa_nid; |
| 2254 | pk->dsa = NULL; |
| 2255 | pk->ecdsa = NULL; |
| 2256 | pk->rsa = NULL; |
| 2257 | pk->ed25519_pk = NULL; |
| 2258 | pk->ed25519_sk = NULL; |
| 2259 | |
| 2260 | switch (k->type) { |
| 2261 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2262 | case KEY_RSA_CERT: |
| 2263 | if ((ret = sshkey_cert_copy(k, pk)) != 0) |
| 2264 | goto fail; |
| 2265 | /* FALLTHROUGH */ |
| 2266 | case KEY_RSA1: |
| 2267 | case KEY_RSA: |
| 2268 | if ((pk->rsa = RSA_new()) == NULL || |
| 2269 | (pk->rsa->e = BN_dup(k->rsa->e)) == NULL || |
| 2270 | (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) { |
| 2271 | ret = SSH_ERR_ALLOC_FAIL; |
| 2272 | goto fail; |
| 2273 | } |
| 2274 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2275 | case KEY_DSA_CERT: |
| 2276 | if ((ret = sshkey_cert_copy(k, pk)) != 0) |
| 2277 | goto fail; |
| 2278 | /* FALLTHROUGH */ |
| 2279 | case KEY_DSA: |
| 2280 | if ((pk->dsa = DSA_new()) == NULL || |
| 2281 | (pk->dsa->p = BN_dup(k->dsa->p)) == NULL || |
| 2282 | (pk->dsa->q = BN_dup(k->dsa->q)) == NULL || |
| 2283 | (pk->dsa->g = BN_dup(k->dsa->g)) == NULL || |
| 2284 | (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) { |
| 2285 | ret = SSH_ERR_ALLOC_FAIL; |
| 2286 | goto fail; |
| 2287 | } |
| 2288 | break; |
| 2289 | case KEY_ECDSA_CERT: |
| 2290 | if ((ret = sshkey_cert_copy(k, pk)) != 0) |
| 2291 | goto fail; |
| 2292 | /* FALLTHROUGH */ |
| 2293 | # ifdef OPENSSL_HAS_ECC |
| 2294 | case KEY_ECDSA: |
| 2295 | pk->ecdsa = EC_KEY_new_by_curve_name(pk->ecdsa_nid); |
| 2296 | if (pk->ecdsa == NULL) { |
| 2297 | ret = SSH_ERR_ALLOC_FAIL; |
| 2298 | goto fail; |
| 2299 | } |
| 2300 | if (EC_KEY_set_public_key(pk->ecdsa, |
| 2301 | EC_KEY_get0_public_key(k->ecdsa)) != 1) { |
| 2302 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2303 | goto fail; |
| 2304 | } |
| 2305 | break; |
| 2306 | # endif /* OPENSSL_HAS_ECC */ |
| 2307 | #endif /* WITH_OPENSSL */ |
| 2308 | case KEY_ED25519_CERT: |
| 2309 | if ((ret = sshkey_cert_copy(k, pk)) != 0) |
| 2310 | goto fail; |
| 2311 | /* FALLTHROUGH */ |
| 2312 | case KEY_ED25519: |
| 2313 | if (k->ed25519_pk != NULL) { |
| 2314 | if ((pk->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) { |
| 2315 | ret = SSH_ERR_ALLOC_FAIL; |
| 2316 | goto fail; |
| 2317 | } |
| 2318 | memcpy(pk->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ); |
| 2319 | } |
| 2320 | break; |
| 2321 | default: |
| 2322 | ret = SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2323 | fail: |
| 2324 | sshkey_free(pk); |
| 2325 | return ret; |
| 2326 | } |
| 2327 | *dkp = pk; |
| 2328 | return 0; |
| 2329 | } |
| 2330 | |
| 2331 | /* Convert a plain key to their _CERT equivalent */ |
| 2332 | int |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2333 | sshkey_to_certified(struct sshkey *k) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2334 | { |
| 2335 | int newtype; |
| 2336 | |
| 2337 | switch (k->type) { |
| 2338 | #ifdef WITH_OPENSSL |
| 2339 | case KEY_RSA: |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2340 | newtype = KEY_RSA_CERT; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2341 | break; |
| 2342 | case KEY_DSA: |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2343 | newtype = KEY_DSA_CERT; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2344 | break; |
| 2345 | case KEY_ECDSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2346 | newtype = KEY_ECDSA_CERT; |
| 2347 | break; |
| 2348 | #endif /* WITH_OPENSSL */ |
| 2349 | case KEY_ED25519: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2350 | newtype = KEY_ED25519_CERT; |
| 2351 | break; |
| 2352 | default: |
| 2353 | return SSH_ERR_INVALID_ARGUMENT; |
| 2354 | } |
| 2355 | if ((k->cert = cert_new()) == NULL) |
| 2356 | return SSH_ERR_ALLOC_FAIL; |
| 2357 | k->type = newtype; |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | /* Convert a certificate to its raw key equivalent */ |
| 2362 | int |
| 2363 | sshkey_drop_cert(struct sshkey *k) |
| 2364 | { |
| 2365 | if (!sshkey_type_is_cert(k->type)) |
| 2366 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2367 | cert_free(k->cert); |
| 2368 | k->cert = NULL; |
| 2369 | k->type = sshkey_type_plain(k->type); |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
| 2373 | /* Sign a certified key, (re-)generating the signed certblob. */ |
| 2374 | int |
djm@openbsd.org | 57464e3 | 2016-05-02 09:36:42 +0000 | [diff] [blame] | 2375 | sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2376 | { |
| 2377 | struct sshbuf *principals = NULL; |
| 2378 | u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32]; |
| 2379 | size_t i, ca_len, sig_len; |
| 2380 | int ret = SSH_ERR_INTERNAL_ERROR; |
| 2381 | struct sshbuf *cert; |
| 2382 | |
| 2383 | if (k == NULL || k->cert == NULL || |
| 2384 | k->cert->certblob == NULL || ca == NULL) |
| 2385 | return SSH_ERR_INVALID_ARGUMENT; |
| 2386 | if (!sshkey_is_cert(k)) |
| 2387 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2388 | if (!sshkey_type_is_valid_ca(ca->type)) |
| 2389 | return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 2390 | |
| 2391 | if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0) |
| 2392 | return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
| 2393 | |
| 2394 | cert = k->cert->certblob; /* for readability */ |
| 2395 | sshbuf_reset(cert); |
| 2396 | if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0) |
| 2397 | goto out; |
| 2398 | |
| 2399 | /* -v01 certs put nonce first */ |
| 2400 | arc4random_buf(&nonce, sizeof(nonce)); |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2401 | if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0) |
| 2402 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2403 | |
| 2404 | /* XXX this substantially duplicates to_blob(); refactor */ |
| 2405 | switch (k->type) { |
| 2406 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2407 | case KEY_DSA_CERT: |
| 2408 | if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 || |
| 2409 | (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 || |
| 2410 | (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 || |
| 2411 | (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0) |
| 2412 | goto out; |
| 2413 | break; |
| 2414 | # ifdef OPENSSL_HAS_ECC |
| 2415 | case KEY_ECDSA_CERT: |
| 2416 | if ((ret = sshbuf_put_cstring(cert, |
| 2417 | sshkey_curve_nid_to_name(k->ecdsa_nid))) != 0 || |
| 2418 | (ret = sshbuf_put_ec(cert, |
| 2419 | EC_KEY_get0_public_key(k->ecdsa), |
| 2420 | EC_KEY_get0_group(k->ecdsa))) != 0) |
| 2421 | goto out; |
| 2422 | break; |
| 2423 | # endif /* OPENSSL_HAS_ECC */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2424 | case KEY_RSA_CERT: |
| 2425 | if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 || |
| 2426 | (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0) |
| 2427 | goto out; |
| 2428 | break; |
| 2429 | #endif /* WITH_OPENSSL */ |
| 2430 | case KEY_ED25519_CERT: |
| 2431 | if ((ret = sshbuf_put_string(cert, |
| 2432 | k->ed25519_pk, ED25519_PK_SZ)) != 0) |
| 2433 | goto out; |
| 2434 | break; |
| 2435 | default: |
| 2436 | ret = SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 55e5bde | 2015-03-06 01:40:56 +0000 | [diff] [blame] | 2437 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2438 | } |
| 2439 | |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2440 | if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 || |
| 2441 | (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2442 | (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0) |
| 2443 | goto out; |
| 2444 | |
| 2445 | if ((principals = sshbuf_new()) == NULL) { |
| 2446 | ret = SSH_ERR_ALLOC_FAIL; |
| 2447 | goto out; |
| 2448 | } |
| 2449 | for (i = 0; i < k->cert->nprincipals; i++) { |
| 2450 | if ((ret = sshbuf_put_cstring(principals, |
| 2451 | k->cert->principals[i])) != 0) |
| 2452 | goto out; |
| 2453 | } |
| 2454 | if ((ret = sshbuf_put_stringb(cert, principals)) != 0 || |
| 2455 | (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 || |
| 2456 | (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 || |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 2457 | (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 || |
| 2458 | (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 || |
| 2459 | (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2460 | (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0) |
| 2461 | goto out; |
| 2462 | |
| 2463 | /* Sign the whole mess */ |
| 2464 | if ((ret = sshkey_sign(ca, &sig_blob, &sig_len, sshbuf_ptr(cert), |
djm@openbsd.org | 57464e3 | 2016-05-02 09:36:42 +0000 | [diff] [blame] | 2465 | sshbuf_len(cert), alg, 0)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2466 | goto out; |
| 2467 | |
| 2468 | /* Append signature and we are done */ |
| 2469 | if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0) |
| 2470 | goto out; |
| 2471 | ret = 0; |
| 2472 | out: |
| 2473 | if (ret != 0) |
| 2474 | sshbuf_reset(cert); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 2475 | free(sig_blob); |
| 2476 | free(ca_blob); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 2477 | sshbuf_free(principals); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2478 | return ret; |
| 2479 | } |
| 2480 | |
| 2481 | int |
| 2482 | sshkey_cert_check_authority(const struct sshkey *k, |
| 2483 | int want_host, int require_principal, |
| 2484 | const char *name, const char **reason) |
| 2485 | { |
| 2486 | u_int i, principal_matches; |
| 2487 | time_t now = time(NULL); |
| 2488 | |
| 2489 | if (reason != NULL) |
| 2490 | *reason = NULL; |
| 2491 | |
| 2492 | if (want_host) { |
| 2493 | if (k->cert->type != SSH2_CERT_TYPE_HOST) { |
| 2494 | *reason = "Certificate invalid: not a host certificate"; |
| 2495 | return SSH_ERR_KEY_CERT_INVALID; |
| 2496 | } |
| 2497 | } else { |
| 2498 | if (k->cert->type != SSH2_CERT_TYPE_USER) { |
| 2499 | *reason = "Certificate invalid: not a user certificate"; |
| 2500 | return SSH_ERR_KEY_CERT_INVALID; |
| 2501 | } |
| 2502 | } |
| 2503 | if (now < 0) { |
| 2504 | /* yikes - system clock before epoch! */ |
| 2505 | *reason = "Certificate invalid: not yet valid"; |
| 2506 | return SSH_ERR_KEY_CERT_INVALID; |
| 2507 | } |
| 2508 | if ((u_int64_t)now < k->cert->valid_after) { |
| 2509 | *reason = "Certificate invalid: not yet valid"; |
| 2510 | return SSH_ERR_KEY_CERT_INVALID; |
| 2511 | } |
| 2512 | if ((u_int64_t)now >= k->cert->valid_before) { |
| 2513 | *reason = "Certificate invalid: expired"; |
| 2514 | return SSH_ERR_KEY_CERT_INVALID; |
| 2515 | } |
| 2516 | if (k->cert->nprincipals == 0) { |
| 2517 | if (require_principal) { |
| 2518 | *reason = "Certificate lacks principal list"; |
| 2519 | return SSH_ERR_KEY_CERT_INVALID; |
| 2520 | } |
| 2521 | } else if (name != NULL) { |
| 2522 | principal_matches = 0; |
| 2523 | for (i = 0; i < k->cert->nprincipals; i++) { |
| 2524 | if (strcmp(name, k->cert->principals[i]) == 0) { |
| 2525 | principal_matches = 1; |
| 2526 | break; |
| 2527 | } |
| 2528 | } |
| 2529 | if (!principal_matches) { |
| 2530 | *reason = "Certificate invalid: name is not a listed " |
| 2531 | "principal"; |
| 2532 | return SSH_ERR_KEY_CERT_INVALID; |
| 2533 | } |
| 2534 | } |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
djm@openbsd.org | 499cf36 | 2015-11-19 01:08:55 +0000 | [diff] [blame] | 2538 | size_t |
| 2539 | sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l) |
| 2540 | { |
| 2541 | char from[32], to[32], ret[64]; |
| 2542 | time_t tt; |
| 2543 | struct tm *tm; |
| 2544 | |
| 2545 | *from = *to = '\0'; |
| 2546 | if (cert->valid_after == 0 && |
| 2547 | cert->valid_before == 0xffffffffffffffffULL) |
| 2548 | return strlcpy(s, "forever", l); |
| 2549 | |
| 2550 | if (cert->valid_after != 0) { |
| 2551 | /* XXX revisit INT_MAX in 2038 :) */ |
| 2552 | tt = cert->valid_after > INT_MAX ? |
| 2553 | INT_MAX : cert->valid_after; |
| 2554 | tm = localtime(&tt); |
| 2555 | strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm); |
| 2556 | } |
| 2557 | if (cert->valid_before != 0xffffffffffffffffULL) { |
| 2558 | /* XXX revisit INT_MAX in 2038 :) */ |
| 2559 | tt = cert->valid_before > INT_MAX ? |
| 2560 | INT_MAX : cert->valid_before; |
| 2561 | tm = localtime(&tt); |
| 2562 | strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm); |
| 2563 | } |
| 2564 | |
| 2565 | if (cert->valid_after == 0) |
| 2566 | snprintf(ret, sizeof(ret), "before %s", to); |
| 2567 | else if (cert->valid_before == 0xffffffffffffffffULL) |
| 2568 | snprintf(ret, sizeof(ret), "after %s", from); |
| 2569 | else |
| 2570 | snprintf(ret, sizeof(ret), "from %s to %s", from, to); |
| 2571 | |
| 2572 | return strlcpy(s, ret, l); |
| 2573 | } |
| 2574 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2575 | int |
| 2576 | sshkey_private_serialize(const struct sshkey *key, struct sshbuf *b) |
| 2577 | { |
| 2578 | int r = SSH_ERR_INTERNAL_ERROR; |
| 2579 | |
| 2580 | if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0) |
| 2581 | goto out; |
| 2582 | switch (key->type) { |
| 2583 | #ifdef WITH_OPENSSL |
| 2584 | case KEY_RSA: |
| 2585 | if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 || |
| 2586 | (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 || |
| 2587 | (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 || |
| 2588 | (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 || |
| 2589 | (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 || |
| 2590 | (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0) |
| 2591 | goto out; |
| 2592 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2593 | case KEY_RSA_CERT: |
| 2594 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 2595 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2596 | goto out; |
| 2597 | } |
| 2598 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 2599 | (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 || |
| 2600 | (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 || |
| 2601 | (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 || |
| 2602 | (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0) |
| 2603 | goto out; |
| 2604 | break; |
| 2605 | case KEY_DSA: |
| 2606 | if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 || |
| 2607 | (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 || |
| 2608 | (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 || |
| 2609 | (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 || |
| 2610 | (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0) |
| 2611 | goto out; |
| 2612 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2613 | case KEY_DSA_CERT: |
| 2614 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 2615 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2616 | goto out; |
| 2617 | } |
| 2618 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 2619 | (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0) |
| 2620 | goto out; |
| 2621 | break; |
| 2622 | # ifdef OPENSSL_HAS_ECC |
| 2623 | case KEY_ECDSA: |
| 2624 | if ((r = sshbuf_put_cstring(b, |
| 2625 | sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 || |
| 2626 | (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 || |
| 2627 | (r = sshbuf_put_bignum2(b, |
| 2628 | EC_KEY_get0_private_key(key->ecdsa))) != 0) |
| 2629 | goto out; |
| 2630 | break; |
| 2631 | case KEY_ECDSA_CERT: |
| 2632 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 2633 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2634 | goto out; |
| 2635 | } |
| 2636 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 2637 | (r = sshbuf_put_bignum2(b, |
| 2638 | EC_KEY_get0_private_key(key->ecdsa))) != 0) |
| 2639 | goto out; |
| 2640 | break; |
| 2641 | # endif /* OPENSSL_HAS_ECC */ |
| 2642 | #endif /* WITH_OPENSSL */ |
| 2643 | case KEY_ED25519: |
| 2644 | if ((r = sshbuf_put_string(b, key->ed25519_pk, |
| 2645 | ED25519_PK_SZ)) != 0 || |
| 2646 | (r = sshbuf_put_string(b, key->ed25519_sk, |
| 2647 | ED25519_SK_SZ)) != 0) |
| 2648 | goto out; |
| 2649 | break; |
| 2650 | case KEY_ED25519_CERT: |
| 2651 | if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) { |
| 2652 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2653 | goto out; |
| 2654 | } |
| 2655 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 || |
| 2656 | (r = sshbuf_put_string(b, key->ed25519_pk, |
| 2657 | ED25519_PK_SZ)) != 0 || |
| 2658 | (r = sshbuf_put_string(b, key->ed25519_sk, |
| 2659 | ED25519_SK_SZ)) != 0) |
| 2660 | goto out; |
| 2661 | break; |
| 2662 | default: |
| 2663 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2664 | goto out; |
| 2665 | } |
| 2666 | /* success */ |
| 2667 | r = 0; |
| 2668 | out: |
| 2669 | return r; |
| 2670 | } |
| 2671 | |
| 2672 | int |
| 2673 | sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) |
| 2674 | { |
| 2675 | char *tname = NULL, *curve = NULL; |
| 2676 | struct sshkey *k = NULL; |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2677 | size_t pklen = 0, sklen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2678 | int type, r = SSH_ERR_INTERNAL_ERROR; |
| 2679 | u_char *ed25519_pk = NULL, *ed25519_sk = NULL; |
| 2680 | #ifdef WITH_OPENSSL |
| 2681 | BIGNUM *exponent = NULL; |
| 2682 | #endif /* WITH_OPENSSL */ |
| 2683 | |
| 2684 | if (kp != NULL) |
| 2685 | *kp = NULL; |
| 2686 | if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0) |
| 2687 | goto out; |
| 2688 | type = sshkey_type_from_name(tname); |
| 2689 | switch (type) { |
| 2690 | #ifdef WITH_OPENSSL |
| 2691 | case KEY_DSA: |
| 2692 | if ((k = sshkey_new_private(type)) == NULL) { |
| 2693 | r = SSH_ERR_ALLOC_FAIL; |
| 2694 | goto out; |
| 2695 | } |
| 2696 | if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 || |
| 2697 | (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 || |
| 2698 | (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 || |
| 2699 | (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 || |
| 2700 | (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0) |
| 2701 | goto out; |
| 2702 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2703 | case KEY_DSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2704 | if ((r = sshkey_froms(buf, &k)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2705 | (r = sshkey_add_private(k)) != 0 || |
| 2706 | (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0) |
| 2707 | goto out; |
| 2708 | break; |
| 2709 | # ifdef OPENSSL_HAS_ECC |
| 2710 | case KEY_ECDSA: |
| 2711 | if ((k = sshkey_new_private(type)) == NULL) { |
| 2712 | r = SSH_ERR_ALLOC_FAIL; |
| 2713 | goto out; |
| 2714 | } |
| 2715 | if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) { |
| 2716 | r = SSH_ERR_INVALID_ARGUMENT; |
| 2717 | goto out; |
| 2718 | } |
| 2719 | if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0) |
| 2720 | goto out; |
| 2721 | if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) { |
| 2722 | r = SSH_ERR_EC_CURVE_MISMATCH; |
| 2723 | goto out; |
| 2724 | } |
| 2725 | k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid); |
| 2726 | if (k->ecdsa == NULL || (exponent = BN_new()) == NULL) { |
| 2727 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 2728 | goto out; |
| 2729 | } |
| 2730 | if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 || |
| 2731 | (r = sshbuf_get_bignum2(buf, exponent))) |
| 2732 | goto out; |
| 2733 | if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) { |
| 2734 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 2735 | goto out; |
| 2736 | } |
| 2737 | if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa), |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 2738 | EC_KEY_get0_public_key(k->ecdsa))) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2739 | (r = sshkey_ec_validate_private(k->ecdsa)) != 0) |
| 2740 | goto out; |
| 2741 | break; |
| 2742 | case KEY_ECDSA_CERT: |
| 2743 | if ((exponent = BN_new()) == NULL) { |
| 2744 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 2745 | goto out; |
| 2746 | } |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2747 | if ((r = sshkey_froms(buf, &k)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2748 | (r = sshkey_add_private(k)) != 0 || |
| 2749 | (r = sshbuf_get_bignum2(buf, exponent)) != 0) |
| 2750 | goto out; |
| 2751 | if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) { |
| 2752 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 2753 | goto out; |
| 2754 | } |
| 2755 | if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa), |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 2756 | EC_KEY_get0_public_key(k->ecdsa))) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2757 | (r = sshkey_ec_validate_private(k->ecdsa)) != 0) |
| 2758 | goto out; |
| 2759 | break; |
| 2760 | # endif /* OPENSSL_HAS_ECC */ |
| 2761 | case KEY_RSA: |
| 2762 | if ((k = sshkey_new_private(type)) == NULL) { |
| 2763 | r = SSH_ERR_ALLOC_FAIL; |
| 2764 | goto out; |
| 2765 | } |
| 2766 | if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 || |
| 2767 | (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 || |
| 2768 | (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 || |
| 2769 | (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 || |
| 2770 | (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 || |
| 2771 | (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 || |
| 2772 | (r = rsa_generate_additional_parameters(k->rsa)) != 0) |
| 2773 | goto out; |
| 2774 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2775 | case KEY_RSA_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2776 | if ((r = sshkey_froms(buf, &k)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2777 | (r = sshkey_add_private(k)) != 0 || |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 2778 | (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 || |
| 2779 | (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 || |
| 2780 | (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 || |
| 2781 | (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2782 | (r = rsa_generate_additional_parameters(k->rsa)) != 0) |
| 2783 | goto out; |
| 2784 | break; |
| 2785 | #endif /* WITH_OPENSSL */ |
| 2786 | case KEY_ED25519: |
| 2787 | if ((k = sshkey_new_private(type)) == NULL) { |
| 2788 | r = SSH_ERR_ALLOC_FAIL; |
| 2789 | goto out; |
| 2790 | } |
| 2791 | if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 || |
| 2792 | (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0) |
| 2793 | goto out; |
| 2794 | if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) { |
| 2795 | r = SSH_ERR_INVALID_FORMAT; |
| 2796 | goto out; |
| 2797 | } |
| 2798 | k->ed25519_pk = ed25519_pk; |
| 2799 | k->ed25519_sk = ed25519_sk; |
| 2800 | ed25519_pk = ed25519_sk = NULL; |
| 2801 | break; |
| 2802 | case KEY_ED25519_CERT: |
djm@openbsd.org | 60b1825 | 2015-01-26 02:59:11 +0000 | [diff] [blame] | 2803 | if ((r = sshkey_froms(buf, &k)) != 0 || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2804 | (r = sshkey_add_private(k)) != 0 || |
| 2805 | (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 || |
| 2806 | (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0) |
| 2807 | goto out; |
| 2808 | if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) { |
| 2809 | r = SSH_ERR_INVALID_FORMAT; |
| 2810 | goto out; |
| 2811 | } |
| 2812 | k->ed25519_pk = ed25519_pk; |
| 2813 | k->ed25519_sk = ed25519_sk; |
| 2814 | ed25519_pk = ed25519_sk = NULL; |
| 2815 | break; |
| 2816 | default: |
| 2817 | r = SSH_ERR_KEY_TYPE_UNKNOWN; |
| 2818 | goto out; |
| 2819 | } |
| 2820 | #ifdef WITH_OPENSSL |
| 2821 | /* enable blinding */ |
| 2822 | switch (k->type) { |
| 2823 | case KEY_RSA: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2824 | case KEY_RSA_CERT: |
| 2825 | case KEY_RSA1: |
| 2826 | if (RSA_blinding_on(k->rsa, NULL) != 1) { |
| 2827 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 2828 | goto out; |
| 2829 | } |
| 2830 | break; |
| 2831 | } |
| 2832 | #endif /* WITH_OPENSSL */ |
| 2833 | /* success */ |
| 2834 | r = 0; |
| 2835 | if (kp != NULL) { |
| 2836 | *kp = k; |
| 2837 | k = NULL; |
| 2838 | } |
| 2839 | out: |
| 2840 | free(tname); |
| 2841 | free(curve); |
| 2842 | #ifdef WITH_OPENSSL |
| 2843 | if (exponent != NULL) |
| 2844 | BN_clear_free(exponent); |
| 2845 | #endif /* WITH_OPENSSL */ |
| 2846 | sshkey_free(k); |
| 2847 | if (ed25519_pk != NULL) { |
| 2848 | explicit_bzero(ed25519_pk, pklen); |
| 2849 | free(ed25519_pk); |
| 2850 | } |
| 2851 | if (ed25519_sk != NULL) { |
| 2852 | explicit_bzero(ed25519_sk, sklen); |
| 2853 | free(ed25519_sk); |
| 2854 | } |
| 2855 | return r; |
| 2856 | } |
| 2857 | |
| 2858 | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
| 2859 | int |
| 2860 | sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public) |
| 2861 | { |
| 2862 | BN_CTX *bnctx; |
| 2863 | EC_POINT *nq = NULL; |
| 2864 | BIGNUM *order, *x, *y, *tmp; |
| 2865 | int ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
| 2866 | |
djm@openbsd.org | a571dbc | 2016-10-04 21:34:40 +0000 | [diff] [blame^] | 2867 | /* |
| 2868 | * NB. This assumes OpenSSL has already verified that the public |
| 2869 | * point lies on the curve. This is done by EC_POINT_oct2point() |
| 2870 | * implicitly calling EC_POINT_is_on_curve(). If this code is ever |
| 2871 | * reachable with public points not unmarshalled using |
| 2872 | * EC_POINT_oct2point then the caller will need to explicitly check. |
| 2873 | */ |
| 2874 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 2875 | if ((bnctx = BN_CTX_new()) == NULL) |
| 2876 | return SSH_ERR_ALLOC_FAIL; |
| 2877 | BN_CTX_start(bnctx); |
| 2878 | |
| 2879 | /* |
| 2880 | * We shouldn't ever hit this case because bignum_get_ecpoint() |
| 2881 | * refuses to load GF2m points. |
| 2882 | */ |
| 2883 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
| 2884 | NID_X9_62_prime_field) |
| 2885 | goto out; |
| 2886 | |
| 2887 | /* Q != infinity */ |
| 2888 | if (EC_POINT_is_at_infinity(group, public)) |
| 2889 | goto out; |
| 2890 | |
| 2891 | if ((x = BN_CTX_get(bnctx)) == NULL || |
| 2892 | (y = BN_CTX_get(bnctx)) == NULL || |
| 2893 | (order = BN_CTX_get(bnctx)) == NULL || |
| 2894 | (tmp = BN_CTX_get(bnctx)) == NULL) { |
| 2895 | ret = SSH_ERR_ALLOC_FAIL; |
| 2896 | goto out; |
| 2897 | } |
| 2898 | |
| 2899 | /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */ |
| 2900 | if (EC_GROUP_get_order(group, order, bnctx) != 1 || |
| 2901 | EC_POINT_get_affine_coordinates_GFp(group, public, |
| 2902 | x, y, bnctx) != 1) { |
| 2903 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2904 | goto out; |
| 2905 | } |
| 2906 | if (BN_num_bits(x) <= BN_num_bits(order) / 2 || |
| 2907 | BN_num_bits(y) <= BN_num_bits(order) / 2) |
| 2908 | goto out; |
| 2909 | |
| 2910 | /* nQ == infinity (n == order of subgroup) */ |
| 2911 | if ((nq = EC_POINT_new(group)) == NULL) { |
| 2912 | ret = SSH_ERR_ALLOC_FAIL; |
| 2913 | goto out; |
| 2914 | } |
| 2915 | if (EC_POINT_mul(group, nq, NULL, public, order, bnctx) != 1) { |
| 2916 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2917 | goto out; |
| 2918 | } |
| 2919 | if (EC_POINT_is_at_infinity(group, nq) != 1) |
| 2920 | goto out; |
| 2921 | |
| 2922 | /* x < order - 1, y < order - 1 */ |
| 2923 | if (!BN_sub(tmp, order, BN_value_one())) { |
| 2924 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2925 | goto out; |
| 2926 | } |
| 2927 | if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0) |
| 2928 | goto out; |
| 2929 | ret = 0; |
| 2930 | out: |
| 2931 | BN_CTX_free(bnctx); |
| 2932 | if (nq != NULL) |
| 2933 | EC_POINT_free(nq); |
| 2934 | return ret; |
| 2935 | } |
| 2936 | |
| 2937 | int |
| 2938 | sshkey_ec_validate_private(const EC_KEY *key) |
| 2939 | { |
| 2940 | BN_CTX *bnctx; |
| 2941 | BIGNUM *order, *tmp; |
| 2942 | int ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
| 2943 | |
| 2944 | if ((bnctx = BN_CTX_new()) == NULL) |
| 2945 | return SSH_ERR_ALLOC_FAIL; |
| 2946 | BN_CTX_start(bnctx); |
| 2947 | |
| 2948 | if ((order = BN_CTX_get(bnctx)) == NULL || |
| 2949 | (tmp = BN_CTX_get(bnctx)) == NULL) { |
| 2950 | ret = SSH_ERR_ALLOC_FAIL; |
| 2951 | goto out; |
| 2952 | } |
| 2953 | |
| 2954 | /* log2(private) > log2(order)/2 */ |
| 2955 | if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, bnctx) != 1) { |
| 2956 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2957 | goto out; |
| 2958 | } |
| 2959 | if (BN_num_bits(EC_KEY_get0_private_key(key)) <= |
| 2960 | BN_num_bits(order) / 2) |
| 2961 | goto out; |
| 2962 | |
| 2963 | /* private < order - 1 */ |
| 2964 | if (!BN_sub(tmp, order, BN_value_one())) { |
| 2965 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 2966 | goto out; |
| 2967 | } |
| 2968 | if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0) |
| 2969 | goto out; |
| 2970 | ret = 0; |
| 2971 | out: |
| 2972 | BN_CTX_free(bnctx); |
| 2973 | return ret; |
| 2974 | } |
| 2975 | |
| 2976 | void |
| 2977 | sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point) |
| 2978 | { |
| 2979 | BIGNUM *x, *y; |
| 2980 | BN_CTX *bnctx; |
| 2981 | |
| 2982 | if (point == NULL) { |
| 2983 | fputs("point=(NULL)\n", stderr); |
| 2984 | return; |
| 2985 | } |
| 2986 | if ((bnctx = BN_CTX_new()) == NULL) { |
| 2987 | fprintf(stderr, "%s: BN_CTX_new failed\n", __func__); |
| 2988 | return; |
| 2989 | } |
| 2990 | BN_CTX_start(bnctx); |
| 2991 | if ((x = BN_CTX_get(bnctx)) == NULL || |
| 2992 | (y = BN_CTX_get(bnctx)) == NULL) { |
| 2993 | fprintf(stderr, "%s: BN_CTX_get failed\n", __func__); |
| 2994 | return; |
| 2995 | } |
| 2996 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != |
| 2997 | NID_X9_62_prime_field) { |
| 2998 | fprintf(stderr, "%s: group is not a prime field\n", __func__); |
| 2999 | return; |
| 3000 | } |
| 3001 | if (EC_POINT_get_affine_coordinates_GFp(group, point, x, y, |
| 3002 | bnctx) != 1) { |
| 3003 | fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n", |
| 3004 | __func__); |
| 3005 | return; |
| 3006 | } |
| 3007 | fputs("x=", stderr); |
| 3008 | BN_print_fp(stderr, x); |
| 3009 | fputs("\ny=", stderr); |
| 3010 | BN_print_fp(stderr, y); |
| 3011 | fputs("\n", stderr); |
| 3012 | BN_CTX_free(bnctx); |
| 3013 | } |
| 3014 | |
| 3015 | void |
| 3016 | sshkey_dump_ec_key(const EC_KEY *key) |
| 3017 | { |
| 3018 | const BIGNUM *exponent; |
| 3019 | |
| 3020 | sshkey_dump_ec_point(EC_KEY_get0_group(key), |
| 3021 | EC_KEY_get0_public_key(key)); |
| 3022 | fputs("exponent=", stderr); |
| 3023 | if ((exponent = EC_KEY_get0_private_key(key)) == NULL) |
| 3024 | fputs("(NULL)", stderr); |
| 3025 | else |
| 3026 | BN_print_fp(stderr, EC_KEY_get0_private_key(key)); |
| 3027 | fputs("\n", stderr); |
| 3028 | } |
| 3029 | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
| 3030 | |
| 3031 | static int |
| 3032 | sshkey_private_to_blob2(const struct sshkey *prv, struct sshbuf *blob, |
| 3033 | const char *passphrase, const char *comment, const char *ciphername, |
| 3034 | int rounds) |
| 3035 | { |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 3036 | u_char *cp, *key = NULL, *pubkeyblob = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3037 | u_char salt[SALT_LEN]; |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 3038 | char *b64 = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3039 | size_t i, pubkeylen, keylen, ivlen, blocksize, authlen; |
| 3040 | u_int check; |
| 3041 | int r = SSH_ERR_INTERNAL_ERROR; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3042 | struct sshcipher_ctx *ciphercontext = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3043 | const struct sshcipher *cipher; |
| 3044 | const char *kdfname = KDFNAME; |
| 3045 | struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL; |
| 3046 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3047 | if (rounds <= 0) |
| 3048 | rounds = DEFAULT_ROUNDS; |
| 3049 | if (passphrase == NULL || !strlen(passphrase)) { |
| 3050 | ciphername = "none"; |
| 3051 | kdfname = "none"; |
| 3052 | } else if (ciphername == NULL) |
| 3053 | ciphername = DEFAULT_CIPHERNAME; |
| 3054 | else if (cipher_number(ciphername) != SSH_CIPHER_SSH2) { |
| 3055 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3056 | goto out; |
| 3057 | } |
| 3058 | if ((cipher = cipher_by_name(ciphername)) == NULL) { |
| 3059 | r = SSH_ERR_INTERNAL_ERROR; |
| 3060 | goto out; |
| 3061 | } |
| 3062 | |
| 3063 | if ((kdf = sshbuf_new()) == NULL || |
| 3064 | (encoded = sshbuf_new()) == NULL || |
| 3065 | (encrypted = sshbuf_new()) == NULL) { |
| 3066 | r = SSH_ERR_ALLOC_FAIL; |
| 3067 | goto out; |
| 3068 | } |
| 3069 | blocksize = cipher_blocksize(cipher); |
| 3070 | keylen = cipher_keylen(cipher); |
| 3071 | ivlen = cipher_ivlen(cipher); |
| 3072 | authlen = cipher_authlen(cipher); |
| 3073 | if ((key = calloc(1, keylen + ivlen)) == NULL) { |
| 3074 | r = SSH_ERR_ALLOC_FAIL; |
| 3075 | goto out; |
| 3076 | } |
| 3077 | if (strcmp(kdfname, "bcrypt") == 0) { |
| 3078 | arc4random_buf(salt, SALT_LEN); |
| 3079 | if (bcrypt_pbkdf(passphrase, strlen(passphrase), |
| 3080 | salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) { |
| 3081 | r = SSH_ERR_INVALID_ARGUMENT; |
| 3082 | goto out; |
| 3083 | } |
| 3084 | if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 || |
| 3085 | (r = sshbuf_put_u32(kdf, rounds)) != 0) |
| 3086 | goto out; |
| 3087 | } else if (strcmp(kdfname, "none") != 0) { |
| 3088 | /* Unsupported KDF type */ |
| 3089 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 3090 | goto out; |
| 3091 | } |
| 3092 | if ((r = cipher_init(&ciphercontext, cipher, key, keylen, |
| 3093 | key + keylen, ivlen, 1)) != 0) |
| 3094 | goto out; |
| 3095 | |
| 3096 | if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 || |
| 3097 | (r = sshbuf_put_cstring(encoded, ciphername)) != 0 || |
| 3098 | (r = sshbuf_put_cstring(encoded, kdfname)) != 0 || |
| 3099 | (r = sshbuf_put_stringb(encoded, kdf)) != 0 || |
| 3100 | (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */ |
| 3101 | (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 || |
| 3102 | (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0) |
| 3103 | goto out; |
| 3104 | |
| 3105 | /* set up the buffer that will be encrypted */ |
| 3106 | |
| 3107 | /* Random check bytes */ |
| 3108 | check = arc4random(); |
| 3109 | if ((r = sshbuf_put_u32(encrypted, check)) != 0 || |
| 3110 | (r = sshbuf_put_u32(encrypted, check)) != 0) |
| 3111 | goto out; |
| 3112 | |
| 3113 | /* append private key and comment*/ |
| 3114 | if ((r = sshkey_private_serialize(prv, encrypted)) != 0 || |
| 3115 | (r = sshbuf_put_cstring(encrypted, comment)) != 0) |
| 3116 | goto out; |
| 3117 | |
| 3118 | /* padding */ |
| 3119 | i = 0; |
| 3120 | while (sshbuf_len(encrypted) % blocksize) { |
| 3121 | if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0) |
| 3122 | goto out; |
| 3123 | } |
| 3124 | |
| 3125 | /* length in destination buffer */ |
| 3126 | if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0) |
| 3127 | goto out; |
| 3128 | |
| 3129 | /* encrypt */ |
| 3130 | if ((r = sshbuf_reserve(encoded, |
| 3131 | sshbuf_len(encrypted) + authlen, &cp)) != 0) |
| 3132 | goto out; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3133 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3134 | sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0) |
| 3135 | goto out; |
| 3136 | |
| 3137 | /* uuencode */ |
| 3138 | if ((b64 = sshbuf_dtob64(encoded)) == NULL) { |
| 3139 | r = SSH_ERR_ALLOC_FAIL; |
| 3140 | goto out; |
| 3141 | } |
| 3142 | |
| 3143 | sshbuf_reset(blob); |
| 3144 | if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0) |
| 3145 | goto out; |
| 3146 | for (i = 0; i < strlen(b64); i++) { |
| 3147 | if ((r = sshbuf_put_u8(blob, b64[i])) != 0) |
| 3148 | goto out; |
| 3149 | /* insert line breaks */ |
| 3150 | if (i % 70 == 69 && (r = sshbuf_put_u8(blob, '\n')) != 0) |
| 3151 | goto out; |
| 3152 | } |
| 3153 | if (i % 70 != 69 && (r = sshbuf_put_u8(blob, '\n')) != 0) |
| 3154 | goto out; |
| 3155 | if ((r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0) |
| 3156 | goto out; |
| 3157 | |
| 3158 | /* success */ |
| 3159 | r = 0; |
| 3160 | |
| 3161 | out: |
| 3162 | sshbuf_free(kdf); |
| 3163 | sshbuf_free(encoded); |
| 3164 | sshbuf_free(encrypted); |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3165 | cipher_free(ciphercontext); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3166 | explicit_bzero(salt, sizeof(salt)); |
| 3167 | if (key != NULL) { |
| 3168 | explicit_bzero(key, keylen + ivlen); |
| 3169 | free(key); |
| 3170 | } |
| 3171 | if (pubkeyblob != NULL) { |
| 3172 | explicit_bzero(pubkeyblob, pubkeylen); |
| 3173 | free(pubkeyblob); |
| 3174 | } |
| 3175 | if (b64 != NULL) { |
| 3176 | explicit_bzero(b64, strlen(b64)); |
| 3177 | free(b64); |
| 3178 | } |
| 3179 | return r; |
| 3180 | } |
| 3181 | |
| 3182 | static int |
| 3183 | sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase, |
| 3184 | struct sshkey **keyp, char **commentp) |
| 3185 | { |
| 3186 | char *comment = NULL, *ciphername = NULL, *kdfname = NULL; |
| 3187 | const struct sshcipher *cipher = NULL; |
| 3188 | const u_char *cp; |
| 3189 | int r = SSH_ERR_INTERNAL_ERROR; |
| 3190 | size_t encoded_len; |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 3191 | size_t i, keylen = 0, ivlen = 0, authlen = 0, slen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3192 | struct sshbuf *encoded = NULL, *decoded = NULL; |
| 3193 | struct sshbuf *kdf = NULL, *decrypted = NULL; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3194 | struct sshcipher_ctx *ciphercontext = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3195 | struct sshkey *k = NULL; |
| 3196 | u_char *key = NULL, *salt = NULL, *dp, pad, last; |
| 3197 | u_int blocksize, rounds, nkeys, encrypted_len, check1, check2; |
| 3198 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3199 | if (keyp != NULL) |
| 3200 | *keyp = NULL; |
| 3201 | if (commentp != NULL) |
| 3202 | *commentp = NULL; |
| 3203 | |
| 3204 | if ((encoded = sshbuf_new()) == NULL || |
| 3205 | (decoded = sshbuf_new()) == NULL || |
| 3206 | (decrypted = sshbuf_new()) == NULL) { |
| 3207 | r = SSH_ERR_ALLOC_FAIL; |
| 3208 | goto out; |
| 3209 | } |
| 3210 | |
| 3211 | /* check preamble */ |
| 3212 | cp = sshbuf_ptr(blob); |
| 3213 | encoded_len = sshbuf_len(blob); |
| 3214 | if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) || |
| 3215 | memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) { |
| 3216 | r = SSH_ERR_INVALID_FORMAT; |
| 3217 | goto out; |
| 3218 | } |
| 3219 | cp += MARK_BEGIN_LEN; |
| 3220 | encoded_len -= MARK_BEGIN_LEN; |
| 3221 | |
| 3222 | /* Look for end marker, removing whitespace as we go */ |
| 3223 | while (encoded_len > 0) { |
| 3224 | if (*cp != '\n' && *cp != '\r') { |
| 3225 | if ((r = sshbuf_put_u8(encoded, *cp)) != 0) |
| 3226 | goto out; |
| 3227 | } |
| 3228 | last = *cp; |
| 3229 | encoded_len--; |
| 3230 | cp++; |
| 3231 | if (last == '\n') { |
| 3232 | if (encoded_len >= MARK_END_LEN && |
| 3233 | memcmp(cp, MARK_END, MARK_END_LEN) == 0) { |
| 3234 | /* \0 terminate */ |
| 3235 | if ((r = sshbuf_put_u8(encoded, 0)) != 0) |
| 3236 | goto out; |
| 3237 | break; |
| 3238 | } |
| 3239 | } |
| 3240 | } |
| 3241 | if (encoded_len == 0) { |
| 3242 | r = SSH_ERR_INVALID_FORMAT; |
| 3243 | goto out; |
| 3244 | } |
| 3245 | |
| 3246 | /* decode base64 */ |
djm@openbsd.org | 3cc1fbb | 2014-10-08 21:45:48 +0000 | [diff] [blame] | 3247 | if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3248 | goto out; |
| 3249 | |
| 3250 | /* check magic */ |
| 3251 | if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) || |
| 3252 | memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) { |
| 3253 | r = SSH_ERR_INVALID_FORMAT; |
| 3254 | goto out; |
| 3255 | } |
| 3256 | /* parse public portion of key */ |
| 3257 | if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 || |
| 3258 | (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 || |
| 3259 | (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 || |
| 3260 | (r = sshbuf_froms(decoded, &kdf)) != 0 || |
| 3261 | (r = sshbuf_get_u32(decoded, &nkeys)) != 0 || |
| 3262 | (r = sshbuf_skip_string(decoded)) != 0 || /* pubkey */ |
| 3263 | (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0) |
| 3264 | goto out; |
| 3265 | |
| 3266 | if ((cipher = cipher_by_name(ciphername)) == NULL) { |
| 3267 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 3268 | goto out; |
| 3269 | } |
| 3270 | if ((passphrase == NULL || strlen(passphrase) == 0) && |
| 3271 | strcmp(ciphername, "none") != 0) { |
| 3272 | /* passphrase required */ |
| 3273 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 3274 | goto out; |
| 3275 | } |
| 3276 | if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) { |
| 3277 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 3278 | goto out; |
| 3279 | } |
| 3280 | if (!strcmp(kdfname, "none") && strcmp(ciphername, "none") != 0) { |
| 3281 | r = SSH_ERR_INVALID_FORMAT; |
| 3282 | goto out; |
| 3283 | } |
| 3284 | if (nkeys != 1) { |
| 3285 | /* XXX only one key supported */ |
| 3286 | r = SSH_ERR_INVALID_FORMAT; |
| 3287 | goto out; |
| 3288 | } |
| 3289 | |
| 3290 | /* check size of encrypted key blob */ |
| 3291 | blocksize = cipher_blocksize(cipher); |
| 3292 | if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) { |
| 3293 | r = SSH_ERR_INVALID_FORMAT; |
| 3294 | goto out; |
| 3295 | } |
| 3296 | |
| 3297 | /* setup key */ |
| 3298 | keylen = cipher_keylen(cipher); |
| 3299 | ivlen = cipher_ivlen(cipher); |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 3300 | authlen = cipher_authlen(cipher); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3301 | if ((key = calloc(1, keylen + ivlen)) == NULL) { |
| 3302 | r = SSH_ERR_ALLOC_FAIL; |
| 3303 | goto out; |
| 3304 | } |
| 3305 | if (strcmp(kdfname, "bcrypt") == 0) { |
| 3306 | if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 || |
| 3307 | (r = sshbuf_get_u32(kdf, &rounds)) != 0) |
| 3308 | goto out; |
| 3309 | if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen, |
| 3310 | key, keylen + ivlen, rounds) < 0) { |
| 3311 | r = SSH_ERR_INVALID_FORMAT; |
| 3312 | goto out; |
| 3313 | } |
| 3314 | } |
| 3315 | |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 3316 | /* check that an appropriate amount of auth data is present */ |
| 3317 | if (sshbuf_len(decoded) < encrypted_len + authlen) { |
| 3318 | r = SSH_ERR_INVALID_FORMAT; |
| 3319 | goto out; |
| 3320 | } |
| 3321 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3322 | /* decrypt private portion of key */ |
| 3323 | if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 || |
| 3324 | (r = cipher_init(&ciphercontext, cipher, key, keylen, |
| 3325 | key + keylen, ivlen, 0)) != 0) |
| 3326 | goto out; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3327 | if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded), |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 3328 | encrypted_len, 0, authlen)) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3329 | /* an integrity error here indicates an incorrect passphrase */ |
| 3330 | if (r == SSH_ERR_MAC_INVALID) |
| 3331 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 3332 | goto out; |
| 3333 | } |
djm@openbsd.org | 63ebf01 | 2015-05-08 03:17:49 +0000 | [diff] [blame] | 3334 | if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3335 | goto out; |
| 3336 | /* there should be no trailing data */ |
| 3337 | if (sshbuf_len(decoded) != 0) { |
| 3338 | r = SSH_ERR_INVALID_FORMAT; |
| 3339 | goto out; |
| 3340 | } |
| 3341 | |
| 3342 | /* check check bytes */ |
| 3343 | if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 || |
| 3344 | (r = sshbuf_get_u32(decrypted, &check2)) != 0) |
| 3345 | goto out; |
| 3346 | if (check1 != check2) { |
| 3347 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 3348 | goto out; |
| 3349 | } |
| 3350 | |
| 3351 | /* Load the private key and comment */ |
| 3352 | if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 || |
| 3353 | (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0) |
| 3354 | goto out; |
| 3355 | |
| 3356 | /* Check deterministic padding */ |
| 3357 | i = 0; |
| 3358 | while (sshbuf_len(decrypted)) { |
| 3359 | if ((r = sshbuf_get_u8(decrypted, &pad)) != 0) |
| 3360 | goto out; |
| 3361 | if (pad != (++i & 0xff)) { |
| 3362 | r = SSH_ERR_INVALID_FORMAT; |
| 3363 | goto out; |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | /* XXX decode pubkey and check against private */ |
| 3368 | |
| 3369 | /* success */ |
| 3370 | r = 0; |
| 3371 | if (keyp != NULL) { |
| 3372 | *keyp = k; |
| 3373 | k = NULL; |
| 3374 | } |
| 3375 | if (commentp != NULL) { |
| 3376 | *commentp = comment; |
| 3377 | comment = NULL; |
| 3378 | } |
| 3379 | out: |
| 3380 | pad = 0; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3381 | cipher_free(ciphercontext); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3382 | free(ciphername); |
| 3383 | free(kdfname); |
| 3384 | free(comment); |
| 3385 | if (salt != NULL) { |
| 3386 | explicit_bzero(salt, slen); |
| 3387 | free(salt); |
| 3388 | } |
| 3389 | if (key != NULL) { |
| 3390 | explicit_bzero(key, keylen + ivlen); |
| 3391 | free(key); |
| 3392 | } |
| 3393 | sshbuf_free(encoded); |
| 3394 | sshbuf_free(decoded); |
| 3395 | sshbuf_free(kdf); |
| 3396 | sshbuf_free(decrypted); |
| 3397 | sshkey_free(k); |
| 3398 | return r; |
| 3399 | } |
| 3400 | |
| 3401 | #if WITH_SSH1 |
| 3402 | /* |
| 3403 | * Serialises the authentication (private) key to a blob, encrypting it with |
| 3404 | * passphrase. The identification of the blob (lowest 64 bits of n) will |
| 3405 | * precede the key to provide identification of the key without needing a |
| 3406 | * passphrase. |
| 3407 | */ |
| 3408 | static int |
| 3409 | sshkey_private_rsa1_to_blob(struct sshkey *key, struct sshbuf *blob, |
| 3410 | const char *passphrase, const char *comment) |
| 3411 | { |
| 3412 | struct sshbuf *buffer = NULL, *encrypted = NULL; |
| 3413 | u_char buf[8]; |
| 3414 | int r, cipher_num; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3415 | struct sshcipher_ctx *ciphercontext = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3416 | const struct sshcipher *cipher; |
| 3417 | u_char *cp; |
| 3418 | |
| 3419 | /* |
| 3420 | * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting |
| 3421 | * to another cipher; otherwise use SSH_AUTHFILE_CIPHER. |
| 3422 | */ |
| 3423 | cipher_num = (strcmp(passphrase, "") == 0) ? |
| 3424 | SSH_CIPHER_NONE : SSH_CIPHER_3DES; |
| 3425 | if ((cipher = cipher_by_number(cipher_num)) == NULL) |
| 3426 | return SSH_ERR_INTERNAL_ERROR; |
| 3427 | |
| 3428 | /* This buffer is used to build the secret part of the private key. */ |
| 3429 | if ((buffer = sshbuf_new()) == NULL) |
| 3430 | return SSH_ERR_ALLOC_FAIL; |
| 3431 | |
| 3432 | /* Put checkbytes for checking passphrase validity. */ |
| 3433 | if ((r = sshbuf_reserve(buffer, 4, &cp)) != 0) |
| 3434 | goto out; |
| 3435 | arc4random_buf(cp, 2); |
| 3436 | memcpy(cp + 2, cp, 2); |
| 3437 | |
| 3438 | /* |
| 3439 | * Store the private key (n and e will not be stored because they |
| 3440 | * will be stored in plain text, and storing them also in encrypted |
| 3441 | * format would just give known plaintext). |
| 3442 | * Note: q and p are stored in reverse order to SSL. |
| 3443 | */ |
| 3444 | if ((r = sshbuf_put_bignum1(buffer, key->rsa->d)) != 0 || |
| 3445 | (r = sshbuf_put_bignum1(buffer, key->rsa->iqmp)) != 0 || |
| 3446 | (r = sshbuf_put_bignum1(buffer, key->rsa->q)) != 0 || |
| 3447 | (r = sshbuf_put_bignum1(buffer, key->rsa->p)) != 0) |
| 3448 | goto out; |
| 3449 | |
| 3450 | /* Pad the part to be encrypted to a size that is a multiple of 8. */ |
| 3451 | explicit_bzero(buf, 8); |
| 3452 | if ((r = sshbuf_put(buffer, buf, 8 - (sshbuf_len(buffer) % 8))) != 0) |
| 3453 | goto out; |
| 3454 | |
| 3455 | /* This buffer will be used to contain the data in the file. */ |
| 3456 | if ((encrypted = sshbuf_new()) == NULL) { |
| 3457 | r = SSH_ERR_ALLOC_FAIL; |
| 3458 | goto out; |
| 3459 | } |
| 3460 | |
| 3461 | /* First store keyfile id string. */ |
| 3462 | if ((r = sshbuf_put(encrypted, LEGACY_BEGIN, |
| 3463 | sizeof(LEGACY_BEGIN))) != 0) |
| 3464 | goto out; |
| 3465 | |
| 3466 | /* Store cipher type and "reserved" field. */ |
| 3467 | if ((r = sshbuf_put_u8(encrypted, cipher_num)) != 0 || |
| 3468 | (r = sshbuf_put_u32(encrypted, 0)) != 0) |
| 3469 | goto out; |
| 3470 | |
| 3471 | /* Store public key. This will be in plain text. */ |
| 3472 | if ((r = sshbuf_put_u32(encrypted, BN_num_bits(key->rsa->n))) != 0 || |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 3473 | (r = sshbuf_put_bignum1(encrypted, key->rsa->n)) != 0 || |
| 3474 | (r = sshbuf_put_bignum1(encrypted, key->rsa->e)) != 0 || |
| 3475 | (r = sshbuf_put_cstring(encrypted, comment)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3476 | goto out; |
| 3477 | |
| 3478 | /* Allocate space for the private part of the key in the buffer. */ |
| 3479 | if ((r = sshbuf_reserve(encrypted, sshbuf_len(buffer), &cp)) != 0) |
| 3480 | goto out; |
| 3481 | |
| 3482 | if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase, |
| 3483 | CIPHER_ENCRYPT)) != 0) |
| 3484 | goto out; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3485 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3486 | sshbuf_ptr(buffer), sshbuf_len(buffer), 0, 0)) != 0) |
| 3487 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3488 | |
| 3489 | r = sshbuf_putb(blob, encrypted); |
| 3490 | |
| 3491 | out: |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3492 | cipher_free(ciphercontext); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3493 | explicit_bzero(buf, sizeof(buf)); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 3494 | sshbuf_free(buffer); |
| 3495 | sshbuf_free(encrypted); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3496 | |
| 3497 | return r; |
| 3498 | } |
| 3499 | #endif /* WITH_SSH1 */ |
| 3500 | |
| 3501 | #ifdef WITH_OPENSSL |
| 3502 | /* convert SSH v2 key in OpenSSL PEM format */ |
| 3503 | static int |
| 3504 | sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *blob, |
| 3505 | const char *_passphrase, const char *comment) |
| 3506 | { |
| 3507 | int success, r; |
| 3508 | int blen, len = strlen(_passphrase); |
| 3509 | u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL; |
| 3510 | #if (OPENSSL_VERSION_NUMBER < 0x00907000L) |
| 3511 | const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL; |
| 3512 | #else |
| 3513 | const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL; |
| 3514 | #endif |
| 3515 | const u_char *bptr; |
| 3516 | BIO *bio = NULL; |
| 3517 | |
| 3518 | if (len > 0 && len <= 4) |
| 3519 | return SSH_ERR_PASSPHRASE_TOO_SHORT; |
| 3520 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 3521 | return SSH_ERR_ALLOC_FAIL; |
| 3522 | |
| 3523 | switch (key->type) { |
| 3524 | case KEY_DSA: |
| 3525 | success = PEM_write_bio_DSAPrivateKey(bio, key->dsa, |
| 3526 | cipher, passphrase, len, NULL, NULL); |
| 3527 | break; |
| 3528 | #ifdef OPENSSL_HAS_ECC |
| 3529 | case KEY_ECDSA: |
| 3530 | success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa, |
| 3531 | cipher, passphrase, len, NULL, NULL); |
| 3532 | break; |
| 3533 | #endif |
| 3534 | case KEY_RSA: |
| 3535 | success = PEM_write_bio_RSAPrivateKey(bio, key->rsa, |
| 3536 | cipher, passphrase, len, NULL, NULL); |
| 3537 | break; |
| 3538 | default: |
| 3539 | success = 0; |
| 3540 | break; |
| 3541 | } |
| 3542 | if (success == 0) { |
| 3543 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3544 | goto out; |
| 3545 | } |
| 3546 | if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) { |
| 3547 | r = SSH_ERR_INTERNAL_ERROR; |
| 3548 | goto out; |
| 3549 | } |
| 3550 | if ((r = sshbuf_put(blob, bptr, blen)) != 0) |
| 3551 | goto out; |
| 3552 | r = 0; |
| 3553 | out: |
| 3554 | BIO_free(bio); |
| 3555 | return r; |
| 3556 | } |
| 3557 | #endif /* WITH_OPENSSL */ |
| 3558 | |
| 3559 | /* Serialise "key" to buffer "blob" */ |
| 3560 | int |
| 3561 | sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob, |
| 3562 | const char *passphrase, const char *comment, |
| 3563 | int force_new_format, const char *new_format_cipher, int new_format_rounds) |
| 3564 | { |
| 3565 | switch (key->type) { |
markus@openbsd.org | f067cca | 2015-01-12 13:29:27 +0000 | [diff] [blame] | 3566 | #ifdef WITH_SSH1 |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3567 | case KEY_RSA1: |
| 3568 | return sshkey_private_rsa1_to_blob(key, blob, |
| 3569 | passphrase, comment); |
markus@openbsd.org | f067cca | 2015-01-12 13:29:27 +0000 | [diff] [blame] | 3570 | #endif /* WITH_SSH1 */ |
| 3571 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3572 | case KEY_DSA: |
| 3573 | case KEY_ECDSA: |
| 3574 | case KEY_RSA: |
| 3575 | if (force_new_format) { |
| 3576 | return sshkey_private_to_blob2(key, blob, passphrase, |
| 3577 | comment, new_format_cipher, new_format_rounds); |
| 3578 | } |
| 3579 | return sshkey_private_pem_to_blob(key, blob, |
| 3580 | passphrase, comment); |
| 3581 | #endif /* WITH_OPENSSL */ |
| 3582 | case KEY_ED25519: |
| 3583 | return sshkey_private_to_blob2(key, blob, passphrase, |
| 3584 | comment, new_format_cipher, new_format_rounds); |
| 3585 | default: |
| 3586 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | #ifdef WITH_SSH1 |
| 3591 | /* |
| 3592 | * Parse the public, unencrypted portion of a RSA1 key. |
| 3593 | */ |
| 3594 | int |
| 3595 | sshkey_parse_public_rsa1_fileblob(struct sshbuf *blob, |
| 3596 | struct sshkey **keyp, char **commentp) |
| 3597 | { |
| 3598 | int r; |
| 3599 | struct sshkey *pub = NULL; |
| 3600 | struct sshbuf *copy = NULL; |
| 3601 | |
| 3602 | if (keyp != NULL) |
| 3603 | *keyp = NULL; |
| 3604 | if (commentp != NULL) |
| 3605 | *commentp = NULL; |
| 3606 | |
| 3607 | /* Check that it is at least big enough to contain the ID string. */ |
| 3608 | if (sshbuf_len(blob) < sizeof(LEGACY_BEGIN)) |
| 3609 | return SSH_ERR_INVALID_FORMAT; |
| 3610 | |
| 3611 | /* |
| 3612 | * Make sure it begins with the id string. Consume the id string |
| 3613 | * from the buffer. |
| 3614 | */ |
| 3615 | if (memcmp(sshbuf_ptr(blob), LEGACY_BEGIN, sizeof(LEGACY_BEGIN)) != 0) |
| 3616 | return SSH_ERR_INVALID_FORMAT; |
| 3617 | /* Make a working copy of the keyblob and skip past the magic */ |
| 3618 | if ((copy = sshbuf_fromb(blob)) == NULL) |
| 3619 | return SSH_ERR_ALLOC_FAIL; |
| 3620 | if ((r = sshbuf_consume(copy, sizeof(LEGACY_BEGIN))) != 0) |
| 3621 | goto out; |
| 3622 | |
| 3623 | /* Skip cipher type, reserved data and key bits. */ |
| 3624 | if ((r = sshbuf_get_u8(copy, NULL)) != 0 || /* cipher type */ |
| 3625 | (r = sshbuf_get_u32(copy, NULL)) != 0 || /* reserved */ |
| 3626 | (r = sshbuf_get_u32(copy, NULL)) != 0) /* key bits */ |
| 3627 | goto out; |
| 3628 | |
| 3629 | /* Read the public key from the buffer. */ |
| 3630 | if ((pub = sshkey_new(KEY_RSA1)) == NULL || |
| 3631 | (r = sshbuf_get_bignum1(copy, pub->rsa->n)) != 0 || |
| 3632 | (r = sshbuf_get_bignum1(copy, pub->rsa->e)) != 0) |
| 3633 | goto out; |
| 3634 | |
| 3635 | /* Finally, the comment */ |
| 3636 | if ((r = sshbuf_get_string(copy, (u_char**)commentp, NULL)) != 0) |
| 3637 | goto out; |
| 3638 | |
| 3639 | /* The encrypted private part is not parsed by this function. */ |
| 3640 | |
| 3641 | r = 0; |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 3642 | if (keyp != NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3643 | *keyp = pub; |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 3644 | pub = NULL; |
| 3645 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3646 | out: |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 3647 | sshbuf_free(copy); |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 3648 | sshkey_free(pub); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3649 | return r; |
| 3650 | } |
| 3651 | |
| 3652 | static int |
| 3653 | sshkey_parse_private_rsa1(struct sshbuf *blob, const char *passphrase, |
| 3654 | struct sshkey **keyp, char **commentp) |
| 3655 | { |
| 3656 | int r; |
| 3657 | u_int16_t check1, check2; |
| 3658 | u_int8_t cipher_type; |
| 3659 | struct sshbuf *decrypted = NULL, *copy = NULL; |
| 3660 | u_char *cp; |
| 3661 | char *comment = NULL; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3662 | struct sshcipher_ctx *ciphercontext = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3663 | const struct sshcipher *cipher; |
| 3664 | struct sshkey *prv = NULL; |
| 3665 | |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 3666 | if (keyp != NULL) |
| 3667 | *keyp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3668 | if (commentp != NULL) |
| 3669 | *commentp = NULL; |
| 3670 | |
| 3671 | /* Check that it is at least big enough to contain the ID string. */ |
| 3672 | if (sshbuf_len(blob) < sizeof(LEGACY_BEGIN)) |
| 3673 | return SSH_ERR_INVALID_FORMAT; |
| 3674 | |
| 3675 | /* |
| 3676 | * Make sure it begins with the id string. Consume the id string |
| 3677 | * from the buffer. |
| 3678 | */ |
| 3679 | if (memcmp(sshbuf_ptr(blob), LEGACY_BEGIN, sizeof(LEGACY_BEGIN)) != 0) |
| 3680 | return SSH_ERR_INVALID_FORMAT; |
| 3681 | |
| 3682 | if ((prv = sshkey_new_private(KEY_RSA1)) == NULL) { |
| 3683 | r = SSH_ERR_ALLOC_FAIL; |
| 3684 | goto out; |
| 3685 | } |
| 3686 | if ((copy = sshbuf_fromb(blob)) == NULL || |
| 3687 | (decrypted = sshbuf_new()) == NULL) { |
| 3688 | r = SSH_ERR_ALLOC_FAIL; |
| 3689 | goto out; |
| 3690 | } |
| 3691 | if ((r = sshbuf_consume(copy, sizeof(LEGACY_BEGIN))) != 0) |
| 3692 | goto out; |
| 3693 | |
| 3694 | /* Read cipher type. */ |
| 3695 | if ((r = sshbuf_get_u8(copy, &cipher_type)) != 0 || |
| 3696 | (r = sshbuf_get_u32(copy, NULL)) != 0) /* reserved */ |
| 3697 | goto out; |
| 3698 | |
| 3699 | /* Read the public key and comment from the buffer. */ |
| 3700 | if ((r = sshbuf_get_u32(copy, NULL)) != 0 || /* key bits */ |
| 3701 | (r = sshbuf_get_bignum1(copy, prv->rsa->n)) != 0 || |
| 3702 | (r = sshbuf_get_bignum1(copy, prv->rsa->e)) != 0 || |
| 3703 | (r = sshbuf_get_cstring(copy, &comment, NULL)) != 0) |
| 3704 | goto out; |
| 3705 | |
| 3706 | /* Check that it is a supported cipher. */ |
| 3707 | cipher = cipher_by_number(cipher_type); |
| 3708 | if (cipher == NULL) { |
| 3709 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
| 3710 | goto out; |
| 3711 | } |
| 3712 | /* Initialize space for decrypted data. */ |
| 3713 | if ((r = sshbuf_reserve(decrypted, sshbuf_len(copy), &cp)) != 0) |
| 3714 | goto out; |
| 3715 | |
| 3716 | /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ |
| 3717 | if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase, |
| 3718 | CIPHER_DECRYPT)) != 0) |
| 3719 | goto out; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3720 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
| 3721 | sshbuf_ptr(copy), sshbuf_len(copy), 0, 0)) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3722 | goto out; |
| 3723 | |
| 3724 | if ((r = sshbuf_get_u16(decrypted, &check1)) != 0 || |
| 3725 | (r = sshbuf_get_u16(decrypted, &check2)) != 0) |
| 3726 | goto out; |
| 3727 | if (check1 != check2) { |
| 3728 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
| 3729 | goto out; |
| 3730 | } |
| 3731 | |
| 3732 | /* Read the rest of the private key. */ |
| 3733 | if ((r = sshbuf_get_bignum1(decrypted, prv->rsa->d)) != 0 || |
| 3734 | (r = sshbuf_get_bignum1(decrypted, prv->rsa->iqmp)) != 0 || |
| 3735 | (r = sshbuf_get_bignum1(decrypted, prv->rsa->q)) != 0 || |
| 3736 | (r = sshbuf_get_bignum1(decrypted, prv->rsa->p)) != 0) |
| 3737 | goto out; |
| 3738 | |
| 3739 | /* calculate p-1 and q-1 */ |
| 3740 | if ((r = rsa_generate_additional_parameters(prv->rsa)) != 0) |
| 3741 | goto out; |
| 3742 | |
| 3743 | /* enable blinding */ |
| 3744 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { |
| 3745 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3746 | goto out; |
| 3747 | } |
| 3748 | r = 0; |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 3749 | if (keyp != NULL) { |
| 3750 | *keyp = prv; |
| 3751 | prv = NULL; |
| 3752 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3753 | if (commentp != NULL) { |
| 3754 | *commentp = comment; |
| 3755 | comment = NULL; |
| 3756 | } |
| 3757 | out: |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 3758 | cipher_free(ciphercontext); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 3759 | free(comment); |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 3760 | sshkey_free(prv); |
mmcc@openbsd.org | 52d7078 | 2015-12-11 04:21:11 +0000 | [diff] [blame] | 3761 | sshbuf_free(copy); |
| 3762 | sshbuf_free(decrypted); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3763 | return r; |
| 3764 | } |
| 3765 | #endif /* WITH_SSH1 */ |
| 3766 | |
| 3767 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 3768 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3769 | sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type, |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 3770 | const char *passphrase, struct sshkey **keyp) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3771 | { |
| 3772 | EVP_PKEY *pk = NULL; |
| 3773 | struct sshkey *prv = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3774 | BIO *bio = NULL; |
| 3775 | int r; |
| 3776 | |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 3777 | if (keyp != NULL) |
| 3778 | *keyp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3779 | |
| 3780 | if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX) |
| 3781 | return SSH_ERR_ALLOC_FAIL; |
| 3782 | if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) != |
| 3783 | (int)sshbuf_len(blob)) { |
| 3784 | r = SSH_ERR_ALLOC_FAIL; |
| 3785 | goto out; |
| 3786 | } |
| 3787 | |
| 3788 | if ((pk = PEM_read_bio_PrivateKey(bio, NULL, NULL, |
| 3789 | (char *)passphrase)) == NULL) { |
djm@openbsd.org | 3147e75 | 2016-06-19 07:48:02 +0000 | [diff] [blame] | 3790 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3791 | goto out; |
| 3792 | } |
| 3793 | if (pk->type == EVP_PKEY_RSA && |
| 3794 | (type == KEY_UNSPEC || type == KEY_RSA)) { |
| 3795 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 3796 | r = SSH_ERR_ALLOC_FAIL; |
| 3797 | goto out; |
| 3798 | } |
| 3799 | prv->rsa = EVP_PKEY_get1_RSA(pk); |
| 3800 | prv->type = KEY_RSA; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3801 | #ifdef DEBUG_PK |
| 3802 | RSA_print_fp(stderr, prv->rsa, 8); |
| 3803 | #endif |
| 3804 | if (RSA_blinding_on(prv->rsa, NULL) != 1) { |
| 3805 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 3806 | goto out; |
| 3807 | } |
| 3808 | } else if (pk->type == EVP_PKEY_DSA && |
| 3809 | (type == KEY_UNSPEC || type == KEY_DSA)) { |
| 3810 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 3811 | r = SSH_ERR_ALLOC_FAIL; |
| 3812 | goto out; |
| 3813 | } |
| 3814 | prv->dsa = EVP_PKEY_get1_DSA(pk); |
| 3815 | prv->type = KEY_DSA; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3816 | #ifdef DEBUG_PK |
| 3817 | DSA_print_fp(stderr, prv->dsa, 8); |
| 3818 | #endif |
| 3819 | #ifdef OPENSSL_HAS_ECC |
| 3820 | } else if (pk->type == EVP_PKEY_EC && |
| 3821 | (type == KEY_UNSPEC || type == KEY_ECDSA)) { |
| 3822 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 3823 | r = SSH_ERR_ALLOC_FAIL; |
| 3824 | goto out; |
| 3825 | } |
| 3826 | prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk); |
| 3827 | prv->type = KEY_ECDSA; |
| 3828 | prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa); |
| 3829 | if (prv->ecdsa_nid == -1 || |
| 3830 | sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL || |
| 3831 | sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa), |
| 3832 | EC_KEY_get0_public_key(prv->ecdsa)) != 0 || |
| 3833 | sshkey_ec_validate_private(prv->ecdsa) != 0) { |
| 3834 | r = SSH_ERR_INVALID_FORMAT; |
| 3835 | goto out; |
| 3836 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3837 | # ifdef DEBUG_PK |
| 3838 | if (prv != NULL && prv->ecdsa != NULL) |
| 3839 | sshkey_dump_ec_key(prv->ecdsa); |
| 3840 | # endif |
| 3841 | #endif /* OPENSSL_HAS_ECC */ |
| 3842 | } else { |
| 3843 | r = SSH_ERR_INVALID_FORMAT; |
| 3844 | goto out; |
| 3845 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3846 | r = 0; |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 3847 | if (keyp != NULL) { |
| 3848 | *keyp = prv; |
| 3849 | prv = NULL; |
| 3850 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3851 | out: |
| 3852 | BIO_free(bio); |
| 3853 | if (pk != NULL) |
| 3854 | EVP_PKEY_free(pk); |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 3855 | sshkey_free(prv); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3856 | return r; |
| 3857 | } |
| 3858 | #endif /* WITH_OPENSSL */ |
| 3859 | |
| 3860 | int |
| 3861 | sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type, |
| 3862 | const char *passphrase, struct sshkey **keyp, char **commentp) |
| 3863 | { |
djm@openbsd.org | dce19bf | 2016-04-09 12:39:30 +0000 | [diff] [blame] | 3864 | if (keyp != NULL) |
| 3865 | *keyp = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3866 | if (commentp != NULL) |
| 3867 | *commentp = NULL; |
| 3868 | |
| 3869 | switch (type) { |
markus@openbsd.org | f067cca | 2015-01-12 13:29:27 +0000 | [diff] [blame] | 3870 | #ifdef WITH_SSH1 |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3871 | case KEY_RSA1: |
| 3872 | return sshkey_parse_private_rsa1(blob, passphrase, |
| 3873 | keyp, commentp); |
markus@openbsd.org | f067cca | 2015-01-12 13:29:27 +0000 | [diff] [blame] | 3874 | #endif /* WITH_SSH1 */ |
| 3875 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3876 | case KEY_DSA: |
| 3877 | case KEY_ECDSA: |
| 3878 | case KEY_RSA: |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 3879 | return sshkey_parse_private_pem_fileblob(blob, type, |
| 3880 | passphrase, keyp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3881 | #endif /* WITH_OPENSSL */ |
| 3882 | case KEY_ED25519: |
| 3883 | return sshkey_parse_private2(blob, type, passphrase, |
| 3884 | keyp, commentp); |
| 3885 | case KEY_UNSPEC: |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 3886 | if (sshkey_parse_private2(blob, type, passphrase, keyp, |
| 3887 | commentp) == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3888 | return 0; |
| 3889 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 3890 | return sshkey_parse_private_pem_fileblob(blob, type, |
| 3891 | passphrase, keyp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3892 | #else |
| 3893 | return SSH_ERR_INVALID_FORMAT; |
| 3894 | #endif /* WITH_OPENSSL */ |
| 3895 | default: |
| 3896 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
| 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | int |
| 3901 | sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase, |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 3902 | struct sshkey **keyp, char **commentp) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3903 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3904 | if (keyp != NULL) |
| 3905 | *keyp = NULL; |
| 3906 | if (commentp != NULL) |
| 3907 | *commentp = NULL; |
| 3908 | |
| 3909 | #ifdef WITH_SSH1 |
| 3910 | /* it's a SSH v1 key if the public key part is readable */ |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 3911 | if (sshkey_parse_public_rsa1_fileblob(buffer, NULL, NULL) == 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3912 | return sshkey_parse_private_fileblob_type(buffer, KEY_RSA1, |
| 3913 | passphrase, keyp, commentp); |
| 3914 | } |
| 3915 | #endif /* WITH_SSH1 */ |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 3916 | return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC, |
| 3917 | passphrase, keyp, commentp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 3918 | } |