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