aescbch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include "misc.h"
  18. #include <jose/b64.h>
  19. #include "../hooks.h"
  20. #include <openssl/rand.h>
  21. #include <openssl/sha.h>
  22. #include <string.h>
  23. #define NAMES "A128CBC-HS256", "A192CBC-HS384", "A256CBC-HS512"
  24. typedef struct {
  25. jose_io_t io;
  26. EVP_CIPHER_CTX *cctx;
  27. jose_io_t *next;
  28. HMAC_CTX *hctx;
  29. json_t *json;
  30. uint64_t al;
  31. } io_t;
  32. static uint64_t
  33. h2be64(uint64_t x)
  34. {
  35. union swap {
  36. uint64_t i;
  37. uint8_t b[8];
  38. } y;
  39. y.b[0] = x >> 0x38;
  40. y.b[1] = x >> 0x30;
  41. y.b[2] = x >> 0x28;
  42. y.b[3] = x >> 0x20;
  43. y.b[4] = x >> 0x18;
  44. y.b[5] = x >> 0x10;
  45. y.b[6] = x >> 0x08;
  46. y.b[7] = x >> 0x00;
  47. return y.i;
  48. }
  49. static json_int_t
  50. alg2len(const char *alg)
  51. {
  52. switch (str2enum(alg, NAMES, NULL)) {
  53. case 0: return 32;
  54. case 1: return 48;
  55. case 2: return 64;
  56. default: return 0;
  57. }
  58. }
  59. static bool
  60. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  61. {
  62. const char *alg = NULL;
  63. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  64. return false;
  65. return alg2len(alg) != 0;
  66. }
  67. static bool
  68. jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
  69. {
  70. const char *alg = NULL;
  71. const char *kty = NULL;
  72. json_int_t byt = 0;
  73. json_int_t len = 0;
  74. if (json_unpack(jwk, "{s:s,s?s,s?I}",
  75. "alg", &alg, "kty", &kty, "bytes", &byt) == -1)
  76. return false;
  77. len = alg2len(alg);
  78. if (len == 0)
  79. return false;
  80. if (byt != 0 && len != byt)
  81. return false;
  82. if (kty && strcmp(kty, "oct") != 0)
  83. return false;
  84. if (json_object_set_new(jwk, "kty", json_string("oct")) < 0)
  85. return false;
  86. if (json_object_set_new(jwk, "bytes", json_integer(len)) < 0)
  87. return false;
  88. return true;
  89. }
  90. static const char *
  91. alg_encr_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *cek)
  92. {
  93. const char *name = NULL;
  94. const char *type = NULL;
  95. size_t len = 0;
  96. if (json_unpack((json_t *) cek, "{s?s,s?s}",
  97. "alg", &name, "kty", &type) < 0)
  98. return NULL;
  99. if (name)
  100. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  101. if (!type || strcmp(type, "oct") != 0)
  102. return NULL;
  103. len = jose_b64_dec(json_object_get(cek, "k"), NULL, 0);
  104. if (len >= SHA512_DIGEST_LENGTH)
  105. return "A256CBC-HS512";
  106. else if (len >= SHA384_DIGEST_LENGTH)
  107. return "A192CBC-HS384";
  108. else if (len >= SHA256_DIGEST_LENGTH)
  109. return "A128CBC-HS256";
  110. return NULL;
  111. }
  112. static void
  113. io_free(jose_io_t *io)
  114. {
  115. io_t *i = containerof(io, io_t, io);
  116. EVP_CIPHER_CTX_free(i->cctx);
  117. jose_io_decref(i->next);
  118. HMAC_CTX_free(i->hctx);
  119. json_decref(i->json);
  120. free(i);
  121. }
  122. static bool
  123. enc_feed(jose_io_t *io, const void *in, size_t len)
  124. {
  125. io_t *i = containerof(io, io_t, io);
  126. uint8_t ct[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  127. const uint8_t *pt = in;
  128. for (size_t j = 0; j < len; j++) {
  129. int l = 0;
  130. if (EVP_EncryptUpdate(i->cctx, ct, &l, &pt[j], 1) <= 0)
  131. return false;
  132. if (!i->next->feed(i->next, ct, l))
  133. return false;
  134. if (HMAC_Update(i->hctx, ct, l) <= 0)
  135. return false;
  136. }
  137. return true;
  138. }
  139. static bool
  140. enc_done(jose_io_t *io)
  141. {
  142. io_t *i = containerof(io, io_t, io);
  143. uint8_t ct[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  144. uint8_t tg[EVP_MD_size(HMAC_CTX_get_md(i->hctx))];
  145. int l = 0;
  146. if (EVP_EncryptFinal(i->cctx, ct, &l) <= 0)
  147. return false;
  148. if (!i->next->feed(i->next, ct, l) || !i->next->done(i->next))
  149. return false;
  150. if (HMAC_Update(i->hctx, ct, l) <= 0)
  151. return false;
  152. if (HMAC_Update(i->hctx, (void *) &i->al, sizeof(i->al)) <= 0)
  153. return false;
  154. if (HMAC_Final(i->hctx, tg, NULL) <= 0)
  155. return false;
  156. if (json_object_set_new(i->json, "tag",
  157. jose_b64_enc(tg, sizeof(tg) / 2)) < 0)
  158. return false;
  159. return true;
  160. }
  161. static bool
  162. dec_feed(jose_io_t *io, const void *in, size_t len)
  163. {
  164. io_t *i = containerof(io, io_t, io);
  165. uint8_t pt[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  166. const uint8_t *ct = in;
  167. bool ret = false;
  168. int l = 0;
  169. if (HMAC_Update(i->hctx, in, len) <= 0)
  170. return false;
  171. for (size_t j = 0; j < len; j++) {
  172. if (EVP_DecryptUpdate(i->cctx, pt, &l, &ct[j], 1) <= 0)
  173. goto egress;
  174. if (!i->next->feed(i->next, pt, l))
  175. goto egress;
  176. }
  177. ret = true;
  178. egress:
  179. OPENSSL_cleanse(pt, sizeof(pt));
  180. return ret;
  181. }
  182. static bool
  183. dec_done(jose_io_t *io)
  184. {
  185. io_t *i = containerof(io, io_t, io);
  186. uint8_t pt[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  187. uint8_t tg[EVP_MD_size(HMAC_CTX_get_md(i->hctx))];
  188. uint8_t bf[sizeof(tg) / 2];
  189. json_t *tag = NULL;
  190. int l = 0;
  191. tag = json_object_get(i->json, "tag");
  192. if (!tag)
  193. return false;
  194. if (jose_b64_dec(tag, NULL, 0) != sizeof(bf))
  195. return false;
  196. if (jose_b64_dec(tag, bf, sizeof(bf)) != sizeof(bf))
  197. return false;
  198. if (HMAC_Update(i->hctx, (void *) &i->al, sizeof(i->al)) <= 0)
  199. return false;
  200. if (HMAC_Final(i->hctx, tg, NULL) <= 0)
  201. return false;
  202. if (CRYPTO_memcmp(tg, bf, sizeof(bf)) != 0)
  203. return false;
  204. if (EVP_DecryptFinal(i->cctx, pt, &l) <= 0)
  205. return false;
  206. if (!i->next->feed(i->next, pt, l) || !i->next->done(i->next)) {
  207. OPENSSL_cleanse(pt, sizeof(pt));
  208. return false;
  209. }
  210. OPENSSL_cleanse(pt, sizeof(pt));
  211. return true;
  212. }
  213. static bool
  214. setup(const EVP_CIPHER *cph, const EVP_MD *md, jose_cfg_t *cfg,
  215. const json_t *jwe, const json_t *cek, uint8_t *iv,
  216. typeof(EVP_EncryptInit) func, io_t *i)
  217. {
  218. uint8_t key[EVP_CIPHER_key_length(cph) * 2];
  219. const char *aad = NULL;
  220. const char *prt = "";
  221. if (jose_b64_dec(json_object_get(cek, "k"), NULL, 0) != sizeof(key))
  222. return false;
  223. if (json_unpack((json_t *) jwe, "{s?s,s?s}",
  224. "aad", &aad, "protected", &prt) < 0)
  225. return false;
  226. i->cctx = EVP_CIPHER_CTX_new();
  227. if (!i->cctx)
  228. return false;
  229. i->hctx = HMAC_CTX_new();
  230. if (!i->hctx)
  231. return false;
  232. if (jose_b64_dec(json_object_get(cek, "k"), NULL, 0) != sizeof(key))
  233. return false;
  234. if (jose_b64_dec(json_object_get(cek, "k"), key,
  235. sizeof(key)) != sizeof(key)) {
  236. OPENSSL_cleanse(key, sizeof(key));
  237. return false;
  238. }
  239. if (HMAC_Init_ex(i->hctx, key, sizeof(key) / 2, md, NULL) <= 0) {
  240. OPENSSL_cleanse(key, sizeof(key));
  241. return false;
  242. }
  243. if (func(i->cctx, cph, &key[sizeof(key) / 2], iv) <= 0) {
  244. OPENSSL_cleanse(key, sizeof(key));
  245. return false;
  246. }
  247. OPENSSL_cleanse(key, sizeof(key));
  248. i->al += strlen(prt);
  249. if (HMAC_Update(i->hctx, (void *) prt, strlen(prt)) <= 0)
  250. return false;
  251. if (aad) {
  252. i->al += 1;
  253. if (HMAC_Update(i->hctx, (void *) ".", 1) <= 0)
  254. return false;
  255. i->al += strlen(aad);
  256. if (HMAC_Update(i->hctx, (void *) aad, strlen(aad)) <= 0)
  257. return false;
  258. }
  259. i->al = h2be64(i->al * 8);
  260. if (HMAC_Update(i->hctx, iv, EVP_CIPHER_iv_length(cph)) <= 0)
  261. return false;
  262. return true;
  263. }
  264. static jose_io_t *
  265. alg_encr_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  266. const json_t *cek, jose_io_t *next)
  267. {
  268. const EVP_CIPHER *cph = NULL;
  269. jose_io_auto_t *io = NULL;
  270. const EVP_MD *md = NULL;
  271. io_t *i = NULL;
  272. switch (str2enum(alg->name, NAMES, NULL)) {
  273. case 0: cph = EVP_aes_128_cbc(); md = EVP_sha256(); break;
  274. case 1: cph = EVP_aes_192_cbc(); md = EVP_sha384(); break;
  275. case 2: cph = EVP_aes_256_cbc(); md = EVP_sha512(); break;
  276. default: return NULL;
  277. }
  278. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  279. if (RAND_bytes(iv, sizeof(iv)) <= 0)
  280. return NULL;
  281. i = calloc(1, sizeof(*i));
  282. if (!i)
  283. return NULL;
  284. io = jose_io_incref(&i->io);
  285. io->feed = enc_feed;
  286. io->done = enc_done;
  287. io->free = io_free;
  288. i->json = json_incref(jwe);
  289. i->next = jose_io_incref(next);
  290. if (!i->json || !i->next)
  291. return NULL;
  292. if (!setup(cph, md, cfg, jwe, cek, iv, EVP_EncryptInit, i))
  293. return NULL;
  294. if (json_object_set_new(jwe, "iv", jose_b64_enc(iv, sizeof(iv))) < 0)
  295. return NULL;
  296. return jose_io_incref(io);
  297. }
  298. static jose_io_t *
  299. alg_encr_dec(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  300. const json_t *cek, jose_io_t *next)
  301. {
  302. const EVP_CIPHER *cph = NULL;
  303. jose_io_auto_t *io = NULL;
  304. const EVP_MD *md = NULL;
  305. io_t *i = NULL;
  306. switch (str2enum(alg->name, NAMES, NULL)) {
  307. case 0: cph = EVP_aes_128_cbc(); md = EVP_sha256(); break;
  308. case 1: cph = EVP_aes_192_cbc(); md = EVP_sha384(); break;
  309. case 2: cph = EVP_aes_256_cbc(); md = EVP_sha512(); break;
  310. default: return NULL;
  311. }
  312. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  313. if (jose_b64_dec(json_object_get(jwe, "iv"), NULL, 0) != sizeof(iv))
  314. return NULL;
  315. if (jose_b64_dec(json_object_get(jwe, "iv"), iv, sizeof(iv)) != sizeof(iv))
  316. return NULL;
  317. i = calloc(1, sizeof(*i));
  318. if (!i)
  319. return NULL;
  320. io = jose_io_incref(&i->io);
  321. io->feed = dec_feed;
  322. io->done = dec_done;
  323. io->free = io_free;
  324. i->json = json_incref((json_t *) jwe);
  325. i->next = jose_io_incref(next);
  326. if (!i->json || !i->next)
  327. return NULL;
  328. if (!setup(cph, md, cfg, jwe, cek, iv, EVP_DecryptInit, i))
  329. return NULL;
  330. return jose_io_incref(io);
  331. }
  332. static void __attribute__((constructor))
  333. constructor(void)
  334. {
  335. static jose_hook_jwk_t jwk = {
  336. .kind = JOSE_HOOK_JWK_KIND_PREP,
  337. .prep.handles = jwk_prep_handles,
  338. .prep.execute = jwk_prep_execute,
  339. };
  340. static jose_hook_alg_t algs[] = {
  341. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  342. .name = "A128CBC-HS256",
  343. .encr.eprm = "encrypt",
  344. .encr.dprm = "decrypt",
  345. .encr.sug = alg_encr_sug,
  346. .encr.enc = alg_encr_enc,
  347. .encr.dec = alg_encr_dec },
  348. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  349. .name = "A192CBC-HS384",
  350. .encr.eprm = "encrypt",
  351. .encr.dprm = "decrypt",
  352. .encr.sug = alg_encr_sug,
  353. .encr.enc = alg_encr_enc,
  354. .encr.dec = alg_encr_dec },
  355. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  356. .name = "A256CBC-HS512",
  357. .encr.eprm = "encrypt",
  358. .encr.dprm = "decrypt",
  359. .encr.sug = alg_encr_sug,
  360. .encr.enc = alg_encr_enc,
  361. .encr.dec = alg_encr_dec },
  362. {}
  363. };
  364. jose_hook_jwk_push(&jwk);
  365. for (size_t i = 0; algs[i].name; i++)
  366. jose_hook_alg_push(&algs[i]);
  367. }