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 /* Maximum length of a blob after header */
70 # define BLOB_MAX_LENGTH 102400
72 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
73 # define MS_PVKMAGIC 0xb0b5f11eL
74 /* Salt length for PVK files */
75 # define PVK_SALTLEN 0x10
76 /* Maximum length in PVK header */
77 # define PVK_MAX_KEYLEN 102400
78 /* Maximum salt length */
79 # define PVK_MAX_SALTLEN 10240
81 static EVP_PKEY
*b2i_rsa(const unsigned char **in
,
82 unsigned int bitlen
, int ispub
);
83 static EVP_PKEY
*b2i_dss(const unsigned char **in
,
84 unsigned int bitlen
, int ispub
);
86 static int do_blob_header(const unsigned char **in
, unsigned int length
,
87 unsigned int *pmagic
, unsigned int *pbitlen
,
88 int *pisdss
, int *pispub
)
90 const unsigned char *p
= *in
;
94 if (*p
== MS_PUBLICKEYBLOB
) {
96 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
100 } else if (*p
== MS_PRIVATEKEYBLOB
) {
102 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
111 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_VERSION_NUMBER
);
114 /* Ignore reserved, aiKeyAlg */
116 *pmagic
= read_ledword(&p
);
117 *pbitlen
= read_ledword(&p
);
126 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
136 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
142 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
149 static unsigned int blob_length(unsigned bitlen
, int isdss
, int ispub
)
151 unsigned int nbyte
, hnbyte
;
152 nbyte
= (bitlen
+ 7) >> 3;
153 hnbyte
= (bitlen
+ 15) >> 4;
157 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
161 return 44 + 3 * nbyte
;
163 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
167 return 64 + 2 * nbyte
;
169 /* Expected length: 4 for 'e' + 'n' */
174 * Expected length: 4 for 'e' and 7 other components. 2
175 * components are bitlen size, 5 are bitlen/2
177 return 4 + 2 * nbyte
+ 5 * hnbyte
;
182 static EVP_PKEY
*do_b2i(const unsigned char **in
, unsigned int length
,
185 const unsigned char *p
= *in
;
186 unsigned int bitlen
, magic
;
188 if (do_blob_header(&p
, length
, &magic
, &bitlen
, &isdss
, &ispub
) <= 0) {
189 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_HEADER_PARSE_ERROR
);
193 if (length
< blob_length(bitlen
, isdss
, ispub
)) {
194 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_TOO_SHORT
);
198 return b2i_dss(&p
, bitlen
, ispub
);
200 return b2i_rsa(&p
, bitlen
, ispub
);
203 static EVP_PKEY
*do_b2i_bio(BIO
*in
, int ispub
)
205 const unsigned char *p
;
206 unsigned char hdr_buf
[16], *buf
= NULL
;
207 unsigned int bitlen
, magic
, length
;
209 EVP_PKEY
*ret
= NULL
;
210 if (BIO_read(in
, hdr_buf
, 16) != 16) {
211 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
215 if (do_blob_header(&p
, 16, &magic
, &bitlen
, &isdss
, &ispub
) <= 0)
218 length
= blob_length(bitlen
, isdss
, ispub
);
219 if (length
> BLOB_MAX_LENGTH
) {
220 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_HEADER_TOO_LONG
);
223 buf
= OPENSSL_malloc(length
);
225 PEMerr(PEM_F_DO_B2I_BIO
, ERR_R_MALLOC_FAILURE
);
229 if (BIO_read(in
, buf
, length
) != (int)length
) {
230 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
235 ret
= b2i_dss(&p
, bitlen
, ispub
);
237 ret
= b2i_rsa(&p
, bitlen
, ispub
);
244 static EVP_PKEY
*b2i_dss(const unsigned char **in
,
245 unsigned int bitlen
, int ispub
)
247 const unsigned char *p
= *in
;
248 EVP_PKEY
*ret
= NULL
;
252 BIGNUM
*pbn
= NULL
, *qbn
= NULL
, *gbn
= NULL
, *priv_key
= NULL
;
253 BIGNUM
*pub_key
= NULL
;
255 nbyte
= (bitlen
+ 7) >> 3;
258 ret
= EVP_PKEY_new();
259 if (dsa
== NULL
|| ret
== NULL
)
261 if (!read_lebn(&p
, nbyte
, &pbn
))
264 if (!read_lebn(&p
, 20, &qbn
))
267 if (!read_lebn(&p
, nbyte
, &gbn
))
271 if (!read_lebn(&p
, nbyte
, &pub_key
))
274 if (!read_lebn(&p
, 20, &priv_key
))
277 /* Calculate public key */
281 if ((ctx
= BN_CTX_new()) == NULL
)
284 if (!BN_mod_exp(pub_key
, gbn
, priv_key
, pbn
, ctx
))
289 if (!DSA_set0_pqg(dsa
, pbn
, qbn
, gbn
))
291 pbn
= qbn
= gbn
= NULL
;
292 if (!DSA_set0_key(dsa
, pub_key
, priv_key
))
295 EVP_PKEY_set1_DSA(ret
, dsa
);
301 PEMerr(PEM_F_B2I_DSS
, ERR_R_MALLOC_FAILURE
);
313 static EVP_PKEY
*b2i_rsa(const unsigned char **in
,
314 unsigned int bitlen
, int ispub
)
316 const unsigned char *pin
= *in
;
317 EVP_PKEY
*ret
= NULL
;
318 BIGNUM
*e
= NULL
, *n
= NULL
, *d
= NULL
;
319 BIGNUM
*p
= NULL
, *q
= NULL
, *dmp1
= NULL
, *dmq1
= NULL
, *iqmp
= NULL
;
321 unsigned int nbyte
, hnbyte
;
322 nbyte
= (bitlen
+ 7) >> 3;
323 hnbyte
= (bitlen
+ 15) >> 4;
325 ret
= EVP_PKEY_new();
326 if (rsa
== NULL
|| ret
== NULL
)
331 if (!BN_set_word(e
, read_ledword(&pin
)))
333 if (!read_lebn(&pin
, nbyte
, &n
))
336 if (!read_lebn(&pin
, hnbyte
, &p
))
338 if (!read_lebn(&pin
, hnbyte
, &q
))
340 if (!read_lebn(&pin
, hnbyte
, &dmp1
))
342 if (!read_lebn(&pin
, hnbyte
, &dmq1
))
344 if (!read_lebn(&pin
, hnbyte
, &iqmp
))
346 if (!read_lebn(&pin
, nbyte
, &d
))
348 RSA_set0_factors(rsa
, p
, q
);
349 RSA_set0_crt_params(rsa
, dmp1
, dmq1
, iqmp
);
351 RSA_set0_key(rsa
, n
, e
, d
);
353 EVP_PKEY_set1_RSA(ret
, rsa
);
358 PEMerr(PEM_F_B2I_RSA
, ERR_R_MALLOC_FAILURE
);
372 EVP_PKEY
*b2i_PrivateKey(const unsigned char **in
, long length
)
374 return do_b2i(in
, length
, 0);
377 EVP_PKEY
*b2i_PublicKey(const unsigned char **in
, long length
)
379 return do_b2i(in
, length
, 1);
382 EVP_PKEY
*b2i_PrivateKey_bio(BIO
*in
)
384 return do_b2i_bio(in
, 0);
387 EVP_PKEY
*b2i_PublicKey_bio(BIO
*in
)
389 return do_b2i_bio(in
, 1);
392 static void write_ledword(unsigned char **out
, unsigned int dw
)
394 unsigned char *p
= *out
;
396 *p
++ = (dw
>> 8) & 0xff;
397 *p
++ = (dw
>> 16) & 0xff;
398 *p
++ = (dw
>> 24) & 0xff;
402 static void write_lebn(unsigned char **out
, const BIGNUM
*bn
, int len
)
404 BN_bn2lebinpad(bn
, *out
, len
);
408 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *magic
);
409 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *magic
);
411 static void write_rsa(unsigned char **out
, RSA
*rsa
, int ispub
);
412 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
);
414 static int do_i2b(unsigned char **out
, EVP_PKEY
*pk
, int ispub
)
417 unsigned int bitlen
, magic
= 0, keyalg
;
418 int outlen
, noinc
= 0;
419 int pktype
= EVP_PKEY_id(pk
);
420 if (pktype
== EVP_PKEY_DSA
) {
421 bitlen
= check_bitlen_dsa(EVP_PKEY_get0_DSA(pk
), ispub
, &magic
);
422 keyalg
= MS_KEYALG_DSS_SIGN
;
423 } else if (pktype
== EVP_PKEY_RSA
) {
424 bitlen
= check_bitlen_rsa(EVP_PKEY_get0_RSA(pk
), ispub
, &magic
);
425 keyalg
= MS_KEYALG_RSA_KEYX
;
430 outlen
= 16 + blob_length(bitlen
,
431 keyalg
== MS_KEYALG_DSS_SIGN
? 1 : 0, ispub
);
437 p
= OPENSSL_malloc(outlen
);
444 *p
++ = MS_PUBLICKEYBLOB
;
446 *p
++ = MS_PRIVATEKEYBLOB
;
450 write_ledword(&p
, keyalg
);
451 write_ledword(&p
, magic
);
452 write_ledword(&p
, bitlen
);
453 if (keyalg
== MS_KEYALG_DSS_SIGN
)
454 write_dsa(&p
, EVP_PKEY_get0_DSA(pk
), ispub
);
456 write_rsa(&p
, EVP_PKEY_get0_RSA(pk
), ispub
);
462 static int do_i2b_bio(BIO
*out
, EVP_PKEY
*pk
, int ispub
)
464 unsigned char *tmp
= NULL
;
466 outlen
= do_i2b(&tmp
, pk
, ispub
);
469 wrlen
= BIO_write(out
, tmp
, outlen
);
476 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *pmagic
)
479 const BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
480 const BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
482 DSA_get0_pqg(dsa
, &p
, &q
, &g
);
483 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
484 bitlen
= BN_num_bits(p
);
485 if ((bitlen
& 7) || (BN_num_bits(q
) != 160)
486 || (BN_num_bits(g
) > bitlen
))
489 if (BN_num_bits(pub_key
) > bitlen
)
491 *pmagic
= MS_DSS1MAGIC
;
493 if (BN_num_bits(priv_key
) > 160)
495 *pmagic
= MS_DSS2MAGIC
;
500 PEMerr(PEM_F_CHECK_BITLEN_DSA
, PEM_R_UNSUPPORTED_KEY_COMPONENTS
);
504 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *pmagic
)
506 int nbyte
, hnbyte
, bitlen
;
509 RSA_get0_key(rsa
, NULL
, &e
, NULL
);
510 if (BN_num_bits(e
) > 32)
512 bitlen
= RSA_bits(rsa
);
513 nbyte
= RSA_size(rsa
);
514 hnbyte
= (bitlen
+ 15) >> 4;
516 *pmagic
= MS_RSA1MAGIC
;
519 const BIGNUM
*d
, *p
, *q
, *iqmp
, *dmp1
, *dmq1
;
521 *pmagic
= MS_RSA2MAGIC
;
524 * For private key each component must fit within nbyte or hnbyte.
526 RSA_get0_key(rsa
, NULL
, NULL
, &d
);
527 if (BN_num_bytes(d
) > nbyte
)
529 RSA_get0_factors(rsa
, &p
, &q
);
530 RSA_get0_crt_params(rsa
, &dmp1
, &dmq1
, &iqmp
);
531 if ((BN_num_bytes(iqmp
) > hnbyte
)
532 || (BN_num_bytes(p
) > hnbyte
)
533 || (BN_num_bytes(q
) > hnbyte
)
534 || (BN_num_bytes(dmp1
) > hnbyte
)
535 || (BN_num_bytes(dmq1
) > hnbyte
))
540 PEMerr(PEM_F_CHECK_BITLEN_RSA
, PEM_R_UNSUPPORTED_KEY_COMPONENTS
);
544 static void write_rsa(unsigned char **out
, RSA
*rsa
, int ispub
)
547 const BIGNUM
*n
, *d
, *e
, *p
, *q
, *iqmp
, *dmp1
, *dmq1
;
549 nbyte
= RSA_size(rsa
);
550 hnbyte
= (RSA_bits(rsa
) + 15) >> 4;
551 RSA_get0_key(rsa
, &n
, &e
, &d
);
552 write_lebn(out
, e
, 4);
553 write_lebn(out
, n
, nbyte
);
556 RSA_get0_factors(rsa
, &p
, &q
);
557 RSA_get0_crt_params(rsa
, &dmp1
, &dmq1
, &iqmp
);
558 write_lebn(out
, p
, hnbyte
);
559 write_lebn(out
, q
, hnbyte
);
560 write_lebn(out
, dmp1
, hnbyte
);
561 write_lebn(out
, dmq1
, hnbyte
);
562 write_lebn(out
, iqmp
, hnbyte
);
563 write_lebn(out
, d
, nbyte
);
566 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
)
569 const BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
570 const BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
572 DSA_get0_pqg(dsa
, &p
, &q
, &g
);
573 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
574 nbyte
= BN_num_bytes(p
);
575 write_lebn(out
, p
, nbyte
);
576 write_lebn(out
, q
, 20);
577 write_lebn(out
, g
, nbyte
);
579 write_lebn(out
, pub_key
, nbyte
);
581 write_lebn(out
, priv_key
, 20);
582 /* Set "invalid" for seed structure values */
583 memset(*out
, 0xff, 24);
588 int i2b_PrivateKey_bio(BIO
*out
, EVP_PKEY
*pk
)
590 return do_i2b_bio(out
, pk
, 0);
593 int i2b_PublicKey_bio(BIO
*out
, EVP_PKEY
*pk
)
595 return do_i2b_bio(out
, pk
, 1);
598 # ifndef OPENSSL_NO_RC4
600 static int do_PVK_header(const unsigned char **in
, unsigned int length
,
602 unsigned int *psaltlen
, unsigned int *pkeylen
)
604 const unsigned char *p
= *in
;
605 unsigned int pvk_magic
, is_encrypted
;
608 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
613 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
616 pvk_magic
= read_ledword(&p
);
617 if (pvk_magic
!= MS_PVKMAGIC
) {
618 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
627 is_encrypted
= read_ledword(&p
);
628 *psaltlen
= read_ledword(&p
);
629 *pkeylen
= read_ledword(&p
);
631 if (*pkeylen
> PVK_MAX_KEYLEN
|| *psaltlen
> PVK_MAX_SALTLEN
)
634 if (is_encrypted
&& !*psaltlen
) {
635 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_INCONSISTENT_HEADER
);
643 static int derive_pvk_key(unsigned char *key
,
644 const unsigned char *salt
, unsigned int saltlen
,
645 const unsigned char *pass
, int passlen
)
647 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
650 || !EVP_DigestInit_ex(mctx
, EVP_sha1(), NULL
)
651 || !EVP_DigestUpdate(mctx
, salt
, saltlen
)
652 || !EVP_DigestUpdate(mctx
, pass
, passlen
)
653 || !EVP_DigestFinal_ex(mctx
, key
, NULL
))
656 EVP_MD_CTX_free(mctx
);
660 static EVP_PKEY
*do_PVK_body(const unsigned char **in
,
661 unsigned int saltlen
, unsigned int keylen
,
662 pem_password_cb
*cb
, void *u
)
664 EVP_PKEY
*ret
= NULL
;
665 const unsigned char *p
= *in
;
667 unsigned char *enctmp
= NULL
, *q
;
669 EVP_CIPHER_CTX
*cctx
= EVP_CIPHER_CTX_new();
671 char psbuf
[PEM_BUFSIZE
];
672 unsigned char keybuf
[20];
673 int enctmplen
, inlen
;
675 inlen
= cb(psbuf
, PEM_BUFSIZE
, 0, u
);
677 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 0, u
);
679 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_PASSWORD_READ
);
682 enctmp
= OPENSSL_malloc(keylen
+ 8);
683 if (enctmp
== NULL
) {
684 PEMerr(PEM_F_DO_PVK_BODY
, ERR_R_MALLOC_FAILURE
);
687 if (!derive_pvk_key(keybuf
, p
, saltlen
,
688 (unsigned char *)psbuf
, inlen
))
691 /* Copy BLOBHEADER across, decrypt rest */
692 memcpy(enctmp
, p
, 8);
695 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_PVK_TOO_SHORT
);
700 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
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
) {
709 memset(keybuf
+ 5, 0, 11);
710 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
712 OPENSSL_cleanse(keybuf
, 20);
713 if (!EVP_DecryptUpdate(cctx
, q
, &enctmplen
, p
, inlen
))
715 if (!EVP_DecryptFinal_ex(cctx
, q
+ enctmplen
, &enctmplen
))
717 magic
= read_ledword((const unsigned char **)&q
);
718 if (magic
!= MS_RSA2MAGIC
&& magic
!= MS_DSS2MAGIC
) {
719 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_DECRYPT
);
723 OPENSSL_cleanse(keybuf
, 20);
727 ret
= b2i_PrivateKey(&p
, keylen
);
729 EVP_CIPHER_CTX_free(cctx
);
730 OPENSSL_free(enctmp
);
734 EVP_PKEY
*b2i_PVK_bio(BIO
*in
, pem_password_cb
*cb
, void *u
)
736 unsigned char pvk_hdr
[24], *buf
= NULL
;
737 const unsigned char *p
;
739 EVP_PKEY
*ret
= NULL
;
740 unsigned int saltlen
, keylen
;
741 if (BIO_read(in
, pvk_hdr
, 24) != 24) {
742 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
747 if (!do_PVK_header(&p
, 24, 0, &saltlen
, &keylen
))
749 buflen
= (int)keylen
+ saltlen
;
750 buf
= OPENSSL_malloc(buflen
);
752 PEMerr(PEM_F_B2I_PVK_BIO
, ERR_R_MALLOC_FAILURE
);
756 if (BIO_read(in
, buf
, buflen
) != buflen
) {
757 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
760 ret
= do_PVK_body(&p
, saltlen
, keylen
, cb
, u
);
763 OPENSSL_clear_free(buf
, buflen
);
767 static int i2b_PVK(unsigned char **out
, EVP_PKEY
*pk
, int enclevel
,
768 pem_password_cb
*cb
, void *u
)
770 int outlen
= 24, pklen
;
771 unsigned char *p
= NULL
, *start
= NULL
, *salt
= NULL
;
772 EVP_CIPHER_CTX
*cctx
= NULL
;
774 outlen
+= PVK_SALTLEN
;
775 pklen
= do_i2b(NULL
, pk
, 0);
784 start
= p
= OPENSSL_malloc(outlen
);
786 PEMerr(PEM_F_I2B_PVK
, ERR_R_MALLOC_FAILURE
);
791 cctx
= EVP_CIPHER_CTX_new();
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)
812 char psbuf
[PEM_BUFSIZE
];
813 unsigned char keybuf
[20];
814 int enctmplen
, inlen
;
816 inlen
= cb(psbuf
, PEM_BUFSIZE
, 1, u
);
818 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 1, u
);
820 PEMerr(PEM_F_I2B_PVK
, PEM_R_BAD_PASSWORD_READ
);
823 if (!derive_pvk_key(keybuf
, salt
, PVK_SALTLEN
,
824 (unsigned char *)psbuf
, inlen
))
827 memset(keybuf
+ 5, 0, 11);
828 p
= salt
+ PVK_SALTLEN
+ 8;
829 if (!EVP_EncryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
831 OPENSSL_cleanse(keybuf
, 20);
832 if (!EVP_DecryptUpdate(cctx
, p
, &enctmplen
, p
, pklen
- 8))
834 if (!EVP_DecryptFinal_ex(cctx
, p
+ enctmplen
, &enctmplen
))
838 EVP_CIPHER_CTX_free(cctx
);
846 EVP_CIPHER_CTX_free(cctx
);
852 int i2b_PVK_bio(BIO
*out
, EVP_PKEY
*pk
, int enclevel
,
853 pem_password_cb
*cb
, void *u
)
855 unsigned char *tmp
= NULL
;
857 outlen
= i2b_PVK(&tmp
, pk
, enclevel
, cb
, u
);
860 wrlen
= BIO_write(out
, tmp
, outlen
);
862 if (wrlen
== outlen
) {
863 PEMerr(PEM_F_I2B_PVK_BIO
, PEM_R_BIO_WRITE_FAILURE
);