1 /* $OpenBSD: pvkfmt.c,v 1.20 2018/08/05 11:19:25 bcook Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
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).
59 /* Support for PVK format keys and related structures (such a PUBLICKEYBLOB
60 * and PRIVATEKEYBLOB).
66 #include <openssl/opensslconf.h>
68 #include <openssl/bn.h>
69 #include <openssl/err.h>
70 #include <openssl/pem.h>
72 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
73 #include <openssl/dsa.h>
74 #include <openssl/rsa.h>
78 /* Utility function: read a DWORD (4 byte unsigned integer) in little endian
83 read_ledword(const unsigned char **in)
85 const unsigned char *p = *in;
96 /* Read a BIGNUM in little endian format. The docs say that this should take up
101 read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
103 const unsigned char *p;
104 unsigned char *tmpbuf, *q;
108 tmpbuf = malloc(nbyte);
112 for (i = 0; i < nbyte; i++)
114 *r = BN_bin2bn(tmpbuf, nbyte, NULL);
124 /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
126 #define MS_PUBLICKEYBLOB 0x6
127 #define MS_PRIVATEKEYBLOB 0x7
128 #define MS_RSA1MAGIC 0x31415352L
129 #define MS_RSA2MAGIC 0x32415352L
130 #define MS_DSS1MAGIC 0x31535344L
131 #define MS_DSS2MAGIC 0x32535344L
133 #define MS_KEYALG_RSA_KEYX 0xa400
134 #define MS_KEYALG_DSS_SIGN 0x2200
136 #define MS_KEYTYPE_KEYX 0x1
137 #define MS_KEYTYPE_SIGN 0x2
139 /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
140 #define MS_PVKMAGIC 0xb0b5f11eL
141 /* Salt length for PVK files */
142 #define PVK_SALTLEN 0x10
144 static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
145 unsigned int bitlen, int ispub);
146 static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
147 unsigned int bitlen, int ispub);
150 do_blob_header(const unsigned char **in, unsigned int length,
151 unsigned int *pmagic, unsigned int *pbitlen, int *pisdss, int *pispub)
153 const unsigned char *p = *in;
158 if (*p == MS_PUBLICKEYBLOB) {
160 PEMerror(PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
164 } else if (*p == MS_PRIVATEKEYBLOB) {
166 PEMerror(PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
175 PEMerror(PEM_R_BAD_VERSION_NUMBER);
178 /* Ignore reserved, aiKeyAlg */
180 *pmagic = read_ledword(&p);
181 *pbitlen = read_ledword(&p);
182 if (*pbitlen > 65536) {
183 PEMerror(PEM_R_INCONSISTENT_HEADER);
193 PEMerror(PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
202 PEMerror(PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
208 PEMerror(PEM_R_BAD_MAGIC_NUMBER);
216 blob_length(unsigned bitlen, int isdss, int ispub)
218 unsigned int nbyte, hnbyte;
220 nbyte = (bitlen + 7) >> 3;
221 hnbyte = (bitlen + 15) >> 4;
224 /* Expected length: 20 for q + 3 components bitlen each + 24
225 * for seed structure.
228 return 44 + 3 * nbyte;
229 /* Expected length: 20 for q, priv, 2 bitlen components + 24
230 * for seed structure.
233 return 64 + 2 * nbyte;
235 /* Expected length: 4 for 'e' + 'n' */
239 /* Expected length: 4 for 'e' and 7 other components.
240 * 2 components are bitlen size, 5 are bitlen/2
242 return 4 + 2*nbyte + 5*hnbyte;
248 do_b2i(const unsigned char **in, unsigned int length, int ispub)
250 const unsigned char *p = *in;
251 unsigned int bitlen, magic;
254 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
255 PEMerror(PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
259 if (length < blob_length(bitlen, isdss, ispub)) {
260 PEMerror(PEM_R_KEYBLOB_TOO_SHORT);
264 return b2i_dss(&p, length, bitlen, ispub);
266 return b2i_rsa(&p, length, bitlen, ispub);
270 do_b2i_bio(BIO *in, int ispub)
272 const unsigned char *p;
273 unsigned char hdr_buf[16], *buf = NULL;
274 unsigned int bitlen, magic, length;
276 EVP_PKEY *ret = NULL;
278 if (BIO_read(in, hdr_buf, 16) != 16) {
279 PEMerror(PEM_R_KEYBLOB_TOO_SHORT);
283 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
286 length = blob_length(bitlen, isdss, ispub);
287 buf = malloc(length);
289 PEMerror(ERR_R_MALLOC_FAILURE);
293 if (BIO_read(in, buf, length) != (int)length) {
294 PEMerror(PEM_R_KEYBLOB_TOO_SHORT);
299 ret = b2i_dss(&p, length, bitlen, ispub);
301 ret = b2i_rsa(&p, length, bitlen, ispub);
309 b2i_dss(const unsigned char **in, unsigned int length, unsigned int bitlen,
312 const unsigned char *p = *in;
313 EVP_PKEY *ret = NULL;
318 nbyte = (bitlen + 7) >> 3;
321 ret = EVP_PKEY_new();
324 if (!read_lebn(&p, nbyte, &dsa->p))
326 if (!read_lebn(&p, 20, &dsa->q))
328 if (!read_lebn(&p, nbyte, &dsa->g))
331 if (!read_lebn(&p, nbyte, &dsa->pub_key))
334 if (!read_lebn(&p, 20, &dsa->priv_key))
336 /* Calculate public key */
337 if (!(dsa->pub_key = BN_new()))
339 if (!(ctx = BN_CTX_new()))
341 if (!BN_mod_exp_ct(dsa->pub_key, dsa->g,
342 dsa->priv_key, dsa->p, ctx))
347 EVP_PKEY_set1_DSA(ret, dsa);
353 PEMerror(ERR_R_MALLOC_FAILURE);
361 b2i_rsa(const unsigned char **in, unsigned int length, unsigned int bitlen,
364 const unsigned char *p = *in;
365 EVP_PKEY *ret = NULL;
367 unsigned int nbyte, hnbyte;
369 nbyte = (bitlen + 7) >> 3;
370 hnbyte = (bitlen + 15) >> 4;
372 ret = EVP_PKEY_new();
378 if (!BN_set_word(rsa->e, read_ledword(&p)))
380 if (!read_lebn(&p, nbyte, &rsa->n))
383 if (!read_lebn(&p, hnbyte, &rsa->p))
385 if (!read_lebn(&p, hnbyte, &rsa->q))
387 if (!read_lebn(&p, hnbyte, &rsa->dmp1))
389 if (!read_lebn(&p, hnbyte, &rsa->dmq1))
391 if (!read_lebn(&p, hnbyte, &rsa->iqmp))
393 if (!read_lebn(&p, nbyte, &rsa->d))
397 EVP_PKEY_set1_RSA(ret, rsa);
403 PEMerror(ERR_R_MALLOC_FAILURE);
410 b2i_PrivateKey(const unsigned char **in, long length)
412 return do_b2i(in, length, 0);
416 b2i_PublicKey(const unsigned char **in, long length)
418 return do_b2i(in, length, 1);
422 b2i_PrivateKey_bio(BIO *in)
424 return do_b2i_bio(in, 0);
428 b2i_PublicKey_bio(BIO *in)
430 return do_b2i_bio(in, 1);
434 write_ledword(unsigned char **out, unsigned int dw)
436 unsigned char *p = *out;
439 *p++ = (dw >> 8) & 0xff;
440 *p++ = (dw >> 16) & 0xff;
441 *p++ = (dw >> 24) & 0xff;
446 write_lebn(unsigned char **out, const BIGNUM *bn, int len)
449 unsigned char *p = *out, *q, c;
451 nb = BN_num_bytes(bn);
454 /* In place byte order reversal */
455 for (i = 0; i < nb / 2; i++) {
461 /* Pad with zeroes if we have to */
465 memset(*out, 0, len);
472 static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
473 static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
475 static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
476 static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
479 do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
482 unsigned int bitlen, magic = 0, keyalg;
483 int outlen, noinc = 0;
485 if (pk->type == EVP_PKEY_DSA) {
486 bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);
487 keyalg = MS_KEYALG_DSS_SIGN;
488 } else if (pk->type == EVP_PKEY_RSA) {
489 bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);
490 keyalg = MS_KEYALG_RSA_KEYX;
495 outlen = 16 + blob_length(bitlen,
496 keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
509 *p++ = MS_PUBLICKEYBLOB;
511 *p++ = MS_PRIVATEKEYBLOB;
515 write_ledword(&p, keyalg);
516 write_ledword(&p, magic);
517 write_ledword(&p, bitlen);
518 if (keyalg == MS_KEYALG_DSS_SIGN)
519 write_dsa(&p, pk->pkey.dsa, ispub);
521 write_rsa(&p, pk->pkey.rsa, ispub);
528 do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
530 unsigned char *tmp = NULL;
533 outlen = do_i2b(&tmp, pk, ispub);
536 wrlen = BIO_write(out, tmp, outlen);
544 check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
548 bitlen = BN_num_bits(dsa->p);
549 if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160) ||
550 (BN_num_bits(dsa->g) > bitlen))
553 if (BN_num_bits(dsa->pub_key) > bitlen)
555 *pmagic = MS_DSS1MAGIC;
557 if (BN_num_bits(dsa->priv_key) > 160)
559 *pmagic = MS_DSS2MAGIC;
565 PEMerror(PEM_R_UNSUPPORTED_KEY_COMPONENTS);
570 check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
572 int nbyte, hnbyte, bitlen;
574 if (BN_num_bits(rsa->e) > 32)
576 bitlen = BN_num_bits(rsa->n);
577 nbyte = BN_num_bytes(rsa->n);
578 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
580 *pmagic = MS_RSA1MAGIC;
583 *pmagic = MS_RSA2MAGIC;
584 /* For private key each component must fit within nbyte or
587 if (BN_num_bytes(rsa->d) > nbyte)
589 if ((BN_num_bytes(rsa->iqmp) > hnbyte) ||
590 (BN_num_bytes(rsa->p) > hnbyte) ||
591 (BN_num_bytes(rsa->q) > hnbyte) ||
592 (BN_num_bytes(rsa->dmp1) > hnbyte) ||
593 (BN_num_bytes(rsa->dmq1) > hnbyte))
599 PEMerror(PEM_R_UNSUPPORTED_KEY_COMPONENTS);
604 write_rsa(unsigned char **out, RSA *rsa, int ispub)
608 nbyte = BN_num_bytes(rsa->n);
609 hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
610 write_lebn(out, rsa->e, 4);
611 write_lebn(out, rsa->n, -1);
614 write_lebn(out, rsa->p, hnbyte);
615 write_lebn(out, rsa->q, hnbyte);
616 write_lebn(out, rsa->dmp1, hnbyte);
617 write_lebn(out, rsa->dmq1, hnbyte);
618 write_lebn(out, rsa->iqmp, hnbyte);
619 write_lebn(out, rsa->d, nbyte);
623 write_dsa(unsigned char **out, DSA *dsa, int ispub)
627 nbyte = BN_num_bytes(dsa->p);
628 write_lebn(out, dsa->p, nbyte);
629 write_lebn(out, dsa->q, 20);
630 write_lebn(out, dsa->g, nbyte);
632 write_lebn(out, dsa->pub_key, nbyte);
634 write_lebn(out, dsa->priv_key, 20);
635 /* Set "invalid" for seed structure values */
636 memset(*out, 0xff, 24);
642 i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
644 return do_i2b_bio(out, pk, 0);
648 i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
650 return do_i2b_bio(out, pk, 1);
653 #ifndef OPENSSL_NO_RC4
656 do_PVK_header(const unsigned char **in, unsigned int length, int skip_magic,
657 unsigned int *psaltlen, unsigned int *pkeylen)
659 const unsigned char *p = *in;
660 unsigned int pvk_magic, is_encrypted;
664 PEMerror(PEM_R_PVK_TOO_SHORT);
670 PEMerror(PEM_R_PVK_TOO_SHORT);
674 pvk_magic = read_ledword(&p);
675 if (pvk_magic != MS_PVKMAGIC) {
676 PEMerror(PEM_R_BAD_MAGIC_NUMBER);
682 /*keytype = */read_ledword(&p);
683 is_encrypted = read_ledword(&p);
684 *psaltlen = read_ledword(&p);
685 *pkeylen = read_ledword(&p);
686 if (*psaltlen > 65536 || *pkeylen > 65536) {
687 PEMerror(PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
691 if (is_encrypted && !*psaltlen) {
692 PEMerror(PEM_R_INCONSISTENT_HEADER);
701 derive_pvk_key(unsigned char *key, const unsigned char *salt,
702 unsigned int saltlen, const unsigned char *pass, int passlen)
707 EVP_MD_CTX_init(&mctx);
708 if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL) ||
709 !EVP_DigestUpdate(&mctx, salt, saltlen) ||
710 !EVP_DigestUpdate(&mctx, pass, passlen) ||
711 !EVP_DigestFinal_ex(&mctx, key, NULL))
714 EVP_MD_CTX_cleanup(&mctx);
719 do_PVK_body(const unsigned char **in, unsigned int saltlen,
720 unsigned int keylen, pem_password_cb *cb, void *u)
722 EVP_PKEY *ret = NULL;
723 const unsigned char *p = *in;
725 unsigned char *enctmp = NULL, *q;
728 EVP_CIPHER_CTX_init(&cctx);
730 char psbuf[PEM_BUFSIZE];
731 unsigned char keybuf[20];
732 int enctmplen, inlen;
735 inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
737 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
739 PEMerror(PEM_R_BAD_PASSWORD_READ);
742 enctmp = malloc(keylen + 8);
744 PEMerror(ERR_R_MALLOC_FAILURE);
747 if (!derive_pvk_key(keybuf, p, saltlen, (unsigned char *)psbuf,
752 /* Copy BLOBHEADER across, decrypt rest */
753 memcpy(enctmp, p, 8);
756 PEMerror(PEM_R_PVK_TOO_SHORT);
761 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
763 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
765 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
767 magic = read_ledword((const unsigned char **)&q);
768 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
770 memset(keybuf + 5, 0, 11);
771 if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf,
774 explicit_bzero(keybuf, 20);
775 if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
777 if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen,
780 magic = read_ledword((const unsigned char **)&q);
781 if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
782 PEMerror(PEM_R_BAD_DECRYPT);
786 explicit_bzero(keybuf, 20);
790 ret = b2i_PrivateKey(&p, keylen);
793 EVP_CIPHER_CTX_cleanup(&cctx);
794 if (enctmp && saltlen)
801 b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
803 unsigned char pvk_hdr[24], *buf = NULL;
804 const unsigned char *p;
806 EVP_PKEY *ret = NULL;
807 unsigned int saltlen, keylen;
809 if (BIO_read(in, pvk_hdr, 24) != 24) {
810 PEMerror(PEM_R_PVK_DATA_TOO_SHORT);
815 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
817 buflen = keylen + saltlen;
818 buf = malloc(buflen);
820 PEMerror(ERR_R_MALLOC_FAILURE);
824 if (BIO_read(in, buf, buflen) != buflen) {
825 PEMerror(PEM_R_PVK_DATA_TOO_SHORT);
828 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
831 freezero(buf, buflen);
836 i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel, pem_password_cb *cb,
839 int outlen = 24, pklen;
840 unsigned char *p, *salt = NULL;
843 EVP_CIPHER_CTX_init(&cctx);
845 outlen += PVK_SALTLEN;
846 pklen = do_i2b(NULL, pk, 0);
852 PEMerror(ERR_R_MALLOC_FAILURE);
856 write_ledword(&p, MS_PVKMAGIC);
857 write_ledword(&p, 0);
858 if (pk->type == EVP_PKEY_DSA)
859 write_ledword(&p, MS_KEYTYPE_SIGN);
861 write_ledword(&p, MS_KEYTYPE_KEYX);
862 write_ledword(&p, enclevel ? 1 : 0);
863 write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
864 write_ledword(&p, pklen);
866 arc4random_buf(p, PVK_SALTLEN);
875 char psbuf[PEM_BUFSIZE];
876 unsigned char keybuf[20];
877 int enctmplen, inlen;
879 inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
881 inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
883 PEMerror(PEM_R_BAD_PASSWORD_READ);
886 if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
887 (unsigned char *)psbuf, inlen))
890 memset(keybuf + 5, 0, 11);
891 p = salt + PVK_SALTLEN + 8;
892 if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
894 explicit_bzero(keybuf, 20);
895 if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))
897 if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))
900 EVP_CIPHER_CTX_cleanup(&cctx);
905 EVP_CIPHER_CTX_cleanup(&cctx);
911 i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb, void *u)
913 unsigned char *tmp = NULL;
916 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
919 wrlen = BIO_write(out, tmp, outlen);
921 if (wrlen == outlen) {
922 PEMerror(PEM_R_BIO_WRITE_FAILURE);