pbes2.c 8.6 KB

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