ecdsa.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <jose/openssl.h>
  21. #include <string.h>
  22. #define NAMES "ES256", "ES384", "ES512"
  23. typedef struct {
  24. jose_io_t io;
  25. jose_io_t *h;
  26. jose_io_t *b;
  27. EC_KEY *key;
  28. json_t *obj;
  29. json_t *sig;
  30. size_t hshl;
  31. void *hsh;
  32. } io_t;
  33. declare_cleanup(ECDSA_SIG)
  34. static void
  35. io_free(jose_io_t *io)
  36. {
  37. io_t *i = containerof(io, io_t, io);
  38. if (i->h)
  39. i->h->free(i->h);
  40. if (i->b)
  41. i->b->free(i->b);
  42. EC_KEY_free(i->key);
  43. json_decref(i->obj);
  44. json_decref(i->sig);
  45. free(i);
  46. }
  47. static bool
  48. io_feed(jose_io_t *io, const void *in, size_t len)
  49. {
  50. io_t *i = containerof(io, io_t, io);
  51. return i->h->feed(i->h, in, len);
  52. }
  53. static bool
  54. sig_done(jose_io_t *io)
  55. {
  56. io_t *i = containerof(io, io_t, io);
  57. uint8_t buf[(EC_GROUP_get_degree(EC_KEY_get0_group(i->key)) + 7) / 8 * 2];
  58. openssl_auto(ECDSA_SIG) *ecdsa = NULL;
  59. const BIGNUM *r = NULL;
  60. const BIGNUM *s = NULL;
  61. if (!i->h->done(i->h))
  62. return false;
  63. ecdsa = ECDSA_do_sign(i->hsh, i->hshl, i->key);
  64. if (!ecdsa)
  65. return false;
  66. ECDSA_SIG_get0(ecdsa, &r, &s);
  67. if (!bn_encode(r, buf, sizeof(buf) / 2))
  68. return false;
  69. if (!bn_encode(s, &buf[sizeof(buf) / 2], sizeof(buf) / 2))
  70. return false;
  71. if (json_object_set_new(i->sig, "signature",
  72. jose_b64_enc(buf, sizeof(buf))) < 0)
  73. return false;
  74. return add_entity(i->obj, i->sig,
  75. "signatures", "signature", "protected", "header", NULL);
  76. }
  77. static bool
  78. ver_done(jose_io_t *io)
  79. {
  80. io_t *i = containerof(io, io_t, io);
  81. uint8_t buf[(EC_GROUP_get_degree(EC_KEY_get0_group(i->key)) + 7) / 8 * 2];
  82. openssl_auto(ECDSA_SIG) *ecdsa = NULL;
  83. const json_t *sig = NULL;
  84. BIGNUM *r = NULL;
  85. BIGNUM *s = NULL;
  86. sig = json_object_get(i->sig, "signature");
  87. if (!sig)
  88. return false;
  89. if (jose_b64_dec(sig, NULL, 0) != sizeof(buf))
  90. return false;
  91. if (jose_b64_dec(sig, buf, sizeof(buf)) != sizeof(buf))
  92. return false;
  93. ecdsa = ECDSA_SIG_new();
  94. if (!ecdsa)
  95. return false;
  96. r = bn_decode(buf, sizeof(buf) / 2);
  97. s = bn_decode(&buf[sizeof(buf) / 2], sizeof(buf) / 2);
  98. if (ECDSA_SIG_set0(ecdsa, r, s) <= 0) {
  99. BN_free(r);
  100. BN_free(s);
  101. return false;
  102. }
  103. if (!i->h->done(i->h))
  104. return false;
  105. return ECDSA_do_verify(i->hsh, i->hshl, ecdsa, i->key) == 1;
  106. }
  107. static bool
  108. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  109. {
  110. const char *alg = NULL;
  111. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  112. return false;
  113. return str2enum(alg, NAMES, NULL) != SIZE_MAX;
  114. }
  115. static json_t *
  116. jwk_prep_execute(jose_cfg_t *cfg, const json_t *jwk)
  117. {
  118. const char *alg = NULL;
  119. const char *grp = NULL;
  120. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  121. return false;
  122. switch (str2enum(alg, NAMES, NULL)) {
  123. case 0: grp = "P-256"; break;
  124. case 1: grp = "P-384"; break;
  125. case 2: grp = "P-521"; break;
  126. default: return false;
  127. }
  128. return json_pack("{s:{s:s,s:s}}", "upd", "kty", "EC", "crv", grp);
  129. }
  130. static const char *
  131. alg_sign_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  132. {
  133. const char *name = NULL;
  134. const char *type = NULL;
  135. const char *curv = NULL;
  136. if (json_unpack((json_t *) jwk, "{s?s,s?s,s?s}",
  137. "alg", &name, "kty", &type, "crv", &curv) < 0)
  138. return NULL;
  139. if (name)
  140. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  141. if (!type || strcmp(type, "EC") != 0)
  142. return NULL;
  143. switch (str2enum(curv, "P-256", "P-384", "P-521", NULL)) {
  144. case 0: return "ES256";
  145. case 1: return "ES384";
  146. case 2: return "ES512";
  147. default: return NULL;
  148. }
  149. }
  150. static jose_io_t *
  151. alg_sign_sig(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jws,
  152. json_t *sig, const json_t *jwk)
  153. {
  154. const jose_hook_alg_t *halg = NULL;
  155. jose_io_auto_t *io = NULL;
  156. const char *prot = NULL;
  157. io_t *i = NULL;
  158. size_t plen = 0;
  159. if (json_unpack(sig, "{s?s%}", "protected", &prot, &plen) < 0)
  160. return NULL;
  161. halg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_HASH, &alg->name[1]);
  162. if (!halg)
  163. return NULL;
  164. i = calloc(1, sizeof(*i));
  165. if (!i)
  166. return NULL;
  167. io = jose_io_incref(&i->io);
  168. io->feed = io_feed;
  169. io->done = sig_done;
  170. io->free = io_free;
  171. i->b = jose_io_malloc(cfg, &i->hsh, &i->hshl);
  172. i->h = halg->hash.hsh(halg, cfg, i->b);
  173. i->obj = json_incref(jws);
  174. i->sig = json_incref(sig);
  175. i->key = jose_openssl_jwk_to_EC_KEY(cfg, jwk);
  176. if (!i->b || !i->h || !i->obj || !i->sig || !i->key)
  177. return NULL;
  178. if (prot && !i->h->feed(i->h, prot, plen))
  179. return NULL;
  180. if (!i->h->feed(i->h, ".", 1))
  181. return NULL;
  182. return jose_io_incref(io);
  183. }
  184. static jose_io_t *
  185. alg_sign_ver(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jws,
  186. const json_t *sig, const json_t *jwk)
  187. {
  188. const jose_hook_alg_t *halg = NULL;
  189. jose_io_auto_t *io = NULL;
  190. const char *prot = NULL;
  191. io_t *i = NULL;
  192. size_t plen = 0;
  193. if (json_unpack((json_t *) sig, "{s?s%}", "protected", &prot, &plen) < 0)
  194. return NULL;
  195. halg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_HASH, &alg->name[1]);
  196. if (!halg)
  197. return NULL;
  198. i = calloc(1, sizeof(*i));
  199. if (!i)
  200. return NULL;
  201. io = jose_io_incref(&i->io);
  202. io->feed = io_feed;
  203. io->done = ver_done;
  204. io->free = io_free;
  205. i->b = jose_io_malloc(cfg, &i->hsh, &i->hshl);
  206. i->h = halg->hash.hsh(halg, cfg, i->b);
  207. i->sig = json_incref((json_t *) sig);
  208. i->key = jose_openssl_jwk_to_EC_KEY(cfg, jwk);
  209. if (!i->b || !i->h || !i->sig || !i->key)
  210. return NULL;
  211. if (prot && !i->h->feed(i->h, prot, plen))
  212. return NULL;
  213. if (!i->h->feed(i->h, ".", 1))
  214. return NULL;
  215. return jose_io_incref(io);
  216. }
  217. static void __attribute__((constructor))
  218. constructor(void)
  219. {
  220. static jose_hook_jwk_t jwk = {
  221. .kind = JOSE_HOOK_JWK_KIND_PREP,
  222. .prep.handles = jwk_prep_handles,
  223. .prep.execute = jwk_prep_execute,
  224. };
  225. static jose_hook_alg_t algs[] = {
  226. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  227. .name = "ES256",
  228. .sign.sprm = "sign",
  229. .sign.vprm = "verify",
  230. .sign.sug = alg_sign_sug,
  231. .sign.sig = alg_sign_sig,
  232. .sign.ver = alg_sign_ver },
  233. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  234. .name = "ES384",
  235. .sign.sprm = "sign",
  236. .sign.vprm = "verify",
  237. .sign.sug = alg_sign_sug,
  238. .sign.sig = alg_sign_sig,
  239. .sign.ver = alg_sign_ver },
  240. { .kind = JOSE_HOOK_ALG_KIND_SIGN,
  241. .name = "ES512",
  242. .sign.sprm = "sign",
  243. .sign.vprm = "verify",
  244. .sign.sug = alg_sign_sug,
  245. .sign.sig = alg_sign_sig,
  246. .sign.ver = alg_sign_ver },
  247. {}
  248. };
  249. jose_hook_jwk_push(&jwk);
  250. for (size_t i = 0; algs[i].name; i++)
  251. jose_hook_alg_push(&algs[i]);
  252. }