aescbch.c 11 KB

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