ecdhes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #define _GNU_SOURCE
  18. #include "misc.h"
  19. #include <jose/b64.h>
  20. #include <jose/jwk.h>
  21. #include "../hooks.h"
  22. #include <jose/openssl.h>
  23. #include <openssl/rand.h>
  24. #include <string.h>
  25. #define NAMES "ECDH-ES", "ECDH-ES+A128KW", "ECDH-ES+A192KW", "ECDH-ES+A256KW"
  26. #include <assert.h>
  27. static uint32_t
  28. h2be32(uint32_t x)
  29. {
  30. union swap {
  31. uint32_t i;
  32. uint8_t b[8];
  33. } y;
  34. y.b[0] = x >> 0x18;
  35. y.b[1] = x >> 0x10;
  36. y.b[2] = x >> 0x08;
  37. y.b[3] = x >> 0x00;
  38. return y.i;
  39. }
  40. static bool
  41. concatkdf(const jose_hook_alg_t *alg, jose_cfg_t *cfg, uint8_t dk[], size_t dkl,
  42. const uint8_t z[], size_t zl, ...)
  43. {
  44. jose_io_auto_t *b = NULL;
  45. uint8_t hsh[alg->hash.size];
  46. size_t hshl = sizeof(hsh);
  47. size_t reps = 0;
  48. size_t left = 0;
  49. reps = dkl / sizeof(hsh);
  50. left = dkl % sizeof(hsh);
  51. b = jose_io_buffer(cfg, &hsh, &hshl);
  52. if (!b)
  53. return false;
  54. for (uint32_t c = 0; c <= reps; c++) {
  55. uint32_t cnt = h2be32(c + 1);
  56. jose_io_auto_t *h = NULL;
  57. va_list ap;
  58. h = alg->hash.hsh(alg, cfg, b);
  59. if (!h)
  60. return false;
  61. if (!h->feed(h, &cnt, sizeof(cnt)))
  62. return false;
  63. if (!h->feed(h, z, zl))
  64. return false;
  65. va_start(ap, zl);
  66. for (void *a = va_arg(ap, void *); a; a = va_arg(ap, void *)) {
  67. size_t l = va_arg(ap, size_t);
  68. uint32_t e = h2be32(l);
  69. if (!h->feed(h, &e, sizeof(e))) {
  70. va_end(ap);
  71. return false;
  72. }
  73. if (!h->feed(h, a, l)) {
  74. va_end(ap);
  75. return false;
  76. }
  77. }
  78. va_end(ap);
  79. if (!h->feed(h, &(uint32_t) { h2be32(dkl * 8) }, 4))
  80. return false;
  81. if (!h->done(h))
  82. return false;
  83. assert(hshl == alg->hash.size);
  84. memcpy(&dk[c * hshl], hsh, c == reps ? left : hshl);
  85. OPENSSL_cleanse(hsh, sizeof(hsh));
  86. hshl = 0;
  87. }
  88. return true;
  89. }
  90. static size_t
  91. encr_alg_keylen(jose_cfg_t *cfg, const char *enc)
  92. {
  93. json_auto_t *tmpl = NULL;
  94. if (!jose_hook_alg_find(JOSE_HOOK_ALG_KIND_ENCR, enc))
  95. return SIZE_MAX;
  96. tmpl = json_pack("{s:s}", "alg", enc);
  97. if (!tmpl)
  98. return SIZE_MAX;
  99. for (const jose_hook_jwk_t *j = jose_hook_jwk_list(); j; j = j->next) {
  100. json_auto_t *upd = NULL;
  101. const char *kty = NULL;
  102. json_int_t len = 0;
  103. if (j->kind != JOSE_HOOK_JWK_KIND_PREP)
  104. continue;
  105. if (!j->prep.handles(cfg, tmpl))
  106. continue;
  107. upd = j->prep.execute(cfg, tmpl);
  108. if (json_unpack(upd, "{s:{s:s,s:I}}",
  109. "upd", "kty", &kty, "bytes", &len) < 0)
  110. return SIZE_MAX;
  111. if (strcmp(kty, "oct") != 0)
  112. return SIZE_MAX;
  113. return len;
  114. }
  115. return SIZE_MAX;
  116. }
  117. static size_t
  118. decode(const json_t *obj, const char *name, uint8_t *buf, size_t len)
  119. {
  120. const char *tmp = NULL;
  121. size_t tmpl = 0;
  122. size_t dlen = 0;
  123. if (json_unpack((json_t *) obj, "{s?s%}", name, &tmp, &tmpl) < 0)
  124. return SIZE_MAX;
  125. if (!tmp)
  126. return 0;
  127. dlen = jose_b64_dec_buf(tmp, tmpl, NULL, 0);
  128. if (dlen > len)
  129. return dlen;
  130. return jose_b64_dec_buf(tmp, tmpl, buf, len);
  131. }
  132. static json_t *
  133. derive(const jose_hook_alg_t *alg, jose_cfg_t *cfg,
  134. json_t *hdr, json_t *cek, const json_t *key)
  135. {
  136. const jose_hook_alg_t *halg = NULL;
  137. const char *name = alg->name;
  138. uint8_t pu[KEYMAX] = {};
  139. uint8_t pv[KEYMAX] = {};
  140. uint8_t dk[KEYMAX] = {};
  141. uint8_t ky[KEYMAX] = {};
  142. const char *enc = NULL;
  143. json_t *out = NULL;
  144. size_t dkl = 0;
  145. size_t pul = 0;
  146. size_t pvl = 0;
  147. size_t kyl = 0;
  148. halg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_HASH, "S256");
  149. if (!halg)
  150. goto egress;
  151. if (json_unpack(hdr, "{s?s}", "enc", &enc) < 0)
  152. goto egress;
  153. if (!enc && json_unpack(cek, "{s:s}", "alg", &enc) < 0)
  154. goto egress;
  155. switch (str2enum(alg->name, NAMES, NULL)) {
  156. case 0: dkl = encr_alg_keylen(cfg, enc); name = enc; break;
  157. case 1: dkl = 16; break;
  158. case 2: dkl = 24; break;
  159. case 3: dkl = 32; break;
  160. default:
  161. goto egress;
  162. }
  163. if (dkl < 16 || dkl > sizeof(dk))
  164. goto egress;
  165. pul = decode(hdr, "apu", pu, sizeof(pu));
  166. if (pul > sizeof(pu))
  167. goto egress;
  168. pvl = decode(hdr, "apv", pv, sizeof(pv));
  169. if (pvl > sizeof(pv))
  170. goto egress;
  171. kyl = decode(key, "x", ky, sizeof(ky));
  172. if (kyl > sizeof(ky))
  173. goto egress;
  174. if (!concatkdf(halg, cfg,
  175. dk, dkl,
  176. ky, kyl,
  177. name, strlen(name),
  178. pu, pul,
  179. pv, pvl,
  180. NULL))
  181. goto egress;
  182. out = json_pack("{s:s,s:s,s:o}", "kty", "oct", "alg", enc,
  183. "k", jose_b64_enc(dk, dkl));
  184. egress:
  185. OPENSSL_cleanse(ky, sizeof(ky));
  186. OPENSSL_cleanse(pu, sizeof(pu));
  187. OPENSSL_cleanse(pv, sizeof(pv));
  188. OPENSSL_cleanse(dk, sizeof(dk));
  189. return out;
  190. }
  191. static bool
  192. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  193. {
  194. const char *alg = NULL;
  195. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  196. return false;
  197. return str2enum(alg, NAMES, NULL) != SIZE_MAX;
  198. }
  199. static json_t *
  200. jwk_prep_execute(jose_cfg_t *cfg, const json_t *jwk)
  201. {
  202. const char *alg = NULL;
  203. const char *grp = NULL;
  204. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  205. return NULL;
  206. switch (str2enum(alg, NAMES, NULL)) {
  207. case 0: grp = "P-521"; break;
  208. case 1: grp = "P-256"; break;
  209. case 2: grp = "P-384"; break;
  210. case 3: grp = "P-521"; break;
  211. default: return NULL;
  212. }
  213. return json_pack("{s:{s:s,s:s}}", "upd", "kty", "EC", "crv", grp);
  214. }
  215. static const char *
  216. alg_wrap_alg(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  217. {
  218. const char *name = NULL;
  219. const char *type = NULL;
  220. const char *curv = NULL;
  221. if (json_unpack((json_t *) jwk, "{s?s,s?s,s?s}",
  222. "alg", &name, "kty", &type, "crv", &curv) < 0)
  223. return NULL;
  224. if (name)
  225. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  226. if (!type || strcmp(type, "EC") != 0)
  227. return NULL;
  228. switch (str2enum(curv, "P-256", "P-384", "P-521", NULL)) {
  229. case 0: return "ECDH-ES+A128KW";
  230. case 1: return "ECDH-ES+A192KW";
  231. case 2: return "ECDH-ES+A256KW";
  232. default: return NULL;
  233. }
  234. }
  235. static const char *
  236. alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  237. {
  238. const char *crv = NULL;
  239. if (json_unpack((json_t *) jwk, "{s?s}", "crv", &crv) < 0)
  240. return NULL;
  241. switch (str2enum(crv, "P-256", "P-384", "P-521", NULL)) {
  242. case 0: return "A128CBC-HS256";
  243. case 1: return "A192CBC-HS384";
  244. case 2: return "A256CBC-HS512";
  245. default: return NULL;
  246. }
  247. }
  248. static bool
  249. alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  250. json_t *rcp, const json_t *jwk, json_t *cek)
  251. {
  252. const jose_hook_alg_t *ecdh = NULL;
  253. json_auto_t *exc = NULL;
  254. json_auto_t *hdr = NULL;
  255. json_auto_t *epk = NULL;
  256. json_auto_t *der = NULL;
  257. const char *wrap = NULL;
  258. json_t *h = NULL;
  259. if (json_object_get(cek, "k")) {
  260. if (strcmp(alg->name, "ECDH-ES") == 0)
  261. return false;
  262. } else if (!jose_jwk_gen(cfg, cek)) {
  263. return false;
  264. }
  265. hdr = jose_jwe_hdr(jwe, rcp);
  266. if (!hdr)
  267. return false;
  268. h = json_object_get(rcp, "header");
  269. if (!h && json_object_set_new(rcp, "header", h = json_object()) == -1)
  270. return false;
  271. epk = json_pack("{s:s,s:O}", "kty", "EC", "crv",
  272. json_object_get(jwk, "crv"));
  273. if (!epk)
  274. return false;
  275. if (!jose_jwk_gen(cfg, epk))
  276. return false;
  277. ecdh = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_EXCH, "ECDH");
  278. if (!ecdh)
  279. return false;
  280. exc = ecdh->exch.exc(ecdh, cfg, epk, jwk);
  281. if (!exc)
  282. return false;
  283. if (!jose_jwk_pub(cfg, epk))
  284. return false;
  285. if (json_object_set(h, "epk", epk) == -1)
  286. return false;
  287. der = derive(alg, cfg, hdr, cek, exc);
  288. if (!der)
  289. return false;
  290. wrap = strchr(alg->name, '+');
  291. if (wrap) {
  292. const jose_hook_alg_t *kw = NULL;
  293. kw = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_WRAP, &wrap[1]);
  294. if (!kw)
  295. return false;
  296. return kw->wrap.wrp(kw, cfg, jwe, rcp, der, cek);
  297. }
  298. if (json_object_update(cek, der) < 0)
  299. return false;
  300. return add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
  301. }
  302. static bool
  303. alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  304. const json_t *rcp, const json_t *jwk, json_t *cek)
  305. {
  306. const json_t *epk = NULL;
  307. json_auto_t *exc = NULL;
  308. json_auto_t *der = NULL;
  309. json_auto_t *hdr = NULL;
  310. const char *wrap = NULL;
  311. hdr = jose_jwe_hdr(jwe, rcp);
  312. epk = json_object_get(hdr, "epk");
  313. if (!hdr || !epk)
  314. return false;
  315. /* If the JWK has a private key, perform the normal exchange. */
  316. if (json_object_get(jwk, "d")) {
  317. const jose_hook_alg_t *ecdh = NULL;
  318. ecdh = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_EXCH, "ECDH");
  319. if (!ecdh)
  320. return false;
  321. exc = ecdh->exch.exc(ecdh, cfg, jwk, epk);
  322. /* Otherwise, allow external exchanges. */
  323. } else if (json_equal(json_object_get(jwk, "crv"),
  324. json_object_get(epk, "crv"))) {
  325. exc = json_deep_copy(jwk);
  326. }
  327. if (!exc)
  328. return false;
  329. der = derive(alg, cfg, hdr, cek, exc);
  330. if (!der)
  331. return false;
  332. wrap = strchr(alg->name, '+');
  333. if (wrap) {
  334. const jose_hook_alg_t *kw = NULL;
  335. kw = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_WRAP, &wrap[1]);
  336. if (!kw)
  337. return false;
  338. return kw->wrap.unw(kw, cfg, jwe, rcp, der, cek);
  339. }
  340. return json_object_update(cek, der) == 0;
  341. }
  342. static void __attribute__((constructor))
  343. constructor(void)
  344. {
  345. static jose_hook_jwk_t jwk = {
  346. .kind = JOSE_HOOK_JWK_KIND_PREP,
  347. .prep.handles = jwk_prep_handles,
  348. .prep.execute = jwk_prep_execute
  349. };
  350. static jose_hook_alg_t algs[] = {
  351. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  352. .name = "ECDH-ES",
  353. .wrap.eprm = "wrapKey",
  354. .wrap.dprm = "unwrapKey",
  355. .wrap.alg = alg_wrap_alg,
  356. .wrap.enc = alg_wrap_enc,
  357. .wrap.wrp = alg_wrap_wrp,
  358. .wrap.unw = alg_wrap_unw },
  359. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  360. .name = "ECDH-ES+A128KW",
  361. .wrap.eprm = "wrapKey",
  362. .wrap.dprm = "unwrapKey",
  363. .wrap.alg = alg_wrap_alg,
  364. .wrap.enc = alg_wrap_enc,
  365. .wrap.wrp = alg_wrap_wrp,
  366. .wrap.unw = alg_wrap_unw },
  367. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  368. .name = "ECDH-ES+A192KW",
  369. .wrap.eprm = "wrapKey",
  370. .wrap.dprm = "unwrapKey",
  371. .wrap.alg = alg_wrap_alg,
  372. .wrap.enc = alg_wrap_enc,
  373. .wrap.wrp = alg_wrap_wrp,
  374. .wrap.unw = alg_wrap_unw },
  375. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  376. .name = "ECDH-ES+A256KW",
  377. .wrap.eprm = "wrapKey",
  378. .wrap.dprm = "unwrapKey",
  379. .wrap.alg = alg_wrap_alg,
  380. .wrap.enc = alg_wrap_enc,
  381. .wrap.wrp = alg_wrap_wrp,
  382. .wrap.unw = alg_wrap_unw },
  383. {}
  384. };
  385. jose_hook_jwk_push(&jwk);
  386. for (size_t i = 0; algs[i].name; i++)
  387. jose_hook_alg_push(&algs[i]);
  388. }