1
0

gen.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <unistd.h>
  19. #define SUMMARY "Creates a random JWK for each input JWK template"
  20. typedef struct {
  21. FILE *output;
  22. json_t *keys;
  23. bool set;
  24. } jcmd_opt_t;
  25. static const char *prefix =
  26. "jose jwk gen -i JWK [-s] [-o JWK]\n\n" SUMMARY;
  27. static const jcmd_doc_t doc_input[] = {
  28. { .arg = "JSON", .doc="Parse JWK(Set) template from JSON" },
  29. { .arg = "FILE", .doc="Read JWK(Set) template from FILE" },
  30. { .arg = "-", .doc="Read JWK(Set) template from standard input" },
  31. {}
  32. };
  33. static const jcmd_cfg_t cfgs[] = {
  34. {
  35. .opt = { "input", required_argument, .val = 'i' },
  36. .off = offsetof(jcmd_opt_t, keys),
  37. .set = jcmd_opt_set_jwkt,
  38. .doc = doc_input,
  39. },
  40. {
  41. .opt = { "output", required_argument, .val = 'o' },
  42. .off = offsetof(jcmd_opt_t, output),
  43. .doc = jcmd_jwk_doc_output,
  44. .set = jcmd_opt_set_ofile,
  45. .def = "-",
  46. },
  47. {
  48. .opt = { "set", no_argument, .val = 's' },
  49. .off = offsetof(jcmd_opt_t, set),
  50. .doc = jcmd_jwk_doc_set,
  51. .set = jcmd_opt_set_flag,
  52. },
  53. {}
  54. };
  55. static void
  56. jcmd_opt_cleanup(jcmd_opt_t *opt)
  57. {
  58. jcmd_file_cleanup(&opt->output);
  59. json_decrefp(&opt->keys);
  60. }
  61. static int
  62. jcmd_jwk_gen(int argc, char *argv[])
  63. {
  64. jcmd_opt_auto_t opt = {};
  65. if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
  66. return EXIT_FAILURE;
  67. if (json_array_size(opt.keys) == 0) {
  68. fprintf(stderr, "At least one JWK template is required!\n");
  69. return EXIT_FAILURE;
  70. }
  71. for (size_t i = 0; i < json_array_size(opt.keys); i++) {
  72. if (!jose_jwk_gen(NULL, json_array_get(opt.keys, i))) {
  73. fprintf(stderr, "JWK generation failed!\n");
  74. return EXIT_FAILURE;
  75. }
  76. }
  77. if (json_array_size(opt.keys) == 1 && !opt.set) {
  78. if (json_dumpf(json_array_get(opt.keys, 0), opt.output,
  79. JSON_COMPACT | JSON_SORT_KEYS) < 0) {
  80. fprintf(stderr, "Error dumping JWK!\n");
  81. return EXIT_FAILURE;
  82. }
  83. } else {
  84. json_auto_t *jwks = NULL;
  85. jwks = json_pack("{s:O}", "keys", opt.keys);
  86. if (!jwks)
  87. return EXIT_FAILURE;
  88. if (json_dumpf(jwks, opt.output, JSON_COMPACT | JSON_SORT_KEYS) < 0) {
  89. fprintf(stderr, "Error dumping JWKSet!\n");
  90. return EXIT_FAILURE;
  91. }
  92. }
  93. if (isatty(fileno(opt.output)))
  94. fprintf(opt.output, "\n");
  95. return EXIT_SUCCESS;
  96. }
  97. JCMD_REGISTER(SUMMARY, jcmd_jwk_gen, "jwk", "gen")