2 * Copyright 2005-2018 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);
126 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
136 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
142 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
149 static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
151 unsigned int nbyte, hnbyte;
152 nbyte = (bitlen + 7) >> 3;
153 hnbyte = (bitlen + 15) >> 4;
157 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
161 return 44 + 3 * nbyte;
163 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
167 return 64 + 2 * nbyte;
169 /* Expected length: 4 for 'e' + 'n' */
174 * Expected length: 4 for 'e' and 7 other components. 2
175 * components are bitlen size, 5 are bitlen/2
177 return 4 + 2 * nbyte + 5 * hnbyte;
182 static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length,
185 const unsigned char *p = *in;
186 unsigned int bitlen, magic;
188 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
189 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
193 if (length < blob_length(bitlen, isdss, ispub)) {
194 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
198 return b2i_dss(&p, bitlen, ispub);
200 return b2i_rsa(&p, bitlen, ispub);
203 static EVP_PKEY *do_b2i_bio(BIO *in, int ispub)
205 const unsigned char *p;
206 unsigned char hdr_buf[16], *buf = NULL;
207 unsigned int bitlen, magic, length;
209 EVP_PKEY *ret = NULL;
210 if (BIO_read(in, hdr_buf, 16) != 16) {
211 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
215 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
218 length = blob_length(bitlen, isdss, ispub);
219 if (length > BLOB_MAX_LENGTH) {
220 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_HEADER_TOO_LONG);
223 buf = OPENSSL_malloc(length);
225 PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
229 if (BIO_read(in, buf, length) != (int)length) {
230 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
235 ret = b2i_dss(&p, bitlen, ispub);
237 ret = b2i_rsa(&p, bitlen, ispub);
244 static EVP_PKEY *b2i_dss(const unsigned char **in,
245 unsigned int bitlen, int ispub)
247 const unsigned char *p = *in;
248 EVP_PKEY *ret = NULL;
252 BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
253 BIGNUM *pub_key = NULL;
255 nbyte = (bitlen + 7) >> 3;
258 ret = EVP_PKEY_new();
259 if (dsa == NULL || ret == NULL)
261 if (!read_lebn(&p, nbyte, &pbn))
264 if (!read_lebn(&p, 20, &qbn))
267 if (!read_lebn(&p, nbyte, &gbn))
271 if (!read_lebn(&p, nbyte, &pub_key))
274 if (!read_lebn(&p, 20, &priv_key))
277 /* Calculate public key */
281 if ((ctx = BN_CTX_new()) == NULL)
284 if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
290 if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
292 pbn = qbn = gbn = NULL;
293 if (!DSA_set0_key(dsa, pub_key, priv_key))
295 pub_key = priv_key = NULL;
297 if (!EVP_PKEY_set1_DSA(ret, dsa))
304 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
316 static EVP_PKEY *b2i_rsa(const unsigned char **in,
317 unsigned int bitlen, int ispub)
319 const unsigned char *pin = *in;
320 EVP_PKEY *ret = NULL;
321 BIGNUM *e = NULL, *n = NULL, *d = NULL;
322 BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
324 unsigned int nbyte, hnbyte;
325 nbyte = (bitlen + 7) >> 3;
326 hnbyte = (bitlen + 15) >> 4;
328 ret = EVP_PKEY_new();
329 if (rsa == NULL || ret == NULL)
334 if (!BN_set_word(e, read_ledword(&pin)))
336 if (!read_lebn(&pin, nbyte, &n))
339 if (!read_lebn(&pin, hnbyte, &p))
341 if (!read_lebn(&pin, hnbyte, &q))
343 if (!read_lebn(&pin, hnbyte, &dmp1))
345 if (!read_lebn(&pin, hnbyte, &dmq1))
347 if (!read_lebn(&pin, hnbyte, &iqmp))
349 if (!read_lebn(&pin, nbyte, &d))
351 if (!RSA_set0_factors(rsa, p, q))
354 if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
356 dmp1 = dmq1 = iqmp = NULL;
358 if (!RSA_set0_key(rsa, n, e, d))
362 if (!EVP_PKEY_set1_RSA(ret, rsa))
368 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
382 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
384 return do_b2i(in, length, 0);
387 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
389 return do_b2i(in, length, 1);
392 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
394 return do_b2i_bio(in, 0);
397 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
399 return do_b2i_bio(in, 1);
402 static void write_ledword(unsigned char **out, unsigned int dw)
404 unsigned char *p = *out;
406 *p++ = (dw >> 8) & 0xff;
407 *p++ = (dw >> 16) & 0xff;
408 *p++ = (dw >> 24) & 0xff;
412 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
414 BN_bn2lebinpad(bn, *out, len);
418 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
419 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
421 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
422 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
424 static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
427 unsigned int bitlen, magic = 0, keyalg;
428 int outlen, noinc = 0;
429 int pktype = EVP_PKEY_id(pk);
430 if (pktype == EVP_PKEY_DSA) {
431 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
432 keyalg = MS_KEYALG_DSS_SIGN;
433 } else if (pktype == EVP_PKEY_RSA) {
434 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
435 keyalg = MS_KEYALG_RSA_KEYX;
440 outlen = 16 + blob_length(bitlen,
441 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
447 if ((p = OPENSSL_malloc(outlen)) == NULL) {
448 PEMerr(PEM_F_DO_I2B, ERR_R_MALLOC_FAILURE);
455 *p++ = MS_PUBLICKEYBLOB;
457 *p++ = MS_PRIVATEKEYBLOB;
461 write_ledword(&p, keyalg);
462 write_ledword(&p, magic);
463 write_ledword(&p, bitlen);
464 if (keyalg == MS_KEYALG_DSS_SIGN)
465 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
467 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
473 static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
475 unsigned char *tmp = NULL;
477 outlen = do_i2b(&tmp, pk, ispub);
480 wrlen = BIO_write(out, tmp, outlen);
487 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
490 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
491 const BIGNUM *pub_key = NULL, *priv_key = NULL;
493 DSA_get0_pqg(dsa, &p, &q, &g);
494 DSA_get0_key(dsa, &pub_key, &priv_key);
495 bitlen = BN_num_bits(p);
496 if ((bitlen & 7) || (BN_num_bits(q) != 160)
497 || (BN_num_bits(g) > bitlen))
500 if (BN_num_bits(pub_key) > bitlen)
502 *pmagic = MS_DSS1MAGIC;
504 if (BN_num_bits(priv_key) > 160)
506 *pmagic = MS_DSS2MAGIC;
511 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
515 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
517 int nbyte, hnbyte, bitlen;
520 RSA_get0_key(rsa, NULL, &e, NULL);
521 if (BN_num_bits(e) > 32)
523 bitlen = RSA_bits(rsa);
524 nbyte = RSA_size(rsa);
525 hnbyte = (bitlen + 15) >> 4;
527 *pmagic = MS_RSA1MAGIC;
530 const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
532 *pmagic = MS_RSA2MAGIC;
535 * For private key each component must fit within nbyte or hnbyte.
537 RSA_get0_key(rsa, NULL, NULL, &d);
538 if (BN_num_bytes(d) > nbyte)
540 RSA_get0_factors(rsa, &p, &q);
541 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
542 if ((BN_num_bytes(iqmp) > hnbyte)
543 || (BN_num_bytes(p) > hnbyte)
544 || (BN_num_bytes(q) > hnbyte)
545 || (BN_num_bytes(dmp1) > hnbyte)
546 || (BN_num_bytes(dmq1) > hnbyte))
551 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
555 static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
558 const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
560 nbyte = RSA_size(rsa);
561 hnbyte = (RSA_bits(rsa) + 15) >> 4;
562 RSA_get0_key(rsa, &n, &e, &d);
563 write_lebn(out, e, 4);
564 write_lebn(out, n, nbyte);
567 RSA_get0_factors(rsa, &p, &q);
568 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
569 write_lebn(out, p, hnbyte);
570 write_lebn(out, q, hnbyte);
571 write_lebn(out, dmp1, hnbyte);
572 write_lebn(out, dmq1, hnbyte);
573 write_lebn(out, iqmp, hnbyte);
574 write_lebn(out, d, nbyte);
577 static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
580 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
581 const BIGNUM *pub_key = NULL, *priv_key = NULL;
583 DSA_get0_pqg(dsa, &p, &q, &g);
584 DSA_get0_key(dsa, &pub_key, &priv_key);
585 nbyte = BN_num_bytes(p);
586 write_lebn(out, p, nbyte);
587 write_lebn(out, q, 20);
588 write_lebn(out, g, nbyte);
590 write_lebn(out, pub_key, nbyte);
592 write_lebn(out, priv_key, 20);
593 /* Set "invalid" for seed structure values */
594 memset(*out, 0xff, 24);
599 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
601 return do_i2b_bio(out, pk, 0);
604 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
606 return do_i2b_bio(out, pk, 1);
609 # ifndef OPENSSL_NO_RC4
611 static int do_PVK_header(const unsigned char **in, unsigned int length,
613 unsigned int *psaltlen, unsigned int *pkeylen)
615 const unsigned char *p = *in;
616 unsigned int pvk_magic, is_encrypted;
619 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
624 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
627 pvk_magic = read_ledword(&p);
628 if (pvk_magic != MS_PVKMAGIC) {
629 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
638 is_encrypted = read_ledword(&p);
639 *psaltlen = read_ledword(&p);
640 *pkeylen = read_ledword(&p);
642 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
645 if (is_encrypted && !*psaltlen) {
646 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
654 static int derive_pvk_key(unsigned char *key,
655 const unsigned char *salt, unsigned int saltlen,
656 const unsigned char *pass, int passlen)
658 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
661 || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
662 || !EVP_DigestUpdate(mctx, salt, saltlen)
663 || !EVP_DigestUpdate(mctx, pass, passlen)
664 || !EVP_DigestFinal_ex(mctx, key, NULL))
667 EVP_MD_CTX_free(mctx);
671 static EVP_PKEY *do_PVK_body(const unsigned char **in,
672 unsigned int saltlen, unsigned int keylen,
673 pem_password_cb *cb, void *u)
675 EVP_PKEY *ret = NULL;
676 const unsigned char *p = *in;
678 unsigned char *enctmp = NULL, *q;
680 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
682 char psbuf[PEM_BUFSIZE];
683 unsigned char keybuf[20];
684 int enctmplen, inlen;
686 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
688 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
690 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
693 enctmp = OPENSSL_malloc(keylen + 8);
694 if (enctmp == NULL) {
695 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
698 if (!derive_pvk_key(keybuf, p, saltlen,
699 (unsigned char *)psbuf, inlen))
702 /* Copy BLOBHEADER across, decrypt rest */
703 memcpy(enctmp, p, 8);
706 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
711 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
713 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
715 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
717 magic = read_ledword((const unsigned char **)&q);
718 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
720 memset(keybuf + 5, 0, 11);
721 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
723 OPENSSL_cleanse(keybuf, 20);
724 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
726 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
728 magic = read_ledword((const unsigned char **)&q);
729 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
730 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
734 OPENSSL_cleanse(keybuf, 20);
738 ret = b2i_PrivateKey(&p, keylen);
740 EVP_CIPHER_CTX_free(cctx);
741 OPENSSL_free(enctmp);
745 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
747 unsigned char pvk_hdr[24], *buf = NULL;
748 const unsigned char *p;
750 EVP_PKEY *ret = NULL;
751 unsigned int saltlen, keylen;
752 if (BIO_read(in, pvk_hdr, 24) != 24) {
753 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
758 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
760 buflen = (int)keylen + saltlen;
761 buf = OPENSSL_malloc(buflen);
763 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
767 if (BIO_read(in, buf, buflen) != buflen) {
768 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
771 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
774 OPENSSL_clear_free(buf, buflen);
778 static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
779 pem_password_cb *cb, void *u)
781 int outlen = 24, pklen;
782 unsigned char *p = NULL, *start = NULL, *salt = NULL;
783 EVP_CIPHER_CTX *cctx = NULL;
785 outlen += PVK_SALTLEN;
786 pklen = do_i2b(NULL, pk, 0);
795 start = p = OPENSSL_malloc(outlen);
797 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
802 cctx = EVP_CIPHER_CTX_new();
806 write_ledword(&p, MS_PVKMAGIC);
807 write_ledword(&p, 0);
808 if (EVP_PKEY_id(pk) == EVP_PKEY_DSA)
809 write_ledword(&p, MS_KEYTYPE_SIGN);
811 write_ledword(&p, MS_KEYTYPE_KEYX);
812 write_ledword(&p, enclevel ? 1 : 0);
813 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
814 write_ledword(&p, pklen);
816 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
823 char psbuf[PEM_BUFSIZE];
824 unsigned char keybuf[20];
825 int enctmplen, inlen;
827 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
829 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
831 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
834 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
835 (unsigned char *)psbuf, inlen))
838 memset(keybuf + 5, 0, 11);
839 p = salt + PVK_SALTLEN + 8;
840 if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
842 OPENSSL_cleanse(keybuf, 20);
843 if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
845 if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen))
849 EVP_CIPHER_CTX_free(cctx);
857 EVP_CIPHER_CTX_free(cctx);
863 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
864 pem_password_cb *cb, void *u)
866 unsigned char *tmp = NULL;
868 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
871 wrlen = BIO_write(out, tmp, outlen);
873 if (wrlen == outlen) {
874 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);