1
0

alg_sign.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "../lib/hooks.h"
  18. #include <jose/jose.h>
  19. #include <assert.h>
  20. #include <string.h>
  21. const char *payloads[] = {
  22. "",
  23. "abc",
  24. "aosidmfoasidhtaoirnigaoiurebxlicjnvalsiouerhnoaiusdnvaisudfrhpqowuiefnali",
  25. NULL
  26. };
  27. static void
  28. test(const jose_hook_alg_t *a, const char *pay, json_t *jwk, bool iter)
  29. {
  30. json_auto_t *jwe = json_object();
  31. json_auto_t *rcp = json_object();
  32. jose_io_auto_t *sio = NULL;
  33. jose_io_auto_t *vio = NULL;
  34. sio = a->sign.sig(a, NULL, jwe, rcp, jwk);
  35. assert(sio);
  36. if (iter) {
  37. assert(sio->feed(sio, pay, strlen(pay)));
  38. } else {
  39. for (size_t i = 0; pay[i]; i++)
  40. assert(sio->feed(sio, &pay[i], 1));
  41. }
  42. assert(sio->done(sio));
  43. assert(json_object_get(jwe, "signature"));
  44. vio = a->sign.ver(a, NULL, jwe, jwe, jwk);
  45. assert(vio);
  46. if (iter) {
  47. assert(vio->feed(vio, pay, strlen(pay)));
  48. } else {
  49. for (size_t i = 0; pay[i]; i++)
  50. assert(vio->feed(vio, &pay[i], 1));
  51. }
  52. assert(vio->done(vio));
  53. }
  54. int
  55. main(int argc, char *argv[])
  56. {
  57. for (const jose_hook_alg_t *a = jose_hook_alg_list(); a; a = a->next) {
  58. json_auto_t *jwk = NULL;
  59. if (a->kind != JOSE_HOOK_ALG_KIND_SIGN)
  60. continue;
  61. fprintf(stderr, "alg: %s\n", a->name);
  62. assert((jwk = json_pack("{s:s}", "alg", a->name)));
  63. assert(jose_jwk_gen(NULL, jwk));
  64. for (size_t i = 0; payloads[i]; i++) {
  65. test(a, payloads[i], jwk, false);
  66. test(a, payloads[i], jwk, true);
  67. }
  68. }
  69. return EXIT_SUCCESS;
  70. }