1
0

aesgcm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 <string.h>
  22. #define NAMES "A128GCM", "A192GCM", "A256GCM"
  23. typedef struct {
  24. jose_io_t io;
  25. EVP_CIPHER_CTX *cctx;
  26. jose_io_t *next;
  27. json_t *json;
  28. } io_t;
  29. static EVP_CIPHER_CTX *
  30. setup(const EVP_CIPHER *cph, jose_cfg_t *cfg, const json_t *jwe,
  31. const json_t *cek, const uint8_t iv[],
  32. typeof(EVP_EncryptInit_ex) *init, typeof(EVP_EncryptUpdate) *push)
  33. {
  34. uint8_t key[EVP_CIPHER_key_length(cph)];
  35. EVP_CIPHER_CTX *ecc = NULL;
  36. const char *aad = NULL;
  37. const char *prt = NULL;
  38. size_t aadl = 0;
  39. size_t prtl = 0;
  40. int tmp;
  41. if (json_unpack((json_t *) jwe, "{s?s%,s?s%}",
  42. "aad", &aad, &aadl, "protected", &prt, &prtl) < 0)
  43. goto error;
  44. ecc = EVP_CIPHER_CTX_new();
  45. if (!ecc)
  46. return NULL;
  47. if (init(ecc, cph, NULL, NULL, NULL) <= 0)
  48. goto error;
  49. if (jose_b64_dec(json_object_get(cek, "k"), NULL, 0) != sizeof(key))
  50. goto error;
  51. if (jose_b64_dec(json_object_get(cek, "k"), key,
  52. sizeof(key)) != sizeof(key)) {
  53. OPENSSL_cleanse(key, sizeof(key));
  54. goto error;
  55. }
  56. tmp = init(ecc, NULL, NULL, key, iv);
  57. OPENSSL_cleanse(key, sizeof(key));
  58. if (tmp <= 0)
  59. goto error;
  60. if (prt && push(ecc, NULL, &tmp, (uint8_t *) prt, prtl) <= 0)
  61. goto error;
  62. if (aad) {
  63. if (push(ecc, NULL, &tmp, (uint8_t *) ".", 1) <= 0)
  64. goto error;
  65. if (push(ecc, NULL, &tmp, (uint8_t *) aad, prtl) <= 0)
  66. goto error;
  67. }
  68. return ecc;
  69. error:
  70. EVP_CIPHER_CTX_free(ecc);
  71. return NULL;
  72. }
  73. static void
  74. io_free(jose_io_t *io)
  75. {
  76. io_t *i = containerof(io, io_t, io);
  77. EVP_CIPHER_CTX_free(i->cctx);
  78. jose_io_decref(i->next);
  79. json_decref(i->json);
  80. free(i);
  81. }
  82. static bool
  83. enc_feed(jose_io_t *io, const void *in, size_t len)
  84. {
  85. io_t *i = containerof(io, io_t, io);
  86. const uint8_t *pt = in;
  87. int l = 0;
  88. for (size_t j = 0; j < len; j++) {
  89. uint8_t ct[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  90. if (EVP_EncryptUpdate(i->cctx, ct, &l, &pt[j], 1) <= 0)
  91. return false;
  92. if (!i->next->feed(i->next, ct, l))
  93. return false;
  94. }
  95. return true;
  96. }
  97. static bool
  98. enc_done(jose_io_t *io)
  99. {
  100. io_t *i = containerof(io, io_t, io);
  101. uint8_t ct[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  102. uint8_t tg[EVP_GCM_TLS_TAG_LEN] = {};
  103. int l = 0;
  104. if (EVP_EncryptFinal(i->cctx, ct, &l) <= 0)
  105. return false;
  106. if (!i->next->feed(i->next, ct, l) || !i->next->done(i->next))
  107. return false;
  108. if (EVP_CIPHER_CTX_ctrl(i->cctx, EVP_CTRL_GCM_GET_TAG, sizeof(tg), tg) <= 0)
  109. return false;
  110. if (json_object_set_new(i->json, "tag",
  111. jose_b64_enc(tg, sizeof(tg))) < 0)
  112. return false;
  113. return true;
  114. }
  115. static bool
  116. dec_feed(jose_io_t *io, const void *in, size_t len)
  117. {
  118. io_t *i = containerof(io, io_t, io);
  119. uint8_t pt[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  120. const uint8_t *ct = in;
  121. bool ret = false;
  122. int l = 0;
  123. for (size_t j = 0; j < len; j++) {
  124. if (EVP_DecryptUpdate(i->cctx, pt, &l, &ct[j], 1) <= 0)
  125. goto egress;
  126. if (i->next->feed(i->next, pt, l) != (size_t) l)
  127. goto egress;
  128. }
  129. ret = true;
  130. egress:
  131. OPENSSL_cleanse(pt, sizeof(pt));
  132. return ret;
  133. }
  134. static bool
  135. dec_done(jose_io_t *io)
  136. {
  137. io_t *i = containerof(io, io_t, io);
  138. uint8_t pt[EVP_CIPHER_CTX_block_size(i->cctx) + 1];
  139. uint8_t tg[EVP_GCM_TLS_TAG_LEN] = {};
  140. json_t *tag = NULL;
  141. int l = 0;
  142. tag = json_object_get(i->json, "tag");
  143. if (!tag)
  144. return false;
  145. if (jose_b64_dec(tag, NULL, 0) != sizeof(tg))
  146. return false;
  147. if (jose_b64_dec(tag, tg, sizeof(tg)) != sizeof(tg))
  148. return false;
  149. if (EVP_CIPHER_CTX_ctrl(i->cctx, EVP_CTRL_GCM_SET_TAG,
  150. sizeof(tg), tg) <= 0)
  151. return false;
  152. if (EVP_DecryptFinal(i->cctx, pt, &l) <= 0)
  153. return false;
  154. if (!i->next->feed(i->next, pt, l) || !i->next->done(i->next)) {
  155. OPENSSL_cleanse(pt, sizeof(pt));
  156. return false;
  157. }
  158. OPENSSL_cleanse(pt, sizeof(pt));
  159. return true;
  160. }
  161. static json_int_t
  162. alg2len(const char *alg)
  163. {
  164. switch (str2enum(alg, NAMES, NULL)) {
  165. case 0: return 16;
  166. case 1: return 24;
  167. case 2: return 32;
  168. default: return 0;
  169. }
  170. }
  171. static bool
  172. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  173. {
  174. const char *alg = NULL;
  175. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  176. return false;
  177. return alg2len(alg) != 0;
  178. }
  179. static bool
  180. jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
  181. {
  182. const char *alg = NULL;
  183. const char *kty = NULL;
  184. json_int_t byt = 0;
  185. json_int_t len = 0;
  186. if (json_unpack(jwk, "{s:s,s?s,s?I}",
  187. "alg", &alg, "kty", &kty, "bytes", &byt) == -1)
  188. return false;
  189. len = alg2len(alg);
  190. if (len == 0)
  191. return false;
  192. if (byt != 0 && len != byt)
  193. return false;
  194. if (kty && strcmp(kty, "oct") != 0)
  195. return false;
  196. if (json_object_set_new(jwk, "kty", json_string("oct")) < 0)
  197. return false;
  198. if (json_object_set_new(jwk, "bytes", json_integer(len)) < 0)
  199. return false;
  200. return true;
  201. }
  202. static const char *
  203. alg_encr_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *cek)
  204. {
  205. const char *name = NULL;
  206. const char *type = NULL;
  207. if (json_unpack((json_t *) cek, "{s?s,s?s}",
  208. "alg", &name, "kty", &type) < 0)
  209. return NULL;
  210. if (name)
  211. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  212. if (!type || strcmp(type, "oct") != 0)
  213. return NULL;
  214. switch (jose_b64_dec(json_object_get(cek, "k"), NULL, 0)) {
  215. case 16: return "A128GCM";
  216. case 24: return "A192GCM";
  217. case 32: return "A256GCM";
  218. default: return NULL;
  219. }
  220. }
  221. static jose_io_t *
  222. alg_encr_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  223. const json_t *cek, jose_io_t *next)
  224. {
  225. const EVP_CIPHER *cph = NULL;
  226. jose_io_auto_t *io = NULL;
  227. io_t *i = NULL;
  228. switch (str2enum(alg->name, NAMES, NULL)) {
  229. case 0: cph = EVP_aes_128_gcm(); break;
  230. case 1: cph = EVP_aes_192_gcm(); break;
  231. case 2: cph = EVP_aes_256_gcm(); break;
  232. default: return NULL;
  233. }
  234. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  235. if (RAND_bytes(iv, sizeof(iv)) <= 0)
  236. return NULL;
  237. i = calloc(1, sizeof(*i));
  238. if (!i)
  239. return NULL;
  240. io = jose_io_incref(&i->io);
  241. io->feed = enc_feed;
  242. io->done = enc_done;
  243. io->free = io_free;
  244. i->json = json_incref(jwe);
  245. i->next = jose_io_incref(next);
  246. i->cctx = setup(cph, cfg, jwe, cek, iv,
  247. EVP_EncryptInit_ex, EVP_EncryptUpdate);
  248. if (!i->json || !i->next || !i->cctx)
  249. return NULL;
  250. if (json_object_set_new(jwe, "iv", jose_b64_enc(iv, sizeof(iv))) < 0)
  251. return NULL;
  252. return jose_io_incref(io);
  253. }
  254. static jose_io_t *
  255. alg_encr_dec(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  256. const json_t *cek, jose_io_t *next)
  257. {
  258. const EVP_CIPHER *cph = NULL;
  259. jose_io_auto_t *io = NULL;
  260. io_t *i = NULL;
  261. switch (str2enum(alg->name, NAMES, NULL)) {
  262. case 0: cph = EVP_aes_128_gcm(); break;
  263. case 1: cph = EVP_aes_192_gcm(); break;
  264. case 2: cph = EVP_aes_256_gcm(); break;
  265. default: return NULL;
  266. }
  267. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  268. if (jose_b64_dec(json_object_get(jwe, "iv"), NULL, 0) != sizeof(iv))
  269. return NULL;
  270. if (jose_b64_dec(json_object_get(jwe, "iv"), iv, sizeof(iv)) != sizeof(iv))
  271. return NULL;
  272. i = calloc(1, sizeof(*i));
  273. if (!i)
  274. return NULL;
  275. io = jose_io_incref(&i->io);
  276. io->feed = dec_feed;
  277. io->done = dec_done;
  278. io->free = io_free;
  279. i->json = json_incref((json_t *) jwe);
  280. i->next = jose_io_incref(next);
  281. i->cctx = setup(cph, cfg, jwe, cek, iv,
  282. EVP_DecryptInit_ex, EVP_DecryptUpdate);
  283. if (!i->json || !i->next || !i->cctx)
  284. return NULL;
  285. return jose_io_incref(io);
  286. }
  287. static void __attribute__((constructor))
  288. constructor(void)
  289. {
  290. static jose_hook_jwk_t jwk = {
  291. .kind = JOSE_HOOK_JWK_KIND_PREP,
  292. .prep.handles = jwk_prep_handles,
  293. .prep.execute = jwk_prep_execute,
  294. };
  295. static jose_hook_alg_t algs[] = {
  296. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  297. .name = "A128GCM",
  298. .encr.eprm = "encrypt",
  299. .encr.dprm = "decrypt",
  300. .encr.sug = alg_encr_sug,
  301. .encr.enc = alg_encr_enc,
  302. .encr.dec = alg_encr_dec },
  303. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  304. .name = "A192GCM",
  305. .encr.eprm = "encrypt",
  306. .encr.dprm = "decrypt",
  307. .encr.sug = alg_encr_sug,
  308. .encr.enc = alg_encr_enc,
  309. .encr.dec = alg_encr_dec },
  310. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  311. .name = "A256GCM",
  312. .encr.eprm = "encrypt",
  313. .encr.dprm = "decrypt",
  314. .encr.sug = alg_encr_sug,
  315. .encr.enc = alg_encr_enc,
  316. .encr.dec = alg_encr_dec },
  317. {}
  318. };
  319. jose_hook_jwk_push(&jwk);
  320. for (size_t i = 0; algs[i].name; i++)
  321. jose_hook_alg_push(&algs[i]);
  322. }