diff options
Diffstat (limited to 'src/lib/libcrypto/pem/pvkfmt.c')
| -rw-r--r-- | src/lib/libcrypto/pem/pvkfmt.c | 931 |
1 files changed, 0 insertions, 931 deletions
diff --git a/src/lib/libcrypto/pem/pvkfmt.c b/src/lib/libcrypto/pem/pvkfmt.c deleted file mode 100644 index 025381bcc0..0000000000 --- a/src/lib/libcrypto/pem/pvkfmt.c +++ /dev/null | |||
| @@ -1,931 +0,0 @@ | |||
| 1 | /* $OpenBSD: pvkfmt.c,v 1.13 2015/05/15 11:00:14 jsg Exp $ */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | ||
| 3 | * project 2005. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2005 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* Support for PVK format keys and related structures (such a PUBLICKEYBLOB | ||
| 60 | * and PRIVATEKEYBLOB). | ||
| 61 | */ | ||
| 62 | |||
| 63 | #include <stdlib.h> | ||
| 64 | #include <string.h> | ||
| 65 | |||
| 66 | #include <openssl/opensslconf.h> | ||
| 67 | |||
| 68 | #include <openssl/bn.h> | ||
| 69 | #include <openssl/err.h> | ||
| 70 | #include <openssl/pem.h> | ||
| 71 | |||
| 72 | #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) | ||
| 73 | #include <openssl/dsa.h> | ||
| 74 | #include <openssl/rsa.h> | ||
| 75 | |||
| 76 | /* Utility function: read a DWORD (4 byte unsigned integer) in little endian | ||
| 77 | * format | ||
| 78 | */ | ||
| 79 | |||
| 80 | static unsigned int | ||
| 81 | read_ledword(const unsigned char **in) | ||
| 82 | { | ||
| 83 | const unsigned char *p = *in; | ||
| 84 | unsigned int ret; | ||
| 85 | |||
| 86 | ret = *p++; | ||
| 87 | ret |= (*p++ << 8); | ||
| 88 | ret |= (*p++ << 16); | ||
| 89 | ret |= (*p++ << 24); | ||
| 90 | *in = p; | ||
| 91 | return ret; | ||
| 92 | } | ||
| 93 | |||
| 94 | /* Read a BIGNUM in little endian format. The docs say that this should take up | ||
| 95 | * bitlen/8 bytes. | ||
| 96 | */ | ||
| 97 | |||
| 98 | static int | ||
| 99 | read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r) | ||
| 100 | { | ||
| 101 | const unsigned char *p; | ||
| 102 | unsigned char *tmpbuf, *q; | ||
| 103 | unsigned int i; | ||
| 104 | |||
| 105 | p = *in + nbyte - 1; | ||
| 106 | tmpbuf = malloc(nbyte); | ||
| 107 | if (!tmpbuf) | ||
| 108 | return 0; | ||
| 109 | q = tmpbuf; | ||
| 110 | for (i = 0; i < nbyte; i++) | ||
| 111 | *q++ = *p--; | ||
| 112 | *r = BN_bin2bn(tmpbuf, nbyte, NULL); | ||
| 113 | free(tmpbuf); | ||
| 114 | if (*r) { | ||
| 115 | *in += nbyte; | ||
| 116 | return 1; | ||
| 117 | } else | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */ | ||
| 123 | |||
| 124 | #define MS_PUBLICKEYBLOB 0x6 | ||
| 125 | #define MS_PRIVATEKEYBLOB 0x7 | ||
| 126 | #define MS_RSA1MAGIC 0x31415352L | ||
| 127 | #define MS_RSA2MAGIC 0x32415352L | ||
| 128 | #define MS_DSS1MAGIC 0x31535344L | ||
| 129 | #define MS_DSS2MAGIC 0x32535344L | ||
| 130 | |||
| 131 | #define MS_KEYALG_RSA_KEYX 0xa400 | ||
| 132 | #define MS_KEYALG_DSS_SIGN 0x2200 | ||
| 133 | |||
| 134 | #define MS_KEYTYPE_KEYX 0x1 | ||
| 135 | #define MS_KEYTYPE_SIGN 0x2 | ||
| 136 | |||
| 137 | /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */ | ||
| 138 | #define MS_PVKMAGIC 0xb0b5f11eL | ||
| 139 | /* Salt length for PVK files */ | ||
| 140 | #define PVK_SALTLEN 0x10 | ||
| 141 | |||
| 142 | static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length, | ||
| 143 | unsigned int bitlen, int ispub); | ||
| 144 | static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length, | ||
| 145 | unsigned int bitlen, int ispub); | ||
| 146 | |||
| 147 | static int | ||
| 148 | do_blob_header(const unsigned char **in, unsigned int length, | ||
| 149 | unsigned int *pmagic, unsigned int *pbitlen, int *pisdss, int *pispub) | ||
| 150 | { | ||
| 151 | const unsigned char *p = *in; | ||
| 152 | |||
| 153 | if (length < 16) | ||
| 154 | return 0; | ||
| 155 | /* bType */ | ||
| 156 | if (*p == MS_PUBLICKEYBLOB) { | ||
| 157 | if (*pispub == 0) { | ||
| 158 | PEMerr(PEM_F_DO_BLOB_HEADER, | ||
| 159 | PEM_R_EXPECTING_PRIVATE_KEY_BLOB); | ||
| 160 | return 0; | ||
| 161 | } | ||
| 162 | *pispub = 1; | ||
| 163 | } else if (*p == MS_PRIVATEKEYBLOB) { | ||
| 164 | if (*pispub == 1) { | ||
| 165 | PEMerr(PEM_F_DO_BLOB_HEADER, | ||
| 166 | PEM_R_EXPECTING_PUBLIC_KEY_BLOB); | ||
| 167 | return 0; | ||
| 168 | } | ||
| 169 | *pispub = 0; | ||
| 170 | } else | ||
| 171 | return 0; | ||
| 172 | p++; | ||
| 173 | /* Version */ | ||
| 174 | if (*p++ != 0x2) { | ||
| 175 | PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER); | ||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | /* Ignore reserved, aiKeyAlg */ | ||
| 179 | p += 6; | ||
| 180 | *pmagic = read_ledword(&p); | ||
| 181 | *pbitlen = read_ledword(&p); | ||
| 182 | *pisdss = 0; | ||
| 183 | switch (*pmagic) { | ||
| 184 | |||
| 185 | case MS_DSS1MAGIC: | ||
| 186 | *pisdss = 1; | ||
| 187 | case MS_RSA1MAGIC: | ||
| 188 | if (*pispub == 0) { | ||
| 189 | PEMerr(PEM_F_DO_BLOB_HEADER, | ||
| 190 | PEM_R_EXPECTING_PRIVATE_KEY_BLOB); | ||
| 191 | return 0; | ||
| 192 | } | ||
| 193 | break; | ||
| 194 | |||
| 195 | case MS_DSS2MAGIC: | ||
| 196 | *pisdss = 1; | ||
| 197 | case MS_RSA2MAGIC: | ||
| 198 | if (*pispub == 1) { | ||
| 199 | PEMerr(PEM_F_DO_BLOB_HEADER, | ||
| 200 | PEM_R_EXPECTING_PUBLIC_KEY_BLOB); | ||
| 201 | return 0; | ||
| 202 | } | ||
| 203 | break; | ||
| 204 | |||
| 205 | default: | ||
| 206 | PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER); | ||
| 207 | return -1; | ||
| 208 | } | ||
| 209 | *in = p; | ||
| 210 | return 1; | ||
| 211 | } | ||
| 212 | |||
| 213 | static unsigned int | ||
| 214 | blob_length(unsigned bitlen, int isdss, int ispub) | ||
| 215 | { | ||
| 216 | unsigned int nbyte, hnbyte; | ||
| 217 | |||
| 218 | nbyte = (bitlen + 7) >> 3; | ||
| 219 | hnbyte = (bitlen + 15) >> 4; | ||
| 220 | if (isdss) { | ||
| 221 | |||
| 222 | /* Expected length: 20 for q + 3 components bitlen each + 24 | ||
| 223 | * for seed structure. | ||
| 224 | */ | ||
| 225 | if (ispub) | ||
| 226 | return 44 + 3 * nbyte; | ||
| 227 | /* Expected length: 20 for q, priv, 2 bitlen components + 24 | ||
| 228 | * for seed structure. | ||
| 229 | */ | ||
| 230 | else | ||
| 231 | return 64 + 2 * nbyte; | ||
| 232 | } else { | ||
| 233 | /* Expected length: 4 for 'e' + 'n' */ | ||
| 234 | if (ispub) | ||
| 235 | return 4 + nbyte; | ||
| 236 | else | ||
| 237 | /* Expected length: 4 for 'e' and 7 other components. | ||
| 238 | * 2 components are bitlen size, 5 are bitlen/2 | ||
| 239 | */ | ||
| 240 | return 4 + 2*nbyte + 5*hnbyte; | ||
| 241 | } | ||
| 242 | |||
| 243 | } | ||
| 244 | |||
| 245 | static EVP_PKEY * | ||
| 246 | do_b2i(const unsigned char **in, unsigned int length, int ispub) | ||
| 247 | { | ||
| 248 | const unsigned char *p = *in; | ||
| 249 | unsigned int bitlen, magic; | ||
| 250 | int isdss; | ||
| 251 | |||
| 252 | if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) { | ||
| 253 | PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR); | ||
| 254 | return NULL; | ||
| 255 | } | ||
| 256 | length -= 16; | ||
| 257 | if (length < blob_length(bitlen, isdss, ispub)) { | ||
| 258 | PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT); | ||
| 259 | return NULL; | ||
| 260 | } | ||
| 261 | if (isdss) | ||
| 262 | return b2i_dss(&p, length, bitlen, ispub); | ||
| 263 | else | ||
| 264 | return b2i_rsa(&p, length, bitlen, ispub); | ||
| 265 | } | ||
| 266 | |||
| 267 | static EVP_PKEY * | ||
| 268 | do_b2i_bio(BIO *in, int ispub) | ||
| 269 | { | ||
| 270 | const unsigned char *p; | ||
| 271 | unsigned char hdr_buf[16], *buf = NULL; | ||
| 272 | unsigned int bitlen, magic, length; | ||
| 273 | int isdss; | ||
| 274 | EVP_PKEY *ret = NULL; | ||
| 275 | |||
| 276 | if (BIO_read(in, hdr_buf, 16) != 16) { | ||
| 277 | PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); | ||
| 278 | return NULL; | ||
| 279 | } | ||
| 280 | p = hdr_buf; | ||
| 281 | if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0) | ||
| 282 | return NULL; | ||
| 283 | |||
| 284 | length = blob_length(bitlen, isdss, ispub); | ||
| 285 | buf = malloc(length); | ||
| 286 | if (!buf) { | ||
| 287 | PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE); | ||
| 288 | goto err; | ||
| 289 | } | ||
| 290 | p = buf; | ||
| 291 | if (BIO_read(in, buf, length) != (int)length) { | ||
| 292 | PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); | ||
| 293 | goto err; | ||
| 294 | } | ||
| 295 | |||
| 296 | if (isdss) | ||
| 297 | ret = b2i_dss(&p, length, bitlen, ispub); | ||
| 298 | else | ||
| 299 | ret = b2i_rsa(&p, length, bitlen, ispub); | ||
| 300 | |||
| 301 | err: | ||
| 302 | free(buf); | ||
| 303 | return ret; | ||
| 304 | } | ||
| 305 | |||
| 306 | static EVP_PKEY * | ||
| 307 | b2i_dss(const unsigned char **in, unsigned int length, unsigned int bitlen, | ||
| 308 | int ispub) | ||
| 309 | { | ||
| 310 | const unsigned char *p = *in; | ||
| 311 | EVP_PKEY *ret = NULL; | ||
| 312 | DSA *dsa = NULL; | ||
| 313 | BN_CTX *ctx = NULL; | ||
| 314 | unsigned int nbyte; | ||
| 315 | |||
| 316 | nbyte = (bitlen + 7) >> 3; | ||
| 317 | |||
| 318 | dsa = DSA_new(); | ||
| 319 | ret = EVP_PKEY_new(); | ||
| 320 | if (!dsa || !ret) | ||
| 321 | goto memerr; | ||
| 322 | if (!read_lebn(&p, nbyte, &dsa->p)) | ||
| 323 | goto memerr; | ||
| 324 | if (!read_lebn(&p, 20, &dsa->q)) | ||
| 325 | goto memerr; | ||
| 326 | if (!read_lebn(&p, nbyte, &dsa->g)) | ||
| 327 | goto memerr; | ||
| 328 | if (ispub) { | ||
| 329 | if (!read_lebn(&p, nbyte, &dsa->pub_key)) | ||
| 330 | goto memerr; | ||
| 331 | } else { | ||
| 332 | if (!read_lebn(&p, 20, &dsa->priv_key)) | ||
| 333 | goto memerr; | ||
| 334 | /* Calculate public key */ | ||
| 335 | if (!(dsa->pub_key = BN_new())) | ||
| 336 | goto memerr; | ||
| 337 | if (!(ctx = BN_CTX_new())) | ||
| 338 | goto memerr; | ||
| 339 | if (!BN_mod_exp(dsa->pub_key, dsa->g, | ||
| 340 | dsa->priv_key, dsa->p, ctx)) | ||
| 341 | goto memerr; | ||
| 342 | BN_CTX_free(ctx); | ||
| 343 | } | ||
| 344 | |||
| 345 | EVP_PKEY_set1_DSA(ret, dsa); | ||
| 346 | DSA_free(dsa); | ||
| 347 | *in = p; | ||
| 348 | return ret; | ||
| 349 | |||
| 350 | memerr: | ||
| 351 | PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE); | ||
| 352 | DSA_free(dsa); | ||
| 353 | EVP_PKEY_free(ret); | ||
| 354 | BN_CTX_free(ctx); | ||
| 355 | return NULL; | ||
| 356 | } | ||
| 357 | |||
| 358 | static EVP_PKEY * | ||
| 359 | b2i_rsa(const unsigned char **in, unsigned int length, unsigned int bitlen, | ||
| 360 | int ispub) | ||
| 361 | { | ||
| 362 | const unsigned char *p = *in; | ||
| 363 | EVP_PKEY *ret = NULL; | ||
| 364 | RSA *rsa = NULL; | ||
| 365 | unsigned int nbyte, hnbyte; | ||
| 366 | |||
| 367 | nbyte = (bitlen + 7) >> 3; | ||
| 368 | hnbyte = (bitlen + 15) >> 4; | ||
| 369 | rsa = RSA_new(); | ||
| 370 | ret = EVP_PKEY_new(); | ||
| 371 | if (!rsa || !ret) | ||
| 372 | goto memerr; | ||
| 373 | rsa->e = BN_new(); | ||
| 374 | if (!rsa->e) | ||
| 375 | goto memerr; | ||
| 376 | if (!BN_set_word(rsa->e, read_ledword(&p))) | ||
| 377 | goto memerr; | ||
| 378 | if (!read_lebn(&p, nbyte, &rsa->n)) | ||
| 379 | goto memerr; | ||
| 380 | if (!ispub) { | ||
| 381 | if (!read_lebn(&p, hnbyte, &rsa->p)) | ||
| 382 | goto memerr; | ||
| 383 | if (!read_lebn(&p, hnbyte, &rsa->q)) | ||
| 384 | goto memerr; | ||
| 385 | if (!read_lebn(&p, hnbyte, &rsa->dmp1)) | ||
| 386 | goto memerr; | ||
| 387 | if (!read_lebn(&p, hnbyte, &rsa->dmq1)) | ||
| 388 | goto memerr; | ||
| 389 | if (!read_lebn(&p, hnbyte, &rsa->iqmp)) | ||
| 390 | goto memerr; | ||
| 391 | if (!read_lebn(&p, nbyte, &rsa->d)) | ||
| 392 | goto memerr; | ||
| 393 | } | ||
| 394 | |||
| 395 | EVP_PKEY_set1_RSA(ret, rsa); | ||
| 396 | RSA_free(rsa); | ||
| 397 | *in = p; | ||
| 398 | return ret; | ||
| 399 | |||
| 400 | memerr: | ||
| 401 | PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE); | ||
| 402 | RSA_free(rsa); | ||
| 403 | EVP_PKEY_free(ret); | ||
| 404 | return NULL; | ||
| 405 | } | ||
| 406 | |||
| 407 | EVP_PKEY * | ||
| 408 | b2i_PrivateKey(const unsigned char **in, long length) | ||
| 409 | { | ||
| 410 | return do_b2i(in, length, 0); | ||
| 411 | } | ||
| 412 | |||
| 413 | EVP_PKEY * | ||
| 414 | b2i_PublicKey(const unsigned char **in, long length) | ||
| 415 | { | ||
| 416 | return do_b2i(in, length, 1); | ||
| 417 | } | ||
| 418 | |||
| 419 | EVP_PKEY * | ||
| 420 | b2i_PrivateKey_bio(BIO *in) | ||
| 421 | { | ||
| 422 | return do_b2i_bio(in, 0); | ||
| 423 | } | ||
| 424 | |||
| 425 | EVP_PKEY * | ||
| 426 | b2i_PublicKey_bio(BIO *in) | ||
| 427 | { | ||
| 428 | return do_b2i_bio(in, 1); | ||
| 429 | } | ||
| 430 | |||
| 431 | static void | ||
| 432 | write_ledword(unsigned char **out, unsigned int dw) | ||
| 433 | { | ||
| 434 | unsigned char *p = *out; | ||
| 435 | |||
| 436 | *p++ = dw & 0xff; | ||
| 437 | *p++ = (dw >> 8) & 0xff; | ||
| 438 | *p++ = (dw >> 16) & 0xff; | ||
| 439 | *p++ = (dw >> 24) & 0xff; | ||
| 440 | *out = p; | ||
| 441 | } | ||
| 442 | |||
| 443 | static void | ||
| 444 | write_lebn(unsigned char **out, const BIGNUM *bn, int len) | ||
| 445 | { | ||
| 446 | int nb, i; | ||
| 447 | unsigned char *p = *out, *q, c; | ||
| 448 | |||
| 449 | nb = BN_num_bytes(bn); | ||
| 450 | BN_bn2bin(bn, p); | ||
| 451 | q = p + nb - 1; | ||
| 452 | /* In place byte order reversal */ | ||
| 453 | for (i = 0; i < nb / 2; i++) { | ||
| 454 | c = *p; | ||
| 455 | *p++ = *q; | ||
| 456 | *q-- = c; | ||
| 457 | } | ||
| 458 | *out += nb; | ||
| 459 | /* Pad with zeroes if we have to */ | ||
| 460 | if (len > 0) { | ||
| 461 | len -= nb; | ||
| 462 | if (len > 0) { | ||
| 463 | memset(*out, 0, len); | ||
| 464 | *out += len; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | |||
| 470 | static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic); | ||
| 471 | static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic); | ||
| 472 | |||
| 473 | static void write_rsa(unsigned char **out, RSA *rsa, int ispub); | ||
| 474 | static void write_dsa(unsigned char **out, DSA *dsa, int ispub); | ||
| 475 | |||
| 476 | static int | ||
| 477 | do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub) | ||
| 478 | { | ||
| 479 | unsigned char *p; | ||
| 480 | unsigned int bitlen, magic = 0, keyalg; | ||
| 481 | int outlen, noinc = 0; | ||
| 482 | |||
| 483 | if (pk->type == EVP_PKEY_DSA) { | ||
| 484 | bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic); | ||
| 485 | keyalg = MS_KEYALG_DSS_SIGN; | ||
| 486 | } else if (pk->type == EVP_PKEY_RSA) { | ||
| 487 | bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic); | ||
| 488 | keyalg = MS_KEYALG_RSA_KEYX; | ||
| 489 | } else | ||
| 490 | return -1; | ||
| 491 | if (bitlen == 0) | ||
| 492 | return -1; | ||
| 493 | outlen = 16 + blob_length(bitlen, | ||
| 494 | keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub); | ||
| 495 | if (out == NULL) | ||
| 496 | return outlen; | ||
| 497 | if (*out) | ||
| 498 | p = *out; | ||
| 499 | else { | ||
| 500 | p = malloc(outlen); | ||
| 501 | if (!p) | ||
| 502 | return -1; | ||
| 503 | *out = p; | ||
| 504 | noinc = 1; | ||
| 505 | } | ||
| 506 | if (ispub) | ||
| 507 | *p++ = MS_PUBLICKEYBLOB; | ||
| 508 | else | ||
| 509 | *p++ = MS_PRIVATEKEYBLOB; | ||
| 510 | *p++ = 0x2; | ||
| 511 | *p++ = 0; | ||
| 512 | *p++ = 0; | ||
| 513 | write_ledword(&p, keyalg); | ||
| 514 | write_ledword(&p, magic); | ||
| 515 | write_ledword(&p, bitlen); | ||
| 516 | if (keyalg == MS_KEYALG_DSS_SIGN) | ||
| 517 | write_dsa(&p, pk->pkey.dsa, ispub); | ||
| 518 | else | ||
| 519 | write_rsa(&p, pk->pkey.rsa, ispub); | ||
| 520 | if (!noinc) | ||
| 521 | *out += outlen; | ||
| 522 | return outlen; | ||
| 523 | } | ||
| 524 | |||
| 525 | static int | ||
| 526 | do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub) | ||
| 527 | { | ||
| 528 | unsigned char *tmp = NULL; | ||
| 529 | int outlen, wrlen; | ||
| 530 | |||
| 531 | outlen = do_i2b(&tmp, pk, ispub); | ||
| 532 | if (outlen < 0) | ||
| 533 | return -1; | ||
| 534 | wrlen = BIO_write(out, tmp, outlen); | ||
| 535 | free(tmp); | ||
| 536 | if (wrlen == outlen) | ||
| 537 | return outlen; | ||
| 538 | return -1; | ||
| 539 | } | ||
| 540 | |||
| 541 | static int | ||
| 542 | check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic) | ||
| 543 | { | ||
| 544 | int bitlen; | ||
| 545 | |||
| 546 | bitlen = BN_num_bits(dsa->p); | ||
| 547 | if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160) || | ||
| 548 | (BN_num_bits(dsa->g) > bitlen)) | ||
| 549 | goto badkey; | ||
| 550 | if (ispub) { | ||
| 551 | if (BN_num_bits(dsa->pub_key) > bitlen) | ||
| 552 | goto badkey; | ||
| 553 | *pmagic = MS_DSS1MAGIC; | ||
| 554 | } else { | ||
| 555 | if (BN_num_bits(dsa->priv_key) > 160) | ||
| 556 | goto badkey; | ||
| 557 | *pmagic = MS_DSS2MAGIC; | ||
| 558 | } | ||
| 559 | |||
| 560 | return bitlen; | ||
| 561 | |||
| 562 | badkey: | ||
| 563 | PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | ||
| 564 | return 0; | ||
| 565 | } | ||
| 566 | |||
| 567 | static int | ||
| 568 | check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic) | ||
| 569 | { | ||
| 570 | int nbyte, hnbyte, bitlen; | ||
| 571 | |||
| 572 | if (BN_num_bits(rsa->e) > 32) | ||
| 573 | goto badkey; | ||
| 574 | bitlen = BN_num_bits(rsa->n); | ||
| 575 | nbyte = BN_num_bytes(rsa->n); | ||
| 576 | hnbyte = (BN_num_bits(rsa->n) + 15) >> 4; | ||
| 577 | if (ispub) { | ||
| 578 | *pmagic = MS_RSA1MAGIC; | ||
| 579 | return bitlen; | ||
| 580 | } else { | ||
| 581 | *pmagic = MS_RSA2MAGIC; | ||
| 582 | /* For private key each component must fit within nbyte or | ||
| 583 | * hnbyte. | ||
| 584 | */ | ||
| 585 | if (BN_num_bytes(rsa->d) > nbyte) | ||
| 586 | goto badkey; | ||
| 587 | if ((BN_num_bytes(rsa->iqmp) > hnbyte) || | ||
| 588 | (BN_num_bytes(rsa->p) > hnbyte) || | ||
| 589 | (BN_num_bytes(rsa->q) > hnbyte) || | ||
| 590 | (BN_num_bytes(rsa->dmp1) > hnbyte) || | ||
| 591 | (BN_num_bytes(rsa->dmq1) > hnbyte)) | ||
| 592 | goto badkey; | ||
| 593 | } | ||
| 594 | return bitlen; | ||
| 595 | |||
| 596 | badkey: | ||
| 597 | PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | ||
| 598 | return 0; | ||
| 599 | } | ||
| 600 | |||
| 601 | static void | ||
| 602 | write_rsa(unsigned char **out, RSA *rsa, int ispub) | ||
| 603 | { | ||
| 604 | int nbyte, hnbyte; | ||
| 605 | |||
| 606 | nbyte = BN_num_bytes(rsa->n); | ||
| 607 | hnbyte = (BN_num_bits(rsa->n) + 15) >> 4; | ||
| 608 | write_lebn(out, rsa->e, 4); | ||
| 609 | write_lebn(out, rsa->n, -1); | ||
| 610 | if (ispub) | ||
| 611 | return; | ||
| 612 | write_lebn(out, rsa->p, hnbyte); | ||
| 613 | write_lebn(out, rsa->q, hnbyte); | ||
| 614 | write_lebn(out, rsa->dmp1, hnbyte); | ||
| 615 | write_lebn(out, rsa->dmq1, hnbyte); | ||
| 616 | write_lebn(out, rsa->iqmp, hnbyte); | ||
| 617 | write_lebn(out, rsa->d, nbyte); | ||
| 618 | } | ||
| 619 | |||
| 620 | static void | ||
| 621 | write_dsa(unsigned char **out, DSA *dsa, int ispub) | ||
| 622 | { | ||
| 623 | int nbyte; | ||
| 624 | |||
| 625 | nbyte = BN_num_bytes(dsa->p); | ||
| 626 | write_lebn(out, dsa->p, nbyte); | ||
| 627 | write_lebn(out, dsa->q, 20); | ||
| 628 | write_lebn(out, dsa->g, nbyte); | ||
| 629 | if (ispub) | ||
| 630 | write_lebn(out, dsa->pub_key, nbyte); | ||
| 631 | else | ||
| 632 | write_lebn(out, dsa->priv_key, 20); | ||
| 633 | /* Set "invalid" for seed structure values */ | ||
| 634 | memset(*out, 0xff, 24); | ||
| 635 | *out += 24; | ||
| 636 | return; | ||
| 637 | } | ||
| 638 | |||
| 639 | int | ||
| 640 | i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk) | ||
| 641 | { | ||
| 642 | return do_i2b_bio(out, pk, 0); | ||
| 643 | } | ||
| 644 | |||
| 645 | int | ||
| 646 | i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk) | ||
| 647 | { | ||
| 648 | return do_i2b_bio(out, pk, 1); | ||
| 649 | } | ||
| 650 | |||
| 651 | #ifndef OPENSSL_NO_RC4 | ||
| 652 | |||
| 653 | static int | ||
| 654 | do_PVK_header(const unsigned char **in, unsigned int length, int skip_magic, | ||
| 655 | unsigned int *psaltlen, unsigned int *pkeylen) | ||
| 656 | { | ||
| 657 | const unsigned char *p = *in; | ||
| 658 | unsigned int pvk_magic, is_encrypted; | ||
| 659 | |||
| 660 | if (skip_magic) { | ||
| 661 | if (length < 20) { | ||
| 662 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT); | ||
| 663 | return 0; | ||
| 664 | } | ||
| 665 | length -= 20; | ||
| 666 | } else { | ||
| 667 | if (length < 24) { | ||
| 668 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT); | ||
| 669 | return 0; | ||
| 670 | } | ||
| 671 | length -= 24; | ||
| 672 | pvk_magic = read_ledword(&p); | ||
| 673 | if (pvk_magic != MS_PVKMAGIC) { | ||
| 674 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER); | ||
| 675 | return 0; | ||
| 676 | } | ||
| 677 | } | ||
| 678 | /* Skip reserved */ | ||
| 679 | p += 4; | ||
| 680 | /*keytype = */read_ledword(&p); | ||
| 681 | is_encrypted = read_ledword(&p); | ||
| 682 | *psaltlen = read_ledword(&p); | ||
| 683 | *pkeylen = read_ledword(&p); | ||
| 684 | |||
| 685 | if (is_encrypted && !*psaltlen) { | ||
| 686 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER); | ||
| 687 | return 0; | ||
| 688 | } | ||
| 689 | |||
| 690 | *in = p; | ||
| 691 | return 1; | ||
| 692 | } | ||
| 693 | |||
| 694 | static int | ||
| 695 | derive_pvk_key(unsigned char *key, const unsigned char *salt, | ||
| 696 | unsigned int saltlen, const unsigned char *pass, int passlen) | ||
| 697 | { | ||
| 698 | EVP_MD_CTX mctx; | ||
| 699 | int rv = 1; | ||
| 700 | |||
| 701 | EVP_MD_CTX_init(&mctx); | ||
| 702 | if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL) || | ||
| 703 | !EVP_DigestUpdate(&mctx, salt, saltlen) || | ||
| 704 | !EVP_DigestUpdate(&mctx, pass, passlen) || | ||
| 705 | !EVP_DigestFinal_ex(&mctx, key, NULL)) | ||
| 706 | rv = 0; | ||
| 707 | |||
| 708 | EVP_MD_CTX_cleanup(&mctx); | ||
| 709 | return rv; | ||
| 710 | } | ||
| 711 | |||
| 712 | static EVP_PKEY * | ||
| 713 | do_PVK_body(const unsigned char **in, unsigned int saltlen, | ||
| 714 | unsigned int keylen, pem_password_cb *cb, void *u) | ||
| 715 | { | ||
| 716 | EVP_PKEY *ret = NULL; | ||
| 717 | const unsigned char *p = *in; | ||
| 718 | unsigned int magic; | ||
| 719 | unsigned char *enctmp = NULL, *q; | ||
| 720 | EVP_CIPHER_CTX cctx; | ||
| 721 | |||
| 722 | EVP_CIPHER_CTX_init(&cctx); | ||
| 723 | if (saltlen) { | ||
| 724 | char psbuf[PEM_BUFSIZE]; | ||
| 725 | unsigned char keybuf[20]; | ||
| 726 | int enctmplen, inlen; | ||
| 727 | |||
| 728 | if (cb) | ||
| 729 | inlen = cb(psbuf, PEM_BUFSIZE, 0, u); | ||
| 730 | else | ||
| 731 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); | ||
| 732 | if (inlen <= 0) { | ||
| 733 | PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ); | ||
| 734 | goto err; | ||
| 735 | } | ||
| 736 | enctmp = malloc(keylen + 8); | ||
| 737 | if (!enctmp) { | ||
| 738 | PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE); | ||
| 739 | goto err; | ||
| 740 | } | ||
| 741 | if (!derive_pvk_key(keybuf, p, saltlen, (unsigned char *)psbuf, | ||
| 742 | inlen)) { | ||
| 743 | goto err; | ||
| 744 | } | ||
| 745 | p += saltlen; | ||
| 746 | /* Copy BLOBHEADER across, decrypt rest */ | ||
| 747 | memcpy(enctmp, p, 8); | ||
| 748 | p += 8; | ||
| 749 | if (keylen < 8) { | ||
| 750 | PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT); | ||
| 751 | goto err; | ||
| 752 | } | ||
| 753 | inlen = keylen - 8; | ||
| 754 | q = enctmp + 8; | ||
| 755 | if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL)) | ||
| 756 | goto err; | ||
| 757 | if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen)) | ||
| 758 | goto err; | ||
| 759 | if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen)) | ||
| 760 | goto err; | ||
| 761 | magic = read_ledword((const unsigned char **)&q); | ||
| 762 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { | ||
| 763 | q = enctmp + 8; | ||
| 764 | memset(keybuf + 5, 0, 11); | ||
| 765 | if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, | ||
| 766 | NULL)) | ||
| 767 | goto err; | ||
| 768 | OPENSSL_cleanse(keybuf, 20); | ||
| 769 | if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen)) | ||
| 770 | goto err; | ||
| 771 | if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, | ||
| 772 | &enctmplen)) | ||
| 773 | goto err; | ||
| 774 | magic = read_ledword((const unsigned char **)&q); | ||
| 775 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { | ||
| 776 | PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT); | ||
| 777 | goto err; | ||
| 778 | } | ||
| 779 | } else | ||
| 780 | OPENSSL_cleanse(keybuf, 20); | ||
| 781 | p = enctmp; | ||
| 782 | } | ||
| 783 | |||
| 784 | ret = b2i_PrivateKey(&p, keylen); | ||
| 785 | |||
| 786 | err: | ||
| 787 | EVP_CIPHER_CTX_cleanup(&cctx); | ||
| 788 | if (enctmp && saltlen) | ||
| 789 | free(enctmp); | ||
| 790 | return ret; | ||
| 791 | } | ||
| 792 | |||
| 793 | |||
| 794 | EVP_PKEY * | ||
| 795 | b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u) | ||
| 796 | { | ||
| 797 | unsigned char pvk_hdr[24], *buf = NULL; | ||
| 798 | const unsigned char *p; | ||
| 799 | int buflen; | ||
| 800 | EVP_PKEY *ret = NULL; | ||
| 801 | unsigned int saltlen, keylen; | ||
| 802 | |||
| 803 | if (BIO_read(in, pvk_hdr, 24) != 24) { | ||
| 804 | PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT); | ||
| 805 | return NULL; | ||
| 806 | } | ||
| 807 | p = pvk_hdr; | ||
| 808 | |||
| 809 | if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen)) | ||
| 810 | return 0; | ||
| 811 | buflen = (int) keylen + saltlen; | ||
| 812 | buf = malloc(buflen); | ||
| 813 | if (!buf) { | ||
| 814 | PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE); | ||
| 815 | return 0; | ||
| 816 | } | ||
| 817 | p = buf; | ||
| 818 | if (BIO_read(in, buf, buflen) != buflen) { | ||
| 819 | PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT); | ||
| 820 | goto err; | ||
| 821 | } | ||
| 822 | ret = do_PVK_body(&p, saltlen, keylen, cb, u); | ||
| 823 | |||
| 824 | err: | ||
| 825 | if (buf) { | ||
| 826 | OPENSSL_cleanse(buf, buflen); | ||
| 827 | free(buf); | ||
| 828 | } | ||
| 829 | return ret; | ||
| 830 | } | ||
| 831 | |||
| 832 | static int | ||
| 833 | i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel, pem_password_cb *cb, | ||
| 834 | void *u) | ||
| 835 | { | ||
| 836 | int outlen = 24, pklen; | ||
| 837 | unsigned char *p, *salt = NULL; | ||
| 838 | EVP_CIPHER_CTX cctx; | ||
| 839 | |||
| 840 | EVP_CIPHER_CTX_init(&cctx); | ||
| 841 | if (enclevel) | ||
| 842 | outlen += PVK_SALTLEN; | ||
| 843 | pklen = do_i2b(NULL, pk, 0); | ||
| 844 | if (pklen < 0) | ||
| 845 | return -1; | ||
| 846 | outlen += pklen; | ||
| 847 | if (!out) | ||
| 848 | return outlen; | ||
| 849 | if (*out) | ||
| 850 | p = *out; | ||
| 851 | else { | ||
| 852 | p = malloc(outlen); | ||
| 853 | if (!p) { | ||
| 854 | PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE); | ||
| 855 | return -1; | ||
| 856 | } | ||
| 857 | *out = p; | ||
| 858 | } | ||
| 859 | |||
| 860 | write_ledword(&p, MS_PVKMAGIC); | ||
| 861 | write_ledword(&p, 0); | ||
| 862 | if (pk->type == EVP_PKEY_DSA) | ||
| 863 | write_ledword(&p, MS_KEYTYPE_SIGN); | ||
| 864 | else | ||
| 865 | write_ledword(&p, MS_KEYTYPE_KEYX); | ||
| 866 | write_ledword(&p, enclevel ? 1 : 0); | ||
| 867 | write_ledword(&p, enclevel ? PVK_SALTLEN : 0); | ||
| 868 | write_ledword(&p, pklen); | ||
| 869 | if (enclevel) { | ||
| 870 | arc4random_buf(p, PVK_SALTLEN); | ||
| 871 | salt = p; | ||
| 872 | p += PVK_SALTLEN; | ||
| 873 | } | ||
| 874 | do_i2b(&p, pk, 0); | ||
| 875 | if (enclevel == 0) | ||
| 876 | return outlen; | ||
| 877 | else { | ||
| 878 | char psbuf[PEM_BUFSIZE]; | ||
| 879 | unsigned char keybuf[20]; | ||
| 880 | int enctmplen, inlen; | ||
| 881 | if (cb) | ||
| 882 | inlen = cb(psbuf, PEM_BUFSIZE, 1, u); | ||
| 883 | else | ||
| 884 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u); | ||
| 885 | if (inlen <= 0) { | ||
| 886 | PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ); | ||
| 887 | goto error; | ||
| 888 | } | ||
| 889 | if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN, | ||
| 890 | (unsigned char *)psbuf, inlen)) | ||
| 891 | goto error; | ||
| 892 | if (enclevel == 1) | ||
| 893 | memset(keybuf + 5, 0, 11); | ||
| 894 | p = salt + PVK_SALTLEN + 8; | ||
| 895 | if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL)) | ||
| 896 | goto error; | ||
| 897 | OPENSSL_cleanse(keybuf, 20); | ||
| 898 | if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8)) | ||
| 899 | goto error; | ||
| 900 | if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen)) | ||
| 901 | goto error; | ||
| 902 | } | ||
| 903 | EVP_CIPHER_CTX_cleanup(&cctx); | ||
| 904 | return outlen; | ||
| 905 | |||
| 906 | error: | ||
| 907 | EVP_CIPHER_CTX_cleanup(&cctx); | ||
| 908 | return -1; | ||
| 909 | } | ||
| 910 | |||
| 911 | int | ||
| 912 | i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb, void *u) | ||
| 913 | { | ||
| 914 | unsigned char *tmp = NULL; | ||
| 915 | int outlen, wrlen; | ||
| 916 | |||
| 917 | outlen = i2b_PVK(&tmp, pk, enclevel, cb, u); | ||
| 918 | if (outlen < 0) | ||
| 919 | return -1; | ||
| 920 | wrlen = BIO_write(out, tmp, outlen); | ||
| 921 | free(tmp); | ||
| 922 | if (wrlen == outlen) { | ||
| 923 | PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE); | ||
| 924 | return outlen; | ||
| 925 | } | ||
| 926 | return -1; | ||
| 927 | } | ||
| 928 | |||
| 929 | #endif | ||
| 930 | |||
| 931 | #endif | ||
