api_jwe.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. static bool
  21. dec(json_t *jwe, json_t *jwk)
  22. {
  23. bool ret = false;
  24. char *pt = NULL;
  25. size_t ptl = 0;
  26. pt = jose_jwe_dec(NULL, jwe, NULL, jwk, &ptl);
  27. if (!pt)
  28. goto error;
  29. if (ptl != 4)
  30. goto error;
  31. if (strcmp(pt, "foo") != 0)
  32. goto error;
  33. ret = true;
  34. error:
  35. free(pt);
  36. return ret;
  37. }
  38. int
  39. main(int argc, char *argv[])
  40. {
  41. json_auto_t *jwke = json_pack("{s:s}", "alg", "ECDH-ES+A128KW");
  42. json_auto_t *jwkr = json_pack("{s:s}", "alg", "RSA1_5");
  43. json_auto_t *jwko = json_pack("{s:s}", "alg", "A128KW");
  44. json_auto_t *set0 = json_pack("{s:[O,O]}", "keys", jwke, jwko);
  45. json_auto_t *set1 = json_pack("{s:[O,O]}", "keys", jwkr, jwko);
  46. json_auto_t *set2 = json_pack("{s:[O,O]}", "keys", jwke, jwkr);
  47. json_auto_t *jwe = NULL;
  48. assert(jose_jwk_gen(NULL, jwke));
  49. assert(jose_jwk_gen(NULL, jwkr));
  50. assert(jose_jwk_gen(NULL, jwko));
  51. json_decref(jwe);
  52. assert((jwe = json_object()));
  53. assert(jose_jwe_enc(NULL, jwe, NULL, jwke, "foo", 4));
  54. assert(dec(jwe, jwke));
  55. assert(!dec(jwe, jwkr));
  56. assert(!dec(jwe, jwko));
  57. assert(dec(jwe, set0));
  58. assert(!dec(jwe, set1));
  59. assert(dec(jwe, set2));
  60. json_decref(jwe);
  61. assert((jwe = json_object()));
  62. assert(jose_jwe_enc(NULL, jwe, NULL, jwkr, "foo", 4));
  63. assert(!dec(jwe, jwke));
  64. assert(dec(jwe, jwkr));
  65. assert(!dec(jwe, jwko));
  66. assert(!dec(jwe, set0));
  67. assert(dec(jwe, set1));
  68. assert(dec(jwe, set2));
  69. json_decref(jwe);
  70. assert((jwe = json_object()));
  71. assert(jose_jwe_enc(NULL, jwe, NULL, jwko, "foo", 4));
  72. assert(!dec(jwe, jwke));
  73. assert(!dec(jwe, jwkr));
  74. assert(dec(jwe, jwko));
  75. assert(dec(jwe, set0));
  76. assert(dec(jwe, set1));
  77. assert(!dec(jwe, set2));
  78. json_decref(jwe);
  79. assert((jwe = json_object()));
  80. assert(jose_jwe_enc(NULL, jwe, NULL, set0, "foo", 4));
  81. assert(dec(jwe, jwke));
  82. assert(!dec(jwe, jwkr));
  83. assert(dec(jwe, jwko));
  84. assert(dec(jwe, set0));
  85. assert(dec(jwe, set1));
  86. assert(dec(jwe, set2));
  87. return EXIT_SUCCESS;
  88. }