der_tests.c 30 KB

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