pbes2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "PBES2-HS256+A128KW", "PBES2-HS384+A192KW", "PBES2-HS512+A256KW"
  24. static json_t *
  25. pbkdf2(const char *alg, jose_cfg_t *cfg, const json_t *jwk, int iter,
  26. uint8_t st[], size_t stl)
  27. {
  28. const EVP_MD *md = NULL;
  29. json_auto_t *key = NULL;
  30. json_t *cek = NULL;
  31. size_t kyl = 0;
  32. size_t dkl = 0;
  33. if (json_is_string(jwk)) {
  34. jwk = key = json_pack("{s:s,s:o}", "kty", "oct", "k",
  35. jose_b64_enc(json_string_value(jwk),
  36. json_string_length(jwk)));
  37. if (!jwk)
  38. return NULL;
  39. }
  40. switch (str2enum(alg, NAMES, NULL)) {
  41. case 0: md = EVP_sha256(); dkl = 16; break;
  42. case 1: md = EVP_sha384(); dkl = 24; break;
  43. case 2: md = EVP_sha512(); dkl = 32; break;
  44. default: return NULL;
  45. }
  46. const size_t pfx = strlen(alg) + 1;
  47. uint8_t slt[pfx + stl];
  48. uint8_t dk[dkl];
  49. char ky[KEYMAX];
  50. memcpy(slt, alg, pfx);
  51. memcpy(&slt[pfx], st, stl);
  52. kyl = jose_b64_dec(json_object_get(jwk, "k"), NULL, 0);
  53. if (kyl > sizeof(ky))
  54. return NULL;
  55. if (jose_b64_dec(json_object_get(jwk, "k"), ky, sizeof(ky)) != kyl) {
  56. OPENSSL_cleanse(ky, sizeof(ky));
  57. return NULL;
  58. }
  59. if (PKCS5_PBKDF2_HMAC(ky, kyl, slt, sizeof(slt), iter, md, dkl, dk) > 0)
  60. cek = json_pack("{s:s,s:o}", "kty", "oct", "k", jose_b64_enc(dk, dkl));
  61. OPENSSL_cleanse(ky, sizeof(ky));
  62. OPENSSL_cleanse(dk, sizeof(dk));
  63. return cek;
  64. }
  65. static bool
  66. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  67. {
  68. const char *alg = NULL;
  69. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) < 0)
  70. return false;
  71. return str2enum(alg, NAMES, NULL) != SIZE_MAX;
  72. }
  73. static json_t *
  74. jwk_prep_execute(jose_cfg_t *cfg, const json_t *jwk)
  75. {
  76. const char *alg = NULL;
  77. json_int_t len = 0;
  78. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) < 0)
  79. return NULL;
  80. switch (str2enum(alg, NAMES, NULL)) {
  81. case 0: len = 16; break;
  82. case 1: len = 24; break;
  83. case 2: len = 32; break;
  84. default: return NULL;
  85. }
  86. return json_pack("{s:{s:s,s:I}}", "upd", "kty", "oct", "bytes", len);
  87. }
  88. static const char *
  89. alg_wrap_alg(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  90. {
  91. size_t len = 0;
  92. if (json_is_object(jwk)) {
  93. const char *name = NULL;
  94. const char *type = NULL;
  95. if (json_unpack((json_t *) jwk, "{s?s,s?s}",
  96. "alg", &name, "kty", &type) < 0)
  97. return NULL;
  98. if (name)
  99. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  100. if (!type || strcmp(type, "oct") != 0)
  101. return NULL;
  102. len = jose_b64_dec(json_object_get(jwk, "k"), NULL, 0);
  103. if (len == SIZE_MAX)
  104. return NULL;
  105. /* Defer to other algorithms if defined... */
  106. for (const jose_hook_alg_t *a = alg->next; a; a = a->next) {
  107. if (a->kind != JOSE_HOOK_ALG_KIND_WRAP)
  108. continue;
  109. if (a->wrap.alg == alg_wrap_alg)
  110. continue;
  111. if (a->wrap.alg(alg, cfg, jwk))
  112. return NULL;
  113. }
  114. } else if (json_is_string(jwk)) {
  115. len = json_string_length(jwk);
  116. if (len > 36)
  117. return "PBES2-HS512+A256KW";
  118. else if (len > 27)
  119. return "PBES2-HS384+A192KW";
  120. else
  121. return "PBES2-HS256+A128KW";
  122. }
  123. return NULL;
  124. }
  125. static const char *
  126. alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  127. {
  128. switch (str2enum (alg->name, NAMES, NULL)) {
  129. case 0: return "A128CBC-HS256";
  130. case 1: return "A192CBC-HS384";
  131. case 2: return "A256CBC-HS512";
  132. default: return NULL;
  133. }
  134. }
  135. static bool
  136. alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  137. json_t *rcp, const json_t *jwk, json_t *cek)
  138. {
  139. json_auto_t *key = NULL;
  140. json_auto_t *hdr = NULL;
  141. const char *aes = NULL;
  142. json_t *h = NULL;
  143. int p2c = 10000;
  144. size_t stl = 0;
  145. if (!json_object_get(cek, "k") && !jose_jwk_gen(cfg, cek))
  146. return false;
  147. switch (str2enum(alg->name, NAMES, NULL)) {
  148. case 0: aes = "A128KW"; stl = 16; break;
  149. case 1: aes = "A192KW"; stl = 24; break;
  150. case 2: aes = "A256KW"; stl = 32; break;
  151. default: return false;
  152. }
  153. uint8_t st[stl];
  154. if (RAND_bytes(st, stl) <= 0)
  155. return false;
  156. h = json_object_get(rcp, "header");
  157. if (!h && json_object_set_new(rcp, "header", h = json_object()) == -1)
  158. return false;
  159. hdr = jose_jwe_hdr(jwe, rcp);
  160. if (!hdr)
  161. return false;
  162. if (json_unpack(hdr, "{s?i}", "p2c", &p2c) < 0)
  163. return false;
  164. if (!json_object_get(hdr, "p2c") &&
  165. json_object_set_new(h, "p2c", json_integer(p2c)) < 0)
  166. return false;
  167. if (p2c < 1000)
  168. return false;
  169. if (json_object_set_new(h, "p2s", jose_b64_enc(st, stl)) == -1)
  170. return false;
  171. key = pbkdf2(alg->name, cfg, jwk, p2c, st, stl);
  172. if (!key)
  173. return false;
  174. alg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_WRAP, aes);
  175. if (!alg)
  176. return false;
  177. return alg->wrap.wrp(alg, cfg, jwe, rcp, key, cek);
  178. }
  179. static bool
  180. alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  181. const json_t *rcp, const json_t *jwk, json_t *cek)
  182. {
  183. json_auto_t *key = NULL;
  184. json_auto_t *hdr = NULL;
  185. uint8_t st[KEYMAX] = {};
  186. const char *aes = NULL;
  187. json_int_t p2c = -1;
  188. size_t stl = 0;
  189. switch (str2enum(alg->name, NAMES, NULL)) {
  190. case 0: aes = "A128KW"; break;
  191. case 1: aes = "A192KW"; break;
  192. case 2: aes = "A256KW"; break;
  193. default: return false;
  194. }
  195. hdr = jose_jwe_hdr(jwe, rcp);
  196. if (!hdr)
  197. return false;
  198. if (json_unpack(hdr, "{s:I}", "p2c", &p2c) == -1)
  199. return false;
  200. stl = jose_b64_dec(json_object_get(hdr, "p2s"), NULL, 0);
  201. if (stl < 8 || stl > sizeof(st))
  202. return false;
  203. if (jose_b64_dec(json_object_get(hdr, "p2s"), st, sizeof(st)) != stl)
  204. return false;
  205. key = pbkdf2(alg->name, cfg, jwk, p2c, st, stl);
  206. if (!key)
  207. return false;
  208. alg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_WRAP, aes);
  209. if (!alg)
  210. return false;
  211. return alg->wrap.unw(alg, cfg, jwe, rcp, key, cek);
  212. }
  213. static void __attribute__((constructor))
  214. constructor(void)
  215. {
  216. static jose_hook_jwk_t jwk = {
  217. .kind = JOSE_HOOK_JWK_KIND_PREP,
  218. .prep.handles = jwk_prep_handles,
  219. .prep.execute = jwk_prep_execute,
  220. };
  221. static jose_hook_alg_t algs[] = {
  222. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  223. .name = "PBES2-HS256+A128KW",
  224. .wrap.eprm = "wrapKey",
  225. .wrap.dprm = "unwrapKey",
  226. .wrap.alg = alg_wrap_alg,
  227. .wrap.enc = alg_wrap_enc,
  228. .wrap.wrp = alg_wrap_wrp,
  229. .wrap.unw = alg_wrap_unw },
  230. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  231. .name = "PBES2-HS384+A192KW",
  232. .wrap.eprm = "wrapKey",
  233. .wrap.dprm = "unwrapKey",
  234. .wrap.alg = alg_wrap_alg,
  235. .wrap.enc = alg_wrap_enc,
  236. .wrap.wrp = alg_wrap_wrp,
  237. .wrap.unw = alg_wrap_unw },
  238. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  239. .name = "PBES2-HS512+A256KW",
  240. .wrap.eprm = "wrapKey",
  241. .wrap.dprm = "unwrapKey",
  242. .wrap.alg = alg_wrap_alg,
  243. .wrap.enc = alg_wrap_enc,
  244. .wrap.wrp = alg_wrap_wrp,
  245. .wrap.unw = alg_wrap_unw },
  246. {}
  247. };
  248. jose_hook_jwk_push(&jwk);
  249. for (size_t i = 0; algs[i].name; i++)
  250. jose_hook_alg_push(&algs[i]);
  251. }