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 <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);
62 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
64 # define MS_PUBLICKEYBLOB 0x6
65 # define MS_PRIVATEKEYBLOB 0x7
66 # define MS_RSA1MAGIC 0x31415352L
67 # define MS_RSA2MAGIC 0x32415352L
68 # define MS_DSS1MAGIC 0x31535344L
69 # define MS_DSS2MAGIC 0x32535344L
71 # define MS_KEYALG_RSA_KEYX 0xa400
72 # define MS_KEYALG_DSS_SIGN 0x2200
74 # define MS_KEYTYPE_KEYX 0x1
75 # define MS_KEYTYPE_SIGN 0x2
77 /* Maximum length of a blob after header */
78 # define BLOB_MAX_LENGTH 102400
80 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
81 # define MS_PVKMAGIC 0xb0b5f11eL
82 /* Salt length for PVK files */
83 # define PVK_SALTLEN 0x10
84 /* Maximum length in PVK header */
85 # define PVK_MAX_KEYLEN 102400
86 /* Maximum salt length */
87 # define PVK_MAX_SALTLEN 10240
89 static EVP_PKEY *b2i_rsa(const unsigned char **in,
90 unsigned int bitlen, int ispub);
91 #ifndef OPENSSL_NO_DSA
92 static EVP_PKEY *b2i_dss(const unsigned char **in,
93 unsigned int bitlen, int ispub);
96 int ossl_do_blob_header(const unsigned char **in, unsigned int length,
97 unsigned int *pmagic, unsigned int *pbitlen,
98 int *pisdss, int *pispub)
100 const unsigned char *p = *in;
105 if (*p == MS_PUBLICKEYBLOB) {
107 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
111 } else if (*p == MS_PRIVATEKEYBLOB) {
113 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
123 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER);
126 /* Ignore reserved, aiKeyAlg */
128 *pmagic = read_ledword(&p);
129 *pbitlen = read_ledword(&p);
138 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
148 ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
154 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
161 static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
163 unsigned int nbyte = (bitlen + 7) >> 3;
164 unsigned int hnbyte = (bitlen + 15) >> 4;
169 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
173 return 44 + 3 * nbyte;
175 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
179 return 64 + 2 * nbyte;
181 /* Expected length: 4 for 'e' + 'n' */
186 * Expected length: 4 for 'e' and 7 other components. 2
187 * components are bitlen size, 5 are bitlen/2
189 return 4 + 2 * nbyte + 5 * hnbyte;
194 EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub)
196 const unsigned char *p = *in;
197 unsigned int bitlen, magic;
200 if (ossl_do_blob_header(&p, length, &magic, &bitlen, &isdss, ispub) <= 0) {
201 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
205 if (length < blob_length(bitlen, isdss, *ispub)) {
206 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
210 return b2i_rsa(&p, bitlen, *ispub);
211 #ifndef OPENSSL_NO_DSA
213 return b2i_dss(&p, bitlen, *ispub);
216 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
220 EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub)
222 const unsigned char *p;
223 unsigned char hdr_buf[16], *buf = NULL;
224 unsigned int bitlen, magic, length;
226 EVP_PKEY *ret = NULL;
228 if (BIO_read(in, hdr_buf, 16) != 16) {
229 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
233 if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0)
236 length = blob_length(bitlen, isdss, *ispub);
237 if (length > BLOB_MAX_LENGTH) {
238 ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
241 buf = OPENSSL_malloc(length);
243 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
247 if (BIO_read(in, buf, length) != (int)length) {
248 ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
253 ret = b2i_rsa(&p, bitlen, *ispub);
254 #ifndef OPENSSL_NO_DSA
256 ret = b2i_dss(&p, bitlen, *ispub);
260 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
267 #ifndef OPENSSL_NO_DSA
268 static EVP_PKEY *b2i_dss(const unsigned char **in,
269 unsigned int bitlen, int ispub)
271 const unsigned char *p = *in;
272 EVP_PKEY *ret = NULL;
275 BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
276 BIGNUM *pub_key = NULL;
277 unsigned int nbyte = (bitlen + 7) >> 3;
280 ret = EVP_PKEY_new();
281 if (dsa == NULL || ret == NULL)
283 if (!read_lebn(&p, nbyte, &pbn))
286 if (!read_lebn(&p, 20, &qbn))
289 if (!read_lebn(&p, nbyte, &gbn))
293 if (!read_lebn(&p, nbyte, &pub_key))
296 if (!read_lebn(&p, 20, &priv_key))
299 /* Set constant time flag before public key calculation */
300 BN_set_flags(priv_key, BN_FLG_CONSTTIME);
302 /* Calculate public key */
306 if ((ctx = BN_CTX_new()) == NULL)
309 if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
315 if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
317 pbn = qbn = gbn = NULL;
318 if (!DSA_set0_key(dsa, pub_key, priv_key))
320 pub_key = priv_key = NULL;
322 if (!EVP_PKEY_set1_DSA(ret, dsa))
329 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
342 static EVP_PKEY *b2i_rsa(const unsigned char **in,
343 unsigned int bitlen, int ispub)
345 const unsigned char *pin = *in;
346 EVP_PKEY *ret = NULL;
347 BIGNUM *e = NULL, *n = NULL, *d = NULL;
348 BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
350 unsigned int nbyte = (bitlen + 7) >> 3;
351 unsigned int hnbyte = (bitlen + 15) >> 4;
354 ret = EVP_PKEY_new();
355 if (rsa == NULL || ret == NULL)
360 if (!BN_set_word(e, read_ledword(&pin)))
362 if (!read_lebn(&pin, nbyte, &n))
365 if (!read_lebn(&pin, hnbyte, &p))
367 if (!read_lebn(&pin, hnbyte, &q))
369 if (!read_lebn(&pin, hnbyte, &dmp1))
371 if (!read_lebn(&pin, hnbyte, &dmq1))
373 if (!read_lebn(&pin, hnbyte, &iqmp))
375 if (!read_lebn(&pin, nbyte, &d))
377 if (!RSA_set0_factors(rsa, p, q))
380 if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
382 dmp1 = dmq1 = iqmp = NULL;
384 if (!RSA_set0_key(rsa, n, e, d))
388 if (!EVP_PKEY_set1_RSA(ret, rsa))
394 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
408 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
412 return ossl_b2i(in, length, &ispub);
415 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
419 return ossl_b2i(in, length, &ispub);
422 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
426 return ossl_b2i_bio(in, &ispub);
429 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
433 return ossl_b2i_bio(in, &ispub);
436 static void write_ledword(unsigned char **out, unsigned int dw)
438 unsigned char *p = *out;
441 *p++ = (dw >> 8) & 0xff;
442 *p++ = (dw >> 16) & 0xff;
443 *p++ = (dw >> 24) & 0xff;
447 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
449 BN_bn2lebinpad(bn, *out, len);
453 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
454 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
456 #ifndef OPENSSL_NO_DSA
457 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
458 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
461 static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub)
464 unsigned int bitlen = 0, magic = 0, keyalg = 0;
465 int outlen = -1, noinc = 0;
467 #ifndef OPENSSL_NO_PROVIDER_CODE
468 EVP_PKEY *pkcopy = NULL;
470 if (evp_pkey_is_provided(pk)) {
471 if (!evp_pkey_copy_downgraded(&pkcopy, pk))
477 pktype = EVP_PKEY_id(pk);
478 if (pktype == EVP_PKEY_RSA) {
479 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
480 keyalg = MS_KEYALG_RSA_KEYX;
481 #ifndef OPENSSL_NO_DSA
482 } else if (pktype == EVP_PKEY_DSA) {
483 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
484 keyalg = MS_KEYALG_DSS_SIGN;
490 outlen = 16 + blob_length(bitlen,
491 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
497 if ((p = OPENSSL_malloc(outlen)) == NULL) {
498 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
506 *p++ = MS_PUBLICKEYBLOB;
508 *p++ = MS_PRIVATEKEYBLOB;
512 write_ledword(&p, keyalg);
513 write_ledword(&p, magic);
514 write_ledword(&p, bitlen);
515 if (keyalg == MS_KEYALG_RSA_KEYX)
516 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
517 #ifndef OPENSSL_NO_DSA
519 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
524 #ifndef OPENSSL_NO_PROVIDER_CODE
525 EVP_PKEY_free(pkcopy);
530 static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub)
532 unsigned char *tmp = NULL;
535 outlen = do_i2b(&tmp, pk, ispub);
538 wrlen = BIO_write(out, tmp, outlen);
545 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
547 int nbyte, hnbyte, bitlen;
550 RSA_get0_key(rsa, NULL, &e, NULL);
551 if (BN_num_bits(e) > 32)
553 bitlen = RSA_bits(rsa);
554 nbyte = RSA_size(rsa);
555 hnbyte = (bitlen + 15) >> 4;
557 *pmagic = MS_RSA1MAGIC;
560 const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
562 *pmagic = MS_RSA2MAGIC;
565 * For private key each component must fit within nbyte or hnbyte.
567 RSA_get0_key(rsa, NULL, NULL, &d);
568 if (BN_num_bytes(d) > nbyte)
570 RSA_get0_factors(rsa, &p, &q);
571 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
572 if ((BN_num_bytes(iqmp) > hnbyte)
573 || (BN_num_bytes(p) > hnbyte)
574 || (BN_num_bytes(q) > hnbyte)
575 || (BN_num_bytes(dmp1) > hnbyte)
576 || (BN_num_bytes(dmq1) > hnbyte))
581 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
585 static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
588 const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
590 nbyte = RSA_size(rsa);
591 hnbyte = (RSA_bits(rsa) + 15) >> 4;
592 RSA_get0_key(rsa, &n, &e, &d);
593 write_lebn(out, e, 4);
594 write_lebn(out, n, nbyte);
597 RSA_get0_factors(rsa, &p, &q);
598 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
599 write_lebn(out, p, hnbyte);
600 write_lebn(out, q, hnbyte);
601 write_lebn(out, dmp1, hnbyte);
602 write_lebn(out, dmq1, hnbyte);
603 write_lebn(out, iqmp, hnbyte);
604 write_lebn(out, d, nbyte);
607 #ifndef OPENSSL_NO_DSA
608 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
611 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
612 const BIGNUM *pub_key = NULL, *priv_key = NULL;
614 DSA_get0_pqg(dsa, &p, &q, &g);
615 DSA_get0_key(dsa, &pub_key, &priv_key);
616 bitlen = BN_num_bits(p);
617 if ((bitlen & 7) || (BN_num_bits(q) != 160)
618 || (BN_num_bits(g) > bitlen))
621 if (BN_num_bits(pub_key) > bitlen)
623 *pmagic = MS_DSS1MAGIC;
625 if (BN_num_bits(priv_key) > 160)
627 *pmagic = MS_DSS2MAGIC;
632 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
636 static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
639 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
640 const BIGNUM *pub_key = NULL, *priv_key = NULL;
642 DSA_get0_pqg(dsa, &p, &q, &g);
643 DSA_get0_key(dsa, &pub_key, &priv_key);
644 nbyte = BN_num_bytes(p);
645 write_lebn(out, p, nbyte);
646 write_lebn(out, q, 20);
647 write_lebn(out, g, nbyte);
649 write_lebn(out, pub_key, nbyte);
651 write_lebn(out, priv_key, 20);
652 /* Set "invalid" for seed structure values */
653 memset(*out, 0xff, 24);
659 int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk)
661 return do_i2b_bio(out, pk, 0);
664 int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk)
666 return do_i2b_bio(out, pk, 1);
669 int ossl_do_PVK_header(const unsigned char **in, unsigned int length,
671 unsigned int *psaltlen, unsigned int *pkeylen)
673 const unsigned char *p = *in;
674 unsigned int pvk_magic, is_encrypted;
678 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
683 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
686 pvk_magic = read_ledword(&p);
687 if (pvk_magic != MS_PVKMAGIC) {
688 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
697 is_encrypted = read_ledword(&p);
698 *psaltlen = read_ledword(&p);
699 *pkeylen = read_ledword(&p);
701 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
704 if (is_encrypted && *psaltlen == 0) {
705 ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER);
713 #ifndef OPENSSL_NO_RC4
714 static int derive_pvk_key(unsigned char *key,
715 const unsigned char *salt, unsigned int saltlen,
716 const unsigned char *pass, int passlen)
718 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
722 || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
723 || !EVP_DigestUpdate(mctx, salt, saltlen)
724 || !EVP_DigestUpdate(mctx, pass, passlen)
725 || !EVP_DigestFinal_ex(mctx, key, NULL))
728 EVP_MD_CTX_free(mctx);
733 static EVP_PKEY *do_PVK_body(const unsigned char **in,
734 unsigned int saltlen, unsigned int keylen,
735 pem_password_cb *cb, void *u)
737 EVP_PKEY *ret = NULL;
738 const unsigned char *p = *in;
739 unsigned char *enctmp = NULL;
740 unsigned char keybuf[20];
742 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
744 #ifndef OPENSSL_NO_RC4
746 char psbuf[PEM_BUFSIZE];
747 int enctmplen, inlen;
751 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
753 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
755 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
758 enctmp = OPENSSL_malloc(keylen + 8);
759 if (enctmp == NULL) {
760 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
763 if (!derive_pvk_key(keybuf, p, saltlen,
764 (unsigned char *)psbuf, inlen))
767 /* Copy BLOBHEADER across, decrypt rest */
768 memcpy(enctmp, p, 8);
771 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
776 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
778 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
780 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
782 magic = read_ledword((const unsigned char **)&q);
783 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
785 memset(keybuf + 5, 0, 11);
786 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
788 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
790 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
792 magic = read_ledword((const unsigned char **)&q);
793 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
794 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT);
800 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
805 ret = b2i_PrivateKey(&p, keylen);
807 EVP_CIPHER_CTX_free(cctx);
808 if (enctmp != NULL) {
809 OPENSSL_cleanse(keybuf, sizeof(keybuf));
810 OPENSSL_free(enctmp);
815 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
817 unsigned char pvk_hdr[24], *buf = NULL;
818 const unsigned char *p;
820 EVP_PKEY *ret = NULL;
821 unsigned int saltlen, keylen;
823 if (BIO_read(in, pvk_hdr, 24) != 24) {
824 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
829 if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen))
831 buflen = (int)keylen + saltlen;
832 buf = OPENSSL_malloc(buflen);
834 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
838 if (BIO_read(in, buf, buflen) != buflen) {
839 ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
842 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
845 OPENSSL_clear_free(buf, buflen);
849 static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel,
850 pem_password_cb *cb, void *u)
852 int outlen = 24, pklen;
853 unsigned char *p = NULL, *start = NULL;
854 EVP_CIPHER_CTX *cctx = NULL;
855 #ifndef OPENSSL_NO_RC4
856 unsigned char *salt = NULL;
860 outlen += PVK_SALTLEN;
861 pklen = do_i2b(NULL, pk, 0);
870 start = p = OPENSSL_malloc(outlen);
872 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
877 cctx = EVP_CIPHER_CTX_new();
881 write_ledword(&p, MS_PVKMAGIC);
882 write_ledword(&p, 0);
883 if (EVP_PKEY_id(pk) == EVP_PKEY_RSA)
884 write_ledword(&p, MS_KEYTYPE_KEYX);
885 #ifndef OPENSSL_NO_DSA
887 write_ledword(&p, MS_KEYTYPE_SIGN);
889 write_ledword(&p, enclevel ? 1 : 0);
890 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
891 write_ledword(&p, pklen);
893 #ifndef OPENSSL_NO_RC4
894 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
902 #ifndef OPENSSL_NO_RC4
903 char psbuf[PEM_BUFSIZE];
904 unsigned char keybuf[20];
905 int enctmplen, inlen;
908 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
910 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
912 ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
915 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
916 (unsigned char *)psbuf, inlen))
919 memset(keybuf + 5, 0, 11);
920 p = salt + PVK_SALTLEN + 8;
921 if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
923 OPENSSL_cleanse(keybuf, 20);
924 if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
926 if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen))
929 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
934 EVP_CIPHER_CTX_free(cctx);
942 EVP_CIPHER_CTX_free(cctx);
948 int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
949 pem_password_cb *cb, void *u)
951 unsigned char *tmp = NULL;
954 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
957 wrlen = BIO_write(out, tmp, outlen);
959 if (wrlen == outlen) {
962 ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE);