2
0

asn1_encode_test.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * Copyright 2017-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 <openssl/rand.h>
  12. #include <openssl/asn1t.h>
  13. #include "internal/numbers.h"
  14. #include "testutil.h"
  15. #ifdef __GNUC__
  16. # pragma GCC diagnostic ignored "-Wunused-function"
  17. # pragma GCC diagnostic ignored "-Wformat"
  18. #endif
  19. #ifdef __clang__
  20. # pragma clang diagnostic ignored "-Wunused-function"
  21. # pragma clang diagnostic ignored "-Wformat"
  22. #endif
  23. /***** Custom test data ******************************************************/
  24. /*
  25. * We conduct tests with these arrays for every type we try out.
  26. * You will find the expected results together with the test structures
  27. * for each type, further down.
  28. */
  29. static unsigned char t_zero[] = {
  30. 0x00
  31. };
  32. static unsigned char t_one[] = {
  33. 0x01
  34. };
  35. static unsigned char t_one_neg[] = {
  36. 0xff
  37. };
  38. static unsigned char t_minus_256[] = {
  39. 0xff, 0x00
  40. };
  41. static unsigned char t_longundef[] = {
  42. 0x7f, 0xff, 0xff, 0xff
  43. };
  44. static unsigned char t_9bytes_1[] = {
  45. 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  46. };
  47. static unsigned char t_8bytes_1[] = {
  48. 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  49. };
  50. static unsigned char t_8bytes_2[] = {
  51. 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  52. };
  53. static unsigned char t_8bytes_3_pad[] = {
  54. 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  55. };
  56. static unsigned char t_8bytes_4_neg[] = {
  57. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  58. };
  59. static unsigned char t_8bytes_5_negpad[] = {
  60. 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  61. };
  62. /* 32-bit long */
  63. static unsigned char t_5bytes_1[] = {
  64. 0x01, 0xff, 0xff, 0xff, 0xff
  65. };
  66. static unsigned char t_4bytes_1[] = {
  67. 0x00, 0x80, 0x00, 0x00, 0x00
  68. };
  69. /* We make the last byte 0xfe to avoid a clash with ASN1_LONG_UNDEF */
  70. static unsigned char t_4bytes_2[] = {
  71. 0x7f, 0xff, 0xff, 0xfe
  72. };
  73. static unsigned char t_4bytes_3_pad[] = {
  74. 0x00, 0x7f, 0xff, 0xff, 0xfe
  75. };
  76. static unsigned char t_4bytes_4_neg[] = {
  77. 0x80, 0x00, 0x00, 0x00
  78. };
  79. static unsigned char t_4bytes_5_negpad[] = {
  80. 0xff, 0x80, 0x00, 0x00, 0x00
  81. };
  82. typedef struct {
  83. unsigned char *bytes1;
  84. size_t nbytes1;
  85. unsigned char *bytes2;
  86. size_t nbytes2;
  87. } TEST_CUSTOM_DATA;
  88. #define CUSTOM_DATA(v) \
  89. { v, sizeof(v), t_one, sizeof(t_one) }, \
  90. { t_one, sizeof(t_one), v, sizeof(v) }
  91. static TEST_CUSTOM_DATA test_custom_data[] = {
  92. CUSTOM_DATA(t_zero),
  93. CUSTOM_DATA(t_longundef),
  94. CUSTOM_DATA(t_one),
  95. CUSTOM_DATA(t_one_neg),
  96. CUSTOM_DATA(t_minus_256),
  97. CUSTOM_DATA(t_9bytes_1),
  98. CUSTOM_DATA(t_8bytes_1),
  99. CUSTOM_DATA(t_8bytes_2),
  100. CUSTOM_DATA(t_8bytes_3_pad),
  101. CUSTOM_DATA(t_8bytes_4_neg),
  102. CUSTOM_DATA(t_8bytes_5_negpad),
  103. CUSTOM_DATA(t_5bytes_1),
  104. CUSTOM_DATA(t_4bytes_1),
  105. CUSTOM_DATA(t_4bytes_2),
  106. CUSTOM_DATA(t_4bytes_3_pad),
  107. CUSTOM_DATA(t_4bytes_4_neg),
  108. CUSTOM_DATA(t_4bytes_5_negpad),
  109. };
  110. /***** Type specific test data ***********************************************/
  111. /*
  112. * First, a few utility things that all type specific data can use, or in some
  113. * cases, MUST use.
  114. */
  115. /*
  116. * For easy creation of arrays of expected data. These macros correspond to
  117. * the uses of CUSTOM_DATA above.
  118. */
  119. #define CUSTOM_EXPECTED_SUCCESS(num, znum) \
  120. { 0xff, num, 1 }, \
  121. { 0xff, 1, znum }
  122. #define CUSTOM_EXPECTED_FAILURE \
  123. { 0, 0, 0 }, \
  124. { 0, 0, 0 }
  125. /*
  126. * A structure to collect all test information in. There MUST be one instance
  127. * of this for each test
  128. */
  129. typedef int i2d_fn(void *a, unsigned char **pp);
  130. typedef void *d2i_fn(void **a, unsigned char **pp, long length);
  131. typedef void ifree_fn(void *a);
  132. typedef struct {
  133. ASN1_ITEM_EXP *asn1_type;
  134. const char *name;
  135. int skip; /* 1 if this package should be skipped */
  136. /* An array of structures to compare decoded custom data with */
  137. void *encode_expectations;
  138. size_t encode_expectations_size;
  139. size_t encode_expectations_elem_size;
  140. /*
  141. * An array of structures that are encoded into a DER blob, which is
  142. * then decoded, and result gets compared with the original.
  143. */
  144. void *encdec_data;
  145. size_t encdec_data_size;
  146. size_t encdec_data_elem_size;
  147. /* The i2d function to use with this type */
  148. i2d_fn *i2d;
  149. /* The d2i function to use with this type */
  150. d2i_fn *d2i;
  151. /* Function to free a decoded structure */
  152. ifree_fn *ifree;
  153. } TEST_PACKAGE;
  154. /* To facilitate the creation of an encdec_data array */
  155. #define ENCDEC_DATA(num, znum) \
  156. { 0xff, num, 1 }, { 0xff, 1, znum }
  157. #define ENCDEC_ARRAY(max, zmax, min, zmin) \
  158. ENCDEC_DATA(max,zmax), \
  159. ENCDEC_DATA(min,zmin), \
  160. ENCDEC_DATA(1, 1), \
  161. ENCDEC_DATA(-1, -1), \
  162. ENCDEC_DATA(0, ASN1_LONG_UNDEF)
  163. #ifndef OPENSSL_NO_DEPRECATED_3_0
  164. /***** LONG ******************************************************************/
  165. typedef struct {
  166. /* If decoding is expected to succeed, set this to 1, otherwise 0 */
  167. ASN1_BOOLEAN success;
  168. long test_long;
  169. long test_zlong;
  170. } ASN1_LONG_DATA;
  171. ASN1_SEQUENCE(ASN1_LONG_DATA) = {
  172. ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_BOOLEAN),
  173. ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG),
  174. ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0)
  175. } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
  176. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
  177. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
  178. static ASN1_LONG_DATA long_expected_32bit[] = {
  179. /* The following should fail on the second because it's the default */
  180. { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
  181. { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
  182. CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
  183. CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
  184. CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
  185. CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
  186. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
  187. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
  188. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
  189. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
  190. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
  191. CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
  192. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
  193. CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
  194. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
  195. CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
  196. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
  197. };
  198. static ASN1_LONG_DATA long_encdec_data_32bit[] = {
  199. ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN),
  200. /* Check that default numbers fail */
  201. { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
  202. };
  203. static TEST_PACKAGE long_test_package_32bit = {
  204. ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4,
  205. long_expected_32bit,
  206. sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]),
  207. long_encdec_data_32bit,
  208. sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]),
  209. (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
  210. (ifree_fn *)ASN1_LONG_DATA_free
  211. };
  212. static ASN1_LONG_DATA long_expected_64bit[] = {
  213. /* The following should fail on the second because it's the default */
  214. { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
  215. { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
  216. CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
  217. CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
  218. CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
  219. CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
  220. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
  221. CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */
  222. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
  223. CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), /* t_8bytes_4_neg */
  224. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
  225. CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), /* t_5bytes_1 */
  226. CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), /* t_4bytes_1 */
  227. CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
  228. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
  229. CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
  230. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
  231. };
  232. static ASN1_LONG_DATA long_encdec_data_64bit[] = {
  233. ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN),
  234. /* Check that default numbers fail */
  235. { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
  236. };
  237. static TEST_PACKAGE long_test_package_64bit = {
  238. ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8,
  239. long_expected_64bit,
  240. sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]),
  241. long_encdec_data_64bit,
  242. sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]),
  243. (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
  244. (ifree_fn *)ASN1_LONG_DATA_free
  245. };
  246. #endif
  247. /***** INT32 *****************************************************************/
  248. typedef struct {
  249. ASN1_BOOLEAN success;
  250. int32_t test_int32;
  251. int32_t test_zint32;
  252. } ASN1_INT32_DATA;
  253. ASN1_SEQUENCE(ASN1_INT32_DATA) = {
  254. ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_BOOLEAN),
  255. ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
  256. ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
  257. } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
  258. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
  259. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
  260. static ASN1_INT32_DATA int32_expected[] = {
  261. CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
  262. CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
  263. CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
  264. CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
  265. CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
  266. CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
  267. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
  268. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
  269. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
  270. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
  271. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
  272. CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
  273. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
  274. CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
  275. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
  276. CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
  277. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
  278. };
  279. static ASN1_INT32_DATA int32_encdec_data[] = {
  280. ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
  281. };
  282. static TEST_PACKAGE int32_test_package = {
  283. ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0,
  284. int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]),
  285. int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]),
  286. (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA,
  287. (ifree_fn *)ASN1_INT32_DATA_free
  288. };
  289. /***** UINT32 ****************************************************************/
  290. typedef struct {
  291. ASN1_BOOLEAN success;
  292. uint32_t test_uint32;
  293. uint32_t test_zuint32;
  294. } ASN1_UINT32_DATA;
  295. ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
  296. ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_BOOLEAN),
  297. ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
  298. ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
  299. } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
  300. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
  301. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
  302. static ASN1_UINT32_DATA uint32_expected[] = {
  303. CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
  304. CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
  305. CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
  306. CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
  307. CUSTOM_EXPECTED_FAILURE, /* t_minus_256 (illegal negative value) */
  308. CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
  309. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
  310. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
  311. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
  312. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
  313. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
  314. CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
  315. CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
  316. CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
  317. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
  318. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
  319. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
  320. };
  321. static ASN1_UINT32_DATA uint32_encdec_data[] = {
  322. ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0),
  323. };
  324. static TEST_PACKAGE uint32_test_package = {
  325. ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0,
  326. uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]),
  327. uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]),
  328. (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA,
  329. (ifree_fn *)ASN1_UINT32_DATA_free
  330. };
  331. /***** INT64 *****************************************************************/
  332. typedef struct {
  333. ASN1_BOOLEAN success;
  334. int64_t test_int64;
  335. int64_t test_zint64;
  336. } ASN1_INT64_DATA;
  337. ASN1_SEQUENCE(ASN1_INT64_DATA) = {
  338. ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_BOOLEAN),
  339. ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
  340. ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
  341. } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
  342. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
  343. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
  344. static ASN1_INT64_DATA int64_expected[] = {
  345. CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
  346. CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
  347. CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
  348. CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
  349. CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
  350. CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
  351. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 (too large positive) */
  352. CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
  353. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
  354. CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */
  355. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
  356. CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */
  357. CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
  358. CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
  359. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
  360. CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
  361. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
  362. };
  363. static ASN1_INT64_DATA int64_encdec_data[] = {
  364. ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN),
  365. ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
  366. };
  367. static TEST_PACKAGE int64_test_package = {
  368. ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0,
  369. int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]),
  370. int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]),
  371. (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA,
  372. (ifree_fn *)ASN1_INT64_DATA_free
  373. };
  374. /***** UINT64 ****************************************************************/
  375. typedef struct {
  376. ASN1_BOOLEAN success;
  377. uint64_t test_uint64;
  378. uint64_t test_zuint64;
  379. } ASN1_UINT64_DATA;
  380. ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
  381. ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_BOOLEAN),
  382. ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
  383. ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
  384. } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
  385. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
  386. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
  387. static ASN1_UINT64_DATA uint64_expected[] = {
  388. CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
  389. CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
  390. CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
  391. CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
  392. CUSTOM_EXPECTED_FAILURE, /* t_minus_256 (illegal negative value) */
  393. CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
  394. CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1),
  395. /* t_8bytes_1 */
  396. CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
  397. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
  398. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
  399. CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
  400. CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */
  401. CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
  402. CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
  403. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
  404. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
  405. CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
  406. };
  407. static ASN1_UINT64_DATA uint64_encdec_data[] = {
  408. ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0),
  409. };
  410. static TEST_PACKAGE uint64_test_package = {
  411. ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0,
  412. uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]),
  413. uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]),
  414. (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA,
  415. (ifree_fn *)ASN1_UINT64_DATA_free
  416. };
  417. /***** General testing functions *********************************************/
  418. /* Template structure to map onto any test data structure */
  419. typedef struct {
  420. ASN1_BOOLEAN success;
  421. unsigned char bytes[1]; /* In reality, there's more */
  422. } EXPECTED;
  423. /*
  424. * do_decode returns a tristate:
  425. *
  426. * -1 Couldn't decode
  427. * 0 decoded structure wasn't what was expected (failure)
  428. * 1 decoded structure was what was expected (success)
  429. */
  430. static int do_decode(unsigned char *bytes, long nbytes,
  431. const EXPECTED *expected, size_t expected_size,
  432. const TEST_PACKAGE *package)
  433. {
  434. EXPECTED *enctst = NULL;
  435. const unsigned char *start;
  436. int ret = 0;
  437. start = bytes;
  438. enctst = package->d2i(NULL, &bytes, nbytes);
  439. if (enctst == NULL) {
  440. if (expected->success == 0) {
  441. ret = 1;
  442. ERR_clear_error();
  443. } else {
  444. ret = -1;
  445. }
  446. } else {
  447. if (start + nbytes == bytes
  448. && memcmp(enctst, expected, expected_size) == 0)
  449. ret = 1;
  450. else
  451. ret = 0;
  452. }
  453. package->ifree(enctst);
  454. return ret;
  455. }
  456. /*
  457. * do_encode returns a tristate:
  458. *
  459. * -1 Couldn't encode
  460. * 0 encoded DER wasn't what was expected (failure)
  461. * 1 encoded DER was what was expected (success)
  462. */
  463. static int do_encode(EXPECTED *input,
  464. const unsigned char *expected, size_t expected_len,
  465. const TEST_PACKAGE *package)
  466. {
  467. unsigned char *data = NULL;
  468. int len;
  469. int ret = 0;
  470. len = package->i2d(input, &data);
  471. if (len < 0)
  472. return -1;
  473. if ((size_t)len != expected_len
  474. || memcmp(data, expected, expected_len) != 0) {
  475. if (input->success == 0) {
  476. ret = 1;
  477. ERR_clear_error();
  478. } else {
  479. ret = 0;
  480. }
  481. } else {
  482. ret = 1;
  483. }
  484. OPENSSL_free(data);
  485. return ret;
  486. }
  487. /* Do an encode/decode round trip */
  488. static int do_enc_dec(EXPECTED *bytes, long nbytes,
  489. const TEST_PACKAGE *package)
  490. {
  491. unsigned char *data = NULL;
  492. int len;
  493. int ret = 0;
  494. void *p = bytes;
  495. len = package->i2d(p, &data);
  496. if (len < 0)
  497. return -1;
  498. ret = do_decode(data, len, bytes, nbytes, package);
  499. OPENSSL_free(data);
  500. return ret;
  501. }
  502. static size_t der_encode_length(size_t len, unsigned char **pp)
  503. {
  504. size_t lenbytes;
  505. OPENSSL_assert(len < 0x8000);
  506. if (len > 255)
  507. lenbytes = 3;
  508. else if (len > 127)
  509. lenbytes = 2;
  510. else
  511. lenbytes = 1;
  512. if (pp != NULL) {
  513. if (lenbytes == 1) {
  514. *(*pp)++ = (unsigned char)len;
  515. } else {
  516. *(*pp)++ = (unsigned char)(lenbytes - 1);
  517. if (lenbytes == 2) {
  518. *(*pp)++ = (unsigned char)(0x80 | len);
  519. } else {
  520. *(*pp)++ = (unsigned char)(0x80 | (len >> 8));
  521. *(*pp)++ = (unsigned char)(len);
  522. }
  523. }
  524. }
  525. return lenbytes;
  526. }
  527. static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data,
  528. unsigned char **encoding, int explicit_default)
  529. {
  530. size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes;
  531. const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff };
  532. unsigned char *p = NULL;
  533. size_t i;
  534. /*
  535. * The first item is just an INTEGER tag, INTEGER length and INTEGER content
  536. */
  537. firstbytes =
  538. 1 + der_encode_length(custom_data->nbytes1, NULL)
  539. + custom_data->nbytes1;
  540. for (i = custom_data->nbytes2; i > 0; i--) {
  541. if (custom_data->bytes2[i - 1] != '\0')
  542. break;
  543. }
  544. if (explicit_default || i > 0) {
  545. /*
  546. * The second item is an explicit tag, content length, INTEGER tag,
  547. * INTEGER length, INTEGER bytes
  548. */
  549. secondbytesinner =
  550. 1 + der_encode_length(custom_data->nbytes2, NULL)
  551. + custom_data->nbytes2;
  552. secondbytes =
  553. 1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner;
  554. }
  555. /*
  556. * The whole sequence is the sequence tag, content length, BOOLEAN true
  557. * (copied from t_true), the first (firstbytes) and second (secondbytes)
  558. * items
  559. */
  560. seqbytes =
  561. 1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL)
  562. + sizeof(t_true) + firstbytes + secondbytes;
  563. *encoding = p = OPENSSL_malloc(seqbytes);
  564. if (*encoding == NULL)
  565. return 0;
  566. /* Sequence tag */
  567. *p++ = 0x30;
  568. der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p);
  569. /* ASN1_BOOLEAN TRUE */
  570. memcpy(p, t_true, sizeof(t_true)); /* Marks decoding success */
  571. p += sizeof(t_true);
  572. /* First INTEGER item (non-optional) */
  573. *p++ = V_ASN1_INTEGER;
  574. der_encode_length(custom_data->nbytes1, &p);
  575. memcpy(p, custom_data->bytes1, custom_data->nbytes1);
  576. p += custom_data->nbytes1;
  577. if (secondbytes > 0) {
  578. /* Second INTEGER item (optional) */
  579. /* Start with the explicit optional tag */
  580. *p++ = 0xa0;
  581. der_encode_length(secondbytesinner, &p);
  582. *p++ = V_ASN1_INTEGER;
  583. der_encode_length(custom_data->nbytes2, &p);
  584. memcpy(p, custom_data->bytes2, custom_data->nbytes2);
  585. p += custom_data->nbytes2;
  586. }
  587. OPENSSL_assert(seqbytes == (size_t)(p - *encoding));
  588. return seqbytes;
  589. }
  590. /* Attempt to decode a custom encoding of the test structure */
  591. static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data,
  592. const EXPECTED *expected, size_t expected_size,
  593. const TEST_PACKAGE *package)
  594. {
  595. unsigned char *encoding = NULL;
  596. /*
  597. * We force the defaults to be explicitly encoded to make sure we test
  598. * for defaults that shouldn't be present (i.e. we check for failure)
  599. */
  600. size_t encoding_length = make_custom_der(custom_data, &encoding, 1);
  601. int ret;
  602. if (encoding_length == 0)
  603. return -1;
  604. ret = do_decode(encoding, encoding_length, expected, expected_size,
  605. package);
  606. OPENSSL_free(encoding);
  607. return ret;
  608. }
  609. /* Attempt to encode the test structure and compare it to custom DER */
  610. static int do_encode_custom(EXPECTED *input,
  611. const TEST_CUSTOM_DATA *custom_data,
  612. const TEST_PACKAGE *package)
  613. {
  614. unsigned char *expected = NULL;
  615. size_t expected_length = make_custom_der(custom_data, &expected, 0);
  616. int ret;
  617. if (expected_length == 0)
  618. return -1;
  619. ret = do_encode(input, expected, expected_length, package);
  620. OPENSSL_free(expected);
  621. return ret;
  622. }
  623. static int do_print_item(const TEST_PACKAGE *package)
  624. {
  625. #define DATA_BUF_SIZE 256
  626. const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type);
  627. ASN1_VALUE *o;
  628. int ret;
  629. OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE);
  630. if ((o = OPENSSL_malloc(DATA_BUF_SIZE)) == NULL)
  631. return 0;
  632. (void)RAND_bytes((unsigned char*)o,
  633. (int)package->encode_expectations_elem_size);
  634. ret = ASN1_item_print(bio_err, o, 0, i, NULL);
  635. OPENSSL_free(o);
  636. return ret;
  637. }
  638. static int test_intern(const TEST_PACKAGE *package)
  639. {
  640. unsigned int i;
  641. size_t nelems;
  642. int fail = 0;
  643. if (package->skip)
  644. return 1;
  645. /* Do decode_custom checks */
  646. nelems = package->encode_expectations_size
  647. / package->encode_expectations_elem_size;
  648. OPENSSL_assert(nelems ==
  649. sizeof(test_custom_data) / sizeof(test_custom_data[0]));
  650. for (i = 0; i < nelems; i++) {
  651. size_t pos = i * package->encode_expectations_elem_size;
  652. EXPECTED *expected
  653. = (EXPECTED *)&((unsigned char *)package->encode_expectations)[pos];
  654. switch (do_encode_custom(expected, &test_custom_data[i], package)) {
  655. case -1:
  656. if (expected->success) {
  657. TEST_error("Failed custom encode round trip %u of %s",
  658. i, package->name);
  659. TEST_openssl_errors();
  660. fail++;
  661. }
  662. break;
  663. case 0:
  664. TEST_error("Custom encode round trip %u of %s mismatch",
  665. i, package->name);
  666. TEST_openssl_errors();
  667. fail++;
  668. break;
  669. case 1:
  670. break;
  671. default:
  672. OPENSSL_die("do_encode_custom() return unknown value",
  673. __FILE__, __LINE__);
  674. }
  675. switch (do_decode_custom(&test_custom_data[i], expected,
  676. package->encode_expectations_elem_size,
  677. package)) {
  678. case -1:
  679. if (expected->success) {
  680. TEST_error("Failed custom decode round trip %u of %s",
  681. i, package->name);
  682. TEST_openssl_errors();
  683. fail++;
  684. }
  685. break;
  686. case 0:
  687. TEST_error("Custom decode round trip %u of %s mismatch",
  688. i, package->name);
  689. TEST_openssl_errors();
  690. fail++;
  691. break;
  692. case 1:
  693. break;
  694. default:
  695. OPENSSL_die("do_decode_custom() return unknown value",
  696. __FILE__, __LINE__);
  697. }
  698. }
  699. /* Do enc_dec checks */
  700. nelems = package->encdec_data_size / package->encdec_data_elem_size;
  701. for (i = 0; i < nelems; i++) {
  702. size_t pos = i * package->encdec_data_elem_size;
  703. EXPECTED *expected
  704. = (EXPECTED *)&((unsigned char *)package->encdec_data)[pos];
  705. switch (do_enc_dec(expected, package->encdec_data_elem_size, package)) {
  706. case -1:
  707. if (expected->success) {
  708. TEST_error("Failed encode/decode round trip %u of %s",
  709. i, package->name);
  710. TEST_openssl_errors();
  711. fail++;
  712. }
  713. break;
  714. case 0:
  715. TEST_error("Encode/decode round trip %u of %s mismatch",
  716. i, package->name);
  717. fail++;
  718. break;
  719. case 1:
  720. break;
  721. default:
  722. OPENSSL_die("do_enc_dec() return unknown value",
  723. __FILE__, __LINE__);
  724. }
  725. }
  726. if (!do_print_item(package)) {
  727. TEST_error("Printing of %s failed", package->name);
  728. TEST_openssl_errors();
  729. fail++;
  730. }
  731. return fail == 0;
  732. }
  733. #ifndef OPENSSL_NO_DEPRECATED_3_0
  734. static int test_long_32bit(void)
  735. {
  736. return test_intern(&long_test_package_32bit);
  737. }
  738. static int test_long_64bit(void)
  739. {
  740. return test_intern(&long_test_package_64bit);
  741. }
  742. #endif
  743. static int test_int32(void)
  744. {
  745. return test_intern(&int32_test_package);
  746. }
  747. static int test_uint32(void)
  748. {
  749. return test_intern(&uint32_test_package);
  750. }
  751. static int test_int64(void)
  752. {
  753. return test_intern(&int64_test_package);
  754. }
  755. static int test_uint64(void)
  756. {
  757. return test_intern(&uint64_test_package);
  758. }
  759. typedef struct {
  760. ASN1_STRING *invalidDirString;
  761. } INVALIDTEMPLATE;
  762. ASN1_SEQUENCE(INVALIDTEMPLATE) = {
  763. /*
  764. * DirectoryString is a CHOICE type so it must use explicit tagging -
  765. * but we deliberately use implicit here, which makes this template invalid.
  766. */
  767. ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
  768. } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
  769. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
  770. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
  771. static int test_invalid_template(void)
  772. {
  773. INVALIDTEMPLATE *temp = INVALIDTEMPLATE_new();
  774. int ret;
  775. if (!TEST_ptr(temp))
  776. return 0;
  777. ret = i2d_INVALIDTEMPLATE(temp, NULL);
  778. INVALIDTEMPLATE_free(temp);
  779. /* We expect the i2d operation to fail */
  780. return ret < 0;
  781. }
  782. int setup_tests(void)
  783. {
  784. #ifndef OPENSSL_NO_DEPRECATED_3_0
  785. ADD_TEST(test_long_32bit);
  786. ADD_TEST(test_long_64bit);
  787. #endif
  788. ADD_TEST(test_int32);
  789. ADD_TEST(test_uint32);
  790. ADD_TEST(test_int64);
  791. ADD_TEST(test_uint64);
  792. ADD_TEST(test_invalid_template);
  793. return 1;
  794. }