pvkfmt.c 25 KB

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