aeskw.c 7.2 KB

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