keys.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright (c) 2020 Red Hat, Inc.
  4. * Author: Sergio Correia <scorreia@redhat.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <dirent.h>
  22. #include <stdio.h>
  23. #include <jose/b64.h>
  24. #include <jose/jwk.h>
  25. #include <jose/jws.h>
  26. #include "keys.h"
  27. #ifndef PATH_MAX
  28. #define PATH_MAX 4096
  29. #endif
  30. static const char**
  31. supported_hashes(void)
  32. {
  33. /* TODO: check if jose has a way to export the hash algorithms it
  34. * supports. */
  35. static const char* hashes[] = {"S1", "S224", "S256", "S384", "S512", NULL};
  36. return hashes;
  37. }
  38. static int
  39. is_hash(const char* alg)
  40. {
  41. if (!alg) {
  42. return 0;
  43. }
  44. const char** algs = supported_hashes();
  45. for (size_t a = 0; algs[a]; a++) {
  46. if (strcmp(alg, algs[a]) == 0) {
  47. return 1;
  48. }
  49. }
  50. return 0;
  51. }
  52. static json_t*
  53. jwk_generate(const char* alg)
  54. {
  55. json_auto_t* jalg = json_pack("{s:s}", "alg", alg);
  56. if (!jalg) {
  57. fprintf(stderr, "Error packing JSON with alg %s\n", alg);
  58. return NULL;
  59. }
  60. if (!jose_jwk_gen(NULL, jalg)) {
  61. fprintf(stderr, "Error generating JWK with alg %s\n", alg);
  62. return NULL;
  63. }
  64. return json_incref(jalg);
  65. }
  66. static char*
  67. jwk_thumbprint(const json_t* jwk, const char* alg)
  68. {
  69. size_t elen = 0;
  70. size_t dlen = 0;
  71. if (!jwk) {
  72. fprintf(stderr, "Invalid JWK\n");
  73. return NULL;
  74. }
  75. if (!alg || !is_hash(alg)) {
  76. fprintf(stderr, "Invalid hash algorithm (%s)\n", alg);
  77. return NULL;
  78. }
  79. dlen = jose_jwk_thp_buf(NULL, NULL, alg, NULL, 0);
  80. if (dlen == SIZE_MAX) {
  81. fprintf(stderr, "Error determining hash size for %s\n", alg);
  82. return NULL;
  83. }
  84. elen = jose_b64_enc_buf(NULL, dlen, NULL, 0);
  85. if (elen == SIZE_MAX) {
  86. fprintf(stderr, "Error determining encoded size for %s\n", alg);
  87. return NULL;
  88. }
  89. uint8_t dec[dlen];
  90. char enc[elen];
  91. if (!jose_jwk_thp_buf(NULL, jwk, alg, dec, sizeof(dec))) {
  92. fprintf(stderr, "Error making thumbprint\n");
  93. return NULL;
  94. }
  95. if (jose_b64_enc_buf(dec, dlen, enc, sizeof(enc)) != elen) {
  96. fprintf(stderr, "Error encoding data Base64\n");
  97. return NULL;
  98. }
  99. return strndup(enc, elen);
  100. }
  101. void
  102. free_tang_keys_info(struct tang_keys_info* tki)
  103. {
  104. if (!tki) {
  105. return;
  106. }
  107. json_t* to_free[] = {tki->m_keys, tki->m_rotated_keys,
  108. tki->m_payload, tki->m_sign
  109. };
  110. size_t len = sizeof(to_free) / sizeof(to_free[0]);
  111. for (size_t i = 0; i < len; i++) {
  112. if (to_free[i] == NULL) {
  113. continue;
  114. }
  115. json_decref(to_free[i]);
  116. }
  117. free(tki);
  118. }
  119. void
  120. cleanup_tang_keys_info(struct tang_keys_info** tki)
  121. {
  122. if (!tki || !*tki) {
  123. return;
  124. }
  125. free_tang_keys_info(*tki);
  126. *tki = NULL;
  127. }
  128. static struct tang_keys_info*
  129. new_tang_keys_info(void)
  130. {
  131. struct tang_keys_info* tki = calloc(1, sizeof(*tki));
  132. if (!tki) {
  133. return NULL;
  134. }
  135. tki->m_keys = json_array();
  136. tki->m_rotated_keys = json_array();
  137. tki->m_payload = json_array();
  138. tki->m_sign = json_array();
  139. if (!tki->m_keys || !tki->m_rotated_keys ||
  140. !tki->m_payload || !tki->m_sign) {
  141. free_tang_keys_info(tki);
  142. return NULL;
  143. }
  144. tki->m_keys_count = 0;
  145. return tki;
  146. }
  147. static int
  148. jwk_valid_for(const json_t* jwk, const char* use)
  149. {
  150. if (!jwk || !use) {
  151. return 0;
  152. }
  153. return jose_jwk_prm(NULL, jwk, false, use);
  154. }
  155. static int
  156. jwk_valid_for_signing_and_verifying(const json_t* jwk)
  157. {
  158. const char* uses[] = {"sign", "verify", NULL};
  159. int ret = 1;
  160. for (int i = 0; uses[i]; i++) {
  161. if (!jwk_valid_for(jwk, uses[i])) {
  162. ret = 0;
  163. break;
  164. }
  165. }
  166. return ret;
  167. }
  168. static int
  169. jwk_valid_for_signing(const json_t* jwk)
  170. {
  171. return jwk_valid_for(jwk, "sign");
  172. }
  173. static int
  174. jwk_valid_for_deriving_keys(const json_t* jwk)
  175. {
  176. return jwk_valid_for(jwk, "deriveKey");
  177. }
  178. static void
  179. cleanup_str(char** str)
  180. {
  181. if (!str || !*str) {
  182. return;
  183. }
  184. free(*str);
  185. *str = NULL;
  186. }
  187. static json_t*
  188. jwk_sign(const json_t* to_sign, const json_t* sig_keys)
  189. {
  190. if (!sig_keys || !json_is_array(sig_keys) || !json_is_array(to_sign)) {
  191. return NULL;
  192. }
  193. json_auto_t* to_sign_copy = json_deep_copy(to_sign);
  194. if (!jose_jwk_pub(NULL, to_sign_copy)) {
  195. fprintf(stderr, "Error removing private material from data to sign\n");
  196. return NULL;
  197. }
  198. json_auto_t* payload = json_pack("{s:O}", "keys", to_sign_copy);
  199. json_auto_t* sig_template = json_pack("{s:{s:s}}",
  200. "protected", "cty", "jwk-set+json");
  201. /* Use the template with the signing keys. */
  202. json_auto_t* sig_template_arr = json_array();
  203. size_t arr_size = json_array_size(sig_keys);
  204. for (size_t i = 0; i < arr_size; i++) {
  205. if (json_array_append(sig_template_arr, sig_template) == -1) {
  206. fprintf(stderr, "Unable to append sig template to array\n");
  207. return NULL;
  208. }
  209. }
  210. __attribute__ ((__cleanup__(cleanup_str))) char* data_to_sign = json_dumps(payload, 0);
  211. json_auto_t* jws = json_pack("{s:o}", "payload",
  212. jose_b64_enc(data_to_sign, strlen(data_to_sign)));
  213. if (!jose_jws_sig(NULL, jws, sig_template_arr, sig_keys)) {
  214. fprintf(stderr, "Error trying to jose_jws_sign\n");
  215. return NULL;
  216. }
  217. return json_incref(jws);
  218. }
  219. static json_t*
  220. find_by_thp(struct tang_keys_info* tki, const char* target)
  221. {
  222. if (!tki) {
  223. return NULL;
  224. }
  225. json_auto_t* keys = json_deep_copy(tki->m_keys);
  226. json_array_extend(keys, tki->m_rotated_keys);
  227. size_t idx;
  228. json_t* jwk;
  229. const char** hashes = supported_hashes();
  230. json_array_foreach(keys, idx, jwk) {
  231. for (int i = 0; hashes[i]; i++) {
  232. __attribute__ ((__cleanup__(cleanup_str))) char* thumbprint = jwk_thumbprint(jwk, hashes[i]);
  233. if (strcmp(thumbprint, target) != 0) {
  234. continue;
  235. }
  236. if (jwk_valid_for_deriving_keys(jwk)) {
  237. return json_incref(jwk);
  238. } else if (jwk_valid_for_signing(jwk)) {
  239. json_auto_t* sign = json_deep_copy(tki->m_sign);
  240. if (json_array_append(sign, jwk) == -1) {
  241. return NULL;
  242. }
  243. json_auto_t* jws = jwk_sign(tki->m_payload, sign);
  244. if (!jws) {
  245. return NULL;
  246. }
  247. return json_incref(jws);
  248. }
  249. }
  250. }
  251. return NULL;
  252. }
  253. static int
  254. prepare_payload_and_sign(struct tang_keys_info* tki)
  255. {
  256. if (!tki) {
  257. return 0;
  258. }
  259. size_t idx;
  260. json_t* jwk;
  261. json_array_foreach(tki->m_keys, idx, jwk) {
  262. if (jwk_valid_for_signing_and_verifying(jwk)) {
  263. if (json_array_append(tki->m_sign, jwk) == -1) {
  264. continue;
  265. }
  266. if (json_array_append(tki->m_payload, jwk) == -1) {
  267. continue;
  268. }
  269. } else if (jwk_valid_for_deriving_keys(jwk)) {
  270. if (json_array_append(tki->m_payload, jwk) == -1) {
  271. continue;
  272. }
  273. }
  274. }
  275. if (json_array_size(tki->m_sign) == 0 || json_array_size(tki->m_payload) == 0) {
  276. return 0;
  277. }
  278. return 1;
  279. }
  280. static int
  281. create_new_keys(const char* jwkdir)
  282. {
  283. const char** hashes = supported_hashes();
  284. const char* alg[] = {"ES512", "ECMR", NULL};
  285. char path[PATH_MAX];
  286. for (int i = 0; alg[i] != NULL; i++) {
  287. json_auto_t* jwk = jwk_generate(alg[i]);
  288. if (!jwk) {
  289. return 0;
  290. }
  291. __attribute__ ((__cleanup__(cleanup_str))) char* thp = jwk_thumbprint(jwk, hashes[0]);
  292. if (!thp) {
  293. return 0;
  294. }
  295. if (snprintf(path, PATH_MAX, "%s/%s.jwk", jwkdir, thp) < 0) {
  296. fprintf(stderr, "Unable to prepare variable with file full path (%s)\n", thp);
  297. return 0;
  298. }
  299. path[sizeof(path) - 1] = '\0';
  300. if (json_dump_file(jwk, path, 0) == -1) {
  301. fprintf(stderr, "Error saving JWK to file (%s)\n", path);
  302. return 0;
  303. }
  304. }
  305. return 1;
  306. }
  307. static struct tang_keys_info*
  308. load_keys(const char* jwkdir)
  309. {
  310. struct tang_keys_info* tki = new_tang_keys_info();
  311. if (!tki) {
  312. return NULL;
  313. }
  314. struct dirent* d;
  315. DIR* dir = opendir(jwkdir);
  316. if (!dir) {
  317. free_tang_keys_info(tki);
  318. return NULL;
  319. }
  320. char filepath[PATH_MAX];
  321. const char* pattern = ".jwk";
  322. while ((d = readdir(dir)) != NULL) {
  323. if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
  324. continue;
  325. }
  326. char* dot = strrchr(d->d_name, '.');
  327. if (!dot) {
  328. continue;
  329. }
  330. if (strcmp(dot, pattern) == 0) {
  331. /* Found a file with .jwk extension. */
  332. if (snprintf(filepath, PATH_MAX, "%s/%s", jwkdir, d->d_name) < 0) {
  333. fprintf(stderr, "Unable to prepare variable with file full path (%s); skipping\n", d->d_name);
  334. continue;
  335. }
  336. filepath[sizeof(filepath) - 1] = '\0';
  337. json_auto_t* json = json_load_file(filepath, 0, NULL);
  338. if (!json) {
  339. fprintf(stderr, "Invalid JSON file (%s); skipping\n", filepath);
  340. continue;
  341. }
  342. json_t* arr = tki->m_keys;
  343. if (d->d_name[0] == '.') {
  344. arr = tki->m_rotated_keys;
  345. }
  346. if (json_array_append(arr, json) == -1) {
  347. fprintf(stderr, "Unable to append JSON (%s) to array; skipping\n", d->d_name);
  348. continue;
  349. }
  350. tki->m_keys_count++;
  351. }
  352. }
  353. closedir(dir);
  354. return tki;
  355. }
  356. struct tang_keys_info*
  357. read_keys(const char* jwkdir)
  358. {
  359. struct tang_keys_info* tki = load_keys(jwkdir);
  360. if (!tki) {
  361. return NULL;
  362. }
  363. if (tki->m_keys_count == 0) {
  364. /* Let's attempt to create a new pair of keys. */
  365. free_tang_keys_info(tki);
  366. if (!create_new_keys(jwkdir)) {
  367. return NULL;
  368. }
  369. tki = load_keys(jwkdir);
  370. }
  371. if (!prepare_payload_and_sign(tki)) {
  372. free_tang_keys_info(tki);
  373. return NULL;
  374. }
  375. return tki;
  376. }
  377. json_t*
  378. find_jws(struct tang_keys_info* tki, const char* thp)
  379. {
  380. if (!tki) {
  381. return NULL;
  382. }
  383. if (thp == NULL) {
  384. /* Default advertisement. */
  385. json_auto_t* jws = jwk_sign(tki->m_payload, tki->m_sign);
  386. if (!jws) {
  387. return NULL;
  388. }
  389. return json_incref(jws);
  390. }
  391. return find_by_thp(tki, thp);
  392. }
  393. json_t*
  394. find_jwk(struct tang_keys_info* tki, const char* thp)
  395. {
  396. if (!tki || !thp) {
  397. return NULL;
  398. }
  399. return find_by_thp(tki, thp);
  400. }