cert-crypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2020 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <unistd.h>
  27. #include <stdarg.h>
  28. #include <string.h>
  29. #include <strings.h>
  30. #include "checksum.h"
  31. #include "cipher.h"
  32. #include "useful.h"
  33. #include "utf8.h"
  34. #include "asn1-private.h"
  35. #include "private.h"
  36. #include "missing.h"
  37. #include "cert.h"
  38. #include "cert-private.h"
  39. /* RFC8018 section 5.1 */
  40. LIB_EXPORT bool l_cert_pkcs5_pbkdf1(enum l_checksum_type type,
  41. const char *password,
  42. const uint8_t *salt, size_t salt_len,
  43. unsigned int iter_count,
  44. uint8_t *out_dk, size_t dk_len)
  45. {
  46. size_t hash_len, t_len;
  47. uint8_t t[20 + salt_len + strlen(password)];
  48. struct l_checksum *checksum;
  49. switch (type) {
  50. case L_CHECKSUM_MD5:
  51. hash_len = 16;
  52. break;
  53. case L_CHECKSUM_SHA1:
  54. hash_len = 20;
  55. break;
  56. case L_CHECKSUM_NONE:
  57. case L_CHECKSUM_MD4:
  58. case L_CHECKSUM_SHA224:
  59. case L_CHECKSUM_SHA256:
  60. case L_CHECKSUM_SHA384:
  61. case L_CHECKSUM_SHA512:
  62. return false;
  63. default:
  64. return false;
  65. }
  66. if (dk_len > hash_len)
  67. return false;
  68. checksum = l_checksum_new(type);
  69. if (!checksum)
  70. return false;
  71. memcpy(t, password, strlen(password));
  72. memcpy(t + strlen(password), salt, salt_len);
  73. t_len = strlen(password) + salt_len;
  74. while (iter_count) {
  75. l_checksum_reset(checksum);
  76. if (!l_checksum_update(checksum, t, t_len))
  77. break;
  78. if (l_checksum_get_digest(checksum, t, hash_len) !=
  79. (ssize_t) hash_len)
  80. break;
  81. t_len = hash_len;
  82. iter_count--;
  83. }
  84. l_checksum_free(checksum);
  85. if (!iter_count)
  86. memcpy(out_dk, t, dk_len);
  87. explicit_bzero(t, sizeof(t));
  88. return !iter_count;
  89. }
  90. /* RFC8018 section 5.2 */
  91. LIB_EXPORT bool l_cert_pkcs5_pbkdf2(enum l_checksum_type type,
  92. const char *password,
  93. const uint8_t *salt, size_t salt_len,
  94. unsigned int iter_count,
  95. uint8_t *out_dk, size_t dk_len)
  96. {
  97. size_t h_len;
  98. struct l_checksum *checksum;
  99. unsigned int i;
  100. switch (type) {
  101. case L_CHECKSUM_SHA1:
  102. h_len = 20;
  103. break;
  104. case L_CHECKSUM_SHA224:
  105. h_len = 28;
  106. break;
  107. case L_CHECKSUM_SHA256:
  108. h_len = 32;
  109. break;
  110. case L_CHECKSUM_SHA384:
  111. h_len = 48;
  112. break;
  113. case L_CHECKSUM_SHA512:
  114. h_len = 64;
  115. break;
  116. case L_CHECKSUM_NONE:
  117. case L_CHECKSUM_MD4:
  118. case L_CHECKSUM_MD5:
  119. return false;
  120. default:
  121. return false;
  122. }
  123. checksum = l_checksum_new_hmac(type, password, strlen(password));
  124. if (!checksum)
  125. return false;
  126. for (i = 1; dk_len; i++) {
  127. unsigned int j, k;
  128. uint8_t u[salt_len + 64];
  129. size_t u_len;
  130. size_t block_len = h_len;
  131. if (block_len > dk_len)
  132. block_len = dk_len;
  133. memset(out_dk, 0, block_len);
  134. memcpy(u, salt, salt_len);
  135. l_put_be32(i, u + salt_len);
  136. u_len = salt_len + 4;
  137. for (j = 0; j < iter_count; j++) {
  138. l_checksum_reset(checksum);
  139. if (!l_checksum_update(checksum, u, u_len))
  140. break;
  141. if (l_checksum_get_digest(checksum, u, h_len) !=
  142. (ssize_t) h_len)
  143. break;
  144. u_len = h_len;
  145. for (k = 0; k < block_len; k++)
  146. out_dk[k] ^= u[k];
  147. }
  148. if (j < iter_count)
  149. break;
  150. out_dk += block_len;
  151. dk_len -= block_len;
  152. }
  153. l_checksum_free(checksum);
  154. return !dk_len;
  155. }
  156. /* RFC7292 Appendix B */
  157. uint8_t *cert_pkcs12_pbkdf(const char *password,
  158. const struct cert_pkcs12_hash *hash,
  159. const uint8_t *salt, size_t salt_len,
  160. unsigned int iterations, uint8_t id,
  161. size_t key_len)
  162. {
  163. /* All lengths in bytes instead of bits */
  164. size_t passwd_len = password ? 2 * strlen(password) + 2 : 0;
  165. uint8_t *bmpstring;
  166. /* Documented as v(ceiling(s/v)), usually will just equal v */
  167. unsigned int s_len = (salt_len + hash->v - 1) & ~(hash->v - 1);
  168. /* Documented as p(ceiling(s/p)), usually will just equal v */
  169. unsigned int p_len = password ?
  170. (passwd_len + hash->v - 1) & ~(hash->v - 1) : 0;
  171. uint8_t di[hash->v + s_len + p_len];
  172. uint8_t *ptr;
  173. unsigned int j;
  174. uint8_t *key;
  175. unsigned int bytes;
  176. struct l_checksum *h = l_checksum_new(hash->alg);
  177. if (!h)
  178. return NULL;
  179. /*
  180. * The BMPString encoding, in practice same as UCS-2, can end up
  181. * at 2 * strlen(password) + 2 bytes or shorter depending on the
  182. * characters used. Recalculate p_len after we know it.
  183. * Important: The password must be valid UTF-8 here.
  184. */
  185. if (password) {
  186. if (!(bmpstring = l_utf8_to_ucs2be(password, &passwd_len))) {
  187. l_checksum_free(h);
  188. return NULL;
  189. }
  190. p_len = (passwd_len + hash->v - 1) & ~(hash->v - 1);
  191. }
  192. memset(di, id, hash->v);
  193. ptr = di + hash->v;
  194. for (j = salt_len; j < s_len; j += salt_len, ptr += salt_len)
  195. memcpy(ptr, salt, salt_len);
  196. if (s_len) {
  197. memcpy(ptr, salt, s_len + salt_len - j);
  198. ptr += s_len + salt_len - j;
  199. }
  200. if (p_len) {
  201. for (j = passwd_len; j < p_len;
  202. j += passwd_len, ptr += passwd_len)
  203. memcpy(ptr, bmpstring, passwd_len);
  204. memcpy(ptr, bmpstring, p_len + passwd_len - j);
  205. explicit_bzero(bmpstring, passwd_len);
  206. l_free(bmpstring);
  207. }
  208. key = l_malloc(key_len + hash->len);
  209. for (bytes = 0; bytes < key_len; bytes += hash->u) {
  210. uint8_t b[hash->v];
  211. uint8_t *input = di;
  212. unsigned int input_len = hash->v + s_len + p_len;
  213. for (j = 0; j < iterations; j++) {
  214. if (!l_checksum_update(h, input, input_len) ||
  215. l_checksum_get_digest(h,
  216. key + bytes,
  217. hash->len) <= 0) {
  218. l_checksum_free(h);
  219. l_free(key);
  220. return NULL;
  221. }
  222. input = key + bytes;
  223. input_len = hash->u;
  224. l_checksum_reset(h);
  225. }
  226. if (bytes + hash->u >= key_len)
  227. break;
  228. for (j = 0; j < hash->v - hash->u; j += hash->u)
  229. memcpy(b + j, input, hash->u);
  230. memcpy(b + j, input, hash->v - j);
  231. ptr = di + hash->v;
  232. for (j = 0; j < s_len + p_len; j += hash->v, ptr += hash->v) {
  233. unsigned int k;
  234. uint16_t carry = 1;
  235. /*
  236. * Not specified in the RFC7292 but implementations
  237. * sum these octet strings as big-endian integers.
  238. * We could use 64-bit additions here but the benefit
  239. * may not compensate the cost of the byteswapping.
  240. */
  241. for (k = hash->v - 1; k > 0; k--) {
  242. carry = ptr[k] + b[k] + carry;
  243. ptr[k] = carry;
  244. carry >>= 8;
  245. }
  246. ptr[k] += b[k] + carry;
  247. explicit_bzero(&carry, sizeof(carry));
  248. }
  249. explicit_bzero(b, sizeof(b));
  250. }
  251. explicit_bzero(di, sizeof(di));
  252. l_checksum_free(h);
  253. return key;
  254. }
  255. /* RFC7292 Appendix A */
  256. static const struct cert_pkcs12_hash pkcs12_sha1_hash = {
  257. .alg = L_CHECKSUM_SHA1,
  258. .len = 20,
  259. .u = 20,
  260. .v = 64,
  261. .oid = { 5, { 0x2b, 0x0e, 0x03, 0x02, 0x1a } },
  262. };
  263. /* RFC8018 Section A.2 */
  264. static struct asn1_oid pkcs5_pbkdf2_oid = {
  265. 9, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0c }
  266. };
  267. /* RFC8018 Section A.4 */
  268. static struct asn1_oid pkcs5_pbes2_oid = {
  269. 9, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0d }
  270. };
  271. /* RFC8018 Section A.3 */
  272. static const struct pkcs5_pbes1_encryption_oid {
  273. enum l_checksum_type checksum_type;
  274. enum l_cipher_type cipher_type;
  275. struct asn1_oid oid;
  276. } pkcs5_pbes1_encryption_oids[] = {
  277. { /* pbeWithMD5AndDES-CBC */
  278. L_CHECKSUM_MD5, L_CIPHER_DES_CBC,
  279. { 9, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x03 } },
  280. },
  281. { /* pbeWithSHA1AndDES-CBC */
  282. L_CHECKSUM_SHA1, L_CIPHER_DES_CBC,
  283. { 9, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0a } },
  284. },
  285. /* MD2- and RC2-based schemes 1, 4, 6 and 11 not supported */
  286. };
  287. /* RFC7292 Appendix C */
  288. static const struct pkcs12_encryption_oid {
  289. enum l_cipher_type cipher_type;
  290. unsigned int key_length;
  291. unsigned int iv_length;
  292. bool copy_k1; /* Expand the 2-Key 3DES key for 3-Key 3DES */
  293. bool is_block;
  294. struct asn1_oid oid;
  295. } pkcs12_encryption_oids[] = {
  296. { /* pbeWithSHAAnd128BitRC4 */
  297. .cipher_type = L_CIPHER_ARC4,
  298. .key_length = 16,
  299. .oid = { 10, {
  300. 0x2a, 0x86, 0x48, 0x86, 0xf7,
  301. 0x0d, 0x01, 0x0c, 0x01, 0x01,
  302. } }
  303. },
  304. { /* pbeWithSHAAnd40BitRC4 */
  305. .cipher_type = L_CIPHER_ARC4,
  306. .key_length = 5,
  307. .oid = { 10, {
  308. 0x2a, 0x86, 0x48, 0x86, 0xf7,
  309. 0x0d, 0x01, 0x0c, 0x01, 0x02,
  310. } }
  311. },
  312. { /* pbeWithSHAAnd3-KeyTripleDES-CBC */
  313. .cipher_type = L_CIPHER_DES3_EDE_CBC,
  314. .key_length = 24,
  315. .iv_length = 8,
  316. .is_block = true,
  317. .oid = { 10, {
  318. 0x2a, 0x86, 0x48, 0x86, 0xf7,
  319. 0x0d, 0x01, 0x0c, 0x01, 0x03,
  320. } }
  321. },
  322. { /* pbeWithSHAAnd2-KeyTripleDES-CBC */
  323. .cipher_type = L_CIPHER_DES3_EDE_CBC,
  324. .key_length = 16,
  325. .iv_length = 8,
  326. .copy_k1 = true,
  327. .is_block = true,
  328. .oid = { 10, {
  329. 0x2a, 0x86, 0x48, 0x86, 0xf7,
  330. 0x0d, 0x01, 0x0c, 0x01, 0x04,
  331. } }
  332. },
  333. { /* pbeWithSHAAnd128BitRC2-CBC */
  334. .cipher_type = L_CIPHER_RC2_CBC,
  335. .key_length = 16,
  336. .iv_length = 8,
  337. .is_block = true,
  338. .oid = { 10, {
  339. 0x2a, 0x86, 0x48, 0x86, 0xf7,
  340. 0x0d, 0x01, 0x0c, 0x01, 0x05,
  341. } }
  342. },
  343. { /* pbeWithSHAAnd40BitRC2-CBC */
  344. .cipher_type = L_CIPHER_RC2_CBC,
  345. .key_length = 5,
  346. .iv_length = 8,
  347. .is_block = true,
  348. .oid = { 10, {
  349. 0x2a, 0x86, 0x48, 0x86, 0xf7,
  350. 0x0d, 0x01, 0x0c, 0x01, 0x06,
  351. } }
  352. },
  353. };
  354. static const struct pkcs5_digest_alg_oid {
  355. enum l_checksum_type type;
  356. struct asn1_oid oid;
  357. } pkcs5_digest_alg_oids[] = {
  358. { /* hmacWithSHA1 */
  359. L_CHECKSUM_SHA1,
  360. { 8, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x07 } },
  361. },
  362. { /* hmacWithSHA224 */
  363. L_CHECKSUM_SHA224,
  364. { 8, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x08 } },
  365. },
  366. { /* hmacWithSHA256 */
  367. L_CHECKSUM_SHA256,
  368. { 8, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x09 } },
  369. },
  370. { /* hmacWithSHA384 */
  371. L_CHECKSUM_SHA384,
  372. { 8, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x0a } },
  373. },
  374. { /* hmacWithSHA512 */
  375. L_CHECKSUM_SHA512,
  376. { 8, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x0b } },
  377. },
  378. /* hmacWithSHA512-224 and hmacWithSHA512-256 not supported */
  379. };
  380. static const struct pkcs5_enc_alg_oid {
  381. enum l_cipher_type cipher_type;
  382. uint8_t key_size, iv_size;
  383. struct asn1_oid oid;
  384. } pkcs5_enc_alg_oids[] = {
  385. { /* desCBC */
  386. L_CIPHER_DES_CBC, 8, 8,
  387. { 5, { 0x2b, 0x0e, 0x03, 0x02, 0x07 } },
  388. },
  389. { /* des-EDE3-CBC */
  390. L_CIPHER_DES3_EDE_CBC, 24, 8,
  391. { 8, { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x03, 0x07 } },
  392. },
  393. /* RC2/RC5-based schemes 2 and 9 not supported */
  394. { /* aes128-CBC-PAD */
  395. L_CIPHER_AES_CBC, 16, 16,
  396. { 9, { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x02 } },
  397. },
  398. { /* aes192-CBC-PAD */
  399. L_CIPHER_AES_CBC, 24, 16,
  400. { 9, { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x16 } },
  401. },
  402. { /* aes256-CBC-PAD */
  403. L_CIPHER_AES_CBC, 32, 16,
  404. { 9, { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2a } },
  405. },
  406. };
  407. static struct l_cipher *cipher_from_pkcs5_pbes2_params(
  408. const uint8_t *pbes2_params,
  409. size_t pbes2_params_len,
  410. const char *password)
  411. {
  412. uint8_t tag;
  413. const uint8_t *kdf_sequence, *enc_sequence, *oid, *params,
  414. *salt, *iter_count_buf, *key_len_buf, *prf_sequence;
  415. size_t kdf_len, enc_len, params_len, salt_len, key_len, tmp_len;
  416. unsigned int i, iter_count, pos;
  417. enum l_checksum_type prf_alg = L_CHECKSUM_NONE;
  418. const struct pkcs5_enc_alg_oid *enc_scheme = NULL;
  419. uint8_t derived_key[64];
  420. struct l_cipher *cipher;
  421. /* RFC8018 section A.4 */
  422. kdf_sequence = asn1_der_find_elem(pbes2_params, pbes2_params_len, 0,
  423. &tag, &kdf_len);
  424. if (!kdf_sequence || tag != ASN1_ID_SEQUENCE)
  425. return NULL;
  426. enc_sequence = asn1_der_find_elem(pbes2_params, pbes2_params_len, 1,
  427. &tag, &enc_len);
  428. if (!enc_sequence || tag != ASN1_ID_SEQUENCE)
  429. return NULL;
  430. if (asn1_der_find_elem(pbes2_params, pbes2_params_len, 2,
  431. &tag, &tmp_len))
  432. return NULL;
  433. /* RFC8018 section A.2 */
  434. oid = asn1_der_find_elem(kdf_sequence, kdf_len, 0, &tag, &tmp_len);
  435. if (!oid || tag != ASN1_ID_OID)
  436. return NULL;
  437. if (!asn1_oid_eq(&pkcs5_pbkdf2_oid, tmp_len, oid))
  438. return NULL;
  439. params = asn1_der_find_elem(kdf_sequence, kdf_len, 1,
  440. &tag, &params_len);
  441. if (!params || tag != ASN1_ID_SEQUENCE)
  442. return NULL;
  443. if (asn1_der_find_elem(kdf_sequence, kdf_len, 2, &tag, &tmp_len))
  444. return NULL;
  445. salt = asn1_der_find_elem(params, params_len, 0, &tag, &salt_len);
  446. if (!salt || tag != ASN1_ID_OCTET_STRING ||
  447. salt_len < 1 || salt_len > 512)
  448. return NULL;
  449. iter_count_buf = asn1_der_find_elem(params, params_len, 1,
  450. &tag, &tmp_len);
  451. if (!iter_count_buf || tag != ASN1_ID_INTEGER ||
  452. tmp_len < 1 || tmp_len > 4)
  453. return NULL;
  454. iter_count = 0;
  455. while (tmp_len--)
  456. iter_count = (iter_count << 8) | *iter_count_buf++;
  457. pos = 2;
  458. key_len_buf = asn1_der_find_elem(params, params_len, pos,
  459. &tag, &tmp_len);
  460. if (key_len_buf && tag == ASN1_ID_INTEGER) {
  461. if (tmp_len != 1)
  462. return NULL;
  463. pos++;
  464. key_len = 0;
  465. while (tmp_len--)
  466. key_len = (key_len << 8) | *key_len_buf++;
  467. } else
  468. key_len = 0;
  469. prf_sequence = asn1_der_find_elem(params, params_len, pos,
  470. &tag, &tmp_len);
  471. if (prf_sequence && tag == ASN1_ID_SEQUENCE) {
  472. pos++;
  473. oid = asn1_der_find_elem(prf_sequence, tmp_len, 0,
  474. &tag, &tmp_len);
  475. if (!oid || tag != ASN1_ID_OID)
  476. return NULL;
  477. for (i = 0; i < L_ARRAY_SIZE(pkcs5_digest_alg_oids); i++)
  478. if (asn1_oid_eq(&pkcs5_digest_alg_oids[i].oid,
  479. tmp_len, oid))
  480. prf_alg = pkcs5_digest_alg_oids[i].type;
  481. if (prf_alg == L_CHECKSUM_NONE)
  482. return NULL;
  483. } else
  484. prf_alg = L_CHECKSUM_SHA1;
  485. oid = asn1_der_find_elem(enc_sequence, enc_len, 0, &tag, &tmp_len);
  486. if (!oid || tag != ASN1_ID_OID)
  487. return NULL;
  488. for (i = 0; i < L_ARRAY_SIZE(pkcs5_enc_alg_oids); i++) {
  489. if (asn1_oid_eq(&pkcs5_enc_alg_oids[i].oid, tmp_len, oid)) {
  490. enc_scheme = &pkcs5_enc_alg_oids[i];
  491. break;
  492. }
  493. }
  494. if (!enc_scheme)
  495. return NULL;
  496. params = asn1_der_find_elem(enc_sequence, enc_len, 1,
  497. &tag, &params_len);
  498. if (!params)
  499. return NULL;
  500. /* RFC8018 section B.2 */
  501. /*
  502. * Since we don't support the RC2/RC5 PBES2 ciphers, our parameters
  503. * only have an obligatory OCTET STRING IV parameter and a fixed key
  504. * length.
  505. */
  506. if (tag != ASN1_ID_OCTET_STRING || params_len != enc_scheme->iv_size)
  507. return NULL;
  508. if (key_len && enc_scheme->key_size != key_len)
  509. return NULL;
  510. key_len = enc_scheme->key_size;
  511. if (asn1_der_find_elem(enc_sequence, enc_len, 2, &tag, &tmp_len))
  512. return NULL;
  513. /* RFC8018 section 6.2 */
  514. if (!l_cert_pkcs5_pbkdf2(prf_alg, password, salt, salt_len, iter_count,
  515. derived_key, key_len))
  516. return NULL;
  517. cipher = l_cipher_new(enc_scheme->cipher_type, derived_key, key_len);
  518. if (cipher && !l_cipher_set_iv(cipher, params, enc_scheme->iv_size)) {
  519. l_cipher_free(cipher);
  520. cipher = NULL;
  521. }
  522. explicit_bzero(derived_key, 16);
  523. return cipher;
  524. }
  525. static struct l_cipher *cipher_from_pkcs12_alg_id(
  526. const struct pkcs12_encryption_oid *scheme,
  527. const uint8_t *params, size_t params_len,
  528. const char *password, bool *out_is_block)
  529. {
  530. uint8_t tag;
  531. const uint8_t *salt;
  532. const uint8_t *iterations_data;
  533. size_t salt_len;
  534. size_t iterations_len;
  535. unsigned int iterations;
  536. uint8_t *key;
  537. size_t key_len;
  538. struct l_cipher *cipher;
  539. /* Same parameters as in PKCS#5 */
  540. salt = asn1_der_find_elem(params, params_len, 0, &tag, &salt_len);
  541. if (!salt || tag != ASN1_ID_OCTET_STRING)
  542. return NULL;
  543. iterations_data = asn1_der_find_elem(params, params_len, 1,
  544. &tag, &iterations_len);
  545. if (!iterations_data || tag != ASN1_ID_INTEGER ||
  546. iterations_len < 1 || iterations_len > 4)
  547. return NULL;
  548. for (iterations = 0; iterations_len; iterations_len--)
  549. iterations = (iterations << 8) | *iterations_data++;
  550. if (iterations < 1 || iterations > 8192)
  551. return NULL;
  552. if (iterations_data != params + params_len)
  553. return NULL;
  554. key_len = scheme->key_length;
  555. key = cert_pkcs12_pbkdf(password, &pkcs12_sha1_hash, salt, salt_len,
  556. iterations, 1, key_len);
  557. if (!key)
  558. return NULL;
  559. if (scheme->copy_k1) {
  560. /*
  561. * 2-Key 3DES is like L_CIPHER_DES3_EDE_CBC except the last
  562. * of the 3 8-byte keys is not generated using a KDF and
  563. * instead is a copy of the first key. In other words
  564. * the first half of the 16-byte key material is appended
  565. * at the end to produce the 24 bytes for DES3_EDE_CBC.
  566. */
  567. uint8_t *key2 = l_malloc(24);
  568. memcpy(key2, key, 16);
  569. memcpy(key2 + 16, key, 8);
  570. explicit_bzero(key, key_len);
  571. l_free(key);
  572. key = key2;
  573. key_len = 24;
  574. }
  575. cipher = l_cipher_new(scheme->cipher_type, key, key_len);
  576. explicit_bzero(key, key_len);
  577. l_free(key);
  578. if (!cipher)
  579. return NULL;
  580. if (scheme->iv_length) {
  581. uint8_t *iv = cert_pkcs12_pbkdf(password, &pkcs12_sha1_hash,
  582. salt, salt_len, iterations, 2,
  583. scheme->iv_length);
  584. if (!iv || !l_cipher_set_iv(cipher, iv, scheme->iv_length)) {
  585. l_cipher_free(cipher);
  586. cipher = NULL;
  587. }
  588. if (iv)
  589. explicit_bzero(iv, scheme->iv_length);
  590. l_free(iv);
  591. }
  592. if (out_is_block)
  593. *out_is_block = scheme->is_block;
  594. return cipher;
  595. }
  596. struct l_cipher *cert_cipher_from_pkcs_alg_id(const uint8_t *id_asn1,
  597. size_t id_asn1_len,
  598. const char *password,
  599. bool *out_is_block)
  600. {
  601. uint8_t tag;
  602. const uint8_t *oid, *params, *salt, *iter_count_buf;
  603. size_t oid_len, params_len, tmp_len;
  604. unsigned int i, iter_count;
  605. const struct pkcs5_pbes1_encryption_oid *pbes1_scheme = NULL;
  606. uint8_t derived_key[16];
  607. struct l_cipher *cipher;
  608. oid = asn1_der_find_elem(id_asn1, id_asn1_len, 0, &tag, &oid_len);
  609. if (!oid || tag != ASN1_ID_OID)
  610. return NULL;
  611. params = asn1_der_find_elem(id_asn1, id_asn1_len, 1, &tag, &params_len);
  612. if (!params || tag != ASN1_ID_SEQUENCE)
  613. return NULL;
  614. if (asn1_der_find_elem(id_asn1, id_asn1_len, 2, &tag, &tmp_len))
  615. return NULL;
  616. if (asn1_oid_eq(&pkcs5_pbes2_oid, oid_len, oid)) {
  617. if (out_is_block)
  618. *out_is_block = true;
  619. return cipher_from_pkcs5_pbes2_params(params, params_len,
  620. password);
  621. }
  622. /* RFC8018 section A.3 */
  623. for (i = 0; i < L_ARRAY_SIZE(pkcs5_pbes1_encryption_oids); i++) {
  624. if (asn1_oid_eq(&pkcs5_pbes1_encryption_oids[i].oid,
  625. oid_len, oid)) {
  626. pbes1_scheme = &pkcs5_pbes1_encryption_oids[i];
  627. break;
  628. }
  629. }
  630. /* Check if this is a PKCS#12 OID */
  631. if (!pbes1_scheme) {
  632. for (i = 0; i < L_ARRAY_SIZE(pkcs12_encryption_oids); i++)
  633. if (asn1_oid_eq(&pkcs12_encryption_oids[i].oid,
  634. oid_len, oid))
  635. return cipher_from_pkcs12_alg_id(
  636. &pkcs12_encryption_oids[i],
  637. params, params_len, password,
  638. out_is_block);
  639. return NULL;
  640. }
  641. salt = asn1_der_find_elem(params, params_len, 0, &tag, &tmp_len);
  642. if (!salt || tag != ASN1_ID_OCTET_STRING || tmp_len != 8)
  643. return NULL;
  644. iter_count_buf = asn1_der_find_elem(params, params_len, 1,
  645. &tag, &tmp_len);
  646. if (!iter_count_buf || tag != ASN1_ID_INTEGER ||
  647. tmp_len < 1 || tmp_len > 4)
  648. return NULL;
  649. iter_count = 0;
  650. while (tmp_len--)
  651. iter_count = (iter_count << 8) | *iter_count_buf++;
  652. if (asn1_der_find_elem(params, params_len, 2, &tag, &tmp_len))
  653. return NULL;
  654. /* RFC8018 section 6.1 */
  655. if (!l_cert_pkcs5_pbkdf1(pbes1_scheme->checksum_type, password,
  656. salt, 8, iter_count, derived_key, 16))
  657. return NULL;
  658. cipher = l_cipher_new(pbes1_scheme->cipher_type, derived_key + 0, 8);
  659. if (cipher && !l_cipher_set_iv(cipher, derived_key + 8, 8)) {
  660. l_cipher_free(cipher);
  661. cipher = NULL;
  662. }
  663. explicit_bzero(derived_key, 16);
  664. if (out_is_block)
  665. *out_is_block = true;
  666. return cipher;
  667. }