2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Support for PVK format keys and related structures (such a PUBLICKEYBLOB
12 * and PRIVATEKEYBLOB).
15 #include "internal/cryptlib.h"
16 #include <openssl/pem.h>
17 #include <openssl/rand.h>
18 #include <openssl/bn.h>
19 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
20 # include <openssl/dsa.h>
21 # include <openssl/rsa.h>
24 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
28 static unsigned int read_ledword(const unsigned char **in)
30 const unsigned char *p = *in;
41 * Read a BIGNUM in little endian format. The docs say that this should take
45 static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
47 *r = BN_lebin2bn(*in, nbyte, NULL);
54 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
56 # define MS_PUBLICKEYBLOB 0x6
57 # define MS_PRIVATEKEYBLOB 0x7
58 # define MS_RSA1MAGIC 0x31415352L
59 # define MS_RSA2MAGIC 0x32415352L
60 # define MS_DSS1MAGIC 0x31535344L
61 # define MS_DSS2MAGIC 0x32535344L
63 # define MS_KEYALG_RSA_KEYX 0xa400
64 # define MS_KEYALG_DSS_SIGN 0x2200
66 # define MS_KEYTYPE_KEYX 0x1
67 # define MS_KEYTYPE_SIGN 0x2
69 /* Maximum length of a blob after header */
70 # define BLOB_MAX_LENGTH 102400
72 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
73 # define MS_PVKMAGIC 0xb0b5f11eL
74 /* Salt length for PVK files */
75 # define PVK_SALTLEN 0x10
76 /* Maximum length in PVK header */
77 # define PVK_MAX_KEYLEN 102400
78 /* Maximum salt length */
79 # define PVK_MAX_SALTLEN 10240
81 static EVP_PKEY *b2i_rsa(const unsigned char **in,
82 unsigned int bitlen, int ispub);
83 static EVP_PKEY *b2i_dss(const unsigned char **in,
84 unsigned int bitlen, int ispub);
86 static int do_blob_header(const unsigned char **in, unsigned int length,
87 unsigned int *pmagic, unsigned int *pbitlen,
88 int *pisdss, int *pispub)
90 const unsigned char *p = *in;
94 if (*p == MS_PUBLICKEYBLOB) {
96 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
100 } else if (*p == MS_PRIVATEKEYBLOB) {
102 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
111 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER);
114 /* Ignore reserved, aiKeyAlg */
116 *pmagic = read_ledword(&p);
117 *pbitlen = read_ledword(&p);
125 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
134 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
140 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
147 static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
149 unsigned int nbyte, hnbyte;
150 nbyte = (bitlen + 7) >> 3;
151 hnbyte = (bitlen + 15) >> 4;
155 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
159 return 44 + 3 * nbyte;
161 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
165 return 64 + 2 * nbyte;
167 /* Expected length: 4 for 'e' + 'n' */
172 * Expected length: 4 for 'e' and 7 other components. 2
173 * components are bitlen size, 5 are bitlen/2
175 return 4 + 2 * nbyte + 5 * hnbyte;
180 static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length,
183 const unsigned char *p = *in;
184 unsigned int bitlen, magic;
186 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
187 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
191 if (length < blob_length(bitlen, isdss, ispub)) {
192 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
196 return b2i_dss(&p, bitlen, ispub);
198 return b2i_rsa(&p, bitlen, ispub);
201 static EVP_PKEY *do_b2i_bio(BIO *in, int ispub)
203 const unsigned char *p;
204 unsigned char hdr_buf[16], *buf = NULL;
205 unsigned int bitlen, magic, length;
207 EVP_PKEY *ret = NULL;
208 if (BIO_read(in, hdr_buf, 16) != 16) {
209 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
213 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
216 length = blob_length(bitlen, isdss, ispub);
217 if (length > BLOB_MAX_LENGTH) {
218 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_HEADER_TOO_LONG);
221 buf = OPENSSL_malloc(length);
223 PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
227 if (BIO_read(in, buf, length) != (int)length) {
228 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
233 ret = b2i_dss(&p, bitlen, ispub);
235 ret = b2i_rsa(&p, bitlen, ispub);
242 static EVP_PKEY *b2i_dss(const unsigned char **in,
243 unsigned int bitlen, int ispub)
245 const unsigned char *p = *in;
246 EVP_PKEY *ret = NULL;
250 BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
251 BIGNUM *pub_key = NULL;
253 nbyte = (bitlen + 7) >> 3;
256 ret = EVP_PKEY_new();
257 if (dsa == NULL || ret == NULL)
259 if (!read_lebn(&p, nbyte, &pbn))
262 if (!read_lebn(&p, 20, &qbn))
265 if (!read_lebn(&p, nbyte, &gbn))
269 if (!read_lebn(&p, nbyte, &pub_key))
272 if (!read_lebn(&p, 20, &priv_key))
275 /* Calculate public key */
279 if ((ctx = BN_CTX_new()) == NULL)
282 if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
287 if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
289 pbn = qbn = gbn = NULL;
290 if (!DSA_set0_key(dsa, pub_key, priv_key))
293 EVP_PKEY_set1_DSA(ret, dsa);
299 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
311 static EVP_PKEY *b2i_rsa(const unsigned char **in,
312 unsigned int bitlen, int ispub)
314 const unsigned char *pin = *in;
315 EVP_PKEY *ret = NULL;
316 BIGNUM *e = NULL, *n = NULL, *d = NULL;
317 BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
319 unsigned int nbyte, hnbyte;
320 nbyte = (bitlen + 7) >> 3;
321 hnbyte = (bitlen + 15) >> 4;
323 ret = EVP_PKEY_new();
324 if (rsa == NULL || ret == NULL)
329 if (!BN_set_word(e, read_ledword(&pin)))
331 if (!read_lebn(&pin, nbyte, &n))
334 if (!read_lebn(&pin, hnbyte, &p))
336 if (!read_lebn(&pin, hnbyte, &q))
338 if (!read_lebn(&pin, hnbyte, &dmp1))
340 if (!read_lebn(&pin, hnbyte, &dmq1))
342 if (!read_lebn(&pin, hnbyte, &iqmp))
344 if (!read_lebn(&pin, nbyte, &d))
346 RSA_set0_factors(rsa, p, q);
347 RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
349 RSA_set0_key(rsa, n, e, d);
351 EVP_PKEY_set1_RSA(ret, rsa);
356 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
370 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
372 return do_b2i(in, length, 0);
375 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
377 return do_b2i(in, length, 1);
380 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
382 return do_b2i_bio(in, 0);
385 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
387 return do_b2i_bio(in, 1);
390 static void write_ledword(unsigned char **out, unsigned int dw)
392 unsigned char *p = *out;
394 *p++ = (dw >> 8) & 0xff;
395 *p++ = (dw >> 16) & 0xff;
396 *p++ = (dw >> 24) & 0xff;
400 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
402 BN_bn2lebinpad(bn, *out, len);
406 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
407 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
409 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
410 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
412 static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
415 unsigned int bitlen, magic = 0, keyalg;
416 int outlen, noinc = 0;
417 int pktype = EVP_PKEY_id(pk);
418 if (pktype == EVP_PKEY_DSA) {
419 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
420 keyalg = MS_KEYALG_DSS_SIGN;
421 } else if (pktype == EVP_PKEY_RSA) {
422 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
423 keyalg = MS_KEYALG_RSA_KEYX;
428 outlen = 16 + blob_length(bitlen,
429 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
435 p = OPENSSL_malloc(outlen);
442 *p++ = MS_PUBLICKEYBLOB;
444 *p++ = MS_PRIVATEKEYBLOB;
448 write_ledword(&p, keyalg);
449 write_ledword(&p, magic);
450 write_ledword(&p, bitlen);
451 if (keyalg == MS_KEYALG_DSS_SIGN)
452 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
454 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
460 static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
462 unsigned char *tmp = NULL;
464 outlen = do_i2b(&tmp, pk, ispub);
467 wrlen = BIO_write(out, tmp, outlen);
474 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
477 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
478 const BIGNUM *pub_key = NULL, *priv_key = NULL;
480 DSA_get0_pqg(dsa, &p, &q, &g);
481 DSA_get0_key(dsa, &pub_key, &priv_key);
482 bitlen = BN_num_bits(p);
483 if ((bitlen & 7) || (BN_num_bits(q) != 160)
484 || (BN_num_bits(g) > bitlen))
487 if (BN_num_bits(pub_key) > bitlen)
489 *pmagic = MS_DSS1MAGIC;
491 if (BN_num_bits(priv_key) > 160)
493 *pmagic = MS_DSS2MAGIC;
498 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
502 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
504 int nbyte, hnbyte, bitlen;
507 RSA_get0_key(rsa, NULL, &e, NULL);
508 if (BN_num_bits(e) > 32)
510 bitlen = RSA_bits(rsa);
511 nbyte = RSA_size(rsa);
512 hnbyte = (bitlen + 15) >> 4;
514 *pmagic = MS_RSA1MAGIC;
517 const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
519 *pmagic = MS_RSA2MAGIC;
522 * For private key each component must fit within nbyte or hnbyte.
524 RSA_get0_key(rsa, NULL, NULL, &d);
525 if (BN_num_bytes(d) > nbyte)
527 RSA_get0_factors(rsa, &p, &q);
528 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
529 if ((BN_num_bytes(iqmp) > hnbyte)
530 || (BN_num_bytes(p) > hnbyte)
531 || (BN_num_bytes(q) > hnbyte)
532 || (BN_num_bytes(dmp1) > hnbyte)
533 || (BN_num_bytes(dmq1) > hnbyte))
538 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
542 static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
545 const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
547 nbyte = RSA_size(rsa);
548 hnbyte = (RSA_bits(rsa) + 15) >> 4;
549 RSA_get0_key(rsa, &n, &e, &d);
550 write_lebn(out, e, 4);
551 write_lebn(out, n, nbyte);
554 RSA_get0_factors(rsa, &p, &q);
555 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
556 write_lebn(out, p, hnbyte);
557 write_lebn(out, q, hnbyte);
558 write_lebn(out, dmp1, hnbyte);
559 write_lebn(out, dmq1, hnbyte);
560 write_lebn(out, iqmp, hnbyte);
561 write_lebn(out, d, nbyte);
564 static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
567 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
568 const BIGNUM *pub_key = NULL, *priv_key = NULL;
570 DSA_get0_pqg(dsa, &p, &q, &g);
571 DSA_get0_key(dsa, &pub_key, &priv_key);
572 nbyte = BN_num_bytes(p);
573 write_lebn(out, p, nbyte);
574 write_lebn(out, q, 20);
575 write_lebn(out, g, nbyte);
577 write_lebn(out, pub_key, nbyte);
579 write_lebn(out, priv_key, 20);
580 /* Set "invalid" for seed structure values */
581 memset(*out, 0xff, 24);
586 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
588 return do_i2b_bio(out, pk, 0);
591 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
593 return do_i2b_bio(out, pk, 1);
596 # ifndef OPENSSL_NO_RC4
598 static int do_PVK_header(const unsigned char **in, unsigned int length,
600 unsigned int *psaltlen, unsigned int *pkeylen)
602 const unsigned char *p = *in;
603 unsigned int pvk_magic, is_encrypted;
606 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
611 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
614 pvk_magic = read_ledword(&p);
615 if (pvk_magic != MS_PVKMAGIC) {
616 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
625 is_encrypted = read_ledword(&p);
626 *psaltlen = read_ledword(&p);
627 *pkeylen = read_ledword(&p);
629 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
632 if (is_encrypted && !*psaltlen) {
633 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
641 static int derive_pvk_key(unsigned char *key,
642 const unsigned char *salt, unsigned int saltlen,
643 const unsigned char *pass, int passlen)
645 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
648 || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
649 || !EVP_DigestUpdate(mctx, salt, saltlen)
650 || !EVP_DigestUpdate(mctx, pass, passlen)
651 || !EVP_DigestFinal_ex(mctx, key, NULL))
654 EVP_MD_CTX_free(mctx);
658 static EVP_PKEY *do_PVK_body(const unsigned char **in,
659 unsigned int saltlen, unsigned int keylen,
660 pem_password_cb *cb, void *u)
662 EVP_PKEY *ret = NULL;
663 const unsigned char *p = *in;
665 unsigned char *enctmp = NULL, *q;
667 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
669 char psbuf[PEM_BUFSIZE];
670 unsigned char keybuf[20];
671 int enctmplen, inlen;
673 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
675 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
677 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
680 enctmp = OPENSSL_malloc(keylen + 8);
681 if (enctmp == NULL) {
682 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
685 if (!derive_pvk_key(keybuf, p, saltlen,
686 (unsigned char *)psbuf, inlen))
689 /* Copy BLOBHEADER across, decrypt rest */
690 memcpy(enctmp, p, 8);
693 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
698 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
700 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
702 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
704 magic = read_ledword((const unsigned char **)&q);
705 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
707 memset(keybuf + 5, 0, 11);
708 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
710 OPENSSL_cleanse(keybuf, 20);
711 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
713 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
715 magic = read_ledword((const unsigned char **)&q);
716 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
717 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
721 OPENSSL_cleanse(keybuf, 20);
725 ret = b2i_PrivateKey(&p, keylen);
727 EVP_CIPHER_CTX_free(cctx);
728 OPENSSL_free(enctmp);
732 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
734 unsigned char pvk_hdr[24], *buf = NULL;
735 const unsigned char *p;
737 EVP_PKEY *ret = NULL;
738 unsigned int saltlen, keylen;
739 if (BIO_read(in, pvk_hdr, 24) != 24) {
740 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
745 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
747 buflen = (int)keylen + saltlen;
748 buf = OPENSSL_malloc(buflen);
750 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
754 if (BIO_read(in, buf, buflen) != buflen) {
755 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
758 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
761 OPENSSL_clear_free(buf, buflen);
765 static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
766 pem_password_cb *cb, void *u)
768 int outlen = 24, pklen;
769 unsigned char *p = NULL, *start = NULL, *salt = NULL;
770 EVP_CIPHER_CTX *cctx = NULL;
772 outlen += PVK_SALTLEN;
773 pklen = do_i2b(NULL, pk, 0);
782 start = p = OPENSSL_malloc(outlen);
784 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
789 cctx = EVP_CIPHER_CTX_new();
793 write_ledword(&p, MS_PVKMAGIC);
794 write_ledword(&p, 0);
795 if (EVP_PKEY_id(pk) == EVP_PKEY_DSA)
796 write_ledword(&p, MS_KEYTYPE_SIGN);
798 write_ledword(&p, MS_KEYTYPE_KEYX);
799 write_ledword(&p, enclevel ? 1 : 0);
800 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
801 write_ledword(&p, pklen);
803 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
810 char psbuf[PEM_BUFSIZE];
811 unsigned char keybuf[20];
812 int enctmplen, inlen;
814 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
816 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
818 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
821 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
822 (unsigned char *)psbuf, inlen))
825 memset(keybuf + 5, 0, 11);
826 p = salt + PVK_SALTLEN + 8;
827 if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
829 OPENSSL_cleanse(keybuf, 20);
830 if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
832 if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen))
836 EVP_CIPHER_CTX_free(cctx);
844 EVP_CIPHER_CTX_free(cctx);
850 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
851 pem_password_cb *cb, void *u)
853 unsigned char *tmp = NULL;
855 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
858 wrlen = BIO_write(out, tmp, outlen);
860 if (wrlen == outlen) {
861 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);