der_tests.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. #include <tomcrypt_test.h>
  2. #if defined(GMP_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(strs[0], "one");
  53. strcpy(strs[1], "one2");
  54. strcpy(strs[2], "two");
  55. strcpy(strs[3], "aaa");
  56. strcpy(strs[4], "aaaa");
  57. strcpy(strs[5], "aab");
  58. strcpy(strs[6], "aaab");
  59. strcpy(strs[7], "bbb");
  60. strcpy(strs[8], "bbba");
  61. strcpy(strs[9], "bbbb");
  62. for (x = 0; x < 10; x++) {
  63. LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], strlen(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(strs[x-1]) <= strlen(strs[x])) && strcmp(strs[x-1], strs[x]) >= 0) {
  81. fprintf(stderr, "error SET OF order at %d 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[] = "[email protected]";
  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. DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
  427. for (zz = 0; zz < 16; zz++) {
  428. #ifdef USE_TFM
  429. for (z = 0; z < 256; z++) {
  430. #else
  431. for (z = 0; z < 1024; z++) {
  432. #endif
  433. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  434. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  435. return 1;
  436. }
  437. DO(mp_read_unsigned_bin(a, buf[0], z));
  438. /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
  439. x = sizeof(buf[0]);
  440. DO(der_encode_integer(a, buf[0], &x));
  441. DO(der_length_integer(a, &y));
  442. if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
  443. mp_set_int(b, 0);
  444. DO(der_decode_integer(buf[0], y, b));
  445. if (y != x || mp_cmp(a, b) != LTC_MP_EQ) {
  446. fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
  447. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  448. return 1;
  449. }
  450. }
  451. }
  452. /* test short integer */
  453. for (zz = 0; zz < 256; zz++) {
  454. for (z = 1; z < 4; z++) {
  455. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  456. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  457. return 1;
  458. }
  459. /* encode with normal */
  460. DO(mp_read_unsigned_bin(a, buf[0], z));
  461. x = sizeof(buf[0]);
  462. DO(der_encode_integer(a, buf[0], &x));
  463. /* encode with short */
  464. y = sizeof(buf[1]);
  465. DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
  466. if (x != y || memcmp(buf[0], buf[1], x)) {
  467. fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu\n", x, y);
  468. for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n");
  469. for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n");
  470. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  471. return 1;
  472. }
  473. /* decode it */
  474. x = 0;
  475. DO(der_decode_short_integer(buf[1], y, &x));
  476. if (x != mp_get_int(a)) {
  477. fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a));
  478. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  479. return 1;
  480. }
  481. }
  482. }
  483. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  484. /* Test bit string */
  485. for (zz = 1; zz < 1536; zz++) {
  486. yarrow_read(buf[0], zz, &yarrow_prng);
  487. for (z = 0; z < zz; z++) {
  488. buf[0][z] &= 0x01;
  489. }
  490. x = sizeof(buf[1]);
  491. DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
  492. DO(der_length_bit_string(zz, &y));
  493. if (y != x) {
  494. fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  495. return 1;
  496. }
  497. y = sizeof(buf[2]);
  498. DO(der_decode_bit_string(buf[1], x, buf[2], &y));
  499. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  500. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  501. return 1;
  502. }
  503. }
  504. /* Test octet string */
  505. for (zz = 1; zz < 1536; zz++) {
  506. yarrow_read(buf[0], zz, &yarrow_prng);
  507. x = sizeof(buf[1]);
  508. DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
  509. DO(der_length_octet_string(zz, &y));
  510. if (y != x) {
  511. fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  512. return 1;
  513. }
  514. y = sizeof(buf[2]);
  515. DO(der_decode_octet_string(buf[1], x, buf[2], &y));
  516. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  517. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  518. return 1;
  519. }
  520. }
  521. /* test OID */
  522. x = sizeof(buf[0]);
  523. DO(der_encode_object_identifier(rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
  524. if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
  525. fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
  526. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  527. fprintf(stderr, "\n");
  528. return 1;
  529. }
  530. y = sizeof(oid[0])/sizeof(oid[0][0]);
  531. DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
  532. if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
  533. fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
  534. for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
  535. fprintf(stderr, "\n");
  536. return 1;
  537. }
  538. /* do random strings */
  539. for (zz = 0; zz < 5000; zz++) {
  540. /* pick a random number of words */
  541. yarrow_read(buf[0], 4, &yarrow_prng);
  542. LOAD32L(z, buf[0]);
  543. z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
  544. /* fill them in */
  545. oid[0][0] = buf[0][0] % 3;
  546. oid[0][1] = buf[0][1] % 40;
  547. for (y = 2; y < z; y++) {
  548. yarrow_read(buf[0], 4, &yarrow_prng);
  549. LOAD32L(oid[0][y], buf[0]);
  550. }
  551. /* encode it */
  552. x = sizeof(buf[0]);
  553. DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
  554. DO(der_length_object_identifier(oid[0], z, &y));
  555. if (x != y) {
  556. fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
  557. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  558. return 1;
  559. }
  560. /* decode it */
  561. y = sizeof(oid[0])/sizeof(oid[0][0]);
  562. DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
  563. if (y != z) {
  564. fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
  565. return 1;
  566. }
  567. if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
  568. fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
  569. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]); fprintf(stderr, "\n\n Got \n\n");
  570. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
  571. return 1;
  572. }
  573. }
  574. /* IA5 string */
  575. x = sizeof(buf[0]);
  576. DO(der_encode_ia5_string(rsa_ia5, strlen(rsa_ia5), buf[0], &x));
  577. if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
  578. fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
  579. return 1;
  580. }
  581. DO(der_length_ia5_string(rsa_ia5, strlen(rsa_ia5), &y));
  582. if (y != x) {
  583. fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
  584. return 1;
  585. }
  586. y = sizeof(buf[1]);
  587. DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
  588. if (y != strlen(rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen(rsa_ia5))) {
  589. fprintf(stderr, "DER IA5 failed test vector\n");
  590. return 1;
  591. }
  592. /* Printable string */
  593. x = sizeof(buf[0]);
  594. DO(der_encode_printable_string(rsa_printable, strlen(rsa_printable), buf[0], &x));
  595. if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
  596. fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
  597. return 1;
  598. }
  599. DO(der_length_printable_string(rsa_printable, strlen(rsa_printable), &y));
  600. if (y != x) {
  601. fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
  602. return 1;
  603. }
  604. y = sizeof(buf[1]);
  605. DO(der_decode_printable_string(buf[0], x, buf[1], &y));
  606. if (y != strlen(rsa_printable) || memcmp(buf[1], rsa_printable, strlen(rsa_printable))) {
  607. fprintf(stderr, "DER printable failed test vector\n");
  608. return 1;
  609. }
  610. /* Test UTC time */
  611. x = sizeof(buf[0]);
  612. DO(der_encode_utctime(&rsa_time1, buf[0], &x));
  613. if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
  614. fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  615. fprintf(stderr, "\n\n");
  616. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  617. return 1;
  618. }
  619. DO(der_length_utctime(&rsa_time1, &y));
  620. if (y != x) {
  621. fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
  622. return 1;
  623. }
  624. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  625. if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
  626. fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
  627. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  628. tmp_time.YY,
  629. tmp_time.MM,
  630. tmp_time.DD,
  631. tmp_time.hh,
  632. tmp_time.mm,
  633. tmp_time.ss,
  634. tmp_time.off_dir,
  635. tmp_time.off_mm,
  636. tmp_time.off_hh);
  637. return 1;
  638. }
  639. x = sizeof(buf[0]);
  640. DO(der_encode_utctime(&rsa_time2, buf[0], &x));
  641. if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
  642. fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  643. fprintf(stderr, "\n\n");
  644. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]); printf("\n");
  645. return 1;
  646. }
  647. DO(der_length_utctime(&rsa_time2, &y));
  648. if (y != x) {
  649. fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
  650. return 1;
  651. }
  652. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  653. if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
  654. fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
  655. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  656. tmp_time.YY,
  657. tmp_time.MM,
  658. tmp_time.DD,
  659. tmp_time.hh,
  660. tmp_time.mm,
  661. tmp_time.ss,
  662. tmp_time.off_dir,
  663. tmp_time.off_mm,
  664. tmp_time.off_hh);
  665. return 1;
  666. }
  667. der_set_test();
  668. der_flexi_test();
  669. return der_choice_test();
  670. }
  671. #endif
  672. /* $Source$ */
  673. /* $Revision$ */
  674. /* $Date$ */