der_tests.c 30 KB

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