pvkfmt.c 30 KB

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