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).
64 #include "internal/cryptlib.h"
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 *r = BN_lebin2bn(*in, nbyte, NULL);
103 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
105 # define MS_PUBLICKEYBLOB 0x6
106 # define MS_PRIVATEKEYBLOB 0x7
107 # define MS_RSA1MAGIC 0x31415352L
108 # define MS_RSA2MAGIC 0x32415352L
109 # define MS_DSS1MAGIC 0x31535344L
110 # define MS_DSS2MAGIC 0x32535344L
112 # define MS_KEYALG_RSA_KEYX 0xa400
113 # define MS_KEYALG_DSS_SIGN 0x2200
115 # define MS_KEYTYPE_KEYX 0x1
116 # define MS_KEYTYPE_SIGN 0x2
118 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
119 # define MS_PVKMAGIC 0xb0b5f11eL
120 /* Salt length for PVK files */
121 # define PVK_SALTLEN 0x10
122 /* Maximum length in PVK header */
123 # define PVK_MAX_KEYLEN 102400
124 /* Maximum salt length */
125 # define PVK_MAX_SALTLEN 10240
127 static EVP_PKEY *b2i_rsa(const unsigned char **in,
128 unsigned int bitlen, int ispub);
129 static EVP_PKEY *b2i_dss(const unsigned char **in,
130 unsigned int bitlen, int ispub);
132 static int do_blob_header(const unsigned char **in, unsigned int length,
133 unsigned int *pmagic, unsigned int *pbitlen,
134 int *pisdss, int *pispub)
136 const unsigned char *p = *in;
140 if (*p == MS_PUBLICKEYBLOB) {
142 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
146 } else if (*p == MS_PRIVATEKEYBLOB) {
148 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
157 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER);
160 /* Ignore reserved, aiKeyAlg */
162 *pmagic = read_ledword(&p);
163 *pbitlen = read_ledword(&p);
171 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
180 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
186 PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
193 static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
195 unsigned int nbyte, hnbyte;
196 nbyte = (bitlen + 7) >> 3;
197 hnbyte = (bitlen + 15) >> 4;
201 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
205 return 44 + 3 * nbyte;
207 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
211 return 64 + 2 * nbyte;
213 /* Expected length: 4 for 'e' + 'n' */
218 * Expected length: 4 for 'e' and 7 other components. 2
219 * components are bitlen size, 5 are bitlen/2
221 return 4 + 2 * nbyte + 5 * hnbyte;
226 static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length,
229 const unsigned char *p = *in;
230 unsigned int bitlen, magic;
232 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
233 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
237 if (length < blob_length(bitlen, isdss, ispub)) {
238 PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
242 return b2i_dss(&p, bitlen, ispub);
244 return b2i_rsa(&p, bitlen, ispub);
247 static EVP_PKEY *do_b2i_bio(BIO *in, int ispub)
249 const unsigned char *p;
250 unsigned char hdr_buf[16], *buf = NULL;
251 unsigned int bitlen, magic, length;
253 EVP_PKEY *ret = NULL;
254 if (BIO_read(in, hdr_buf, 16) != 16) {
255 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
259 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
262 length = blob_length(bitlen, isdss, ispub);
263 buf = OPENSSL_malloc(length);
265 PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
269 if (BIO_read(in, buf, length) != (int)length) {
270 PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
275 ret = b2i_dss(&p, bitlen, ispub);
277 ret = b2i_rsa(&p, bitlen, ispub);
284 static EVP_PKEY *b2i_dss(const unsigned char **in,
285 unsigned int bitlen, int ispub)
287 const unsigned char *p = *in;
288 EVP_PKEY *ret = NULL;
292 BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
293 BIGNUM *pub_key = NULL;
295 nbyte = (bitlen + 7) >> 3;
298 ret = EVP_PKEY_new();
299 if (dsa == NULL || ret == NULL)
301 if (!read_lebn(&p, nbyte, &pbn))
304 if (!read_lebn(&p, 20, &qbn))
307 if (!read_lebn(&p, nbyte, &gbn))
311 if (!read_lebn(&p, nbyte, &pub_key))
314 if (!read_lebn(&p, 20, &priv_key))
317 /* Calculate public key */
321 if ((ctx = BN_CTX_new()) == NULL)
324 if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
329 if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
331 pbn = qbn = gbn = NULL;
332 if (!DSA_set0_key(dsa, pub_key, priv_key))
335 EVP_PKEY_set1_DSA(ret, dsa);
341 PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
353 static EVP_PKEY *b2i_rsa(const unsigned char **in,
354 unsigned int bitlen, int ispub)
356 const unsigned char *p = *in;
357 EVP_PKEY *ret = NULL;
359 unsigned int nbyte, hnbyte;
360 nbyte = (bitlen + 7) >> 3;
361 hnbyte = (bitlen + 15) >> 4;
363 ret = EVP_PKEY_new();
364 if (rsa == NULL || ret == NULL)
369 if (!BN_set_word(rsa->e, read_ledword(&p)))
371 if (!read_lebn(&p, nbyte, &rsa->n))
374 if (!read_lebn(&p, hnbyte, &rsa->p))
376 if (!read_lebn(&p, hnbyte, &rsa->q))
378 if (!read_lebn(&p, hnbyte, &rsa->dmp1))
380 if (!read_lebn(&p, hnbyte, &rsa->dmq1))
382 if (!read_lebn(&p, hnbyte, &rsa->iqmp))
384 if (!read_lebn(&p, nbyte, &rsa->d))
388 EVP_PKEY_set1_RSA(ret, rsa);
393 PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
399 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
401 return do_b2i(in, length, 0);
404 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
406 return do_b2i(in, length, 1);
409 EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
411 return do_b2i_bio(in, 0);
414 EVP_PKEY *b2i_PublicKey_bio(BIO *in)
416 return do_b2i_bio(in, 1);
419 static void write_ledword(unsigned char **out, unsigned int dw)
421 unsigned char *p = *out;
423 *p++ = (dw >> 8) & 0xff;
424 *p++ = (dw >> 16) & 0xff;
425 *p++ = (dw >> 24) & 0xff;
429 static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
431 BN_bn2lebinpad(bn, *out, len);
435 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
436 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
438 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
439 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
441 static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
444 unsigned int bitlen, magic = 0, keyalg;
445 int outlen, noinc = 0;
446 int pktype = EVP_PKEY_id(pk);
447 if (pktype == EVP_PKEY_DSA) {
448 bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
449 keyalg = MS_KEYALG_DSS_SIGN;
450 } else if (pktype == EVP_PKEY_RSA) {
451 bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
452 keyalg = MS_KEYALG_RSA_KEYX;
457 outlen = 16 + blob_length(bitlen,
458 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
464 p = OPENSSL_malloc(outlen);
471 *p++ = MS_PUBLICKEYBLOB;
473 *p++ = MS_PRIVATEKEYBLOB;
477 write_ledword(&p, keyalg);
478 write_ledword(&p, magic);
479 write_ledword(&p, bitlen);
480 if (keyalg == MS_KEYALG_DSS_SIGN)
481 write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
483 write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
489 static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
491 unsigned char *tmp = NULL;
493 outlen = do_i2b(&tmp, pk, ispub);
496 wrlen = BIO_write(out, tmp, outlen);
503 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
506 bitlen = BN_num_bits(DSA_get0_p(dsa));
507 if ((bitlen & 7) || (BN_num_bits(DSA_get0_q(dsa)) != 160)
508 || (BN_num_bits(DSA_get0_g(dsa)) > bitlen))
511 if (BN_num_bits(DSA_get0_pub_key(dsa)) > bitlen)
513 *pmagic = MS_DSS1MAGIC;
515 if (BN_num_bits(DSA_get0_priv_key(dsa)) > 160)
517 *pmagic = MS_DSS2MAGIC;
522 PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
526 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
528 int nbyte, hnbyte, bitlen;
529 if (BN_num_bits(rsa->e) > 32)
531 bitlen = BN_num_bits(rsa->n);
532 nbyte = BN_num_bytes(rsa->n);
533 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
535 *pmagic = MS_RSA1MAGIC;
538 *pmagic = MS_RSA2MAGIC;
540 * For private key each component must fit within nbyte or hnbyte.
542 if (BN_num_bytes(rsa->d) > nbyte)
544 if ((BN_num_bytes(rsa->iqmp) > hnbyte)
545 || (BN_num_bytes(rsa->p) > hnbyte)
546 || (BN_num_bytes(rsa->q) > hnbyte)
547 || (BN_num_bytes(rsa->dmp1) > hnbyte)
548 || (BN_num_bytes(rsa->dmq1) > hnbyte))
553 PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
557 static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
560 nbyte = BN_num_bytes(rsa->n);
561 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
562 write_lebn(out, rsa->e, 4);
563 write_lebn(out, rsa->n, -1);
566 write_lebn(out, rsa->p, hnbyte);
567 write_lebn(out, rsa->q, hnbyte);
568 write_lebn(out, rsa->dmp1, hnbyte);
569 write_lebn(out, rsa->dmq1, hnbyte);
570 write_lebn(out, rsa->iqmp, hnbyte);
571 write_lebn(out, rsa->d, nbyte);
574 static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
577 nbyte = BN_num_bytes(DSA_get0_p(dsa));
578 write_lebn(out, DSA_get0_p(dsa), nbyte);
579 write_lebn(out, DSA_get0_q(dsa), 20);
580 write_lebn(out, DSA_get0_g(dsa), nbyte);
582 write_lebn(out, DSA_get0_pub_key(dsa), nbyte);
584 write_lebn(out, DSA_get0_priv_key(dsa), 20);
585 /* Set "invalid" for seed structure values */
586 memset(*out, 0xff, 24);
591 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
593 return do_i2b_bio(out, pk, 0);
596 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
598 return do_i2b_bio(out, pk, 1);
601 # ifndef OPENSSL_NO_RC4
603 static int do_PVK_header(const unsigned char **in, unsigned int length,
605 unsigned int *psaltlen, unsigned int *pkeylen)
607 const unsigned char *p = *in;
608 unsigned int pvk_magic, is_encrypted;
611 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
616 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
619 pvk_magic = read_ledword(&p);
620 if (pvk_magic != MS_PVKMAGIC) {
621 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
630 is_encrypted = read_ledword(&p);
631 *psaltlen = read_ledword(&p);
632 *pkeylen = read_ledword(&p);
634 if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
637 if (is_encrypted && !*psaltlen) {
638 PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
646 static int derive_pvk_key(unsigned char *key,
647 const unsigned char *salt, unsigned int saltlen,
648 const unsigned char *pass, int passlen)
650 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
653 || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
654 || !EVP_DigestUpdate(mctx, salt, saltlen)
655 || !EVP_DigestUpdate(mctx, pass, passlen)
656 || !EVP_DigestFinal_ex(mctx, key, NULL))
659 EVP_MD_CTX_free(mctx);
663 static EVP_PKEY *do_PVK_body(const unsigned char **in,
664 unsigned int saltlen, unsigned int keylen,
665 pem_password_cb *cb, void *u)
667 EVP_PKEY *ret = NULL;
668 const unsigned char *p = *in;
670 unsigned char *enctmp = NULL, *q;
672 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
674 char psbuf[PEM_BUFSIZE];
675 unsigned char keybuf[20];
676 int enctmplen, inlen;
678 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
680 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
682 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
685 enctmp = OPENSSL_malloc(keylen + 8);
686 if (enctmp == NULL) {
687 PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
690 if (!derive_pvk_key(keybuf, p, saltlen,
691 (unsigned char *)psbuf, inlen))
694 /* Copy BLOBHEADER across, decrypt rest */
695 memcpy(enctmp, p, 8);
698 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
703 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
705 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
707 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
709 magic = read_ledword((const unsigned char **)&q);
710 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
712 memset(keybuf + 5, 0, 11);
713 if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
715 OPENSSL_cleanse(keybuf, 20);
716 if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
718 if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
720 magic = read_ledword((const unsigned char **)&q);
721 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
722 PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
726 OPENSSL_cleanse(keybuf, 20);
730 ret = b2i_PrivateKey(&p, keylen);
732 EVP_CIPHER_CTX_free(cctx);
733 OPENSSL_free(enctmp);
737 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
739 unsigned char pvk_hdr[24], *buf = NULL;
740 const unsigned char *p;
742 EVP_PKEY *ret = NULL;
743 unsigned int saltlen, keylen;
744 if (BIO_read(in, pvk_hdr, 24) != 24) {
745 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
750 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
752 buflen = (int)keylen + saltlen;
753 buf = OPENSSL_malloc(buflen);
755 PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
759 if (BIO_read(in, buf, buflen) != buflen) {
760 PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
763 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
766 OPENSSL_clear_free(buf, buflen);
770 static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
771 pem_password_cb *cb, void *u)
773 int outlen = 24, pklen;
774 unsigned char *p, *salt = NULL;
775 EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
777 outlen += PVK_SALTLEN;
778 pklen = do_i2b(NULL, pk, 0);
787 p = OPENSSL_malloc(outlen);
789 PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
795 write_ledword(&p, MS_PVKMAGIC);
796 write_ledword(&p, 0);
797 if (EVP_PKEY_id(pk) == EVP_PKEY_DSA)
798 write_ledword(&p, MS_KEYTYPE_SIGN);
800 write_ledword(&p, MS_KEYTYPE_KEYX);
801 write_ledword(&p, enclevel ? 1 : 0);
802 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
803 write_ledword(&p, pklen);
805 if (RAND_bytes(p, PVK_SALTLEN) <= 0)
814 char psbuf[PEM_BUFSIZE];
815 unsigned char keybuf[20];
816 int enctmplen, inlen;
818 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
820 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
822 PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
825 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
826 (unsigned char *)psbuf, inlen))
829 memset(keybuf + 5, 0, 11);
830 p = salt + PVK_SALTLEN + 8;
831 if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
833 OPENSSL_cleanse(keybuf, 20);
834 if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
836 if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen))
839 EVP_CIPHER_CTX_free(cctx);
843 EVP_CIPHER_CTX_free(cctx);
847 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
848 pem_password_cb *cb, void *u)
850 unsigned char *tmp = NULL;
852 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
855 wrlen = BIO_write(out, tmp, outlen);
857 if (wrlen == outlen) {
858 PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);