aesgcm.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 bool
  162. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  163. {
  164. const char *alg = NULL;
  165. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  166. return false;
  167. return str2enum(alg, NAMES, NULL) != SIZE_MAX;
  168. }
  169. static json_t *
  170. jwk_prep_execute(jose_cfg_t *cfg, const json_t *jwk)
  171. {
  172. const char *alg = NULL;
  173. json_int_t len = 0;
  174. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  175. return NULL;
  176. switch (str2enum(alg, NAMES, NULL)) {
  177. case 0: len = 16; break;
  178. case 1: len = 24; break;
  179. case 2: len = 32; break;
  180. default: return NULL;
  181. }
  182. return json_pack("{s:{s:s,s:I}}", "upd", "kty", "oct", "bytes", len);
  183. }
  184. static const char *
  185. alg_encr_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *cek)
  186. {
  187. const char *name = NULL;
  188. const char *type = NULL;
  189. if (json_unpack((json_t *) cek, "{s?s,s?s}",
  190. "alg", &name, "kty", &type) < 0)
  191. return NULL;
  192. if (name)
  193. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  194. if (!type || strcmp(type, "oct") != 0)
  195. return NULL;
  196. switch (jose_b64_dec(json_object_get(cek, "k"), NULL, 0)) {
  197. case 16: return "A128GCM";
  198. case 24: return "A192GCM";
  199. case 32: return "A256GCM";
  200. default: return NULL;
  201. }
  202. }
  203. static jose_io_t *
  204. alg_encr_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  205. const json_t *cek, jose_io_t *next)
  206. {
  207. const EVP_CIPHER *cph = NULL;
  208. jose_io_auto_t *io = NULL;
  209. io_t *i = NULL;
  210. switch (str2enum(alg->name, NAMES, NULL)) {
  211. case 0: cph = EVP_aes_128_gcm(); break;
  212. case 1: cph = EVP_aes_192_gcm(); break;
  213. case 2: cph = EVP_aes_256_gcm(); break;
  214. default: return NULL;
  215. }
  216. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  217. if (RAND_bytes(iv, sizeof(iv)) <= 0)
  218. return NULL;
  219. i = calloc(1, sizeof(*i));
  220. if (!i)
  221. return NULL;
  222. io = jose_io_incref(&i->io);
  223. io->feed = enc_feed;
  224. io->done = enc_done;
  225. io->free = io_free;
  226. i->json = json_incref(jwe);
  227. i->next = jose_io_incref(next);
  228. i->cctx = setup(cph, cfg, jwe, cek, iv,
  229. EVP_EncryptInit_ex, EVP_EncryptUpdate);
  230. if (!i->json || !i->next || !i->cctx)
  231. return NULL;
  232. if (json_object_set_new(jwe, "iv", jose_b64_enc(iv, sizeof(iv))) < 0)
  233. return NULL;
  234. return jose_io_incref(io);
  235. }
  236. static jose_io_t *
  237. alg_encr_dec(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  238. const json_t *cek, jose_io_t *next)
  239. {
  240. const EVP_CIPHER *cph = NULL;
  241. jose_io_auto_t *io = NULL;
  242. io_t *i = NULL;
  243. switch (str2enum(alg->name, NAMES, NULL)) {
  244. case 0: cph = EVP_aes_128_gcm(); break;
  245. case 1: cph = EVP_aes_192_gcm(); break;
  246. case 2: cph = EVP_aes_256_gcm(); break;
  247. default: return NULL;
  248. }
  249. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  250. if (jose_b64_dec(json_object_get(jwe, "iv"), NULL, 0) != sizeof(iv))
  251. return NULL;
  252. if (jose_b64_dec(json_object_get(jwe, "iv"), iv, sizeof(iv)) != sizeof(iv))
  253. return NULL;
  254. i = calloc(1, sizeof(*i));
  255. if (!i)
  256. return NULL;
  257. io = jose_io_incref(&i->io);
  258. io->feed = dec_feed;
  259. io->done = dec_done;
  260. io->free = io_free;
  261. i->json = json_incref((json_t *) jwe);
  262. i->next = jose_io_incref(next);
  263. i->cctx = setup(cph, cfg, jwe, cek, iv,
  264. EVP_DecryptInit_ex, EVP_DecryptUpdate);
  265. if (!i->json || !i->next || !i->cctx)
  266. return NULL;
  267. return jose_io_incref(io);
  268. }
  269. static void __attribute__((constructor))
  270. constructor(void)
  271. {
  272. static jose_hook_jwk_t jwk = {
  273. .kind = JOSE_HOOK_JWK_KIND_PREP,
  274. .prep.handles = jwk_prep_handles,
  275. .prep.execute = jwk_prep_execute,
  276. };
  277. static jose_hook_alg_t algs[] = {
  278. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  279. .name = "A128GCM",
  280. .encr.eprm = "encrypt",
  281. .encr.dprm = "decrypt",
  282. .encr.sug = alg_encr_sug,
  283. .encr.enc = alg_encr_enc,
  284. .encr.dec = alg_encr_dec },
  285. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  286. .name = "A192GCM",
  287. .encr.eprm = "encrypt",
  288. .encr.dprm = "decrypt",
  289. .encr.sug = alg_encr_sug,
  290. .encr.enc = alg_encr_enc,
  291. .encr.dec = alg_encr_dec },
  292. { .kind = JOSE_HOOK_ALG_KIND_ENCR,
  293. .name = "A256GCM",
  294. .encr.eprm = "encrypt",
  295. .encr.dprm = "decrypt",
  296. .encr.sug = alg_encr_sug,
  297. .encr.enc = alg_encr_enc,
  298. .encr.dec = alg_encr_dec },
  299. {}
  300. };
  301. jose_hook_jwk_push(&jwk);
  302. for (size_t i = 0; algs[i].name; i++)
  303. jose_hook_alg_push(&algs[i]);
  304. }