pvkfmt.c 21 KB

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