jws.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #pragma once
  18. #include "../jose.h"
  19. #include <string.h>
  20. static const jcmd_field_t jcmd_jws_fields[] = {
  21. { .name = "protected", .mult = "signatures" },
  22. { .name = "payload" },
  23. { .name = "signature", .mult = "signatures" },
  24. {}
  25. };
  26. static const jcmd_doc_t jcmd_jws_doc_input[] = {
  27. { .arg = "JSON", .doc="Parse JWS from JSON" },
  28. { .arg = "FILE", .doc="Read JWS from FILE" },
  29. { .arg = "-", .doc="Read JWS from standard input" },
  30. {}
  31. };
  32. static const jcmd_doc_t jcmd_jws_doc_detached[] = {
  33. { .arg = "FILE", .doc="Read decoded payload from FILE" },
  34. { .arg = "-", .doc="Read decoded payload from standard input" },
  35. {}
  36. };
  37. static const jcmd_doc_t jcmd_jws_doc_output[] = {
  38. { .arg = "FILE", .doc="Write JWS to FILE" },
  39. { .arg = "-", .doc="Write JWS to stdout (default)" },
  40. {}
  41. };
  42. static const jcmd_doc_t jcmd_jws_doc_detach[] = {
  43. { .arg = "FILE", .doc="Detach payload and decode to FILE" },
  44. { .arg = "-", .doc="Detach payload and decode to standard output" },
  45. {}
  46. };
  47. static const jcmd_doc_t jcmd_jws_doc_compact[] = {
  48. { .doc="Output JWS using compact serialization" },
  49. {}
  50. };
  51. static void
  52. jcmd_jws_ios_auto(jose_io_t ***iosp)
  53. {
  54. jose_io_t **ios = *iosp;
  55. for (size_t i = 0; ios && ios[i]; i++)
  56. jose_io_auto(&ios[i]);
  57. }
  58. static jose_io_t *
  59. jcmd_jws_prep_io(jcmd_opt_io_t *opt, jose_io_t *io)
  60. {
  61. jose_io_t __attribute__((cleanup(jcmd_jws_ios_auto))) **ios = NULL;
  62. size_t i = 0;
  63. ios = alloca(sizeof(*ios) * 3);
  64. memset(ios, 0, sizeof(*ios) * 3);
  65. if (io)
  66. ios[i++] = io;
  67. if (opt->detach) {
  68. jose_io_auto_t *b64 = NULL;
  69. ios[i] = jose_io_file(NULL, opt->detach);
  70. if (!ios[i])
  71. return NULL;
  72. b64 = jose_b64_dec_io(ios[i]);
  73. if (!b64)
  74. return NULL;
  75. jose_io_auto(&ios[i]);
  76. ios[i] = jose_io_incref(b64);
  77. } else if (opt->output) {
  78. ios[i] = jose_io_file(NULL, opt->output);
  79. if (!ios[i])
  80. return NULL;
  81. }
  82. for (i = 0; opt->detached && ios[i]; i++) {
  83. jose_io_auto_t *b64 = NULL;
  84. b64 = jose_b64_enc_io(ios[i]);
  85. if (!b64)
  86. return NULL;
  87. jose_io_decref(ios[i]);
  88. ios[i] = jose_io_incref(b64);
  89. }
  90. return jose_io_multiplex(NULL, ios, true);
  91. }