crypto_keys_subr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <limits.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <openssl/bn.h>
  8. #include <openssl/err.h>
  9. #include <openssl/rsa.h>
  10. #include "crypto_compat.h"
  11. #include "crypto_entropy.h"
  12. #include "sysendian.h"
  13. #include "warnp.h"
  14. #include "crypto_internal.h"
  15. /**
  16. * RSA private key data format:
  17. * n || e || d || p || q || (d mod (p-1)) || (d mod (q-1)) || (1/q mod p)
  18. * RSA public key data format:
  19. * n || e
  20. * All integers are stored in little-endian large integer format:
  21. * len || x[0] || x[1] ... x[len - 1]
  22. * where len is a 32-bit little-endian integer.
  23. */
  24. /**
  25. * HMAC key data format:
  26. * x[0] || x[1] || x[2] ... x[31]
  27. */
  28. static int import_BN(BIGNUM **, const uint8_t **, size_t *);
  29. static int export_BN(const BIGNUM *, uint8_t **, size_t *, uint32_t *);
  30. /**
  31. * import_BN(bn, buf, buflen):
  32. * Import a large integer from the provided buffer, advance the buffer
  33. * pointer, and adjust the remaining buffer length.
  34. */
  35. static int
  36. import_BN(BIGNUM ** bn, const uint8_t ** buf, size_t * buflen)
  37. {
  38. uint32_t len;
  39. uint8_t * bnbuf;
  40. size_t i;
  41. /* Parse integer length. */
  42. if (*buflen < sizeof(uint32_t)) {
  43. warn0("Unexpected EOF of key data");
  44. goto err0;
  45. }
  46. len = le32dec(*buf);
  47. *buf += sizeof(uint32_t);
  48. *buflen -= sizeof(uint32_t);
  49. /* Sanity check. */
  50. if (len > INT_MAX) {
  51. warn0("Unexpected key length");
  52. goto err0;
  53. }
  54. /* Make sure there's enough data. */
  55. if (*buflen < len) {
  56. warn0("Unexpected EOF of key data");
  57. goto err0;
  58. }
  59. /*
  60. * OpenSSL's BN_bin2bn wants input in big-endian format, so we need
  61. * to use a temporary buffer to convert from le to be.
  62. */
  63. if ((bnbuf = malloc(len)) == NULL)
  64. goto err0;
  65. for (i = 0; i < len; i++)
  66. bnbuf[len - 1 - i] = (*buf)[i];
  67. if ((*bn = BN_bin2bn(bnbuf, (int)len, NULL)) == NULL) {
  68. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  69. goto err1;
  70. }
  71. free(bnbuf);
  72. /* Advance buffer pointer, adjust remaining buffer length. */
  73. *buf += len;
  74. *buflen -= len;
  75. /* Success! */
  76. return (0);
  77. err1:
  78. free(bnbuf);
  79. err0:
  80. /* Failure! */
  81. return (-1);
  82. }
  83. /**
  84. * export_BN(bn, buf, buflen, len):
  85. * If ${*buf} != NULL, export the provided large integer into the buffer,
  86. * and adjust the buffer pointer and remaining buffer length appropriately.
  87. * Add the required storage length to ${len}.
  88. */
  89. static int
  90. export_BN(const BIGNUM * bn, uint8_t ** buf, size_t * buflen,
  91. uint32_t * len)
  92. {
  93. size_t i;
  94. unsigned int bnlen;
  95. /* Figure out how much space we need. */
  96. bnlen = (unsigned int)BN_num_bytes(bn);
  97. /* Add the required storage length to ${len}. */
  98. if (*len + sizeof(uint32_t) < *len) {
  99. errno = ENOMEM;
  100. goto err0;
  101. }
  102. *len += sizeof(uint32_t);
  103. if (*len + bnlen < *len) {
  104. errno = ENOMEM;
  105. goto err0;
  106. }
  107. *len += bnlen;
  108. /* If ${*buf} == NULL, we're done. */
  109. if (*buf == NULL)
  110. goto done;
  111. /* Export the length of the integer. */
  112. if (*buflen < sizeof(uint32_t)) {
  113. warn0("Unexpected end of key buffer");
  114. goto err0;
  115. }
  116. le32enc(*buf, bnlen);
  117. *buf += sizeof(uint32_t);
  118. *buflen -= sizeof(uint32_t);
  119. /* Export the key as a big-endian integer. */
  120. if (*buflen < bnlen) {
  121. warn0("Unexpected end of key buffer");
  122. goto err0;
  123. }
  124. BN_bn2bin(bn, *buf);
  125. /* Convert to little-endian format. */
  126. for (i = 0; i < bnlen - 1 - i; i++) {
  127. (*buf)[i] ^= (*buf)[bnlen - 1 - i];
  128. (*buf)[bnlen - 1 - i] ^= (*buf)[i];
  129. (*buf)[i] ^= (*buf)[bnlen - 1 - i];
  130. }
  131. /* Adjust buffer pointer and remaining buffer length. */
  132. *buf += bnlen;
  133. *buflen -= bnlen;
  134. done:
  135. /* Success! */
  136. return (0);
  137. err0:
  138. /* Failure! */
  139. return (-1);
  140. }
  141. /**
  142. * crypto_keys_subr_import_RSA_priv(key, buf, buflen):
  143. * Import the specified RSA private key from the provided buffer.
  144. */
  145. int
  146. crypto_keys_subr_import_RSA_priv(void ** key, const uint8_t * buf,
  147. size_t buflen)
  148. {
  149. BIGNUM * n, * e, * d, * p, * q, * dmp1, * dmq1, * iqmp;
  150. /* This simplifies the error path cleanup. */
  151. n = e = d = p = q = dmp1 = dmq1 = iqmp = NULL;
  152. /* Free any existing key. */
  153. if (*key != NULL)
  154. RSA_free(*key);
  155. *key = NULL;
  156. /* Create a new key. */
  157. if ((*key = RSA_new()) == NULL) {
  158. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  159. goto err0;
  160. }
  161. /* Load values. */
  162. if (import_BN(&n, &buf, &buflen))
  163. goto err2;
  164. if (import_BN(&e, &buf, &buflen))
  165. goto err2;
  166. if (import_BN(&d, &buf, &buflen))
  167. goto err2;
  168. if (import_BN(&p, &buf, &buflen))
  169. goto err2;
  170. if (import_BN(&q, &buf, &buflen))
  171. goto err2;
  172. if (import_BN(&dmp1, &buf, &buflen))
  173. goto err2;
  174. if (import_BN(&dmq1, &buf, &buflen))
  175. goto err2;
  176. if (import_BN(&iqmp, &buf, &buflen))
  177. goto err2;
  178. /* We should have no unprocessed data left. */
  179. if (buflen)
  180. goto err2;
  181. /* Load values into the RSA key. */
  182. if (crypto_compat_RSA_import(*key, n, e, d, p, q, dmp1, dmq1, iqmp))
  183. goto err1;
  184. /* Success! */
  185. return (0);
  186. err2:
  187. BN_free(n);
  188. BN_free(e);
  189. BN_clear_free(d);
  190. BN_clear_free(p);
  191. BN_clear_free(q);
  192. BN_clear_free(dmp1);
  193. BN_clear_free(dmq1);
  194. BN_clear_free(iqmp);
  195. err1:
  196. RSA_free(*key);
  197. *key = NULL;
  198. err0:
  199. /* Failure! */
  200. return (-1);
  201. }
  202. /**
  203. * crypto_keys_subr_import_RSA_pub(key, buf, buflen):
  204. * Import the specified RSA public key from the provided buffer.
  205. */
  206. int
  207. crypto_keys_subr_import_RSA_pub(void ** key, const uint8_t * buf, size_t buflen)
  208. {
  209. BIGNUM * n, * e;
  210. /* This simplifies the error path cleanup. */
  211. n = e = NULL;
  212. /* Free any existing key. */
  213. if (*key != NULL)
  214. RSA_free(*key);
  215. *key = NULL;
  216. /* Create a new key. */
  217. if ((*key = RSA_new()) == NULL) {
  218. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  219. goto err0;
  220. }
  221. /* Load values. */
  222. if (import_BN(&n, &buf, &buflen))
  223. goto err2;
  224. if (import_BN(&e, &buf, &buflen))
  225. goto err2;
  226. /* We should have no unprocessed data left. */
  227. if (buflen)
  228. goto err2;
  229. /* Load values into the RSA key. */
  230. if (crypto_compat_RSA_import(*key, n, e, NULL, NULL, NULL, NULL, NULL,
  231. NULL))
  232. goto err1;
  233. /* Success! */
  234. return (0);
  235. err2:
  236. BN_free(n);
  237. BN_free(e);
  238. err1:
  239. RSA_free(*key);
  240. *key = NULL;
  241. err0:
  242. /* Failure! */
  243. return (-1);
  244. }
  245. /**
  246. * crypto_keys_subr_import_HMAC(key, buf, buflen):
  247. * Import the specified HMAC key from the provided buffer.
  248. */
  249. int
  250. crypto_keys_subr_import_HMAC(struct crypto_hmac_key ** key,
  251. const uint8_t * buf, size_t buflen)
  252. {
  253. /* Free any existing key. */
  254. if (*key != NULL) {
  255. free((*key)->key);
  256. free(*key);
  257. }
  258. *key = NULL;
  259. /* Make sure the buffer is the right length. */
  260. if (buflen != 32) {
  261. warn0("Incorrect HMAC key size: %zu", buflen);
  262. goto err0;
  263. }
  264. /* Allocate key structure. */
  265. if ((*key = malloc(sizeof(struct crypto_hmac_key))) == NULL)
  266. goto err0;
  267. /* Allocate key buffer. */
  268. if (((*key)->key = malloc(buflen)) == NULL)
  269. goto err1;
  270. /* Copy key data and length. */
  271. (*key)->len = buflen;
  272. memcpy((*key)->key, buf, buflen);
  273. /* Success! */
  274. return (0);
  275. err1:
  276. free(*key);
  277. *key = NULL;
  278. err0:
  279. /* Failure! */
  280. return (-1);
  281. }
  282. /**
  283. * crypto_keys_subr_export_RSA_priv(key, buf, buflen):
  284. * If buf != NULL, export the specified RSA private key. Return the key
  285. * length in bytes.
  286. */
  287. uint32_t
  288. crypto_keys_subr_export_RSA_priv(void * key, uint8_t * buf, size_t buflen)
  289. {
  290. const BIGNUM * n, * e, * d, * p, * q, * dmp1, * dmq1, * iqmp;
  291. uint32_t len = 0;
  292. if (key == NULL) {
  293. warn0("Cannot export a key which we don't have!");
  294. goto err0;
  295. }
  296. /* Get values from the RSA key. */
  297. if (crypto_compat_RSA_export(key, &n, &e, &d, &p, &q, &dmp1, &dmq1,
  298. &iqmp))
  299. goto err0;
  300. /* Each large integer gets exported. */
  301. if (export_BN(n, &buf, &buflen, &len))
  302. goto err0;
  303. if (export_BN(e, &buf, &buflen, &len))
  304. goto err0;
  305. if (export_BN(d, &buf, &buflen, &len))
  306. goto err0;
  307. if (export_BN(p, &buf, &buflen, &len))
  308. goto err0;
  309. if (export_BN(q, &buf, &buflen, &len))
  310. goto err0;
  311. if (export_BN(dmp1, &buf, &buflen, &len))
  312. goto err0;
  313. if (export_BN(dmq1, &buf, &buflen, &len))
  314. goto err0;
  315. if (export_BN(iqmp, &buf, &buflen, &len))
  316. goto err0;
  317. /* Success! */
  318. return (len);
  319. err0:
  320. /* Failure! */
  321. return ((uint32_t)(-1));
  322. }
  323. /**
  324. * crypto_keys_subr_export_RSA_pub(key, buf, buflen):
  325. * If buf != NULL, export the specified RSA public key. Return the key
  326. * length in bytes.
  327. */
  328. uint32_t
  329. crypto_keys_subr_export_RSA_pub(void * key, uint8_t * buf, size_t buflen)
  330. {
  331. const BIGNUM * n, * e;
  332. uint32_t len = 0;
  333. if (key == NULL) {
  334. warn0("Cannot export a key which we don't have!");
  335. goto err0;
  336. }
  337. /* Get values from the RSA key. */
  338. if (crypto_compat_RSA_export(key, &n, &e, NULL, NULL, NULL, NULL, NULL,
  339. NULL))
  340. goto err0;
  341. /* Each large integer gets exported. */
  342. if (export_BN(n, &buf, &buflen, &len))
  343. goto err0;
  344. if (export_BN(e, &buf, &buflen, &len))
  345. goto err0;
  346. /* Success! */
  347. return (len);
  348. err0:
  349. /* Failure! */
  350. return ((uint32_t)(-1));
  351. }
  352. /**
  353. * crypto_keys_subr_export_HMAC(key, buf, buflen):
  354. * If buf != NULL, export the specified HMAC key. Return the key length
  355. * in bytes.
  356. */
  357. uint32_t
  358. crypto_keys_subr_export_HMAC(struct crypto_hmac_key * key, uint8_t * buf,
  359. size_t buflen)
  360. {
  361. if (key == NULL) {
  362. warn0("Cannot export a key which we don't have!");
  363. goto err0;
  364. }
  365. /* Sanity check. (uint32_t)(-1) is reserved for errors. */
  366. assert(key->len <= UINT32_MAX - 1);
  367. if (buf != NULL) {
  368. if (buflen < key->len) {
  369. warn0("Unexpected end of key buffer");
  370. goto err0;
  371. }
  372. memcpy(buf, key->key, key->len);
  373. }
  374. /* Success! */
  375. return ((uint32_t)(key->len));
  376. err0:
  377. /* Failure! */
  378. return ((uint32_t)(-1));
  379. }
  380. /**
  381. * crypto_keys_subr_generate_RSA(priv, pub):
  382. * Generate an RSA key and store the private and public parts.
  383. */
  384. int
  385. crypto_keys_subr_generate_RSA(void ** priv, void ** pub)
  386. {
  387. /* Free any existing keys. */
  388. if (*priv != NULL)
  389. RSA_free(*priv);
  390. if (*pub != NULL)
  391. RSA_free(*pub);
  392. *priv = *pub = NULL;
  393. if ((*priv = crypto_compat_RSA_generate_key()) == NULL) {
  394. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  395. goto err0;
  396. }
  397. if ((*pub = RSAPublicKey_dup(*priv)) == NULL) {
  398. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  399. goto err1;
  400. }
  401. /* Success! */
  402. return (0);
  403. err1:
  404. RSA_free(*priv);
  405. *priv = NULL;
  406. err0:
  407. /* Failure! */
  408. return (-1);
  409. }
  410. /**
  411. * crypto_keys_subr_generate_HMAC(key):
  412. * Generate an HMAC key.
  413. */
  414. int
  415. crypto_keys_subr_generate_HMAC(struct crypto_hmac_key ** key)
  416. {
  417. /* Free any existing key. */
  418. if (*key != NULL) {
  419. free((*key)->key);
  420. free(*key);
  421. }
  422. /* Allocate memory. */
  423. if ((*key = malloc(sizeof(struct crypto_hmac_key))) == NULL)
  424. goto err0;
  425. if (((*key)->key = malloc(32)) == NULL)
  426. goto err1;
  427. /* Store key length. */
  428. (*key)->len = 32;
  429. /* Generate key. */
  430. if (crypto_entropy_read((*key)->key, 32)) {
  431. warnp("Could not obtain sufficient entropy");
  432. goto err2;
  433. }
  434. /* Success! */
  435. return (0);
  436. err2:
  437. free((*key)->key);
  438. err1:
  439. free(*key);
  440. err0:
  441. /* Failure! */
  442. return (-1);
  443. }
  444. /**
  445. * crypto_keys_subr_free_HMAC(key):
  446. * Free an HMAC key.
  447. */
  448. void
  449. crypto_keys_subr_free_HMAC(struct crypto_hmac_key ** key)
  450. {
  451. if (*key != NULL) {
  452. free((*key)->key);
  453. free(*key);
  454. }
  455. *key = NULL;
  456. }