pvkfmt.c 29 KB

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