der_tests.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. #include <tomcrypt_test.h>
  2. #if defined(GMP_LTC_DESC) || defined(USE_GMP)
  3. #include <gmp.h>
  4. #endif
  5. #ifndef LTC_DER
  6. int der_tests(void)
  7. {
  8. fprintf(stderr, "NOP");
  9. return 0;
  10. }
  11. #else
  12. static void der_set_test(void)
  13. {
  14. ltc_asn1_list list[10];
  15. static const unsigned char oct_str[] = { 1, 2, 3, 4 };
  16. static const unsigned char bin_str[] = { 1, 0, 0, 1 };
  17. static const unsigned long int_val = 12345678UL;
  18. unsigned char strs[10][10], outbuf[128];
  19. unsigned long x, val, outlen;
  20. int err;
  21. /* make structure and encode it */
  22. LTC_SET_ASN1(list, 0, LTC_ASN1_OCTET_STRING, oct_str, sizeof(oct_str));
  23. LTC_SET_ASN1(list, 1, LTC_ASN1_BIT_STRING, bin_str, sizeof(bin_str));
  24. LTC_SET_ASN1(list, 2, LTC_ASN1_SHORT_INTEGER, &int_val, 1);
  25. /* encode it */
  26. outlen = sizeof(outbuf);
  27. if ((err = der_encode_set(list, 3, outbuf, &outlen)) != CRYPT_OK) {
  28. fprintf(stderr, "error encoding set: %s\n", error_to_string(err));
  29. exit(EXIT_FAILURE);
  30. }
  31. /* first let's test the set_decoder out of order to see what happens, we should get all the fields we expect even though they're in a diff order */
  32. LTC_SET_ASN1(list, 0, LTC_ASN1_BIT_STRING, strs[1], sizeof(strs[1]));
  33. LTC_SET_ASN1(list, 1, LTC_ASN1_SHORT_INTEGER, &val, 1);
  34. LTC_SET_ASN1(list, 2, LTC_ASN1_OCTET_STRING, strs[0], sizeof(strs[0]));
  35. if ((err = der_decode_set(outbuf, outlen, list, 3)) != CRYPT_OK) {
  36. fprintf(stderr, "error decoding set using der_decode_set: %s\n", error_to_string(err));
  37. exit(EXIT_FAILURE);
  38. }
  39. /* now compare the items */
  40. if (memcmp(strs[0], oct_str, sizeof(oct_str))) {
  41. fprintf(stderr, "error decoding set using der_decode_set (oct_str is wrong):\n");
  42. exit(EXIT_FAILURE);
  43. }
  44. if (memcmp(strs[1], bin_str, sizeof(bin_str))) {
  45. fprintf(stderr, "error decoding set using der_decode_set (bin_str is wrong):\n");
  46. exit(EXIT_FAILURE);
  47. }
  48. if (val != int_val) {
  49. fprintf(stderr, "error decoding set using der_decode_set (int_val is wrong):\n");
  50. exit(EXIT_FAILURE);
  51. }
  52. strcpy((char*)strs[0], "one");
  53. strcpy((char*)strs[1], "one2");
  54. strcpy((char*)strs[2], "two");
  55. strcpy((char*)strs[3], "aaa");
  56. strcpy((char*)strs[4], "aaaa");
  57. strcpy((char*)strs[5], "aab");
  58. strcpy((char*)strs[6], "aaab");
  59. strcpy((char*)strs[7], "bbb");
  60. strcpy((char*)strs[8], "bbba");
  61. strcpy((char*)strs[9], "bbbb");
  62. for (x = 0; x < 10; x++) {
  63. LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], strlen((char*)strs[x]));
  64. }
  65. outlen = sizeof(outbuf);
  66. if ((err = der_encode_setof(list, 10, outbuf, &outlen)) != CRYPT_OK) {
  67. fprintf(stderr, "error encoding SET OF: %s\n", error_to_string(err));
  68. exit(EXIT_FAILURE);
  69. }
  70. for (x = 0; x < 10; x++) {
  71. LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], sizeof(strs[x]) - 1);
  72. }
  73. XMEMSET(strs, 0, sizeof(strs));
  74. if ((err = der_decode_set(outbuf, outlen, list, 10)) != CRYPT_OK) {
  75. fprintf(stderr, "error decoding SET OF: %s\n", error_to_string(err));
  76. exit(EXIT_FAILURE);
  77. }
  78. /* now compare */
  79. for (x = 1; x < 10; x++) {
  80. if (!(strlen((char*)strs[x-1]) <= strlen((char*)strs[x])) && strcmp((char*)strs[x-1], (char*)strs[x]) >= 0) {
  81. fprintf(stderr, "error SET OF order at %lu is wrong\n", x);
  82. exit(EXIT_FAILURE);
  83. }
  84. }
  85. }
  86. /* we are encoding
  87. SEQUENCE {
  88. PRINTABLE "printable"
  89. IA5 "ia5"
  90. SEQUENCE {
  91. INTEGER 12345678
  92. UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
  93. SEQUENCE {
  94. OCTET STRING { 1, 2, 3, 4 }
  95. BIT STRING { 1, 0, 0, 1 }
  96. SEQUENCE {
  97. OID { 1, 2, 840, 113549 }
  98. NULL
  99. SET OF {
  100. PRINTABLE "333" // WILL GET SORTED
  101. PRINTABLE "222"
  102. }
  103. }
  104. }
  105. }
  106. */
  107. static void der_flexi_test(void)
  108. {
  109. static const char printable_str[] = "printable";
  110. static const char set1_str[] = "333";
  111. static const char set2_str[] = "222";
  112. static const char ia5_str[] = "ia5";
  113. static const unsigned long int_val = 12345678UL;
  114. static const ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  115. static const unsigned char oct_str[] = { 1, 2, 3, 4 };
  116. static const unsigned char bit_str[] = { 1, 0, 0, 1 };
  117. static const unsigned long oid_str[] = { 1, 2, 840, 113549 };
  118. unsigned char encode_buf[192];
  119. unsigned long encode_buf_len, decode_len;
  120. int err;
  121. ltc_asn1_list static_list[5][3], *decoded_list, *l;
  122. /* build list */
  123. LTC_SET_ASN1(static_list[0], 0, LTC_ASN1_PRINTABLE_STRING, (void *)printable_str, strlen(printable_str));
  124. LTC_SET_ASN1(static_list[0], 1, LTC_ASN1_IA5_STRING, (void *)ia5_str, strlen(ia5_str));
  125. LTC_SET_ASN1(static_list[0], 2, LTC_ASN1_SEQUENCE, static_list[1], 3);
  126. LTC_SET_ASN1(static_list[1], 0, LTC_ASN1_SHORT_INTEGER, (void *)&int_val, 1);
  127. LTC_SET_ASN1(static_list[1], 1, LTC_ASN1_UTCTIME, (void *)&utctime, 1);
  128. LTC_SET_ASN1(static_list[1], 2, LTC_ASN1_SEQUENCE, static_list[2], 3);
  129. LTC_SET_ASN1(static_list[2], 0, LTC_ASN1_OCTET_STRING, (void *)oct_str, 4);
  130. LTC_SET_ASN1(static_list[2], 1, LTC_ASN1_BIT_STRING, (void *)bit_str, 4);
  131. LTC_SET_ASN1(static_list[2], 2, LTC_ASN1_SEQUENCE, static_list[3], 3);
  132. LTC_SET_ASN1(static_list[3], 0, LTC_ASN1_OBJECT_IDENTIFIER,(void *)oid_str, 4);
  133. LTC_SET_ASN1(static_list[3], 1, LTC_ASN1_NULL, NULL, 0);
  134. LTC_SET_ASN1(static_list[3], 2, LTC_ASN1_SETOF, static_list[4], 2);
  135. LTC_SET_ASN1(static_list[4], 0, LTC_ASN1_PRINTABLE_STRING, set1_str, strlen(set1_str));
  136. LTC_SET_ASN1(static_list[4], 1, LTC_ASN1_PRINTABLE_STRING, set2_str, strlen(set2_str));
  137. /* encode it */
  138. encode_buf_len = sizeof(encode_buf);
  139. if ((err = der_encode_sequence(&static_list[0][0], 3, encode_buf, &encode_buf_len)) != CRYPT_OK) {
  140. fprintf(stderr, "Encoding static_list: %s\n", error_to_string(err));
  141. exit(EXIT_FAILURE);
  142. }
  143. #if 0
  144. {
  145. FILE *f;
  146. f = fopen("t.bin", "wb");
  147. fwrite(encode_buf, 1, encode_buf_len, f);
  148. fclose(f);
  149. }
  150. #endif
  151. /* decode with flexi */
  152. decode_len = encode_buf_len;
  153. if ((err = der_decode_sequence_flexi(encode_buf, &decode_len, &decoded_list)) != CRYPT_OK) {
  154. fprintf(stderr, "decoding static_list: %s\n", error_to_string(err));
  155. exit(EXIT_FAILURE);
  156. }
  157. if (decode_len != encode_buf_len) {
  158. fprintf(stderr, "Decode len of %lu does not match encode len of %lu \n", decode_len, encode_buf_len);
  159. exit(EXIT_FAILURE);
  160. }
  161. /* we expect l->next to be NULL and l->child to not be */
  162. l = decoded_list;
  163. if (l->next != NULL || l->child == NULL) {
  164. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  165. exit(EXIT_FAILURE);
  166. }
  167. /* we expect a SEQUENCE */
  168. if (l->type != LTC_ASN1_SEQUENCE) {
  169. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  170. exit(EXIT_FAILURE);
  171. }
  172. l = l->child;
  173. /* PRINTABLE STRING */
  174. /* we expect printable_str */
  175. if (l->next == NULL || l->child != NULL) {
  176. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  177. exit(EXIT_FAILURE);
  178. }
  179. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  180. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  181. exit(EXIT_FAILURE);
  182. }
  183. if (l->size != strlen(printable_str) || memcmp(printable_str, l->data, l->size)) {
  184. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  185. exit(EXIT_FAILURE);
  186. }
  187. /* move to next */
  188. l = l->next;
  189. /* IA5 STRING */
  190. /* we expect ia5_str */
  191. if (l->next == NULL || l->child != NULL) {
  192. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  193. exit(EXIT_FAILURE);
  194. }
  195. if (l->type != LTC_ASN1_IA5_STRING) {
  196. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  197. exit(EXIT_FAILURE);
  198. }
  199. if (l->size != strlen(ia5_str) || memcmp(ia5_str, l->data, l->size)) {
  200. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  201. exit(EXIT_FAILURE);
  202. }
  203. /* move to next */
  204. l = l->next;
  205. /* expect child anve move down */
  206. if (l->next != NULL || l->child == NULL) {
  207. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  208. exit(EXIT_FAILURE);
  209. }
  210. if (l->type != LTC_ASN1_SEQUENCE) {
  211. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  212. exit(EXIT_FAILURE);
  213. }
  214. l = l->child;
  215. /* INTEGER */
  216. if (l->next == NULL || l->child != NULL) {
  217. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  218. exit(EXIT_FAILURE);
  219. }
  220. if (l->type != LTC_ASN1_INTEGER) {
  221. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  222. exit(EXIT_FAILURE);
  223. }
  224. if (mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) {
  225. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  226. exit(EXIT_FAILURE);
  227. }
  228. /* move to next */
  229. l = l->next;
  230. /* UTCTIME */
  231. if (l->next == NULL || l->child != NULL) {
  232. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  233. exit(EXIT_FAILURE);
  234. }
  235. if (l->type != LTC_ASN1_UTCTIME) {
  236. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  237. exit(EXIT_FAILURE);
  238. }
  239. if (memcmp(l->data, &utctime, sizeof(utctime))) {
  240. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  241. exit(EXIT_FAILURE);
  242. }
  243. /* move to next */
  244. l = l->next;
  245. /* expect child anve move down */
  246. if (l->next != NULL || l->child == NULL) {
  247. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  248. exit(EXIT_FAILURE);
  249. }
  250. if (l->type != LTC_ASN1_SEQUENCE) {
  251. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  252. exit(EXIT_FAILURE);
  253. }
  254. l = l->child;
  255. /* OCTET STRING */
  256. /* we expect oct_str */
  257. if (l->next == NULL || l->child != NULL) {
  258. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  259. exit(EXIT_FAILURE);
  260. }
  261. if (l->type != LTC_ASN1_OCTET_STRING) {
  262. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  263. exit(EXIT_FAILURE);
  264. }
  265. if (l->size != sizeof(oct_str) || memcmp(oct_str, l->data, l->size)) {
  266. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  267. exit(EXIT_FAILURE);
  268. }
  269. /* move to next */
  270. l = l->next;
  271. /* BIT STRING */
  272. /* we expect oct_str */
  273. if (l->next == NULL || l->child != NULL) {
  274. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  275. exit(EXIT_FAILURE);
  276. }
  277. if (l->type != LTC_ASN1_BIT_STRING) {
  278. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  279. exit(EXIT_FAILURE);
  280. }
  281. if (l->size != sizeof(bit_str) || memcmp(bit_str, l->data, l->size)) {
  282. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  283. exit(EXIT_FAILURE);
  284. }
  285. /* move to next */
  286. l = l->next;
  287. /* expect child anve move down */
  288. if (l->next != NULL || l->child == NULL) {
  289. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  290. exit(EXIT_FAILURE);
  291. }
  292. if (l->type != LTC_ASN1_SEQUENCE) {
  293. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  294. exit(EXIT_FAILURE);
  295. }
  296. l = l->child;
  297. /* OID STRING */
  298. /* we expect oid_str */
  299. if (l->next == NULL || l->child != NULL) {
  300. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  301. exit(EXIT_FAILURE);
  302. }
  303. if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) {
  304. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  305. exit(EXIT_FAILURE);
  306. }
  307. if (l->size != sizeof(oid_str)/sizeof(oid_str[0]) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
  308. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  309. exit(EXIT_FAILURE);
  310. }
  311. /* move to next */
  312. l = l->next;
  313. /* NULL */
  314. if (l->type != LTC_ASN1_NULL) {
  315. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  316. exit(EXIT_FAILURE);
  317. }
  318. /* move to next */
  319. l = l->next;
  320. /* expect child anve move down */
  321. if (l->next != NULL || l->child == NULL) {
  322. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  323. exit(EXIT_FAILURE);
  324. }
  325. if (l->type != LTC_ASN1_SET) {
  326. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  327. exit(EXIT_FAILURE);
  328. }
  329. l = l->child;
  330. /* PRINTABLE STRING */
  331. /* we expect printable_str */
  332. if (l->next == NULL || l->child != NULL) {
  333. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  334. exit(EXIT_FAILURE);
  335. }
  336. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  337. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  338. exit(EXIT_FAILURE);
  339. }
  340. /* note we compare set2_str FIRST because the SET OF is sorted and "222" comes before "333" */
  341. if (l->size != strlen(set2_str) || memcmp(set2_str, l->data, l->size)) {
  342. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  343. exit(EXIT_FAILURE);
  344. }
  345. /* move to next */
  346. l = l->next;
  347. /* PRINTABLE STRING */
  348. /* we expect printable_str */
  349. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  350. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  351. exit(EXIT_FAILURE);
  352. }
  353. if (l->size != strlen(set1_str) || memcmp(set1_str, l->data, l->size)) {
  354. fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
  355. exit(EXIT_FAILURE);
  356. }
  357. der_sequence_free(l);
  358. }
  359. static int der_choice_test(void)
  360. {
  361. ltc_asn1_list types[7], host[1];
  362. unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256];
  363. unsigned long integer, oidbuf[10], outlen, inlen, x, y;
  364. void *mpinteger;
  365. ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  366. /* setup variables */
  367. for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; }
  368. for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; }
  369. for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; }
  370. for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; }
  371. integer = 1;
  372. for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; }
  373. DO(mp_init(&mpinteger));
  374. for (x = 0; x < 14; x++) {
  375. /* setup list */
  376. LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
  377. LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
  378. LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
  379. LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
  380. if (x > 7) {
  381. LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
  382. } else {
  383. LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, mpinteger, 1);
  384. }
  385. LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
  386. LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
  387. LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7);
  388. /* encode */
  389. outlen = sizeof(outbuf);
  390. DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen));
  391. /* decode it */
  392. inlen = outlen;
  393. DO(der_decode_sequence(outbuf, inlen, &host[0], 1));
  394. for (y = 0; y < 7; y++) {
  395. if (types[y].used && y != (x>6?x-7:x)) {
  396. fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to one\n", y, x);
  397. return 1;
  398. }
  399. if (!types[y].used && y == (x>6?x-7:x)) {
  400. fprintf(stderr, "CHOICE, flag %lu in trial %lu was incorrectly set to zero\n", y, x);
  401. return 1;
  402. }
  403. }
  404. }
  405. mp_clear(mpinteger);
  406. return 0;
  407. }
  408. int der_tests(void)
  409. {
  410. unsigned long x, y, z, zz, oid[2][32];
  411. unsigned char buf[3][2048];
  412. void *a, *b, *c, *d, *e, *f, *g;
  413. static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
  414. static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 };
  415. static const unsigned char rsa_ia5[] = "test1@rsa.com";
  416. static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
  417. 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
  418. static const unsigned char rsa_printable[] = "Test User 1";
  419. static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
  420. 0x73, 0x65, 0x72, 0x20, 0x31 };
  421. static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  422. static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
  423. ltc_utctime tmp_time;
  424. static const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 };
  425. static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
  426. static const wchar_t utf8_1[] = { 0x0041, 0x2262, 0x0391, 0x002E };
  427. static const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E };
  428. static const wchar_t utf8_2[] = { 0xD55C, 0xAD6D, 0xC5B4 };
  429. static const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 };
  430. unsigned char utf8_buf[32];
  431. wchar_t utf8_out[32];
  432. DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
  433. for (zz = 0; zz < 16; zz++) {
  434. #ifdef USE_TFM
  435. for (z = 0; z < 256; z++) {
  436. #else
  437. for (z = 0; z < 1024; z++) {
  438. #endif
  439. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  440. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  441. return 1;
  442. }
  443. DO(mp_read_unsigned_bin(a, buf[0], z));
  444. /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
  445. x = sizeof(buf[0]);
  446. DO(der_encode_integer(a, buf[0], &x));
  447. DO(der_length_integer(a, &y));
  448. if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
  449. mp_set_int(b, 0);
  450. DO(der_decode_integer(buf[0], y, b));
  451. if (y != x || mp_cmp(a, b) != LTC_MP_EQ) {
  452. fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
  453. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  454. return 1;
  455. }
  456. }
  457. }
  458. /* test short integer */
  459. for (zz = 0; zz < 256; zz++) {
  460. for (z = 1; z < 4; z++) {
  461. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  462. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  463. return 1;
  464. }
  465. /* encode with normal */
  466. DO(mp_read_unsigned_bin(a, buf[0], z));
  467. x = sizeof(buf[0]);
  468. DO(der_encode_integer(a, buf[0], &x));
  469. /* encode with short */
  470. y = sizeof(buf[1]);
  471. DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
  472. if (x != y || memcmp(buf[0], buf[1], x)) {
  473. fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
  474. for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
  475. for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
  476. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  477. return 1;
  478. }
  479. /* decode it */
  480. x = 0;
  481. DO(der_decode_short_integer(buf[1], y, &x));
  482. if (x != mp_get_int(a)) {
  483. fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a));
  484. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  485. return 1;
  486. }
  487. }
  488. }
  489. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  490. /* Test bit string */
  491. for (zz = 1; zz < 1536; zz++) {
  492. yarrow_read(buf[0], zz, &yarrow_prng);
  493. for (z = 0; z < zz; z++) {
  494. buf[0][z] &= 0x01;
  495. }
  496. x = sizeof(buf[1]);
  497. DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
  498. DO(der_length_bit_string(zz, &y));
  499. if (y != x) {
  500. fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  501. return 1;
  502. }
  503. y = sizeof(buf[2]);
  504. DO(der_decode_bit_string(buf[1], x, buf[2], &y));
  505. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  506. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  507. return 1;
  508. }
  509. }
  510. /* Test octet string */
  511. for (zz = 1; zz < 1536; zz++) {
  512. yarrow_read(buf[0], zz, &yarrow_prng);
  513. x = sizeof(buf[1]);
  514. DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
  515. DO(der_length_octet_string(zz, &y));
  516. if (y != x) {
  517. fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  518. return 1;
  519. }
  520. y = sizeof(buf[2]);
  521. DO(der_decode_octet_string(buf[1], x, buf[2], &y));
  522. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  523. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  524. return 1;
  525. }
  526. }
  527. /* test OID */
  528. x = sizeof(buf[0]);
  529. DO(der_encode_object_identifier((unsigned long*)rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
  530. if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
  531. fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
  532. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  533. fprintf(stderr, "\n");
  534. return 1;
  535. }
  536. y = sizeof(oid[0])/sizeof(oid[0][0]);
  537. DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
  538. if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
  539. fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
  540. for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
  541. fprintf(stderr, "\n");
  542. return 1;
  543. }
  544. /* do random strings */
  545. for (zz = 0; zz < 5000; zz++) {
  546. /* pick a random number of words */
  547. yarrow_read(buf[0], 4, &yarrow_prng);
  548. LOAD32L(z, buf[0]);
  549. z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
  550. /* fill them in */
  551. oid[0][0] = buf[0][0] % 3;
  552. oid[0][1] = buf[0][1] % 40;
  553. for (y = 2; y < z; y++) {
  554. yarrow_read(buf[0], 4, &yarrow_prng);
  555. LOAD32L(oid[0][y], buf[0]);
  556. }
  557. /* encode it */
  558. x = sizeof(buf[0]);
  559. DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
  560. DO(der_length_object_identifier(oid[0], z, &y));
  561. if (x != y) {
  562. fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
  563. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  564. return 1;
  565. }
  566. /* decode it */
  567. y = sizeof(oid[0])/sizeof(oid[0][0]);
  568. DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
  569. if (y != z) {
  570. fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
  571. return 1;
  572. }
  573. if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
  574. fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
  575. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]); fprintf(stderr, "\n\n Got \n\n");
  576. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
  577. return 1;
  578. }
  579. }
  580. /* IA5 string */
  581. x = sizeof(buf[0]);
  582. DO(der_encode_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), buf[0], &x));
  583. if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
  584. fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
  585. return 1;
  586. }
  587. DO(der_length_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), &y));
  588. if (y != x) {
  589. fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
  590. return 1;
  591. }
  592. y = sizeof(buf[1]);
  593. DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
  594. if (y != strlen((char*)rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen((char*)rsa_ia5))) {
  595. fprintf(stderr, "DER IA5 failed test vector\n");
  596. return 1;
  597. }
  598. /* Printable string */
  599. x = sizeof(buf[0]);
  600. DO(der_encode_printable_string(rsa_printable, strlen((char*)rsa_printable), buf[0], &x));
  601. if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
  602. fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
  603. return 1;
  604. }
  605. DO(der_length_printable_string(rsa_printable, strlen((char*)rsa_printable), &y));
  606. if (y != x) {
  607. fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
  608. return 1;
  609. }
  610. y = sizeof(buf[1]);
  611. DO(der_decode_printable_string(buf[0], x, buf[1], &y));
  612. if (y != strlen((char*)rsa_printable) || memcmp(buf[1], rsa_printable, strlen((char*)rsa_printable))) {
  613. fprintf(stderr, "DER printable failed test vector\n");
  614. return 1;
  615. }
  616. /* Test UTC time */
  617. x = sizeof(buf[0]);
  618. DO(der_encode_utctime((ltc_utctime*)&rsa_time1, buf[0], &x));
  619. if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
  620. fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  621. fprintf(stderr, "\n\n");
  622. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  623. return 1;
  624. }
  625. DO(der_length_utctime((ltc_utctime*)&rsa_time1, &y));
  626. if (y != x) {
  627. fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
  628. return 1;
  629. }
  630. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  631. if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
  632. fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
  633. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  634. tmp_time.YY,
  635. tmp_time.MM,
  636. tmp_time.DD,
  637. tmp_time.hh,
  638. tmp_time.mm,
  639. tmp_time.ss,
  640. tmp_time.off_dir,
  641. tmp_time.off_mm,
  642. tmp_time.off_hh);
  643. return 1;
  644. }
  645. x = sizeof(buf[0]);
  646. DO(der_encode_utctime((ltc_utctime*)&rsa_time2, buf[0], &x));
  647. if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
  648. fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  649. fprintf(stderr, "\n\n");
  650. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  651. return 1;
  652. }
  653. DO(der_length_utctime((ltc_utctime*)&rsa_time2, &y));
  654. if (y != x) {
  655. fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
  656. return 1;
  657. }
  658. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  659. if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
  660. fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
  661. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  662. tmp_time.YY,
  663. tmp_time.MM,
  664. tmp_time.DD,
  665. tmp_time.hh,
  666. tmp_time.mm,
  667. tmp_time.ss,
  668. tmp_time.off_dir,
  669. tmp_time.off_mm,
  670. tmp_time.off_hh);
  671. return 1;
  672. }
  673. /* UTF 8 */
  674. /* encode it */
  675. x = sizeof(utf8_buf);
  676. DO(der_encode_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), utf8_buf, &x));
  677. if (x != sizeof(utf8_1_der) || memcmp(utf8_buf, utf8_1_der, x)) {
  678. fprintf(stderr, "DER UTF8_1 encoded to %lu bytes\n", x);
  679. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]); fprintf(stderr, "\n");
  680. return 1;
  681. }
  682. /* decode it */
  683. y = sizeof(utf8_out) / sizeof(utf8_out[0]);
  684. DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
  685. if (y != (sizeof(utf8_1) / sizeof(utf8_1[0])) || memcmp(utf8_1, utf8_out, y * sizeof(wchar_t))) {
  686. fprintf(stderr, "DER UTF8_1 decoded to %lu wchar_t\n", y);
  687. for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]); fprintf(stderr, "\n");
  688. return 1;
  689. }
  690. /* encode it */
  691. x = sizeof(utf8_buf);
  692. DO(der_encode_utf8_string(utf8_2, sizeof(utf8_2) / sizeof(utf8_2[0]), utf8_buf, &x));
  693. if (x != sizeof(utf8_2_der) || memcmp(utf8_buf, utf8_2_der, x)) {
  694. fprintf(stderr, "DER UTF8_2 encoded to %lu bytes\n", x);
  695. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]); fprintf(stderr, "\n");
  696. return 1;
  697. }
  698. /* decode it */
  699. y = sizeof(utf8_out) / sizeof(utf8_out[0]);
  700. DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
  701. if (y != (sizeof(utf8_2) / sizeof(utf8_2[0])) || memcmp(utf8_2, utf8_out, y * sizeof(wchar_t))) {
  702. fprintf(stderr, "DER UTF8_2 decoded to %lu wchar_t\n", y);
  703. for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]); fprintf(stderr, "\n");
  704. return 1;
  705. }
  706. der_set_test();
  707. der_flexi_test();
  708. return der_choice_test();
  709. }
  710. #endif
  711. /* $Source$ */
  712. /* $Revision$ */
  713. /* $Date$ */