2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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).
15 #include "internal/cryptlib.h"
16 #include <openssl/pem.h>
17 #include <openssl/rand.h>
18 #include <openssl/bn.h>
19 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
20 # include <openssl/dsa.h>
21 # include <openssl/rsa.h>
24 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
28 static unsigned int read_ledword(const unsigned char **in
)
30 const unsigned char *p
= *in
;
41 * Read a BIGNUM in little endian format. The docs say that this should take
45 static int read_lebn(const unsigned char **in
, unsigned int nbyte
, BIGNUM
**r
)
47 *r
= BN_lebin2bn(*in
, nbyte
, NULL
);
54 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
56 # define MS_PUBLICKEYBLOB 0x6
57 # define MS_PRIVATEKEYBLOB 0x7
58 # define MS_RSA1MAGIC 0x31415352L
59 # define MS_RSA2MAGIC 0x32415352L
60 # define MS_DSS1MAGIC 0x31535344L
61 # define MS_DSS2MAGIC 0x32535344L
63 # define MS_KEYALG_RSA_KEYX 0xa400
64 # define MS_KEYALG_DSS_SIGN 0x2200
66 # define MS_KEYTYPE_KEYX 0x1
67 # define MS_KEYTYPE_SIGN 0x2
69 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
70 # define MS_PVKMAGIC 0xb0b5f11eL
71 /* Salt length for PVK files */
72 # define PVK_SALTLEN 0x10
73 /* Maximum length in PVK header */
74 # define PVK_MAX_KEYLEN 102400
75 /* Maximum salt length */
76 # define PVK_MAX_SALTLEN 10240
78 static EVP_PKEY
*b2i_rsa(const unsigned char **in
,
79 unsigned int bitlen
, int ispub
);
80 static EVP_PKEY
*b2i_dss(const unsigned char **in
,
81 unsigned int bitlen
, int ispub
);
83 static int do_blob_header(const unsigned char **in
, unsigned int length
,
84 unsigned int *pmagic
, unsigned int *pbitlen
,
85 int *pisdss
, int *pispub
)
87 const unsigned char *p
= *in
;
91 if (*p
== MS_PUBLICKEYBLOB
) {
93 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
97 } else if (*p
== MS_PRIVATEKEYBLOB
) {
99 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
108 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_VERSION_NUMBER
);
111 /* Ignore reserved, aiKeyAlg */
113 *pmagic
= read_ledword(&p
);
114 *pbitlen
= read_ledword(&p
);
122 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
131 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
137 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
144 static unsigned int blob_length(unsigned bitlen
, int isdss
, int ispub
)
146 unsigned int nbyte
, hnbyte
;
147 nbyte
= (bitlen
+ 7) >> 3;
148 hnbyte
= (bitlen
+ 15) >> 4;
152 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
156 return 44 + 3 * nbyte
;
158 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
162 return 64 + 2 * nbyte
;
164 /* Expected length: 4 for 'e' + 'n' */
169 * Expected length: 4 for 'e' and 7 other components. 2
170 * components are bitlen size, 5 are bitlen/2
172 return 4 + 2 * nbyte
+ 5 * hnbyte
;
177 static EVP_PKEY
*do_b2i(const unsigned char **in
, unsigned int length
,
180 const unsigned char *p
= *in
;
181 unsigned int bitlen
, magic
;
183 if (do_blob_header(&p
, length
, &magic
, &bitlen
, &isdss
, &ispub
) <= 0) {
184 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_HEADER_PARSE_ERROR
);
188 if (length
< blob_length(bitlen
, isdss
, ispub
)) {
189 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_TOO_SHORT
);
193 return b2i_dss(&p
, bitlen
, ispub
);
195 return b2i_rsa(&p
, bitlen
, ispub
);
198 static EVP_PKEY
*do_b2i_bio(BIO
*in
, int ispub
)
200 const unsigned char *p
;
201 unsigned char hdr_buf
[16], *buf
= NULL
;
202 unsigned int bitlen
, magic
, length
;
204 EVP_PKEY
*ret
= NULL
;
205 if (BIO_read(in
, hdr_buf
, 16) != 16) {
206 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
210 if (do_blob_header(&p
, 16, &magic
, &bitlen
, &isdss
, &ispub
) <= 0)
213 length
= blob_length(bitlen
, isdss
, ispub
);
214 buf
= OPENSSL_malloc(length
);
216 PEMerr(PEM_F_DO_B2I_BIO
, ERR_R_MALLOC_FAILURE
);
220 if (BIO_read(in
, buf
, length
) != (int)length
) {
221 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
226 ret
= b2i_dss(&p
, bitlen
, ispub
);
228 ret
= b2i_rsa(&p
, bitlen
, ispub
);
235 static EVP_PKEY
*b2i_dss(const unsigned char **in
,
236 unsigned int bitlen
, int ispub
)
238 const unsigned char *p
= *in
;
239 EVP_PKEY
*ret
= NULL
;
243 BIGNUM
*pbn
= NULL
, *qbn
= NULL
, *gbn
= NULL
, *priv_key
= NULL
;
244 BIGNUM
*pub_key
= NULL
;
246 nbyte
= (bitlen
+ 7) >> 3;
249 ret
= EVP_PKEY_new();
250 if (dsa
== NULL
|| ret
== NULL
)
252 if (!read_lebn(&p
, nbyte
, &pbn
))
255 if (!read_lebn(&p
, 20, &qbn
))
258 if (!read_lebn(&p
, nbyte
, &gbn
))
262 if (!read_lebn(&p
, nbyte
, &pub_key
))
265 if (!read_lebn(&p
, 20, &priv_key
))
268 /* Calculate public key */
272 if ((ctx
= BN_CTX_new()) == NULL
)
275 if (!BN_mod_exp(pub_key
, gbn
, priv_key
, pbn
, ctx
))
280 if (!DSA_set0_pqg(dsa
, pbn
, qbn
, gbn
))
282 pbn
= qbn
= gbn
= NULL
;
283 if (!DSA_set0_key(dsa
, pub_key
, priv_key
))
286 EVP_PKEY_set1_DSA(ret
, dsa
);
292 PEMerr(PEM_F_B2I_DSS
, ERR_R_MALLOC_FAILURE
);
304 static EVP_PKEY
*b2i_rsa(const unsigned char **in
,
305 unsigned int bitlen
, int ispub
)
307 const unsigned char *pin
= *in
;
308 EVP_PKEY
*ret
= NULL
;
309 BIGNUM
*e
= NULL
, *n
= NULL
, *d
= NULL
;
310 BIGNUM
*p
= NULL
, *q
= NULL
, *dmp1
= NULL
, *dmq1
= NULL
, *iqmp
= NULL
;
312 unsigned int nbyte
, hnbyte
;
313 nbyte
= (bitlen
+ 7) >> 3;
314 hnbyte
= (bitlen
+ 15) >> 4;
316 ret
= EVP_PKEY_new();
317 if (rsa
== NULL
|| ret
== NULL
)
322 if (!BN_set_word(e
, read_ledword(&pin
)))
324 if (!read_lebn(&pin
, nbyte
, &n
))
327 if (!read_lebn(&pin
, hnbyte
, &p
))
329 if (!read_lebn(&pin
, hnbyte
, &q
))
331 if (!read_lebn(&pin
, hnbyte
, &dmp1
))
333 if (!read_lebn(&pin
, hnbyte
, &dmq1
))
335 if (!read_lebn(&pin
, hnbyte
, &iqmp
))
337 if (!read_lebn(&pin
, nbyte
, &d
))
339 RSA_set0_factors(rsa
, p
, q
);
340 RSA_set0_crt_params(rsa
, dmp1
, dmq1
, iqmp
);
342 RSA_set0_key(rsa
, n
, e
, d
);
344 EVP_PKEY_set1_RSA(ret
, rsa
);
349 PEMerr(PEM_F_B2I_RSA
, ERR_R_MALLOC_FAILURE
);
363 EVP_PKEY
*b2i_PrivateKey(const unsigned char **in
, long length
)
365 return do_b2i(in
, length
, 0);
368 EVP_PKEY
*b2i_PublicKey(const unsigned char **in
, long length
)
370 return do_b2i(in
, length
, 1);
373 EVP_PKEY
*b2i_PrivateKey_bio(BIO
*in
)
375 return do_b2i_bio(in
, 0);
378 EVP_PKEY
*b2i_PublicKey_bio(BIO
*in
)
380 return do_b2i_bio(in
, 1);
383 static void write_ledword(unsigned char **out
, unsigned int dw
)
385 unsigned char *p
= *out
;
387 *p
++ = (dw
>> 8) & 0xff;
388 *p
++ = (dw
>> 16) & 0xff;
389 *p
++ = (dw
>> 24) & 0xff;
393 static void write_lebn(unsigned char **out
, const BIGNUM
*bn
, int len
)
395 BN_bn2lebinpad(bn
, *out
, len
);
399 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *magic
);
400 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *magic
);
402 static void write_rsa(unsigned char **out
, RSA
*rsa
, int ispub
);
403 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
);
405 static int do_i2b(unsigned char **out
, EVP_PKEY
*pk
, int ispub
)
408 unsigned int bitlen
, magic
= 0, keyalg
;
409 int outlen
, noinc
= 0;
410 int pktype
= EVP_PKEY_id(pk
);
411 if (pktype
== EVP_PKEY_DSA
) {
412 bitlen
= check_bitlen_dsa(EVP_PKEY_get0_DSA(pk
), ispub
, &magic
);
413 keyalg
= MS_KEYALG_DSS_SIGN
;
414 } else if (pktype
== EVP_PKEY_RSA
) {
415 bitlen
= check_bitlen_rsa(EVP_PKEY_get0_RSA(pk
), ispub
, &magic
);
416 keyalg
= MS_KEYALG_RSA_KEYX
;
421 outlen
= 16 + blob_length(bitlen
,
422 keyalg
== MS_KEYALG_DSS_SIGN
? 1 : 0, ispub
);
428 p
= OPENSSL_malloc(outlen
);
435 *p
++ = MS_PUBLICKEYBLOB
;
437 *p
++ = MS_PRIVATEKEYBLOB
;
441 write_ledword(&p
, keyalg
);
442 write_ledword(&p
, magic
);
443 write_ledword(&p
, bitlen
);
444 if (keyalg
== MS_KEYALG_DSS_SIGN
)
445 write_dsa(&p
, EVP_PKEY_get0_DSA(pk
), ispub
);
447 write_rsa(&p
, EVP_PKEY_get0_RSA(pk
), ispub
);
453 static int do_i2b_bio(BIO
*out
, EVP_PKEY
*pk
, int ispub
)
455 unsigned char *tmp
= NULL
;
457 outlen
= do_i2b(&tmp
, pk
, ispub
);
460 wrlen
= BIO_write(out
, tmp
, outlen
);
467 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *pmagic
)
470 BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
, *pub_key
= NULL
, *priv_key
= NULL
;
472 DSA_get0_pqg(dsa
, &p
, &q
, &g
);
473 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
474 bitlen
= BN_num_bits(p
);
475 if ((bitlen
& 7) || (BN_num_bits(q
) != 160)
476 || (BN_num_bits(g
) > bitlen
))
479 if (BN_num_bits(pub_key
) > bitlen
)
481 *pmagic
= MS_DSS1MAGIC
;
483 if (BN_num_bits(priv_key
) > 160)
485 *pmagic
= MS_DSS2MAGIC
;
490 PEMerr(PEM_F_CHECK_BITLEN_DSA
, PEM_R_UNSUPPORTED_KEY_COMPONENTS
);
494 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *pmagic
)
496 int nbyte
, hnbyte
, bitlen
;
499 RSA_get0_key(rsa
, &e
, NULL
, NULL
);
500 if (BN_num_bits(e
) > 32)
502 bitlen
= RSA_bits(rsa
);
503 nbyte
= RSA_size(rsa
);
504 hnbyte
= (bitlen
+ 15) >> 4;
506 *pmagic
= MS_RSA1MAGIC
;
509 BIGNUM
*d
, *p
, *q
, *iqmp
, *dmp1
, *dmq1
;
511 *pmagic
= MS_RSA2MAGIC
;
514 * For private key each component must fit within nbyte or hnbyte.
516 RSA_get0_key(rsa
, NULL
, NULL
, &d
);
517 if (BN_num_bytes(d
) > nbyte
)
519 RSA_get0_factors(rsa
, &p
, &q
);
520 RSA_get0_crt_params(rsa
, &dmp1
, &dmq1
, &iqmp
);
521 if ((BN_num_bytes(iqmp
) > hnbyte
)
522 || (BN_num_bytes(p
) > hnbyte
)
523 || (BN_num_bytes(q
) > hnbyte
)
524 || (BN_num_bytes(dmp1
) > hnbyte
)
525 || (BN_num_bytes(dmq1
) > hnbyte
))
530 PEMerr(PEM_F_CHECK_BITLEN_RSA
, PEM_R_UNSUPPORTED_KEY_COMPONENTS
);
534 static void write_rsa(unsigned char **out
, RSA
*rsa
, int ispub
)
537 BIGNUM
*n
, *d
, *e
, *p
, *q
, *iqmp
, *dmp1
, *dmq1
;
539 nbyte
= RSA_size(rsa
);
540 hnbyte
= (RSA_bits(rsa
) + 15) >> 4;
541 RSA_get0_key(rsa
, &e
, &n
, &d
);
542 write_lebn(out
, e
, 4);
543 write_lebn(out
, n
, -1);
546 RSA_get0_factors(rsa
, &p
, &q
);
547 RSA_get0_crt_params(rsa
, &dmp1
, &dmq1
, &iqmp
);
548 write_lebn(out
, p
, hnbyte
);
549 write_lebn(out
, q
, hnbyte
);
550 write_lebn(out
, dmp1
, hnbyte
);
551 write_lebn(out
, dmq1
, hnbyte
);
552 write_lebn(out
, iqmp
, hnbyte
);
553 write_lebn(out
, d
, nbyte
);
556 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
)
559 BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
, *pub_key
= NULL
, *priv_key
= NULL
;
561 DSA_get0_pqg(dsa
, &p
, &q
, &g
);
562 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
563 nbyte
= BN_num_bytes(p
);
564 write_lebn(out
, p
, nbyte
);
565 write_lebn(out
, q
, 20);
566 write_lebn(out
, g
, nbyte
);
568 write_lebn(out
, pub_key
, nbyte
);
570 write_lebn(out
, priv_key
, 20);
571 /* Set "invalid" for seed structure values */
572 memset(*out
, 0xff, 24);
577 int i2b_PrivateKey_bio(BIO
*out
, EVP_PKEY
*pk
)
579 return do_i2b_bio(out
, pk
, 0);
582 int i2b_PublicKey_bio(BIO
*out
, EVP_PKEY
*pk
)
584 return do_i2b_bio(out
, pk
, 1);
587 # ifndef OPENSSL_NO_RC4
589 static int do_PVK_header(const unsigned char **in
, unsigned int length
,
591 unsigned int *psaltlen
, unsigned int *pkeylen
)
593 const unsigned char *p
= *in
;
594 unsigned int pvk_magic
, is_encrypted
;
597 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
602 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
605 pvk_magic
= read_ledword(&p
);
606 if (pvk_magic
!= MS_PVKMAGIC
) {
607 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
616 is_encrypted
= read_ledword(&p
);
617 *psaltlen
= read_ledword(&p
);
618 *pkeylen
= read_ledword(&p
);
620 if (*pkeylen
> PVK_MAX_KEYLEN
|| *psaltlen
> PVK_MAX_SALTLEN
)
623 if (is_encrypted
&& !*psaltlen
) {
624 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_INCONSISTENT_HEADER
);
632 static int derive_pvk_key(unsigned char *key
,
633 const unsigned char *salt
, unsigned int saltlen
,
634 const unsigned char *pass
, int passlen
)
636 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
639 || !EVP_DigestInit_ex(mctx
, EVP_sha1(), NULL
)
640 || !EVP_DigestUpdate(mctx
, salt
, saltlen
)
641 || !EVP_DigestUpdate(mctx
, pass
, passlen
)
642 || !EVP_DigestFinal_ex(mctx
, key
, NULL
))
645 EVP_MD_CTX_free(mctx
);
649 static EVP_PKEY
*do_PVK_body(const unsigned char **in
,
650 unsigned int saltlen
, unsigned int keylen
,
651 pem_password_cb
*cb
, void *u
)
653 EVP_PKEY
*ret
= NULL
;
654 const unsigned char *p
= *in
;
656 unsigned char *enctmp
= NULL
, *q
;
658 EVP_CIPHER_CTX
*cctx
= EVP_CIPHER_CTX_new();
660 char psbuf
[PEM_BUFSIZE
];
661 unsigned char keybuf
[20];
662 int enctmplen
, inlen
;
664 inlen
= cb(psbuf
, PEM_BUFSIZE
, 0, u
);
666 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 0, u
);
668 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_PASSWORD_READ
);
671 enctmp
= OPENSSL_malloc(keylen
+ 8);
672 if (enctmp
== NULL
) {
673 PEMerr(PEM_F_DO_PVK_BODY
, ERR_R_MALLOC_FAILURE
);
676 if (!derive_pvk_key(keybuf
, p
, saltlen
,
677 (unsigned char *)psbuf
, inlen
))
680 /* Copy BLOBHEADER across, decrypt rest */
681 memcpy(enctmp
, p
, 8);
684 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_PVK_TOO_SHORT
);
689 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
691 if (!EVP_DecryptUpdate(cctx
, q
, &enctmplen
, p
, inlen
))
693 if (!EVP_DecryptFinal_ex(cctx
, q
+ enctmplen
, &enctmplen
))
695 magic
= read_ledword((const unsigned char **)&q
);
696 if (magic
!= MS_RSA2MAGIC
&& magic
!= MS_DSS2MAGIC
) {
698 memset(keybuf
+ 5, 0, 11);
699 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
701 OPENSSL_cleanse(keybuf
, 20);
702 if (!EVP_DecryptUpdate(cctx
, q
, &enctmplen
, p
, inlen
))
704 if (!EVP_DecryptFinal_ex(cctx
, q
+ enctmplen
, &enctmplen
))
706 magic
= read_ledword((const unsigned char **)&q
);
707 if (magic
!= MS_RSA2MAGIC
&& magic
!= MS_DSS2MAGIC
) {
708 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_DECRYPT
);
712 OPENSSL_cleanse(keybuf
, 20);
716 ret
= b2i_PrivateKey(&p
, keylen
);
718 EVP_CIPHER_CTX_free(cctx
);
719 OPENSSL_free(enctmp
);
723 EVP_PKEY
*b2i_PVK_bio(BIO
*in
, pem_password_cb
*cb
, void *u
)
725 unsigned char pvk_hdr
[24], *buf
= NULL
;
726 const unsigned char *p
;
728 EVP_PKEY
*ret
= NULL
;
729 unsigned int saltlen
, keylen
;
730 if (BIO_read(in
, pvk_hdr
, 24) != 24) {
731 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
736 if (!do_PVK_header(&p
, 24, 0, &saltlen
, &keylen
))
738 buflen
= (int)keylen
+ saltlen
;
739 buf
= OPENSSL_malloc(buflen
);
741 PEMerr(PEM_F_B2I_PVK_BIO
, ERR_R_MALLOC_FAILURE
);
745 if (BIO_read(in
, buf
, buflen
) != buflen
) {
746 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
749 ret
= do_PVK_body(&p
, saltlen
, keylen
, cb
, u
);
752 OPENSSL_clear_free(buf
, buflen
);
756 static int i2b_PVK(unsigned char **out
, EVP_PKEY
*pk
, int enclevel
,
757 pem_password_cb
*cb
, void *u
)
759 int outlen
= 24, pklen
;
760 unsigned char *p
= NULL
, *salt
= NULL
;
761 EVP_CIPHER_CTX
*cctx
= NULL
;
763 outlen
+= PVK_SALTLEN
;
764 pklen
= do_i2b(NULL
, pk
, 0);
773 p
= OPENSSL_malloc(outlen
);
775 PEMerr(PEM_F_I2B_PVK
, ERR_R_MALLOC_FAILURE
);
780 cctx
= EVP_CIPHER_CTX_new();
784 write_ledword(&p
, MS_PVKMAGIC
);
785 write_ledword(&p
, 0);
786 if (EVP_PKEY_id(pk
) == EVP_PKEY_DSA
)
787 write_ledword(&p
, MS_KEYTYPE_SIGN
);
789 write_ledword(&p
, MS_KEYTYPE_KEYX
);
790 write_ledword(&p
, enclevel
? 1 : 0);
791 write_ledword(&p
, enclevel
? PVK_SALTLEN
: 0);
792 write_ledword(&p
, pklen
);
794 if (RAND_bytes(p
, PVK_SALTLEN
) <= 0)
801 char psbuf
[PEM_BUFSIZE
];
802 unsigned char keybuf
[20];
803 int enctmplen
, inlen
;
805 inlen
= cb(psbuf
, PEM_BUFSIZE
, 1, u
);
807 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 1, u
);
809 PEMerr(PEM_F_I2B_PVK
, PEM_R_BAD_PASSWORD_READ
);
812 if (!derive_pvk_key(keybuf
, salt
, PVK_SALTLEN
,
813 (unsigned char *)psbuf
, inlen
))
816 memset(keybuf
+ 5, 0, 11);
817 p
= salt
+ PVK_SALTLEN
+ 8;
818 if (!EVP_EncryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
820 OPENSSL_cleanse(keybuf
, 20);
821 if (!EVP_DecryptUpdate(cctx
, p
, &enctmplen
, p
, pklen
- 8))
823 if (!EVP_DecryptFinal_ex(cctx
, p
+ enctmplen
, &enctmplen
))
827 EVP_CIPHER_CTX_free(cctx
);
835 EVP_CIPHER_CTX_free(cctx
);
841 int i2b_PVK_bio(BIO
*out
, EVP_PKEY
*pk
, int enclevel
,
842 pem_password_cb
*cb
, void *u
)
844 unsigned char *tmp
= NULL
;
846 outlen
= i2b_PVK(&tmp
, pk
, enclevel
, cb
, u
);
849 wrlen
= BIO_write(out
, tmp
, outlen
);
851 if (wrlen
== outlen
) {
852 PEMerr(PEM_F_I2B_PVK_BIO
, PEM_R_BIO_WRITE_FAILURE
);