dir.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "misc.h"
  18. #include "../hooks.h"
  19. #include <string.h>
  20. static const char *
  21. alg_wrap_alg(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  22. {
  23. const char *enc = NULL;
  24. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &enc) == -1)
  25. return NULL;
  26. for (const jose_hook_alg_t *a = jose_hook_alg_list(); a; a = a->next) {
  27. if (a->kind != JOSE_HOOK_ALG_KIND_ENCR)
  28. continue;
  29. if (strcmp(a->name, enc) == 0)
  30. return "dir";
  31. }
  32. return NULL;
  33. }
  34. static const char *
  35. alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  36. {
  37. const char *enc = NULL;
  38. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &enc) == -1)
  39. return NULL;
  40. for (const jose_hook_alg_t *a = jose_hook_alg_list(); a; a = a->next) {
  41. if (a->kind != JOSE_HOOK_ALG_KIND_ENCR)
  42. continue;
  43. if (strcmp(a->name, enc) == 0)
  44. return a->name;
  45. }
  46. return NULL;
  47. }
  48. static bool
  49. copy(json_t *to, const json_t *from)
  50. {
  51. json_auto_t *cpy = NULL;
  52. cpy = json_deep_copy(from);
  53. return json_object_update(to, cpy) == 0;
  54. }
  55. static bool
  56. alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  57. json_t *rcp, const json_t *jwk, json_t *cek)
  58. {
  59. if (!json_object_get(cek, "k") && !copy(cek, jwk))
  60. return false;
  61. if (json_object_set_new(rcp, "encrypted_key", json_string("")) < 0)
  62. return false;
  63. return add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
  64. }
  65. static bool
  66. alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  67. const json_t *rcp, const json_t *jwk, json_t *cek)
  68. {
  69. return copy(cek, jwk);
  70. }
  71. static void __attribute__((constructor))
  72. constructor(void)
  73. {
  74. static jose_hook_alg_t algs[] = {
  75. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  76. .name = "dir",
  77. .wrap.eprm = "encrypt",
  78. .wrap.dprm = "decrypt",
  79. .wrap.alg = alg_wrap_alg,
  80. .wrap.enc = alg_wrap_enc,
  81. .wrap.wrp = alg_wrap_wrp,
  82. .wrap.unw = alg_wrap_unw },
  83. {}
  84. };
  85. for (size_t i = 0; algs[i].name; i++)
  86. jose_hook_alg_push(&algs[i]);
  87. }