66 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
74 static unsigned int read_ledword(
const unsigned char **in)
76 const unsigned char *
p = *in;
90 static int read_lebn(
const unsigned char **in,
unsigned int nbyte,
BIGNUM **r)
92 const unsigned char *
p;
93 unsigned char *tmpbuf, *
q;
100 for (i = 0; i < nbyte; i++)
116 #define MS_PUBLICKEYBLOB 0x6
117 #define MS_PRIVATEKEYBLOB 0x7
118 #define MS_RSA1MAGIC 0x31415352L
119 #define MS_RSA2MAGIC 0x32415352L
120 #define MS_DSS1MAGIC 0x31535344L
121 #define MS_DSS2MAGIC 0x32535344L
123 #define MS_KEYALG_RSA_KEYX 0xa400
124 #define MS_KEYALG_DSS_SIGN 0x2200
126 #define MS_KEYTYPE_KEYX 0x1
127 #define MS_KEYTYPE_SIGN 0x2
130 #define MS_PVKMAGIC 0xb0b5f11eL
132 #define PVK_SALTLEN 0x10
134 static EVP_PKEY *b2i_rsa(
const unsigned char **in,
unsigned int length,
135 unsigned int bitlen,
int ispub);
136 static EVP_PKEY *b2i_dss(
const unsigned char **in,
unsigned int length,
137 unsigned int bitlen,
int ispub);
139 static int do_blob_header(
const unsigned char **in,
unsigned int length,
140 unsigned int *pmagic,
unsigned int *pbitlen,
141 int *pisdss,
int *pispub)
143 const unsigned char *p = *in;
178 *pmagic = read_ledword(&p);
179 *pbitlen = read_ledword(&p);
214 static unsigned int blob_length(
unsigned bitlen,
int isdss,
int ispub)
216 unsigned int nbyte, hnbyte;
217 nbyte = (bitlen + 7) >> 3;
218 hnbyte = (bitlen + 15) >> 4;
226 return 44 + 3 * nbyte;
231 return 64 + 2 * nbyte;
242 return 4 + 2*nbyte + 5*hnbyte;
247 static EVP_PKEY *do_b2i(
const unsigned char **in,
unsigned int length,
250 const unsigned char *p = *in;
251 unsigned int bitlen, magic;
253 if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0)
259 if (length < blob_length(bitlen, isdss, ispub))
265 return b2i_dss(&p, length, bitlen, ispub);
267 return b2i_rsa(&p, length, bitlen, ispub);
272 const unsigned char *
p;
273 unsigned char hdr_buf[16], *buf = NULL;
274 unsigned int bitlen, magic,
length;
277 if (
BIO_read(in, hdr_buf, 16) != 16)
283 if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
286 length = blob_length(bitlen, isdss, ispub);
294 if (
BIO_read(in, buf, length) != (
int)length)
301 ret = b2i_dss(&p, length, bitlen, ispub);
303 ret = b2i_rsa(&p, length, bitlen, ispub);
311 static EVP_PKEY *b2i_dss(
const unsigned char **in,
unsigned int length,
312 unsigned int bitlen,
int ispub)
314 const unsigned char *p = *in;
319 nbyte = (bitlen + 7) >> 3;
325 if (!read_lebn(&p, nbyte, &dsa->
p))
327 if (!read_lebn(&p, 20, &dsa->
q))
329 if (!read_lebn(&p, nbyte, &dsa->
g))
333 if (!read_lebn(&p, nbyte, &dsa->
pub_key))
338 if (!read_lebn(&p, 20, &dsa->
priv_key))
369 static EVP_PKEY *b2i_rsa(
const unsigned char **in,
unsigned int length,
370 unsigned int bitlen,
int ispub)
373 const unsigned char *p = *in;
376 unsigned int nbyte, hnbyte;
377 nbyte = (bitlen + 7) >> 3;
378 hnbyte = (bitlen + 15) >> 4;
388 if (!read_lebn(&p, nbyte, &rsa->
n))
392 if (!read_lebn(&p, hnbyte, &rsa->
p))
394 if (!read_lebn(&p, hnbyte, &rsa->
q))
396 if (!read_lebn(&p, hnbyte, &rsa->
dmp1))
398 if (!read_lebn(&p, hnbyte, &rsa->
dmq1))
400 if (!read_lebn(&p, hnbyte, &rsa->
iqmp))
402 if (!read_lebn(&p, nbyte, &rsa->
d))
421 return do_b2i(in, length, 0);
426 return do_b2i(in, length, 1);
432 return do_b2i_bio(in, 0);
437 return do_b2i_bio(in, 1);
440 static void write_ledword(
unsigned char **out,
unsigned int dw)
442 unsigned char *p = *out;
444 *p++ = (dw>>8) & 0xff;
445 *p++ = (dw>>16) & 0xff;
446 *p++ = (dw>>24) & 0xff;
450 static void write_lebn(
unsigned char **out,
const BIGNUM *bn,
int len)
453 unsigned char *p = *out, *
q,
c;
458 for (i = 0; i < nb/2; i++)
471 memset(*out, 0, len);
478 static int check_bitlen_rsa(
RSA *rsa,
int ispub,
unsigned int *magic);
479 static int check_bitlen_dsa(
DSA *dsa,
int ispub,
unsigned int *magic);
481 static void write_rsa(
unsigned char **out,
RSA *rsa,
int ispub);
482 static void write_dsa(
unsigned char **out,
DSA *dsa,
int ispub);
484 static int do_i2b(
unsigned char **out,
EVP_PKEY *pk,
int ispub)
487 unsigned int bitlen, magic = 0, keyalg;
488 int outlen, noinc = 0;
491 bitlen = check_bitlen_dsa(pk->
pkey.
dsa, ispub, &magic);
496 bitlen = check_bitlen_rsa(pk->
pkey.
rsa, ispub, &magic);
503 outlen = 16 + blob_length(bitlen,
524 write_ledword(&p, keyalg);
525 write_ledword(&p, magic);
526 write_ledword(&p, bitlen);
528 write_dsa(&p, pk->
pkey.
dsa, ispub);
530 write_rsa(&p, pk->
pkey.
rsa, ispub);
536 static int do_i2b_bio(
BIO *out,
EVP_PKEY *pk,
int ispub)
538 unsigned char *tmp = NULL;
540 outlen = do_i2b(&tmp, pk, ispub);
550 static int check_bitlen_dsa(
DSA *dsa,
int ispub,
unsigned int *pmagic)
576 static int check_bitlen_rsa(
RSA *rsa,
int ispub,
unsigned int *pmagic)
578 int nbyte, hnbyte, bitlen;
611 static void write_rsa(
unsigned char **out,
RSA *rsa,
int ispub)
616 write_lebn(out, rsa->
e, 4);
617 write_lebn(out, rsa->
n, -1);
620 write_lebn(out, rsa->
p, hnbyte);
621 write_lebn(out, rsa->
q, hnbyte);
622 write_lebn(out, rsa->
dmp1, hnbyte);
623 write_lebn(out, rsa->
dmq1, hnbyte);
624 write_lebn(out, rsa->
iqmp, hnbyte);
625 write_lebn(out, rsa->
d, nbyte);
629 static void write_dsa(
unsigned char **out,
DSA *dsa,
int ispub)
633 write_lebn(out, dsa->
p, nbyte);
634 write_lebn(out, dsa->
q, 20);
635 write_lebn(out, dsa->
g, nbyte);
637 write_lebn(out, dsa->
pub_key, nbyte);
641 memset(*out, 0xff, 24);
649 return do_i2b_bio(out, pk, 0);
654 return do_i2b_bio(out, pk, 1);
657 #ifndef OPENSSL_NO_RC4
659 static int do_PVK_header(
const unsigned char **in,
unsigned int length,
661 unsigned int *psaltlen,
unsigned int *pkeylen)
664 const unsigned char *p = *in;
665 unsigned int pvk_magic, is_encrypted;
683 pvk_magic = read_ledword(&p);
693 is_encrypted = read_ledword(&p);
694 *psaltlen = read_ledword(&p);
695 *pkeylen = read_ledword(&p);
697 if (is_encrypted && !*psaltlen)
707 static int derive_pvk_key(
unsigned char *
key,
708 const unsigned char *salt,
unsigned int saltlen,
725 static EVP_PKEY *do_PVK_body(
const unsigned char **in,
726 unsigned int saltlen,
unsigned int keylen,
730 const unsigned char *p = *in;
732 unsigned char *enctmp = NULL, *
q;
738 unsigned char keybuf[20];
739 int enctmplen, inlen;
755 if (!derive_pvk_key(keybuf, p, saltlen,
756 (
unsigned char *)psbuf, inlen))
760 memcpy(enctmp, p, 8);
770 magic = read_ledword((
const unsigned char **)&q);
774 memset(keybuf + 5, 0, 11);
784 magic = read_ledword((
const unsigned char **)&q);
799 if (enctmp && saltlen)
807 unsigned char pvk_hdr[24], *buf = NULL;
808 const unsigned char *
p;
811 unsigned int saltlen, keylen;
812 if (
BIO_read(in, pvk_hdr, 24) != 24)
819 if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
821 buflen = (int) keylen + saltlen;
829 if (
BIO_read(in, buf, buflen) != buflen)
834 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
847 static int i2b_PVK(
unsigned char **out,
EVP_PKEY*pk,
int enclevel,
850 int outlen = 24, pklen;
851 unsigned char *
p, *salt = NULL;
856 pklen = do_i2b(NULL, pk, 0);
876 write_ledword(&p, 0);
881 write_ledword(&p, enclevel ? 1 : 0);
883 write_ledword(&p, pklen);
897 unsigned char keybuf[20];
898 int enctmplen, inlen;
909 (
unsigned char *)psbuf, inlen))
912 memset(keybuf + 5, 0, 11);
933 unsigned char *tmp = NULL;
935 outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);