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 "internal/cryptlib.h"
27 #include "crypto/pem.h"
28 #include "crypto/evp.h"
31 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
35 static unsigned int read_ledword(const unsigned char **in)
37 const unsigned char *p = *in;
40 ret = (unsigned int)*p++;
41 ret |= (unsigned int)*p++ << 8;
42 ret |= (unsigned int)*p++ << 16;
43 ret |= (unsigned int)*p++ << 24;
49 * Read a BIGNUM in little endian format. The docs say that this should take
53 static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
55 *r = BN_lebin2bn(*in, nbyte, NULL);
63 * Create an EVP_PKEY from a type specific key.
64 * This takes ownership of |key|, as long as the |evp_type| is acceptable
65 * (EVP_PKEY_RSA or EVP_PKEY_DSA), even if the resulting EVP_PKEY wasn't
68 #define isdss_to_evp_type(isdss) \
69 (isdss == 0 ? EVP_PKEY_RSA : isdss == 1 ? EVP_PKEY_DSA : EVP_PKEY_NONE)
70 static EVP_PKEY *evp_pkey_new0_key(void *key, int evp_type)
72 EVP_PKEY *pkey = NULL;
75 * It's assumed that if |key| is NULL, something went wrong elsewhere
76 * and suitable errors are already reported.
81 if (!ossl_assert(evp_type == EVP_PKEY_RSA || evp_type == EVP_PKEY_DSA)) {
82 ERR_raise(ERR_LIB_PEM, ERR_R_INTERNAL_ERROR);
86 if ((pkey = EVP_PKEY_new()) != NULL) {
89 if (EVP_PKEY_set1_RSA(pkey, key))
94 #ifndef OPENSSL_NO_DSA
96 if (EVP_PKEY_set1_DSA(pkey, key))
109 #ifndef OPENSSL_NO_DSA
117 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
121 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
123 # define MS_PUBLICKEYBLOB 0x6
124 # define MS_PRIVATEKEYBLOB 0x7
125 # define MS_RSA1MAGIC 0x31415352L
126 # define MS_RSA2MAGIC 0x32415352L
127 # define MS_DSS1MAGIC 0x31535344L
128 # define MS_DSS2MAGIC 0x32535344L
130 # define MS_KEYALG_RSA_KEYX 0xa400
131 # define MS_KEYALG_DSS_SIGN 0x2200
133 # define MS_KEYTYPE_KEYX 0x1
134 # define MS_KEYTYPE_SIGN 0x2
136 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
137 # define MS_PVKMAGIC 0xb0b5f11eL
138 /* Salt length for PVK files */
139 # define PVK_SALTLEN 0x10
140 /* Maximum length in PVK header */
141 # define PVK_MAX_KEYLEN 102400
142 /* Maximum salt length */
143 # define PVK_MAX_SALTLEN 10240
146 * Read the MSBLOB header and get relevant data from it.
148 * |pisdss| and |pispub| have a double role, as they can be used for
149 * discovery as well as to check the the blob meets expectations.
150 * |*pisdss| is the indicator for whether the key is a DSA key or not.
151 * |*pispub| is the indicator for whether the key is public or not.
152 * In both cases, the following input values apply:
154 * 0 Expected to not be what the variable indicates.
155 * 1 Expected to be what the variable indicates.
156 * -1 No expectations, this function will assign 0 or 1 depending on
159 int ossl_do_blob_header(const unsigned char **in, unsigned int length,
160 unsigned int *pmagic, unsigned int *pbitlen,
161 int *pisdss, int *pispub)
163 const unsigned char *p = *in;
169 case MS_PUBLICKEYBLOB:
171 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
177 case MS_PRIVATEKEYBLOB:
179 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
191 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER);
194 /* Ignore reserved, aiKeyAlg */
196 *pmagic = read_ledword(&p);
197 *pbitlen = read_ledword(&p);
199 /* Consistency check for private vs public */
204 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
212 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
218 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
222 /* Check that we got the expected type */
227 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_DSS_KEY_BLOB);
235 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_RSA_KEY_BLOB);
242 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
249 unsigned int ossl_blob_length(unsigned bitlen, int isdss, int ispub)
251 unsigned int nbyte = (bitlen + 7) >> 3;
252 unsigned int hnbyte = (bitlen + 15) >> 4;
257 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
261 return 44 + 3 * nbyte;
263 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
267 return 64 + 2 * nbyte;
269 /* Expected length: 4 for 'e' + 'n' */
274 * Expected length: 4 for 'e' and 7 other components. 2
275 * components are bitlen size, 5 are bitlen/2
277 return 4 + 2 * nbyte + 5 * hnbyte;
282 static void *do_b2i_key(const unsigned char **in, unsigned int length,
283 int *isdss, int *ispub)
285 const unsigned char *p = *in;
286 unsigned int bitlen, magic;
289 if (ossl_do_blob_header(&p, length, &magic, &bitlen, isdss, ispub) <= 0) {
290 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
294 if (length < ossl_blob_length(bitlen, *isdss, *ispub)) {
295 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
299 key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
300 #ifndef OPENSSL_NO_DSA
302 key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
306 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
313 EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub)
316 void *key = do_b2i_key(in, length, &isdss, ispub);
318 return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
321 EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub)
323 const unsigned char *p;
324 unsigned char hdr_buf[16], *buf = NULL;
325 unsigned int bitlen, magic, length;
328 EVP_PKEY *pkey = NULL;
330 if (BIO_read(in, hdr_buf, 16) != 16) {
331 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
335 if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0)
338 length = ossl_blob_length(bitlen, isdss, *ispub);
339 if (length > BLOB_MAX_LENGTH) {
340 ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
343 buf = OPENSSL_malloc(length);
345 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
349 if (BIO_read(in, buf, length) != (int)length) {
350 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
355 key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
356 #ifndef OPENSSL_NO_DSA
358 key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
362 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
366 pkey = evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
372 #ifndef OPENSSL_NO_DSA
373 DSA *ossl_b2i_DSA_after_header(const unsigned char **in, unsigned int bitlen,
376 const unsigned char *p = *in;
379 BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
380 BIGNUM *pub_key = NULL;
381 unsigned int nbyte = (bitlen + 7) >> 3;
386 if (!read_lebn(&p, nbyte, &pbn))
389 if (!read_lebn(&p, 20, &qbn))
392 if (!read_lebn(&p, nbyte, &gbn))
396 if (!read_lebn(&p, nbyte, &pub_key))
399 if (!read_lebn(&p, 20, &priv_key))
402 /* Set constant time flag before public key calculation */
403 BN_set_flags(priv_key, BN_FLG_CONSTTIME);
405 /* Calculate public key */
409 if ((ctx = BN_CTX_new()) == NULL)
412 if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
418 if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
420 pbn = qbn = gbn = NULL;
421 if (!DSA_set0_key(dsa, pub_key, priv_key))
423 pub_key = priv_key = NULL;
429 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
441 RSA *ossl_b2i_RSA_after_header(const unsigned char **in, unsigned int bitlen,
444 const unsigned char *pin = *in;
445 BIGNUM *e = NULL, *n = NULL, *d = NULL;
446 BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
448 unsigned int nbyte = (bitlen + 7) >> 3;
449 unsigned int hnbyte = (bitlen + 15) >> 4;
457 if (!BN_set_word(e, read_ledword(&pin)))
459 if (!read_lebn(&pin, nbyte, &n))
462 if (!read_lebn(&pin, hnbyte, &p))
464 if (!read_lebn(&pin, hnbyte, &q))
466 if (!read_lebn(&pin, hnbyte, &dmp1))
468 if (!read_lebn(&pin, hnbyte, &dmq1))
470 if (!read_lebn(&pin, hnbyte, &iqmp))
472 if (!read_lebn(&pin, nbyte, &d))
474 if (!RSA_set0_factors(rsa, p, q))
477 if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
479 dmp1 = dmq1 = iqmp = NULL;
481 if (!RSA_set0_key(rsa, n, e, d))
488 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
501 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
505 return ossl_b2i(in, length, &ispub);
508 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
512 return ossl_b2i(in, length, &ispub);
515 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
519 return ossl_b2i_bio(in, &ispub);
522 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
526 return ossl_b2i_bio(in, &ispub);
529 static void write_ledword(unsigned char **out, unsigned int dw)
531 unsigned char *p = *out;
534 *p++ = (dw >> 8) & 0xff;
535 *p++ = (dw >> 16) & 0xff;
536 *p++ = (dw >> 24) & 0xff;
540 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
542 BN_bn2lebinpad(bn, *out, len);
546 static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *magic);
547 static void write_rsa(unsigned char **out, const RSA *rsa, int ispub);
549 #ifndef OPENSSL_NO_DSA
550 static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *magic);
551 static void write_dsa(unsigned char **out, const DSA *dsa, int ispub);
554 static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub)
557 unsigned int bitlen = 0, magic = 0, keyalg = 0;
558 int outlen = -1, noinc = 0;
560 if (EVP_PKEY_is_a(pk, "RSA")) {
561 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
562 keyalg = MS_KEYALG_RSA_KEYX;
563 #ifndef OPENSSL_NO_DSA
564 } else if (EVP_PKEY_is_a(pk, "DSA")) {
565 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
566 keyalg = MS_KEYALG_DSS_SIGN;
573 + ossl_blob_length(bitlen, keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
579 if ((p = OPENSSL_malloc(outlen)) == NULL) {
580 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
588 *p++ = MS_PUBLICKEYBLOB;
590 *p++ = MS_PRIVATEKEYBLOB;
594 write_ledword(&p, keyalg);
595 write_ledword(&p, magic);
596 write_ledword(&p, bitlen);
597 if (keyalg == MS_KEYALG_RSA_KEYX)
598 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
599 #ifndef OPENSSL_NO_DSA
601 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
609 static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub)
611 unsigned char *tmp = NULL;
614 outlen = do_i2b(&tmp, pk, ispub);
617 wrlen = BIO_write(out, tmp, outlen);
624 static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *pmagic)
626 int nbyte, hnbyte, bitlen;
629 RSA_get0_key(rsa, NULL, &e, NULL);
630 if (BN_num_bits(e) > 32)
632 bitlen = RSA_bits(rsa);
633 nbyte = RSA_size(rsa);
634 hnbyte = (bitlen + 15) >> 4;
636 *pmagic = MS_RSA1MAGIC;
639 const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
641 *pmagic = MS_RSA2MAGIC;
644 * For private key each component must fit within nbyte or hnbyte.
646 RSA_get0_key(rsa, NULL, NULL, &d);
647 if (BN_num_bytes(d) > nbyte)
649 RSA_get0_factors(rsa, &p, &q);
650 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
651 if ((BN_num_bytes(iqmp) > hnbyte)
652 || (BN_num_bytes(p) > hnbyte)
653 || (BN_num_bytes(q) > hnbyte)
654 || (BN_num_bytes(dmp1) > hnbyte)
655 || (BN_num_bytes(dmq1) > hnbyte))
660 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
664 static void write_rsa(unsigned char **out, const RSA *rsa, int ispub)
667 const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
669 nbyte = RSA_size(rsa);
670 hnbyte = (RSA_bits(rsa) + 15) >> 4;
671 RSA_get0_key(rsa, &n, &e, &d);
672 write_lebn(out, e, 4);
673 write_lebn(out, n, nbyte);
676 RSA_get0_factors(rsa, &p, &q);
677 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
678 write_lebn(out, p, hnbyte);
679 write_lebn(out, q, hnbyte);
680 write_lebn(out, dmp1, hnbyte);
681 write_lebn(out, dmq1, hnbyte);
682 write_lebn(out, iqmp, hnbyte);
683 write_lebn(out, d, nbyte);
686 #ifndef OPENSSL_NO_DSA
687 static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *pmagic)
690 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
691 const BIGNUM *pub_key = NULL, *priv_key = NULL;
693 DSA_get0_pqg(dsa, &p, &q, &g);
694 DSA_get0_key(dsa, &pub_key, &priv_key);
695 bitlen = BN_num_bits(p);
696 if ((bitlen & 7) || (BN_num_bits(q) != 160)
697 || (BN_num_bits(g) > bitlen))
700 if (BN_num_bits(pub_key) > bitlen)
702 *pmagic = MS_DSS1MAGIC;
704 if (BN_num_bits(priv_key) > 160)
706 *pmagic = MS_DSS2MAGIC;
711 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
715 static void write_dsa(unsigned char **out, const DSA *dsa, int ispub)
718 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
719 const BIGNUM *pub_key = NULL, *priv_key = NULL;
721 DSA_get0_pqg(dsa, &p, &q, &g);
722 DSA_get0_key(dsa, &pub_key, &priv_key);
723 nbyte = BN_num_bytes(p);
724 write_lebn(out, p, nbyte);
725 write_lebn(out, q, 20);
726 write_lebn(out, g, nbyte);
728 write_lebn(out, pub_key, nbyte);
730 write_lebn(out, priv_key, 20);
731 /* Set "invalid" for seed structure values */
732 memset(*out, 0xff, 24);
738 int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk)
740 return do_i2b_bio(out, pk, 0);
743 int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk)
745 return do_i2b_bio(out, pk, 1);
748 int ossl_do_PVK_header(const unsigned char **in, unsigned int length,
750 unsigned int *psaltlen, unsigned int *pkeylen)
752 const unsigned char *p = *in;
753 unsigned int pvk_magic, is_encrypted;
757 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
762 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
765 pvk_magic = read_ledword(&p);
766 if (pvk_magic != MS_PVKMAGIC) {
767 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
776 is_encrypted = read_ledword(&p);
777 *psaltlen = read_ledword(&p);
778 *pkeylen = read_ledword(&p);
780 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
783 if (is_encrypted && *psaltlen == 0) {
784 ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER);
792 #ifndef OPENSSL_NO_RC4
793 static int derive_pvk_key(unsigned char *key,
794 const unsigned char *salt, unsigned int saltlen,
795 const unsigned char *pass, int passlen,
796 OSSL_LIB_CTX *libctx, const char *propq)
798 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
802 if ((sha1 = EVP_MD_fetch(libctx, SN_sha1, propq)) == NULL)
806 || !EVP_DigestInit_ex(mctx, sha1, NULL)
807 || !EVP_DigestUpdate(mctx, salt, saltlen)
808 || !EVP_DigestUpdate(mctx, pass, passlen)
809 || !EVP_DigestFinal_ex(mctx, key, NULL))
814 EVP_MD_CTX_free(mctx);
820 static void *do_PVK_body_key(const unsigned char **in,
821 unsigned int saltlen, unsigned int keylen,
822 pem_password_cb *cb, void *u,
823 int *isdss, int *ispub,
824 OSSL_LIB_CTX *libctx, const char *propq)
826 const unsigned char *p = *in;
827 unsigned char *enctmp = NULL;
828 unsigned char keybuf[20];
830 #ifndef OPENSSL_NO_RC4
831 EVP_CIPHER *rc4 = NULL;
833 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
836 #ifndef OPENSSL_NO_RC4
838 char psbuf[PEM_BUFSIZE];
839 int enctmplen, inlen;
843 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
845 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
847 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
850 enctmp = OPENSSL_malloc(keylen + 8);
851 if (enctmp == NULL) {
852 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
855 if (!derive_pvk_key(keybuf, p, saltlen,
856 (unsigned char *)psbuf, inlen, libctx, propq))
859 /* Copy BLOBHEADER across, decrypt rest */
860 memcpy(enctmp, p, 8);
863 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
868 if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
870 if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
872 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
874 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
876 magic = read_ledword((const unsigned char **)&q);
877 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
879 memset(keybuf + 5, 0, 11);
880 if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
882 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
884 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
886 magic = read_ledword((const unsigned char **)&q);
887 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
888 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT);
894 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
899 key = do_b2i_key(&p, keylen, isdss, ispub);
901 EVP_CIPHER_CTX_free(cctx);
902 #ifndef OPENSSL_NO_RC4
903 EVP_CIPHER_free(rc4);
905 if (enctmp != NULL) {
906 OPENSSL_cleanse(keybuf, sizeof(keybuf));
907 OPENSSL_free(enctmp);
912 static void *do_PVK_key_bio(BIO *in, pem_password_cb *cb, void *u,
913 int *isdss, int *ispub,
914 OSSL_LIB_CTX *libctx, const char *propq)
916 unsigned char pvk_hdr[24], *buf = NULL;
917 const unsigned char *p;
920 unsigned int saltlen, keylen;
922 if (BIO_read(in, pvk_hdr, 24) != 24) {
923 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
928 if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen))
930 buflen = (int)keylen + saltlen;
931 buf = OPENSSL_malloc(buflen);
933 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
937 if (BIO_read(in, buf, buflen) != buflen) {
938 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
941 key = do_PVK_body_key(&p, saltlen, keylen, cb, u, isdss, ispub, libctx, propq);
944 OPENSSL_clear_free(buf, buflen);
948 #ifndef OPENSSL_NO_DSA
949 DSA *b2i_DSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
950 OSSL_LIB_CTX *libctx, const char *propq)
953 int ispub = 0; /* PVK keys are always private */
955 return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
958 DSA *b2i_DSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
960 return b2i_DSA_PVK_bio_ex(in, cb, u, NULL, NULL);
964 RSA *b2i_RSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
965 OSSL_LIB_CTX *libctx, const char *propq)
968 int ispub = 0; /* PVK keys are always private */
970 return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
973 RSA *b2i_RSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
975 return b2i_RSA_PVK_bio_ex(in, cb, u, NULL, NULL);
978 EVP_PKEY *b2i_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
979 OSSL_LIB_CTX *libctx, const char *propq)
983 void *key = do_PVK_key_bio(in, cb, u, &isdss, &ispub, NULL, NULL);
985 return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
988 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
990 return b2i_PVK_bio_ex(in, cb, u, NULL, NULL);
993 static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel,
994 pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
998 int outlen = 24, pklen;
999 unsigned char *p = NULL, *start = NULL;
1000 EVP_CIPHER_CTX *cctx = NULL;
1001 #ifndef OPENSSL_NO_RC4
1002 unsigned char *salt = NULL;
1003 EVP_CIPHER *rc4 = NULL;
1007 outlen += PVK_SALTLEN;
1008 pklen = do_i2b(NULL, pk, 0);
1017 start = p = OPENSSL_malloc(outlen);
1019 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
1024 cctx = EVP_CIPHER_CTX_new();
1028 write_ledword(&p, MS_PVKMAGIC);
1029 write_ledword(&p, 0);
1030 if (EVP_PKEY_id(pk) == EVP_PKEY_RSA)
1031 write_ledword(&p, MS_KEYTYPE_KEYX);
1032 #ifndef OPENSSL_NO_DSA
1034 write_ledword(&p, MS_KEYTYPE_SIGN);
1036 write_ledword(&p, enclevel ? 1 : 0);
1037 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
1038 write_ledword(&p, pklen);
1040 #ifndef OPENSSL_NO_RC4
1041 if (RAND_bytes_ex(libctx, p, PVK_SALTLEN, 0) <= 0)
1048 if (enclevel != 0) {
1049 #ifndef OPENSSL_NO_RC4
1050 char psbuf[PEM_BUFSIZE];
1051 unsigned char keybuf[20];
1052 int enctmplen, inlen;
1054 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
1056 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
1058 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
1061 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
1062 (unsigned char *)psbuf, inlen, libctx, propq))
1064 if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
1067 memset(keybuf + 5, 0, 11);
1068 p = salt + PVK_SALTLEN + 8;
1069 if (!EVP_EncryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
1071 OPENSSL_cleanse(keybuf, 20);
1072 if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
1074 if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen))
1077 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
1086 EVP_CIPHER_CTX_free(cctx);
1087 #ifndef OPENSSL_NO_RC4
1088 EVP_CIPHER_free(rc4);
1091 OPENSSL_free(start);
1096 int i2b_PVK_bio_ex(BIO *out, const EVP_PKEY *pk, int enclevel,
1097 pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
1100 unsigned char *tmp = NULL;
1103 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u, libctx, propq);
1106 wrlen = BIO_write(out, tmp, outlen);
1108 if (wrlen == outlen) {
1111 ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE);
1115 int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
1116 pem_password_cb *cb, void *u)
1118 return i2b_PVK_bio_ex(out, pk, enclevel, cb, u, NULL, NULL);