1
0

gen.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "jwk.h"
  18. #include "../../lib/hooks.h"
  19. #include <unistd.h>
  20. #include <string.h>
  21. #define SUMMARY "Creates a random JWK for each input JWK template"
  22. typedef struct {
  23. FILE *output;
  24. json_t *keys;
  25. bool set;
  26. } jcmd_opt_t;
  27. static const char *prefix =
  28. "jose jwk gen -i JWK [-s] [-o JWK]\n\n" SUMMARY;
  29. static const jcmd_doc_t doc_input[] = {
  30. { .arg = "JSON", .doc="Parse JWK(Set) template from JSON" },
  31. { .arg = "FILE", .doc="Read JWK(Set) template from FILE" },
  32. { .arg = "-", .doc="Read JWK(Set) template from standard input" },
  33. {}
  34. };
  35. static const jcmd_cfg_t cfgs[] = {
  36. {
  37. .opt = { "input", required_argument, .val = 'i' },
  38. .off = offsetof(jcmd_opt_t, keys),
  39. .set = jcmd_opt_set_jwkt,
  40. .doc = doc_input,
  41. },
  42. {
  43. .opt = { "output", required_argument, .val = 'o' },
  44. .off = offsetof(jcmd_opt_t, output),
  45. .doc = jcmd_jwk_doc_output,
  46. .set = jcmd_opt_set_ofile,
  47. .def = "-",
  48. },
  49. {
  50. .opt = { "set", no_argument, .val = 's' },
  51. .off = offsetof(jcmd_opt_t, set),
  52. .doc = jcmd_jwk_doc_set,
  53. .set = jcmd_opt_set_flag,
  54. },
  55. {}
  56. };
  57. static int jcmd_jwk_invalid_key(const char* key, const char* msg) {
  58. int invalid_key = strcmp(key, "kty") && strcmp(key, "alg");
  59. if (invalid_key)
  60. fprintf(stderr, "%s, unknown json key:%s\n", msg, key);
  61. return invalid_key;
  62. }
  63. static int jcmd_jwk_invalid_algo(const json_t* value, const char* msg) {
  64. if (json_is_string(value)) {
  65. const char* algo = json_string_value(value);
  66. if (!jose_hook_alg_find_any(algo)) {
  67. fprintf(stderr, "%s, unknown algorithm:%s\n", msg, algo);
  68. return 1;
  69. }
  70. }
  71. return 0;
  72. }
  73. static int
  74. jcmd_jwk_dump_error(json_t* elem)
  75. {
  76. if (!json_is_object(elem))
  77. return -1;
  78. for (void* iter = json_object_iter(elem); iter;
  79. iter = json_object_iter_next(elem, iter)) {
  80. const char* msg = "JWK generation failed";
  81. const char* key = json_object_iter_key(iter);
  82. if (jcmd_jwk_invalid_key(key, msg) ||
  83. jcmd_jwk_invalid_algo(json_object_iter_value(iter), msg)) {
  84. break;
  85. }
  86. }
  87. return 0;
  88. }
  89. static void
  90. jcmd_opt_cleanup(jcmd_opt_t *opt)
  91. {
  92. jcmd_file_cleanup(&opt->output);
  93. json_decrefp(&opt->keys);
  94. }
  95. static int
  96. jcmd_jwk_gen(int argc, char *argv[])
  97. {
  98. jcmd_opt_auto_t opt = {};
  99. if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
  100. return EXIT_FAILURE;
  101. if (json_array_size(opt.keys) == 0) {
  102. fprintf(stderr, "At least one JWK template is required!\n");
  103. return EXIT_FAILURE;
  104. }
  105. for (size_t i = 0; i < json_array_size(opt.keys); i++) {
  106. if (!jose_jwk_gen(NULL, json_array_get(opt.keys, i))) {
  107. if (jcmd_jwk_dump_error(json_array_get(opt.keys, i)) < 0)
  108. fprintf(stderr, "JWK generation failed! (unknown issue)\n");
  109. return EXIT_FAILURE;
  110. }
  111. }
  112. if (json_array_size(opt.keys) == 1 && !opt.set) {
  113. if (json_dumpf(json_array_get(opt.keys, 0), opt.output,
  114. JSON_COMPACT | JSON_SORT_KEYS) < 0) {
  115. fprintf(stderr, "Error dumping JWK!\n");
  116. return EXIT_FAILURE;
  117. }
  118. } else {
  119. json_auto_t *jwks = NULL;
  120. jwks = json_pack("{s:O}", "keys", opt.keys);
  121. if (!jwks)
  122. return EXIT_FAILURE;
  123. if (json_dumpf(jwks, opt.output, JSON_COMPACT | JSON_SORT_KEYS) < 0) {
  124. fprintf(stderr, "Error dumping JWKSet!\n");
  125. return EXIT_FAILURE;
  126. }
  127. }
  128. if (isatty(fileno(opt.output)))
  129. fprintf(opt.output, "\n");
  130. return EXIT_SUCCESS;
  131. }
  132. JCMD_REGISTER(SUMMARY, jcmd_jwk_gen, "jwk", "gen")