pvkfmt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2005.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  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
  18. * distribution.
  19. *
  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/)"
  24. *
  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.
  29. *
  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.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  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. * ====================================================================
  52. *
  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).
  56. *
  57. */
  58. /*
  59. * Support for PVK format keys and related structures (such a PUBLICKEYBLOB
  60. * and PRIVATEKEYBLOB).
  61. */
  62. #include "internal/cryptlib.h"
  63. #include <openssl/pem.h>
  64. #include <openssl/rand.h>
  65. #include <openssl/bn.h>
  66. #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
  67. # include <openssl/dsa.h>
  68. # include <openssl/rsa.h>
  69. /*
  70. * Utility function: read a DWORD (4 byte unsigned integer) in little endian
  71. * format
  72. */
  73. static unsigned int read_ledword(const unsigned char **in)
  74. {
  75. const unsigned char *p = *in;
  76. unsigned int ret;
  77. ret = *p++;
  78. ret |= (*p++ << 8);
  79. ret |= (*p++ << 16);
  80. ret |= (*p++ << 24);
  81. *in = p;
  82. return ret;
  83. }
  84. /*
  85. * Read a BIGNUM in little endian format. The docs say that this should take
  86. * up bitlen/8 bytes.
  87. */
  88. static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
  89. {
  90. const unsigned char *p;
  91. unsigned char *tmpbuf, *q;
  92. unsigned int i;
  93. p = *in + nbyte - 1;
  94. tmpbuf = OPENSSL_malloc(nbyte);
  95. if (!tmpbuf)
  96. return 0;
  97. q = tmpbuf;
  98. for (i = 0; i < nbyte; i++)
  99. *q++ = *p--;
  100. *r = BN_bin2bn(tmpbuf, nbyte, NULL);
  101. OPENSSL_free(tmpbuf);
  102. if (*r) {
  103. *in += nbyte;
  104. return 1;
  105. } else
  106. return 0;
  107. }
  108. /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
  109. # define MS_PUBLICKEYBLOB 0x6
  110. # define MS_PRIVATEKEYBLOB 0x7
  111. # define MS_RSA1MAGIC 0x31415352L
  112. # define MS_RSA2MAGIC 0x32415352L
  113. # define MS_DSS1MAGIC 0x31535344L
  114. # define MS_DSS2MAGIC 0x32535344L
  115. # define MS_KEYALG_RSA_KEYX 0xa400
  116. # define MS_KEYALG_DSS_SIGN 0x2200
  117. # define MS_KEYTYPE_KEYX 0x1
  118. # define MS_KEYTYPE_SIGN 0x2
  119. /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
  120. # define MS_PVKMAGIC 0xb0b5f11eL
  121. /* Salt length for PVK files */
  122. # define PVK_SALTLEN 0x10
  123. static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
  124. unsigned int bitlen, int ispub);
  125. static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
  126. unsigned int bitlen, int ispub);
  127. static int do_blob_header(const unsigned char **in, unsigned int length,
  128. unsigned int *pmagic, unsigned int *pbitlen,
  129. int *pisdss, int *pispub)
  130. {
  131. const unsigned char *p = *in;
  132. if (length < 16)
  133. return 0;
  134. /* bType */
  135. if (*p == MS_PUBLICKEYBLOB) {
  136. if (*pispub == 0) {
  137. PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
  138. return 0;
  139. }
  140. *pispub = 1;
  141. } else if (*p == MS_PRIVATEKEYBLOB) {
  142. if (*pispub == 1) {
  143. PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
  144. return 0;
  145. }
  146. *pispub = 0;
  147. } else
  148. return 0;
  149. p++;
  150. /* Version */
  151. if (*p++ != 0x2) {
  152. PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER);
  153. return 0;
  154. }
  155. /* Ignore reserved, aiKeyAlg */
  156. p += 6;
  157. *pmagic = read_ledword(&p);
  158. *pbitlen = read_ledword(&p);
  159. *pisdss = 0;
  160. switch (*pmagic) {
  161. case MS_DSS1MAGIC:
  162. *pisdss = 1;
  163. case MS_RSA1MAGIC:
  164. if (*pispub == 0) {
  165. PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
  166. return 0;
  167. }
  168. break;
  169. case MS_DSS2MAGIC:
  170. *pisdss = 1;
  171. case MS_RSA2MAGIC:
  172. if (*pispub == 1) {
  173. PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
  174. return 0;
  175. }
  176. break;
  177. default:
  178. PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
  179. return -1;
  180. }
  181. *in = p;
  182. return 1;
  183. }
  184. static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
  185. {
  186. unsigned int nbyte, hnbyte;
  187. nbyte = (bitlen + 7) >> 3;
  188. hnbyte = (bitlen + 15) >> 4;
  189. if (isdss) {
  190. /*
  191. * Expected length: 20 for q + 3 components bitlen each + 24 for seed
  192. * structure.
  193. */
  194. if (ispub)
  195. return 44 + 3 * nbyte;
  196. /*
  197. * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
  198. * structure.
  199. */
  200. else
  201. return 64 + 2 * nbyte;
  202. } else {
  203. /* Expected length: 4 for 'e' + 'n' */
  204. if (ispub)
  205. return 4 + nbyte;
  206. else
  207. /*
  208. * Expected length: 4 for 'e' and 7 other components. 2
  209. * components are bitlen size, 5 are bitlen/2
  210. */
  211. return 4 + 2 * nbyte + 5 * hnbyte;
  212. }
  213. }
  214. static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length,
  215. int ispub)
  216. {
  217. const unsigned char *p = *in;
  218. unsigned int bitlen, magic;
  219. int isdss;
  220. if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
  221. PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
  222. return NULL;
  223. }
  224. length -= 16;
  225. if (length < blob_length(bitlen, isdss, ispub)) {
  226. PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
  227. return NULL;
  228. }
  229. if (isdss)
  230. return b2i_dss(&p, length, bitlen, ispub);
  231. else
  232. return b2i_rsa(&p, length, bitlen, ispub);
  233. }
  234. static EVP_PKEY *do_b2i_bio(BIO *in, int ispub)
  235. {
  236. const unsigned char *p;
  237. unsigned char hdr_buf[16], *buf = NULL;
  238. unsigned int bitlen, magic, length;
  239. int isdss;
  240. EVP_PKEY *ret = NULL;
  241. if (BIO_read(in, hdr_buf, 16) != 16) {
  242. PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
  243. return NULL;
  244. }
  245. p = hdr_buf;
  246. if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
  247. return NULL;
  248. length = blob_length(bitlen, isdss, ispub);
  249. buf = OPENSSL_malloc(length);
  250. if (!buf) {
  251. PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
  252. goto err;
  253. }
  254. p = buf;
  255. if (BIO_read(in, buf, length) != (int)length) {
  256. PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
  257. goto err;
  258. }
  259. if (isdss)
  260. ret = b2i_dss(&p, length, bitlen, ispub);
  261. else
  262. ret = b2i_rsa(&p, length, bitlen, ispub);
  263. err:
  264. OPENSSL_free(buf);
  265. return ret;
  266. }
  267. static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
  268. unsigned int bitlen, int ispub)
  269. {
  270. const unsigned char *p = *in;
  271. EVP_PKEY *ret = NULL;
  272. DSA *dsa = NULL;
  273. BN_CTX *ctx = NULL;
  274. unsigned int nbyte;
  275. nbyte = (bitlen + 7) >> 3;
  276. dsa = DSA_new();
  277. ret = EVP_PKEY_new();
  278. if (!dsa || !ret)
  279. goto memerr;
  280. if (!read_lebn(&p, nbyte, &dsa->p))
  281. goto memerr;
  282. if (!read_lebn(&p, 20, &dsa->q))
  283. goto memerr;
  284. if (!read_lebn(&p, nbyte, &dsa->g))
  285. goto memerr;
  286. if (ispub) {
  287. if (!read_lebn(&p, nbyte, &dsa->pub_key))
  288. goto memerr;
  289. } else {
  290. if (!read_lebn(&p, 20, &dsa->priv_key))
  291. goto memerr;
  292. /* Calculate public key */
  293. if ((dsa->pub_key = BN_new()) == NULL)
  294. goto memerr;
  295. if ((ctx = BN_CTX_new()) == NULL)
  296. goto memerr;
  297. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))
  298. goto memerr;
  299. BN_CTX_free(ctx);
  300. }
  301. EVP_PKEY_set1_DSA(ret, dsa);
  302. DSA_free(dsa);
  303. *in = p;
  304. return ret;
  305. memerr:
  306. PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
  307. DSA_free(dsa);
  308. EVP_PKEY_free(ret);
  309. BN_CTX_free(ctx);
  310. return NULL;
  311. }
  312. static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
  313. unsigned int bitlen, int ispub)
  314. {
  315. const unsigned char *p = *in;
  316. EVP_PKEY *ret = NULL;
  317. RSA *rsa = NULL;
  318. unsigned int nbyte, hnbyte;
  319. nbyte = (bitlen + 7) >> 3;
  320. hnbyte = (bitlen + 15) >> 4;
  321. rsa = RSA_new();
  322. ret = EVP_PKEY_new();
  323. if (!rsa || !ret)
  324. goto memerr;
  325. rsa->e = BN_new();
  326. if (!rsa->e)
  327. goto memerr;
  328. if (!BN_set_word(rsa->e, read_ledword(&p)))
  329. goto memerr;
  330. if (!read_lebn(&p, nbyte, &rsa->n))
  331. goto memerr;
  332. if (!ispub) {
  333. if (!read_lebn(&p, hnbyte, &rsa->p))
  334. goto memerr;
  335. if (!read_lebn(&p, hnbyte, &rsa->q))
  336. goto memerr;
  337. if (!read_lebn(&p, hnbyte, &rsa->dmp1))
  338. goto memerr;
  339. if (!read_lebn(&p, hnbyte, &rsa->dmq1))
  340. goto memerr;
  341. if (!read_lebn(&p, hnbyte, &rsa->iqmp))
  342. goto memerr;
  343. if (!read_lebn(&p, nbyte, &rsa->d))
  344. goto memerr;
  345. }
  346. EVP_PKEY_set1_RSA(ret, rsa);
  347. RSA_free(rsa);
  348. *in = p;
  349. return ret;
  350. memerr:
  351. PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
  352. RSA_free(rsa);
  353. EVP_PKEY_free(ret);
  354. return NULL;
  355. }
  356. EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
  357. {
  358. return do_b2i(in, length, 0);
  359. }
  360. EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
  361. {
  362. return do_b2i(in, length, 1);
  363. }
  364. EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
  365. {
  366. return do_b2i_bio(in, 0);
  367. }
  368. EVP_PKEY *b2i_PublicKey_bio(BIO *in)
  369. {
  370. return do_b2i_bio(in, 1);
  371. }
  372. static void write_ledword(unsigned char **out, unsigned int dw)
  373. {
  374. unsigned char *p = *out;
  375. *p++ = dw & 0xff;
  376. *p++ = (dw >> 8) & 0xff;
  377. *p++ = (dw >> 16) & 0xff;
  378. *p++ = (dw >> 24) & 0xff;
  379. *out = p;
  380. }
  381. static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
  382. {
  383. int nb, i;
  384. unsigned char *p = *out, *q, c;
  385. nb = BN_num_bytes(bn);
  386. BN_bn2bin(bn, p);
  387. q = p + nb - 1;
  388. /* In place byte order reversal */
  389. for (i = 0; i < nb / 2; i++) {
  390. c = *p;
  391. *p++ = *q;
  392. *q-- = c;
  393. }
  394. *out += nb;
  395. /* Pad with zeroes if we have to */
  396. if (len > 0) {
  397. len -= nb;
  398. if (len > 0) {
  399. memset(*out, 0, len);
  400. *out += len;
  401. }
  402. }
  403. }
  404. static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
  405. static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
  406. static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
  407. static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
  408. static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
  409. {
  410. unsigned char *p;
  411. unsigned int bitlen, magic = 0, keyalg;
  412. int outlen, noinc = 0;
  413. if (pk->type == EVP_PKEY_DSA) {
  414. bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);
  415. keyalg = MS_KEYALG_DSS_SIGN;
  416. } else if (pk->type == EVP_PKEY_RSA) {
  417. bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);
  418. keyalg = MS_KEYALG_RSA_KEYX;
  419. } else
  420. return -1;
  421. if (bitlen == 0)
  422. return -1;
  423. outlen = 16 + blob_length(bitlen,
  424. keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
  425. if (out == NULL)
  426. return outlen;
  427. if (*out)
  428. p = *out;
  429. else {
  430. p = OPENSSL_malloc(outlen);
  431. if (!p)
  432. return -1;
  433. *out = p;
  434. noinc = 1;
  435. }
  436. if (ispub)
  437. *p++ = MS_PUBLICKEYBLOB;
  438. else
  439. *p++ = MS_PRIVATEKEYBLOB;
  440. *p++ = 0x2;
  441. *p++ = 0;
  442. *p++ = 0;
  443. write_ledword(&p, keyalg);
  444. write_ledword(&p, magic);
  445. write_ledword(&p, bitlen);
  446. if (keyalg == MS_KEYALG_DSS_SIGN)
  447. write_dsa(&p, pk->pkey.dsa, ispub);
  448. else
  449. write_rsa(&p, pk->pkey.rsa, ispub);
  450. if (!noinc)
  451. *out += outlen;
  452. return outlen;
  453. }
  454. static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
  455. {
  456. unsigned char *tmp = NULL;
  457. int outlen, wrlen;
  458. outlen = do_i2b(&tmp, pk, ispub);
  459. if (outlen < 0)
  460. return -1;
  461. wrlen = BIO_write(out, tmp, outlen);
  462. OPENSSL_free(tmp);
  463. if (wrlen == outlen)
  464. return outlen;
  465. return -1;
  466. }
  467. static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
  468. {
  469. int bitlen;
  470. bitlen = BN_num_bits(dsa->p);
  471. if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160)
  472. || (BN_num_bits(dsa->g) > bitlen))
  473. goto badkey;
  474. if (ispub) {
  475. if (BN_num_bits(dsa->pub_key) > bitlen)
  476. goto badkey;
  477. *pmagic = MS_DSS1MAGIC;
  478. } else {
  479. if (BN_num_bits(dsa->priv_key) > 160)
  480. goto badkey;
  481. *pmagic = MS_DSS2MAGIC;
  482. }
  483. return bitlen;
  484. badkey:
  485. PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  486. return 0;
  487. }
  488. static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
  489. {
  490. int nbyte, hnbyte, bitlen;
  491. if (BN_num_bits(rsa->e) > 32)
  492. goto badkey;
  493. bitlen = BN_num_bits(rsa->n);
  494. nbyte = BN_num_bytes(rsa->n);
  495. hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
  496. if (ispub) {
  497. *pmagic = MS_RSA1MAGIC;
  498. return bitlen;
  499. } else {
  500. *pmagic = MS_RSA2MAGIC;
  501. /*
  502. * For private key each component must fit within nbyte or hnbyte.
  503. */
  504. if (BN_num_bytes(rsa->d) > nbyte)
  505. goto badkey;
  506. if ((BN_num_bytes(rsa->iqmp) > hnbyte)
  507. || (BN_num_bytes(rsa->p) > hnbyte)
  508. || (BN_num_bytes(rsa->q) > hnbyte)
  509. || (BN_num_bytes(rsa->dmp1) > hnbyte)
  510. || (BN_num_bytes(rsa->dmq1) > hnbyte))
  511. goto badkey;
  512. }
  513. return bitlen;
  514. badkey:
  515. PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  516. return 0;
  517. }
  518. static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
  519. {
  520. int nbyte, hnbyte;
  521. nbyte = BN_num_bytes(rsa->n);
  522. hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
  523. write_lebn(out, rsa->e, 4);
  524. write_lebn(out, rsa->n, -1);
  525. if (ispub)
  526. return;
  527. write_lebn(out, rsa->p, hnbyte);
  528. write_lebn(out, rsa->q, hnbyte);
  529. write_lebn(out, rsa->dmp1, hnbyte);
  530. write_lebn(out, rsa->dmq1, hnbyte);
  531. write_lebn(out, rsa->iqmp, hnbyte);
  532. write_lebn(out, rsa->d, nbyte);
  533. }
  534. static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
  535. {
  536. int nbyte;
  537. nbyte = BN_num_bytes(dsa->p);
  538. write_lebn(out, dsa->p, nbyte);
  539. write_lebn(out, dsa->q, 20);
  540. write_lebn(out, dsa->g, nbyte);
  541. if (ispub)
  542. write_lebn(out, dsa->pub_key, nbyte);
  543. else
  544. write_lebn(out, dsa->priv_key, 20);
  545. /* Set "invalid" for seed structure values */
  546. memset(*out, 0xff, 24);
  547. *out += 24;
  548. return;
  549. }
  550. int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
  551. {
  552. return do_i2b_bio(out, pk, 0);
  553. }
  554. int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
  555. {
  556. return do_i2b_bio(out, pk, 1);
  557. }
  558. # ifndef OPENSSL_NO_RC4
  559. static int do_PVK_header(const unsigned char **in, unsigned int length,
  560. int skip_magic,
  561. unsigned int *psaltlen, unsigned int *pkeylen)
  562. {
  563. const unsigned char *p = *in;
  564. unsigned int pvk_magic, is_encrypted;
  565. if (skip_magic) {
  566. if (length < 20) {
  567. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
  568. return 0;
  569. }
  570. length -= 20;
  571. } else {
  572. if (length < 24) {
  573. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
  574. return 0;
  575. }
  576. length -= 24;
  577. pvk_magic = read_ledword(&p);
  578. if (pvk_magic != MS_PVKMAGIC) {
  579. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
  580. return 0;
  581. }
  582. }
  583. /* Skip reserved */
  584. p += 4;
  585. /*
  586. * keytype =
  587. */ read_ledword(&p);
  588. is_encrypted = read_ledword(&p);
  589. *psaltlen = read_ledword(&p);
  590. *pkeylen = read_ledword(&p);
  591. if (is_encrypted && !*psaltlen) {
  592. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
  593. return 0;
  594. }
  595. *in = p;
  596. return 1;
  597. }
  598. static int derive_pvk_key(unsigned char *key,
  599. const unsigned char *salt, unsigned int saltlen,
  600. const unsigned char *pass, int passlen)
  601. {
  602. EVP_MD_CTX mctx;
  603. int rv = 1;
  604. EVP_MD_CTX_init(&mctx);
  605. if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL)
  606. || !EVP_DigestUpdate(&mctx, salt, saltlen)
  607. || !EVP_DigestUpdate(&mctx, pass, passlen)
  608. || !EVP_DigestFinal_ex(&mctx, key, NULL))
  609. rv = 0;
  610. EVP_MD_CTX_cleanup(&mctx);
  611. return rv;
  612. }
  613. static EVP_PKEY *do_PVK_body(const unsigned char **in,
  614. unsigned int saltlen, unsigned int keylen,
  615. pem_password_cb *cb, void *u)
  616. {
  617. EVP_PKEY *ret = NULL;
  618. const unsigned char *p = *in;
  619. unsigned int magic;
  620. unsigned char *enctmp = NULL, *q;
  621. EVP_CIPHER_CTX cctx;
  622. EVP_CIPHER_CTX_init(&cctx);
  623. if (saltlen) {
  624. char psbuf[PEM_BUFSIZE];
  625. unsigned char keybuf[20];
  626. int enctmplen, inlen;
  627. if (cb)
  628. inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
  629. else
  630. inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  631. if (inlen <= 0) {
  632. PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
  633. return NULL;
  634. }
  635. enctmp = OPENSSL_malloc(keylen + 8);
  636. if (!enctmp) {
  637. PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
  638. return NULL;
  639. }
  640. if (!derive_pvk_key(keybuf, p, saltlen,
  641. (unsigned char *)psbuf, inlen))
  642. return NULL;
  643. p += saltlen;
  644. /* Copy BLOBHEADER across, decrypt rest */
  645. memcpy(enctmp, p, 8);
  646. p += 8;
  647. if (keylen < 8) {
  648. PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
  649. return NULL;
  650. }
  651. inlen = keylen - 8;
  652. q = enctmp + 8;
  653. if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
  654. goto err;
  655. if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
  656. goto err;
  657. if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
  658. goto err;
  659. magic = read_ledword((const unsigned char **)&q);
  660. if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
  661. q = enctmp + 8;
  662. memset(keybuf + 5, 0, 11);
  663. if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
  664. goto err;
  665. OPENSSL_cleanse(keybuf, 20);
  666. if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
  667. goto err;
  668. if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
  669. goto err;
  670. magic = read_ledword((const unsigned char **)&q);
  671. if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
  672. PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
  673. goto err;
  674. }
  675. } else
  676. OPENSSL_cleanse(keybuf, 20);
  677. p = enctmp;
  678. }
  679. ret = b2i_PrivateKey(&p, keylen);
  680. err:
  681. EVP_CIPHER_CTX_cleanup(&cctx);
  682. OPENSSL_free(enctmp);
  683. return ret;
  684. }
  685. EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
  686. {
  687. unsigned char pvk_hdr[24], *buf = NULL;
  688. const unsigned char *p;
  689. int buflen;
  690. EVP_PKEY *ret = NULL;
  691. unsigned int saltlen, keylen;
  692. if (BIO_read(in, pvk_hdr, 24) != 24) {
  693. PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
  694. return NULL;
  695. }
  696. p = pvk_hdr;
  697. if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
  698. return 0;
  699. buflen = (int)keylen + saltlen;
  700. buf = OPENSSL_malloc(buflen);
  701. if (!buf) {
  702. PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
  703. return 0;
  704. }
  705. p = buf;
  706. if (BIO_read(in, buf, buflen) != buflen) {
  707. PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
  708. goto err;
  709. }
  710. ret = do_PVK_body(&p, saltlen, keylen, cb, u);
  711. err:
  712. OPENSSL_clear_free(buf, buflen);
  713. return ret;
  714. }
  715. static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
  716. pem_password_cb *cb, void *u)
  717. {
  718. int outlen = 24, pklen;
  719. unsigned char *p, *salt = NULL;
  720. EVP_CIPHER_CTX cctx;
  721. EVP_CIPHER_CTX_init(&cctx);
  722. if (enclevel)
  723. outlen += PVK_SALTLEN;
  724. pklen = do_i2b(NULL, pk, 0);
  725. if (pklen < 0)
  726. return -1;
  727. outlen += pklen;
  728. if (!out)
  729. return outlen;
  730. if (*out)
  731. p = *out;
  732. else {
  733. p = OPENSSL_malloc(outlen);
  734. if (!p) {
  735. PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
  736. return -1;
  737. }
  738. *out = p;
  739. }
  740. write_ledword(&p, MS_PVKMAGIC);
  741. write_ledword(&p, 0);
  742. if (pk->type == EVP_PKEY_DSA)
  743. write_ledword(&p, MS_KEYTYPE_SIGN);
  744. else
  745. write_ledword(&p, MS_KEYTYPE_KEYX);
  746. write_ledword(&p, enclevel ? 1 : 0);
  747. write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
  748. write_ledword(&p, pklen);
  749. if (enclevel) {
  750. if (RAND_bytes(p, PVK_SALTLEN) <= 0)
  751. goto error;
  752. salt = p;
  753. p += PVK_SALTLEN;
  754. }
  755. do_i2b(&p, pk, 0);
  756. if (enclevel == 0)
  757. return outlen;
  758. else {
  759. char psbuf[PEM_BUFSIZE];
  760. unsigned char keybuf[20];
  761. int enctmplen, inlen;
  762. if (cb)
  763. inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
  764. else
  765. inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
  766. if (inlen <= 0) {
  767. PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
  768. goto error;
  769. }
  770. if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
  771. (unsigned char *)psbuf, inlen))
  772. goto error;
  773. if (enclevel == 1)
  774. memset(keybuf + 5, 0, 11);
  775. p = salt + PVK_SALTLEN + 8;
  776. if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
  777. goto error;
  778. OPENSSL_cleanse(keybuf, 20);
  779. if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))
  780. goto error;
  781. if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))
  782. goto error;
  783. }
  784. EVP_CIPHER_CTX_cleanup(&cctx);
  785. return outlen;
  786. error:
  787. EVP_CIPHER_CTX_cleanup(&cctx);
  788. return -1;
  789. }
  790. int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
  791. pem_password_cb *cb, void *u)
  792. {
  793. unsigned char *tmp = NULL;
  794. int outlen, wrlen;
  795. outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
  796. if (outlen < 0)
  797. return -1;
  798. wrlen = BIO_write(out, tmp, outlen);
  799. OPENSSL_free(tmp);
  800. if (wrlen == outlen) {
  801. PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
  802. return outlen;
  803. }
  804. return -1;
  805. }
  806. # endif
  807. #endif