hmac.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. jhmac(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. HMAC_CTX *hctx = NULL;
  85. size_t keyl = 0;
  86. switch (str2enum(alg->name, NAMES, NULL)) {
  87. case 0: md = EVP_sha256(); break;
  88. case 1: md = EVP_sha384(); break;
  89. case 2: md = EVP_sha512(); break;
  90. default: return NULL;
  91. }
  92. keyl = jose_b64_dec(json_object_get(jwk, "k"), NULL, 0);
  93. if (keyl == SIZE_MAX) {
  94. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID, "Error decoding JWK");
  95. return NULL;
  96. }
  97. /* Per RFC 7518 Section 3.2 */
  98. if (keyl < (size_t) EVP_MD_size(md)) {
  99. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID,
  100. "Key is too small (cf. RFC 7518 Section 3.2)");
  101. return NULL;
  102. }
  103. if (keyl > KEYMAX) {
  104. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID, "Key is too large");
  105. return NULL;
  106. }
  107. if (jose_b64_dec(json_object_get(jwk, "k"), key, sizeof(key)) != keyl) {
  108. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID,
  109. "JWK 'k' parameter contains invalid Base64");
  110. goto error;
  111. }
  112. hctx = HMAC_CTX_new();
  113. if (!hctx)
  114. goto error;
  115. if (HMAC_Init_ex(hctx, key, keyl, md, NULL) <= 0)
  116. goto error;
  117. OPENSSL_cleanse(key, sizeof(key));
  118. return hctx;
  119. error:
  120. OPENSSL_cleanse(key, sizeof(key));
  121. HMAC_CTX_free(hctx);
  122. return NULL;
  123. }
  124. static json_int_t
  125. alg2len(const char *alg)
  126. {
  127. switch (str2enum(alg, NAMES, NULL)) {
  128. case 0: return 32;
  129. case 1: return 48;
  130. case 2: return 64;
  131. default: return 0;
  132. }
  133. }
  134. static bool
  135. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  136. {
  137. const char *alg = NULL;
  138. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  139. return false;
  140. return alg2len(alg) != 0;
  141. }
  142. static bool
  143. jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
  144. {
  145. const char *alg = NULL;
  146. const char *kty = NULL;
  147. json_int_t byt = 0;
  148. json_int_t len = 0;
  149. if (json_unpack(jwk, "{s:s,s?s,s?I}",
  150. "alg", &alg, "kty", &kty, "bytes", &byt) < 0)
  151. return false;
  152. len = alg2len(alg);
  153. if (len == 0)
  154. return false;
  155. if (byt != 0 && byt != len)
  156. return false;
  157. if (kty && strcmp(kty, "oct") != 0)
  158. return false;
  159. if (json_object_set_new(jwk, "kty", json_string("oct")) < 0)
  160. return false;
  161. if (json_object_set_new(jwk, "bytes", json_integer(len)) < 0)
  162. return false;
  163. return true;
  164. }
  165. static const char *
  166. alg_sign_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  167. {
  168. const char *name = NULL;
  169. const char *type = NULL;
  170. size_t len = 0;
  171. if (json_unpack((json_t *) jwk, "{s?s,s?s}", "alg", &name, "kty", &type) < 0)
  172. return NULL;
  173. if (name)
  174. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  175. if (!type || strcmp(type, "oct") != 0)
  176. return NULL;
  177. len = jose_b64_dec(json_object_get(jwk, "k"), NULL, 0);
  178. if (len == SIZE_MAX)
  179. return NULL;
  180. if (len >= SHA512_DIGEST_LENGTH)
  181. return "HS512";
  182. else if (len >= SHA384_DIGEST_LENGTH)
  183. return "HS384";
  184. else if (len >= SHA256_DIGEST_LENGTH)
  185. return "HS256";
  186. return NULL;
  187. }
  188. static jose_io_t *
  189. alg_sign_sig(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jws,
  190. json_t *sig, const json_t *jwk)
  191. {
  192. jose_io_auto_t *io = NULL;
  193. io_t *i = NULL;
  194. i = calloc(1, sizeof(*i));
  195. if (!i)
  196. return false;
  197. io = jose_io_incref(&i->io);
  198. io->feed = io_feed;
  199. io->done = sig_done;
  200. io->free = io_free;
  201. i->obj = json_incref(jws);
  202. i->sig = json_incref(sig);
  203. i->hctx = jhmac(alg, cfg, sig, jwk);
  204. if (!i->obj || !i->sig || !i->hctx)
  205. return NULL;
  206. return jose_io_incref(io);
  207. }
  208. static jose_io_t *
  209. alg_sign_ver(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jws,
  210. const json_t *sig, const json_t *jwk)
  211. {
  212. jose_io_auto_t *io = NULL;
  213. io_t *i = NULL;
  214. i = calloc(1, sizeof(*i));
  215. if (!i)
  216. return false;
  217. io = jose_io_incref(&i->io);
  218. io->feed = io_feed;
  219. io->done = ver_done;
  220. io->free = io_free;
  221. i->sig = json_incref((json_t *) sig);
  222. i->hctx = jhmac(alg, cfg, sig, jwk);
  223. if (!i->sig || !i->hctx)
  224. return NULL;
  225. return jose_io_incref(io);
  226. }
  227. static void __attribute__((constructor))
  228. constructor(void)
  229. {
  230. static jose_hook_jwk_t jwk = {
  231. .kind = JOSE_HOOK_JWK_KIND_PREP,
  232. .prep.handles = jwk_prep_handles,
  233. .prep.execute = jwk_prep_execute,
  234. };
  235. static jose_hook_alg_t algs[] = {
  236. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  237. .name = "HS256",
  238. .sign.sprm = "sign",
  239. .sign.vprm = "verify",
  240. .sign.sug = alg_sign_sug,
  241. .sign.sig = alg_sign_sig,
  242. .sign.ver = alg_sign_ver },
  243. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  244. .name = "HS384",
  245. .sign.sprm = "sign",
  246. .sign.vprm = "verify",
  247. .sign.sug = alg_sign_sug,
  248. .sign.sig = alg_sign_sig,
  249. .sign.ver = alg_sign_ver },
  250. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  251. .name = "HS512",
  252. .sign.sprm = "sign",
  253. .sign.vprm = "verify",
  254. .sign.sug = alg_sign_sug,
  255. .sign.sig = alg_sign_sig,
  256. .sign.ver = alg_sign_ver },
  257. {}
  258. };
  259. jose_hook_jwk_push(&jwk);
  260. for (size_t i = 0; algs[i].name; i++)
  261. jose_hook_alg_push(&algs[i]);
  262. }