2 * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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).
16 * RSA and DSA low level APIs are deprecated for public use, but still ok for
19 #include "internal/deprecated.h"
21 #include <openssl/pem.h>
22 #include <openssl/rand.h>
23 #include <openssl/bn.h>
24 #include <openssl/dsa.h>
25 #include <openssl/rsa.h>
26 #include <openssl/kdf.h>
27 #include <openssl/core_names.h>
28 #include "internal/cryptlib.h"
29 #include "crypto/pem.h"
30 #include "crypto/evp.h"
33 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
37 static unsigned int read_ledword(const unsigned char **in)
39 const unsigned char *p = *in;
42 ret = (unsigned int)*p++;
43 ret |= (unsigned int)*p++ << 8;
44 ret |= (unsigned int)*p++ << 16;
45 ret |= (unsigned int)*p++ << 24;
51 * Read a BIGNUM in little endian format. The docs say that this should take
55 static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
57 *r = BN_lebin2bn(*in, nbyte, NULL);
65 * Create an EVP_PKEY from a type specific key.
66 * This takes ownership of |key|, as long as the |evp_type| is acceptable
67 * (EVP_PKEY_RSA or EVP_PKEY_DSA), even if the resulting EVP_PKEY wasn't
70 #define isdss_to_evp_type(isdss) \
71 (isdss == 0 ? EVP_PKEY_RSA : isdss == 1 ? EVP_PKEY_DSA : EVP_PKEY_NONE)
72 static EVP_PKEY *evp_pkey_new0_key(void *key, int evp_type)
74 EVP_PKEY *pkey = NULL;
77 * It's assumed that if |key| is NULL, something went wrong elsewhere
78 * and suitable errors are already reported.
83 if (!ossl_assert(evp_type == EVP_PKEY_RSA || evp_type == EVP_PKEY_DSA)) {
84 ERR_raise(ERR_LIB_PEM, ERR_R_INTERNAL_ERROR);
88 if ((pkey = EVP_PKEY_new()) != NULL) {
91 if (EVP_PKEY_set1_RSA(pkey, key))
96 #ifndef OPENSSL_NO_DSA
98 if (EVP_PKEY_set1_DSA(pkey, key))
111 #ifndef OPENSSL_NO_DSA
119 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
123 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
125 # define MS_PUBLICKEYBLOB 0x6
126 # define MS_PRIVATEKEYBLOB 0x7
127 # define MS_RSA1MAGIC 0x31415352L
128 # define MS_RSA2MAGIC 0x32415352L
129 # define MS_DSS1MAGIC 0x31535344L
130 # define MS_DSS2MAGIC 0x32535344L
132 # define MS_KEYALG_RSA_KEYX 0xa400
133 # define MS_KEYALG_DSS_SIGN 0x2200
135 # define MS_KEYTYPE_KEYX 0x1
136 # define MS_KEYTYPE_SIGN 0x2
138 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
139 # define MS_PVKMAGIC 0xb0b5f11eL
140 /* Salt length for PVK files */
141 # define PVK_SALTLEN 0x10
142 /* Maximum length in PVK header */
143 # define PVK_MAX_KEYLEN 102400
144 /* Maximum salt length */
145 # define PVK_MAX_SALTLEN 10240
148 * Read the MSBLOB header and get relevant data from it.
150 * |pisdss| and |pispub| have a double role, as they can be used for
151 * discovery as well as to check the the blob meets expectations.
152 * |*pisdss| is the indicator for whether the key is a DSA key or not.
153 * |*pispub| is the indicator for whether the key is public or not.
154 * In both cases, the following input values apply:
156 * 0 Expected to not be what the variable indicates.
157 * 1 Expected to be what the variable indicates.
158 * -1 No expectations, this function will assign 0 or 1 depending on
161 int ossl_do_blob_header(const unsigned char **in, unsigned int length,
162 unsigned int *pmagic, unsigned int *pbitlen,
163 int *pisdss, int *pispub)
165 const unsigned char *p = *in;
171 case MS_PUBLICKEYBLOB:
173 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
179 case MS_PRIVATEKEYBLOB:
181 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
193 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER);
196 /* Ignore reserved, aiKeyAlg */
198 *pmagic = read_ledword(&p);
199 *pbitlen = read_ledword(&p);
201 /* Consistency check for private vs public */
206 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
214 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
220 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
224 /* Check that we got the expected type */
229 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_DSS_KEY_BLOB);
237 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_RSA_KEY_BLOB);
244 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
251 unsigned int ossl_blob_length(unsigned bitlen, int isdss, int ispub)
253 unsigned int nbyte = (bitlen + 7) >> 3;
254 unsigned int hnbyte = (bitlen + 15) >> 4;
259 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
263 return 44 + 3 * nbyte;
265 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
269 return 64 + 2 * nbyte;
271 /* Expected length: 4 for 'e' + 'n' */
276 * Expected length: 4 for 'e' and 7 other components. 2
277 * components are bitlen size, 5 are bitlen/2
279 return 4 + 2 * nbyte + 5 * hnbyte;
284 static void *do_b2i_key(const unsigned char **in, unsigned int length,
285 int *isdss, int *ispub)
287 const unsigned char *p = *in;
288 unsigned int bitlen, magic;
291 if (ossl_do_blob_header(&p, length, &magic, &bitlen, isdss, ispub) <= 0) {
292 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
296 if (length < ossl_blob_length(bitlen, *isdss, *ispub)) {
297 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
301 key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
302 #ifndef OPENSSL_NO_DSA
304 key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
308 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
315 EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub)
318 void *key = do_b2i_key(in, length, &isdss, ispub);
320 return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
323 EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub)
325 const unsigned char *p;
326 unsigned char hdr_buf[16], *buf = NULL;
327 unsigned int bitlen, magic, length;
330 EVP_PKEY *pkey = NULL;
332 if (BIO_read(in, hdr_buf, 16) != 16) {
333 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
337 if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0)
340 length = ossl_blob_length(bitlen, isdss, *ispub);
341 if (length > BLOB_MAX_LENGTH) {
342 ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
345 buf = OPENSSL_malloc(length);
347 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
351 if (BIO_read(in, buf, length) != (int)length) {
352 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
357 key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
358 #ifndef OPENSSL_NO_DSA
360 key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
364 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
368 pkey = evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
374 #ifndef OPENSSL_NO_DSA
375 DSA *ossl_b2i_DSA_after_header(const unsigned char **in, unsigned int bitlen,
378 const unsigned char *p = *in;
381 BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
382 BIGNUM *pub_key = NULL;
383 unsigned int nbyte = (bitlen + 7) >> 3;
388 if (!read_lebn(&p, nbyte, &pbn))
391 if (!read_lebn(&p, 20, &qbn))
394 if (!read_lebn(&p, nbyte, &gbn))
398 if (!read_lebn(&p, nbyte, &pub_key))
401 if (!read_lebn(&p, 20, &priv_key))
404 /* Set constant time flag before public key calculation */
405 BN_set_flags(priv_key, BN_FLG_CONSTTIME);
407 /* Calculate public key */
411 if ((ctx = BN_CTX_new()) == NULL)
414 if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
420 if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
422 pbn = qbn = gbn = NULL;
423 if (!DSA_set0_key(dsa, pub_key, priv_key))
425 pub_key = priv_key = NULL;
431 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
443 RSA *ossl_b2i_RSA_after_header(const unsigned char **in, unsigned int bitlen,
446 const unsigned char *pin = *in;
447 BIGNUM *e = NULL, *n = NULL, *d = NULL;
448 BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
450 unsigned int nbyte = (bitlen + 7) >> 3;
451 unsigned int hnbyte = (bitlen + 15) >> 4;
459 if (!BN_set_word(e, read_ledword(&pin)))
461 if (!read_lebn(&pin, nbyte, &n))
464 if (!read_lebn(&pin, hnbyte, &p))
466 if (!read_lebn(&pin, hnbyte, &q))
468 if (!read_lebn(&pin, hnbyte, &dmp1))
470 if (!read_lebn(&pin, hnbyte, &dmq1))
472 if (!read_lebn(&pin, hnbyte, &iqmp))
474 if (!read_lebn(&pin, nbyte, &d))
476 if (!RSA_set0_factors(rsa, p, q))
479 if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
481 dmp1 = dmq1 = iqmp = NULL;
483 if (!RSA_set0_key(rsa, n, e, d))
490 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
503 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
507 return ossl_b2i(in, length, &ispub);
510 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
514 return ossl_b2i(in, length, &ispub);
517 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
521 return ossl_b2i_bio(in, &ispub);
524 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
528 return ossl_b2i_bio(in, &ispub);
531 static void write_ledword(unsigned char **out, unsigned int dw)
533 unsigned char *p = *out;
536 *p++ = (dw >> 8) & 0xff;
537 *p++ = (dw >> 16) & 0xff;
538 *p++ = (dw >> 24) & 0xff;
542 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
544 BN_bn2lebinpad(bn, *out, len);
548 static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *magic);
549 static void write_rsa(unsigned char **out, const RSA *rsa, int ispub);
551 #ifndef OPENSSL_NO_DSA
552 static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *magic);
553 static void write_dsa(unsigned char **out, const DSA *dsa, int ispub);
556 static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub)
559 unsigned int bitlen = 0, magic = 0, keyalg = 0;
560 int outlen = -1, noinc = 0;
562 if (EVP_PKEY_is_a(pk, "RSA")) {
563 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
564 keyalg = MS_KEYALG_RSA_KEYX;
565 #ifndef OPENSSL_NO_DSA
566 } else if (EVP_PKEY_is_a(pk, "DSA")) {
567 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
568 keyalg = MS_KEYALG_DSS_SIGN;
575 + ossl_blob_length(bitlen, keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
581 if ((p = OPENSSL_malloc(outlen)) == NULL) {
582 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
590 *p++ = MS_PUBLICKEYBLOB;
592 *p++ = MS_PRIVATEKEYBLOB;
596 write_ledword(&p, keyalg);
597 write_ledword(&p, magic);
598 write_ledword(&p, bitlen);
599 if (keyalg == MS_KEYALG_RSA_KEYX)
600 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
601 #ifndef OPENSSL_NO_DSA
603 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
611 static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub)
613 unsigned char *tmp = NULL;
616 outlen = do_i2b(&tmp, pk, ispub);
619 wrlen = BIO_write(out, tmp, outlen);
626 static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *pmagic)
628 int nbyte, hnbyte, bitlen;
631 RSA_get0_key(rsa, NULL, &e, NULL);
632 if (BN_num_bits(e) > 32)
634 bitlen = RSA_bits(rsa);
635 nbyte = RSA_size(rsa);
636 hnbyte = (bitlen + 15) >> 4;
638 *pmagic = MS_RSA1MAGIC;
641 const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
643 *pmagic = MS_RSA2MAGIC;
646 * For private key each component must fit within nbyte or hnbyte.
648 RSA_get0_key(rsa, NULL, NULL, &d);
649 if (BN_num_bytes(d) > nbyte)
651 RSA_get0_factors(rsa, &p, &q);
652 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
653 if ((BN_num_bytes(iqmp) > hnbyte)
654 || (BN_num_bytes(p) > hnbyte)
655 || (BN_num_bytes(q) > hnbyte)
656 || (BN_num_bytes(dmp1) > hnbyte)
657 || (BN_num_bytes(dmq1) > hnbyte))
662 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
666 static void write_rsa(unsigned char **out, const RSA *rsa, int ispub)
669 const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
671 nbyte = RSA_size(rsa);
672 hnbyte = (RSA_bits(rsa) + 15) >> 4;
673 RSA_get0_key(rsa, &n, &e, &d);
674 write_lebn(out, e, 4);
675 write_lebn(out, n, nbyte);
678 RSA_get0_factors(rsa, &p, &q);
679 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
680 write_lebn(out, p, hnbyte);
681 write_lebn(out, q, hnbyte);
682 write_lebn(out, dmp1, hnbyte);
683 write_lebn(out, dmq1, hnbyte);
684 write_lebn(out, iqmp, hnbyte);
685 write_lebn(out, d, nbyte);
688 #ifndef OPENSSL_NO_DSA
689 static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *pmagic)
692 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
693 const BIGNUM *pub_key = NULL, *priv_key = NULL;
695 DSA_get0_pqg(dsa, &p, &q, &g);
696 DSA_get0_key(dsa, &pub_key, &priv_key);
697 bitlen = BN_num_bits(p);
698 if ((bitlen & 7) || (BN_num_bits(q) != 160)
699 || (BN_num_bits(g) > bitlen))
702 if (BN_num_bits(pub_key) > bitlen)
704 *pmagic = MS_DSS1MAGIC;
706 if (BN_num_bits(priv_key) > 160)
708 *pmagic = MS_DSS2MAGIC;
713 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
717 static void write_dsa(unsigned char **out, const DSA *dsa, int ispub)
720 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
721 const BIGNUM *pub_key = NULL, *priv_key = NULL;
723 DSA_get0_pqg(dsa, &p, &q, &g);
724 DSA_get0_key(dsa, &pub_key, &priv_key);
725 nbyte = BN_num_bytes(p);
726 write_lebn(out, p, nbyte);
727 write_lebn(out, q, 20);
728 write_lebn(out, g, nbyte);
730 write_lebn(out, pub_key, nbyte);
732 write_lebn(out, priv_key, 20);
733 /* Set "invalid" for seed structure values */
734 memset(*out, 0xff, 24);
740 int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk)
742 return do_i2b_bio(out, pk, 0);
745 int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk)
747 return do_i2b_bio(out, pk, 1);
750 int ossl_do_PVK_header(const unsigned char **in, unsigned int length,
752 unsigned int *psaltlen, unsigned int *pkeylen)
754 const unsigned char *p = *in;
755 unsigned int pvk_magic, is_encrypted;
759 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
764 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
767 pvk_magic = read_ledword(&p);
768 if (pvk_magic != MS_PVKMAGIC) {
769 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
778 is_encrypted = read_ledword(&p);
779 *psaltlen = read_ledword(&p);
780 *pkeylen = read_ledword(&p);
782 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
785 if (is_encrypted && *psaltlen == 0) {
786 ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER);
794 #ifndef OPENSSL_NO_RC4
795 static int derive_pvk_key(unsigned char *key, size_t keylen,
796 const unsigned char *salt, unsigned int saltlen,
797 const unsigned char *pass, int passlen,
798 OSSL_LIB_CTX *libctx, const char *propq)
802 OSSL_PARAM params[5], *p = params;
805 if ((kdf = EVP_KDF_fetch(libctx, "PVKKDF", propq)) == NULL)
807 ctx = EVP_KDF_CTX_new(kdf);
812 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
813 (void *)salt, saltlen);
814 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
815 (void *)pass, passlen);
816 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, SN_sha1, 0);
817 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
819 *p = OSSL_PARAM_construct_end();
821 rv = EVP_KDF_derive(ctx, key, keylen, params);
822 EVP_KDF_CTX_free(ctx);
827 static void *do_PVK_body_key(const unsigned char **in,
828 unsigned int saltlen, unsigned int keylen,
829 pem_password_cb *cb, void *u,
830 int *isdss, int *ispub,
831 OSSL_LIB_CTX *libctx, const char *propq)
833 const unsigned char *p = *in;
834 unsigned char *enctmp = NULL;
835 unsigned char keybuf[20];
837 #ifndef OPENSSL_NO_RC4
838 EVP_CIPHER *rc4 = NULL;
840 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
843 #ifndef OPENSSL_NO_RC4
845 char psbuf[PEM_BUFSIZE];
846 int enctmplen, inlen;
850 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
852 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
854 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
857 enctmp = OPENSSL_malloc(keylen + 8);
858 if (enctmp == NULL) {
859 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
862 if (!derive_pvk_key(keybuf, sizeof(keybuf), p, saltlen,
863 (unsigned char *)psbuf, inlen, libctx, propq))
866 /* Copy BLOBHEADER across, decrypt rest */
867 memcpy(enctmp, p, 8);
870 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
875 if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
877 if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
879 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
881 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
883 magic = read_ledword((const unsigned char **)&q);
884 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
886 memset(keybuf + 5, 0, 11);
887 if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
889 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
891 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
893 magic = read_ledword((const unsigned char **)&q);
894 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
895 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT);
901 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
906 key = do_b2i_key(&p, keylen, isdss, ispub);
908 EVP_CIPHER_CTX_free(cctx);
909 #ifndef OPENSSL_NO_RC4
910 EVP_CIPHER_free(rc4);
912 if (enctmp != NULL) {
913 OPENSSL_cleanse(keybuf, sizeof(keybuf));
914 OPENSSL_free(enctmp);
919 static void *do_PVK_key_bio(BIO *in, pem_password_cb *cb, void *u,
920 int *isdss, int *ispub,
921 OSSL_LIB_CTX *libctx, const char *propq)
923 unsigned char pvk_hdr[24], *buf = NULL;
924 const unsigned char *p;
927 unsigned int saltlen, keylen;
929 if (BIO_read(in, pvk_hdr, 24) != 24) {
930 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
935 if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen))
937 buflen = (int)keylen + saltlen;
938 buf = OPENSSL_malloc(buflen);
940 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
944 if (BIO_read(in, buf, buflen) != buflen) {
945 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
948 key = do_PVK_body_key(&p, saltlen, keylen, cb, u, isdss, ispub, libctx, propq);
951 OPENSSL_clear_free(buf, buflen);
955 #ifndef OPENSSL_NO_DSA
956 DSA *b2i_DSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
957 OSSL_LIB_CTX *libctx, const char *propq)
960 int ispub = 0; /* PVK keys are always private */
962 return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
965 DSA *b2i_DSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
967 return b2i_DSA_PVK_bio_ex(in, cb, u, NULL, NULL);
971 RSA *b2i_RSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
972 OSSL_LIB_CTX *libctx, const char *propq)
975 int ispub = 0; /* PVK keys are always private */
977 return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
980 RSA *b2i_RSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
982 return b2i_RSA_PVK_bio_ex(in, cb, u, NULL, NULL);
985 EVP_PKEY *b2i_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
986 OSSL_LIB_CTX *libctx, const char *propq)
990 void *key = do_PVK_key_bio(in, cb, u, &isdss, &ispub, NULL, NULL);
992 return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
995 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
997 return b2i_PVK_bio_ex(in, cb, u, NULL, NULL);
1000 static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel,
1001 pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
1005 int outlen = 24, pklen;
1006 unsigned char *p = NULL, *start = NULL;
1007 EVP_CIPHER_CTX *cctx = NULL;
1008 #ifndef OPENSSL_NO_RC4
1009 unsigned char *salt = NULL;
1010 EVP_CIPHER *rc4 = NULL;
1014 outlen += PVK_SALTLEN;
1015 pklen = do_i2b(NULL, pk, 0);
1024 start = p = OPENSSL_malloc(outlen);
1026 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
1031 cctx = EVP_CIPHER_CTX_new();
1035 write_ledword(&p, MS_PVKMAGIC);
1036 write_ledword(&p, 0);
1037 if (EVP_PKEY_get_id(pk) == EVP_PKEY_RSA)
1038 write_ledword(&p, MS_KEYTYPE_KEYX);
1039 #ifndef OPENSSL_NO_DSA
1041 write_ledword(&p, MS_KEYTYPE_SIGN);
1043 write_ledword(&p, enclevel ? 1 : 0);
1044 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
1045 write_ledword(&p, pklen);
1047 #ifndef OPENSSL_NO_RC4
1048 if (RAND_bytes_ex(libctx, p, PVK_SALTLEN, 0) <= 0)
1055 if (enclevel != 0) {
1056 #ifndef OPENSSL_NO_RC4
1057 char psbuf[PEM_BUFSIZE];
1058 unsigned char keybuf[20];
1059 int enctmplen, inlen;
1061 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
1063 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
1065 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
1068 if (!derive_pvk_key(keybuf, sizeof(keybuf), salt, PVK_SALTLEN,
1069 (unsigned char *)psbuf, inlen, libctx, propq))
1071 if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
1074 memset(keybuf + 5, 0, 11);
1075 p = salt + PVK_SALTLEN + 8;
1076 if (!EVP_EncryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
1078 OPENSSL_cleanse(keybuf, 20);
1079 if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
1081 if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen))
1084 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
1093 EVP_CIPHER_CTX_free(cctx);
1094 #ifndef OPENSSL_NO_RC4
1095 EVP_CIPHER_free(rc4);
1098 OPENSSL_free(start);
1103 int i2b_PVK_bio_ex(BIO *out, const EVP_PKEY *pk, int enclevel,
1104 pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
1107 unsigned char *tmp = NULL;
1110 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u, libctx, propq);
1113 wrlen = BIO_write(out, tmp, outlen);
1115 if (wrlen == outlen) {
1118 ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE);
1122 int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
1123 pem_password_cb *cb, void *u)
1125 return i2b_PVK_bio_ex(out, pk, enclevel, cb, u, NULL, NULL);