2 * Copyright 2005-2018 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 * DSA low level APIs are deprecated for public use, but still ok for
19 #include "internal/deprecated.h"
21 #include "internal/cryptlib.h"
22 #include <openssl/pem.h>
23 #include <openssl/rand.h>
24 #include <openssl/bn.h>
25 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
26 # include <openssl/dsa.h>
27 # include <openssl/rsa.h>
30 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
34 static unsigned int read_ledword(const unsigned char **in
)
36 const unsigned char *p
= *in
;
47 * Read a BIGNUM in little endian format. The docs say that this should take
51 static int read_lebn(const unsigned char **in
, unsigned int nbyte
, BIGNUM
**r
)
53 *r
= BN_lebin2bn(*in
, nbyte
, NULL
);
60 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
62 # define MS_PUBLICKEYBLOB 0x6
63 # define MS_PRIVATEKEYBLOB 0x7
64 # define MS_RSA1MAGIC 0x31415352L
65 # define MS_RSA2MAGIC 0x32415352L
66 # define MS_DSS1MAGIC 0x31535344L
67 # define MS_DSS2MAGIC 0x32535344L
69 # define MS_KEYALG_RSA_KEYX 0xa400
70 # define MS_KEYALG_DSS_SIGN 0x2200
72 # define MS_KEYTYPE_KEYX 0x1
73 # define MS_KEYTYPE_SIGN 0x2
75 /* Maximum length of a blob after header */
76 # define BLOB_MAX_LENGTH 102400
78 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
79 # define MS_PVKMAGIC 0xb0b5f11eL
80 /* Salt length for PVK files */
81 # define PVK_SALTLEN 0x10
82 /* Maximum length in PVK header */
83 # define PVK_MAX_KEYLEN 102400
84 /* Maximum salt length */
85 # define PVK_MAX_SALTLEN 10240
87 static EVP_PKEY
*b2i_rsa(const unsigned char **in
,
88 unsigned int bitlen
, int ispub
);
89 static EVP_PKEY
*b2i_dss(const unsigned char **in
,
90 unsigned int bitlen
, int ispub
);
92 static int do_blob_header(const unsigned char **in
, unsigned int length
,
93 unsigned int *pmagic
, unsigned int *pbitlen
,
94 int *pisdss
, int *pispub
)
96 const unsigned char *p
= *in
;
100 if (*p
== MS_PUBLICKEYBLOB
) {
102 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
106 } else if (*p
== MS_PRIVATEKEYBLOB
) {
108 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
117 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_VERSION_NUMBER
);
120 /* Ignore reserved, aiKeyAlg */
122 *pmagic
= read_ledword(&p
);
123 *pbitlen
= read_ledword(&p
);
132 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
142 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
148 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
155 static unsigned int blob_length(unsigned bitlen
, int isdss
, int ispub
)
157 unsigned int nbyte
, hnbyte
;
158 nbyte
= (bitlen
+ 7) >> 3;
159 hnbyte
= (bitlen
+ 15) >> 4;
163 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
167 return 44 + 3 * nbyte
;
169 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
173 return 64 + 2 * nbyte
;
175 /* Expected length: 4 for 'e' + 'n' */
180 * Expected length: 4 for 'e' and 7 other components. 2
181 * components are bitlen size, 5 are bitlen/2
183 return 4 + 2 * nbyte
+ 5 * hnbyte
;
188 static EVP_PKEY
*do_b2i(const unsigned char **in
, unsigned int length
,
191 const unsigned char *p
= *in
;
192 unsigned int bitlen
, magic
;
194 if (do_blob_header(&p
, length
, &magic
, &bitlen
, &isdss
, &ispub
) <= 0) {
195 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_HEADER_PARSE_ERROR
);
199 if (length
< blob_length(bitlen
, isdss
, ispub
)) {
200 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_TOO_SHORT
);
204 return b2i_dss(&p
, bitlen
, ispub
);
206 return b2i_rsa(&p
, bitlen
, ispub
);
209 static EVP_PKEY
*do_b2i_bio(BIO
*in
, int ispub
)
211 const unsigned char *p
;
212 unsigned char hdr_buf
[16], *buf
= NULL
;
213 unsigned int bitlen
, magic
, length
;
215 EVP_PKEY
*ret
= NULL
;
216 if (BIO_read(in
, hdr_buf
, 16) != 16) {
217 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
221 if (do_blob_header(&p
, 16, &magic
, &bitlen
, &isdss
, &ispub
) <= 0)
224 length
= blob_length(bitlen
, isdss
, ispub
);
225 if (length
> BLOB_MAX_LENGTH
) {
226 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_HEADER_TOO_LONG
);
229 buf
= OPENSSL_malloc(length
);
231 PEMerr(PEM_F_DO_B2I_BIO
, ERR_R_MALLOC_FAILURE
);
235 if (BIO_read(in
, buf
, length
) != (int)length
) {
236 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
241 ret
= b2i_dss(&p
, bitlen
, ispub
);
243 ret
= b2i_rsa(&p
, bitlen
, ispub
);
250 static EVP_PKEY
*b2i_dss(const unsigned char **in
,
251 unsigned int bitlen
, int ispub
)
253 const unsigned char *p
= *in
;
254 EVP_PKEY
*ret
= NULL
;
258 BIGNUM
*pbn
= NULL
, *qbn
= NULL
, *gbn
= NULL
, *priv_key
= NULL
;
259 BIGNUM
*pub_key
= NULL
;
261 nbyte
= (bitlen
+ 7) >> 3;
264 ret
= EVP_PKEY_new();
265 if (dsa
== NULL
|| ret
== NULL
)
267 if (!read_lebn(&p
, nbyte
, &pbn
))
270 if (!read_lebn(&p
, 20, &qbn
))
273 if (!read_lebn(&p
, nbyte
, &gbn
))
277 if (!read_lebn(&p
, nbyte
, &pub_key
))
280 if (!read_lebn(&p
, 20, &priv_key
))
283 /* Set constant time flag before public key calculation */
284 BN_set_flags(priv_key
, BN_FLG_CONSTTIME
);
286 /* Calculate public key */
290 if ((ctx
= BN_CTX_new()) == NULL
)
293 if (!BN_mod_exp(pub_key
, gbn
, priv_key
, pbn
, ctx
))
299 if (!DSA_set0_pqg(dsa
, pbn
, qbn
, gbn
))
301 pbn
= qbn
= gbn
= NULL
;
302 if (!DSA_set0_key(dsa
, pub_key
, priv_key
))
304 pub_key
= priv_key
= NULL
;
306 if (!EVP_PKEY_set1_DSA(ret
, dsa
))
313 PEMerr(PEM_F_B2I_DSS
, ERR_R_MALLOC_FAILURE
);
325 static EVP_PKEY
*b2i_rsa(const unsigned char **in
,
326 unsigned int bitlen
, int ispub
)
328 const unsigned char *pin
= *in
;
329 EVP_PKEY
*ret
= NULL
;
330 BIGNUM
*e
= NULL
, *n
= NULL
, *d
= NULL
;
331 BIGNUM
*p
= NULL
, *q
= NULL
, *dmp1
= NULL
, *dmq1
= NULL
, *iqmp
= NULL
;
333 unsigned int nbyte
, hnbyte
;
334 nbyte
= (bitlen
+ 7) >> 3;
335 hnbyte
= (bitlen
+ 15) >> 4;
337 ret
= EVP_PKEY_new();
338 if (rsa
== NULL
|| ret
== NULL
)
343 if (!BN_set_word(e
, read_ledword(&pin
)))
345 if (!read_lebn(&pin
, nbyte
, &n
))
348 if (!read_lebn(&pin
, hnbyte
, &p
))
350 if (!read_lebn(&pin
, hnbyte
, &q
))
352 if (!read_lebn(&pin
, hnbyte
, &dmp1
))
354 if (!read_lebn(&pin
, hnbyte
, &dmq1
))
356 if (!read_lebn(&pin
, hnbyte
, &iqmp
))
358 if (!read_lebn(&pin
, nbyte
, &d
))
360 if (!RSA_set0_factors(rsa
, p
, q
))
363 if (!RSA_set0_crt_params(rsa
, dmp1
, dmq1
, iqmp
))
365 dmp1
= dmq1
= iqmp
= NULL
;
367 if (!RSA_set0_key(rsa
, n
, e
, d
))
371 if (!EVP_PKEY_set1_RSA(ret
, rsa
))
377 PEMerr(PEM_F_B2I_RSA
, ERR_R_MALLOC_FAILURE
);
391 EVP_PKEY
*b2i_PrivateKey(const unsigned char **in
, long length
)
393 return do_b2i(in
, length
, 0);
396 EVP_PKEY
*b2i_PublicKey(const unsigned char **in
, long length
)
398 return do_b2i(in
, length
, 1);
401 EVP_PKEY
*b2i_PrivateKey_bio(BIO
*in
)
403 return do_b2i_bio(in
, 0);
406 EVP_PKEY
*b2i_PublicKey_bio(BIO
*in
)
408 return do_b2i_bio(in
, 1);
411 static void write_ledword(unsigned char **out
, unsigned int dw
)
413 unsigned char *p
= *out
;
415 *p
++ = (dw
>> 8) & 0xff;
416 *p
++ = (dw
>> 16) & 0xff;
417 *p
++ = (dw
>> 24) & 0xff;
421 static void write_lebn(unsigned char **out
, const BIGNUM
*bn
, int len
)
423 BN_bn2lebinpad(bn
, *out
, len
);
427 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *magic
);
428 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *magic
);
430 static void write_rsa(unsigned char **out
, RSA
*rsa
, int ispub
);
431 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
);
433 static int do_i2b(unsigned char **out
, const EVP_PKEY
*pk
, int ispub
)
436 unsigned int bitlen
, magic
= 0, keyalg
;
437 int outlen
, noinc
= 0;
438 int pktype
= EVP_PKEY_id(pk
);
439 if (pktype
== EVP_PKEY_DSA
) {
440 bitlen
= check_bitlen_dsa(EVP_PKEY_get0_DSA(pk
), ispub
, &magic
);
441 keyalg
= MS_KEYALG_DSS_SIGN
;
442 } else if (pktype
== EVP_PKEY_RSA
) {
443 bitlen
= check_bitlen_rsa(EVP_PKEY_get0_RSA(pk
), ispub
, &magic
);
444 keyalg
= MS_KEYALG_RSA_KEYX
;
449 outlen
= 16 + blob_length(bitlen
,
450 keyalg
== MS_KEYALG_DSS_SIGN
? 1 : 0, ispub
);
456 if ((p
= OPENSSL_malloc(outlen
)) == NULL
) {
457 PEMerr(PEM_F_DO_I2B
, ERR_R_MALLOC_FAILURE
);
464 *p
++ = MS_PUBLICKEYBLOB
;
466 *p
++ = MS_PRIVATEKEYBLOB
;
470 write_ledword(&p
, keyalg
);
471 write_ledword(&p
, magic
);
472 write_ledword(&p
, bitlen
);
473 if (keyalg
== MS_KEYALG_DSS_SIGN
)
474 write_dsa(&p
, EVP_PKEY_get0_DSA(pk
), ispub
);
476 write_rsa(&p
, EVP_PKEY_get0_RSA(pk
), ispub
);
482 static int do_i2b_bio(BIO
*out
, const EVP_PKEY
*pk
, int ispub
)
484 unsigned char *tmp
= NULL
;
486 outlen
= do_i2b(&tmp
, pk
, ispub
);
489 wrlen
= BIO_write(out
, tmp
, outlen
);
496 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *pmagic
)
499 const BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
500 const BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
502 DSA_get0_pqg(dsa
, &p
, &q
, &g
);
503 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
504 bitlen
= BN_num_bits(p
);
505 if ((bitlen
& 7) || (BN_num_bits(q
) != 160)
506 || (BN_num_bits(g
) > bitlen
))
509 if (BN_num_bits(pub_key
) > bitlen
)
511 *pmagic
= MS_DSS1MAGIC
;
513 if (BN_num_bits(priv_key
) > 160)
515 *pmagic
= MS_DSS2MAGIC
;
520 PEMerr(PEM_F_CHECK_BITLEN_DSA
, PEM_R_UNSUPPORTED_KEY_COMPONENTS
);
524 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *pmagic
)
526 int nbyte
, hnbyte
, bitlen
;
529 RSA_get0_key(rsa
, NULL
, &e
, NULL
);
530 if (BN_num_bits(e
) > 32)
532 bitlen
= RSA_bits(rsa
);
533 nbyte
= RSA_size(rsa
);
534 hnbyte
= (bitlen
+ 15) >> 4;
536 *pmagic
= MS_RSA1MAGIC
;
539 const BIGNUM
*d
, *p
, *q
, *iqmp
, *dmp1
, *dmq1
;
541 *pmagic
= MS_RSA2MAGIC
;
544 * For private key each component must fit within nbyte or hnbyte.
546 RSA_get0_key(rsa
, NULL
, NULL
, &d
);
547 if (BN_num_bytes(d
) > nbyte
)
549 RSA_get0_factors(rsa
, &p
, &q
);
550 RSA_get0_crt_params(rsa
, &dmp1
, &dmq1
, &iqmp
);
551 if ((BN_num_bytes(iqmp
) > hnbyte
)
552 || (BN_num_bytes(p
) > hnbyte
)
553 || (BN_num_bytes(q
) > hnbyte
)
554 || (BN_num_bytes(dmp1
) > hnbyte
)
555 || (BN_num_bytes(dmq1
) > hnbyte
))
560 PEMerr(PEM_F_CHECK_BITLEN_RSA
, PEM_R_UNSUPPORTED_KEY_COMPONENTS
);
564 static void write_rsa(unsigned char **out
, RSA
*rsa
, int ispub
)
567 const BIGNUM
*n
, *d
, *e
, *p
, *q
, *iqmp
, *dmp1
, *dmq1
;
569 nbyte
= RSA_size(rsa
);
570 hnbyte
= (RSA_bits(rsa
) + 15) >> 4;
571 RSA_get0_key(rsa
, &n
, &e
, &d
);
572 write_lebn(out
, e
, 4);
573 write_lebn(out
, n
, nbyte
);
576 RSA_get0_factors(rsa
, &p
, &q
);
577 RSA_get0_crt_params(rsa
, &dmp1
, &dmq1
, &iqmp
);
578 write_lebn(out
, p
, hnbyte
);
579 write_lebn(out
, q
, hnbyte
);
580 write_lebn(out
, dmp1
, hnbyte
);
581 write_lebn(out
, dmq1
, hnbyte
);
582 write_lebn(out
, iqmp
, hnbyte
);
583 write_lebn(out
, d
, nbyte
);
586 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
)
589 const BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
590 const BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
592 DSA_get0_pqg(dsa
, &p
, &q
, &g
);
593 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
594 nbyte
= BN_num_bytes(p
);
595 write_lebn(out
, p
, nbyte
);
596 write_lebn(out
, q
, 20);
597 write_lebn(out
, g
, nbyte
);
599 write_lebn(out
, pub_key
, nbyte
);
601 write_lebn(out
, priv_key
, 20);
602 /* Set "invalid" for seed structure values */
603 memset(*out
, 0xff, 24);
608 int i2b_PrivateKey_bio(BIO
*out
, const EVP_PKEY
*pk
)
610 return do_i2b_bio(out
, pk
, 0);
613 int i2b_PublicKey_bio(BIO
*out
, const EVP_PKEY
*pk
)
615 return do_i2b_bio(out
, pk
, 1);
618 # ifndef OPENSSL_NO_RC4
620 static int do_PVK_header(const unsigned char **in
, unsigned int length
,
622 unsigned int *psaltlen
, unsigned int *pkeylen
)
624 const unsigned char *p
= *in
;
625 unsigned int pvk_magic
, is_encrypted
;
629 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
634 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
637 pvk_magic
= read_ledword(&p
);
638 if (pvk_magic
!= MS_PVKMAGIC
) {
639 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
648 is_encrypted
= read_ledword(&p
);
649 *psaltlen
= read_ledword(&p
);
650 *pkeylen
= read_ledword(&p
);
652 if (*pkeylen
> PVK_MAX_KEYLEN
|| *psaltlen
> PVK_MAX_SALTLEN
)
655 if (is_encrypted
&& *psaltlen
== 0) {
656 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_INCONSISTENT_HEADER
);
664 static int derive_pvk_key(unsigned char *key
,
665 const unsigned char *salt
, unsigned int saltlen
,
666 const unsigned char *pass
, int passlen
)
668 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
671 || !EVP_DigestInit_ex(mctx
, EVP_sha1(), NULL
)
672 || !EVP_DigestUpdate(mctx
, salt
, saltlen
)
673 || !EVP_DigestUpdate(mctx
, pass
, passlen
)
674 || !EVP_DigestFinal_ex(mctx
, key
, NULL
))
677 EVP_MD_CTX_free(mctx
);
681 static EVP_PKEY
*do_PVK_body(const unsigned char **in
,
682 unsigned int saltlen
, unsigned int keylen
,
683 pem_password_cb
*cb
, void *u
)
685 EVP_PKEY
*ret
= NULL
;
686 const unsigned char *p
= *in
;
688 unsigned char *enctmp
= NULL
, *q
;
689 unsigned char keybuf
[20];
691 EVP_CIPHER_CTX
*cctx
= EVP_CIPHER_CTX_new();
693 char psbuf
[PEM_BUFSIZE
];
694 int enctmplen
, inlen
;
696 inlen
= cb(psbuf
, PEM_BUFSIZE
, 0, u
);
698 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 0, u
);
700 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_PASSWORD_READ
);
703 enctmp
= OPENSSL_malloc(keylen
+ 8);
704 if (enctmp
== NULL
) {
705 PEMerr(PEM_F_DO_PVK_BODY
, ERR_R_MALLOC_FAILURE
);
708 if (!derive_pvk_key(keybuf
, p
, saltlen
,
709 (unsigned char *)psbuf
, inlen
))
712 /* Copy BLOBHEADER across, decrypt rest */
713 memcpy(enctmp
, p
, 8);
716 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_PVK_TOO_SHORT
);
721 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
723 if (!EVP_DecryptUpdate(cctx
, q
, &enctmplen
, p
, inlen
))
725 if (!EVP_DecryptFinal_ex(cctx
, q
+ enctmplen
, &enctmplen
))
727 magic
= read_ledword((const unsigned char **)&q
);
728 if (magic
!= MS_RSA2MAGIC
&& magic
!= MS_DSS2MAGIC
) {
730 memset(keybuf
+ 5, 0, 11);
731 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
733 if (!EVP_DecryptUpdate(cctx
, q
, &enctmplen
, p
, inlen
))
735 if (!EVP_DecryptFinal_ex(cctx
, q
+ enctmplen
, &enctmplen
))
737 magic
= read_ledword((const unsigned char **)&q
);
738 if (magic
!= MS_RSA2MAGIC
&& magic
!= MS_DSS2MAGIC
) {
739 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_DECRYPT
);
746 ret
= b2i_PrivateKey(&p
, keylen
);
748 EVP_CIPHER_CTX_free(cctx
);
749 if (enctmp
!= NULL
) {
750 OPENSSL_cleanse(keybuf
, sizeof(keybuf
));
751 OPENSSL_free(enctmp
);
756 EVP_PKEY
*b2i_PVK_bio(BIO
*in
, pem_password_cb
*cb
, void *u
)
758 unsigned char pvk_hdr
[24], *buf
= NULL
;
759 const unsigned char *p
;
761 EVP_PKEY
*ret
= NULL
;
762 unsigned int saltlen
, keylen
;
763 if (BIO_read(in
, pvk_hdr
, 24) != 24) {
764 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
769 if (!do_PVK_header(&p
, 24, 0, &saltlen
, &keylen
))
771 buflen
= (int)keylen
+ saltlen
;
772 buf
= OPENSSL_malloc(buflen
);
774 PEMerr(PEM_F_B2I_PVK_BIO
, ERR_R_MALLOC_FAILURE
);
778 if (BIO_read(in
, buf
, buflen
) != buflen
) {
779 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
782 ret
= do_PVK_body(&p
, saltlen
, keylen
, cb
, u
);
785 OPENSSL_clear_free(buf
, buflen
);
789 static int i2b_PVK(unsigned char **out
, const EVP_PKEY
*pk
, int enclevel
,
790 pem_password_cb
*cb
, void *u
)
792 int outlen
= 24, pklen
;
793 unsigned char *p
= NULL
, *start
= NULL
, *salt
= NULL
;
794 EVP_CIPHER_CTX
*cctx
= NULL
;
796 outlen
+= PVK_SALTLEN
;
797 pklen
= do_i2b(NULL
, pk
, 0);
806 start
= p
= OPENSSL_malloc(outlen
);
808 PEMerr(PEM_F_I2B_PVK
, ERR_R_MALLOC_FAILURE
);
813 cctx
= EVP_CIPHER_CTX_new();
817 write_ledword(&p
, MS_PVKMAGIC
);
818 write_ledword(&p
, 0);
819 if (EVP_PKEY_id(pk
) == EVP_PKEY_DSA
)
820 write_ledword(&p
, MS_KEYTYPE_SIGN
);
822 write_ledword(&p
, MS_KEYTYPE_KEYX
);
823 write_ledword(&p
, enclevel
? 1 : 0);
824 write_ledword(&p
, enclevel
? PVK_SALTLEN
: 0);
825 write_ledword(&p
, pklen
);
827 if (RAND_bytes(p
, PVK_SALTLEN
) <= 0)
834 char psbuf
[PEM_BUFSIZE
];
835 unsigned char keybuf
[20];
836 int enctmplen
, inlen
;
838 inlen
= cb(psbuf
, PEM_BUFSIZE
, 1, u
);
840 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 1, u
);
842 PEMerr(PEM_F_I2B_PVK
, PEM_R_BAD_PASSWORD_READ
);
845 if (!derive_pvk_key(keybuf
, salt
, PVK_SALTLEN
,
846 (unsigned char *)psbuf
, inlen
))
849 memset(keybuf
+ 5, 0, 11);
850 p
= salt
+ PVK_SALTLEN
+ 8;
851 if (!EVP_EncryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
853 OPENSSL_cleanse(keybuf
, 20);
854 if (!EVP_DecryptUpdate(cctx
, p
, &enctmplen
, p
, pklen
- 8))
856 if (!EVP_DecryptFinal_ex(cctx
, p
+ enctmplen
, &enctmplen
))
860 EVP_CIPHER_CTX_free(cctx
);
868 EVP_CIPHER_CTX_free(cctx
);
874 int i2b_PVK_bio(BIO
*out
, const EVP_PKEY
*pk
, int enclevel
,
875 pem_password_cb
*cb
, void *u
)
877 unsigned char *tmp
= NULL
;
879 outlen
= i2b_PVK(&tmp
, pk
, enclevel
, cb
, u
);
882 wrlen
= BIO_write(out
, tmp
, outlen
);
884 if (wrlen
== outlen
) {
885 PEMerr(PEM_F_I2B_PVK_BIO
, PEM_R_BIO_WRITE_FAILURE
);