aeskw.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <openssl/rand.h>
  22. #include <string.h>
  23. #define NAMES "A128KW", "A192KW", "A256KW"
  24. static json_int_t
  25. alg2len(const char *alg)
  26. {
  27. switch (str2enum(alg, NAMES, NULL)) {
  28. case 0: return 16;
  29. case 1: return 24;
  30. case 2: return 32;
  31. default: return 0;
  32. }
  33. }
  34. static bool
  35. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  36. {
  37. const char *alg = NULL;
  38. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  39. return false;
  40. return alg2len(alg) != 0;
  41. }
  42. static bool
  43. jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
  44. {
  45. const char *alg = NULL;
  46. const char *kty = NULL;
  47. json_int_t byt = 0;
  48. json_int_t len = 0;
  49. if (json_unpack(jwk, "{s:s,s?s,s?I}",
  50. "alg", &alg, "kty", &kty, "bytes", &byt) == -1)
  51. return false;
  52. len = alg2len(alg);
  53. if (len == 0)
  54. return false;
  55. if (byt != 0 && len != byt)
  56. return false;
  57. if (kty && strcmp(kty, "oct") != 0)
  58. return false;
  59. if (json_object_set_new(jwk, "kty", json_string("oct")) < 0)
  60. return false;
  61. if (json_object_set_new(jwk, "bytes", json_integer(len)) < 0)
  62. return false;
  63. return true;
  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 "A128KW";
  79. case 24: return "A192KW";
  80. case 32: return "A256KW";
  81. default: return NULL;
  82. }
  83. }
  84. static const char *
  85. alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  86. {
  87. switch (str2enum(alg->name, NAMES, NULL)) {
  88. case 0: return "A128CBC-HS256";
  89. case 1: return "A192CBC-HS384";
  90. case 2: return "A256CBC-HS512";
  91. default: return NULL;
  92. }
  93. }
  94. static bool
  95. alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  96. json_t *rcp, const json_t *jwk, json_t *cek)
  97. {
  98. const EVP_CIPHER *cph = NULL;
  99. EVP_CIPHER_CTX *ecc = NULL;
  100. bool ret = false;
  101. size_t ptl = 0;
  102. size_t ctl = 0;
  103. int len = 0;
  104. if (!json_object_get(cek, "k") && !jose_jwk_gen(cfg, cek))
  105. return false;
  106. switch (str2enum(alg->name, NAMES, NULL)) {
  107. case 0: cph = EVP_aes_128_wrap(); break;
  108. case 1: cph = EVP_aes_192_wrap(); break;
  109. case 2: cph = EVP_aes_256_wrap(); break;
  110. default: return false;
  111. }
  112. uint8_t ky[EVP_CIPHER_key_length(cph)];
  113. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  114. uint8_t pt[KEYMAX];
  115. uint8_t ct[sizeof(pt) + EVP_CIPHER_block_size(cph) * 2];
  116. memset(iv, 0xA6, EVP_CIPHER_iv_length(cph));
  117. if (jose_b64_dec(json_object_get(jwk, "k"), NULL, 0) != sizeof(ky))
  118. goto egress;
  119. if (jose_b64_dec(json_object_get(jwk, "k"), ky, sizeof(ky)) != sizeof(ky))
  120. goto egress;
  121. ptl = jose_b64_dec(json_object_get(cek, "k"), NULL, 0);
  122. if (ptl > sizeof(pt))
  123. goto egress;
  124. if (jose_b64_dec(json_object_get(cek, "k"), pt, ptl) != ptl)
  125. goto egress;
  126. ecc = EVP_CIPHER_CTX_new();
  127. if (!ecc)
  128. goto egress;
  129. EVP_CIPHER_CTX_set_flags(ecc, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  130. if (EVP_EncryptInit_ex(ecc, cph, NULL, ky, iv) <= 0)
  131. goto egress;
  132. if (EVP_EncryptUpdate(ecc, ct, &len, pt, ptl) <= 0)
  133. goto egress;
  134. ctl = len;
  135. if (EVP_EncryptFinal(ecc, &ct[len], &len) <= 0)
  136. goto egress;
  137. ctl += len;
  138. if (json_object_set_new(rcp, "encrypted_key", jose_b64_enc(ct, ctl)) < 0)
  139. goto egress;
  140. ret = add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
  141. egress:
  142. OPENSSL_cleanse(ky, sizeof(ky));
  143. OPENSSL_cleanse(pt, sizeof(pt));
  144. EVP_CIPHER_CTX_free(ecc);
  145. return ret;
  146. }
  147. static bool
  148. alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  149. const json_t *rcp, const json_t *jwk, json_t *cek)
  150. {
  151. const EVP_CIPHER *cph = NULL;
  152. EVP_CIPHER_CTX *ecc = NULL;
  153. bool ret = false;
  154. size_t ctl = 0;
  155. size_t ptl = 0;
  156. int len = 0;
  157. switch (str2enum(alg->name, NAMES, NULL)) {
  158. case 0: cph = EVP_aes_128_wrap(); break;
  159. case 1: cph = EVP_aes_192_wrap(); break;
  160. case 2: cph = EVP_aes_256_wrap(); break;
  161. default: return NULL;
  162. }
  163. uint8_t ky[EVP_CIPHER_key_length(cph)];
  164. uint8_t iv[EVP_CIPHER_iv_length(cph)];
  165. uint8_t ct[KEYMAX + EVP_CIPHER_block_size(cph) * 2];
  166. uint8_t pt[sizeof(ct)];
  167. memset(iv, 0xA6, sizeof(iv));
  168. if (jose_b64_dec(json_object_get(jwk, "k"), NULL, 0) != sizeof(ky))
  169. goto egress;
  170. if (jose_b64_dec(json_object_get(jwk, "k"), ky, sizeof(ky)) != sizeof(ky))
  171. goto egress;
  172. ctl = jose_b64_dec(json_object_get(rcp, "encrypted_key"), NULL, 0);
  173. if (ctl > sizeof(ct))
  174. goto egress;
  175. if (jose_b64_dec(json_object_get(rcp, "encrypted_key"), ct, ctl) != ctl)
  176. goto egress;
  177. ecc = EVP_CIPHER_CTX_new();
  178. if (!ecc)
  179. goto egress;
  180. EVP_CIPHER_CTX_set_flags(ecc, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  181. if (EVP_DecryptInit_ex(ecc, cph, NULL, ky, iv) <= 0)
  182. goto egress;
  183. if (EVP_DecryptUpdate(ecc, pt, &len, ct, ctl) <= 0)
  184. goto egress;
  185. ptl = len;
  186. if (EVP_DecryptFinal(ecc, &pt[len], &len) <= 0)
  187. goto egress;
  188. ptl += len;
  189. ret = json_object_set_new(cek, "k", jose_b64_enc(pt, ptl)) == 0;
  190. egress:
  191. OPENSSL_cleanse(ky, sizeof(ky));
  192. OPENSSL_cleanse(pt, sizeof(pt));
  193. EVP_CIPHER_CTX_free(ecc);
  194. return ret;
  195. }
  196. static void __attribute__((constructor))
  197. constructor(void)
  198. {
  199. static jose_hook_jwk_t jwk = {
  200. .kind = JOSE_HOOK_JWK_KIND_PREP,
  201. .prep.handles = jwk_prep_handles,
  202. .prep.execute = jwk_prep_execute,
  203. };
  204. static jose_hook_alg_t algs[] = {
  205. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  206. .name = "A128KW",
  207. .wrap.eprm = "wrapKey",
  208. .wrap.dprm = "unwrapKey",
  209. .wrap.alg = alg_wrap_alg,
  210. .wrap.enc = alg_wrap_enc,
  211. .wrap.wrp = alg_wrap_wrp,
  212. .wrap.unw = alg_wrap_unw },
  213. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  214. .name = "A192KW",
  215. .wrap.eprm = "wrapKey",
  216. .wrap.dprm = "unwrapKey",
  217. .wrap.alg = alg_wrap_alg,
  218. .wrap.enc = alg_wrap_enc,
  219. .wrap.wrp = alg_wrap_wrp,
  220. .wrap.unw = alg_wrap_unw },
  221. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  222. .name = "A256KW",
  223. .wrap.eprm = "wrapKey",
  224. .wrap.dprm = "unwrapKey",
  225. .wrap.alg = alg_wrap_alg,
  226. .wrap.enc = alg_wrap_enc,
  227. .wrap.wrp = alg_wrap_wrp,
  228. .wrap.unw = alg_wrap_unw },
  229. {}
  230. };
  231. jose_hook_jwk_push(&jwk);
  232. for (size_t i = 0; algs[i].name; i++)
  233. jose_hook_alg_push(&algs[i]);
  234. }