2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 * Support for PVK format keys and related structures (such a PUBLICKEYBLOB
61 * and PRIVATEKEYBLOB).
65 #include <openssl/pem.h>
66 #include <openssl/rand.h>
67 #include <openssl/bn.h>
68 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
69 # include <openssl/dsa.h>
70 # include <openssl/rsa.h>
73 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
77 static unsigned int read_ledword(const unsigned char **in)
79 const unsigned char *p = *in;
90 * Read a BIGNUM in little endian format. The docs say that this should take
94 static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
96 const unsigned char *p;
97 unsigned char *tmpbuf, *q;
100 tmpbuf = OPENSSL_malloc(nbyte);
104 for (i = 0; i < nbyte; i++)
106 *r = BN_bin2bn(tmpbuf, nbyte, NULL);
107 OPENSSL_free(tmpbuf);
115 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
117 # define MS_PUBLICKEYBLOB 0x6
118 # define MS_PRIVATEKEYBLOB 0x7
119 # define MS_RSA1MAGIC 0x31415352L
120 # define MS_RSA2MAGIC 0x32415352L
121 # define MS_DSS1MAGIC 0x31535344L
122 # define MS_DSS2MAGIC 0x32535344L
124 # define MS_KEYALG_RSA_KEYX 0xa400
125 # define MS_KEYALG_DSS_SIGN 0x2200
127 # define MS_KEYTYPE_KEYX 0x1
128 # define MS_KEYTYPE_SIGN 0x2
130 /* Maximum length of a blob after header */
131 # define BLOB_MAX_LENGTH 102400
133 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
134 # define MS_PVKMAGIC 0xb0b5f11eL
135 /* Salt length for PVK files */
136 # define PVK_SALTLEN 0x10
137 /* Maximum length in PVK header */
138 # define PVK_MAX_KEYLEN 102400
139 /* Maximum salt length */
140 # define PVK_MAX_SALTLEN 10240
142 static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
143 unsigned int bitlen, int ispub);
144 static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
145 unsigned int bitlen, int ispub);
147 static int do_blob_header(const unsigned char **in, unsigned int length,
148 unsigned int *pmagic, unsigned int *pbitlen,
149 int *pisdss, int *pispub)
151 const unsigned char *p = *in;
155 if (*p == MS_PUBLICKEYBLOB) {
157 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
161 } else if (*p == MS_PRIVATEKEYBLOB) {
163 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
172 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER);
175 /* Ignore reserved, aiKeyAlg */
177 *pmagic = read_ledword(&p);
178 *pbitlen = read_ledword(&p);
186 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
195 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
201 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
208 static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
210 unsigned int nbyte, hnbyte;
211 nbyte = (bitlen + 7) >> 3;
212 hnbyte = (bitlen + 15) >> 4;
216 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
220 return 44 + 3 * nbyte;
222 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
226 return 64 + 2 * nbyte;
228 /* Expected length: 4 for 'e' + 'n' */
233 * Expected length: 4 for 'e' and 7 other components. 2
234 * components are bitlen size, 5 are bitlen/2
236 return 4 + 2 * nbyte + 5 * hnbyte;
241 static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length,
244 const unsigned char *p = *in;
245 unsigned int bitlen, magic;
247 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
248 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
252 if (length < blob_length(bitlen, isdss, ispub)) {
253 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
257 return b2i_dss(&p, length, bitlen, ispub);
259 return b2i_rsa(&p, length, bitlen, ispub);
262 static EVP_PKEY *do_b2i_bio(BIO *in, int ispub)
264 const unsigned char *p;
265 unsigned char hdr_buf[16], *buf = NULL;
266 unsigned int bitlen, magic, length;
268 EVP_PKEY *ret = NULL;
269 if (BIO_read(in, hdr_buf, 16) != 16) {
270 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
274 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
277 length = blob_length(bitlen, isdss, ispub);
278 if (length > BLOB_MAX_LENGTH) {
279 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_HEADER_TOO_LONG);
282 buf = OPENSSL_malloc(length);
284 PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
288 if (BIO_read(in, buf, length) != (int)length) {
289 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
294 ret = b2i_dss(&p, length, bitlen, ispub);
296 ret = b2i_rsa(&p, length, bitlen, ispub);
304 static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
305 unsigned int bitlen, int ispub)
307 const unsigned char *p = *in;
308 EVP_PKEY *ret = NULL;
312 nbyte = (bitlen + 7) >> 3;
315 ret = EVP_PKEY_new();
318 if (!read_lebn(&p, nbyte, &dsa->p))
320 if (!read_lebn(&p, 20, &dsa->q))
322 if (!read_lebn(&p, nbyte, &dsa->g))
325 if (!read_lebn(&p, nbyte, &dsa->pub_key))
328 if (!read_lebn(&p, 20, &dsa->priv_key))
330 /* Calculate public key */
331 if (!(dsa->pub_key = BN_new()))
333 if (!(ctx = BN_CTX_new()))
336 if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))
342 EVP_PKEY_set1_DSA(ret, dsa);
348 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
358 static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
359 unsigned int bitlen, int ispub)
361 const unsigned char *p = *in;
362 EVP_PKEY *ret = NULL;
364 unsigned int nbyte, hnbyte;
365 nbyte = (bitlen + 7) >> 3;
366 hnbyte = (bitlen + 15) >> 4;
368 ret = EVP_PKEY_new();
374 if (!BN_set_word(rsa->e, read_ledword(&p)))
376 if (!read_lebn(&p, nbyte, &rsa->n))
379 if (!read_lebn(&p, hnbyte, &rsa->p))
381 if (!read_lebn(&p, hnbyte, &rsa->q))
383 if (!read_lebn(&p, hnbyte, &rsa->dmp1))
385 if (!read_lebn(&p, hnbyte, &rsa->dmq1))
387 if (!read_lebn(&p, hnbyte, &rsa->iqmp))
389 if (!read_lebn(&p, nbyte, &rsa->d))
393 EVP_PKEY_set1_RSA(ret, rsa);
398 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
406 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
408 return do_b2i(in, length, 0);
411 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
413 return do_b2i(in, length, 1);
416 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
418 return do_b2i_bio(in, 0);
421 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
423 return do_b2i_bio(in, 1);
426 static void write_ledword(unsigned char **out, unsigned int dw)
428 unsigned char *p = *out;
430 *p++ = (dw >> 8) & 0xff;
431 *p++ = (dw >> 16) & 0xff;
432 *p++ = (dw >> 24) & 0xff;
436 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
439 unsigned char *p = *out, *q, c;
440 nb = BN_num_bytes(bn);
443 /* In place byte order reversal */
444 for (i = 0; i < nb / 2; i++) {
450 /* Pad with zeroes if we have to */
454 memset(*out, 0, len);
460 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
461 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
463 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
464 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
466 static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
469 unsigned int bitlen, magic = 0, keyalg;
470 int outlen, noinc = 0;
471 if (pk->type == EVP_PKEY_DSA) {
472 bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);
473 keyalg = MS_KEYALG_DSS_SIGN;
474 } else if (pk->type == EVP_PKEY_RSA) {
475 bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);
476 keyalg = MS_KEYALG_RSA_KEYX;
481 outlen = 16 + blob_length(bitlen,
482 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
488 p = OPENSSL_malloc(outlen);
495 *p++ = MS_PUBLICKEYBLOB;
497 *p++ = MS_PRIVATEKEYBLOB;
501 write_ledword(&p, keyalg);
502 write_ledword(&p, magic);
503 write_ledword(&p, bitlen);
504 if (keyalg == MS_KEYALG_DSS_SIGN)
505 write_dsa(&p, pk->pkey.dsa, ispub);
507 write_rsa(&p, pk->pkey.rsa, ispub);
513 static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
515 unsigned char *tmp = NULL;
517 outlen = do_i2b(&tmp, pk, ispub);
520 wrlen = BIO_write(out, tmp, outlen);
527 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
530 bitlen = BN_num_bits(dsa->p);
531 if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160)
532 || (BN_num_bits(dsa->g) > bitlen))
535 if (BN_num_bits(dsa->pub_key) > bitlen)
537 *pmagic = MS_DSS1MAGIC;
539 if (BN_num_bits(dsa->priv_key) > 160)
541 *pmagic = MS_DSS2MAGIC;
546 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
550 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
552 int nbyte, hnbyte, bitlen;
553 if (BN_num_bits(rsa->e) > 32)
555 bitlen = BN_num_bits(rsa->n);
556 nbyte = BN_num_bytes(rsa->n);
557 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
559 *pmagic = MS_RSA1MAGIC;
562 *pmagic = MS_RSA2MAGIC;
564 * For private key each component must fit within nbyte or hnbyte.
566 if (BN_num_bytes(rsa->d) > nbyte)
568 if ((BN_num_bytes(rsa->iqmp) > hnbyte)
569 || (BN_num_bytes(rsa->p) > hnbyte)
570 || (BN_num_bytes(rsa->q) > hnbyte)
571 || (BN_num_bytes(rsa->dmp1) > hnbyte)
572 || (BN_num_bytes(rsa->dmq1) > hnbyte))
577 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
581 static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
584 nbyte = BN_num_bytes(rsa->n);
585 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
586 write_lebn(out, rsa->e, 4);
587 write_lebn(out, rsa->n, -1);
590 write_lebn(out, rsa->p, hnbyte);
591 write_lebn(out, rsa->q, hnbyte);
592 write_lebn(out, rsa->dmp1, hnbyte);
593 write_lebn(out, rsa->dmq1, hnbyte);
594 write_lebn(out, rsa->iqmp, hnbyte);
595 write_lebn(out, rsa->d, nbyte);
598 static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
601 nbyte = BN_num_bytes(dsa->p);
602 write_lebn(out, dsa->p, nbyte);
603 write_lebn(out, dsa->q, 20);
604 write_lebn(out, dsa->g, nbyte);
606 write_lebn(out, dsa->pub_key, nbyte);
608 write_lebn(out, dsa->priv_key, 20);
609 /* Set "invalid" for seed structure values */
610 memset(*out, 0xff, 24);
615 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
617 return do_i2b_bio(out, pk, 0);
620 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
622 return do_i2b_bio(out, pk, 1);
625 # ifndef OPENSSL_NO_RC4
627 static int do_PVK_header(const unsigned char **in, unsigned int length,
629 unsigned int *psaltlen, unsigned int *pkeylen)
631 const unsigned char *p = *in;
632 unsigned int pvk_magic, is_encrypted;
635 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
640 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
643 pvk_magic = read_ledword(&p);
644 if (pvk_magic != MS_PVKMAGIC) {
645 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
654 is_encrypted = read_ledword(&p);
655 *psaltlen = read_ledword(&p);
656 *pkeylen = read_ledword(&p);
658 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
661 if (is_encrypted && !*psaltlen) {
662 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
670 static int derive_pvk_key(unsigned char *key,
671 const unsigned char *salt, unsigned int saltlen,
672 const unsigned char *pass, int passlen)
676 EVP_MD_CTX_init(&mctx);
677 if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL)
678 || !EVP_DigestUpdate(&mctx, salt, saltlen)
679 || !EVP_DigestUpdate(&mctx, pass, passlen)
680 || !EVP_DigestFinal_ex(&mctx, key, NULL))
683 EVP_MD_CTX_cleanup(&mctx);
687 static EVP_PKEY *do_PVK_body(const unsigned char **in,
688 unsigned int saltlen, unsigned int keylen,
689 pem_password_cb *cb, void *u)
691 EVP_PKEY *ret = NULL;
692 const unsigned char *p = *in;
694 unsigned char *enctmp = NULL, *q;
696 EVP_CIPHER_CTX_init(&cctx);
698 char psbuf[PEM_BUFSIZE];
699 unsigned char keybuf[20];
700 int enctmplen, inlen;
702 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
704 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
706 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
709 enctmp = OPENSSL_malloc(keylen + 8);
711 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
714 if (!derive_pvk_key(keybuf, p, saltlen,
715 (unsigned char *)psbuf, inlen))
718 /* Copy BLOBHEADER across, decrypt rest */
719 memcpy(enctmp, p, 8);
722 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
727 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
729 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
731 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
733 magic = read_ledword((const unsigned char **)&q);
734 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
736 memset(keybuf + 5, 0, 11);
737 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
739 OPENSSL_cleanse(keybuf, 20);
740 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
742 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
744 magic = read_ledword((const unsigned char **)&q);
745 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
746 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
750 OPENSSL_cleanse(keybuf, 20);
754 ret = b2i_PrivateKey(&p, keylen);
756 EVP_CIPHER_CTX_cleanup(&cctx);
757 if (enctmp && saltlen)
758 OPENSSL_free(enctmp);
762 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
764 unsigned char pvk_hdr[24], *buf = NULL;
765 const unsigned char *p;
767 EVP_PKEY *ret = NULL;
768 unsigned int saltlen, keylen;
769 if (BIO_read(in, pvk_hdr, 24) != 24) {
770 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
775 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
777 buflen = (int)keylen + saltlen;
778 buf = OPENSSL_malloc(buflen);
780 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
784 if (BIO_read(in, buf, buflen) != buflen) {
785 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
788 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
792 OPENSSL_cleanse(buf, buflen);
798 static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
799 pem_password_cb *cb, void *u)
801 int outlen = 24, pklen;
802 unsigned char *p, *salt = NULL;
804 EVP_CIPHER_CTX_init(&cctx);
806 outlen += PVK_SALTLEN;
807 pklen = do_i2b(NULL, pk, 0);
816 p = OPENSSL_malloc(outlen);
818 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
824 write_ledword(&p, MS_PVKMAGIC);
825 write_ledword(&p, 0);
826 if (pk->type == EVP_PKEY_DSA)
827 write_ledword(&p, MS_KEYTYPE_SIGN);
829 write_ledword(&p, MS_KEYTYPE_KEYX);
830 write_ledword(&p, enclevel ? 1 : 0);
831 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
832 write_ledword(&p, pklen);
834 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
843 char psbuf[PEM_BUFSIZE];
844 unsigned char keybuf[20];
845 int enctmplen, inlen;
847 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
849 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
851 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
854 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
855 (unsigned char *)psbuf, inlen))
858 memset(keybuf + 5, 0, 11);
859 p = salt + PVK_SALTLEN + 8;
860 if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
862 OPENSSL_cleanse(keybuf, 20);
863 if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))
865 if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))
868 EVP_CIPHER_CTX_cleanup(&cctx);
872 EVP_CIPHER_CTX_cleanup(&cctx);
876 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
877 pem_password_cb *cb, void *u)
879 unsigned char *tmp = NULL;
881 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
884 wrlen = BIO_write(out, tmp, outlen);
886 if (wrlen == outlen) {
887 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);