1
0

jwk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 <jose/b64.h>
  18. #include <jose/jwk.h>
  19. #include "hooks.h"
  20. #include "misc.h"
  21. #include "hsh.h"
  22. #include <stddef.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. static bool
  26. jwk_hook(jose_cfg_t *cfg, json_t *jwk, jose_hook_jwk_kind_t kind)
  27. {
  28. for (const jose_hook_jwk_t *j = jose_hook_jwk_list(); j; j = j->next) {
  29. if (j->kind != kind)
  30. continue;
  31. switch (kind) {
  32. case JOSE_HOOK_JWK_KIND_PREP:
  33. if (j->prep.handles(cfg, jwk) && !j->prep.execute(cfg, jwk))
  34. return false;
  35. break;
  36. case JOSE_HOOK_JWK_KIND_MAKE:
  37. if (j->make.handles(cfg, jwk))
  38. return j->make.execute(cfg, jwk);
  39. break;
  40. default:
  41. continue;
  42. }
  43. }
  44. return kind == JOSE_HOOK_JWK_KIND_PREP;
  45. }
  46. bool
  47. jose_jwk_gen(jose_cfg_t *cfg, json_t *jwk)
  48. {
  49. const json_t *ko = NULL;
  50. const char *alg = NULL;
  51. const char *kty = NULL;
  52. const char *use = NULL;
  53. if (!jwk_hook(cfg, jwk, JOSE_HOOK_JWK_KIND_PREP))
  54. return false;
  55. if (!jwk_hook(cfg, jwk, JOSE_HOOK_JWK_KIND_MAKE))
  56. return false;
  57. if (json_unpack(jwk, "{s?s,s:s,s?s,s?o}",
  58. "alg", &alg, "kty", &kty, "use", &use, "key_ops", &ko) < 0)
  59. return false;
  60. for (const jose_hook_alg_t *a = jose_hook_alg_list();
  61. a && alg && !use && !ko; a = a->next) {
  62. json_auto_t *ops = NULL;
  63. if (strcmp(alg, a->name) != 0)
  64. continue;
  65. ops = json_array();
  66. if (!ops)
  67. return false;
  68. switch (a->kind) {
  69. case JOSE_HOOK_ALG_KIND_SIGN:
  70. if (json_array_append_new(ops, json_string("sign")) < 0)
  71. return false;
  72. if (json_array_append_new(ops, json_string("verify")) < 0)
  73. return false;
  74. break;
  75. case JOSE_HOOK_ALG_KIND_WRAP:
  76. if (json_array_append_new(ops, json_string("wrapKey")) < 0)
  77. return false;
  78. if (json_array_append_new(ops, json_string("unwrapKey")) < 0)
  79. return false;
  80. break;
  81. case JOSE_HOOK_ALG_KIND_ENCR:
  82. if (json_array_append_new(ops, json_string("encrypt")) < 0)
  83. return false;
  84. if (json_array_append_new(ops, json_string("decrypt")) < 0)
  85. return false;
  86. break;
  87. case JOSE_HOOK_ALG_KIND_EXCH:
  88. if (json_array_append_new(ops, json_string("deriveKey")) < 0)
  89. return false;
  90. break;
  91. default:
  92. break;
  93. }
  94. if (json_array_size(ops) > 0 &&
  95. json_object_set(jwk, "key_ops", ops) < 0)
  96. return false;
  97. break;
  98. }
  99. for (const jose_hook_jwk_t *j = jose_hook_jwk_list(); j; j = j->next) {
  100. if (j->kind != JOSE_HOOK_JWK_KIND_TYPE)
  101. continue;
  102. if (strcmp(j->type.kty, kty) == 0) {
  103. for (size_t i = 0; j->type.req[i]; i++) {
  104. if (!json_object_get(jwk, j->type.req[i]))
  105. return false;
  106. }
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. static bool
  113. jwk_clean(jose_cfg_t *cfg, json_t *jwk)
  114. {
  115. const jose_hook_jwk_t *type = NULL;
  116. const char *kty = NULL;
  117. bool sym = false;
  118. if (json_unpack(jwk, "{s:s}", "kty", &kty) == -1)
  119. return false;
  120. for (type = jose_hook_jwk_list(); type; type = type->next) {
  121. if (type->kind != JOSE_HOOK_JWK_KIND_TYPE)
  122. continue;
  123. if (strcasecmp(kty, type->type.kty) == 0)
  124. break;
  125. }
  126. if (!type)
  127. return false;
  128. sym = !type->type.pub || !type->type.pub[0];
  129. for (size_t i = 0; type->type.prv[i]; i++) {
  130. if (!json_object_get(jwk, type->type.prv[i]))
  131. continue;
  132. if (json_object_del(jwk, type->type.prv[i]) == -1)
  133. return false;
  134. }
  135. for (const jose_hook_jwk_t *o = jose_hook_jwk_list(); o; o = o->next) {
  136. json_t *arr = NULL;
  137. if (o->kind != JOSE_HOOK_JWK_KIND_OPER)
  138. continue;
  139. if (!o->oper.prv && (!sym || !o->oper.pub))
  140. continue;
  141. arr = json_object_get(jwk, "key_ops");
  142. for (size_t i = 0; i < json_array_size(arr); i++) {
  143. const char *ko = NULL;
  144. ko = json_string_value(json_array_get(arr, i));
  145. if (!ko)
  146. continue;
  147. if ((!o->oper.prv || strcmp(o->oper.prv, ko) != 0) &&
  148. (!sym || !o->oper.pub || strcmp(o->oper.pub, ko) != 0))
  149. continue;
  150. if (json_array_remove(arr, i--) == -1)
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. bool
  157. jose_jwk_pub(jose_cfg_t *cfg, json_t *jwk)
  158. {
  159. json_t *keys = NULL;
  160. if (json_is_array(jwk))
  161. keys = jwk;
  162. else if (json_is_array(json_object_get(jwk, "keys")))
  163. keys = json_object_get(jwk, "keys");
  164. if (!keys)
  165. return jwk_clean(cfg, jwk);
  166. for (size_t i = 0; i < json_array_size(keys); i++) {
  167. if (!jwk_clean(cfg, json_array_get(keys, i)))
  168. return false;
  169. }
  170. return true;
  171. }
  172. bool
  173. jose_jwk_prm(jose_cfg_t *cfg, const json_t *jwk, bool req, const char *op)
  174. {
  175. const char *use = NULL;
  176. json_t *ko = NULL;
  177. if (!json_is_object(jwk))
  178. return true;
  179. if (!op)
  180. return false;
  181. if (json_unpack((json_t *) jwk, "{s?s,s?o}",
  182. "use", &use, "key_ops", &ko) != 0)
  183. return false;
  184. if (!use && !ko)
  185. return !req;
  186. for (size_t i = 0; i < json_array_size(ko); i++) {
  187. json_t *v = json_array_get(ko, i);
  188. if (json_is_string(v) && strcmp(op, json_string_value(v)) == 0)
  189. return true;
  190. }
  191. for (const jose_hook_jwk_t *o = jose_hook_jwk_list(); use && o; o = o->next) {
  192. if (o->kind != JOSE_HOOK_JWK_KIND_OPER)
  193. continue;
  194. if (!o->oper.use || strcmp(use, o->oper.use) != 0)
  195. continue;
  196. if (o->oper.pub && strcmp(op, o->oper.pub) == 0)
  197. return true;
  198. if (o->oper.prv && strcmp(op, o->oper.prv) == 0)
  199. return true;
  200. }
  201. return false;
  202. }
  203. static const jose_hook_jwk_t *
  204. find_type(const json_t *jwk)
  205. {
  206. const char *kty = NULL;
  207. if (json_unpack((json_t *) jwk, "{s:s}", "kty", &kty) < 0)
  208. return NULL;
  209. for (const jose_hook_jwk_t *t = jose_hook_jwk_list(); t; t = t->next) {
  210. if (t->kind != JOSE_HOOK_JWK_KIND_TYPE)
  211. continue;
  212. if (strcasecmp(kty, t->type.kty) == 0)
  213. return t;
  214. }
  215. return NULL;
  216. }
  217. bool
  218. jose_jwk_eql(jose_cfg_t *cfg, const json_t *a, const json_t *b)
  219. {
  220. const jose_hook_jwk_t *type = NULL;
  221. type = find_type(a);
  222. if (!type)
  223. return false;
  224. if (!json_equal(json_object_get(a, "kty"), json_object_get(b, "kty")))
  225. return false;
  226. for (size_t i = 0; type->type.req[i]; i++) {
  227. json_t *aa = json_object_get(a, type->type.req[i]);
  228. json_t *bb = json_object_get(b, type->type.req[i]);
  229. if (!aa || !bb || !json_equal(aa, bb))
  230. return false;
  231. }
  232. return true;
  233. }
  234. static char *
  235. jwk_str(const json_t *jwk)
  236. {
  237. const jose_hook_jwk_t *type = NULL;
  238. json_auto_t *key = NULL;
  239. type = find_type(jwk);
  240. if (!type)
  241. return NULL;
  242. key = json_object();
  243. if (!key)
  244. return NULL;
  245. if (json_object_set(key, "kty", json_object_get(jwk, "kty")) < 0)
  246. return NULL;
  247. for (size_t i = 0; type->type.req[i]; i++) {
  248. json_t *tmp = NULL;
  249. tmp = json_object_get(jwk, type->type.req[i]);
  250. if (!tmp)
  251. return NULL;
  252. if (json_object_set(key, type->type.req[i], tmp) < 0)
  253. return NULL;
  254. }
  255. return json_dumps(key, JSON_SORT_KEYS | JSON_COMPACT);
  256. }
  257. json_t *
  258. jose_jwk_thp(jose_cfg_t *cfg, const json_t *jwk, const char *hash)
  259. {
  260. json_t *thp = NULL;
  261. char *str = NULL;
  262. str = jwk_str(jwk);
  263. if (!str)
  264. return NULL;
  265. thp = hsh(cfg, hash, str, strlen(str));
  266. zero(str, strlen(str));
  267. free(str);
  268. return thp;
  269. }
  270. size_t
  271. jose_jwk_thp_buf(jose_cfg_t *cfg, const json_t *jwk,
  272. const char *alg, uint8_t *thp, size_t len)
  273. {
  274. char *str = NULL;
  275. if (!thp || len == 0)
  276. return hsh_buf(cfg, alg, NULL, 0, NULL, 0);
  277. str = jwk_str(jwk);
  278. if (!str)
  279. return SIZE_MAX;
  280. len = hsh_buf(cfg, alg, str, strlen(str), thp, len);
  281. zero(str, strlen(str));
  282. free(str);
  283. return len;
  284. }
  285. json_t *
  286. jose_jwk_exc(jose_cfg_t *cfg, const json_t *prv, const json_t *pub)
  287. {
  288. const char *alga = NULL;
  289. const char *algb = NULL;
  290. const char *ktya = NULL;
  291. const char *ktyb = NULL;
  292. if (json_unpack((json_t *) prv, "{s:s,s?s}",
  293. "kty", &ktya, "alg", &alga) < 0) {
  294. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID, "Private JWK is invalid");
  295. return NULL;
  296. }
  297. if (json_unpack((json_t *) pub, "{s:s,s?s}",
  298. "kty", &ktyb, "alg", &algb) < 0) {
  299. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_INVALID, "Public JWK is invalid");
  300. return NULL;
  301. }
  302. if (strcmp(ktya, ktyb) != 0) {
  303. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_MISMATCH,
  304. "Public and private JWKs are different types");
  305. return NULL;
  306. }
  307. if (alga && algb && strcmp(alga, algb) != 0) {
  308. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_MISMATCH,
  309. "Public and private JWKs have different algorithms");
  310. return NULL;
  311. }
  312. for (const jose_hook_alg_t *a = jose_hook_alg_list();
  313. !alga && !algb && a; a = a->next) {
  314. if (a->kind != JOSE_HOOK_ALG_KIND_EXCH)
  315. continue;
  316. alga = a->exch.sug(a, cfg, prv, pub);
  317. }
  318. if (!alga && !algb) {
  319. jose_cfg_err(cfg, JOSE_CFG_ERR_ALG_NOINFER,
  320. "Exchange algorithm cannot be inferred");
  321. return NULL;
  322. }
  323. for (const jose_hook_alg_t *a = jose_hook_alg_list(); a; a = a->next) {
  324. if (a->kind != JOSE_HOOK_ALG_KIND_EXCH)
  325. continue;
  326. if (strcmp(alga ? alga : algb, a->name) != 0)
  327. continue;
  328. if (!jose_jwk_prm(cfg, prv, false, a->exch.prm)) {
  329. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_DENIED,
  330. "Private JWK cannot be used to derive keys");
  331. return NULL;
  332. }
  333. if (!jose_jwk_prm(cfg, pub, false, a->exch.prm)) {
  334. jose_cfg_err(cfg, JOSE_CFG_ERR_JWK_DENIED,
  335. "Public JWK cannot be used to derive keys");
  336. return NULL;
  337. }
  338. return a->exch.exc(a, cfg, prv, pub);
  339. }
  340. jose_cfg_err(cfg, JOSE_CFG_ERR_ALG_NOTSUP,
  341. "Exchange algorithm %s is unsupported", alga ? alga : algb);
  342. return NULL;
  343. }
  344. static void __attribute__((constructor))
  345. constructor(void)
  346. {
  347. static const char *oct_req[] = { "k", NULL };
  348. static const char *oct_prv[] = { "k", NULL };
  349. static const char *rsa_req[] = { "e", "n", NULL };
  350. static const char *rsa_pub[] = { "e", "n", NULL };
  351. static const char *rsa_prv[] = { "p", "d", "q", "dp", "dq", "qi", "oth", NULL };
  352. static const char *ec_req[] = { "crv", "x", "y", NULL };
  353. static const char *ec_pub[] = { "x", "y", NULL };
  354. static const char *ec_prv[] = { "d", NULL };
  355. static jose_hook_jwk_t hooks[] = {
  356. { .kind = JOSE_HOOK_JWK_KIND_TYPE,
  357. .type = { .kty = "oct", .req = oct_req, .prv = oct_prv } },
  358. { .kind = JOSE_HOOK_JWK_KIND_TYPE,
  359. .type = { .kty = "RSA", .req = rsa_req, .pub = rsa_pub, .prv = rsa_prv } },
  360. { .kind = JOSE_HOOK_JWK_KIND_TYPE,
  361. .type = { .kty = "EC", .req = ec_req, .pub = ec_pub, .prv = ec_prv } },
  362. { .kind = JOSE_HOOK_JWK_KIND_OPER,
  363. .oper = { .pub = "verify", .prv = "sign", .use = "sig" } },
  364. { .kind = JOSE_HOOK_JWK_KIND_OPER,
  365. .oper = { .pub = "encrypt", .prv = "decrypt", .use = "enc" } },
  366. { .kind = JOSE_HOOK_JWK_KIND_OPER,
  367. .oper = { .pub = "wrapKey", .prv = "unwrapKey", .use = "enc" } },
  368. { .kind = JOSE_HOOK_JWK_KIND_OPER,
  369. .oper = { .pub = "deriveKey" } },
  370. { .kind = JOSE_HOOK_JWK_KIND_OPER,
  371. .oper = { .pub = "deriveBits" } },
  372. {}
  373. };
  374. for (size_t i = 0; hooks[i].kind != JOSE_HOOK_JWK_KIND_NONE; i++)
  375. jose_hook_jwk_push(&hooks[i]);
  376. }