1
0

api_jwe.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include "../lib/hooks.h" /* for MAX_COMPRESSED_SIZE */
  21. static bool
  22. dec_cmp(json_t *jwe, json_t *jwk, const char* expected_data, size_t expected_len)
  23. {
  24. bool ret = false;
  25. char *pt = NULL;
  26. size_t ptl = 0;
  27. pt = jose_jwe_dec(NULL, jwe, NULL, jwk, &ptl);
  28. if (!pt)
  29. goto error;
  30. if (ptl != expected_len)
  31. goto error;
  32. if (strcmp(pt, expected_data) != 0)
  33. goto error;
  34. ret = true;
  35. error:
  36. free(pt);
  37. return ret;
  38. }
  39. static bool
  40. dec(json_t *jwe, json_t *jwk)
  41. {
  42. return dec_cmp(jwe, jwk, "foo", 4);
  43. }
  44. struct zip_test_data_t {
  45. char* data;
  46. size_t datalen;
  47. bool expected;
  48. };
  49. static char*
  50. make_data(size_t len)
  51. {
  52. assert(len > 0);
  53. char *data = malloc(len);
  54. assert(data);
  55. for (size_t i = 0; i < len; i++) {
  56. data[i] = 'A' + (random() % 26);
  57. }
  58. data[len-1] = '\0';
  59. return data;
  60. }
  61. int
  62. main(int argc, char *argv[])
  63. {
  64. json_auto_t *jwke = json_pack("{s:s}", "alg", "ECDH-ES+A128KW");
  65. json_auto_t *jwkr = json_pack("{s:s}", "alg", "RSA1_5");
  66. json_auto_t *jwko = json_pack("{s:s}", "alg", "A128KW");
  67. json_auto_t *jwkz = json_pack("{s:s, s:i}", "kty", "oct", "bytes", 16);
  68. json_auto_t *set0 = json_pack("{s:[O,O]}", "keys", jwke, jwko);
  69. json_auto_t *set1 = json_pack("{s:[O,O]}", "keys", jwkr, jwko);
  70. json_auto_t *set2 = json_pack("{s:[O,O]}", "keys", jwke, jwkr);
  71. json_auto_t *jwe = NULL;
  72. assert(jose_jwk_gen(NULL, jwke));
  73. assert(jose_jwk_gen(NULL, jwkr));
  74. assert(jose_jwk_gen(NULL, jwko));
  75. assert(jose_jwk_gen(NULL, jwkz));
  76. json_decref(jwe);
  77. assert((jwe = json_object()));
  78. assert(jose_jwe_enc(NULL, jwe, NULL, jwke, "foo", 4));
  79. assert(dec(jwe, jwke));
  80. assert(!dec(jwe, jwkr));
  81. assert(!dec(jwe, jwko));
  82. assert(dec(jwe, set0));
  83. assert(!dec(jwe, set1));
  84. assert(dec(jwe, set2));
  85. json_decref(jwe);
  86. assert((jwe = json_object()));
  87. assert(jose_jwe_enc(NULL, jwe, NULL, jwkr, "foo", 4));
  88. assert(!dec(jwe, jwke));
  89. assert(dec(jwe, jwkr));
  90. assert(!dec(jwe, jwko));
  91. assert(!dec(jwe, set0));
  92. assert(dec(jwe, set1));
  93. assert(dec(jwe, set2));
  94. json_decref(jwe);
  95. assert((jwe = json_object()));
  96. assert(jose_jwe_enc(NULL, jwe, NULL, jwko, "foo", 4));
  97. assert(!dec(jwe, jwke));
  98. assert(!dec(jwe, jwkr));
  99. assert(dec(jwe, jwko));
  100. assert(dec(jwe, set0));
  101. assert(dec(jwe, set1));
  102. assert(!dec(jwe, set2));
  103. json_decref(jwe);
  104. assert((jwe = json_object()));
  105. assert(jose_jwe_enc(NULL, jwe, NULL, set0, "foo", 4));
  106. assert(dec(jwe, jwke));
  107. assert(!dec(jwe, jwkr));
  108. assert(dec(jwe, jwko));
  109. assert(dec(jwe, set0));
  110. assert(dec(jwe, set1));
  111. assert(dec(jwe, set2));
  112. json_decref(jwe);
  113. assert((jwe = json_pack("{s:{s:s,s:s,s:s,s:s}}", "protected", "alg", "A128KW", "enc", "A128GCM", "typ", "JWE", "zip", "DEF")));
  114. assert(jose_jwe_enc(NULL, jwe, NULL, jwkz, "foo", 4));
  115. assert(dec(jwe, jwkz));
  116. assert(!dec(jwe, jwkr));
  117. assert(!dec(jwe, jwko));
  118. assert(!dec(jwe, set0));
  119. assert(!dec(jwe, set1));
  120. assert(!dec(jwe, set2));
  121. /* Some tests with "zip": "DEF" */
  122. struct zip_test_data_t zip[] = {
  123. {
  124. .data = make_data(5),
  125. .datalen = 5,
  126. .expected = true,
  127. },
  128. {
  129. .data = make_data(50),
  130. .datalen = 50,
  131. .expected = true,
  132. },
  133. {
  134. .data = make_data(1000),
  135. .datalen = 1000,
  136. .expected = true,
  137. },
  138. {
  139. .data = make_data(10000000),
  140. .datalen = 10000000,
  141. .expected = false, /* compressed len will be ~8000000+
  142. * (i.e. > MAX_COMPRESSED_SIZE)
  143. */
  144. },
  145. {
  146. .data = make_data(50000),
  147. .datalen = 50000,
  148. .expected = true
  149. },
  150. {
  151. .data = NULL
  152. }
  153. };
  154. for (size_t i = 0; zip[i].data != NULL; i++) {
  155. json_decref(jwe);
  156. assert((jwe = json_pack("{s:{s:s,s:s,s:s,s:s}}", "protected", "alg", "A128KW", "enc", "A128GCM", "typ", "JWE", "zip", "DEF")));
  157. assert(jose_jwe_enc(NULL, jwe, NULL, jwkz, zip[i].data, zip[i].datalen));
  158. /* Now let's get the ciphertext compressed len. */
  159. char *ct = NULL;
  160. size_t ctl = 0;
  161. assert(json_unpack(jwe, "{s:s%}", "ciphertext", &ct, &ctl) != -1);
  162. /* And check our expectation is correct. */
  163. assert(zip[i].expected == (ctl < MAX_COMPRESSED_SIZE));
  164. assert(dec_cmp(jwe, jwkz, zip[i].data, zip[i].datalen) == zip[i].expected);
  165. free(zip[i].data);
  166. zip[i].data = NULL;
  167. }
  168. return EXIT_SUCCESS;
  169. }