oct.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <jose/b64.h>
  19. #include "../hooks.h"
  20. #include <string.h>
  21. #include <openssl/rand.h>
  22. static bool
  23. jwk_make_handles(jose_cfg_t *cfg, const json_t *jwk)
  24. {
  25. const char *kty = NULL;
  26. if (json_unpack((json_t *) jwk, "{s:s}", "kty", &kty) < 0)
  27. return false;
  28. return strcmp(kty, "oct") == 0;
  29. }
  30. static json_t *
  31. jwk_make_execute(jose_cfg_t *cfg, const json_t *jwk)
  32. {
  33. uint8_t key[KEYMAX] = {};
  34. json_int_t len = 0;
  35. json_t *ret = NULL;
  36. if (!jwk_make_handles(cfg, jwk))
  37. return NULL;
  38. if (json_unpack((json_t *) jwk, "{s:I}", "bytes", &len) < 0)
  39. return NULL;
  40. if (len > KEYMAX)
  41. return NULL;
  42. if (RAND_bytes(key, len) > 0) {
  43. ret = json_pack("{s:[s],s:{s:o}}",
  44. "del", "bytes",
  45. "upd", "k", jose_b64_enc(key, len));
  46. }
  47. OPENSSL_cleanse(key, len);
  48. return ret;
  49. }
  50. static void __attribute__((constructor))
  51. constructor(void)
  52. {
  53. static jose_hook_jwk_t jwk = {
  54. .kind = JOSE_HOOK_JWK_KIND_MAKE,
  55. .make.handles = jwk_make_handles,
  56. .make.execute = jwk_make_execute
  57. };
  58. jose_hook_jwk_push(&jwk);
  59. }