pvkfmt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. /* 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. if (buf)
  265. OPENSSL_free(buf);
  266. return ret;
  267. }
  268. static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
  269. unsigned int bitlen, int ispub)
  270. {
  271. const unsigned char *p = *in;
  272. EVP_PKEY *ret = NULL;
  273. DSA *dsa = NULL;
  274. BN_CTX *ctx = NULL;
  275. unsigned int nbyte;
  276. nbyte = (bitlen + 7) >> 3;
  277. dsa = DSA_new();
  278. ret = EVP_PKEY_new();
  279. if (!dsa || !ret)
  280. goto memerr;
  281. if (!read_lebn(&p, nbyte, &dsa->p))
  282. goto memerr;
  283. if (!read_lebn(&p, 20, &dsa->q))
  284. goto memerr;
  285. if (!read_lebn(&p, nbyte, &dsa->g))
  286. goto memerr;
  287. if (ispub) {
  288. if (!read_lebn(&p, nbyte, &dsa->pub_key))
  289. goto memerr;
  290. } else {
  291. if (!read_lebn(&p, 20, &dsa->priv_key))
  292. goto memerr;
  293. /* Calculate public key */
  294. if (!(dsa->pub_key = BN_new()))
  295. goto memerr;
  296. if (!(ctx = BN_CTX_new()))
  297. goto memerr;
  298. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))
  299. goto memerr;
  300. BN_CTX_free(ctx);
  301. }
  302. EVP_PKEY_set1_DSA(ret, dsa);
  303. DSA_free(dsa);
  304. *in = p;
  305. return ret;
  306. memerr:
  307. PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
  308. if (dsa)
  309. DSA_free(dsa);
  310. if (ret)
  311. EVP_PKEY_free(ret);
  312. if (ctx)
  313. BN_CTX_free(ctx);
  314. return NULL;
  315. }
  316. static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
  317. unsigned int bitlen, int ispub)
  318. {
  319. const unsigned char *p = *in;
  320. EVP_PKEY *ret = NULL;
  321. RSA *rsa = NULL;
  322. unsigned int nbyte, hnbyte;
  323. nbyte = (bitlen + 7) >> 3;
  324. hnbyte = (bitlen + 15) >> 4;
  325. rsa = RSA_new();
  326. ret = EVP_PKEY_new();
  327. if (!rsa || !ret)
  328. goto memerr;
  329. rsa->e = BN_new();
  330. if (!rsa->e)
  331. goto memerr;
  332. if (!BN_set_word(rsa->e, read_ledword(&p)))
  333. goto memerr;
  334. if (!read_lebn(&p, nbyte, &rsa->n))
  335. goto memerr;
  336. if (!ispub) {
  337. if (!read_lebn(&p, hnbyte, &rsa->p))
  338. goto memerr;
  339. if (!read_lebn(&p, hnbyte, &rsa->q))
  340. goto memerr;
  341. if (!read_lebn(&p, hnbyte, &rsa->dmp1))
  342. goto memerr;
  343. if (!read_lebn(&p, hnbyte, &rsa->dmq1))
  344. goto memerr;
  345. if (!read_lebn(&p, hnbyte, &rsa->iqmp))
  346. goto memerr;
  347. if (!read_lebn(&p, nbyte, &rsa->d))
  348. goto memerr;
  349. }
  350. EVP_PKEY_set1_RSA(ret, rsa);
  351. RSA_free(rsa);
  352. *in = p;
  353. return ret;
  354. memerr:
  355. PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
  356. if (rsa)
  357. RSA_free(rsa);
  358. if (ret)
  359. EVP_PKEY_free(ret);
  360. return NULL;
  361. }
  362. EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
  363. {
  364. return do_b2i(in, length, 0);
  365. }
  366. EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
  367. {
  368. return do_b2i(in, length, 1);
  369. }
  370. EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
  371. {
  372. return do_b2i_bio(in, 0);
  373. }
  374. EVP_PKEY *b2i_PublicKey_bio(BIO *in)
  375. {
  376. return do_b2i_bio(in, 1);
  377. }
  378. static void write_ledword(unsigned char **out, unsigned int dw)
  379. {
  380. unsigned char *p = *out;
  381. *p++ = dw & 0xff;
  382. *p++ = (dw >> 8) & 0xff;
  383. *p++ = (dw >> 16) & 0xff;
  384. *p++ = (dw >> 24) & 0xff;
  385. *out = p;
  386. }
  387. static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
  388. {
  389. int nb, i;
  390. unsigned char *p = *out, *q, c;
  391. nb = BN_num_bytes(bn);
  392. BN_bn2bin(bn, p);
  393. q = p + nb - 1;
  394. /* In place byte order reversal */
  395. for (i = 0; i < nb / 2; i++) {
  396. c = *p;
  397. *p++ = *q;
  398. *q-- = c;
  399. }
  400. *out += nb;
  401. /* Pad with zeroes if we have to */
  402. if (len > 0) {
  403. len -= nb;
  404. if (len > 0) {
  405. memset(*out, 0, len);
  406. *out += len;
  407. }
  408. }
  409. }
  410. static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
  411. static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
  412. static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
  413. static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
  414. static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
  415. {
  416. unsigned char *p;
  417. unsigned int bitlen, magic = 0, keyalg;
  418. int outlen, noinc = 0;
  419. if (pk->type == EVP_PKEY_DSA) {
  420. bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic);
  421. keyalg = MS_KEYALG_DSS_SIGN;
  422. } else if (pk->type == EVP_PKEY_RSA) {
  423. bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic);
  424. keyalg = MS_KEYALG_RSA_KEYX;
  425. } else
  426. return -1;
  427. if (bitlen == 0)
  428. return -1;
  429. outlen = 16 + blob_length(bitlen,
  430. keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
  431. if (out == NULL)
  432. return outlen;
  433. if (*out)
  434. p = *out;
  435. else {
  436. p = OPENSSL_malloc(outlen);
  437. if (!p)
  438. return -1;
  439. *out = p;
  440. noinc = 1;
  441. }
  442. if (ispub)
  443. *p++ = MS_PUBLICKEYBLOB;
  444. else
  445. *p++ = MS_PRIVATEKEYBLOB;
  446. *p++ = 0x2;
  447. *p++ = 0;
  448. *p++ = 0;
  449. write_ledword(&p, keyalg);
  450. write_ledword(&p, magic);
  451. write_ledword(&p, bitlen);
  452. if (keyalg == MS_KEYALG_DSS_SIGN)
  453. write_dsa(&p, pk->pkey.dsa, ispub);
  454. else
  455. write_rsa(&p, pk->pkey.rsa, ispub);
  456. if (!noinc)
  457. *out += outlen;
  458. return outlen;
  459. }
  460. static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
  461. {
  462. unsigned char *tmp = NULL;
  463. int outlen, wrlen;
  464. outlen = do_i2b(&tmp, pk, ispub);
  465. if (outlen < 0)
  466. return -1;
  467. wrlen = BIO_write(out, tmp, outlen);
  468. OPENSSL_free(tmp);
  469. if (wrlen == outlen)
  470. return outlen;
  471. return -1;
  472. }
  473. static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
  474. {
  475. int bitlen;
  476. bitlen = BN_num_bits(dsa->p);
  477. if ((bitlen & 7) || (BN_num_bits(dsa->q) != 160)
  478. || (BN_num_bits(dsa->g) > bitlen))
  479. goto badkey;
  480. if (ispub) {
  481. if (BN_num_bits(dsa->pub_key) > bitlen)
  482. goto badkey;
  483. *pmagic = MS_DSS1MAGIC;
  484. } else {
  485. if (BN_num_bits(dsa->priv_key) > 160)
  486. goto badkey;
  487. *pmagic = MS_DSS2MAGIC;
  488. }
  489. return bitlen;
  490. badkey:
  491. PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  492. return 0;
  493. }
  494. static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
  495. {
  496. int nbyte, hnbyte, bitlen;
  497. if (BN_num_bits(rsa->e) > 32)
  498. goto badkey;
  499. bitlen = BN_num_bits(rsa->n);
  500. nbyte = BN_num_bytes(rsa->n);
  501. hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
  502. if (ispub) {
  503. *pmagic = MS_RSA1MAGIC;
  504. return bitlen;
  505. } else {
  506. *pmagic = MS_RSA2MAGIC;
  507. /*
  508. * For private key each component must fit within nbyte or hnbyte.
  509. */
  510. if (BN_num_bytes(rsa->d) > nbyte)
  511. goto badkey;
  512. if ((BN_num_bytes(rsa->iqmp) > hnbyte)
  513. || (BN_num_bytes(rsa->p) > hnbyte)
  514. || (BN_num_bytes(rsa->q) > hnbyte)
  515. || (BN_num_bytes(rsa->dmp1) > hnbyte)
  516. || (BN_num_bytes(rsa->dmq1) > hnbyte))
  517. goto badkey;
  518. }
  519. return bitlen;
  520. badkey:
  521. PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  522. return 0;
  523. }
  524. static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
  525. {
  526. int nbyte, hnbyte;
  527. nbyte = BN_num_bytes(rsa->n);
  528. hnbyte = (BN_num_bits(rsa->n) + 15) >> 4;
  529. write_lebn(out, rsa->e, 4);
  530. write_lebn(out, rsa->n, -1);
  531. if (ispub)
  532. return;
  533. write_lebn(out, rsa->p, hnbyte);
  534. write_lebn(out, rsa->q, hnbyte);
  535. write_lebn(out, rsa->dmp1, hnbyte);
  536. write_lebn(out, rsa->dmq1, hnbyte);
  537. write_lebn(out, rsa->iqmp, hnbyte);
  538. write_lebn(out, rsa->d, nbyte);
  539. }
  540. static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
  541. {
  542. int nbyte;
  543. nbyte = BN_num_bytes(dsa->p);
  544. write_lebn(out, dsa->p, nbyte);
  545. write_lebn(out, dsa->q, 20);
  546. write_lebn(out, dsa->g, nbyte);
  547. if (ispub)
  548. write_lebn(out, dsa->pub_key, nbyte);
  549. else
  550. write_lebn(out, dsa->priv_key, 20);
  551. /* Set "invalid" for seed structure values */
  552. memset(*out, 0xff, 24);
  553. *out += 24;
  554. return;
  555. }
  556. int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
  557. {
  558. return do_i2b_bio(out, pk, 0);
  559. }
  560. int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
  561. {
  562. return do_i2b_bio(out, pk, 1);
  563. }
  564. # ifndef OPENSSL_NO_RC4
  565. static int do_PVK_header(const unsigned char **in, unsigned int length,
  566. int skip_magic,
  567. unsigned int *psaltlen, unsigned int *pkeylen)
  568. {
  569. const unsigned char *p = *in;
  570. unsigned int pvk_magic, is_encrypted;
  571. if (skip_magic) {
  572. if (length < 20) {
  573. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
  574. return 0;
  575. }
  576. } else {
  577. if (length < 24) {
  578. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
  579. return 0;
  580. }
  581. pvk_magic = read_ledword(&p);
  582. if (pvk_magic != MS_PVKMAGIC) {
  583. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
  584. return 0;
  585. }
  586. }
  587. /* Skip reserved */
  588. p += 4;
  589. /*
  590. * keytype =
  591. */ read_ledword(&p);
  592. is_encrypted = read_ledword(&p);
  593. *psaltlen = read_ledword(&p);
  594. *pkeylen = read_ledword(&p);
  595. if (is_encrypted && !*psaltlen) {
  596. PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
  597. return 0;
  598. }
  599. *in = p;
  600. return 1;
  601. }
  602. static int derive_pvk_key(unsigned char *key,
  603. const unsigned char *salt, unsigned int saltlen,
  604. const unsigned char *pass, int passlen)
  605. {
  606. EVP_MD_CTX mctx;
  607. int rv = 1;
  608. EVP_MD_CTX_init(&mctx);
  609. if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL)
  610. || !EVP_DigestUpdate(&mctx, salt, saltlen)
  611. || !EVP_DigestUpdate(&mctx, pass, passlen)
  612. || !EVP_DigestFinal_ex(&mctx, key, NULL))
  613. rv = 0;
  614. EVP_MD_CTX_cleanup(&mctx);
  615. return rv;
  616. }
  617. static EVP_PKEY *do_PVK_body(const unsigned char **in,
  618. unsigned int saltlen, unsigned int keylen,
  619. pem_password_cb *cb, void *u)
  620. {
  621. EVP_PKEY *ret = NULL;
  622. const unsigned char *p = *in;
  623. unsigned int magic;
  624. unsigned char *enctmp = NULL, *q;
  625. EVP_CIPHER_CTX cctx;
  626. EVP_CIPHER_CTX_init(&cctx);
  627. if (saltlen) {
  628. char psbuf[PEM_BUFSIZE];
  629. unsigned char keybuf[20];
  630. int enctmplen, inlen;
  631. if (cb)
  632. inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
  633. else
  634. inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  635. if (inlen <= 0) {
  636. PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
  637. goto err;
  638. }
  639. enctmp = OPENSSL_malloc(keylen + 8);
  640. if (!enctmp) {
  641. PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
  642. goto err;
  643. }
  644. if (!derive_pvk_key(keybuf, p, saltlen,
  645. (unsigned char *)psbuf, inlen))
  646. goto err;
  647. p += saltlen;
  648. /* Copy BLOBHEADER across, decrypt rest */
  649. memcpy(enctmp, p, 8);
  650. p += 8;
  651. if (keylen < 8) {
  652. PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
  653. goto err;
  654. }
  655. inlen = keylen - 8;
  656. q = enctmp + 8;
  657. if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
  658. goto err;
  659. if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
  660. goto err;
  661. if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
  662. goto err;
  663. magic = read_ledword((const unsigned char **)&q);
  664. if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
  665. q = enctmp + 8;
  666. memset(keybuf + 5, 0, 11);
  667. if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
  668. goto err;
  669. OPENSSL_cleanse(keybuf, 20);
  670. if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
  671. goto err;
  672. if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
  673. goto err;
  674. magic = read_ledword((const unsigned char **)&q);
  675. if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
  676. PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
  677. goto err;
  678. }
  679. } else
  680. OPENSSL_cleanse(keybuf, 20);
  681. p = enctmp;
  682. }
  683. ret = b2i_PrivateKey(&p, keylen);
  684. err:
  685. EVP_CIPHER_CTX_cleanup(&cctx);
  686. if (enctmp && saltlen)
  687. OPENSSL_free(enctmp);
  688. return ret;
  689. }
  690. EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
  691. {
  692. unsigned char pvk_hdr[24], *buf = NULL;
  693. const unsigned char *p;
  694. int buflen;
  695. EVP_PKEY *ret = NULL;
  696. unsigned int saltlen, keylen;
  697. if (BIO_read(in, pvk_hdr, 24) != 24) {
  698. PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
  699. return NULL;
  700. }
  701. p = pvk_hdr;
  702. if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
  703. return 0;
  704. buflen = (int)keylen + saltlen;
  705. buf = OPENSSL_malloc(buflen);
  706. if (!buf) {
  707. PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
  708. return 0;
  709. }
  710. p = buf;
  711. if (BIO_read(in, buf, buflen) != buflen) {
  712. PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
  713. goto err;
  714. }
  715. ret = do_PVK_body(&p, saltlen, keylen, cb, u);
  716. err:
  717. if (buf) {
  718. OPENSSL_cleanse(buf, buflen);
  719. OPENSSL_free(buf);
  720. }
  721. return ret;
  722. }
  723. static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
  724. pem_password_cb *cb, void *u)
  725. {
  726. int outlen = 24, pklen;
  727. unsigned char *p, *salt = NULL;
  728. EVP_CIPHER_CTX cctx;
  729. EVP_CIPHER_CTX_init(&cctx);
  730. if (enclevel)
  731. outlen += PVK_SALTLEN;
  732. pklen = do_i2b(NULL, pk, 0);
  733. if (pklen < 0)
  734. return -1;
  735. outlen += pklen;
  736. if (!out)
  737. return outlen;
  738. if (*out)
  739. p = *out;
  740. else {
  741. p = OPENSSL_malloc(outlen);
  742. if (!p) {
  743. PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
  744. return -1;
  745. }
  746. *out = p;
  747. }
  748. write_ledword(&p, MS_PVKMAGIC);
  749. write_ledword(&p, 0);
  750. if (pk->type == EVP_PKEY_DSA)
  751. write_ledword(&p, MS_KEYTYPE_SIGN);
  752. else
  753. write_ledword(&p, MS_KEYTYPE_KEYX);
  754. write_ledword(&p, enclevel ? 1 : 0);
  755. write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
  756. write_ledword(&p, pklen);
  757. if (enclevel) {
  758. if (RAND_bytes(p, PVK_SALTLEN) <= 0)
  759. goto error;
  760. salt = p;
  761. p += PVK_SALTLEN;
  762. }
  763. do_i2b(&p, pk, 0);
  764. if (enclevel == 0)
  765. return outlen;
  766. else {
  767. char psbuf[PEM_BUFSIZE];
  768. unsigned char keybuf[20];
  769. int enctmplen, inlen;
  770. if (cb)
  771. inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
  772. else
  773. inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
  774. if (inlen <= 0) {
  775. PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
  776. goto error;
  777. }
  778. if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
  779. (unsigned char *)psbuf, inlen))
  780. goto error;
  781. if (enclevel == 1)
  782. memset(keybuf + 5, 0, 11);
  783. p = salt + PVK_SALTLEN + 8;
  784. if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
  785. goto error;
  786. OPENSSL_cleanse(keybuf, 20);
  787. if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))
  788. goto error;
  789. if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))
  790. goto error;
  791. }
  792. EVP_CIPHER_CTX_cleanup(&cctx);
  793. return outlen;
  794. error:
  795. EVP_CIPHER_CTX_cleanup(&cctx);
  796. return -1;
  797. }
  798. int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
  799. pem_password_cb *cb, void *u)
  800. {
  801. unsigned char *tmp = NULL;
  802. int outlen, wrlen;
  803. outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
  804. if (outlen < 0)
  805. return -1;
  806. wrlen = BIO_write(out, tmp, outlen);
  807. OPENSSL_free(tmp);
  808. if (wrlen == outlen) {
  809. PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
  810. return outlen;
  811. }
  812. return -1;
  813. }
  814. # endif
  815. #endif