exdatatest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <openssl/crypto.h>
  13. #include "testutil.h"
  14. static long saved_argl;
  15. static void *saved_argp;
  16. static int saved_idx;
  17. static int saved_idx2;
  18. static int saved_idx3;
  19. static int gbl_result;
  20. /*
  21. * SIMPLE EX_DATA IMPLEMENTATION
  22. * Apps explicitly set/get ex_data as needed
  23. */
  24. static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  25. int idx, long argl, void *argp)
  26. {
  27. if (!TEST_int_eq(idx, saved_idx)
  28. || !TEST_long_eq(argl, saved_argl)
  29. || !TEST_ptr_eq(argp, saved_argp)
  30. || !TEST_ptr_null(ptr))
  31. gbl_result = 0;
  32. }
  33. static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  34. void **from_d, int idx, long argl, void *argp)
  35. {
  36. if (!TEST_int_eq(idx, saved_idx)
  37. || !TEST_long_eq(argl, saved_argl)
  38. || !TEST_ptr_eq(argp, saved_argp)
  39. || !TEST_ptr(from_d))
  40. gbl_result = 0;
  41. return 1;
  42. }
  43. static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  44. int idx, long argl, void *argp)
  45. {
  46. if (!TEST_int_eq(idx, saved_idx)
  47. || !TEST_long_eq(argl, saved_argl)
  48. || !TEST_ptr_eq(argp, saved_argp))
  49. gbl_result = 0;
  50. }
  51. /*
  52. * PRE-ALLOCATED EX_DATA IMPLEMENTATION
  53. * Extended data structure is allocated in exnew2/freed in exfree2
  54. * Data is stored inside extended data structure
  55. */
  56. typedef struct myobj_ex_data_st {
  57. char *hello;
  58. int new;
  59. int dup;
  60. } MYOBJ_EX_DATA;
  61. static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  62. int idx, long argl, void *argp)
  63. {
  64. MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data));
  65. if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
  66. || !TEST_long_eq(argl, saved_argl)
  67. || !TEST_ptr_eq(argp, saved_argp)
  68. || !TEST_ptr_null(ptr)
  69. || !TEST_ptr(ex_data)
  70. || !TEST_true(CRYPTO_set_ex_data(ad, idx, ex_data))) {
  71. gbl_result = 0;
  72. OPENSSL_free(ex_data);
  73. } else {
  74. ex_data->new = 1;
  75. }
  76. }
  77. static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  78. void **from_d, int idx, long argl, void *argp)
  79. {
  80. MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
  81. MYOBJ_EX_DATA *ex_data = NULL;
  82. if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
  83. || !TEST_long_eq(argl, saved_argl)
  84. || !TEST_ptr_eq(argp, saved_argp)
  85. || !TEST_ptr(from_d)
  86. || !TEST_ptr(*update_ex_data)
  87. || !TEST_ptr(ex_data = CRYPTO_get_ex_data(to, idx))
  88. || !TEST_true(ex_data->new)) {
  89. gbl_result = 0;
  90. } else {
  91. /* Copy hello over */
  92. ex_data->hello = (*update_ex_data)->hello;
  93. /* indicate this is a dup */
  94. ex_data->dup = 1;
  95. /* Keep my original ex_data */
  96. *update_ex_data = ex_data;
  97. }
  98. return 1;
  99. }
  100. static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  101. int idx, long argl, void *argp)
  102. {
  103. MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, idx);
  104. if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
  105. || !TEST_long_eq(argl, saved_argl)
  106. || !TEST_ptr_eq(argp, saved_argp)
  107. || !TEST_true(CRYPTO_set_ex_data(ad, idx, NULL)))
  108. gbl_result = 0;
  109. OPENSSL_free(ex_data);
  110. }
  111. typedef struct myobj_st {
  112. CRYPTO_EX_DATA ex_data;
  113. int id;
  114. int st;
  115. } MYOBJ;
  116. static MYOBJ *MYOBJ_new(void)
  117. {
  118. static int count = 0;
  119. MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
  120. if (obj != NULL) {
  121. obj->id = ++count;
  122. obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
  123. }
  124. return obj;
  125. }
  126. static void MYOBJ_sethello(MYOBJ *obj, char *cp)
  127. {
  128. obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
  129. if (!TEST_int_eq(obj->st, 1))
  130. gbl_result = 0;
  131. }
  132. static char *MYOBJ_gethello(MYOBJ *obj)
  133. {
  134. return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
  135. }
  136. static void MYOBJ_sethello2(MYOBJ *obj, char *cp)
  137. {
  138. MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
  139. if (TEST_ptr(ex_data))
  140. ex_data->hello = cp;
  141. else
  142. obj->st = gbl_result = 0;
  143. }
  144. static char *MYOBJ_gethello2(MYOBJ *obj)
  145. {
  146. MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
  147. if (TEST_ptr(ex_data))
  148. return ex_data->hello;
  149. obj->st = gbl_result = 0;
  150. return NULL;
  151. }
  152. static void MYOBJ_allochello3(MYOBJ *obj, char *cp)
  153. {
  154. MYOBJ_EX_DATA* ex_data = NULL;
  155. if (TEST_ptr_null(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3))
  156. && TEST_true(CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_APP, obj,
  157. &obj->ex_data, saved_idx3))
  158. && TEST_ptr(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3)))
  159. ex_data->hello = cp;
  160. else
  161. obj->st = gbl_result = 0;
  162. }
  163. static char *MYOBJ_gethello3(MYOBJ *obj)
  164. {
  165. MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3);
  166. if (TEST_ptr(ex_data))
  167. return ex_data->hello;
  168. obj->st = gbl_result = 0;
  169. return NULL;
  170. }
  171. static void MYOBJ_free(MYOBJ *obj)
  172. {
  173. if (obj != NULL) {
  174. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
  175. OPENSSL_free(obj);
  176. }
  177. }
  178. static MYOBJ *MYOBJ_dup(MYOBJ *in)
  179. {
  180. MYOBJ *obj = MYOBJ_new();
  181. if (obj != NULL)
  182. obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
  183. &in->ex_data);
  184. return obj;
  185. }
  186. static int test_exdata(void)
  187. {
  188. MYOBJ *t1 = NULL, *t2 = NULL, *t3 = NULL;
  189. MYOBJ_EX_DATA *ex_data = NULL;
  190. const char *cp;
  191. char *p;
  192. int res = 0;
  193. gbl_result = 1;
  194. if (!TEST_ptr(p = OPENSSL_strdup("hello world")))
  195. return 0;
  196. saved_argl = 21;
  197. if (!TEST_ptr(saved_argp = OPENSSL_malloc(1)))
  198. goto err;
  199. saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  200. saved_argl, saved_argp,
  201. exnew, exdup, exfree);
  202. saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  203. saved_argl, saved_argp,
  204. exnew2, exdup2, exfree2);
  205. t1 = MYOBJ_new();
  206. t2 = MYOBJ_new();
  207. if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
  208. goto err;
  209. if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
  210. goto err;
  211. /*
  212. * saved_idx3 differs from other indexes by being created after the exdata
  213. * was initialized.
  214. */
  215. saved_idx3 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  216. saved_argl, saved_argp,
  217. exnew2, exdup2, exfree2);
  218. if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3)))
  219. goto err;
  220. MYOBJ_sethello(t1, p);
  221. cp = MYOBJ_gethello(t1);
  222. if (!TEST_ptr_eq(cp, p))
  223. goto err;
  224. MYOBJ_sethello2(t1, p);
  225. cp = MYOBJ_gethello2(t1);
  226. if (!TEST_ptr_eq(cp, p))
  227. goto err;
  228. MYOBJ_allochello3(t1, p);
  229. cp = MYOBJ_gethello3(t1);
  230. if (!TEST_ptr_eq(cp, p))
  231. goto err;
  232. cp = MYOBJ_gethello(t2);
  233. if (!TEST_ptr_null(cp))
  234. goto err;
  235. cp = MYOBJ_gethello2(t2);
  236. if (!TEST_ptr_null(cp))
  237. goto err;
  238. t3 = MYOBJ_dup(t1);
  239. if (!TEST_int_eq(t3->st, 1))
  240. goto err;
  241. ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
  242. if (!TEST_ptr(ex_data))
  243. goto err;
  244. if (!TEST_int_eq(ex_data->dup, 1))
  245. goto err;
  246. cp = MYOBJ_gethello(t3);
  247. if (!TEST_ptr_eq(cp, p))
  248. goto err;
  249. cp = MYOBJ_gethello2(t3);
  250. if (!TEST_ptr_eq(cp, p))
  251. goto err;
  252. cp = MYOBJ_gethello3(t3);
  253. if (!TEST_ptr_eq(cp, p))
  254. goto err;
  255. if (gbl_result)
  256. res = 1;
  257. err:
  258. MYOBJ_free(t1);
  259. MYOBJ_free(t2);
  260. MYOBJ_free(t3);
  261. OPENSSL_free(saved_argp);
  262. saved_argp = NULL;
  263. OPENSSL_free(p);
  264. return res;
  265. }
  266. int setup_tests(void)
  267. {
  268. ADD_TEST(test_exdata);
  269. return 1;
  270. }