pvkfmt.c 24 KB

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