fmt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 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 "jws.h"
  18. #include <string.h>
  19. #include <unistd.h>
  20. #define SUMMARY "Converts a JWS between serialization formats"
  21. static const char *prefix =
  22. "jose jws fmt -i JWS [-I PAY] [-o JWS] [-O PAY] [-c]\n\n" SUMMARY;
  23. static const jcmd_cfg_t cfgs[] = {
  24. {
  25. .opt = { "input", required_argument, .val = 'i' },
  26. .set = jcmd_opt_io_set_input,
  27. .doc = jcmd_jws_doc_input,
  28. },
  29. {
  30. .opt = { "detached", required_argument, .val = 'I' },
  31. .off = offsetof(jcmd_opt_io_t, detached),
  32. .set = jcmd_opt_set_ifile,
  33. .doc = jcmd_jws_doc_detached,
  34. },
  35. {
  36. .opt = { "output", required_argument, .val = 'o' },
  37. .off = offsetof(jcmd_opt_io_t, output),
  38. .set = jcmd_opt_set_ofile,
  39. .doc = jcmd_jws_doc_output,
  40. .def = "-",
  41. },
  42. {
  43. .opt = { "detach", required_argument, .val = 'O' },
  44. .off = offsetof(jcmd_opt_io_t, detach),
  45. .set = jcmd_opt_set_ofile,
  46. .doc = jcmd_jws_doc_detach,
  47. },
  48. {
  49. .opt = { "compact", no_argument, .val = 'c' },
  50. .off = offsetof(jcmd_opt_io_t, compact),
  51. .set = jcmd_opt_set_flag,
  52. .doc = jcmd_jws_doc_compact,
  53. },
  54. {}
  55. };
  56. static int
  57. jcmd_jws_fmt(int argc, char *argv[])
  58. {
  59. jcmd_opt_io_auto_t opt = { .fields = jcmd_jws_fields };
  60. jose_io_auto_t *io = NULL;
  61. if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
  62. return EXIT_FAILURE;
  63. io = jcmd_jws_prep_io(&opt, NULL);
  64. if (!io)
  65. return EXIT_FAILURE;
  66. if (opt.compact) {
  67. const jcmd_field_t *f = &jcmd_jws_fields[0];
  68. const char *k = f->name;
  69. const char *v = NULL;
  70. if (json_unpack(opt.obj, "{s:[{s?s}!]}", f->mult, k, &v) < 0 &&
  71. json_unpack(opt.obj, "{s?s}", k, &v) < 0) {
  72. fprintf(stderr, "Input JWS cannot be converted to compact.\n");
  73. return EXIT_FAILURE;
  74. }
  75. fprintf(opt.output, "%s.", v ? v : "");
  76. } else {
  77. fprintf(opt.output, "{");
  78. if (!opt.detach)
  79. fprintf(opt.output, "\"payload\":\"");
  80. }
  81. if (opt.detached || opt.input) {
  82. FILE *f = opt.detached ? opt.detached : opt.input;
  83. for (int c = fgetc(f); c != EOF; c = fgetc(f)) {
  84. uint8_t b = c;
  85. if (!opt.detached && b == '.')
  86. break;
  87. if (!io->feed(io, &b, sizeof(b)))
  88. return EXIT_FAILURE;
  89. }
  90. for (int c = 0; opt.detached && opt.input && c != EOF && c != '.'; )
  91. c = fgetc(opt.input);
  92. } else {
  93. const char *pay = NULL;
  94. size_t payl = 0;
  95. if (json_unpack(opt.obj, "{s?s%}", "payload", &pay, &payl) < 0)
  96. return EXIT_FAILURE;
  97. if (!io->feed(io, pay ? pay : "", payl))
  98. return EXIT_FAILURE;
  99. }
  100. if (!io->done(io))
  101. return EXIT_FAILURE;
  102. if (opt.input) {
  103. if (json_object_set_new(opt.obj, "signature",
  104. jcmd_compact_field(opt.input)) < 0) {
  105. fprintf(stderr, "Error reading last compact field!\n");
  106. return EXIT_FAILURE;
  107. }
  108. }
  109. if (opt.compact) {
  110. const char *v = NULL;
  111. if (json_unpack(opt.obj, "{s:s}", "signature", &v) < 0 &&
  112. json_unpack(opt.obj, "{s:[{s:s}!]}",
  113. "signatures", "signature", &v) < 0) {
  114. fprintf(stderr, "Missing signature parameter!\n");
  115. return EXIT_FAILURE;
  116. }
  117. fprintf(opt.output, ".%s", v);
  118. } else {
  119. if (!opt.detach)
  120. fprintf(opt.output, "\",");
  121. json_dumpf(opt.obj, opt.output,
  122. JSON_EMBED | JSON_COMPACT | JSON_SORT_KEYS);
  123. fprintf(opt.output, "}");
  124. }
  125. if (isatty(fileno(opt.output)))
  126. fprintf(opt.output, "\n");
  127. return EXIT_SUCCESS;
  128. }
  129. JCMD_REGISTER(SUMMARY, jcmd_jws_fmt, "jws", "fmt")