use.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "Validates the permissions of a key for the specified use(s)"
  21. typedef struct {
  22. FILE *output;
  23. json_t *keys;
  24. json_t *uses;
  25. bool req;
  26. bool all;
  27. bool set;
  28. } jcmd_opt_t;
  29. static const char *prefix =
  30. "jose jwk use -i JWK [-a] [-r] -u OP [-o JWK [-s]]\n\n" SUMMARY;
  31. static void
  32. jcmd_opt_cleanup(jcmd_opt_t *opt)
  33. {
  34. jcmd_file_cleanup(&opt->output);
  35. json_decrefp(&opt->keys);
  36. json_decrefp(&opt->uses);
  37. }
  38. static bool
  39. opt_set_use(const jcmd_cfg_t *cfg, void *vopt, const char *arg)
  40. {
  41. json_t **uses = vopt;
  42. if (!*uses)
  43. *uses = json_array();
  44. return json_array_append_new(*uses, json_string(arg)) == 0;
  45. }
  46. static const jcmd_doc_t doc_use[] = {
  47. { .arg = "sign", .doc = "Validate the key for signing" },
  48. { .arg = "verify", .doc = "Validate the key for verifying" },
  49. { .arg = "encrypt", .doc = "Validate the key for encrypting" },
  50. { .arg = "decrypt", .doc = "Validate the key for decrypting" },
  51. { .arg = "wrapKey", .doc = "Validate the key for wrapping" },
  52. { .arg = "unwrapKey", .doc = "Validate the key for unwrapping" },
  53. { .arg = "deriveKey", .doc = "Validate the key for deriving keys" },
  54. { .arg = "deriveBits", .doc = "Validate the key for deriving bits" },
  55. {}
  56. };
  57. static const jcmd_doc_t doc_all[] = {
  58. { .doc = "Succeeds only if all operations are allowed" },
  59. {}
  60. };
  61. static const jcmd_doc_t doc_req[] = {
  62. { .doc = "Operations must be explicitly allowed" },
  63. {}
  64. };
  65. static const jcmd_doc_t doc_output[] = {
  66. { .arg = "FILE", .doc = "Filter keys to FILE as JWK(Set)" },
  67. { .arg = "-", .doc = "Filter keys to standard output as JWK(Set)" },
  68. {}
  69. };
  70. static const jcmd_cfg_t cfgs[] = {
  71. {
  72. .opt = { "input", required_argument, .val = 'i' },
  73. .off = offsetof(jcmd_opt_t, keys),
  74. .set = jcmd_opt_set_jwks,
  75. .doc = jcmd_jwk_doc_input,
  76. },
  77. {
  78. .opt = { "use", required_argument, .val = 'u' },
  79. .off = offsetof(jcmd_opt_t, uses),
  80. .set = opt_set_use,
  81. .doc = doc_use,
  82. },
  83. {
  84. .opt = { "all", no_argument, .val = 'a' },
  85. .off = offsetof(jcmd_opt_t, all),
  86. .set = jcmd_opt_set_flag,
  87. .doc = doc_all,
  88. },
  89. {
  90. .opt = { "required", no_argument, .val = 'r' },
  91. .off = offsetof(jcmd_opt_t, req),
  92. .set = jcmd_opt_set_flag,
  93. .doc = doc_req,
  94. },
  95. {
  96. .opt = { "output", required_argument, .val = 'o' },
  97. .off = offsetof(jcmd_opt_t, output),
  98. .set = jcmd_opt_set_ofile,
  99. .doc = doc_output,
  100. },
  101. {
  102. .opt = { "set", no_argument, .val = 's' },
  103. .off = offsetof(jcmd_opt_t, set),
  104. .doc = jcmd_jwk_doc_set,
  105. .set = jcmd_opt_set_flag,
  106. },
  107. {}
  108. };
  109. static int
  110. jcmd_jwk_use(int argc, char *argv[])
  111. {
  112. jcmd_opt_auto_t opt = {};
  113. json_auto_t *arr = NULL;
  114. if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
  115. return EXIT_FAILURE;
  116. if (json_array_size(opt.uses) == 0) {
  117. fprintf(stderr, "No uses specified!\n");
  118. return EXIT_FAILURE;
  119. }
  120. if (json_array_size(opt.keys) == 0) {
  121. fprintf(stderr, "No JWK specified!\n");
  122. return EXIT_FAILURE;
  123. }
  124. arr = json_array();
  125. if (!arr)
  126. return EXIT_FAILURE;
  127. for (size_t i = 0; i < json_array_size(opt.keys); i++) {
  128. json_t *jwk = json_array_get(opt.keys, i);
  129. bool status = false;
  130. for (size_t j = 0; j < json_array_size(opt.uses); j++) {
  131. const char *use = json_string_value(json_array_get(opt.uses, j));
  132. if (opt.all)
  133. status |= !jose_jwk_prm(NULL, jwk, opt.req, use);
  134. else
  135. status |= jose_jwk_prm(NULL, jwk, opt.req, use);
  136. }
  137. status = opt.all ? !status : status;
  138. if (opt.output) {
  139. if (status && json_array_append(arr, jwk) < 0)
  140. return EXIT_FAILURE;
  141. } else if (!status) {
  142. return EXIT_FAILURE;
  143. }
  144. }
  145. if (opt.output) {
  146. json_auto_t *jwkset = NULL;
  147. switch (json_array_size(arr)) {
  148. case 0: return EXIT_FAILURE;
  149. case 1:
  150. if (!opt.set) {
  151. jwkset = json_incref(json_array_get(arr, 0));
  152. break;
  153. }
  154. /* fallthrough */
  155. default:
  156. jwkset = json_pack("{s:O}", "keys", arr);
  157. break;
  158. }
  159. if (!jwkset)
  160. return EXIT_FAILURE;
  161. if (json_dumpf(jwkset, opt.output, JSON_COMPACT | JSON_SORT_KEYS) < 0)
  162. return EXIT_FAILURE;
  163. if (isatty(fileno(opt.output)))
  164. fprintf(opt.output, "\n");
  165. }
  166. return EXIT_SUCCESS;
  167. }
  168. JCMD_REGISTER(SUMMARY, jcmd_jwk_use, "jwk", "use")