1
0

exc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <string.h>
  19. #include <unistd.h>
  20. #define SUMMARY "Performs a key exchange using the two input keys"
  21. typedef struct {
  22. FILE *output;
  23. json_t *keys;
  24. json_t *lcl;
  25. json_t *rem;
  26. } jcmd_opt_t;
  27. static const char *prefix =
  28. "jose jwk exc [-i JWK] -l JWK -r JWK [-o JWK]\n\n" SUMMARY;
  29. static const jcmd_doc_t doc_input[] = {
  30. { .arg = "JSON", .doc="Parse JWK template from JSON" },
  31. { .arg = "FILE", .doc="Read JWK template from FILE" },
  32. { .arg = "-", .doc="Read JWK template from standard input" },
  33. {}
  34. };
  35. static const jcmd_doc_t doc_output[] = {
  36. { .arg = "FILE", .doc="Write JWK to FILE" },
  37. { .arg = "-", .doc="Write JWK to standard input" },
  38. {}
  39. };
  40. static const jcmd_doc_t doc_local[] = {
  41. { .arg = "FILE", .doc="Read local JWK from FILE" },
  42. { .arg = "-", .doc="Read local JWK from standard input" },
  43. {}
  44. };
  45. static const jcmd_doc_t doc_remote[] = {
  46. { .arg = "FILE", .doc="Read remote JWK from FILE" },
  47. { .arg = "-", .doc="Read remote JWK from standard input" },
  48. {}
  49. };
  50. static const jcmd_cfg_t cfgs[] = {
  51. {
  52. .opt = { "input", required_argument, .val = 'i' },
  53. .off = offsetof(jcmd_opt_t, keys),
  54. .set = jcmd_opt_set_jwkt,
  55. .doc = doc_input,
  56. .def = "{}",
  57. },
  58. {
  59. .opt = { "output", required_argument, .val = 'o' },
  60. .off = offsetof(jcmd_opt_t, output),
  61. .set = jcmd_opt_set_ofile,
  62. .doc = doc_output,
  63. .def = "-",
  64. },
  65. {
  66. .opt = { "local", required_argument, .val = 'l' },
  67. .off = offsetof(jcmd_opt_t, lcl),
  68. .set = jcmd_opt_set_jwks,
  69. .doc = doc_local,
  70. },
  71. {
  72. .opt = { "remote", required_argument, .val = 'r' },
  73. .off = offsetof(jcmd_opt_t, rem),
  74. .set = jcmd_opt_set_jwks,
  75. .doc = doc_remote,
  76. },
  77. {}
  78. };
  79. static void
  80. jcmd_opt_cleanup(jcmd_opt_t *opt)
  81. {
  82. jcmd_file_cleanup(&opt->output);
  83. json_decrefp(&opt->keys);
  84. json_decrefp(&opt->lcl);
  85. json_decrefp(&opt->rem);
  86. }
  87. static int
  88. jcmd_jwk_exc(int argc, char *argv[])
  89. {
  90. jcmd_opt_auto_t opt = {};
  91. json_auto_t *key = NULL;
  92. json_t *tmpl = NULL;
  93. if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
  94. return EXIT_FAILURE;
  95. if (json_array_size(opt.keys) > 1 && json_array_remove(opt.keys, 0) < 0)
  96. return EXIT_FAILURE;
  97. if (json_array_size(opt.lcl) != 1) {
  98. fprintf(stderr, "Local JWK must be specified exactly once!\n");
  99. return EXIT_FAILURE;
  100. }
  101. if (json_array_size(opt.rem) != 1) {
  102. fprintf(stderr, "Remote JWK must be specified exactly once!\n");
  103. return EXIT_FAILURE;
  104. }
  105. key = jose_jwk_exc(NULL, json_array_get(opt.lcl, 0),
  106. json_array_get(opt.rem, 0));
  107. if (!key) {
  108. fprintf(stderr, "Error performing exchange!\n");
  109. return EXIT_FAILURE;
  110. }
  111. tmpl = json_array_get(opt.keys, json_array_size(opt.keys) - 1);
  112. if (json_object_update(tmpl, key) < 0)
  113. return EXIT_FAILURE;
  114. if (json_dumpf(tmpl, opt.output, JSON_COMPACT | JSON_SORT_KEYS) < 0) {
  115. fprintf(stderr, "Error writing JWK!\n");
  116. return EXIT_FAILURE;
  117. }
  118. if (isatty(fileno(opt.output)))
  119. fprintf(opt.output, "\n");
  120. return EXIT_SUCCESS;
  121. }
  122. JCMD_REGISTER(SUMMARY, jcmd_jwk_exc, "jwk", "exc")