jws.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #define _GNU_SOURCE
  18. #include "misc.h"
  19. #include <jose/b64.h>
  20. #include <jose/jwk.h>
  21. #include <jose/jws.h>
  22. #include "hooks.h"
  23. #include <errno.h>
  24. #include <string.h>
  25. static const jose_hook_alg_t *
  26. find_alg(jose_cfg_t *cfg, json_t *jws, json_t *sig, const json_t *jwk)
  27. {
  28. const jose_hook_alg_t *alg = NULL;
  29. const char *halg = NULL;
  30. const char *kalg = NULL;
  31. json_auto_t *hdr = NULL;
  32. hdr = jose_jws_hdr(sig);
  33. if (!hdr)
  34. return NULL;
  35. if (json_unpack(hdr, "{s:s}", "alg", &halg) < 0) {
  36. for (alg = jose_hook_alg_list(); alg && !halg; alg = alg->next) {
  37. if (alg->kind != JOSE_HOOK_ALG_KIND_SIGN)
  38. continue;
  39. halg = alg->sign.sug(alg, cfg, jwk);
  40. }
  41. if (!halg) {
  42. jose_cfg_err(cfg, JOSE_CFG_ERR_ALG_NOINFER,
  43. "Unable to infer signing algorithm");
  44. return NULL;
  45. }
  46. alg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_SIGN, halg);
  47. if (alg) {
  48. json_t *h = NULL;
  49. h = json_object_get(sig, "protected");
  50. if (!h && json_object_set_new(sig, "protected", h = json_object()) < 0)
  51. return NULL;
  52. if (json_object_set_new(h, "alg", json_string(alg->name)) < 0)
  53. return NULL;
  54. }
  55. } else {
  56. alg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_SIGN, halg);
  57. }
  58. if (!alg) {
  59. jose_cfg_err(cfg, JOSE_CFG_ERR_ALG_NOTSUP,
  60. "Signing algorithm (%s) is not supported", halg);
  61. return NULL;
  62. }
  63. if (json_unpack((json_t *) jwk, "{s?s}", "alg", &kalg) < 0)
  64. return NULL;
  65. if (halg && kalg && strcmp(halg, kalg) != 0) {
  66. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_MISMATCH,
  67. "Algorithm mismatch (%s != %s)", halg, kalg);
  68. return NULL;
  69. }
  70. if (!jose_jwk_prm(cfg, jwk, false, alg->sign.sprm)) {
  71. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_DENIED,
  72. "JWK cannot be used to sign");
  73. return NULL;
  74. }
  75. return alg;
  76. }
  77. static void
  78. ios_auto(jose_io_t ***iosp)
  79. {
  80. jose_io_t **ios = *iosp;
  81. for (size_t i = 0; ios && ios[i]; i++)
  82. jose_io_auto(&ios[i]);
  83. free(ios);
  84. }
  85. json_t *
  86. jose_jws_hdr(const json_t *sig)
  87. {
  88. json_auto_t *p = NULL;
  89. json_t *h = NULL;
  90. p = json_object_get(sig, "protected");
  91. if (!p)
  92. p = json_object();
  93. else if (json_is_object(p))
  94. p = json_deep_copy(p);
  95. else if (json_is_string(p))
  96. p = jose_b64_dec_load(p);
  97. if (!json_is_object(p))
  98. return NULL;
  99. h = json_object_get(sig, "header");
  100. if (h) {
  101. if (json_object_update_missing(p, h) == -1)
  102. return NULL;
  103. }
  104. return json_incref(p);
  105. }
  106. bool
  107. jose_jws_sig(jose_cfg_t *cfg, json_t *jws, json_t *sig, const json_t *jwk)
  108. {
  109. jose_io_auto_t *io = NULL;
  110. const char *pay = NULL;
  111. size_t payl = 0;
  112. if (json_unpack(jws, "{s:s%}", "payload", &pay, &payl) < 0) {
  113. jose_cfg_err(cfg, JOSE_CFG_ERR_JWS_INVALID,
  114. "JWS missing payload attribute");
  115. return false;
  116. }
  117. io = jose_jws_sig_io(cfg, jws, sig, jwk);
  118. return io && io->feed(io, pay, payl) && io->done(io);
  119. }
  120. jose_io_t *
  121. jose_jws_sig_io(jose_cfg_t *cfg, json_t *jws, json_t *sig, const json_t *jwk)
  122. {
  123. const jose_hook_alg_t *alg = NULL;
  124. json_auto_t *s = NULL;
  125. if (json_is_array(jwk) || json_is_array(json_object_get(jwk, "keys"))) {
  126. jose_io_t __attribute__((cleanup(ios_auto))) **ios = NULL;
  127. const json_t *key = NULL;
  128. size_t i = 0;
  129. if (!json_is_array(jwk))
  130. jwk = json_object_get(jwk, "keys");
  131. if (json_is_array(sig) && json_array_size(sig) != json_array_size(jwk))
  132. return NULL;
  133. ios = calloc(json_array_size(jwk) + 1, sizeof(*ios));
  134. if (!ios)
  135. return NULL;
  136. json_array_foreach(jwk, i, key) {
  137. json_auto_t *tmp = NULL;
  138. if (json_is_array(sig))
  139. tmp = json_incref(json_array_get(sig, i));
  140. else
  141. tmp = json_deep_copy(sig);
  142. ios[i] = jose_jws_sig_io(cfg, jws, tmp, key);
  143. if (!ios[i])
  144. return NULL;
  145. }
  146. return jose_io_multiplex(cfg, ios, true);
  147. }
  148. s = sig ? json_incref(sig) : json_object();
  149. if (!json_is_object(s)) {
  150. jose_cfg_err(cfg, EINVAL, "Parameter sig MUST be an object or NULL");
  151. return NULL;
  152. }
  153. alg = find_alg(cfg, jws, s, jwk);
  154. if (!alg)
  155. return NULL;
  156. if (!encode_protected(s))
  157. return NULL;
  158. return alg->sign.sig(alg, cfg, jws, s, jwk);
  159. }
  160. bool
  161. jose_jws_ver(jose_cfg_t *cfg, const json_t *jws, const json_t *sig,
  162. const json_t *jwk, bool all)
  163. {
  164. jose_io_auto_t *io = NULL;
  165. const char *pay = NULL;
  166. size_t payl = 0;
  167. if (json_unpack((json_t *) jws, "{s:s%}", "payload", &pay, &payl) < 0) {
  168. jose_cfg_err(cfg, JOSE_CFG_ERR_JWS_INVALID,
  169. "JWS missing payload attribute");
  170. return false;
  171. }
  172. io = jose_jws_ver_io(cfg, jws, sig, jwk, all);
  173. return io && io->feed(io, pay, payl) && io->done(io);
  174. }
  175. jose_io_t *
  176. jose_jws_ver_io(jose_cfg_t *cfg, const json_t *jws, const json_t *sig,
  177. const json_t *jwk, bool all)
  178. {
  179. const jose_hook_alg_t *alg = NULL;
  180. const char *kalg = NULL;
  181. const char *halg = NULL;
  182. json_auto_t *hdr = NULL;
  183. if (json_is_array(jwk) || json_is_array(json_object_get(jwk, "keys"))) {
  184. jose_io_t __attribute__((cleanup(ios_auto))) **ios = NULL;
  185. size_t j = 0;
  186. if (!json_is_array(jwk))
  187. jwk = json_object_get(jwk, "keys");
  188. if (json_is_array(sig) && json_array_size(sig) != json_array_size(jwk))
  189. return NULL;
  190. ios = calloc(json_array_size(jwk) + 1, sizeof(*ios));
  191. if (!ios)
  192. return NULL;
  193. for (size_t i = 0; i < json_array_size(jwk); i++) {
  194. const json_t *s = json_is_object(sig) ? sig : json_array_get(sig, i);
  195. const json_t *k = json_array_get(jwk, i);
  196. ios[j] = jose_jws_ver_io(cfg, jws, s, k, false);
  197. if (ios[j])
  198. j++;
  199. else if (all)
  200. return NULL;
  201. }
  202. return jose_io_multiplex(cfg, ios, all);
  203. }
  204. if (!sig) {
  205. jose_io_t __attribute__((cleanup(ios_auto))) **ios = NULL;
  206. const json_t *array = NULL;
  207. const json_t *s = NULL;
  208. size_t i = 0;
  209. size_t j = 0;
  210. array = json_object_get(jws, "signatures");
  211. if (!json_is_array(array))
  212. return jose_jws_ver_io(cfg, jws, jws, jwk, true);
  213. ios = calloc(json_array_size(array) + 1, sizeof(*ios));
  214. if (!ios)
  215. return NULL;
  216. json_array_foreach(array, i, s) {
  217. ios[j] = jose_jws_ver_io(cfg, jws, s, jwk, true);
  218. if (ios[j])
  219. j++;
  220. }
  221. return jose_io_multiplex(cfg, ios, false);
  222. } else if (!json_is_object(sig))
  223. return NULL;
  224. if (json_unpack((json_t *) jwk, "{s?s}", "alg", &kalg) < 0)
  225. return NULL;
  226. hdr = jose_jws_hdr(sig);
  227. if (!hdr)
  228. return NULL;
  229. if (json_unpack(hdr, "{s?s}", "alg", &halg) < 0)
  230. return NULL;
  231. if (!halg) {
  232. if (!kalg) {
  233. jose_cfg_err(cfg, JOSE_CFG_ERR_ALG_NOINFER,
  234. "Signature algorithm cannot be inferred");
  235. return NULL;
  236. }
  237. halg = kalg;
  238. } else if (kalg && strcmp(halg, kalg) < 0) {
  239. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_MISMATCH,
  240. "Signing algorithm mismatch (%s != %s)", halg, kalg);
  241. return NULL;
  242. }
  243. alg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_SIGN, halg);
  244. if (!alg) {
  245. jose_cfg_err(cfg, JOSE_CFG_ERR_ALG_NOTSUP,
  246. "Signing algorithm (%s) is not supported", halg);
  247. return NULL;
  248. }
  249. if (!jose_jwk_prm(cfg, jwk, false, alg->sign.vprm)) {
  250. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_DENIED,
  251. "JWK cannot be used to verify");
  252. return false;
  253. }
  254. return alg->sign.ver(alg, cfg, jws, sig, jwk);
  255. }