hmac.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "../hooks.h"
  20. #include <openssl/rand.h>
  21. #include <openssl/sha.h>
  22. #include <string.h>
  23. #define NAMES "HS256", "HS384", "HS512"
  24. typedef struct {
  25. jose_io_t io;
  26. HMAC_CTX *hctx;
  27. json_t *obj;
  28. json_t *sig;
  29. } io_t;
  30. static void
  31. io_free(jose_io_t *io)
  32. {
  33. io_t *i = containerof(io, io_t, io);
  34. HMAC_CTX_free(i->hctx);
  35. json_decref(i->obj);
  36. json_decref(i->sig);
  37. free(i);
  38. }
  39. static bool
  40. io_feed(jose_io_t *io, const void *in, size_t len)
  41. {
  42. io_t *i = containerof(io, io_t, io);
  43. return HMAC_Update(i->hctx, in, len) > 0;
  44. }
  45. static bool
  46. sig_done(jose_io_t *io)
  47. {
  48. io_t *i = containerof(io, io_t, io);
  49. uint8_t hash[HMAC_size(i->hctx)];
  50. unsigned int len = 0;
  51. if (HMAC_Final(i->hctx, hash, &len) <= 0 || len != sizeof(hash))
  52. return false;
  53. if (json_object_set_new(i->sig, "signature",
  54. jose_b64_enc(hash, sizeof(hash))) < 0)
  55. return false;
  56. return add_entity(i->obj, i->sig,
  57. "signatures", "signature", "protected", "header", NULL);
  58. }
  59. static bool
  60. ver_done(jose_io_t *io)
  61. {
  62. io_t *i = containerof(io, io_t, io);
  63. uint8_t hash[HMAC_size(i->hctx)];
  64. uint8_t test[HMAC_size(i->hctx)];
  65. const json_t *sig = NULL;
  66. unsigned int len = 0;
  67. sig = json_object_get(i->sig, "signature");
  68. if (!sig)
  69. return false;
  70. if (jose_b64_dec(sig, NULL, 0) != sizeof(test))
  71. return false;
  72. if (jose_b64_dec(sig, test, sizeof(test)) != sizeof(test))
  73. return false;
  74. if (HMAC_Final(i->hctx, hash, &len) <= 0 || len != sizeof(hash))
  75. return false;
  76. return CRYPTO_memcmp(hash, test, sizeof(hash)) == 0;
  77. }
  78. static HMAC_CTX *
  79. hmac(const jose_hook_alg_t *alg, jose_cfg_t *cfg,
  80. const json_t *sig, const json_t *jwk)
  81. {
  82. uint8_t key[KEYMAX] = {};
  83. const EVP_MD *md = NULL;
  84. const char *prot = NULL;
  85. HMAC_CTX *hctx = NULL;
  86. size_t keyl = 0;
  87. if (json_unpack((json_t *) sig, "{s?s}", "protected", &prot) < 0)
  88. return NULL;
  89. switch (str2enum(alg->name, NAMES, NULL)) {
  90. case 0: md = EVP_sha256(); break;
  91. case 1: md = EVP_sha384(); break;
  92. case 2: md = EVP_sha512(); break;
  93. default: return NULL;
  94. }
  95. keyl = jose_b64_dec(json_object_get(jwk, "k"), NULL, 0);
  96. if (keyl == SIZE_MAX) {
  97. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID, "Error decoding JWK");
  98. return NULL;
  99. }
  100. /* Per RFC 7518 Section 3.2 */
  101. if (keyl < (size_t) EVP_MD_size(md)) {
  102. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID,
  103. "Key is too small (cf. RFC 7518 Section 3.2)");
  104. return NULL;
  105. }
  106. if (keyl > KEYMAX) {
  107. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID, "Key is too large");
  108. return NULL;
  109. }
  110. if (jose_b64_dec(json_object_get(jwk, "k"), key, sizeof(key)) != keyl) {
  111. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID,
  112. "JWK 'k' parameter contains invalid Base64");
  113. goto error;
  114. }
  115. hctx = HMAC_CTX_new();
  116. if (!hctx)
  117. goto error;
  118. if (HMAC_Init_ex(hctx, key, keyl, md, NULL) <= 0)
  119. goto error;
  120. if (prot && HMAC_Update(hctx, (uint8_t *) prot, strlen(prot)) <= 0)
  121. goto error;
  122. if (HMAC_Update(hctx, (uint8_t *) ".", 1) <= 0)
  123. goto error;
  124. OPENSSL_cleanse(key, sizeof(key));
  125. return hctx;
  126. error:
  127. OPENSSL_cleanse(key, sizeof(key));
  128. HMAC_CTX_free(hctx);
  129. return NULL;
  130. }
  131. static bool
  132. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  133. {
  134. const char *alg = NULL;
  135. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  136. return false;
  137. return str2enum(alg, NAMES, NULL) != SIZE_MAX;
  138. }
  139. static json_t *
  140. jwk_prep_execute(jose_cfg_t *cfg, const json_t *jwk)
  141. {
  142. const char *alg = NULL;
  143. json_int_t len = 0;
  144. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) < 0)
  145. return NULL;
  146. switch (str2enum(alg, NAMES, NULL)) {
  147. case 0: len = 32; break;
  148. case 1: len = 48; break;
  149. case 2: len = 64; break;
  150. default: return NULL;
  151. }
  152. return json_pack("{s:{s:s,s:I}}", "upd", "kty", "oct", "bytes", len);
  153. }
  154. static const char *
  155. alg_sign_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  156. {
  157. const char *name = NULL;
  158. const char *type = NULL;
  159. size_t len = 0;
  160. if (json_unpack((json_t *) jwk, "{s?s,s?s}", "alg", &name, "kty", &type) < 0)
  161. return NULL;
  162. if (name)
  163. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  164. if (!type || strcmp(type, "oct") != 0)
  165. return NULL;
  166. len = jose_b64_dec(json_object_get(jwk, "k"), NULL, 0);
  167. if (len == SIZE_MAX)
  168. return NULL;
  169. if (len >= SHA512_DIGEST_LENGTH)
  170. return "HS512";
  171. else if (len >= SHA384_DIGEST_LENGTH)
  172. return "HS384";
  173. else if (len >= SHA256_DIGEST_LENGTH)
  174. return "HS256";
  175. return NULL;
  176. }
  177. static jose_io_t *
  178. alg_sign_sig(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jws,
  179. json_t *sig, const json_t *jwk)
  180. {
  181. jose_io_auto_t *io = NULL;
  182. io_t *i = NULL;
  183. i = calloc(1, sizeof(*i));
  184. if (!i)
  185. return false;
  186. io = jose_io_incref(&i->io);
  187. io->feed = io_feed;
  188. io->done = sig_done;
  189. io->free = io_free;
  190. i->obj = json_incref(jws);
  191. i->sig = json_incref(sig);
  192. i->hctx = hmac(alg, cfg, sig, jwk);
  193. if (!i->obj || !i->sig || !i->hctx)
  194. return NULL;
  195. return jose_io_incref(io);
  196. }
  197. static jose_io_t *
  198. alg_sign_ver(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jws,
  199. const json_t *sig, const json_t *jwk)
  200. {
  201. jose_io_auto_t *io = NULL;
  202. io_t *i = NULL;
  203. i = calloc(1, sizeof(*i));
  204. if (!i)
  205. return false;
  206. io = jose_io_incref(&i->io);
  207. io->feed = io_feed;
  208. io->done = ver_done;
  209. io->free = io_free;
  210. i->sig = json_incref((json_t *) sig);
  211. i->hctx = hmac(alg, cfg, sig, jwk);
  212. if (!i->sig || !i->hctx)
  213. return NULL;
  214. return jose_io_incref(io);
  215. }
  216. static void __attribute__((constructor))
  217. constructor(void)
  218. {
  219. static jose_hook_jwk_t jwk = {
  220. .kind = JOSE_HOOK_JWK_KIND_PREP,
  221. .prep.handles = jwk_prep_handles,
  222. .prep.execute = jwk_prep_execute,
  223. };
  224. static jose_hook_alg_t algs[] = {
  225. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  226. .name = "HS256",
  227. .sign.sprm = "sign",
  228. .sign.vprm = "verify",
  229. .sign.sug = alg_sign_sug,
  230. .sign.sig = alg_sign_sig,
  231. .sign.ver = alg_sign_ver },
  232. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  233. .name = "HS384",
  234. .sign.sprm = "sign",
  235. .sign.vprm = "verify",
  236. .sign.sug = alg_sign_sug,
  237. .sign.sig = alg_sign_sig,
  238. .sign.ver = alg_sign_ver },
  239. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  240. .name = "HS512",
  241. .sign.sprm = "sign",
  242. .sign.vprm = "verify",
  243. .sign.sug = alg_sign_sug,
  244. .sign.sig = alg_sign_sig,
  245. .sign.ver = alg_sign_ver },
  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. }