aesgcmkw.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <jose/jwk.h>
  20. #include "../hooks.h"
  21. #include <string.h>
  22. #define NAMES "A128GCMKW", "A192GCMKW", "A256GCMKW"
  23. static inline const char *
  24. kw2enc(const jose_hook_alg_t *alg)
  25. {
  26. switch (str2enum(alg->name, NAMES, NULL)) {
  27. case 0: return "A128GCM";
  28. case 1: return "A192GCM";
  29. case 2: return "A256GCM";
  30. default: return NULL;
  31. }
  32. }
  33. static inline const jose_hook_alg_t *
  34. kw2alg(const jose_hook_alg_t *alg)
  35. {
  36. const char *enc = NULL;
  37. enc = kw2enc(alg);
  38. if (!enc)
  39. return NULL;
  40. return jose_hook_alg_find(JOSE_HOOK_ALG_KIND_ENCR, enc);
  41. }
  42. static json_int_t
  43. alg2len(const char *alg)
  44. {
  45. switch (str2enum(alg, NAMES, NULL)) {
  46. case 0: return 16;
  47. case 1: return 24;
  48. case 2: return 32;
  49. default: return 0;
  50. }
  51. }
  52. static bool
  53. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  54. {
  55. const char *alg = NULL;
  56. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  57. return false;
  58. return alg2len(alg) != 0;
  59. }
  60. static bool
  61. jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
  62. {
  63. const char *alg = NULL;
  64. const char *kty = NULL;
  65. json_int_t byt = 0;
  66. json_int_t len = 0;
  67. if (json_unpack(jwk, "{s:s,s?s,s?I}",
  68. "alg", &alg, "kty", &kty, "bytes", &byt) == -1)
  69. return false;
  70. len = alg2len(alg);
  71. if (len == 0)
  72. return false;
  73. if (byt != 0 && len != byt)
  74. return false;
  75. if (kty && strcmp(kty, "oct") != 0)
  76. return false;
  77. if (json_object_set_new(jwk, "kty", json_string("oct")) < 0)
  78. return false;
  79. if (json_object_set_new(jwk, "bytes", json_integer(len)) < 0)
  80. return false;
  81. return true;
  82. }
  83. static const char *
  84. alg_wrap_alg(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  85. {
  86. const char *name = NULL;
  87. const char *type = NULL;
  88. if (json_unpack((json_t *) jwk, "{s?s,s?s}",
  89. "alg", &name, "kty", &type) < 0)
  90. return NULL;
  91. if (name)
  92. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  93. if (!type || strcmp(type, "oct") != 0)
  94. return NULL;
  95. switch (jose_b64_dec(json_object_get(jwk, "k"), NULL, 0)) {
  96. case 16: return "A128GCMKW";
  97. case 24: return "A192GCMKW";
  98. case 32: return "A256GCMKW";
  99. default: break;
  100. }
  101. return NULL;
  102. }
  103. static const char *
  104. alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  105. {
  106. return kw2enc(alg);
  107. }
  108. static bool
  109. alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  110. json_t *rcp, const json_t *jwk, json_t *cek)
  111. {
  112. const jose_hook_alg_t *enc = NULL;
  113. jose_io_auto_t *e = NULL;
  114. jose_io_auto_t *d = NULL;
  115. jose_io_auto_t *c = NULL;
  116. jose_io_auto_t *p = NULL;
  117. json_auto_t *tmp = NULL;
  118. const char *k = NULL;
  119. json_t *h = NULL;
  120. void *ct = NULL;
  121. void *pt = NULL;
  122. size_t ptl = 0;
  123. size_t ctl = 0;
  124. size_t kl = 0;
  125. if (!json_object_get(cek, "k") && !jose_jwk_gen(cfg, cek))
  126. return false;
  127. /* Obtain the plaintext to wrap. */
  128. if (json_unpack(cek, "{s:s%}", "k", &k, &kl) < 0)
  129. return false;
  130. p = jose_io_malloc(cfg, &pt, &ptl);
  131. if (!p)
  132. return false;
  133. d = jose_b64_dec_io(p);
  134. if (!d || !d->feed(d, k, kl) || !d->done(d))
  135. return false;
  136. /* Perform the wrapping. */
  137. enc = kw2alg(alg);
  138. if (!enc)
  139. return false;
  140. tmp = json_object();
  141. if (!tmp)
  142. return false;
  143. c = jose_io_malloc(cfg, &ct, &ctl);
  144. if (!c)
  145. return false;
  146. e = enc->encr.enc(enc, cfg, tmp, jwk, c);
  147. if (!e || !e->feed(e, pt, ptl) || !e->done(e))
  148. return false;
  149. /* Save the output. */
  150. h = json_object_get(rcp, "header");
  151. if (!h) {
  152. if (json_object_set_new(rcp, "header", h = json_object()) < 0)
  153. return false;
  154. }
  155. if (!json_is_object(h))
  156. return false;
  157. if (json_object_update(h, tmp) < 0)
  158. return false;
  159. if (json_object_set_new(rcp, "encrypted_key", jose_b64_enc(ct, ctl)) < 0)
  160. return false;
  161. return add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
  162. }
  163. static bool
  164. alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  165. const json_t *rcp, const json_t *jwk, json_t *cek)
  166. {
  167. const jose_hook_alg_t *enc = NULL;
  168. jose_io_auto_t *c = NULL;
  169. jose_io_auto_t *d = NULL;
  170. jose_io_auto_t *p = NULL;
  171. json_auto_t *hdr = NULL;
  172. json_auto_t *tmp = NULL;
  173. const char *ct = NULL;
  174. void *pt = NULL;
  175. size_t ptl = 0;
  176. size_t ctl = 0;
  177. /* Prepare synthetic JWE */
  178. hdr = jose_jwe_hdr(jwe, rcp);
  179. if (!hdr)
  180. return false;
  181. tmp = json_object();
  182. if (!tmp)
  183. return false;
  184. if (json_object_set(tmp, "iv", json_object_get(hdr, "iv")) < 0)
  185. return false;
  186. if (json_object_set(tmp, "tag", json_object_get(hdr, "tag")) < 0)
  187. return false;
  188. /* Perform the unwrap. */
  189. if (json_unpack((json_t *) rcp, "{s:s%}", "encrypted_key", &ct, &ctl) < 0)
  190. return false;
  191. enc = kw2alg(alg);
  192. if (!enc)
  193. return false;
  194. p = jose_io_malloc(cfg, &pt, &ptl);
  195. if (!p)
  196. return false;
  197. c = enc->encr.dec(enc, cfg, tmp, jwk, p);
  198. if (!c)
  199. return false;
  200. d = jose_b64_dec_io(c);
  201. if (!d || !d->feed(d, ct, ctl) || !d->done(d))
  202. return false;
  203. /* Set the output value */
  204. if (json_object_set_new(cek, "k", jose_b64_enc(pt, ptl)) < 0)
  205. return false;
  206. return true;
  207. }
  208. static void __attribute__((constructor))
  209. constructor(void)
  210. {
  211. static jose_hook_jwk_t jwk = {
  212. .kind = JOSE_HOOK_JWK_KIND_PREP,
  213. .prep.handles = jwk_prep_handles,
  214. .prep.execute = jwk_prep_execute,
  215. };
  216. static jose_hook_alg_t algs[] = {
  217. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  218. .name = "A128GCMKW",
  219. .wrap.eprm = "wrapKey",
  220. .wrap.dprm = "unwrapKey",
  221. .wrap.alg = alg_wrap_alg,
  222. .wrap.enc = alg_wrap_enc,
  223. .wrap.wrp = alg_wrap_wrp,
  224. .wrap.unw = alg_wrap_unw },
  225. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  226. .name = "A192GCMKW",
  227. .wrap.eprm = "wrapKey",
  228. .wrap.dprm = "unwrapKey",
  229. .wrap.alg = alg_wrap_alg,
  230. .wrap.enc = alg_wrap_enc,
  231. .wrap.wrp = alg_wrap_wrp,
  232. .wrap.unw = alg_wrap_unw },
  233. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  234. .name = "A256GCMKW",
  235. .wrap.eprm = "wrapKey",
  236. .wrap.dprm = "unwrapKey",
  237. .wrap.alg = alg_wrap_alg,
  238. .wrap.enc = alg_wrap_enc,
  239. .wrap.wrp = alg_wrap_wrp,
  240. .wrap.unw = alg_wrap_unw },
  241. {}
  242. };
  243. jose_hook_jwk_push(&jwk);
  244. for (size_t i = 0; algs[i].name; i++)
  245. jose_hook_alg_push(&algs[i]);
  246. }