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 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 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
131 # define MS_PVKMAGIC 0xb0b5f11eL
132 /* Salt length for PVK files */
133 # define PVK_SALTLEN 0x10
135 static EVP_PKEY
*b2i_rsa(const unsigned char **in
, unsigned int length
,
136 unsigned int bitlen
, int ispub
);
137 static EVP_PKEY
*b2i_dss(const unsigned char **in
, unsigned int length
,
138 unsigned int bitlen
, int ispub
);
140 static int do_blob_header(const unsigned char **in
, unsigned int length
,
141 unsigned int *pmagic
, unsigned int *pbitlen
,
142 int *pisdss
, int *pispub
)
144 const unsigned char *p
= *in
;
148 if (*p
== MS_PUBLICKEYBLOB
) {
150 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
154 } else if (*p
== MS_PRIVATEKEYBLOB
) {
156 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
165 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_VERSION_NUMBER
);
168 /* Ignore reserved, aiKeyAlg */
170 *pmagic
= read_ledword(&p
);
171 *pbitlen
= read_ledword(&p
);
179 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PRIVATE_KEY_BLOB
);
188 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_EXPECTING_PUBLIC_KEY_BLOB
);
194 PEMerr(PEM_F_DO_BLOB_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
201 static unsigned int blob_length(unsigned bitlen
, int isdss
, int ispub
)
203 unsigned int nbyte
, hnbyte
;
204 nbyte
= (bitlen
+ 7) >> 3;
205 hnbyte
= (bitlen
+ 15) >> 4;
209 * Expected length: 20 for q + 3 components bitlen each + 24 for seed
213 return 44 + 3 * nbyte
;
215 * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
219 return 64 + 2 * nbyte
;
221 /* Expected length: 4 for 'e' + 'n' */
226 * Expected length: 4 for 'e' and 7 other components. 2
227 * components are bitlen size, 5 are bitlen/2
229 return 4 + 2 * nbyte
+ 5 * hnbyte
;
234 static EVP_PKEY
*do_b2i(const unsigned char **in
, unsigned int length
,
237 const unsigned char *p
= *in
;
238 unsigned int bitlen
, magic
;
240 if (do_blob_header(&p
, length
, &magic
, &bitlen
, &isdss
, &ispub
) <= 0) {
241 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_HEADER_PARSE_ERROR
);
245 if (length
< blob_length(bitlen
, isdss
, ispub
)) {
246 PEMerr(PEM_F_DO_B2I
, PEM_R_KEYBLOB_TOO_SHORT
);
250 return b2i_dss(&p
, length
, bitlen
, ispub
);
252 return b2i_rsa(&p
, length
, bitlen
, ispub
);
255 static EVP_PKEY
*do_b2i_bio(BIO
*in
, int ispub
)
257 const unsigned char *p
;
258 unsigned char hdr_buf
[16], *buf
= NULL
;
259 unsigned int bitlen
, magic
, length
;
261 EVP_PKEY
*ret
= NULL
;
262 if (BIO_read(in
, hdr_buf
, 16) != 16) {
263 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
267 if (do_blob_header(&p
, 16, &magic
, &bitlen
, &isdss
, &ispub
) <= 0)
270 length
= blob_length(bitlen
, isdss
, ispub
);
271 buf
= OPENSSL_malloc(length
);
273 PEMerr(PEM_F_DO_B2I_BIO
, ERR_R_MALLOC_FAILURE
);
277 if (BIO_read(in
, buf
, length
) != (int)length
) {
278 PEMerr(PEM_F_DO_B2I_BIO
, PEM_R_KEYBLOB_TOO_SHORT
);
283 ret
= b2i_dss(&p
, length
, bitlen
, ispub
);
285 ret
= b2i_rsa(&p
, length
, bitlen
, ispub
);
292 static EVP_PKEY
*b2i_dss(const unsigned char **in
, unsigned int length
,
293 unsigned int bitlen
, int ispub
)
295 const unsigned char *p
= *in
;
296 EVP_PKEY
*ret
= NULL
;
300 nbyte
= (bitlen
+ 7) >> 3;
303 ret
= EVP_PKEY_new();
304 if (dsa
== NULL
|| ret
== NULL
)
306 if (!read_lebn(&p
, nbyte
, &dsa
->p
))
308 if (!read_lebn(&p
, 20, &dsa
->q
))
310 if (!read_lebn(&p
, nbyte
, &dsa
->g
))
313 if (!read_lebn(&p
, nbyte
, &dsa
->pub_key
))
316 if (!read_lebn(&p
, 20, &dsa
->priv_key
))
318 /* Calculate public key */
319 if ((dsa
->pub_key
= BN_new()) == NULL
)
321 if ((ctx
= BN_CTX_new()) == NULL
)
324 if (!BN_mod_exp(dsa
->pub_key
, dsa
->g
, dsa
->priv_key
, dsa
->p
, ctx
))
329 EVP_PKEY_set1_DSA(ret
, dsa
);
335 PEMerr(PEM_F_B2I_DSS
, ERR_R_MALLOC_FAILURE
);
342 static EVP_PKEY
*b2i_rsa(const unsigned char **in
, unsigned int length
,
343 unsigned int bitlen
, int ispub
)
345 const unsigned char *p
= *in
;
346 EVP_PKEY
*ret
= NULL
;
348 unsigned int nbyte
, hnbyte
;
349 nbyte
= (bitlen
+ 7) >> 3;
350 hnbyte
= (bitlen
+ 15) >> 4;
352 ret
= EVP_PKEY_new();
353 if (rsa
== NULL
|| ret
== NULL
)
358 if (!BN_set_word(rsa
->e
, read_ledword(&p
)))
360 if (!read_lebn(&p
, nbyte
, &rsa
->n
))
363 if (!read_lebn(&p
, hnbyte
, &rsa
->p
))
365 if (!read_lebn(&p
, hnbyte
, &rsa
->q
))
367 if (!read_lebn(&p
, hnbyte
, &rsa
->dmp1
))
369 if (!read_lebn(&p
, hnbyte
, &rsa
->dmq1
))
371 if (!read_lebn(&p
, hnbyte
, &rsa
->iqmp
))
373 if (!read_lebn(&p
, nbyte
, &rsa
->d
))
377 EVP_PKEY_set1_RSA(ret
, rsa
);
382 PEMerr(PEM_F_B2I_RSA
, ERR_R_MALLOC_FAILURE
);
388 EVP_PKEY
*b2i_PrivateKey(const unsigned char **in
, long length
)
390 return do_b2i(in
, length
, 0);
393 EVP_PKEY
*b2i_PublicKey(const unsigned char **in
, long length
)
395 return do_b2i(in
, length
, 1);
398 EVP_PKEY
*b2i_PrivateKey_bio(BIO
*in
)
400 return do_b2i_bio(in
, 0);
403 EVP_PKEY
*b2i_PublicKey_bio(BIO
*in
)
405 return do_b2i_bio(in
, 1);
408 static void write_ledword(unsigned char **out
, unsigned int dw
)
410 unsigned char *p
= *out
;
412 *p
++ = (dw
>> 8) & 0xff;
413 *p
++ = (dw
>> 16) & 0xff;
414 *p
++ = (dw
>> 24) & 0xff;
418 static void write_lebn(unsigned char **out
, const BIGNUM
*bn
, int len
)
421 unsigned char *p
= *out
, *q
, c
;
422 nb
= BN_num_bytes(bn
);
425 /* In place byte order reversal */
426 for (i
= 0; i
< nb
/ 2; i
++) {
432 /* Pad with zeroes if we have to */
436 memset(*out
, 0, len
);
442 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *magic
);
443 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *magic
);
445 static void write_rsa(unsigned char **out
, RSA
*rsa
, int ispub
);
446 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
);
448 static int do_i2b(unsigned char **out
, EVP_PKEY
*pk
, int ispub
)
451 unsigned int bitlen
, magic
= 0, keyalg
;
452 int outlen
, noinc
= 0;
453 int pktype
= EVP_PKEY_id(pk
);
454 if (pktype
== EVP_PKEY_DSA
) {
455 bitlen
= check_bitlen_dsa(EVP_PKEY_get0_DSA(pk
), ispub
, &magic
);
456 keyalg
= MS_KEYALG_DSS_SIGN
;
457 } else if (pktype
== EVP_PKEY_RSA
) {
458 bitlen
= check_bitlen_rsa(EVP_PKEY_get0_RSA(pk
), ispub
, &magic
);
459 keyalg
= MS_KEYALG_RSA_KEYX
;
464 outlen
= 16 + blob_length(bitlen
,
465 keyalg
== MS_KEYALG_DSS_SIGN
? 1 : 0, ispub
);
471 p
= OPENSSL_malloc(outlen
);
478 *p
++ = MS_PUBLICKEYBLOB
;
480 *p
++ = MS_PRIVATEKEYBLOB
;
484 write_ledword(&p
, keyalg
);
485 write_ledword(&p
, magic
);
486 write_ledword(&p
, bitlen
);
487 if (keyalg
== MS_KEYALG_DSS_SIGN
)
488 write_dsa(&p
, EVP_PKEY_get0_DSA(pk
), ispub
);
490 write_rsa(&p
, EVP_PKEY_get0_RSA(pk
), ispub
);
496 static int do_i2b_bio(BIO
*out
, EVP_PKEY
*pk
, int ispub
)
498 unsigned char *tmp
= NULL
;
500 outlen
= do_i2b(&tmp
, pk
, ispub
);
503 wrlen
= BIO_write(out
, tmp
, outlen
);
510 static int check_bitlen_dsa(DSA
*dsa
, int ispub
, unsigned int *pmagic
)
513 bitlen
= BN_num_bits(dsa
->p
);
514 if ((bitlen
& 7) || (BN_num_bits(dsa
->q
) != 160)
515 || (BN_num_bits(dsa
->g
) > bitlen
))
518 if (BN_num_bits(dsa
->pub_key
) > bitlen
)
520 *pmagic
= MS_DSS1MAGIC
;
522 if (BN_num_bits(dsa
->priv_key
) > 160)
524 *pmagic
= MS_DSS2MAGIC
;
529 PEMerr(PEM_F_CHECK_BITLEN_DSA
, PEM_R_UNSUPPORTED_KEY_COMPONENTS
);
533 static int check_bitlen_rsa(RSA
*rsa
, int ispub
, unsigned int *pmagic
)
535 int nbyte
, hnbyte
, bitlen
;
536 if (BN_num_bits(rsa
->e
) > 32)
538 bitlen
= BN_num_bits(rsa
->n
);
539 nbyte
= BN_num_bytes(rsa
->n
);
540 hnbyte
= (BN_num_bits(rsa
->n
) + 15) >> 4;
542 *pmagic
= MS_RSA1MAGIC
;
545 *pmagic
= MS_RSA2MAGIC
;
547 * For private key each component must fit within nbyte or hnbyte.
549 if (BN_num_bytes(rsa
->d
) > nbyte
)
551 if ((BN_num_bytes(rsa
->iqmp
) > hnbyte
)
552 || (BN_num_bytes(rsa
->p
) > hnbyte
)
553 || (BN_num_bytes(rsa
->q
) > hnbyte
)
554 || (BN_num_bytes(rsa
->dmp1
) > hnbyte
)
555 || (BN_num_bytes(rsa
->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 nbyte
= BN_num_bytes(rsa
->n
);
568 hnbyte
= (BN_num_bits(rsa
->n
) + 15) >> 4;
569 write_lebn(out
, rsa
->e
, 4);
570 write_lebn(out
, rsa
->n
, -1);
573 write_lebn(out
, rsa
->p
, hnbyte
);
574 write_lebn(out
, rsa
->q
, hnbyte
);
575 write_lebn(out
, rsa
->dmp1
, hnbyte
);
576 write_lebn(out
, rsa
->dmq1
, hnbyte
);
577 write_lebn(out
, rsa
->iqmp
, hnbyte
);
578 write_lebn(out
, rsa
->d
, nbyte
);
581 static void write_dsa(unsigned char **out
, DSA
*dsa
, int ispub
)
584 nbyte
= BN_num_bytes(dsa
->p
);
585 write_lebn(out
, dsa
->p
, nbyte
);
586 write_lebn(out
, dsa
->q
, 20);
587 write_lebn(out
, dsa
->g
, nbyte
);
589 write_lebn(out
, dsa
->pub_key
, nbyte
);
591 write_lebn(out
, dsa
->priv_key
, 20);
592 /* Set "invalid" for seed structure values */
593 memset(*out
, 0xff, 24);
598 int i2b_PrivateKey_bio(BIO
*out
, EVP_PKEY
*pk
)
600 return do_i2b_bio(out
, pk
, 0);
603 int i2b_PublicKey_bio(BIO
*out
, EVP_PKEY
*pk
)
605 return do_i2b_bio(out
, pk
, 1);
608 # ifndef OPENSSL_NO_RC4
610 static int do_PVK_header(const unsigned char **in
, unsigned int length
,
612 unsigned int *psaltlen
, unsigned int *pkeylen
)
614 const unsigned char *p
= *in
;
615 unsigned int pvk_magic
, is_encrypted
;
618 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
623 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_PVK_TOO_SHORT
);
626 pvk_magic
= read_ledword(&p
);
627 if (pvk_magic
!= MS_PVKMAGIC
) {
628 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_BAD_MAGIC_NUMBER
);
637 is_encrypted
= read_ledword(&p
);
638 *psaltlen
= read_ledword(&p
);
639 *pkeylen
= read_ledword(&p
);
641 if (is_encrypted
&& !*psaltlen
) {
642 PEMerr(PEM_F_DO_PVK_HEADER
, PEM_R_INCONSISTENT_HEADER
);
650 static int derive_pvk_key(unsigned char *key
,
651 const unsigned char *salt
, unsigned int saltlen
,
652 const unsigned char *pass
, int passlen
)
654 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
657 || !EVP_DigestInit_ex(mctx
, EVP_sha1(), NULL
)
658 || !EVP_DigestUpdate(mctx
, salt
, saltlen
)
659 || !EVP_DigestUpdate(mctx
, pass
, passlen
)
660 || !EVP_DigestFinal_ex(mctx
, key
, NULL
))
663 EVP_MD_CTX_free(mctx
);
667 static EVP_PKEY
*do_PVK_body(const unsigned char **in
,
668 unsigned int saltlen
, unsigned int keylen
,
669 pem_password_cb
*cb
, void *u
)
671 EVP_PKEY
*ret
= NULL
;
672 const unsigned char *p
= *in
;
674 unsigned char *enctmp
= NULL
, *q
;
676 EVP_CIPHER_CTX
*cctx
= EVP_CIPHER_CTX_new();
678 char psbuf
[PEM_BUFSIZE
];
679 unsigned char keybuf
[20];
680 int enctmplen
, inlen
;
682 inlen
= cb(psbuf
, PEM_BUFSIZE
, 0, u
);
684 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 0, u
);
686 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_PASSWORD_READ
);
689 enctmp
= OPENSSL_malloc(keylen
+ 8);
690 if (enctmp
== NULL
) {
691 PEMerr(PEM_F_DO_PVK_BODY
, ERR_R_MALLOC_FAILURE
);
694 if (!derive_pvk_key(keybuf
, p
, saltlen
,
695 (unsigned char *)psbuf
, inlen
))
698 /* Copy BLOBHEADER across, decrypt rest */
699 memcpy(enctmp
, p
, 8);
702 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_PVK_TOO_SHORT
);
707 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
709 if (!EVP_DecryptUpdate(cctx
, q
, &enctmplen
, p
, inlen
))
711 if (!EVP_DecryptFinal_ex(cctx
, q
+ enctmplen
, &enctmplen
))
713 magic
= read_ledword((const unsigned char **)&q
);
714 if (magic
!= MS_RSA2MAGIC
&& magic
!= MS_DSS2MAGIC
) {
716 memset(keybuf
+ 5, 0, 11);
717 if (!EVP_DecryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
719 OPENSSL_cleanse(keybuf
, 20);
720 if (!EVP_DecryptUpdate(cctx
, q
, &enctmplen
, p
, inlen
))
722 if (!EVP_DecryptFinal_ex(cctx
, q
+ enctmplen
, &enctmplen
))
724 magic
= read_ledword((const unsigned char **)&q
);
725 if (magic
!= MS_RSA2MAGIC
&& magic
!= MS_DSS2MAGIC
) {
726 PEMerr(PEM_F_DO_PVK_BODY
, PEM_R_BAD_DECRYPT
);
730 OPENSSL_cleanse(keybuf
, 20);
734 ret
= b2i_PrivateKey(&p
, keylen
);
736 EVP_CIPHER_CTX_free(cctx
);
737 OPENSSL_free(enctmp
);
741 EVP_PKEY
*b2i_PVK_bio(BIO
*in
, pem_password_cb
*cb
, void *u
)
743 unsigned char pvk_hdr
[24], *buf
= NULL
;
744 const unsigned char *p
;
746 EVP_PKEY
*ret
= NULL
;
747 unsigned int saltlen
, keylen
;
748 if (BIO_read(in
, pvk_hdr
, 24) != 24) {
749 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
754 if (!do_PVK_header(&p
, 24, 0, &saltlen
, &keylen
))
756 buflen
= (int)keylen
+ saltlen
;
757 buf
= OPENSSL_malloc(buflen
);
759 PEMerr(PEM_F_B2I_PVK_BIO
, ERR_R_MALLOC_FAILURE
);
763 if (BIO_read(in
, buf
, buflen
) != buflen
) {
764 PEMerr(PEM_F_B2I_PVK_BIO
, PEM_R_PVK_DATA_TOO_SHORT
);
767 ret
= do_PVK_body(&p
, saltlen
, keylen
, cb
, u
);
770 OPENSSL_clear_free(buf
, buflen
);
774 static int i2b_PVK(unsigned char **out
, EVP_PKEY
*pk
, int enclevel
,
775 pem_password_cb
*cb
, void *u
)
777 int outlen
= 24, pklen
;
778 unsigned char *p
, *salt
= NULL
;
779 EVP_CIPHER_CTX
*cctx
= EVP_CIPHER_CTX_new();
781 outlen
+= PVK_SALTLEN
;
782 pklen
= do_i2b(NULL
, pk
, 0);
791 p
= OPENSSL_malloc(outlen
);
793 PEMerr(PEM_F_I2B_PVK
, ERR_R_MALLOC_FAILURE
);
799 write_ledword(&p
, MS_PVKMAGIC
);
800 write_ledword(&p
, 0);
801 if (EVP_PKEY_id(pk
) == EVP_PKEY_DSA
)
802 write_ledword(&p
, MS_KEYTYPE_SIGN
);
804 write_ledword(&p
, MS_KEYTYPE_KEYX
);
805 write_ledword(&p
, enclevel
? 1 : 0);
806 write_ledword(&p
, enclevel
? PVK_SALTLEN
: 0);
807 write_ledword(&p
, pklen
);
809 if (RAND_bytes(p
, PVK_SALTLEN
) <= 0)
818 char psbuf
[PEM_BUFSIZE
];
819 unsigned char keybuf
[20];
820 int enctmplen
, inlen
;
822 inlen
= cb(psbuf
, PEM_BUFSIZE
, 1, u
);
824 inlen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 1, u
);
826 PEMerr(PEM_F_I2B_PVK
, PEM_R_BAD_PASSWORD_READ
);
829 if (!derive_pvk_key(keybuf
, salt
, PVK_SALTLEN
,
830 (unsigned char *)psbuf
, inlen
))
833 memset(keybuf
+ 5, 0, 11);
834 p
= salt
+ PVK_SALTLEN
+ 8;
835 if (!EVP_EncryptInit_ex(cctx
, EVP_rc4(), NULL
, keybuf
, NULL
))
837 OPENSSL_cleanse(keybuf
, 20);
838 if (!EVP_DecryptUpdate(cctx
, p
, &enctmplen
, p
, pklen
- 8))
840 if (!EVP_DecryptFinal_ex(cctx
, p
+ enctmplen
, &enctmplen
))
843 EVP_CIPHER_CTX_free(cctx
);
847 EVP_CIPHER_CTX_free(cctx
);
851 int i2b_PVK_bio(BIO
*out
, EVP_PKEY
*pk
, int enclevel
,
852 pem_password_cb
*cb
, void *u
)
854 unsigned char *tmp
= NULL
;
856 outlen
= i2b_PVK(&tmp
, pk
, enclevel
, cb
, u
);
859 wrlen
= BIO_write(out
, tmp
, outlen
);
861 if (wrlen
== outlen
) {
862 PEMerr(PEM_F_I2B_PVK_BIO
, PEM_R_BIO_WRITE_FAILURE
);