2 * Copyright 2005-2020 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 "internal/cryptlib.h"
22 #include <openssl/pem.h>
23 #include "crypto/pem.h"
24 #include <openssl/rand.h>
25 #include <openssl/bn.h>
26 #include <openssl/dsa.h>
27 #include <openssl/rsa.h>
30 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
34 static unsigned int read_ledword(const unsigned char **in)
36 const unsigned char *p = *in;
39 ret = (unsigned int)*p++;
40 ret |= (unsigned int)*p++ << 8;
41 ret |= (unsigned int)*p++ << 16;
42 ret |= (unsigned int)*p++ << 24;
48 * Read a BIGNUM in little endian format. The docs say that this should take
52 static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
54 *r = BN_lebin2bn(*in, nbyte, NULL);
61 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
63 # define MS_PUBLICKEYBLOB 0x6
64 # define MS_PRIVATEKEYBLOB 0x7
65 # define MS_RSA1MAGIC 0x31415352L
66 # define MS_RSA2MAGIC 0x32415352L
67 # define MS_DSS1MAGIC 0x31535344L
68 # define MS_DSS2MAGIC 0x32535344L
70 # define MS_KEYALG_RSA_KEYX 0xa400
71 # define MS_KEYALG_DSS_SIGN 0x2200
73 # define MS_KEYTYPE_KEYX 0x1
74 # define MS_KEYTYPE_SIGN 0x2
76 /* Maximum length of a blob after header */
77 # define BLOB_MAX_LENGTH 102400
79 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
80 # define MS_PVKMAGIC 0xb0b5f11eL
81 /* Salt length for PVK files */
82 # define PVK_SALTLEN 0x10
83 /* Maximum length in PVK header */
84 # define PVK_MAX_KEYLEN 102400
85 /* Maximum salt length */
86 # define PVK_MAX_SALTLEN 10240
88 static EVP_PKEY *b2i_rsa(const unsigned char **in,
89 unsigned int bitlen, int ispub);
90 #ifndef OPENSSL_NO_DSA
91 static EVP_PKEY *b2i_dss(const unsigned char **in,
92 unsigned int bitlen, int ispub);
95 int ossl_do_blob_header(const unsigned char **in, unsigned int length,
96 unsigned int *pmagic, unsigned int *pbitlen,
97 int *pisdss, int *pispub)
99 const unsigned char *p = *in;
104 if (*p == MS_PUBLICKEYBLOB) {
106 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
110 } else if (*p == MS_PRIVATEKEYBLOB) {
112 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
122 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER);
125 /* Ignore reserved, aiKeyAlg */
127 *pmagic = read_ledword(&p);
128 *pbitlen = read_ledword(&p);
137 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
147 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
153 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
160 static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
162 unsigned int nbyte = (bitlen + 7) >> 3;
163 unsigned int hnbyte = (bitlen + 15) >> 4;
168 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
172 return 44 + 3 * nbyte;
174 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
178 return 64 + 2 * nbyte;
180 /* Expected length: 4 for 'e' + 'n' */
185 * Expected length: 4 for 'e' and 7 other components. 2
186 * components are bitlen size, 5 are bitlen/2
188 return 4 + 2 * nbyte + 5 * hnbyte;
193 EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub)
195 const unsigned char *p = *in;
196 unsigned int bitlen, magic;
199 if (ossl_do_blob_header(&p, length, &magic, &bitlen, &isdss, ispub) <= 0) {
200 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
204 if (length < blob_length(bitlen, isdss, *ispub)) {
205 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
209 return b2i_rsa(&p, bitlen, *ispub);
210 #ifndef OPENSSL_NO_DSA
212 return b2i_dss(&p, bitlen, *ispub);
215 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
219 EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub)
221 const unsigned char *p;
222 unsigned char hdr_buf[16], *buf = NULL;
223 unsigned int bitlen, magic, length;
225 EVP_PKEY *ret = NULL;
227 if (BIO_read(in, hdr_buf, 16) != 16) {
228 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
232 if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0)
235 length = blob_length(bitlen, isdss, *ispub);
236 if (length > BLOB_MAX_LENGTH) {
237 ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
240 buf = OPENSSL_malloc(length);
242 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
246 if (BIO_read(in, buf, length) != (int)length) {
247 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
252 ret = b2i_rsa(&p, bitlen, *ispub);
253 #ifndef OPENSSL_NO_DSA
255 ret = b2i_dss(&p, bitlen, *ispub);
259 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
266 #ifndef OPENSSL_NO_DSA
267 static EVP_PKEY *b2i_dss(const unsigned char **in,
268 unsigned int bitlen, int ispub)
270 const unsigned char *p = *in;
271 EVP_PKEY *ret = NULL;
274 BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
275 BIGNUM *pub_key = NULL;
276 unsigned int nbyte = (bitlen + 7) >> 3;
279 ret = EVP_PKEY_new();
280 if (dsa == NULL || ret == NULL)
282 if (!read_lebn(&p, nbyte, &pbn))
285 if (!read_lebn(&p, 20, &qbn))
288 if (!read_lebn(&p, nbyte, &gbn))
292 if (!read_lebn(&p, nbyte, &pub_key))
295 if (!read_lebn(&p, 20, &priv_key))
298 /* Set constant time flag before public key calculation */
299 BN_set_flags(priv_key, BN_FLG_CONSTTIME);
301 /* Calculate public key */
305 if ((ctx = BN_CTX_new()) == NULL)
308 if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
314 if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
316 pbn = qbn = gbn = NULL;
317 if (!DSA_set0_key(dsa, pub_key, priv_key))
319 pub_key = priv_key = NULL;
321 if (!EVP_PKEY_set1_DSA(ret, dsa))
328 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
341 static EVP_PKEY *b2i_rsa(const unsigned char **in,
342 unsigned int bitlen, int ispub)
344 const unsigned char *pin = *in;
345 EVP_PKEY *ret = NULL;
346 BIGNUM *e = NULL, *n = NULL, *d = NULL;
347 BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
349 unsigned int nbyte = (bitlen + 7) >> 3;
350 unsigned int hnbyte = (bitlen + 15) >> 4;
353 ret = EVP_PKEY_new();
354 if (rsa == NULL || ret == NULL)
359 if (!BN_set_word(e, read_ledword(&pin)))
361 if (!read_lebn(&pin, nbyte, &n))
364 if (!read_lebn(&pin, hnbyte, &p))
366 if (!read_lebn(&pin, hnbyte, &q))
368 if (!read_lebn(&pin, hnbyte, &dmp1))
370 if (!read_lebn(&pin, hnbyte, &dmq1))
372 if (!read_lebn(&pin, hnbyte, &iqmp))
374 if (!read_lebn(&pin, nbyte, &d))
376 if (!RSA_set0_factors(rsa, p, q))
379 if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
381 dmp1 = dmq1 = iqmp = NULL;
383 if (!RSA_set0_key(rsa, n, e, d))
387 if (!EVP_PKEY_set1_RSA(ret, rsa))
393 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
407 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
411 return ossl_b2i(in, length, &ispub);
414 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
418 return ossl_b2i(in, length, &ispub);
421 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
425 return ossl_b2i_bio(in, &ispub);
428 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
432 return ossl_b2i_bio(in, &ispub);
435 static void write_ledword(unsigned char **out, unsigned int dw)
437 unsigned char *p = *out;
440 *p++ = (dw >> 8) & 0xff;
441 *p++ = (dw >> 16) & 0xff;
442 *p++ = (dw >> 24) & 0xff;
446 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
448 BN_bn2lebinpad(bn, *out, len);
452 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
453 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
455 #ifndef OPENSSL_NO_DSA
456 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
457 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
460 static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub)
463 unsigned int bitlen = 0, magic = 0, keyalg = 0;
464 int outlen, noinc = 0;
465 int pktype = EVP_PKEY_id(pk);
467 if (pktype == EVP_PKEY_RSA) {
468 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
469 keyalg = MS_KEYALG_RSA_KEYX;
470 #ifndef OPENSSL_NO_DSA
471 } else if (pktype == EVP_PKEY_DSA) {
472 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
473 keyalg = MS_KEYALG_DSS_SIGN;
478 outlen = 16 + blob_length(bitlen,
479 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
485 if ((p = OPENSSL_malloc(outlen)) == NULL) {
486 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
493 *p++ = MS_PUBLICKEYBLOB;
495 *p++ = MS_PRIVATEKEYBLOB;
499 write_ledword(&p, keyalg);
500 write_ledword(&p, magic);
501 write_ledword(&p, bitlen);
502 if (keyalg == MS_KEYALG_RSA_KEYX)
503 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
504 #ifndef OPENSSL_NO_DSA
506 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
513 static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub)
515 unsigned char *tmp = NULL;
518 outlen = do_i2b(&tmp, pk, ispub);
521 wrlen = BIO_write(out, tmp, outlen);
528 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
530 int nbyte, hnbyte, bitlen;
533 RSA_get0_key(rsa, NULL, &e, NULL);
534 if (BN_num_bits(e) > 32)
536 bitlen = RSA_bits(rsa);
537 nbyte = RSA_size(rsa);
538 hnbyte = (bitlen + 15) >> 4;
540 *pmagic = MS_RSA1MAGIC;
543 const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
545 *pmagic = MS_RSA2MAGIC;
548 * For private key each component must fit within nbyte or hnbyte.
550 RSA_get0_key(rsa, NULL, NULL, &d);
551 if (BN_num_bytes(d) > nbyte)
553 RSA_get0_factors(rsa, &p, &q);
554 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
555 if ((BN_num_bytes(iqmp) > hnbyte)
556 || (BN_num_bytes(p) > hnbyte)
557 || (BN_num_bytes(q) > hnbyte)
558 || (BN_num_bytes(dmp1) > hnbyte)
559 || (BN_num_bytes(dmq1) > hnbyte))
564 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
568 static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
571 const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
573 nbyte = RSA_size(rsa);
574 hnbyte = (RSA_bits(rsa) + 15) >> 4;
575 RSA_get0_key(rsa, &n, &e, &d);
576 write_lebn(out, e, 4);
577 write_lebn(out, n, nbyte);
580 RSA_get0_factors(rsa, &p, &q);
581 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
582 write_lebn(out, p, hnbyte);
583 write_lebn(out, q, hnbyte);
584 write_lebn(out, dmp1, hnbyte);
585 write_lebn(out, dmq1, hnbyte);
586 write_lebn(out, iqmp, hnbyte);
587 write_lebn(out, d, nbyte);
590 #ifndef OPENSSL_NO_DSA
591 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
594 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
595 const BIGNUM *pub_key = NULL, *priv_key = NULL;
597 DSA_get0_pqg(dsa, &p, &q, &g);
598 DSA_get0_key(dsa, &pub_key, &priv_key);
599 bitlen = BN_num_bits(p);
600 if ((bitlen & 7) || (BN_num_bits(q) != 160)
601 || (BN_num_bits(g) > bitlen))
604 if (BN_num_bits(pub_key) > bitlen)
606 *pmagic = MS_DSS1MAGIC;
608 if (BN_num_bits(priv_key) > 160)
610 *pmagic = MS_DSS2MAGIC;
615 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
619 static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
622 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
623 const BIGNUM *pub_key = NULL, *priv_key = NULL;
625 DSA_get0_pqg(dsa, &p, &q, &g);
626 DSA_get0_key(dsa, &pub_key, &priv_key);
627 nbyte = BN_num_bytes(p);
628 write_lebn(out, p, nbyte);
629 write_lebn(out, q, 20);
630 write_lebn(out, g, nbyte);
632 write_lebn(out, pub_key, nbyte);
634 write_lebn(out, priv_key, 20);
635 /* Set "invalid" for seed structure values */
636 memset(*out, 0xff, 24);
642 int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk)
644 return do_i2b_bio(out, pk, 0);
647 int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk)
649 return do_i2b_bio(out, pk, 1);
652 int ossl_do_PVK_header(const unsigned char **in, unsigned int length,
654 unsigned int *psaltlen, unsigned int *pkeylen)
656 const unsigned char *p = *in;
657 unsigned int pvk_magic, is_encrypted;
661 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
666 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
669 pvk_magic = read_ledword(&p);
670 if (pvk_magic != MS_PVKMAGIC) {
671 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
680 is_encrypted = read_ledword(&p);
681 *psaltlen = read_ledword(&p);
682 *pkeylen = read_ledword(&p);
684 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
687 if (is_encrypted && *psaltlen == 0) {
688 ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER);
696 #ifndef OPENSSL_NO_RC4
697 static int derive_pvk_key(unsigned char *key,
698 const unsigned char *salt, unsigned int saltlen,
699 const unsigned char *pass, int passlen)
701 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
705 || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
706 || !EVP_DigestUpdate(mctx, salt, saltlen)
707 || !EVP_DigestUpdate(mctx, pass, passlen)
708 || !EVP_DigestFinal_ex(mctx, key, NULL))
711 EVP_MD_CTX_free(mctx);
716 static EVP_PKEY *do_PVK_body(const unsigned char **in,
717 unsigned int saltlen, unsigned int keylen,
718 pem_password_cb *cb, void *u)
720 EVP_PKEY *ret = NULL;
721 const unsigned char *p = *in;
722 unsigned char *enctmp = NULL;
723 unsigned char keybuf[20];
725 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
727 #ifndef OPENSSL_NO_RC4
729 char psbuf[PEM_BUFSIZE];
730 int enctmplen, inlen;
734 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
736 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
738 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
741 enctmp = OPENSSL_malloc(keylen + 8);
742 if (enctmp == NULL) {
743 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
746 if (!derive_pvk_key(keybuf, p, saltlen,
747 (unsigned char *)psbuf, inlen))
750 /* Copy BLOBHEADER across, decrypt rest */
751 memcpy(enctmp, p, 8);
754 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
759 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
761 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
763 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
765 magic = read_ledword((const unsigned char **)&q);
766 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
768 memset(keybuf + 5, 0, 11);
769 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
771 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
773 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
775 magic = read_ledword((const unsigned char **)&q);
776 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
777 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT);
783 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
788 ret = b2i_PrivateKey(&p, keylen);
790 EVP_CIPHER_CTX_free(cctx);
791 if (enctmp != NULL) {
792 OPENSSL_cleanse(keybuf, sizeof(keybuf));
793 OPENSSL_free(enctmp);
798 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
800 unsigned char pvk_hdr[24], *buf = NULL;
801 const unsigned char *p;
803 EVP_PKEY *ret = NULL;
804 unsigned int saltlen, keylen;
806 if (BIO_read(in, pvk_hdr, 24) != 24) {
807 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
812 if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen))
814 buflen = (int)keylen + saltlen;
815 buf = OPENSSL_malloc(buflen);
817 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
821 if (BIO_read(in, buf, buflen) != buflen) {
822 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
825 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
828 OPENSSL_clear_free(buf, buflen);
832 static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel,
833 pem_password_cb *cb, void *u)
835 int outlen = 24, pklen;
836 unsigned char *p = NULL, *start = NULL;
837 EVP_CIPHER_CTX *cctx = NULL;
838 #ifndef OPENSSL_NO_RC4
839 unsigned char *salt = NULL;
843 outlen += PVK_SALTLEN;
844 pklen = do_i2b(NULL, pk, 0);
853 start = p = OPENSSL_malloc(outlen);
855 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
860 cctx = EVP_CIPHER_CTX_new();
864 write_ledword(&p, MS_PVKMAGIC);
865 write_ledword(&p, 0);
866 if (EVP_PKEY_id(pk) == EVP_PKEY_RSA)
867 write_ledword(&p, MS_KEYTYPE_KEYX);
868 #ifndef OPENSSL_NO_DSA
870 write_ledword(&p, MS_KEYTYPE_SIGN);
872 write_ledword(&p, enclevel ? 1 : 0);
873 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
874 write_ledword(&p, pklen);
876 #ifndef OPENSSL_NO_RC4
877 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
885 #ifndef OPENSSL_NO_RC4
886 char psbuf[PEM_BUFSIZE];
887 unsigned char keybuf[20];
888 int enctmplen, inlen;
891 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
893 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
895 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
898 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
899 (unsigned char *)psbuf, inlen))
902 memset(keybuf + 5, 0, 11);
903 p = salt + PVK_SALTLEN + 8;
904 if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
906 OPENSSL_cleanse(keybuf, 20);
907 if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
909 if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen))
912 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
917 EVP_CIPHER_CTX_free(cctx);
925 EVP_CIPHER_CTX_free(cctx);
931 int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
932 pem_password_cb *cb, void *u)
934 unsigned char *tmp = NULL;
937 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
940 wrlen = BIO_write(out, tmp, outlen);
942 if (wrlen == outlen) {
945 ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE);