der_test.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include <tomcrypt_test.h>
  4. #if !defined(LTC_DER)
  5. int der_test(void)
  6. {
  7. return CRYPT_NOP;
  8. }
  9. #else
  10. #include <wchar.h>
  11. #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 2
  12. #define LTC_DER_TESTS_PRINT_FLEXI
  13. #endif
  14. static const char s_der_tests_stinky_root_cert[] =
  15. "MIIFETCCA/mgAwIBAgIQbv53JNmv518t5lkCHE272jANBgkqhkiG9w0BAQUFADCB"
  16. "lTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAlVUMRcwFQYDVQQHEw5TYWx0IExha2Ug"
  17. "Q2l0eTEeMBwGA1UEChMVVGhlIFVTRVJUUlVTVCBOZXR3b3JrMSEwHwYDVQQLExho"
  18. "dHRwOi8vd3d3LnVzZXJ0cnVzdC5jb20xHTAbBgNVBAMTFFVUTi1VU0VSRmlyc3Qt"
  19. "T2JqZWN0MB4XDTA4MDQyOTAwMDAwMFoXDTEwMDQyOTIzNTk1OVowgbUxCzAJBgNV"
  20. "BAYTAlVTMQ4wDAYDVQQRDAU0NDE0MzELMAkGA1UECAwCT0gxGTAXBgNVBAcMEE1h"
  21. "eWZpZWxkIFZpbGxhZ2UxEDAOBgNVBAkMB1N1aXRlIEExFDASBgNVBAkMCzc2NyBC"
  22. "ZXRhIERyMSIwIAYDVQQKDBlQcmVlbXB0aXZlIFNvbHV0aW9ucywgTExDMSIwIAYD"
  23. "VQQDDBlQcmVlbXB0aXZlIFNvbHV0aW9ucywgTExDMIIBIjANBgkqhkiG9w0BAQEF"
  24. "AAOCAQ8AMIIBCgKCAQEAzH7ZBkMcBuHx8d2f10RGTHAf7gzzVteGbOihJGH2BwlS"
  25. "ZvNp6WEE4DfL+s1vp0wzk1XeLN5tRjg2qum9YqyCk7okh7pXGy46f5mWbLQiefGA"
  26. "j5UXRcr6WJ3xeACdbXxKrYMV0REia+4Jb2UbFA8S81PjhRon6vcRz76ziUWwt8NC"
  27. "igX+4ZC0skhhKzKszel6KGL7bJCtLG7ukw9DZCrvPCRcKFeM/GwQ6ACMgP88CSCL"
  28. "t1fbIXDH1vd/x2XM3QlaSDN6hYDbef8m1T+9TCkXVKeqG1GYjSUrHzYnCZUmTRrR"
  29. "38jgC3qXxiIpDKW105uM0nlXe2XF9c+ot2MdWvV4TwIDAQABo4IBOTCCATUwHwYD"
  30. "VR0jBBgwFoAU2u1kdBScFDyr3ZmpvVsoTYs8ydgwHQYDVR0OBBYEFK+1HzZE4i28"
  31. "oLIzuqlFR9SspiCIMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMBMGA1Ud"
  32. "JQQMMAoGCCsGAQUFBwMDMBEGCWCGSAGG+EIBAQQEAwIEEDBGBgNVHSAEPzA9MDsG"
  33. "DCsGAQQBsjEBAgEDAjArMCkGCCsGAQUFBwIBFh1odHRwczovL3NlY3VyZS5jb21v"
  34. "ZG8ubmV0L0NQUzBCBgNVHR8EOzA5MDegNaAzhjFodHRwOi8vY3JsLnVzZXJ0cnVz"
  35. "dC5jb20vVVROLVVTRVJGaXJzdC1PYmplY3QuY3JsMCEGA1UdEQQaMBiBFnN1cHBv"
  36. "cnRAcHJlZW1wdGl2ZS5jb20wDQYJKoZIhvcNAQEFBQADggEBAC+JM26Dokvonudl"
  37. "JXe/Yun7IBhimkagZUjbk9l/GQWN6i+v1o95UJ1wGJtBdm2+MxbSaPoNTDZR4B+2"
  38. "lYL9MW57UVmePrnfUPXQKZZG+8gTRDz8+7ol/CEAKmS3MLKCRcH5oe+J5345sGxi"
  39. "FC/KWNKedTNraW95xlg8NTlL2yRP7TMsjvBxgLmkbaFUoXzPTbQWmtovIagIT8GC"
  40. "JeXwdFaRjbamiz3Irl+u7x/mhxdza6RvgBYylXRFMudANpeGsV7gDXlnfzpFDKHQ"
  41. "niVwB7P5sbPFIlmIc+4/xRItkLIRjCVXaepgN9KYu3VOgiSDI6wXiTwP44/LUXQM"
  42. "hetwa7s=";
  43. const char ltc_der_tests_cacert_root_cert[] =
  44. "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290"
  45. "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB"
  46. "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA"
  47. "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO"
  48. "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi"
  49. "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ"
  50. "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC"
  51. "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ"
  52. "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6"
  53. "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y"
  54. "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7"
  55. "w2t6iPGwcswlWyCR7BYCEo8y6RcYSNDHBS4CMEK4JZwFaz+qOqfrU0j36NK2B5jc"
  56. "G8Y0f3/JHIJ6BVgrCFvzOKKrF11myZjXnhCLotLddJr3cQxyYN/Nb5gznZY0dj4k"
  57. "epKwDpUeb+agRThHqtdB7Uq3EvbXG4OKDy7YCbZZ16oE/9KTfWgu3YtLq1i6L43q"
  58. "laegw1SJpfvbi1EinbLDvhG+LJGGi5Z4rSDTii8aP8bQUWWHIbEZAWV/RRyH9XzQ"
  59. "QUxPKZgh/TMfdQwEUfoZd9vUFBzugcMd9Zi3aQaRIt0AUMyBMawSB3s42mhb5ivU"
  60. "fslfrejrckzzAeVLIL+aplfKkQABi6F1ITe1Yw1nPkZPcCBnzsXWWdsC4PDSy826"
  61. "YreQQejdIOQpvGQpQsgi3Hia/0PsmBsJUUtaWsJx8cTLc6nloQsCAwEAAaOCAc4w"
  62. "ggHKMB0GA1UdDgQWBBQWtTIb1Mfz4OaO873SsDrusjkY0TCBowYDVR0jBIGbMIGY"
  63. "gBQWtTIb1Mfz4OaO873SsDrusjkY0aF9pHsweTEQMA4GA1UEChMHUm9vdCBDQTEe"
  64. "MBwGA1UECxMVaHR0cDovL3d3dy5jYWNlcnQub3JnMSIwIAYDVQQDExlDQSBDZXJ0"
  65. "IFNpZ25pbmcgQXV0aG9yaXR5MSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0QGNhY2Vy"
  66. "dC5vcmeCAQAwDwYDVR0TAQH/BAUwAwEB/zAyBgNVHR8EKzApMCegJaAjhiFodHRw"
  67. "czovL3d3dy5jYWNlcnQub3JnL3Jldm9rZS5jcmwwMAYJYIZIAYb4QgEEBCMWIWh0"
  68. "dHBzOi8vd3d3LmNhY2VydC5vcmcvcmV2b2tlLmNybDA0BglghkgBhvhCAQgEJxYl"
  69. "aHR0cDovL3d3dy5jYWNlcnQub3JnL2luZGV4LnBocD9pZD0xMDBWBglghkgBhvhC"
  70. "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg"
  71. "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB"
  72. "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc"
  73. "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg"
  74. "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c"
  75. "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl"
  76. "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY"
  77. "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T"
  78. "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF"
  79. "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum"
  80. "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk"
  81. "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW"
  82. "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD";
  83. const unsigned long ltc_der_tests_cacert_root_cert_size = sizeof(ltc_der_tests_cacert_root_cert);
  84. #ifdef LTC_DER_TESTS_PRINT_FLEXI
  85. #define LTC_DER_PRINT_FLEXI_NO_MAIN
  86. #include "../demos/der_print_flexi.c"
  87. static void s_der_tests_print_flexi(ltc_asn1_list* l)
  88. {
  89. fprintf(stderr, "\n\n");
  90. s_der_print_flexi_i(l, 0);
  91. fprintf(stderr, "\n\n");
  92. }
  93. #else
  94. static void s_der_tests_print_flexi(ltc_asn1_list* l)
  95. {
  96. LTC_UNUSED_PARAM(l);
  97. }
  98. #endif
  99. #ifndef ASN1_FMTSTRING_FMT
  100. #define ASN1_FMTSTRING_FMT "line: %d, type=%d, size=%lu, data=%p, self=%p, next=%p, prev=%p, parent=%p, child=%p"
  101. #define ASN1_FMTSTRING_VAL(l) __LINE__, (l)->type, (l)->size, (l)->data, (l), (l)->next, (l)->prev, (l)->parent, (l)->child
  102. #endif
  103. #define ASN1_ERR(l) fprintf(stderr, ASN1_FMTSTRING_FMT "\n", ASN1_FMTSTRING_VAL(l)); \
  104. exit(EXIT_FAILURE)
  105. #define CHECK_ASN1_HAS(l, w) do { if ((l)->w == NULL) { \
  106. ASN1_ERR(l);\
  107. } } while(0)
  108. #define CHECK_ASN1_HAS_NO(l, w) do { if ((l)->w != NULL) { \
  109. ASN1_ERR(l);\
  110. } } while(0)
  111. #define CHECK_ASN1_TYPE(l, t) do { if ((l)->type != (t)) { \
  112. ASN1_ERR(l);\
  113. } } while(0)
  114. #define CHECK_ASN1_HAS_CHILD(l) CHECK_ASN1_HAS(l, child)
  115. #define CHECK_ASN1_HAS_NO_CHILD(l) CHECK_ASN1_HAS_NO(l, child)
  116. #define CHECK_ASN1_HAS_NEXT(l) CHECK_ASN1_HAS(l, next)
  117. #define CHECK_ASN1_HAS_NO_NEXT(l) CHECK_ASN1_HAS_NO(l, next)
  118. #define CHECK_ASN1_HAS_DATA(l) CHECK_ASN1_HAS(l, data)
  119. #define CHECK_ASN1_HAS_NO_DATA(l) CHECK_ASN1_HAS_NO(l, data)
  120. static void der_cacert_test(void)
  121. {
  122. unsigned char buf[sizeof(ltc_der_tests_cacert_root_cert)];
  123. unsigned long len1 = sizeof(buf), len2;
  124. ltc_asn1_list *decoded_list, *l, *l1, *l2;
  125. DO(base64_decode(s_der_tests_stinky_root_cert, sizeof(s_der_tests_stinky_root_cert), buf, &len1));
  126. len2 = len1;
  127. DO(der_decode_sequence_flexi(buf, &len2, &decoded_list));
  128. der_free_sequence_flexi(decoded_list);
  129. len1 = sizeof(buf);
  130. DO(base64_decode(ltc_der_tests_cacert_root_cert, sizeof(ltc_der_tests_cacert_root_cert), buf, &len1));
  131. len2 = len1;
  132. DO(der_decode_sequence_flexi(buf, &len2, &decoded_list));
  133. CHECK_ASN1_TYPE(decoded_list, LTC_ASN1_SEQUENCE);
  134. CHECK_ASN1_HAS_DATA(decoded_list);
  135. der_sequence_shrink(decoded_list);
  136. CHECK_ASN1_TYPE(decoded_list, LTC_ASN1_SEQUENCE);
  137. CHECK_ASN1_HAS_NO_DATA(decoded_list);
  138. s_der_tests_print_flexi(decoded_list);
  139. l = decoded_list;
  140. /*
  141. SEQUENCE(3 elem)
  142. SEQUENCE(8 elem)
  143. */
  144. CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
  145. CHECK_ASN1_HAS_CHILD(l);
  146. CHECK_ASN1_HAS_NO_NEXT(l);
  147. l = l->child;
  148. CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
  149. CHECK_ASN1_HAS_CHILD(l);
  150. CHECK_ASN1_HAS_NEXT(l);
  151. l1 = l->child;
  152. /*
  153. [0](1)
  154. INTEGER 2
  155. */
  156. CHECK_ASN1_TYPE(l1, LTC_ASN1_CUSTOM_TYPE);
  157. CHECK_ASN1_HAS_CHILD(l1);
  158. CHECK_ASN1_HAS_NEXT(l1);
  159. l2 = l1->child;
  160. CHECK_ASN1_TYPE(l2, LTC_ASN1_INTEGER);
  161. CHECK_ASN1_HAS_NO_CHILD(l2);
  162. CHECK_ASN1_HAS_NO_NEXT(l2);
  163. l1 = l1->next;
  164. /*
  165. INTEGER 0
  166. */
  167. CHECK_ASN1_TYPE(l1, LTC_ASN1_INTEGER);
  168. CHECK_ASN1_HAS_NO_CHILD(l1);
  169. CHECK_ASN1_HAS_NEXT(l1);
  170. l1 = l1->next;
  171. /*
  172. SEQUENCE(2 elem)
  173. OBJECT IDENTIFIER 1.2.840.113549.1.1.4
  174. NULL
  175. */
  176. CHECK_ASN1_TYPE(l1, LTC_ASN1_SEQUENCE);
  177. CHECK_ASN1_HAS_CHILD(l1);
  178. CHECK_ASN1_HAS_NEXT(l1);
  179. l2 = l1->child;
  180. CHECK_ASN1_TYPE(l2, LTC_ASN1_OBJECT_IDENTIFIER);
  181. CHECK_ASN1_HAS_NO_CHILD(l2);
  182. CHECK_ASN1_HAS_NEXT(l2);
  183. l2 = l2->next;
  184. CHECK_ASN1_TYPE(l2, LTC_ASN1_NULL);
  185. CHECK_ASN1_HAS_NO_CHILD(l2);
  186. CHECK_ASN1_HAS_NO_NEXT(l2);
  187. /*
  188. SEQUENCE(4 elem)
  189. SET(1 elem)
  190. SEQUENCE(2 elem)
  191. OBJECT IDENTIFIER 2.5.4.10
  192. PrintableString Root CA
  193. SET(1 elem)
  194. SEQUENCE(2 elem)
  195. OBJECT IDENTIFIER 2.5.4.11
  196. PrintableString http://www.cacert.org
  197. SET(1 elem)
  198. SEQUENCE(2 elem)
  199. OBJECT IDENTIFIER 2.5.4.3
  200. PrintableString CA Cert Signing Authority
  201. SET(1 elem)
  202. SEQUENCE(2 elem)
  203. OBJECT IDENTIFIER 1.2.840.113549.1.9.1
  204. IA5String [email protected]
  205. */
  206. l = l->next;
  207. /*
  208. SEQUENCE(2 elem)
  209. OBJECT IDENTIFIER 1.2.840.113549.1.1.4
  210. NULL
  211. */
  212. CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
  213. CHECK_ASN1_HAS_CHILD(l);
  214. CHECK_ASN1_HAS_NEXT(l);
  215. l1 = l->child;
  216. CHECK_ASN1_TYPE(l1, LTC_ASN1_OBJECT_IDENTIFIER);
  217. CHECK_ASN1_HAS_NO_CHILD(l1);
  218. CHECK_ASN1_HAS_NEXT(l1);
  219. l1 = l1->next;
  220. CHECK_ASN1_TYPE(l1, LTC_ASN1_NULL);
  221. CHECK_ASN1_HAS_NO_CHILD(l1);
  222. CHECK_ASN1_HAS_NO_NEXT(l1);
  223. l = l->next;
  224. /*
  225. BIT STRING(4096 bit)
  226. */
  227. CHECK_ASN1_TYPE(l, LTC_ASN1_BIT_STRING);
  228. CHECK_ASN1_HAS_NO_CHILD(l);
  229. CHECK_ASN1_HAS_NO_NEXT(l);
  230. der_free_sequence_flexi(decoded_list);
  231. }
  232. static void der_set_test(void)
  233. {
  234. ltc_asn1_list list[10];
  235. static const unsigned char oct_str[] = { 1, 2, 3, 4 };
  236. static const unsigned char bin_str[] = { 1, 0, 0, 1 };
  237. static const unsigned long int_val = 12345678UL;
  238. char strs[10][10];
  239. unsigned char outbuf[128];
  240. unsigned long x, val, outlen;
  241. /* make structure and encode it */
  242. LTC_SET_ASN1(list, 0, LTC_ASN1_OCTET_STRING, oct_str, sizeof(oct_str));
  243. LTC_SET_ASN1(list, 1, LTC_ASN1_BIT_STRING, bin_str, sizeof(bin_str));
  244. LTC_SET_ASN1(list, 2, LTC_ASN1_SHORT_INTEGER, &int_val, 1);
  245. /* encode it */
  246. outlen = sizeof(outbuf);
  247. DO(der_encode_set(list, 3, outbuf, &outlen));
  248. /* 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 */
  249. LTC_SET_ASN1(list, 0, LTC_ASN1_BIT_STRING, strs[1], sizeof(strs[1]));
  250. LTC_SET_ASN1(list, 1, LTC_ASN1_SHORT_INTEGER, &val, 1);
  251. LTC_SET_ASN1(list, 2, LTC_ASN1_OCTET_STRING, strs[0], sizeof(strs[0]));
  252. DO(der_decode_set(outbuf, outlen, list, 3));
  253. /* now compare the items */
  254. if (memcmp(strs[0], oct_str, sizeof(oct_str))) {
  255. fprintf(stderr, "error decoding set using der_decode_set (oct_str is wrong):\n");
  256. exit(EXIT_FAILURE);
  257. }
  258. if (memcmp(strs[1], bin_str, sizeof(bin_str))) {
  259. fprintf(stderr, "error decoding set using der_decode_set (bin_str is wrong):\n");
  260. exit(EXIT_FAILURE);
  261. }
  262. if (val != int_val) {
  263. fprintf(stderr, "error decoding set using der_decode_set (int_val is wrong):\n");
  264. exit(EXIT_FAILURE);
  265. }
  266. strcpy(strs[0], "one");
  267. strcpy(strs[1], "one2");
  268. strcpy(strs[2], "two");
  269. strcpy(strs[3], "aaa");
  270. strcpy(strs[4], "aaaa");
  271. strcpy(strs[5], "aab");
  272. strcpy(strs[6], "aaab");
  273. strcpy(strs[7], "bbb");
  274. strcpy(strs[8], "bbba");
  275. strcpy(strs[9], "bbbb");
  276. for (x = 0; x < 10; x++) {
  277. LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], XSTRLEN(strs[x]));
  278. }
  279. outlen = sizeof(outbuf);
  280. DO(der_encode_setof(list, 10, outbuf, &outlen));
  281. for (x = 0; x < 10; x++) {
  282. LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], sizeof(strs[x]) - 1);
  283. }
  284. XMEMSET(strs, 0, sizeof(strs));
  285. DO(der_decode_set(outbuf, outlen, list, 10));
  286. /* now compare */
  287. for (x = 1; x < 10; x++) {
  288. if (!(XSTRLEN(strs[x-1]) <= XSTRLEN(strs[x])) && strcmp(strs[x-1], strs[x]) >= 0) {
  289. fprintf(stderr, "error SET OF order at %lu is wrong\n", x);
  290. exit(EXIT_FAILURE);
  291. }
  292. }
  293. }
  294. static void s_der_oid_test(void)
  295. {
  296. static const unsigned char oid_x690_8_19_5_example[] = { 0x06, 0x03, 0x88, 0x37, 0x03 };
  297. unsigned long len, oid[3];
  298. unsigned char buf[64];
  299. ltc_asn1_list *decoded_list, static_list[1];
  300. len = sizeof(oid_x690_8_19_5_example);
  301. DO(der_decode_sequence_flexi(oid_x690_8_19_5_example, &len, &decoded_list));
  302. LTC_SET_ASN1(static_list, 0, LTC_ASN1_OBJECT_IDENTIFIER, (void *)decoded_list->data, decoded_list->size);
  303. len = sizeof(buf);
  304. DO(der_encode_object_identifier(decoded_list->data, decoded_list->size, buf, &len));
  305. der_sequence_free(decoded_list);
  306. COMPARE_TESTVECTOR(buf, len, oid_x690_8_19_5_example, sizeof(oid_x690_8_19_5_example), "OID X6.90 Ch. 8.19.5 Example", 0);
  307. oid[0] = 3;
  308. oid[1] = 4;
  309. oid[2] = 5;
  310. len = sizeof(buf);
  311. SHOULD_FAIL(der_encode_object_identifier(oid, 3, buf, &len));
  312. len = sizeof(buf);
  313. SHOULD_FAIL(der_length_object_identifier(oid, 3, &len));
  314. }
  315. /* we are encoding
  316. SEQUENCE {
  317. PRINTABLE "printable"
  318. IA5 "ia5"
  319. SEQUENCE {
  320. INTEGER 12345678
  321. UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
  322. GENERALIZEDTIME { 2017, 03, 21, 10, 21, 12, 4, 1, 2, 0 }
  323. SEQUENCE {
  324. OCTET STRING { 1, 2, 3, 4 }
  325. BIT STRING { 1, 0, 0, 1 }
  326. SEQUENCE {
  327. OID { 1, 2, 840, 113549 }
  328. NULL
  329. SET OF {
  330. PRINTABLE "333" -- WILL GET SORTED
  331. PRINTABLE "222"
  332. }
  333. }
  334. }
  335. }
  336. */
  337. static void der_flexi_test(void)
  338. {
  339. static const char printable_str[] = "printable";
  340. static const char set1_str[] = "333";
  341. static const char set2_str[] = "222";
  342. static const char ia5_str[] = "ia5";
  343. static const unsigned long int_val = 12345678UL;
  344. static const ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  345. static const ltc_generalizedtime gtime = { 2017, 03, 21, 10, 21, 12, 421, 1, 2, 0 };
  346. static const unsigned char oct_str[] = { 1, 2, 3, 4 };
  347. static const unsigned char bit_str[] = { 1, 0, 0, 1 };
  348. static const unsigned long oid_str[] = { 1, 2, 840, 113549 };
  349. unsigned char encode_buf[192];
  350. unsigned long encode_buf_len, decode_len;
  351. ltc_asn1_list static_list[5][4], *decoded_list, *l;
  352. /* build list */
  353. LTC_SET_ASN1(static_list[0], 0, LTC_ASN1_PRINTABLE_STRING, (void *)printable_str, XSTRLEN(printable_str));
  354. LTC_SET_ASN1(static_list[0], 1, LTC_ASN1_IA5_STRING, (void *)ia5_str, XSTRLEN(ia5_str));
  355. LTC_SET_ASN1(static_list[0], 2, LTC_ASN1_SEQUENCE, static_list[1], 4);
  356. LTC_SET_ASN1(static_list[1], 0, LTC_ASN1_SHORT_INTEGER, (void *)&int_val, 1);
  357. LTC_SET_ASN1(static_list[1], 1, LTC_ASN1_UTCTIME, (void *)&utctime, 1);
  358. LTC_SET_ASN1(static_list[1], 2, LTC_ASN1_GENERALIZEDTIME, (void *)&gtime, 1);
  359. LTC_SET_ASN1(static_list[1], 3, LTC_ASN1_SEQUENCE, static_list[2], 3);
  360. LTC_SET_ASN1(static_list[2], 0, LTC_ASN1_OCTET_STRING, (void *)oct_str, 4);
  361. LTC_SET_ASN1(static_list[2], 1, LTC_ASN1_BIT_STRING, (void *)bit_str, 4);
  362. LTC_SET_ASN1(static_list[2], 2, LTC_ASN1_SEQUENCE, static_list[3], 3);
  363. LTC_SET_ASN1(static_list[3], 0, LTC_ASN1_OBJECT_IDENTIFIER,(void *)oid_str, 4);
  364. LTC_SET_ASN1(static_list[3], 1, LTC_ASN1_NULL, NULL, 0);
  365. LTC_SET_ASN1(static_list[3], 2, LTC_ASN1_SETOF, static_list[4], 2);
  366. LTC_SET_ASN1(static_list[4], 0, LTC_ASN1_PRINTABLE_STRING, set1_str, XSTRLEN(set1_str));
  367. LTC_SET_ASN1(static_list[4], 1, LTC_ASN1_PRINTABLE_STRING, set2_str, XSTRLEN(set2_str));
  368. /* encode it */
  369. encode_buf_len = sizeof(encode_buf);
  370. DO(der_encode_sequence(&static_list[0][0], 3, encode_buf, &encode_buf_len));
  371. #if 0
  372. {
  373. FILE *f;
  374. f = fopen("t.bin", "wb");
  375. fwrite(encode_buf, 1, encode_buf_len, f);
  376. fclose(f);
  377. }
  378. #endif
  379. /* decode with flexi */
  380. decode_len = encode_buf_len;
  381. DO(der_decode_sequence_flexi(encode_buf, &decode_len, &decoded_list));
  382. if (decode_len != encode_buf_len) {
  383. fprintf(stderr, "Decode len of %lu does not match encode len of %lu \n", decode_len, encode_buf_len);
  384. exit(EXIT_FAILURE);
  385. }
  386. /* we expect l->next to be NULL and l->child to not be */
  387. l = decoded_list;
  388. if (l->next != NULL || l->child == NULL) {
  389. 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);
  390. exit(EXIT_FAILURE);
  391. }
  392. /* we expect a SEQUENCE */
  393. if (l->type != LTC_ASN1_SEQUENCE) {
  394. 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);
  395. exit(EXIT_FAILURE);
  396. }
  397. l = l->child;
  398. /* PRINTABLE STRING */
  399. /* we expect printable_str */
  400. if (l->next == NULL || l->child != NULL) {
  401. 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);
  402. exit(EXIT_FAILURE);
  403. }
  404. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  405. 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);
  406. exit(EXIT_FAILURE);
  407. }
  408. if (l->size != XSTRLEN(printable_str) || memcmp(printable_str, l->data, l->size)) {
  409. 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);
  410. exit(EXIT_FAILURE);
  411. }
  412. /* move to next */
  413. l = l->next;
  414. /* IA5 STRING */
  415. /* we expect ia5_str */
  416. if (l->next == NULL || l->child != NULL) {
  417. 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);
  418. exit(EXIT_FAILURE);
  419. }
  420. if (l->type != LTC_ASN1_IA5_STRING) {
  421. 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);
  422. exit(EXIT_FAILURE);
  423. }
  424. if (l->size != XSTRLEN(ia5_str) || memcmp(ia5_str, l->data, l->size)) {
  425. 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);
  426. exit(EXIT_FAILURE);
  427. }
  428. /* move to next */
  429. l = l->next;
  430. /* expect child anve move down */
  431. if (l->next != NULL || l->child == NULL) {
  432. 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);
  433. exit(EXIT_FAILURE);
  434. }
  435. if (l->type != LTC_ASN1_SEQUENCE) {
  436. 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);
  437. exit(EXIT_FAILURE);
  438. }
  439. l = l->child;
  440. /* INTEGER */
  441. if (l->next == NULL || l->child != NULL) {
  442. 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);
  443. exit(EXIT_FAILURE);
  444. }
  445. if (l->type != LTC_ASN1_INTEGER) {
  446. 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);
  447. exit(EXIT_FAILURE);
  448. }
  449. if (ltc_mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) {
  450. 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);
  451. exit(EXIT_FAILURE);
  452. }
  453. /* move to next */
  454. l = l->next;
  455. /* UTCTIME */
  456. if (l->next == NULL || l->child != NULL) {
  457. 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);
  458. exit(EXIT_FAILURE);
  459. }
  460. if (l->type != LTC_ASN1_UTCTIME) {
  461. 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);
  462. exit(EXIT_FAILURE);
  463. }
  464. if (memcmp(l->data, &utctime, sizeof(utctime))) {
  465. 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);
  466. exit(EXIT_FAILURE);
  467. }
  468. /* move to next */
  469. l = l->next;
  470. /* GeneralizedTime */
  471. if (l->next == NULL || l->child != NULL) {
  472. 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);
  473. exit(EXIT_FAILURE);
  474. }
  475. if (l->type != LTC_ASN1_GENERALIZEDTIME) {
  476. 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);
  477. exit(EXIT_FAILURE);
  478. }
  479. if (memcmp(l->data, &gtime, sizeof(gtime))) {
  480. 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);
  481. exit(EXIT_FAILURE);
  482. }
  483. /* move to next */
  484. l = l->next;
  485. /* expect child anve move down */
  486. if (l->next != NULL || l->child == NULL) {
  487. 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);
  488. exit(EXIT_FAILURE);
  489. }
  490. if (l->type != LTC_ASN1_SEQUENCE) {
  491. 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);
  492. exit(EXIT_FAILURE);
  493. }
  494. l = l->child;
  495. /* OCTET STRING */
  496. /* we expect oct_str */
  497. if (l->next == NULL || l->child != NULL) {
  498. 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);
  499. exit(EXIT_FAILURE);
  500. }
  501. if (l->type != LTC_ASN1_OCTET_STRING) {
  502. 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);
  503. exit(EXIT_FAILURE);
  504. }
  505. if (l->size != sizeof(oct_str) || memcmp(oct_str, l->data, l->size)) {
  506. 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);
  507. exit(EXIT_FAILURE);
  508. }
  509. /* move to next */
  510. l = l->next;
  511. /* BIT STRING */
  512. /* we expect oct_str */
  513. if (l->next == NULL || l->child != NULL) {
  514. 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);
  515. exit(EXIT_FAILURE);
  516. }
  517. if (l->type != LTC_ASN1_BIT_STRING) {
  518. 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);
  519. exit(EXIT_FAILURE);
  520. }
  521. if (l->size != sizeof(bit_str) || memcmp(bit_str, l->data, l->size)) {
  522. 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);
  523. exit(EXIT_FAILURE);
  524. }
  525. /* move to next */
  526. l = l->next;
  527. /* expect child anve move down */
  528. if (l->next != NULL || l->child == NULL) {
  529. 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);
  530. exit(EXIT_FAILURE);
  531. }
  532. if (l->type != LTC_ASN1_SEQUENCE) {
  533. 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);
  534. exit(EXIT_FAILURE);
  535. }
  536. l = l->child;
  537. /* OID STRING */
  538. /* we expect oid_str */
  539. if (l->next == NULL || l->child != NULL) {
  540. 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);
  541. exit(EXIT_FAILURE);
  542. }
  543. if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) {
  544. 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);
  545. exit(EXIT_FAILURE);
  546. }
  547. if (l->size != LTC_ARRAY_SIZE(oid_str) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
  548. 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);
  549. exit(EXIT_FAILURE);
  550. }
  551. /* move to next */
  552. l = l->next;
  553. /* NULL */
  554. if (l->type != LTC_ASN1_NULL) {
  555. 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);
  556. exit(EXIT_FAILURE);
  557. }
  558. /* move to next */
  559. l = l->next;
  560. /* expect child anve move down */
  561. if (l->next != NULL || l->child == NULL) {
  562. 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);
  563. exit(EXIT_FAILURE);
  564. }
  565. if (l->type != LTC_ASN1_SET) {
  566. 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);
  567. exit(EXIT_FAILURE);
  568. }
  569. l = l->child;
  570. /* PRINTABLE STRING */
  571. /* we expect printable_str */
  572. if (l->next == NULL || l->child != NULL) {
  573. 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);
  574. exit(EXIT_FAILURE);
  575. }
  576. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  577. 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);
  578. exit(EXIT_FAILURE);
  579. }
  580. /* note we compare set2_str FIRST because the SET OF is sorted and "222" comes before "333" */
  581. if (l->size != XSTRLEN(set2_str) || memcmp(set2_str, l->data, l->size)) {
  582. 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);
  583. exit(EXIT_FAILURE);
  584. }
  585. /* move to next */
  586. l = l->next;
  587. /* PRINTABLE STRING */
  588. /* we expect printable_str */
  589. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  590. 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);
  591. exit(EXIT_FAILURE);
  592. }
  593. if (l->size != XSTRLEN(set1_str) || memcmp(set1_str, l->data, l->size)) {
  594. 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);
  595. exit(EXIT_FAILURE);
  596. }
  597. der_sequence_free(l);
  598. }
  599. static int der_choice_n_custom_test(void)
  600. {
  601. ltc_asn1_list types[10], host[1], custom[1], root[1], child[1];
  602. int boolean[1];
  603. unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256], custbuf[256], x, y;
  604. wchar_t utf8buf[10];
  605. unsigned long integer, oidbuf[10], outlen, custlen, inlen, n;
  606. void *mpinteger;
  607. ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  608. ltc_generalizedtime gtime = { 2038, 01, 19, 3, 14, 8, 0, 0, 0, 0 };
  609. /* setup variables */
  610. for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; }
  611. for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; }
  612. for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; }
  613. for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; }
  614. for (x = 0; x < LTC_ARRAY_SIZE(utf8buf); x++) { utf8buf[x] = L'a'; }
  615. integer = 1;
  616. boolean[0] = 1;
  617. for (x = 0; x < LTC_ARRAY_SIZE(oidbuf); x++) { oidbuf[x] = x + 1; }
  618. DO(ltc_mp_init(&mpinteger));
  619. n = LTC_ARRAY_SIZE(types);
  620. for (x = 0; x < n * 2; x++) {
  621. /* setup list */
  622. y = 0;
  623. LTC_SET_ASN1(types, y++, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
  624. if (x > n) {
  625. LTC_SET_ASN1(types, y++, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
  626. } else {
  627. LTC_SET_ASN1(types, y++, LTC_ASN1_RAW_BIT_STRING, bitbuf, sizeof(bitbuf));
  628. }
  629. LTC_SET_ASN1(types, y++, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
  630. LTC_SET_ASN1(types, y++, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
  631. LTC_SET_ASN1(types, y++, LTC_ASN1_BOOLEAN, boolean, LTC_ARRAY_SIZE(boolean));
  632. if (x > n) {
  633. LTC_SET_ASN1(types, y++, LTC_ASN1_SHORT_INTEGER, &integer, 1);
  634. } else {
  635. LTC_SET_ASN1(types, y++, LTC_ASN1_INTEGER, mpinteger, 1);
  636. }
  637. LTC_SET_ASN1(types, y++, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, LTC_ARRAY_SIZE(oidbuf));
  638. if (x > n) {
  639. LTC_SET_ASN1(types, y++, LTC_ASN1_UTCTIME, &utctime, 1);
  640. } else {
  641. LTC_SET_ASN1(types, y++, LTC_ASN1_GENERALIZEDTIME, &gtime, 1);
  642. }
  643. LTC_SET_ASN1(custom, 0, LTC_ASN1_NULL, NULL, 0);
  644. LTC_SET_ASN1_CUSTOM_CONSTRUCTED(types, y++, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom);
  645. LTC_SET_ASN1(types, y++, LTC_ASN1_UTF8_STRING, utf8buf, LTC_ARRAY_SIZE(utf8buf));
  646. LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, n);
  647. /* encode */
  648. outlen = sizeof(outbuf);
  649. DO(der_encode_sequence(&types[x % n], 1, outbuf, &outlen));
  650. /* custom encode */
  651. child[0] = types[x % n];
  652. if (x < n) {
  653. LTC_SET_ASN1_CUSTOM_CONSTRUCTED(root, 0, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1uLL << (x % n), child);
  654. } else {
  655. LTC_SET_ASN1_CUSTOM_PRIMITIVE(root, 0, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1uLL << (x % n), child->type, child->data, child->size);
  656. }
  657. custlen = sizeof(custbuf);
  658. /* don't try to custom-encode a primitive custom-type */
  659. if (child[0].type != LTC_ASN1_CUSTOM_TYPE || root->pc != LTC_ASN1_PC_PRIMITIVE) {
  660. DO(der_encode_custom_type(root, custbuf, &custlen));
  661. }
  662. /* decode it */
  663. inlen = outlen;
  664. DO(der_decode_sequence(outbuf, inlen, host, 1));
  665. for (y = 0; y < n; y++) {
  666. if (types[y].used && y != (x % n)) {
  667. fprintf(stderr, "CHOICE, flag %u in trial %u was incorrectly set to one\n", y, x);
  668. return 1;
  669. }
  670. if (!types[y].used && y == (x % n)) {
  671. fprintf(stderr, "CHOICE, flag %u in trial %u was incorrectly set to zero\n", y, x);
  672. return 1;
  673. }
  674. }
  675. /* custom decode */
  676. if (child[0].type != LTC_ASN1_CUSTOM_TYPE || root->pc != LTC_ASN1_PC_PRIMITIVE) {
  677. DO(der_decode_custom_type(custbuf, custlen, root));
  678. }
  679. }
  680. ltc_mp_clear(mpinteger);
  681. return 0;
  682. }
  683. static void s_der_decode_print(const void* p, unsigned long* plen)
  684. {
  685. ltc_asn1_list *list;
  686. DO(der_decode_sequence_flexi(p, plen, &list));
  687. s_der_tests_print_flexi(list);
  688. der_sequence_free(list);
  689. }
  690. static const unsigned char eckey_privc_der[] = {
  691. 0x30, 0x81, 0xf0, 0x02, 0x01, 0x01, 0x04, 0x18, 0x96, 0x9d, 0x28, 0xf2, 0x40, 0x48, 0x19, 0x11,
  692. 0x79, 0xb0, 0x47, 0x8e, 0x8c, 0x6b, 0x3d, 0x9b, 0xf2, 0x31, 0x16, 0x10, 0x08, 0x72, 0xb1, 0x86,
  693. 0xa0, 0x81, 0xb2, 0x30, 0x81, 0xaf, 0x02, 0x01, 0x01, 0x30, 0x24, 0x06, 0x07, 0x2a, 0x86, 0x48,
  694. 0xce, 0x3d, 0x01, 0x01, 0x02, 0x19, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  695. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x30,
  696. 0x4b, 0x04, 0x18, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  697. 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x04, 0x18, 0x22, 0x12, 0x3d,
  698. 0xc2, 0x39, 0x5a, 0x05, 0xca, 0xa7, 0x42, 0x3d, 0xae, 0xcc, 0xc9, 0x47, 0x60, 0xa7, 0xd4, 0x62,
  699. 0x25, 0x6b, 0xd5, 0x69, 0x16, 0x03, 0x15, 0x00, 0xc4, 0x69, 0x68, 0x44, 0x35, 0xde, 0xb3, 0x78,
  700. 0xc4, 0xb6, 0x5c, 0xa9, 0x59, 0x1e, 0x2a, 0x57, 0x63, 0x05, 0x9a, 0x2e, 0x04, 0x19, 0x02, 0x7d,
  701. 0x29, 0x77, 0x81, 0x00, 0xc6, 0x5a, 0x1d, 0xa1, 0x78, 0x37, 0x16, 0x58, 0x8d, 0xce, 0x2b, 0x8b,
  702. 0x4a, 0xee, 0x8e, 0x22, 0x8f, 0x18, 0x96, 0x02, 0x19, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  703. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7a, 0x62, 0xd0, 0x31, 0xc8, 0x3f, 0x42, 0x94, 0xf6, 0x40,
  704. 0xec, 0x13, 0x02, 0x01, 0x01, 0xa1, 0x1c, 0x03, 0x1a, 0x00, 0x02, 0x55, 0x2c, 0xb8, 0x73, 0x5c,
  705. 0x9d, 0x98, 0xe4, 0x57, 0xfe, 0xd5, 0x96, 0x0a, 0x73, 0x8d, 0x82, 0xd7, 0xce, 0x05, 0xa9, 0x79,
  706. 0x91, 0x5c, 0xf9
  707. };
  708. static const unsigned char eckey_privs_der[] = {
  709. 0x30, 0x50, 0x02, 0x01, 0x01, 0x04, 0x14, 0x82, 0xef, 0x42, 0x0b, 0xc7, 0xe2, 0x9f, 0x3a, 0x84,
  710. 0xe5, 0x74, 0xec, 0x9c, 0xc5, 0x10, 0x26, 0x63, 0x8d, 0xb5, 0x46, 0xa0, 0x07, 0x06, 0x05, 0x2b,
  711. 0x81, 0x04, 0x00, 0x09, 0xa1, 0x2c, 0x03, 0x2a, 0x00, 0x04, 0xb5, 0xb1, 0x5a, 0xb0, 0x2a, 0x10,
  712. 0xd1, 0xf5, 0x4d, 0x6a, 0x41, 0xde, 0xcd, 0x69, 0x09, 0xb3, 0x5f, 0x26, 0xb0, 0xa2, 0xaf, 0xd3,
  713. 0x02, 0x89, 0x5e, 0xd4, 0x96, 0x5c, 0xbc, 0x2a, 0x7e, 0x75, 0x85, 0x86, 0x29, 0xb3, 0x29, 0x13,
  714. 0x77, 0xc3
  715. };
  716. static void der_custom_test(void)
  717. {
  718. ltc_asn1_list bool_ean[1], seq1[1], custom[1];
  719. int boolean;
  720. unsigned long len;
  721. unsigned char buf[1024];
  722. unsigned char buf1[] = { 0xbf, 0xa0, 0x00, 0x04, 0x30, 0x02, 0x05, 0x00 };
  723. unsigned char buf2[] = { 0x30, 0x08, 0xbf, 0xa0, 0x00, 0x04, 0x30, 0x02, 0x05, 0x00 };
  724. boolean = 0x1;
  725. LTC_SET_ASN1(bool_ean, 0, LTC_ASN1_BOOLEAN, &boolean, 1);
  726. LTC_SET_ASN1(seq1, 0, LTC_ASN1_SEQUENCE, bool_ean, 1);
  727. LTC_SET_ASN1_CUSTOM_CONSTRUCTED(custom, 0, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0x1000, seq1);
  728. DO(der_length_custom_type(custom, &len, NULL));
  729. len = sizeof(buf);
  730. DO(der_encode_custom_type(custom, buf, &len));
  731. s_der_decode_print(buf, &len);
  732. boolean = 0x0;
  733. DO(der_decode_custom_type(buf, len, custom));
  734. DO(der_length_sequence(custom, 1, &len));
  735. len = sizeof(buf);
  736. DO(der_encode_sequence(custom, 1, buf, &len));
  737. s_der_decode_print(buf, &len);
  738. boolean = 0x0;
  739. DO(der_decode_sequence(buf, len, custom, 1));
  740. LTC_SET_ASN1_CUSTOM_PRIMITIVE(bool_ean, 0, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0x8000, LTC_ASN1_BOOLEAN, &boolean, 1);
  741. DO(der_length_custom_type(bool_ean, &len, NULL));
  742. len = sizeof(buf);
  743. DO(der_encode_custom_type(bool_ean, buf, &len));
  744. s_der_decode_print(buf, &len);
  745. LTC_SET_ASN1_CUSTOM_PRIMITIVE(bool_ean, 0, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0x8000, LTC_ASN1_BOOLEAN, &boolean, 1);
  746. DO(der_decode_custom_type(buf, len, bool_ean));
  747. len = sizeof(buf1);
  748. s_der_decode_print(buf1, &len);
  749. len = sizeof(buf2);
  750. s_der_decode_print(buf2, &len);
  751. len = sizeof(eckey_privc_der);
  752. s_der_decode_print(eckey_privc_der, &len);
  753. len = sizeof(eckey_privs_der);
  754. s_der_decode_print(eckey_privs_der, &len);
  755. }
  756. typedef int (*s_der_Xcode)(const void*, unsigned long, void*, unsigned long*);
  757. typedef struct {
  758. s_der_Xcode encode;
  759. s_der_Xcode decode;
  760. const void* in;
  761. size_t in_sz;
  762. size_t factor;
  763. size_t type_sz;
  764. const char* what;
  765. } der_Xcode_t;
  766. static void der_Xcode_run(const der_Xcode_t* x)
  767. {
  768. unsigned long l1, l2, sz;
  769. void *d1, *d2;
  770. l1 = 1;
  771. d1 = XMALLOC(l1 * x->type_sz);
  772. sz = (x->in_sz * x->factor)/x->type_sz;
  773. if (x->encode(x->in, sz, d1, &l1) == CRYPT_BUFFER_OVERFLOW) {
  774. d1 = XREALLOC(d1, l1 * x->type_sz);
  775. }
  776. DO(x->encode(x->in, sz, d1, &l1));
  777. l2 = 1;
  778. d2 = XMALLOC(l2 * x->type_sz);
  779. while (x->decode(d1, l1, d2, &l2) == CRYPT_BUFFER_OVERFLOW) {
  780. d2 = XREALLOC(d2, l2 * x->type_sz);
  781. }
  782. DO(x->decode(d1, l1, d2, &l2));
  783. COMPARE_TESTVECTOR(d2, (l2/x->factor) * x->type_sz, x->in, x->in_sz, x->what, __LINE__);
  784. XFREE(d2);
  785. XFREE(d1);
  786. }
  787. #if defined(_MSC_VER)
  788. #define typeof(x) x
  789. #endif
  790. #define DER_XCODE_X(n, b, x) { \
  791. (s_der_Xcode)der_encode_ ## n, \
  792. (s_der_Xcode)der_decode_ ## n, \
  793. b, \
  794. sizeof(b), \
  795. x, \
  796. sizeof(typeof(b[0])),\
  797. #n \
  798. }
  799. #define DER_XCODE(n, b) DER_XCODE_X(n, b, 1)
  800. static void der_Xcode_test(void)
  801. {
  802. unsigned long i;
  803. ltc_asn1_list *list;
  804. ltc_asn1_list ttex_neg_int[2];
  805. unsigned char buf[128];
  806. void* mpinteger;
  807. const unsigned long oid[3] = { 1, 23, 42 };
  808. const unsigned char bit_string[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };
  809. const unsigned char multi_buf[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  810. const char multi_string[] = {'l','i','b','t','o','m','c','r','y','p','t'};
  811. const wchar_t wchar_string[] = L"libtomcrypt";
  812. const unsigned char teletex_neg_int[] = { 0x30, 0x11, 0x14, 0x0b, 0x6c, 0x69, 0x62, 0x74,
  813. 0x6f, 0x6d, 0x63, 0x72, 0x79, 0x70, 0x74, 0x02,
  814. 0x02, 0xfc, 0x19 };
  815. const der_Xcode_t xcode_tests[] =
  816. {
  817. DER_XCODE(bit_string, bit_string),
  818. DER_XCODE_X(raw_bit_string, multi_buf, 8),
  819. DER_XCODE(octet_string, multi_buf),
  820. DER_XCODE(object_identifier, oid),
  821. DER_XCODE(ia5_string, multi_string),
  822. DER_XCODE(printable_string, multi_string),
  823. DER_XCODE(utf8_string, wchar_string),
  824. };
  825. for (i = 0; i < LTC_ARRAY_SIZE(xcode_tests); ++i) {
  826. der_Xcode_run(&xcode_tests[i]);
  827. }
  828. i = sizeof(teletex_neg_int);
  829. DO(der_decode_sequence_flexi(teletex_neg_int, &i, &list));
  830. s_der_tests_print_flexi(list);
  831. if (list->child == NULL || list->child->next == NULL)
  832. exit(EXIT_FAILURE);
  833. ttex_neg_int[0] = *list->child->next;
  834. i = sizeof(buf);
  835. DO(der_encode_sequence(ttex_neg_int, 1, buf, &i));
  836. der_sequence_free(list);
  837. DO(ltc_mp_init(&mpinteger));
  838. LTC_SET_ASN1(ttex_neg_int, 0, LTC_ASN1_TELETEX_STRING, buf, sizeof(buf));
  839. LTC_SET_ASN1(ttex_neg_int, 1, LTC_ASN1_INTEGER, mpinteger, 1);
  840. DO(der_decode_sequence(teletex_neg_int, sizeof(teletex_neg_int), ttex_neg_int, 2));
  841. ltc_mp_clear(mpinteger);
  842. }
  843. #ifdef LTC_TEST_READDIR
  844. static int s_der_decode_sequence_flexi(const void *in, unsigned long inlen, void* ctx)
  845. {
  846. ltc_asn1_list** list = ctx;
  847. if (der_decode_sequence_flexi(in, &inlen, list) == CRYPT_OK) {
  848. s_der_tests_print_flexi(*list);
  849. der_sequence_free(*list);
  850. }
  851. return CRYPT_OK;
  852. }
  853. #endif
  854. static void s_der_regression_test(void)
  855. {
  856. static const unsigned char s_broken_sequence[] = {
  857. 0x30,0x41,0x02,0x84,0x7f,0xff,0xff,0xff,0x1e,0x41,0xb4,0x79,0xad,0x57,0x69,
  858. 0x05,0xb9,0x60,0xfe,0x14,0xea,0xdb,0x91,0xb0,0xcc,0xf3,0x48,0x43,0xda,0xb9,
  859. 0x16,0x17,0x3b,0xb8,0xc9,0xcd,0x02,0x1d,0x00,0xad,0xe6,0x59,0x88,0xd2,0x37,
  860. 0xd3,0x0f,0x9e,0xf4,0x1d,0xd4,0x24,0xa4,0xe1,0xc8,0xf1,0x69,0x67,0xcf,0x33,
  861. 0x65,0x81,0x3f,0xe8,0x78,0x62,0x36
  862. };
  863. static const unsigned char s_addtl_bytes[] = {
  864. 0x30,0x45,0x02,0x21,0x00,0xb7,0xba,0xba,0xe9,0x33,0x2b,0x54,0xb8,0xa3,0xa0,0x5b,0x70,0x04,0x57,
  865. 0x98,0x21,0xa8,0x87,0xa1,0xb2,0x14,0x65,0xf7,0xdb,0x8a,0x3d,0x49,0x1b,0x39,0xfd,0x2c,0x3f,0x02,
  866. 0x20,0x74,0x72,0x91,0xdd,0x2f,0x3f,0x44,0xaf,0x7a,0xce,0x68,0xea,0x33,0x43,0x1d,0x6f,0x94,0xe4,
  867. 0x18,0xc1,0x06,0xa6,0xe7,0x62,0x85,0xcd,0x59,0xf4,0x32,0x60,0xec,0xce,0x00,0x00
  868. };
  869. static const unsigned char issue_507[] = "\x30\x04" /* Start DER-sequence of length 4 */
  870. "\x0c\x02\xbf\xbf" /* Start UTF8 string of actual length 2 and evaluated length 3 */
  871. "\xaa" /* One byte padding */
  872. "\x04\x82\xff\xff"; /* Start OCTET sequence of length 0xffff */
  873. /* (this will include the adjacent data into the decoded certificate) */
  874. static const unsigned char utf8_length[] = "\x0c\x02\x61\x61\x61";
  875. wchar_t wtmp[4];
  876. unsigned long len, outlen;
  877. void *x, *y;
  878. ltc_asn1_list seq[2];
  879. ltc_asn1_list *l;
  880. ltc_mp_init_multi(&x, &y, LTC_NULL);
  881. LTC_SET_ASN1(seq, 0, LTC_ASN1_INTEGER, x, 1UL);
  882. LTC_SET_ASN1(seq, 1, LTC_ASN1_INTEGER, y, 1UL);
  883. SHOULD_FAIL(der_decode_sequence(s_broken_sequence, sizeof(s_broken_sequence), seq, 2));
  884. ltc_mp_cleanup_multi(&y, &x, LTC_NULL);
  885. len = sizeof(s_broken_sequence);
  886. ltc_mp_init_multi(&x, &y, LTC_NULL);
  887. LTC_SET_ASN1(seq, 0, LTC_ASN1_INTEGER, x, 1UL);
  888. LTC_SET_ASN1(seq, 1, LTC_ASN1_INTEGER, y, 1UL);
  889. SHOULD_FAIL_WITH(der_decode_sequence(s_addtl_bytes, sizeof(s_addtl_bytes), seq, 2), CRYPT_INPUT_TOO_LONG);
  890. ltc_mp_cleanup_multi(&y, &x, LTC_NULL);
  891. len = sizeof(s_addtl_bytes);
  892. s_der_decode_print(s_addtl_bytes, &len);
  893. len = sizeof(issue_507);
  894. SHOULD_FAIL(der_decode_sequence_flexi(issue_507, &len, &l));
  895. len = sizeof(utf8_length);
  896. outlen = LTC_ARRAY_SIZE(wtmp);
  897. DO(der_decode_utf8_string(utf8_length, len, wtmp, &outlen));
  898. ENSURE(outlen == 2);
  899. }
  900. static void der_toolong_test(void)
  901. {
  902. int n, err, failed = 0;
  903. ltc_asn1_list *list;
  904. unsigned long len, oid[16];
  905. unsigned char buf5[5], buf12[12], buf32[32];
  906. static const unsigned char invalid1[] = {
  907. 0x30,0x19, /* SEQUENCE len=25 bytes */
  908. 0x30,0x0a, /* SEQUENCE len=10 bytes (which is wrong, should be 9) */
  909. 0x04,0x05, /* OCTET STRING len=5 */ 0x2b,0x0e,0x03,0x02,0x1a,
  910. 0x05,0x00, /* NULL */
  911. 0x04,0x0c, /* OCTET STRING len=12 */ 0xf7,0xff,0x9e,0x8b,0x7b,0xb2,0xe0,0x9b,0x70,0x93,0x5a,0x5d,
  912. };
  913. static const unsigned char invalid2[] = {
  914. 0x30,0x0d, /* SEQUENCE len=13 bytes*/
  915. 0x02,0x05, /* INTEGER len=5 */ 0x00,0xb7,0xba,0xba,0xe9,
  916. 0x02,0x04, /* INTEGER len=4 */ 0x74,0x72,0x91,0xdd,
  917. 0x00,0x00 /* garbage after the sequence, der_decode_sequence_flexi should ignore this */
  918. };
  919. static const unsigned char invalid3[] = {
  920. 0x30,0x0f, /* SEQUENCE len=15 bytes*/
  921. 0x02,0x05, /* INTEGER len=5 */ 0x00,0xb7,0xba,0xba,0xe9,
  922. 0x02,0x04, /* INTEGER len=4 */ 0x74,0x72,0x91,0xdd,
  923. 0x00,0x00 /* garbage inside the sequence */
  924. };
  925. static const unsigned char invalid4[] = {
  926. 0x30, 0x30,
  927. 0x30, 0x0d,
  928. 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  929. 0x05, 0x00,
  930. 0x04, 0x20, 0x53, 0x2e, 0xaa, 0xbd, 0x95, 0x74, 0x88, 0x0d, 0xbf, 0x76, 0xb9, 0xb8, 0xcc, 0x00, 0x83, 0x2c,
  931. 0x20, 0xa6, 0xec, 0x11, 0x3d, 0x68, 0x22, 0x99, 0x55, 0x0d, 0x7a, 0x6e, 0x0f, 0x34, 0x5e, 0x25
  932. };
  933. static const unsigned char invalid5[] = {
  934. 0x30, 0x31,
  935. 0x30, 0x0e,
  936. 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  937. 0x05, 0x00,
  938. 0x04, 0x20, 0x53, 0x2e, 0xaa, 0xbd, 0x95,0x74, 0x88, 0x0d, 0xbf, 0x76, 0xb9, 0xb8, 0xcc,0x00, 0x83, 0x2c,
  939. 0x20, 0xa6, 0xec, 0x11, 0x3d,0x68, 0x22, 0x99, 0x55, 0x0d, 0x7a, 0x6e, 0x0f,0x34, 0x5e, 0x25
  940. };
  941. static const unsigned char invalid6[] = {
  942. 0x30, 0x31,
  943. 0x30, 0x0c,
  944. 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  945. 0x05, 0x00,
  946. 0x04, 0x20, 0x53, 0x2e, 0xaa, 0xbd, 0x95,0x74, 0x88, 0x0d, 0xbf, 0x76, 0xb9, 0xb8, 0xcc,0x00, 0x83, 0x2c,
  947. 0x20, 0xa6, 0xec, 0x11, 0x3d,0x68, 0x22, 0x99, 0x55, 0x0d, 0x7a, 0x6e, 0x0f,0x34, 0x5e, 0x25
  948. };
  949. ltc_asn1_list seqsub[2], seqoid[2], seqmain[2], seqint[2];
  950. void *int1, *int2;
  951. LTC_SET_ASN1(seqsub, 0, LTC_ASN1_OCTET_STRING, buf5, 5);
  952. LTC_SET_ASN1(seqsub, 1, LTC_ASN1_NULL, NULL, 0);
  953. LTC_SET_ASN1(seqmain, 0, LTC_ASN1_SEQUENCE, seqsub, 2);
  954. LTC_SET_ASN1(seqmain, 1, LTC_ASN1_OCTET_STRING, buf12, 12);
  955. n = 1;
  956. len = sizeof(invalid1);
  957. err = der_decode_sequence_strict(invalid1, len, seqmain, 2);
  958. if (err == CRYPT_OK) {
  959. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence\n", n);
  960. failed = 1;
  961. }
  962. len = sizeof(invalid1);
  963. err = der_decode_sequence_flexi(invalid1, &len, &list);
  964. if (err == CRYPT_OK) {
  965. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence_flexi\n", n);
  966. failed = 1;
  967. der_sequence_free(list);
  968. }
  969. ltc_mp_init_multi(&int1, &int2, LTC_NULL);
  970. LTC_SET_ASN1(seqint, 0, LTC_ASN1_INTEGER, int1, 1);
  971. LTC_SET_ASN1(seqint, 1, LTC_ASN1_INTEGER, int2, 1);
  972. n++;
  973. len = sizeof(invalid2);
  974. err = der_decode_sequence_strict(invalid2, len, seqint, 2);
  975. if (err == CRYPT_OK) {
  976. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence\n", n);
  977. failed = 1;
  978. }
  979. len = sizeof(invalid2);
  980. err = der_decode_sequence_flexi(invalid2, &len, &list);
  981. /* flexi parser should decode this; however returning "len" shorter than "sizeof(invalid2)" */
  982. if (err != CRYPT_OK || len != 15) {
  983. fprintf(stderr,"der_decode_sequence_flexi failed, err=%d (expected 0) len=%lu (expected 15)\n", err, len);
  984. failed = 1;
  985. }
  986. if (err == CRYPT_OK)
  987. der_sequence_free(list);
  988. n++;
  989. len = sizeof(invalid3);
  990. err = der_decode_sequence_strict(invalid3, len, seqint, 2);
  991. if (err == CRYPT_OK) {
  992. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence\n", n);
  993. failed = 1;
  994. }
  995. len = sizeof(invalid3);
  996. err = der_decode_sequence_flexi(invalid3, &len, &list);
  997. if (err == CRYPT_OK) {
  998. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence_flexi\n", n);
  999. failed = 1;
  1000. der_sequence_free(list);
  1001. }
  1002. ltc_mp_deinit_multi(int1, int2, LTC_NULL);
  1003. LTC_SET_ASN1(seqoid, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, LTC_ARRAY_SIZE(oid));
  1004. LTC_SET_ASN1(seqoid, 1, LTC_ASN1_NULL, NULL, 0);
  1005. LTC_SET_ASN1(seqmain, 0, LTC_ASN1_SEQUENCE, seqoid, 2);
  1006. LTC_SET_ASN1(seqmain, 1, LTC_ASN1_OCTET_STRING, buf32, 32);
  1007. n++;
  1008. len = sizeof(invalid4);
  1009. err = der_decode_sequence_strict(invalid4, len, seqmain, 2);
  1010. if (err == CRYPT_OK) {
  1011. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence\n", n);
  1012. failed = 1;
  1013. }
  1014. len = sizeof(invalid4);
  1015. err = der_decode_sequence_flexi(invalid4, &len, &list);
  1016. if (err == CRYPT_OK) {
  1017. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence_flexi\n", n);
  1018. failed = 1;
  1019. der_sequence_free(list);
  1020. }
  1021. n++;
  1022. len = sizeof(invalid5);
  1023. err = der_decode_sequence_strict(invalid5, len, seqmain, 2);
  1024. if (err == CRYPT_OK) {
  1025. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence\n", n);
  1026. failed = 1;
  1027. }
  1028. len = sizeof(invalid5);
  1029. err = der_decode_sequence_flexi(invalid5, &len, &list);
  1030. if (err == CRYPT_OK) {
  1031. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence_flexi\n", n);
  1032. failed = 1;
  1033. der_sequence_free(list);
  1034. }
  1035. n++;
  1036. len = sizeof(invalid6);
  1037. err = der_decode_sequence_strict(invalid6, len, seqmain, 2);
  1038. if (err == CRYPT_OK) {
  1039. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence\n", n);
  1040. failed = 1;
  1041. }
  1042. len = sizeof(invalid6);
  1043. err = der_decode_sequence_flexi(invalid6, &len, &list);
  1044. if (err == CRYPT_OK) {
  1045. fprintf(stderr,"Sequence invalid%d accepted by der_decode_sequence_flexi\n", n);
  1046. failed = 1;
  1047. der_sequence_free(list);
  1048. }
  1049. if (failed) exit(EXIT_FAILURE);
  1050. }
  1051. static void s_der_recursion_limit(void)
  1052. {
  1053. unsigned int n, m;
  1054. unsigned long integer = 123, s;
  1055. ltc_asn1_list seqs[LTC_DER_MAX_RECURSION + 2], dummy[1], *flexi;
  1056. unsigned char buf[2048];
  1057. for (m = 0; m < 3; ++m) {
  1058. LTC_SET_ASN1(dummy, 0, LTC_ASN1_SHORT_INTEGER, &integer, 1);
  1059. LTC_SET_ASN1(seqs, LTC_DER_MAX_RECURSION + 1, LTC_ASN1_SEQUENCE, dummy, 1);
  1060. for (n = m; n < LTC_DER_MAX_RECURSION + 1; ++n) {
  1061. LTC_SET_ASN1(seqs, LTC_DER_MAX_RECURSION - n, LTC_ASN1_SEQUENCE, &seqs[LTC_DER_MAX_RECURSION - n + 1], 1);
  1062. }
  1063. s = sizeof(buf);
  1064. DO(der_encode_sequence(&seqs[m], 1, buf, &s));
  1065. DO(der_decode_sequence(buf, s, &seqs[m], 1));
  1066. if (m < 2) {
  1067. SHOULD_FAIL(der_decode_sequence_flexi(buf, &s, &flexi));
  1068. }
  1069. else {
  1070. DO(der_decode_sequence_flexi(buf, &s, &flexi));
  1071. der_free_sequence_flexi(flexi);
  1072. }
  1073. }
  1074. }
  1075. int der_test(void)
  1076. {
  1077. unsigned long x, y, z, zz, oid[2][32];
  1078. unsigned char buf[3][2048];
  1079. void *a, *b, *c, *d, *e, *f, *g;
  1080. static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
  1081. static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 };
  1082. static const unsigned char rsa_ia5[] = "[email protected]";
  1083. static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
  1084. 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
  1085. unsigned long rsa_ia5_len = XSTRLEN((char*)rsa_ia5);
  1086. static const unsigned char rsa_printable[] = "Test User 1";
  1087. static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
  1088. 0x73, 0x65, 0x72, 0x20, 0x31 };
  1089. unsigned long rsa_printable_len = XSTRLEN((char*)rsa_printable);
  1090. static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  1091. static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
  1092. ltc_utctime tmp_time;
  1093. 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 };
  1094. static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
  1095. static const wchar_t utf8_1[] = { 0x0041, 0x2262, 0x0391, 0x002E };
  1096. static const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E };
  1097. static const wchar_t utf8_2[] = { 0xD55C, 0xAD6D, 0xC5B4 };
  1098. static const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 };
  1099. static const wchar_t utf8_3[] = { 0x05E9, 0x05DC, 0x05D5, 0x05DD };
  1100. static const unsigned char utf8_3_der[] = { 0x0C, 0x08, 0xD7, 0xA9, 0xD7, 0x9C, 0xD7, 0x95, 0xD7, 0x9D };
  1101. unsigned char utf8_buf[32];
  1102. wchar_t utf8_out[32];
  1103. #ifdef LTC_TEST_READDIR
  1104. ltc_asn1_list *list;
  1105. #endif
  1106. if (ltc_mp.name == NULL) return CRYPT_NOP;
  1107. s_der_recursion_limit();
  1108. der_Xcode_test();
  1109. #ifdef LTC_TEST_READDIR
  1110. DO(test_process_dir("tests/asn1", &list, s_der_decode_sequence_flexi, NULL, NULL, "DER ASN.1 special cases"));
  1111. #endif
  1112. der_custom_test();
  1113. s_der_regression_test();
  1114. der_toolong_test();
  1115. der_cacert_test();
  1116. s_der_oid_test();
  1117. y = 0xffffff00;
  1118. #if ULONG_MAX == ULLONG_MAX
  1119. y <<= 32;
  1120. #endif
  1121. while (y != 0) {
  1122. /* we have to modify x to be larger than the encoded
  1123. * length as der_decode_asn1_length() checks also if
  1124. * the encoded length is reasonable in regards to the
  1125. * available buffer size.
  1126. */
  1127. x = sizeof(buf[0]);
  1128. DO(der_encode_asn1_length(y, buf[0], &x));
  1129. x = y + x;
  1130. DO(der_decode_asn1_length(buf[0], &x, &z));
  1131. if (y != z) {
  1132. fprintf(stderr, "Failed to en- or decode length correctly! %lu != %lu\n", y, z);
  1133. return 1;
  1134. }
  1135. y >>= 3;
  1136. }
  1137. DO(ltc_mp_init_multi(&a, &b, &c, &d, &e, &f, &g, LTC_NULL));
  1138. for (zz = 0; zz < 16; zz++) {
  1139. #ifdef USE_TFM
  1140. for (z = 0; z < 256; z++) {
  1141. #else
  1142. for (z = 0; z < 1024; z++) {
  1143. #endif
  1144. ENSURE(yarrow_read(buf[0], z, &yarrow_prng) == z);
  1145. DO(ltc_mp_read_unsigned_bin(a, buf[0], z));
  1146. /* if (ltc_mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
  1147. x = sizeof(buf[0]);
  1148. DO(der_encode_integer(a, buf[0], &x));
  1149. DO(der_length_integer(a, &y));
  1150. if (y != x) { fprintf(stderr, "DER INTEGER size mismatch %lu != %lu\n", y, x); return 1; }
  1151. ltc_mp_set_int(b, 0);
  1152. DO(der_decode_integer(buf[0], y, b));
  1153. if (y != x || ltc_mp_cmp(a, b) != LTC_MP_EQ) {
  1154. fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
  1155. ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL);
  1156. return 1;
  1157. }
  1158. }
  1159. }
  1160. /* test short integer */
  1161. for (zz = 0; zz < 256; zz++) {
  1162. for (z = 1; z < 4; z++) {
  1163. ENSURE(yarrow_read(buf[2], z, &yarrow_prng) == z);
  1164. /* encode with normal */
  1165. DO(ltc_mp_read_unsigned_bin(a, buf[2], z));
  1166. x = sizeof(buf[0]);
  1167. DO(der_encode_integer(a, buf[0], &x));
  1168. /* encode with short */
  1169. y = sizeof(buf[1]);
  1170. DO(der_encode_short_integer(ltc_mp_get_int(a), buf[1], &y));
  1171. if (x != y || memcmp(buf[0], buf[1], x)) {
  1172. fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu, 0x%lX\n", x, y, ltc_mp_get_int(a));
  1173. for (zz = 0; zz < z; zz++) fprintf(stderr, "%02x ", buf[2][zz]);
  1174. fprintf(stderr, "\n");
  1175. for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]);
  1176. fprintf(stderr, "\n");
  1177. for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]);
  1178. fprintf(stderr, "\n");
  1179. ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL);
  1180. return 1;
  1181. }
  1182. /* decode it */
  1183. x = 0;
  1184. DO(der_decode_short_integer(buf[1], y, &x));
  1185. if (x != ltc_mp_get_int(a)) {
  1186. fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, ltc_mp_get_int(a));
  1187. ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL);
  1188. return 1;
  1189. }
  1190. }
  1191. }
  1192. ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL);
  1193. /* Test bit string */
  1194. for (zz = 1; zz < 1536; zz++) {
  1195. ENSURE(yarrow_read(buf[0], zz, &yarrow_prng) == zz);
  1196. for (z = 0; z < zz; z++) {
  1197. buf[0][z] &= 0x01;
  1198. }
  1199. x = sizeof(buf[1]);
  1200. DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
  1201. DO(der_length_bit_string(zz, &y));
  1202. if (y != x) {
  1203. fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  1204. return 1;
  1205. }
  1206. y = sizeof(buf[2]);
  1207. DO(der_decode_bit_string(buf[1], x, buf[2], &y));
  1208. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  1209. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  1210. return 1;
  1211. }
  1212. }
  1213. /* Test octet string */
  1214. for (zz = 1; zz < 1536; zz++) {
  1215. ENSURE(yarrow_read(buf[0], zz, &yarrow_prng) == zz);
  1216. x = sizeof(buf[1]);
  1217. DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
  1218. DO(der_length_octet_string(zz, &y));
  1219. if (y != x) {
  1220. fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  1221. return 1;
  1222. }
  1223. y = sizeof(buf[2]);
  1224. DO(der_decode_octet_string(buf[1], x, buf[2], &y));
  1225. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  1226. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  1227. return 1;
  1228. }
  1229. }
  1230. /* test OID */
  1231. x = sizeof(buf[0]);
  1232. DO(der_encode_object_identifier((unsigned long*)rsa_oid, LTC_ARRAY_SIZE(rsa_oid), buf[0], &x));
  1233. if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
  1234. fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
  1235. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  1236. fprintf(stderr, "\n");
  1237. return 1;
  1238. }
  1239. y = LTC_ARRAY_SIZE(oid[0]);
  1240. DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
  1241. if (y != LTC_ARRAY_SIZE(rsa_oid) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
  1242. fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
  1243. for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
  1244. fprintf(stderr, "\n");
  1245. return 1;
  1246. }
  1247. /* do random strings */
  1248. for (zz = 0; zz < 5000; zz++) {
  1249. /* pick a random number of words */
  1250. ENSURE(yarrow_read(buf[0], 4, &yarrow_prng) == 4);
  1251. LOAD32L(z, buf[0]);
  1252. z = 2 + (z % (LTC_ARRAY_SIZE(oid[0]) - 2));
  1253. /* fill them in */
  1254. oid[0][0] = buf[0][0] % 3;
  1255. oid[0][1] = buf[0][1] % 40;
  1256. for (y = 2; y < z; y++) {
  1257. ENSURE(yarrow_read(buf[0], 4, &yarrow_prng) == 4);
  1258. LOAD32L(oid[0][y], buf[0]);
  1259. }
  1260. /* encode it */
  1261. x = sizeof(buf[0]);
  1262. DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
  1263. DO(der_length_object_identifier(oid[0], z, &y));
  1264. if (x != y) {
  1265. fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
  1266. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  1267. return 1;
  1268. }
  1269. /* decode it */
  1270. y = LTC_ARRAY_SIZE(oid[0]);
  1271. DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
  1272. if (y != z) {
  1273. fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
  1274. return 1;
  1275. }
  1276. if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
  1277. fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
  1278. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  1279. fprintf(stderr, "\n\n Got \n\n");
  1280. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
  1281. return 1;
  1282. }
  1283. }
  1284. /* IA5 string */
  1285. x = sizeof(buf[0]);
  1286. DO(der_encode_ia5_string(rsa_ia5, rsa_ia5_len, buf[0], &x));
  1287. if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
  1288. fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
  1289. return 1;
  1290. }
  1291. DO(der_length_ia5_string(rsa_ia5, rsa_ia5_len, &y));
  1292. if (y != x) {
  1293. fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
  1294. return 1;
  1295. }
  1296. y = sizeof(buf[1]);
  1297. DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
  1298. if (y != rsa_ia5_len || memcmp(buf[1], rsa_ia5, rsa_ia5_len)) {
  1299. fprintf(stderr, "DER IA5 failed test vector\n");
  1300. return 1;
  1301. }
  1302. /* Printable string */
  1303. x = sizeof(buf[0]);
  1304. DO(der_encode_printable_string(rsa_printable, rsa_printable_len, buf[0], &x));
  1305. if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
  1306. fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
  1307. return 1;
  1308. }
  1309. DO(der_length_printable_string(rsa_printable, rsa_printable_len, &y));
  1310. if (y != x) {
  1311. fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
  1312. return 1;
  1313. }
  1314. y = sizeof(buf[1]);
  1315. DO(der_decode_printable_string(buf[0], x, buf[1], &y));
  1316. if (y != rsa_printable_len || memcmp(buf[1], rsa_printable, rsa_printable_len)) {
  1317. fprintf(stderr, "DER printable failed test vector\n");
  1318. return 1;
  1319. }
  1320. /* Test UTC time */
  1321. x = sizeof(buf[0]);
  1322. DO(der_encode_utctime((ltc_utctime*)&rsa_time1, buf[0], &x));
  1323. if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
  1324. fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  1325. fprintf(stderr, "\n\n");
  1326. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  1327. fprintf(stderr, "\n");
  1328. return 1;
  1329. }
  1330. DO(der_length_utctime((ltc_utctime*)&rsa_time1, &y));
  1331. if (y != x) {
  1332. fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
  1333. return 1;
  1334. }
  1335. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  1336. if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
  1337. fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
  1338. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  1339. tmp_time.YY,
  1340. tmp_time.MM,
  1341. tmp_time.DD,
  1342. tmp_time.hh,
  1343. tmp_time.mm,
  1344. tmp_time.ss,
  1345. tmp_time.off_dir,
  1346. tmp_time.off_mm,
  1347. tmp_time.off_hh);
  1348. return 1;
  1349. }
  1350. x = sizeof(buf[0]);
  1351. DO(der_encode_utctime((ltc_utctime*)&rsa_time2, buf[0], &x));
  1352. if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
  1353. fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  1354. fprintf(stderr, "\n\n");
  1355. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  1356. fprintf(stderr, "\n");
  1357. return 1;
  1358. }
  1359. DO(der_length_utctime((ltc_utctime*)&rsa_time2, &y));
  1360. if (y != x) {
  1361. fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
  1362. return 1;
  1363. }
  1364. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  1365. if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
  1366. fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
  1367. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  1368. tmp_time.YY,
  1369. tmp_time.MM,
  1370. tmp_time.DD,
  1371. tmp_time.hh,
  1372. tmp_time.mm,
  1373. tmp_time.ss,
  1374. tmp_time.off_dir,
  1375. tmp_time.off_mm,
  1376. tmp_time.off_hh);
  1377. return 1;
  1378. }
  1379. /* UTF 8 */
  1380. /* encode it */
  1381. x = sizeof(utf8_buf);
  1382. DO(der_encode_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), utf8_buf, &x));
  1383. DO(der_length_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), &y));
  1384. if (x != sizeof(utf8_1_der) || memcmp(utf8_buf, utf8_1_der, x) || x != y) {
  1385. fprintf(stderr, "DER UTF8_1 encoded to %lu bytes\n", x);
  1386. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]);
  1387. fprintf(stderr, "\n");
  1388. return 1;
  1389. }
  1390. /* decode it */
  1391. y = sizeof(utf8_out) / sizeof(utf8_out[0]);
  1392. DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
  1393. if (y != (sizeof(utf8_1) / sizeof(utf8_1[0])) || memcmp(utf8_1, utf8_out, y * sizeof(wchar_t))) {
  1394. fprintf(stderr, "DER UTF8_1 decoded to %lu wchar_t\n", y);
  1395. for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]);
  1396. fprintf(stderr, "\n");
  1397. return 1;
  1398. }
  1399. /* encode it */
  1400. x = sizeof(utf8_buf);
  1401. DO(der_encode_utf8_string(utf8_2, sizeof(utf8_2) / sizeof(utf8_2[0]), utf8_buf, &x));
  1402. if (x != sizeof(utf8_2_der) || memcmp(utf8_buf, utf8_2_der, x)) {
  1403. fprintf(stderr, "DER UTF8_2 encoded to %lu bytes\n", x);
  1404. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]);
  1405. fprintf(stderr, "\n");
  1406. return 1;
  1407. }
  1408. /* decode it */
  1409. y = sizeof(utf8_out) / sizeof(utf8_out[0]);
  1410. DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
  1411. if (y != (sizeof(utf8_2) / sizeof(utf8_2[0])) || memcmp(utf8_2, utf8_out, y * sizeof(wchar_t))) {
  1412. fprintf(stderr, "DER UTF8_2 decoded to %lu wchar_t\n", y);
  1413. for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]);
  1414. fprintf(stderr, "\n");
  1415. return 1;
  1416. }
  1417. /* encode it */
  1418. x = sizeof(utf8_buf);
  1419. DO(der_encode_utf8_string(utf8_3, sizeof(utf8_3) / sizeof(utf8_3[0]), utf8_buf, &x));
  1420. if (x != sizeof(utf8_3_der) || memcmp(utf8_buf, utf8_3_der, x)) {
  1421. fprintf(stderr, "DER UTF8_3 encoded to %lu bytes\n", x);
  1422. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]);
  1423. fprintf(stderr, "\n");
  1424. return 1;
  1425. }
  1426. /* decode it */
  1427. y = sizeof(utf8_out) / sizeof(utf8_out[0]);
  1428. DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
  1429. if (y != (sizeof(utf8_3) / sizeof(utf8_3[0])) || memcmp(utf8_3, utf8_out, y * sizeof(wchar_t))) {
  1430. fprintf(stderr, "DER UTF8_3 decoded to %lu wchar_t\n", y);
  1431. for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]);
  1432. fprintf(stderr, "\n");
  1433. return 1;
  1434. }
  1435. der_set_test();
  1436. der_flexi_test();
  1437. return der_choice_n_custom_test();
  1438. }
  1439. #endif