aesgcmkw.c 6.9 KB

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