pvkfmt.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * Copyright 2005-2023 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. * RSA and DSA low level APIs are deprecated for public use, but still ok for
  15. * internal use.
  16. */
  17. #include "internal/deprecated.h"
  18. #include <openssl/pem.h>
  19. #include <openssl/rand.h>
  20. #include <openssl/bn.h>
  21. #include <openssl/dsa.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/kdf.h>
  24. #include <openssl/core_names.h>
  25. #include "internal/cryptlib.h"
  26. #include "crypto/pem.h"
  27. #include "crypto/evp.h"
  28. /*
  29. * Utility function: read a DWORD (4 byte unsigned integer) in little endian
  30. * format
  31. */
  32. static unsigned int read_ledword(const unsigned char **in)
  33. {
  34. const unsigned char *p = *in;
  35. unsigned int ret;
  36. ret = (unsigned int)*p++;
  37. ret |= (unsigned int)*p++ << 8;
  38. ret |= (unsigned int)*p++ << 16;
  39. ret |= (unsigned int)*p++ << 24;
  40. *in = p;
  41. return ret;
  42. }
  43. /*
  44. * Read a BIGNUM in little endian format. The docs say that this should take
  45. * up bitlen/8 bytes.
  46. */
  47. static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
  48. {
  49. *r = BN_lebin2bn(*in, nbyte, NULL);
  50. if (*r == NULL)
  51. return 0;
  52. *in += nbyte;
  53. return 1;
  54. }
  55. /*
  56. * Create an EVP_PKEY from a type specific key.
  57. * This takes ownership of |key|, as long as the |evp_type| is acceptable
  58. * (EVP_PKEY_RSA or EVP_PKEY_DSA), even if the resulting EVP_PKEY wasn't
  59. * created.
  60. */
  61. #define isdss_to_evp_type(isdss) \
  62. (isdss == 0 ? EVP_PKEY_RSA : isdss == 1 ? EVP_PKEY_DSA : EVP_PKEY_NONE)
  63. static EVP_PKEY *evp_pkey_new0_key(void *key, int evp_type)
  64. {
  65. EVP_PKEY *pkey = NULL;
  66. /*
  67. * It's assumed that if |key| is NULL, something went wrong elsewhere
  68. * and suitable errors are already reported.
  69. */
  70. if (key == NULL)
  71. return NULL;
  72. if (!ossl_assert(evp_type == EVP_PKEY_RSA || evp_type == EVP_PKEY_DSA)) {
  73. ERR_raise(ERR_LIB_PEM, ERR_R_INTERNAL_ERROR);
  74. return NULL;
  75. }
  76. if ((pkey = EVP_PKEY_new()) != NULL) {
  77. switch (evp_type) {
  78. case EVP_PKEY_RSA:
  79. if (EVP_PKEY_set1_RSA(pkey, key))
  80. break;
  81. ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
  82. EVP_PKEY_free(pkey);
  83. pkey = NULL;
  84. break;
  85. #ifndef OPENSSL_NO_DSA
  86. case EVP_PKEY_DSA:
  87. if (EVP_PKEY_set1_DSA(pkey, key))
  88. break;
  89. ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
  90. EVP_PKEY_free(pkey);
  91. pkey = NULL;
  92. break;
  93. #endif
  94. }
  95. } else {
  96. ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
  97. }
  98. switch (evp_type) {
  99. case EVP_PKEY_RSA:
  100. RSA_free(key);
  101. break;
  102. #ifndef OPENSSL_NO_DSA
  103. case EVP_PKEY_DSA:
  104. DSA_free(key);
  105. break;
  106. #endif
  107. }
  108. return pkey;
  109. }
  110. /* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
  111. # define MS_PUBLICKEYBLOB 0x6
  112. # define MS_PRIVATEKEYBLOB 0x7
  113. # define MS_RSA1MAGIC 0x31415352L
  114. # define MS_RSA2MAGIC 0x32415352L
  115. # define MS_DSS1MAGIC 0x31535344L
  116. # define MS_DSS2MAGIC 0x32535344L
  117. # define MS_KEYALG_RSA_KEYX 0xa400
  118. # define MS_KEYALG_DSS_SIGN 0x2200
  119. # define MS_KEYTYPE_KEYX 0x1
  120. # define MS_KEYTYPE_SIGN 0x2
  121. /* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
  122. # define MS_PVKMAGIC 0xb0b5f11eL
  123. /* Salt length for PVK files */
  124. # define PVK_SALTLEN 0x10
  125. /* Maximum length in PVK header */
  126. # define PVK_MAX_KEYLEN 102400
  127. /* Maximum salt length */
  128. # define PVK_MAX_SALTLEN 10240
  129. /*
  130. * Read the MSBLOB header and get relevant data from it.
  131. *
  132. * |pisdss| and |pispub| have a double role, as they can be used for
  133. * discovery as well as to check the blob meets expectations.
  134. * |*pisdss| is the indicator for whether the key is a DSA key or not.
  135. * |*pispub| is the indicator for whether the key is public or not.
  136. * In both cases, the following input values apply:
  137. *
  138. * 0 Expected to not be what the variable indicates.
  139. * 1 Expected to be what the variable indicates.
  140. * -1 No expectations, this function will assign 0 or 1 depending on
  141. * header data.
  142. */
  143. int ossl_do_blob_header(const unsigned char **in, unsigned int length,
  144. unsigned int *pmagic, unsigned int *pbitlen,
  145. int *pisdss, int *pispub)
  146. {
  147. const unsigned char *p = *in;
  148. if (length < 16)
  149. return 0;
  150. /* bType */
  151. switch (*p) {
  152. case MS_PUBLICKEYBLOB:
  153. if (*pispub == 0) {
  154. ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
  155. return 0;
  156. }
  157. *pispub = 1;
  158. break;
  159. case MS_PRIVATEKEYBLOB:
  160. if (*pispub == 1) {
  161. ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
  162. return 0;
  163. }
  164. *pispub = 0;
  165. break;
  166. default:
  167. return 0;
  168. }
  169. p++;
  170. /* Version */
  171. if (*p++ != 0x2) {
  172. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER);
  173. return 0;
  174. }
  175. /* Ignore reserved, aiKeyAlg */
  176. p += 6;
  177. *pmagic = read_ledword(&p);
  178. *pbitlen = read_ledword(&p);
  179. /* Consistency check for private vs public */
  180. switch (*pmagic) {
  181. case MS_DSS1MAGIC:
  182. case MS_RSA1MAGIC:
  183. if (*pispub == 0) {
  184. ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
  185. return 0;
  186. }
  187. break;
  188. case MS_DSS2MAGIC:
  189. case MS_RSA2MAGIC:
  190. if (*pispub == 1) {
  191. ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
  192. return 0;
  193. }
  194. break;
  195. default:
  196. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
  197. return -1;
  198. }
  199. /* Check that we got the expected type */
  200. switch (*pmagic) {
  201. case MS_DSS1MAGIC:
  202. case MS_DSS2MAGIC:
  203. if (*pisdss == 0) {
  204. ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_DSS_KEY_BLOB);
  205. return 0;
  206. }
  207. *pisdss = 1;
  208. break;
  209. case MS_RSA1MAGIC:
  210. case MS_RSA2MAGIC:
  211. if (*pisdss == 1) {
  212. ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_RSA_KEY_BLOB);
  213. return 0;
  214. }
  215. *pisdss = 0;
  216. break;
  217. default:
  218. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
  219. return -1;
  220. }
  221. *in = p;
  222. return 1;
  223. }
  224. unsigned int ossl_blob_length(unsigned bitlen, int isdss, int ispub)
  225. {
  226. unsigned int nbyte = (bitlen + 7) >> 3;
  227. unsigned int hnbyte = (bitlen + 15) >> 4;
  228. if (isdss) {
  229. /*
  230. * Expected length: 20 for q + 3 components bitlen each + 24 for seed
  231. * structure.
  232. */
  233. if (ispub)
  234. return 44 + 3 * nbyte;
  235. /*
  236. * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
  237. * structure.
  238. */
  239. else
  240. return 64 + 2 * nbyte;
  241. } else {
  242. /* Expected length: 4 for 'e' + 'n' */
  243. if (ispub)
  244. return 4 + nbyte;
  245. else
  246. /*
  247. * Expected length: 4 for 'e' and 7 other components. 2
  248. * components are bitlen size, 5 are bitlen/2
  249. */
  250. return 4 + 2 * nbyte + 5 * hnbyte;
  251. }
  252. }
  253. static void *do_b2i_key(const unsigned char **in, unsigned int length,
  254. int *isdss, int *ispub)
  255. {
  256. const unsigned char *p = *in;
  257. unsigned int bitlen, magic;
  258. void *key = NULL;
  259. if (ossl_do_blob_header(&p, length, &magic, &bitlen, isdss, ispub) <= 0) {
  260. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
  261. return NULL;
  262. }
  263. length -= 16;
  264. if (length < ossl_blob_length(bitlen, *isdss, *ispub)) {
  265. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  266. return NULL;
  267. }
  268. if (!*isdss)
  269. key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
  270. #ifndef OPENSSL_NO_DSA
  271. else
  272. key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
  273. #endif
  274. if (key == NULL) {
  275. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  276. return NULL;
  277. }
  278. return key;
  279. }
  280. EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub)
  281. {
  282. int isdss = -1;
  283. void *key = do_b2i_key(in, length, &isdss, ispub);
  284. return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
  285. }
  286. EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub)
  287. {
  288. const unsigned char *p;
  289. unsigned char hdr_buf[16], *buf = NULL;
  290. unsigned int bitlen, magic, length;
  291. int isdss = -1;
  292. void *key = NULL;
  293. EVP_PKEY *pkey = NULL;
  294. if (BIO_read(in, hdr_buf, 16) != 16) {
  295. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  296. return NULL;
  297. }
  298. p = hdr_buf;
  299. if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0)
  300. return NULL;
  301. length = ossl_blob_length(bitlen, isdss, *ispub);
  302. if (length > BLOB_MAX_LENGTH) {
  303. ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
  304. return NULL;
  305. }
  306. buf = OPENSSL_malloc(length);
  307. if (buf == NULL)
  308. goto err;
  309. p = buf;
  310. if (BIO_read(in, buf, length) != (int)length) {
  311. ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
  312. goto err;
  313. }
  314. if (!isdss)
  315. key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
  316. #ifndef OPENSSL_NO_DSA
  317. else
  318. key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
  319. #endif
  320. if (key == NULL) {
  321. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  322. goto err;
  323. }
  324. pkey = evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
  325. err:
  326. OPENSSL_free(buf);
  327. return pkey;
  328. }
  329. #ifndef OPENSSL_NO_DSA
  330. DSA *ossl_b2i_DSA_after_header(const unsigned char **in, unsigned int bitlen,
  331. int ispub)
  332. {
  333. const unsigned char *p = *in;
  334. DSA *dsa = NULL;
  335. BN_CTX *ctx = NULL;
  336. BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
  337. BIGNUM *pub_key = NULL;
  338. unsigned int nbyte = (bitlen + 7) >> 3;
  339. dsa = DSA_new();
  340. if (dsa == NULL)
  341. goto dsaerr;
  342. if (!read_lebn(&p, nbyte, &pbn))
  343. goto bnerr;
  344. if (!read_lebn(&p, 20, &qbn))
  345. goto bnerr;
  346. if (!read_lebn(&p, nbyte, &gbn))
  347. goto bnerr;
  348. if (ispub) {
  349. if (!read_lebn(&p, nbyte, &pub_key))
  350. goto bnerr;
  351. } else {
  352. if (!read_lebn(&p, 20, &priv_key))
  353. goto bnerr;
  354. /* Set constant time flag before public key calculation */
  355. BN_set_flags(priv_key, BN_FLG_CONSTTIME);
  356. /* Calculate public key */
  357. pub_key = BN_new();
  358. if (pub_key == NULL)
  359. goto bnerr;
  360. if ((ctx = BN_CTX_new()) == NULL)
  361. goto bnerr;
  362. if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
  363. goto bnerr;
  364. BN_CTX_free(ctx);
  365. ctx = NULL;
  366. }
  367. if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
  368. goto dsaerr;
  369. pbn = qbn = gbn = NULL;
  370. if (!DSA_set0_key(dsa, pub_key, priv_key))
  371. goto dsaerr;
  372. pub_key = priv_key = NULL;
  373. *in = p;
  374. return dsa;
  375. dsaerr:
  376. ERR_raise(ERR_LIB_PEM, ERR_R_DSA_LIB);
  377. goto err;
  378. bnerr:
  379. ERR_raise(ERR_LIB_PEM, ERR_R_BN_LIB);
  380. err:
  381. DSA_free(dsa);
  382. BN_free(pbn);
  383. BN_free(qbn);
  384. BN_free(gbn);
  385. BN_free(pub_key);
  386. BN_free(priv_key);
  387. BN_CTX_free(ctx);
  388. return NULL;
  389. }
  390. #endif
  391. RSA *ossl_b2i_RSA_after_header(const unsigned char **in, unsigned int bitlen,
  392. int ispub)
  393. {
  394. const unsigned char *pin = *in;
  395. BIGNUM *e = NULL, *n = NULL, *d = NULL;
  396. BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
  397. RSA *rsa = NULL;
  398. unsigned int nbyte = (bitlen + 7) >> 3;
  399. unsigned int hnbyte = (bitlen + 15) >> 4;
  400. rsa = RSA_new();
  401. if (rsa == NULL)
  402. goto rsaerr;
  403. e = BN_new();
  404. if (e == NULL)
  405. goto bnerr;
  406. if (!BN_set_word(e, read_ledword(&pin)))
  407. goto bnerr;
  408. if (!read_lebn(&pin, nbyte, &n))
  409. goto bnerr;
  410. if (!ispub) {
  411. if (!read_lebn(&pin, hnbyte, &p))
  412. goto bnerr;
  413. if (!read_lebn(&pin, hnbyte, &q))
  414. goto bnerr;
  415. if (!read_lebn(&pin, hnbyte, &dmp1))
  416. goto bnerr;
  417. if (!read_lebn(&pin, hnbyte, &dmq1))
  418. goto bnerr;
  419. if (!read_lebn(&pin, hnbyte, &iqmp))
  420. goto bnerr;
  421. if (!read_lebn(&pin, nbyte, &d))
  422. goto bnerr;
  423. if (!RSA_set0_factors(rsa, p, q))
  424. goto rsaerr;
  425. p = q = NULL;
  426. if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
  427. goto rsaerr;
  428. dmp1 = dmq1 = iqmp = NULL;
  429. }
  430. if (!RSA_set0_key(rsa, n, e, d))
  431. goto rsaerr;
  432. n = e = d = NULL;
  433. *in = pin;
  434. return rsa;
  435. rsaerr:
  436. ERR_raise(ERR_LIB_PEM, ERR_R_RSA_LIB);
  437. goto err;
  438. bnerr:
  439. ERR_raise(ERR_LIB_PEM, ERR_R_BN_LIB);
  440. err:
  441. BN_free(e);
  442. BN_free(n);
  443. BN_free(p);
  444. BN_free(q);
  445. BN_free(dmp1);
  446. BN_free(dmq1);
  447. BN_free(iqmp);
  448. BN_free(d);
  449. RSA_free(rsa);
  450. return NULL;
  451. }
  452. EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
  453. {
  454. int ispub = 0;
  455. return ossl_b2i(in, length, &ispub);
  456. }
  457. EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
  458. {
  459. int ispub = 1;
  460. return ossl_b2i(in, length, &ispub);
  461. }
  462. EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
  463. {
  464. int ispub = 0;
  465. return ossl_b2i_bio(in, &ispub);
  466. }
  467. EVP_PKEY *b2i_PublicKey_bio(BIO *in)
  468. {
  469. int ispub = 1;
  470. return ossl_b2i_bio(in, &ispub);
  471. }
  472. static void write_ledword(unsigned char **out, unsigned int dw)
  473. {
  474. unsigned char *p = *out;
  475. *p++ = dw & 0xff;
  476. *p++ = (dw >> 8) & 0xff;
  477. *p++ = (dw >> 16) & 0xff;
  478. *p++ = (dw >> 24) & 0xff;
  479. *out = p;
  480. }
  481. static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
  482. {
  483. BN_bn2lebinpad(bn, *out, len);
  484. *out += len;
  485. }
  486. static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *magic);
  487. static void write_rsa(unsigned char **out, const RSA *rsa, int ispub);
  488. #ifndef OPENSSL_NO_DSA
  489. static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *magic);
  490. static void write_dsa(unsigned char **out, const DSA *dsa, int ispub);
  491. #endif
  492. static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub)
  493. {
  494. unsigned char *p;
  495. unsigned int bitlen = 0, magic = 0, keyalg = 0;
  496. int outlen = -1, noinc = 0;
  497. if (EVP_PKEY_is_a(pk, "RSA")) {
  498. bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
  499. keyalg = MS_KEYALG_RSA_KEYX;
  500. #ifndef OPENSSL_NO_DSA
  501. } else if (EVP_PKEY_is_a(pk, "DSA")) {
  502. bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
  503. keyalg = MS_KEYALG_DSS_SIGN;
  504. #endif
  505. }
  506. if (bitlen == 0) {
  507. goto end;
  508. }
  509. outlen = 16
  510. + ossl_blob_length(bitlen, keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
  511. if (out == NULL)
  512. goto end;
  513. if (*out)
  514. p = *out;
  515. else {
  516. if ((p = OPENSSL_malloc(outlen)) == NULL) {
  517. outlen = -1;
  518. goto end;
  519. }
  520. *out = p;
  521. noinc = 1;
  522. }
  523. if (ispub)
  524. *p++ = MS_PUBLICKEYBLOB;
  525. else
  526. *p++ = MS_PRIVATEKEYBLOB;
  527. *p++ = 0x2;
  528. *p++ = 0;
  529. *p++ = 0;
  530. write_ledword(&p, keyalg);
  531. write_ledword(&p, magic);
  532. write_ledword(&p, bitlen);
  533. if (keyalg == MS_KEYALG_RSA_KEYX)
  534. write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
  535. #ifndef OPENSSL_NO_DSA
  536. else
  537. write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
  538. #endif
  539. if (!noinc)
  540. *out += outlen;
  541. end:
  542. return outlen;
  543. }
  544. static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub)
  545. {
  546. unsigned char *tmp = NULL;
  547. int outlen, wrlen;
  548. outlen = do_i2b(&tmp, pk, ispub);
  549. if (outlen < 0)
  550. return -1;
  551. wrlen = BIO_write(out, tmp, outlen);
  552. OPENSSL_free(tmp);
  553. if (wrlen == outlen)
  554. return outlen;
  555. return -1;
  556. }
  557. static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *pmagic)
  558. {
  559. int nbyte, hnbyte, bitlen;
  560. const BIGNUM *e;
  561. RSA_get0_key(rsa, NULL, &e, NULL);
  562. if (BN_num_bits(e) > 32)
  563. goto badkey;
  564. bitlen = RSA_bits(rsa);
  565. nbyte = RSA_size(rsa);
  566. hnbyte = (bitlen + 15) >> 4;
  567. if (ispub) {
  568. *pmagic = MS_RSA1MAGIC;
  569. return bitlen;
  570. } else {
  571. const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
  572. *pmagic = MS_RSA2MAGIC;
  573. /*
  574. * For private key each component must fit within nbyte or hnbyte.
  575. */
  576. RSA_get0_key(rsa, NULL, NULL, &d);
  577. if (BN_num_bytes(d) > nbyte)
  578. goto badkey;
  579. RSA_get0_factors(rsa, &p, &q);
  580. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  581. if ((BN_num_bytes(iqmp) > hnbyte)
  582. || (BN_num_bytes(p) > hnbyte)
  583. || (BN_num_bytes(q) > hnbyte)
  584. || (BN_num_bytes(dmp1) > hnbyte)
  585. || (BN_num_bytes(dmq1) > hnbyte))
  586. goto badkey;
  587. }
  588. return bitlen;
  589. badkey:
  590. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  591. return 0;
  592. }
  593. static void write_rsa(unsigned char **out, const RSA *rsa, int ispub)
  594. {
  595. int nbyte, hnbyte;
  596. const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
  597. nbyte = RSA_size(rsa);
  598. hnbyte = (RSA_bits(rsa) + 15) >> 4;
  599. RSA_get0_key(rsa, &n, &e, &d);
  600. write_lebn(out, e, 4);
  601. write_lebn(out, n, nbyte);
  602. if (ispub)
  603. return;
  604. RSA_get0_factors(rsa, &p, &q);
  605. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  606. write_lebn(out, p, hnbyte);
  607. write_lebn(out, q, hnbyte);
  608. write_lebn(out, dmp1, hnbyte);
  609. write_lebn(out, dmq1, hnbyte);
  610. write_lebn(out, iqmp, hnbyte);
  611. write_lebn(out, d, nbyte);
  612. }
  613. #ifndef OPENSSL_NO_DSA
  614. static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *pmagic)
  615. {
  616. int bitlen;
  617. const BIGNUM *p = NULL, *q = NULL, *g = NULL;
  618. const BIGNUM *pub_key = NULL, *priv_key = NULL;
  619. DSA_get0_pqg(dsa, &p, &q, &g);
  620. DSA_get0_key(dsa, &pub_key, &priv_key);
  621. bitlen = BN_num_bits(p);
  622. if ((bitlen & 7) || (BN_num_bits(q) != 160)
  623. || (BN_num_bits(g) > bitlen))
  624. goto badkey;
  625. if (ispub) {
  626. if (BN_num_bits(pub_key) > bitlen)
  627. goto badkey;
  628. *pmagic = MS_DSS1MAGIC;
  629. } else {
  630. if (BN_num_bits(priv_key) > 160)
  631. goto badkey;
  632. *pmagic = MS_DSS2MAGIC;
  633. }
  634. return bitlen;
  635. badkey:
  636. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
  637. return 0;
  638. }
  639. static void write_dsa(unsigned char **out, const DSA *dsa, int ispub)
  640. {
  641. int nbyte;
  642. const BIGNUM *p = NULL, *q = NULL, *g = NULL;
  643. const BIGNUM *pub_key = NULL, *priv_key = NULL;
  644. DSA_get0_pqg(dsa, &p, &q, &g);
  645. DSA_get0_key(dsa, &pub_key, &priv_key);
  646. nbyte = BN_num_bytes(p);
  647. write_lebn(out, p, nbyte);
  648. write_lebn(out, q, 20);
  649. write_lebn(out, g, nbyte);
  650. if (ispub)
  651. write_lebn(out, pub_key, nbyte);
  652. else
  653. write_lebn(out, priv_key, 20);
  654. /* Set "invalid" for seed structure values */
  655. memset(*out, 0xff, 24);
  656. *out += 24;
  657. return;
  658. }
  659. #endif
  660. int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk)
  661. {
  662. return do_i2b_bio(out, pk, 0);
  663. }
  664. int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk)
  665. {
  666. return do_i2b_bio(out, pk, 1);
  667. }
  668. int ossl_do_PVK_header(const unsigned char **in, unsigned int length,
  669. int skip_magic,
  670. unsigned int *psaltlen, unsigned int *pkeylen)
  671. {
  672. const unsigned char *p = *in;
  673. unsigned int pvk_magic, is_encrypted;
  674. if (skip_magic) {
  675. if (length < 20) {
  676. ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
  677. return 0;
  678. }
  679. } else {
  680. if (length < 24) {
  681. ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
  682. return 0;
  683. }
  684. pvk_magic = read_ledword(&p);
  685. if (pvk_magic != MS_PVKMAGIC) {
  686. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
  687. return 0;
  688. }
  689. }
  690. /* Skip reserved */
  691. p += 4;
  692. /*
  693. * keytype =
  694. */ read_ledword(&p);
  695. is_encrypted = read_ledword(&p);
  696. *psaltlen = read_ledword(&p);
  697. *pkeylen = read_ledword(&p);
  698. if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
  699. return 0;
  700. if (is_encrypted && *psaltlen == 0) {
  701. ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER);
  702. return 0;
  703. }
  704. *in = p;
  705. return 1;
  706. }
  707. #ifndef OPENSSL_NO_RC4
  708. static int derive_pvk_key(unsigned char *key, size_t keylen,
  709. const unsigned char *salt, unsigned int saltlen,
  710. const unsigned char *pass, int passlen,
  711. OSSL_LIB_CTX *libctx, const char *propq)
  712. {
  713. EVP_KDF *kdf;
  714. EVP_KDF_CTX *ctx;
  715. OSSL_PARAM params[5], *p = params;
  716. int rv;
  717. if ((kdf = EVP_KDF_fetch(libctx, "PVKKDF", propq)) == NULL)
  718. return 0;
  719. ctx = EVP_KDF_CTX_new(kdf);
  720. EVP_KDF_free(kdf);
  721. if (ctx == NULL)
  722. return 0;
  723. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  724. (void *)salt, saltlen);
  725. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  726. (void *)pass, passlen);
  727. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, SN_sha1, 0);
  728. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
  729. (char *)propq, 0);
  730. *p = OSSL_PARAM_construct_end();
  731. rv = EVP_KDF_derive(ctx, key, keylen, params);
  732. EVP_KDF_CTX_free(ctx);
  733. return rv;
  734. }
  735. #endif
  736. static void *do_PVK_body_key(const unsigned char **in,
  737. unsigned int saltlen, unsigned int keylen,
  738. pem_password_cb *cb, void *u,
  739. int *isdss, int *ispub,
  740. OSSL_LIB_CTX *libctx, const char *propq)
  741. {
  742. const unsigned char *p = *in;
  743. unsigned char *enctmp = NULL;
  744. unsigned char keybuf[20];
  745. void *key = NULL;
  746. #ifndef OPENSSL_NO_RC4
  747. EVP_CIPHER *rc4 = NULL;
  748. #endif
  749. EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
  750. if (cctx == NULL) {
  751. ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
  752. goto err;
  753. }
  754. if (saltlen) {
  755. #ifndef OPENSSL_NO_RC4
  756. unsigned int magic;
  757. char psbuf[PEM_BUFSIZE];
  758. int enctmplen, inlen;
  759. unsigned char *q;
  760. if (cb)
  761. inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
  762. else
  763. inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  764. if (inlen < 0) {
  765. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
  766. goto err;
  767. }
  768. enctmp = OPENSSL_malloc(keylen + 8);
  769. if (enctmp == NULL)
  770. goto err;
  771. if (!derive_pvk_key(keybuf, sizeof(keybuf), p, saltlen,
  772. (unsigned char *)psbuf, inlen, libctx, propq))
  773. goto err;
  774. p += saltlen;
  775. /* Copy BLOBHEADER across, decrypt rest */
  776. memcpy(enctmp, p, 8);
  777. p += 8;
  778. if (keylen < 8) {
  779. ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
  780. goto err;
  781. }
  782. inlen = keylen - 8;
  783. q = enctmp + 8;
  784. if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
  785. goto err;
  786. if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
  787. goto err;
  788. if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
  789. goto err;
  790. if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
  791. goto err;
  792. magic = read_ledword((const unsigned char **)&q);
  793. if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
  794. q = enctmp + 8;
  795. memset(keybuf + 5, 0, 11);
  796. if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
  797. goto err;
  798. if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
  799. goto err;
  800. if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
  801. goto err;
  802. magic = read_ledword((const unsigned char **)&q);
  803. if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
  804. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT);
  805. goto err;
  806. }
  807. }
  808. p = enctmp;
  809. #else
  810. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
  811. goto err;
  812. #endif
  813. }
  814. key = do_b2i_key(&p, keylen, isdss, ispub);
  815. err:
  816. EVP_CIPHER_CTX_free(cctx);
  817. #ifndef OPENSSL_NO_RC4
  818. EVP_CIPHER_free(rc4);
  819. #endif
  820. if (enctmp != NULL) {
  821. OPENSSL_cleanse(keybuf, sizeof(keybuf));
  822. OPENSSL_free(enctmp);
  823. }
  824. return key;
  825. }
  826. static void *do_PVK_key_bio(BIO *in, pem_password_cb *cb, void *u,
  827. int *isdss, int *ispub,
  828. OSSL_LIB_CTX *libctx, const char *propq)
  829. {
  830. unsigned char pvk_hdr[24], *buf = NULL;
  831. const unsigned char *p;
  832. int buflen;
  833. void *key = NULL;
  834. unsigned int saltlen, keylen;
  835. if (BIO_read(in, pvk_hdr, 24) != 24) {
  836. ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
  837. return NULL;
  838. }
  839. p = pvk_hdr;
  840. if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen))
  841. return 0;
  842. buflen = (int)keylen + saltlen;
  843. buf = OPENSSL_malloc(buflen);
  844. if (buf == NULL)
  845. return 0;
  846. p = buf;
  847. if (BIO_read(in, buf, buflen) != buflen) {
  848. ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
  849. goto err;
  850. }
  851. key = do_PVK_body_key(&p, saltlen, keylen, cb, u, isdss, ispub, libctx, propq);
  852. err:
  853. OPENSSL_clear_free(buf, buflen);
  854. return key;
  855. }
  856. #ifndef OPENSSL_NO_DSA
  857. DSA *b2i_DSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
  858. OSSL_LIB_CTX *libctx, const char *propq)
  859. {
  860. int isdss = 1;
  861. int ispub = 0; /* PVK keys are always private */
  862. return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
  863. }
  864. DSA *b2i_DSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
  865. {
  866. return b2i_DSA_PVK_bio_ex(in, cb, u, NULL, NULL);
  867. }
  868. #endif
  869. RSA *b2i_RSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
  870. OSSL_LIB_CTX *libctx, const char *propq)
  871. {
  872. int isdss = 0;
  873. int ispub = 0; /* PVK keys are always private */
  874. return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
  875. }
  876. RSA *b2i_RSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
  877. {
  878. return b2i_RSA_PVK_bio_ex(in, cb, u, NULL, NULL);
  879. }
  880. EVP_PKEY *b2i_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
  881. OSSL_LIB_CTX *libctx, const char *propq)
  882. {
  883. int isdss = -1;
  884. int ispub = -1;
  885. void *key = do_PVK_key_bio(in, cb, u, &isdss, &ispub, NULL, NULL);
  886. return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
  887. }
  888. EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
  889. {
  890. return b2i_PVK_bio_ex(in, cb, u, NULL, NULL);
  891. }
  892. static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel,
  893. pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
  894. const char *propq)
  895. {
  896. int ret = -1;
  897. int outlen = 24, pklen;
  898. unsigned char *p = NULL, *start = NULL;
  899. EVP_CIPHER_CTX *cctx = NULL;
  900. #ifndef OPENSSL_NO_RC4
  901. unsigned char *salt = NULL;
  902. EVP_CIPHER *rc4 = NULL;
  903. #endif
  904. if (enclevel)
  905. outlen += PVK_SALTLEN;
  906. pklen = do_i2b(NULL, pk, 0);
  907. if (pklen < 0)
  908. return -1;
  909. outlen += pklen;
  910. if (out == NULL)
  911. return outlen;
  912. if (*out != NULL) {
  913. p = *out;
  914. } else {
  915. start = p = OPENSSL_malloc(outlen);
  916. if (p == NULL)
  917. return -1;
  918. }
  919. cctx = EVP_CIPHER_CTX_new();
  920. if (cctx == NULL)
  921. goto error;
  922. write_ledword(&p, MS_PVKMAGIC);
  923. write_ledword(&p, 0);
  924. if (EVP_PKEY_get_id(pk) == EVP_PKEY_RSA)
  925. write_ledword(&p, MS_KEYTYPE_KEYX);
  926. #ifndef OPENSSL_NO_DSA
  927. else
  928. write_ledword(&p, MS_KEYTYPE_SIGN);
  929. #endif
  930. write_ledword(&p, enclevel ? 1 : 0);
  931. write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
  932. write_ledword(&p, pklen);
  933. if (enclevel) {
  934. #ifndef OPENSSL_NO_RC4
  935. if (RAND_bytes_ex(libctx, p, PVK_SALTLEN, 0) <= 0)
  936. goto error;
  937. salt = p;
  938. p += PVK_SALTLEN;
  939. #endif
  940. }
  941. do_i2b(&p, pk, 0);
  942. if (enclevel != 0) {
  943. #ifndef OPENSSL_NO_RC4
  944. char psbuf[PEM_BUFSIZE];
  945. unsigned char keybuf[20];
  946. int enctmplen, inlen;
  947. if (cb)
  948. inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
  949. else
  950. inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
  951. if (inlen <= 0) {
  952. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
  953. goto error;
  954. }
  955. if (!derive_pvk_key(keybuf, sizeof(keybuf), salt, PVK_SALTLEN,
  956. (unsigned char *)psbuf, inlen, libctx, propq))
  957. goto error;
  958. if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
  959. goto error;
  960. if (enclevel == 1)
  961. memset(keybuf + 5, 0, 11);
  962. p = salt + PVK_SALTLEN + 8;
  963. if (!EVP_EncryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
  964. goto error;
  965. OPENSSL_cleanse(keybuf, 20);
  966. if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
  967. goto error;
  968. if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen))
  969. goto error;
  970. #else
  971. ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
  972. goto error;
  973. #endif
  974. }
  975. if (*out == NULL)
  976. *out = start;
  977. ret = outlen;
  978. error:
  979. EVP_CIPHER_CTX_free(cctx);
  980. #ifndef OPENSSL_NO_RC4
  981. EVP_CIPHER_free(rc4);
  982. #endif
  983. if (*out == NULL)
  984. OPENSSL_free(start);
  985. return ret;
  986. }
  987. int i2b_PVK_bio_ex(BIO *out, const EVP_PKEY *pk, int enclevel,
  988. pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
  989. const char *propq)
  990. {
  991. unsigned char *tmp = NULL;
  992. int outlen, wrlen;
  993. outlen = i2b_PVK(&tmp, pk, enclevel, cb, u, libctx, propq);
  994. if (outlen < 0)
  995. return -1;
  996. wrlen = BIO_write(out, tmp, outlen);
  997. OPENSSL_free(tmp);
  998. if (wrlen == outlen) {
  999. return outlen;
  1000. }
  1001. ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE);
  1002. return -1;
  1003. }
  1004. int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
  1005. pem_password_cb *cb, void *u)
  1006. {
  1007. return i2b_PVK_bio_ex(out, pk, enclevel, cb, u, NULL, NULL);
  1008. }