oct.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 bool
  31. jwk_make_execute(jose_cfg_t *cfg, json_t *jwk)
  32. {
  33. uint8_t key[KEYMAX] = {};
  34. json_int_t len = 0;
  35. if (!jwk_make_handles(cfg, jwk))
  36. return false;
  37. if (json_unpack(jwk, "{s:I}", "bytes", &len) < 0)
  38. return false;
  39. if (len > KEYMAX)
  40. return false;
  41. if (RAND_bytes(key, len) <= 0)
  42. return false;
  43. if (json_object_del(jwk, "bytes") < 0)
  44. return false;
  45. if (json_object_set_new(jwk, "k", jose_b64_enc(key, len)) < 0)
  46. return false;
  47. OPENSSL_cleanse(key, len);
  48. return true;
  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. }