pvkfmt.c 24 KB

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