api_jws.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <jose/jose.h>
  18. #include <assert.h>
  19. #include <string.h>
  20. int
  21. main(int argc, char *argv[])
  22. {
  23. json_auto_t *jwke = json_pack("{s:s}", "alg", "ES256");
  24. json_auto_t *jwkr = json_pack("{s:s}", "alg", "RS256");
  25. json_auto_t *jwkh = json_pack("{s:s}", "alg", "HS256");
  26. json_auto_t *set0 = json_pack("{s:[O,O]}", "keys", jwke, jwkh);
  27. json_auto_t *set1 = json_pack("{s:[O,O]}", "keys", jwkr, jwkh);
  28. json_auto_t *set2 = json_pack("{s:[O,O]}", "keys", jwke, jwkr);
  29. json_auto_t *jws = NULL;
  30. assert(jose_jwk_gen(NULL, jwke));
  31. assert(jose_jwk_gen(NULL, jwkr));
  32. assert(jose_jwk_gen(NULL, jwkh));
  33. json_decref(jws);
  34. assert((jws = json_pack("{s:s}", "payload", "foo")));
  35. assert(jose_jws_sig(NULL, jws, NULL, jwke));
  36. assert(jose_jws_ver(NULL, jws, NULL, jwke, false));
  37. assert(jose_jws_ver(NULL, jws, NULL, set0, false));
  38. assert(!jose_jws_ver(NULL, jws, NULL, set0, true));
  39. assert(!jose_jws_ver(NULL, jws, NULL, set1, false));
  40. assert(!jose_jws_ver(NULL, jws, NULL, set1, true));
  41. assert(jose_jws_ver(NULL, jws, NULL, set2, false));
  42. assert(!jose_jws_ver(NULL, jws, NULL, set2, true));
  43. json_decref(jws);
  44. assert((jws = json_pack("{s:s}", "payload", "foo")));
  45. assert(jose_jws_sig(NULL, jws, NULL, jwkr));
  46. assert(jose_jws_ver(NULL, jws, NULL, jwkr, false));
  47. assert(!jose_jws_ver(NULL, jws, NULL, set0, false));
  48. assert(!jose_jws_ver(NULL, jws, NULL, set0, true));
  49. assert(jose_jws_ver(NULL, jws, NULL, set1, false));
  50. assert(!jose_jws_ver(NULL, jws, NULL, set1, true));
  51. assert(jose_jws_ver(NULL, jws, NULL, set2, false));
  52. assert(!jose_jws_ver(NULL, jws, NULL, set2, true));
  53. json_decref(jws);
  54. assert((jws = json_pack("{s:s}", "payload", "foo")));
  55. assert(jose_jws_sig(NULL, jws, NULL, jwkh));
  56. assert(jose_jws_ver(NULL, jws, NULL, jwkh, false));
  57. assert(jose_jws_ver(NULL, jws, NULL, set0, false));
  58. assert(!jose_jws_ver(NULL, jws, NULL, set0, true));
  59. assert(jose_jws_ver(NULL, jws, NULL, set1, false));
  60. assert(!jose_jws_ver(NULL, jws, NULL, set1, true));
  61. assert(!jose_jws_ver(NULL, jws, NULL, set2, false));
  62. assert(!jose_jws_ver(NULL, jws, NULL, set2, true));
  63. json_decref(jws);
  64. assert((jws = json_pack("{s:s}", "payload", "foo")));
  65. assert(jose_jws_sig(NULL, jws, NULL, set0));
  66. assert(jose_jws_ver(NULL, jws, NULL, set0, false));
  67. assert(jose_jws_ver(NULL, jws, NULL, set0, true));
  68. assert(jose_jws_ver(NULL, jws, NULL, set1, false));
  69. assert(!jose_jws_ver(NULL, jws, NULL, set1, true));
  70. assert(jose_jws_ver(NULL, jws, NULL, set2, false));
  71. assert(!jose_jws_ver(NULL, jws, NULL, set2, true));
  72. return EXIT_SUCCESS;
  73. }