pvkfmt.c 25 KB

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