pub.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "Cleans private keys from a JWK"
  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 pub -i JWK [-s] [-o JWK]\n\n" SUMMARY;
  27. static const jcmd_cfg_t cfgs[] = {
  28. {
  29. .opt = { "input", required_argument, .val = 'i' },
  30. .off = offsetof(jcmd_opt_t, keys),
  31. .set = jcmd_opt_set_jwks,
  32. .doc = jcmd_jwk_doc_input,
  33. },
  34. {
  35. .opt = { "output", required_argument, .val = 'o' },
  36. .off = offsetof(jcmd_opt_t, output),
  37. .doc = jcmd_jwk_doc_output,
  38. .set = jcmd_opt_set_ofile,
  39. .def = "-",
  40. },
  41. {
  42. .opt = { "set", no_argument, .val = 's' },
  43. .off = offsetof(jcmd_opt_t, set),
  44. .doc = jcmd_jwk_doc_set,
  45. .set = jcmd_opt_set_flag,
  46. },
  47. {}
  48. };
  49. static void
  50. jcmd_opt_cleanup(jcmd_opt_t *opt)
  51. {
  52. jcmd_file_cleanup(&opt->output);
  53. json_decrefp(&opt->keys);
  54. }
  55. static int
  56. jcmd_jwk_pub(int argc, char *argv[])
  57. {
  58. jcmd_opt_auto_t opt = {};
  59. json_auto_t *out = NULL;
  60. bool fail = false;
  61. if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
  62. return EXIT_FAILURE;
  63. for (size_t i = 0; !fail && i < json_array_size(opt.keys); i++)
  64. fail |= !jose_jwk_pub(NULL, json_array_get(opt.keys, i));
  65. if (fail) {
  66. fprintf(stderr, "Error removing private keys!\n");
  67. return EXIT_FAILURE;
  68. }
  69. switch (json_array_size(opt.keys)) {
  70. case 0:
  71. fprintf(stderr, "MUST specify at least one JWK(Set)!\n");
  72. return EXIT_FAILURE;
  73. case 1:
  74. if (!opt.set) {
  75. out = json_incref(json_array_get(opt.keys, 0));
  76. break;
  77. }
  78. /* fallthrough */
  79. default:
  80. out = json_pack("{s:O}", "keys", opt.keys);
  81. break;
  82. }
  83. if (json_dumpf(out, opt.output, JSON_SORT_KEYS | JSON_COMPACT) < 0) {
  84. fprintf(stderr, "Error dumping JWK(Set)!\n");
  85. return EXIT_FAILURE;
  86. }
  87. if (isatty(fileno(opt.output)))
  88. fprintf(opt.output, "\n");
  89. return EXIT_SUCCESS;
  90. }
  91. JCMD_REGISTER(SUMMARY, jcmd_jwk_pub, "jwk", "pub")