der_tests.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. #include <tomcrypt_test.h>
  2. #if defined(GMP_LTC_DESC) || defined(USE_GMP)
  3. #include <gmp.h>
  4. #endif
  5. #ifndef LTC_DER
  6. int der_tests(void)
  7. {
  8. return CRYPT_NOP;
  9. }
  10. #else
  11. static const unsigned char _der_tests_stinky_root_cert[] =
  12. "MIIFETCCA/mgAwIBAgIQbv53JNmv518t5lkCHE272jANBgkqhkiG9w0BAQUFADCB"
  13. "lTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAlVUMRcwFQYDVQQHEw5TYWx0IExha2Ug"
  14. "Q2l0eTEeMBwGA1UEChMVVGhlIFVTRVJUUlVTVCBOZXR3b3JrMSEwHwYDVQQLExho"
  15. "dHRwOi8vd3d3LnVzZXJ0cnVzdC5jb20xHTAbBgNVBAMTFFVUTi1VU0VSRmlyc3Qt"
  16. "T2JqZWN0MB4XDTA4MDQyOTAwMDAwMFoXDTEwMDQyOTIzNTk1OVowgbUxCzAJBgNV"
  17. "BAYTAlVTMQ4wDAYDVQQRDAU0NDE0MzELMAkGA1UECAwCT0gxGTAXBgNVBAcMEE1h"
  18. "eWZpZWxkIFZpbGxhZ2UxEDAOBgNVBAkMB1N1aXRlIEExFDASBgNVBAkMCzc2NyBC"
  19. "ZXRhIERyMSIwIAYDVQQKDBlQcmVlbXB0aXZlIFNvbHV0aW9ucywgTExDMSIwIAYD"
  20. "VQQDDBlQcmVlbXB0aXZlIFNvbHV0aW9ucywgTExDMIIBIjANBgkqhkiG9w0BAQEF"
  21. "AAOCAQ8AMIIBCgKCAQEAzH7ZBkMcBuHx8d2f10RGTHAf7gzzVteGbOihJGH2BwlS"
  22. "ZvNp6WEE4DfL+s1vp0wzk1XeLN5tRjg2qum9YqyCk7okh7pXGy46f5mWbLQiefGA"
  23. "j5UXRcr6WJ3xeACdbXxKrYMV0REia+4Jb2UbFA8S81PjhRon6vcRz76ziUWwt8NC"
  24. "igX+4ZC0skhhKzKszel6KGL7bJCtLG7ukw9DZCrvPCRcKFeM/GwQ6ACMgP88CSCL"
  25. "t1fbIXDH1vd/x2XM3QlaSDN6hYDbef8m1T+9TCkXVKeqG1GYjSUrHzYnCZUmTRrR"
  26. "38jgC3qXxiIpDKW105uM0nlXe2XF9c+ot2MdWvV4TwIDAQABo4IBOTCCATUwHwYD"
  27. "VR0jBBgwFoAU2u1kdBScFDyr3ZmpvVsoTYs8ydgwHQYDVR0OBBYEFK+1HzZE4i28"
  28. "oLIzuqlFR9SspiCIMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMBMGA1Ud"
  29. "JQQMMAoGCCsGAQUFBwMDMBEGCWCGSAGG+EIBAQQEAwIEEDBGBgNVHSAEPzA9MDsG"
  30. "DCsGAQQBsjEBAgEDAjArMCkGCCsGAQUFBwIBFh1odHRwczovL3NlY3VyZS5jb21v"
  31. "ZG8ubmV0L0NQUzBCBgNVHR8EOzA5MDegNaAzhjFodHRwOi8vY3JsLnVzZXJ0cnVz"
  32. "dC5jb20vVVROLVVTRVJGaXJzdC1PYmplY3QuY3JsMCEGA1UdEQQaMBiBFnN1cHBv"
  33. "cnRAcHJlZW1wdGl2ZS5jb20wDQYJKoZIhvcNAQEFBQADggEBAC+JM26Dokvonudl"
  34. "JXe/Yun7IBhimkagZUjbk9l/GQWN6i+v1o95UJ1wGJtBdm2+MxbSaPoNTDZR4B+2"
  35. "lYL9MW57UVmePrnfUPXQKZZG+8gTRDz8+7ol/CEAKmS3MLKCRcH5oe+J5345sGxi"
  36. "FC/KWNKedTNraW95xlg8NTlL2yRP7TMsjvBxgLmkbaFUoXzPTbQWmtovIagIT8GC"
  37. "JeXwdFaRjbamiz3Irl+u7x/mhxdza6RvgBYylXRFMudANpeGsV7gDXlnfzpFDKHQ"
  38. "niVwB7P5sbPFIlmIc+4/xRItkLIRjCVXaepgN9KYu3VOgiSDI6wXiTwP44/LUXQM"
  39. "hetwa7s=";
  40. const unsigned char _der_tests_cacert_root_cert[] =
  41. "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290"
  42. "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB"
  43. "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA"
  44. "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO"
  45. "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi"
  46. "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ"
  47. "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC"
  48. "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ"
  49. "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6"
  50. "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y"
  51. "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7"
  52. "w2t6iPGwcswlWyCR7BYCEo8y6RcYSNDHBS4CMEK4JZwFaz+qOqfrU0j36NK2B5jc"
  53. "G8Y0f3/JHIJ6BVgrCFvzOKKrF11myZjXnhCLotLddJr3cQxyYN/Nb5gznZY0dj4k"
  54. "epKwDpUeb+agRThHqtdB7Uq3EvbXG4OKDy7YCbZZ16oE/9KTfWgu3YtLq1i6L43q"
  55. "laegw1SJpfvbi1EinbLDvhG+LJGGi5Z4rSDTii8aP8bQUWWHIbEZAWV/RRyH9XzQ"
  56. "QUxPKZgh/TMfdQwEUfoZd9vUFBzugcMd9Zi3aQaRIt0AUMyBMawSB3s42mhb5ivU"
  57. "fslfrejrckzzAeVLIL+aplfKkQABi6F1ITe1Yw1nPkZPcCBnzsXWWdsC4PDSy826"
  58. "YreQQejdIOQpvGQpQsgi3Hia/0PsmBsJUUtaWsJx8cTLc6nloQsCAwEAAaOCAc4w"
  59. "ggHKMB0GA1UdDgQWBBQWtTIb1Mfz4OaO873SsDrusjkY0TCBowYDVR0jBIGbMIGY"
  60. "gBQWtTIb1Mfz4OaO873SsDrusjkY0aF9pHsweTEQMA4GA1UEChMHUm9vdCBDQTEe"
  61. "MBwGA1UECxMVaHR0cDovL3d3dy5jYWNlcnQub3JnMSIwIAYDVQQDExlDQSBDZXJ0"
  62. "IFNpZ25pbmcgQXV0aG9yaXR5MSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0QGNhY2Vy"
  63. "dC5vcmeCAQAwDwYDVR0TAQH/BAUwAwEB/zAyBgNVHR8EKzApMCegJaAjhiFodHRw"
  64. "czovL3d3dy5jYWNlcnQub3JnL3Jldm9rZS5jcmwwMAYJYIZIAYb4QgEEBCMWIWh0"
  65. "dHBzOi8vd3d3LmNhY2VydC5vcmcvcmV2b2tlLmNybDA0BglghkgBhvhCAQgEJxYl"
  66. "aHR0cDovL3d3dy5jYWNlcnQub3JnL2luZGV4LnBocD9pZD0xMDBWBglghkgBhvhC"
  67. "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg"
  68. "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB"
  69. "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc"
  70. "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg"
  71. "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c"
  72. "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl"
  73. "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY"
  74. "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T"
  75. "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF"
  76. "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum"
  77. "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk"
  78. "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW"
  79. "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD";
  80. const unsigned long _der_tests_cacert_root_cert_size = sizeof(_der_tests_cacert_root_cert);
  81. /*
  82. SEQUENCE(3 elem)
  83. SEQUENCE(8 elem)
  84. [0](1)
  85. INTEGER 2
  86. INTEGER 0
  87. SEQUENCE(2 elem)
  88. OBJECT IDENTIFIER 1.2.840.113549.1.1.4
  89. NULL
  90. SEQUENCE(4 elem)
  91. SET(1 elem)
  92. SEQUENCE(2 elem)
  93. OBJECT IDENTIFIER 2.5.4.10
  94. PrintableString Root CA
  95. SET(1 elem)
  96. SEQUENCE(2 elem)
  97. OBJECT IDENTIFIER 2.5.4.11
  98. PrintableString http://www.cacert.org
  99. SET(1 elem)
  100. SEQUENCE(2 elem)
  101. OBJECT IDENTIFIER 2.5.4.3
  102. PrintableString CA Cert Signing Authority
  103. SET(1 elem)
  104. SEQUENCE(2 elem)
  105. OBJECT IDENTIFIER 1.2.840.113549.1.9.1
  106. IA5String [email protected]
  107. SEQUENCE(2 elem)
  108. UTCTime03-03-30 12:29:49 UTC
  109. UTCTime33-03-29 12:29:49 UTC
  110. SEQUENCE(4 elem)
  111. SET(1 elem)
  112. SEQUENCE(2 elem)
  113. OBJECT IDENTIFIER 2.5.4.10
  114. PrintableString Root CA
  115. SET(1 elem)
  116. SEQUENCE(2 elem)
  117. OBJECT IDENTIFIER 2.5.4.11
  118. PrintableString http://www.cacert.org
  119. SET(1 elem)
  120. SEQUENCE(2 elem)
  121. OBJECT IDENTIFIER 2.5.4.3
  122. PrintableString CA Cert Signing Authority
  123. SET(1 elem)
  124. SEQUENCE(2 elem)
  125. OBJECT IDENTIFIER 1.2.840.113549.1.9.1
  126. IA5String [email protected]
  127. SEQUENCE(2 elem)
  128. SEQUENCE(2 elem)
  129. OBJECT IDENTIFIER 1.2.840.113549.1.1.1
  130. NULL
  131. BIT STRING(1 elem)
  132. SEQUENCE(2 elem)
  133. INTEGER (4096 bit)
  134. INTEGER 65537
  135. [3](1)
  136. SEQUENCE(7 elem)
  137. SEQUENCE(2 elem)
  138. OBJECT IDENTIFIER 2.5.29.14
  139. OCTET STRING(1 elem)
  140. OCTET STRING(20 byte) 16B5321BD4C7F3E0E68EF3BDD2B03AEEB23918D1
  141. SEQUENCE(2 elem)
  142. OBJECT IDENTIFIER 2.5.29.35
  143. OCTET STRING(1 elem)
  144. SEQUENCE(3 elem)
  145. [0]
  146. [1](1)
  147. [4](1)
  148. SEQUENCE(4 elem)
  149. SET(1 elem)
  150. SEQUENCE(2 elem)
  151. OBJECT IDENTIFIER 2.5.4.10
  152. PrintableString Root CA
  153. SET(1 elem)
  154. SEQUENCE(2 elem)
  155. OBJECT IDENTIFIER 2.5.4.11
  156. PrintableString http://www.cacert.org
  157. SET(1 elem)
  158. SEQUENCE(2 elem)
  159. OBJECT IDENTIFIER 2.5.4.3
  160. PrintableString CA Cert Signing Authority
  161. SET(1 elem)
  162. SEQUENCE(2 elem)
  163. OBJECT IDENTIFIER 1.2.840.113549.1.9.1
  164. IA5String [email protected]
  165. [2]
  166. SEQUENCE(3 elem)
  167. OBJECT IDENTIFIER 2.5.29.19
  168. BOOLEAN true
  169. OCTET STRING(1 elem)
  170. SEQUENCE(1 elem)
  171. BOOLEAN true
  172. SEQUENCE(2 elem)
  173. OBJECT IDENTIFIER 2.5.29.31
  174. OCTET STRING(1 elem)
  175. SEQUENCE(1 elem)
  176. SEQUENCE(1 elem)
  177. [0](1)
  178. [0](1)
  179. [6]
  180. SEQUENCE(2 elem)
  181. OBJECT IDENTIFIER 2.16.840.1.113730.1.4
  182. OCTET STRING(1 elem)
  183. IA5String https://www.cacert.org/revoke.crl
  184. SEQUENCE(2 elem)
  185. OBJECT IDENTIFIER 2.16.840.1.113730.1.8
  186. OCTET STRING(1 elem)
  187. IA5String http://www.cacert.org/index.php?id=10
  188. SEQUENCE(2 elem)
  189. OBJECT IDENTIFIER 2.16.840.1.113730.1.13
  190. OCTET STRING(1 elem)
  191. IA5String To get your own certificate for FREE head over to http://www.cacert.org
  192. SEQUENCE(2 elem)
  193. OBJECT IDENTIFIER 1.2.840.113549.1.1.4
  194. NULL
  195. BIT STRING(4096 bit)
  196. */
  197. #define __ASN1_FMTSTRING_FMT "line: %d, type=%d, size=%lu, data=%p, self=%p, next=%p, prev=%p, parent=%p, child=%p"
  198. #define __ASN1_FMTSTRING_VAL(l) __LINE__, (l)->type, (l)->size, (l)->data, (l), (l)->next, (l)->prev, (l)->parent, (l)->child
  199. #define __ASN1_ERR(l) fprintf(stderr, __ASN1_FMTSTRING_FMT "\n", __ASN1_FMTSTRING_VAL(l)); \
  200. exit(EXIT_FAILURE)
  201. #define __CHECK_ASN1_HAS(l, w) do { if ((l)->w == NULL) { \
  202. __ASN1_ERR(l);\
  203. } } while(0)
  204. #define __CHECK_ASN1_HAS_NO(l, w) do { if ((l)->w != NULL) { \
  205. __ASN1_ERR(l);\
  206. } } while(0)
  207. #define CHECK_ASN1_TYPE(l, t) do { if ((l)->type != (t)) { \
  208. __ASN1_ERR(l);\
  209. } } while(0)
  210. #define CHECK_ASN1_HAS_CHILD(l) __CHECK_ASN1_HAS(l, child)
  211. #define CHECK_ASN1_HAS_NO_CHILD(l) __CHECK_ASN1_HAS_NO(l, child)
  212. #define CHECK_ASN1_HAS_NEXT(l) __CHECK_ASN1_HAS(l, next)
  213. #define CHECK_ASN1_HAS_NO_NEXT(l) __CHECK_ASN1_HAS_NO(l, next)
  214. #define CHECK_ASN1_HAS_DATA(l) __CHECK_ASN1_HAS(l, data)
  215. #define CHECK_ASN1_HAS_NO_DATA(l) __CHECK_ASN1_HAS_NO(l, data)
  216. #ifdef LTC_DER_TESTS_PRINT_FLEXI
  217. static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
  218. {
  219. char buf[1024];
  220. char* name = NULL;
  221. char* text = NULL;
  222. ltc_asn1_list* ostring = NULL;
  223. unsigned int n;
  224. switch (l->type)
  225. {
  226. case LTC_ASN1_EOL:
  227. name = "EOL";
  228. snprintf(buf, sizeof(buf),__ASN1_FMTSTRING_FMT "\n", __ASN1_FMTSTRING_VAL(l));
  229. text = buf;
  230. break;
  231. case LTC_ASN1_BOOLEAN:
  232. name = "BOOLEAN";
  233. {
  234. if (*(int*)l->data)
  235. text = "true";
  236. else
  237. text = "false";
  238. }
  239. break;
  240. case LTC_ASN1_INTEGER:
  241. name = "INTEGER";
  242. break;
  243. case LTC_ASN1_SHORT_INTEGER:
  244. name = "SHORT INTEGER";
  245. break;
  246. case LTC_ASN1_BIT_STRING:
  247. name = "BIT STRING";
  248. break;
  249. case LTC_ASN1_OCTET_STRING:
  250. name = "OCTET STRING";
  251. {
  252. unsigned long ostring_l = l->size;
  253. /* sometimes there's another sequence in an octet string...
  254. * try to decode that... if it fails print out the octet string
  255. */
  256. if (der_decode_sequence_flexi(l->data, &ostring_l, &ostring) == CRYPT_OK) {
  257. text = "";
  258. }
  259. else {
  260. int r;
  261. char* s = buf;
  262. int sz = sizeof(buf);
  263. for (n = 0; n < l->size; ++n) {
  264. r = snprintf(s, sz, "%02X", ((unsigned char*)l->data)[n]);
  265. if (r < 0 || r >= sz) {
  266. printf("Octet string boom");
  267. exit(EXIT_FAILURE);
  268. }
  269. s += r;
  270. sz -= r;
  271. }
  272. text = buf;
  273. }
  274. }
  275. break;
  276. case LTC_ASN1_NULL:
  277. name = "NULL";
  278. text = "";
  279. break;
  280. case LTC_ASN1_OBJECT_IDENTIFIER:
  281. name = "OBJECT IDENTIFIER";
  282. {
  283. unsigned long i;
  284. int r;
  285. char* s = buf;
  286. int sz = sizeof(buf);
  287. for (i = 0; i < l->size; ++i) {
  288. r = snprintf(s, sz, "%lu.", ((unsigned long*)l->data)[i]);
  289. if (r < 0 || r >= sz) {
  290. printf("OID boom");
  291. exit(EXIT_FAILURE);
  292. }
  293. s += r;
  294. sz -= r;
  295. }
  296. /* replace the last . with a \0 */
  297. *(s - 1) = '\0';
  298. text = buf;
  299. }
  300. break;
  301. case LTC_ASN1_IA5_STRING:
  302. name = "IA5 STRING";
  303. text = l->data;
  304. break;
  305. case LTC_ASN1_PRINTABLE_STRING:
  306. name = "PRINTABLE STRING";
  307. text = l->data;
  308. break;
  309. case LTC_ASN1_UTF8_STRING:
  310. name = "UTF8 STRING";
  311. break;
  312. case LTC_ASN1_UTCTIME:
  313. name = "UTCTIME";
  314. {
  315. ltc_utctime* ut = l->data;
  316. snprintf(buf, sizeof(buf), "%02d-%02d-%02d %02d:%02d:%02d %c%02d:%02d",
  317. ut->YY, ut->MM, ut->DD, ut->hh, ut->mm, ut->ss,
  318. ut->off_dir ? '-' : '+', ut->off_hh, ut->off_mm);
  319. text = buf;
  320. }
  321. break;
  322. case LTC_ASN1_GENERALIZEDTIME:
  323. name = "GENERALIZED TIME";
  324. {
  325. ltc_generalizedtime* gt = l->data;
  326. if(gt->fs)
  327. snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d.%02dZ",
  328. gt->YYYY, gt->MM, gt->DD, gt->hh, gt->mm, gt->ss, gt->fs);
  329. else
  330. snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02dZ",
  331. gt->YYYY, gt->MM, gt->DD, gt->hh, gt->mm, gt->ss);
  332. text = buf;
  333. }
  334. break;
  335. case LTC_ASN1_CHOICE:
  336. name = "CHOICE";
  337. break;
  338. case LTC_ASN1_SEQUENCE:
  339. name = "SEQUENCE";
  340. text = "";
  341. break;
  342. case LTC_ASN1_SET:
  343. name = "SET";
  344. text = "";
  345. break;
  346. case LTC_ASN1_SETOF:
  347. name = "SETOF";
  348. text = "";
  349. break;
  350. case LTC_ASN1_RAW_BIT_STRING:
  351. name = "RAW BIT STRING";
  352. break;
  353. case LTC_ASN1_TELETEX_STRING:
  354. name = "TELETEX STRING";
  355. text = l->data;
  356. break;
  357. case LTC_ASN1_CONSTRUCTED:
  358. if (l->used & 0x80)
  359. name = "CONTEXT SPECIFIC";
  360. else
  361. name = "CONSTRUCTED";
  362. snprintf(buf, sizeof(buf), "[%d]", l->used & 0x1f);
  363. text = buf;
  364. break;
  365. case LTC_ASN1_CONTEXT_SPECIFIC:
  366. name = "CONTEXT SPECIFIC";
  367. {
  368. int r;
  369. char* s = buf;
  370. int sz = sizeof(buf);
  371. r = snprintf(s, sz, "[%d] ", l->used & 0x1f);
  372. if (r < 0 || r >= sz) {
  373. printf("Context Specific boom");
  374. exit(EXIT_FAILURE);
  375. }
  376. s += r;
  377. sz -= r;
  378. for (n = 0; n < l->size; ++n) {
  379. r = snprintf(s, sz, "%02X", ((unsigned char*)l->data)[n]);
  380. if (r < 0 || r >= sz) {
  381. printf("Context Specific boom");
  382. exit(EXIT_FAILURE);
  383. }
  384. s += r;
  385. sz -= r;
  386. }
  387. text = buf;
  388. }
  389. break;
  390. }
  391. for (n = 0; n < level; ++n) {
  392. printf(" ");
  393. }
  394. if (name) {
  395. if (text)
  396. printf("%s %s\n", name, text);
  397. else
  398. printf("%s <missing decoding>\n", name);
  399. }
  400. else
  401. printf("WTF type=%i\n", l->type);
  402. if (ostring) {
  403. _der_tests_print_flexi(ostring, level + 1);
  404. der_free_sequence_flexi(ostring);
  405. }
  406. if (l->child)
  407. _der_tests_print_flexi(l->child, level + 1);
  408. if (l->next)
  409. _der_tests_print_flexi(l->next, level);
  410. }
  411. #endif
  412. static void der_cacert_test(void)
  413. {
  414. unsigned char buf[sizeof(_der_tests_cacert_root_cert)];
  415. unsigned long len1 = sizeof(buf), len2;
  416. ltc_asn1_list *decoded_list, *l, *l1, *l2;
  417. DO(base64_decode(_der_tests_stinky_root_cert, sizeof(_der_tests_stinky_root_cert), buf, &len1));
  418. len2 = len1;
  419. DO(der_decode_sequence_flexi(buf, &len2, &decoded_list));
  420. der_free_sequence_flexi(decoded_list);
  421. len1 = sizeof(buf);
  422. DO(base64_decode(_der_tests_cacert_root_cert, sizeof(_der_tests_cacert_root_cert), buf, &len1));
  423. len2 = len1;
  424. DO(der_decode_sequence_flexi(buf, &len2, &decoded_list));
  425. CHECK_ASN1_TYPE(decoded_list, LTC_ASN1_SEQUENCE);
  426. CHECK_ASN1_HAS_DATA(decoded_list);
  427. der_sequence_shrink(decoded_list);
  428. CHECK_ASN1_TYPE(decoded_list, LTC_ASN1_SEQUENCE);
  429. CHECK_ASN1_HAS_NO_DATA(decoded_list);
  430. #ifdef LTC_DER_TESTS_PRINT_FLEXI
  431. printf("\n\n--- test print start ---\n\n");
  432. _der_tests_print_flexi(decoded_list, 0);
  433. printf("\n\n--- test print end ---\n\n");
  434. #endif
  435. l = decoded_list;
  436. /*
  437. SEQUENCE(3 elem)
  438. SEQUENCE(8 elem)
  439. */
  440. CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
  441. CHECK_ASN1_HAS_CHILD(l);
  442. CHECK_ASN1_HAS_NO_NEXT(l);
  443. l = l->child;
  444. CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
  445. CHECK_ASN1_HAS_CHILD(l);
  446. CHECK_ASN1_HAS_NEXT(l);
  447. l1 = l->child;
  448. /*
  449. [0](1)
  450. INTEGER 2
  451. */
  452. CHECK_ASN1_TYPE(l1, LTC_ASN1_CONSTRUCTED);
  453. CHECK_ASN1_HAS_CHILD(l1);
  454. CHECK_ASN1_HAS_NEXT(l1);
  455. l2 = l1->child;
  456. CHECK_ASN1_TYPE(l2, LTC_ASN1_INTEGER);
  457. CHECK_ASN1_HAS_NO_CHILD(l2);
  458. CHECK_ASN1_HAS_NO_NEXT(l2);
  459. l1 = l1->next;
  460. /*
  461. INTEGER 0
  462. */
  463. CHECK_ASN1_TYPE(l1, LTC_ASN1_INTEGER);
  464. CHECK_ASN1_HAS_NO_CHILD(l1);
  465. CHECK_ASN1_HAS_NEXT(l1);
  466. l1 = l1->next;
  467. /*
  468. SEQUENCE(2 elem)
  469. OBJECT IDENTIFIER 1.2.840.113549.1.1.4
  470. NULL
  471. */
  472. CHECK_ASN1_TYPE(l1, LTC_ASN1_SEQUENCE);
  473. CHECK_ASN1_HAS_CHILD(l1);
  474. CHECK_ASN1_HAS_NEXT(l1);
  475. l2 = l1->child;
  476. CHECK_ASN1_TYPE(l2, LTC_ASN1_OBJECT_IDENTIFIER);
  477. CHECK_ASN1_HAS_NO_CHILD(l2);
  478. CHECK_ASN1_HAS_NEXT(l2);
  479. l2 = l2->next;
  480. CHECK_ASN1_TYPE(l2, LTC_ASN1_NULL);
  481. CHECK_ASN1_HAS_NO_CHILD(l2);
  482. CHECK_ASN1_HAS_NO_NEXT(l2);
  483. /*
  484. SEQUENCE(4 elem)
  485. SET(1 elem)
  486. SEQUENCE(2 elem)
  487. OBJECT IDENTIFIER 2.5.4.10
  488. PrintableString Root CA
  489. SET(1 elem)
  490. SEQUENCE(2 elem)
  491. OBJECT IDENTIFIER 2.5.4.11
  492. PrintableString http://www.cacert.org
  493. SET(1 elem)
  494. SEQUENCE(2 elem)
  495. OBJECT IDENTIFIER 2.5.4.3
  496. PrintableString CA Cert Signing Authority
  497. SET(1 elem)
  498. SEQUENCE(2 elem)
  499. OBJECT IDENTIFIER 1.2.840.113549.1.9.1
  500. IA5String [email protected]
  501. */
  502. l = l->next;
  503. /*
  504. SEQUENCE(2 elem)
  505. OBJECT IDENTIFIER 1.2.840.113549.1.1.4
  506. NULL
  507. */
  508. CHECK_ASN1_TYPE(l, LTC_ASN1_SEQUENCE);
  509. CHECK_ASN1_HAS_CHILD(l);
  510. CHECK_ASN1_HAS_NEXT(l);
  511. l1 = l->child;
  512. CHECK_ASN1_TYPE(l1, LTC_ASN1_OBJECT_IDENTIFIER);
  513. CHECK_ASN1_HAS_NO_CHILD(l1);
  514. CHECK_ASN1_HAS_NEXT(l1);
  515. l1 = l1->next;
  516. CHECK_ASN1_TYPE(l1, LTC_ASN1_NULL);
  517. CHECK_ASN1_HAS_NO_CHILD(l1);
  518. CHECK_ASN1_HAS_NO_NEXT(l1);
  519. l = l->next;
  520. /*
  521. BIT STRING(4096 bit)
  522. */
  523. CHECK_ASN1_TYPE(l, LTC_ASN1_BIT_STRING);
  524. CHECK_ASN1_HAS_NO_CHILD(l);
  525. CHECK_ASN1_HAS_NO_NEXT(l);
  526. der_free_sequence_flexi(decoded_list);
  527. }
  528. static void der_set_test(void)
  529. {
  530. ltc_asn1_list list[10];
  531. static const unsigned char oct_str[] = { 1, 2, 3, 4 };
  532. static const unsigned char bin_str[] = { 1, 0, 0, 1 };
  533. static const unsigned long int_val = 12345678UL;
  534. unsigned char strs[10][10], outbuf[128];
  535. unsigned long x, val, outlen;
  536. /* make structure and encode it */
  537. LTC_SET_ASN1(list, 0, LTC_ASN1_OCTET_STRING, oct_str, sizeof(oct_str));
  538. LTC_SET_ASN1(list, 1, LTC_ASN1_BIT_STRING, bin_str, sizeof(bin_str));
  539. LTC_SET_ASN1(list, 2, LTC_ASN1_SHORT_INTEGER, &int_val, 1);
  540. /* encode it */
  541. outlen = sizeof(outbuf);
  542. DO(der_encode_set(list, 3, outbuf, &outlen));
  543. /* 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 */
  544. LTC_SET_ASN1(list, 0, LTC_ASN1_BIT_STRING, strs[1], sizeof(strs[1]));
  545. LTC_SET_ASN1(list, 1, LTC_ASN1_SHORT_INTEGER, &val, 1);
  546. LTC_SET_ASN1(list, 2, LTC_ASN1_OCTET_STRING, strs[0], sizeof(strs[0]));
  547. DO(der_decode_set(outbuf, outlen, list, 3));
  548. /* now compare the items */
  549. if (memcmp(strs[0], oct_str, sizeof(oct_str))) {
  550. fprintf(stderr, "error decoding set using der_decode_set (oct_str is wrong):\n");
  551. exit(EXIT_FAILURE);
  552. }
  553. if (memcmp(strs[1], bin_str, sizeof(bin_str))) {
  554. fprintf(stderr, "error decoding set using der_decode_set (bin_str is wrong):\n");
  555. exit(EXIT_FAILURE);
  556. }
  557. if (val != int_val) {
  558. fprintf(stderr, "error decoding set using der_decode_set (int_val is wrong):\n");
  559. exit(EXIT_FAILURE);
  560. }
  561. strcpy((char*)strs[0], "one");
  562. strcpy((char*)strs[1], "one2");
  563. strcpy((char*)strs[2], "two");
  564. strcpy((char*)strs[3], "aaa");
  565. strcpy((char*)strs[4], "aaaa");
  566. strcpy((char*)strs[5], "aab");
  567. strcpy((char*)strs[6], "aaab");
  568. strcpy((char*)strs[7], "bbb");
  569. strcpy((char*)strs[8], "bbba");
  570. strcpy((char*)strs[9], "bbbb");
  571. for (x = 0; x < 10; x++) {
  572. LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], strlen((char*)strs[x]));
  573. }
  574. outlen = sizeof(outbuf);
  575. DO(der_encode_setof(list, 10, outbuf, &outlen));
  576. for (x = 0; x < 10; x++) {
  577. LTC_SET_ASN1(list, x, LTC_ASN1_PRINTABLE_STRING, strs[x], sizeof(strs[x]) - 1);
  578. }
  579. XMEMSET(strs, 0, sizeof(strs));
  580. DO(der_decode_set(outbuf, outlen, list, 10));
  581. /* now compare */
  582. for (x = 1; x < 10; x++) {
  583. if (!(strlen((char*)strs[x-1]) <= strlen((char*)strs[x])) && strcmp((char*)strs[x-1], (char*)strs[x]) >= 0) {
  584. fprintf(stderr, "error SET OF order at %lu is wrong\n", x);
  585. exit(EXIT_FAILURE);
  586. }
  587. }
  588. }
  589. /* we are encoding
  590. SEQUENCE {
  591. PRINTABLE "printable"
  592. IA5 "ia5"
  593. SEQUENCE {
  594. INTEGER 12345678
  595. UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
  596. GENERALIZEDTIME { 2017, 03, 21, 10, 21, 12, 4, 1, 2, 0 }
  597. SEQUENCE {
  598. OCTET STRING { 1, 2, 3, 4 }
  599. BIT STRING { 1, 0, 0, 1 }
  600. SEQUENCE {
  601. OID { 1, 2, 840, 113549 }
  602. NULL
  603. SET OF {
  604. PRINTABLE "333" -- WILL GET SORTED
  605. PRINTABLE "222"
  606. }
  607. }
  608. }
  609. }
  610. */
  611. static void der_flexi_test(void)
  612. {
  613. static const char printable_str[] = "printable";
  614. static const char set1_str[] = "333";
  615. static const char set2_str[] = "222";
  616. static const char ia5_str[] = "ia5";
  617. static const unsigned long int_val = 12345678UL;
  618. static const ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  619. static const ltc_generalizedtime gtime = { 2017, 03, 21, 10, 21, 12, 421, 1, 2, 0 };
  620. static const unsigned char oct_str[] = { 1, 2, 3, 4 };
  621. static const unsigned char bit_str[] = { 1, 0, 0, 1 };
  622. static const unsigned long oid_str[] = { 1, 2, 840, 113549 };
  623. unsigned char encode_buf[192];
  624. unsigned long encode_buf_len, decode_len;
  625. ltc_asn1_list static_list[5][4], *decoded_list, *l;
  626. /* build list */
  627. LTC_SET_ASN1(static_list[0], 0, LTC_ASN1_PRINTABLE_STRING, (void *)printable_str, strlen(printable_str));
  628. LTC_SET_ASN1(static_list[0], 1, LTC_ASN1_IA5_STRING, (void *)ia5_str, strlen(ia5_str));
  629. LTC_SET_ASN1(static_list[0], 2, LTC_ASN1_SEQUENCE, static_list[1], 4);
  630. LTC_SET_ASN1(static_list[1], 0, LTC_ASN1_SHORT_INTEGER, (void *)&int_val, 1);
  631. LTC_SET_ASN1(static_list[1], 1, LTC_ASN1_UTCTIME, (void *)&utctime, 1);
  632. LTC_SET_ASN1(static_list[1], 2, LTC_ASN1_GENERALIZEDTIME, (void *)&gtime, 1);
  633. LTC_SET_ASN1(static_list[1], 3, LTC_ASN1_SEQUENCE, static_list[2], 3);
  634. LTC_SET_ASN1(static_list[2], 0, LTC_ASN1_OCTET_STRING, (void *)oct_str, 4);
  635. LTC_SET_ASN1(static_list[2], 1, LTC_ASN1_BIT_STRING, (void *)bit_str, 4);
  636. LTC_SET_ASN1(static_list[2], 2, LTC_ASN1_SEQUENCE, static_list[3], 3);
  637. LTC_SET_ASN1(static_list[3], 0, LTC_ASN1_OBJECT_IDENTIFIER,(void *)oid_str, 4);
  638. LTC_SET_ASN1(static_list[3], 1, LTC_ASN1_NULL, NULL, 0);
  639. LTC_SET_ASN1(static_list[3], 2, LTC_ASN1_SETOF, static_list[4], 2);
  640. LTC_SET_ASN1(static_list[4], 0, LTC_ASN1_PRINTABLE_STRING, set1_str, strlen(set1_str));
  641. LTC_SET_ASN1(static_list[4], 1, LTC_ASN1_PRINTABLE_STRING, set2_str, strlen(set2_str));
  642. /* encode it */
  643. encode_buf_len = sizeof(encode_buf);
  644. DO(der_encode_sequence(&static_list[0][0], 3, encode_buf, &encode_buf_len));
  645. #if 0
  646. {
  647. FILE *f;
  648. f = fopen("t.bin", "wb");
  649. fwrite(encode_buf, 1, encode_buf_len, f);
  650. fclose(f);
  651. }
  652. #endif
  653. /* decode with flexi */
  654. decode_len = encode_buf_len;
  655. DO(der_decode_sequence_flexi(encode_buf, &decode_len, &decoded_list));
  656. if (decode_len != encode_buf_len) {
  657. fprintf(stderr, "Decode len of %lu does not match encode len of %lu \n", decode_len, encode_buf_len);
  658. exit(EXIT_FAILURE);
  659. }
  660. /* we expect l->next to be NULL and l->child to not be */
  661. l = decoded_list;
  662. if (l->next != NULL || l->child == NULL) {
  663. 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);
  664. exit(EXIT_FAILURE);
  665. }
  666. /* we expect a SEQUENCE */
  667. if (l->type != LTC_ASN1_SEQUENCE) {
  668. 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);
  669. exit(EXIT_FAILURE);
  670. }
  671. l = l->child;
  672. /* PRINTABLE STRING */
  673. /* we expect printable_str */
  674. if (l->next == NULL || l->child != NULL) {
  675. 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);
  676. exit(EXIT_FAILURE);
  677. }
  678. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  679. 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);
  680. exit(EXIT_FAILURE);
  681. }
  682. if (l->size != strlen(printable_str) || memcmp(printable_str, l->data, l->size)) {
  683. 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);
  684. exit(EXIT_FAILURE);
  685. }
  686. /* move to next */
  687. l = l->next;
  688. /* IA5 STRING */
  689. /* we expect ia5_str */
  690. if (l->next == NULL || l->child != NULL) {
  691. 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);
  692. exit(EXIT_FAILURE);
  693. }
  694. if (l->type != LTC_ASN1_IA5_STRING) {
  695. 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);
  696. exit(EXIT_FAILURE);
  697. }
  698. if (l->size != strlen(ia5_str) || memcmp(ia5_str, l->data, l->size)) {
  699. 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);
  700. exit(EXIT_FAILURE);
  701. }
  702. /* move to next */
  703. l = l->next;
  704. /* expect child anve move down */
  705. if (l->next != NULL || l->child == NULL) {
  706. 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);
  707. exit(EXIT_FAILURE);
  708. }
  709. if (l->type != LTC_ASN1_SEQUENCE) {
  710. 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);
  711. exit(EXIT_FAILURE);
  712. }
  713. l = l->child;
  714. /* INTEGER */
  715. if (l->next == NULL || l->child != NULL) {
  716. 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);
  717. exit(EXIT_FAILURE);
  718. }
  719. if (l->type != LTC_ASN1_INTEGER) {
  720. 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);
  721. exit(EXIT_FAILURE);
  722. }
  723. if (mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) {
  724. 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);
  725. exit(EXIT_FAILURE);
  726. }
  727. /* move to next */
  728. l = l->next;
  729. /* UTCTIME */
  730. if (l->next == NULL || l->child != NULL) {
  731. 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);
  732. exit(EXIT_FAILURE);
  733. }
  734. if (l->type != LTC_ASN1_UTCTIME) {
  735. 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);
  736. exit(EXIT_FAILURE);
  737. }
  738. if (memcmp(l->data, &utctime, sizeof(utctime))) {
  739. 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);
  740. exit(EXIT_FAILURE);
  741. }
  742. /* move to next */
  743. l = l->next;
  744. /* GeneralizedTime */
  745. if (l->next == NULL || l->child != NULL) {
  746. 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);
  747. exit(EXIT_FAILURE);
  748. }
  749. if (l->type != LTC_ASN1_GENERALIZEDTIME) {
  750. 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);
  751. exit(EXIT_FAILURE);
  752. }
  753. if (memcmp(l->data, &gtime, sizeof(gtime))) {
  754. 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);
  755. exit(EXIT_FAILURE);
  756. }
  757. /* move to next */
  758. l = l->next;
  759. /* expect child anve move down */
  760. if (l->next != NULL || l->child == NULL) {
  761. 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);
  762. exit(EXIT_FAILURE);
  763. }
  764. if (l->type != LTC_ASN1_SEQUENCE) {
  765. 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);
  766. exit(EXIT_FAILURE);
  767. }
  768. l = l->child;
  769. /* OCTET STRING */
  770. /* we expect oct_str */
  771. if (l->next == NULL || l->child != NULL) {
  772. 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);
  773. exit(EXIT_FAILURE);
  774. }
  775. if (l->type != LTC_ASN1_OCTET_STRING) {
  776. 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);
  777. exit(EXIT_FAILURE);
  778. }
  779. if (l->size != sizeof(oct_str) || memcmp(oct_str, l->data, l->size)) {
  780. 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);
  781. exit(EXIT_FAILURE);
  782. }
  783. /* move to next */
  784. l = l->next;
  785. /* BIT STRING */
  786. /* we expect oct_str */
  787. if (l->next == NULL || l->child != NULL) {
  788. 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);
  789. exit(EXIT_FAILURE);
  790. }
  791. if (l->type != LTC_ASN1_BIT_STRING) {
  792. 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);
  793. exit(EXIT_FAILURE);
  794. }
  795. if (l->size != sizeof(bit_str) || memcmp(bit_str, l->data, l->size)) {
  796. 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);
  797. exit(EXIT_FAILURE);
  798. }
  799. /* move to next */
  800. l = l->next;
  801. /* expect child anve move down */
  802. if (l->next != NULL || l->child == NULL) {
  803. 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);
  804. exit(EXIT_FAILURE);
  805. }
  806. if (l->type != LTC_ASN1_SEQUENCE) {
  807. 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);
  808. exit(EXIT_FAILURE);
  809. }
  810. l = l->child;
  811. /* OID STRING */
  812. /* we expect oid_str */
  813. if (l->next == NULL || l->child != NULL) {
  814. 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);
  815. exit(EXIT_FAILURE);
  816. }
  817. if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) {
  818. 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);
  819. exit(EXIT_FAILURE);
  820. }
  821. if (l->size != sizeof(oid_str)/sizeof(oid_str[0]) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
  822. 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);
  823. exit(EXIT_FAILURE);
  824. }
  825. /* move to next */
  826. l = l->next;
  827. /* NULL */
  828. if (l->type != LTC_ASN1_NULL) {
  829. 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);
  830. exit(EXIT_FAILURE);
  831. }
  832. /* move to next */
  833. l = l->next;
  834. /* expect child anve move down */
  835. if (l->next != NULL || l->child == NULL) {
  836. 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);
  837. exit(EXIT_FAILURE);
  838. }
  839. if (l->type != LTC_ASN1_SET) {
  840. 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);
  841. exit(EXIT_FAILURE);
  842. }
  843. l = l->child;
  844. /* PRINTABLE STRING */
  845. /* we expect printable_str */
  846. if (l->next == NULL || l->child != NULL) {
  847. 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);
  848. exit(EXIT_FAILURE);
  849. }
  850. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  851. 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);
  852. exit(EXIT_FAILURE);
  853. }
  854. /* note we compare set2_str FIRST because the SET OF is sorted and "222" comes before "333" */
  855. if (l->size != strlen(set2_str) || memcmp(set2_str, l->data, l->size)) {
  856. 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);
  857. exit(EXIT_FAILURE);
  858. }
  859. /* move to next */
  860. l = l->next;
  861. /* PRINTABLE STRING */
  862. /* we expect printable_str */
  863. if (l->type != LTC_ASN1_PRINTABLE_STRING) {
  864. 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);
  865. exit(EXIT_FAILURE);
  866. }
  867. if (l->size != strlen(set1_str) || memcmp(set1_str, l->data, l->size)) {
  868. 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);
  869. exit(EXIT_FAILURE);
  870. }
  871. der_sequence_free(l);
  872. }
  873. static int der_choice_test(void)
  874. {
  875. ltc_asn1_list types[7], host[1];
  876. unsigned char bitbuf[10], octetbuf[10], ia5buf[10], printbuf[10], outbuf[256], x, y;
  877. unsigned long integer, oidbuf[10], outlen, inlen;
  878. void *mpinteger;
  879. ltc_utctime utctime = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  880. ltc_generalizedtime gtime = { 2038, 01, 19, 3, 14, 8, 0, 0, 0, 0 };
  881. /* setup variables */
  882. for (x = 0; x < sizeof(bitbuf); x++) { bitbuf[x] = x & 1; }
  883. for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x; }
  884. for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; }
  885. for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; }
  886. integer = 1;
  887. for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; }
  888. DO(mp_init(&mpinteger));
  889. for (x = 0; x < 14; x++) {
  890. /* setup list */
  891. LTC_SET_ASN1(types, 0, LTC_ASN1_PRINTABLE_STRING, printbuf, sizeof(printbuf));
  892. LTC_SET_ASN1(types, 1, LTC_ASN1_BIT_STRING, bitbuf, sizeof(bitbuf));
  893. LTC_SET_ASN1(types, 2, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
  894. LTC_SET_ASN1(types, 3, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
  895. if (x > 7) {
  896. LTC_SET_ASN1(types, 4, LTC_ASN1_SHORT_INTEGER, &integer, 1);
  897. } else {
  898. LTC_SET_ASN1(types, 4, LTC_ASN1_INTEGER, mpinteger, 1);
  899. }
  900. LTC_SET_ASN1(types, 5, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
  901. if (x > 7) {
  902. LTC_SET_ASN1(types, 6, LTC_ASN1_UTCTIME, &utctime, 1);
  903. } else {
  904. LTC_SET_ASN1(types, 6, LTC_ASN1_GENERALIZEDTIME, &gtime, 1);
  905. }
  906. LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, 7);
  907. /* encode */
  908. outlen = sizeof(outbuf);
  909. DO(der_encode_sequence(&types[x>6?x-7:x], 1, outbuf, &outlen));
  910. /* decode it */
  911. inlen = outlen;
  912. DO(der_decode_sequence(outbuf, inlen, &host[0], 1));
  913. for (y = 0; y < 7; y++) {
  914. if (types[y].used && y != (x>6?x-7:x)) {
  915. fprintf(stderr, "CHOICE, flag %u in trial %u was incorrectly set to one\n", y, x);
  916. return 1;
  917. }
  918. if (!types[y].used && y == (x>6?x-7:x)) {
  919. fprintf(stderr, "CHOICE, flag %u in trial %u was incorrectly set to zero\n", y, x);
  920. return 1;
  921. }
  922. }
  923. }
  924. mp_clear(mpinteger);
  925. return 0;
  926. }
  927. int der_tests(void)
  928. {
  929. unsigned long x, y, z, zz, oid[2][32];
  930. unsigned char buf[3][2048];
  931. void *a, *b, *c, *d, *e, *f, *g;
  932. static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d };
  933. static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 };
  934. static const unsigned char rsa_ia5[] = "[email protected]";
  935. static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31,
  936. 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d };
  937. static const unsigned char rsa_printable[] = "Test User 1";
  938. static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55,
  939. 0x73, 0x65, 0x72, 0x20, 0x31 };
  940. static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 };
  941. static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 };
  942. ltc_utctime tmp_time;
  943. 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 };
  944. static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a };
  945. static const wchar_t utf8_1[] = { 0x0041, 0x2262, 0x0391, 0x002E };
  946. static const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E };
  947. static const wchar_t utf8_2[] = { 0xD55C, 0xAD6D, 0xC5B4 };
  948. static const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 };
  949. unsigned char utf8_buf[32];
  950. wchar_t utf8_out[32];
  951. der_cacert_test();
  952. DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
  953. for (zz = 0; zz < 16; zz++) {
  954. #ifdef USE_TFM
  955. for (z = 0; z < 256; z++) {
  956. #else
  957. for (z = 0; z < 1024; z++) {
  958. #endif
  959. if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
  960. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  961. return 1;
  962. }
  963. DO(mp_read_unsigned_bin(a, buf[0], z));
  964. /* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
  965. x = sizeof(buf[0]);
  966. DO(der_encode_integer(a, buf[0], &x));
  967. DO(der_length_integer(a, &y));
  968. if (y != x) { fprintf(stderr, "DER INTEGER size mismatch\n"); return 1; }
  969. mp_set_int(b, 0);
  970. DO(der_decode_integer(buf[0], y, b));
  971. if (y != x || mp_cmp(a, b) != LTC_MP_EQ) {
  972. fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y);
  973. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  974. return 1;
  975. }
  976. }
  977. }
  978. /* test short integer */
  979. for (zz = 0; zz < 256; zz++) {
  980. for (z = 1; z < 4; z++) {
  981. if (yarrow_read(buf[2], z, &yarrow_prng) != z) {
  982. fprintf(stderr, "Failed to read %lu bytes from yarrow\n", z);
  983. return 1;
  984. }
  985. /* encode with normal */
  986. DO(mp_read_unsigned_bin(a, buf[2], z));
  987. x = sizeof(buf[0]);
  988. DO(der_encode_integer(a, buf[0], &x));
  989. /* encode with short */
  990. y = sizeof(buf[1]);
  991. DO(der_encode_short_integer(mp_get_int(a), buf[1], &y));
  992. if (x != y || memcmp(buf[0], buf[1], x)) {
  993. fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu, 0x%lX\n", x, y, mp_get_int(a));
  994. for (zz = 0; zz < z; zz++) fprintf(stderr, "%02x ", buf[2][zz]);
  995. fprintf(stderr, "\n");
  996. for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]);
  997. fprintf(stderr, "\n");
  998. for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]);
  999. fprintf(stderr, "\n");
  1000. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  1001. return 1;
  1002. }
  1003. /* decode it */
  1004. x = 0;
  1005. DO(der_decode_short_integer(buf[1], y, &x));
  1006. if (x != mp_get_int(a)) {
  1007. fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a));
  1008. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  1009. return 1;
  1010. }
  1011. }
  1012. }
  1013. mp_clear_multi(a, b, c, d, e, f, g, NULL);
  1014. /* Test bit string */
  1015. for (zz = 1; zz < 1536; zz++) {
  1016. yarrow_read(buf[0], zz, &yarrow_prng);
  1017. for (z = 0; z < zz; z++) {
  1018. buf[0][z] &= 0x01;
  1019. }
  1020. x = sizeof(buf[1]);
  1021. DO(der_encode_bit_string(buf[0], zz, buf[1], &x));
  1022. DO(der_length_bit_string(zz, &y));
  1023. if (y != x) {
  1024. fprintf(stderr, "\nDER BIT STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  1025. return 1;
  1026. }
  1027. y = sizeof(buf[2]);
  1028. DO(der_decode_bit_string(buf[1], x, buf[2], &y));
  1029. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  1030. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  1031. return 1;
  1032. }
  1033. }
  1034. /* Test octet string */
  1035. for (zz = 1; zz < 1536; zz++) {
  1036. yarrow_read(buf[0], zz, &yarrow_prng);
  1037. x = sizeof(buf[1]);
  1038. DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
  1039. DO(der_length_octet_string(zz, &y));
  1040. if (y != x) {
  1041. fprintf(stderr, "\nDER OCTET STRING length of encoded not match expected : %lu, %lu, %lu\n", z, x, y);
  1042. return 1;
  1043. }
  1044. y = sizeof(buf[2]);
  1045. DO(der_decode_octet_string(buf[1], x, buf[2], &y));
  1046. if (y != zz || memcmp(buf[0], buf[2], zz)) {
  1047. fprintf(stderr, "%lu, %lu, %d\n", y, zz, memcmp(buf[0], buf[2], zz));
  1048. return 1;
  1049. }
  1050. }
  1051. /* test OID */
  1052. x = sizeof(buf[0]);
  1053. DO(der_encode_object_identifier((unsigned long*)rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
  1054. if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
  1055. fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
  1056. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  1057. fprintf(stderr, "\n");
  1058. return 1;
  1059. }
  1060. y = sizeof(oid[0])/sizeof(oid[0][0]);
  1061. DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
  1062. if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
  1063. fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
  1064. for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
  1065. fprintf(stderr, "\n");
  1066. return 1;
  1067. }
  1068. /* do random strings */
  1069. for (zz = 0; zz < 5000; zz++) {
  1070. /* pick a random number of words */
  1071. yarrow_read(buf[0], 4, &yarrow_prng);
  1072. LOAD32L(z, buf[0]);
  1073. z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
  1074. /* fill them in */
  1075. oid[0][0] = buf[0][0] % 3;
  1076. oid[0][1] = buf[0][1] % 40;
  1077. for (y = 2; y < z; y++) {
  1078. yarrow_read(buf[0], 4, &yarrow_prng);
  1079. LOAD32L(oid[0][y], buf[0]);
  1080. }
  1081. /* encode it */
  1082. x = sizeof(buf[0]);
  1083. DO(der_encode_object_identifier(oid[0], z, buf[0], &x));
  1084. DO(der_length_object_identifier(oid[0], z, &y));
  1085. if (x != y) {
  1086. fprintf(stderr, "Random OID %lu test failed, length mismatch: %lu, %lu\n", z, x, y);
  1087. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  1088. return 1;
  1089. }
  1090. /* decode it */
  1091. y = sizeof(oid[0])/sizeof(oid[0][0]);
  1092. DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
  1093. if (y != z) {
  1094. fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);
  1095. return 1;
  1096. }
  1097. if (memcmp(oid[0], oid[1], sizeof(oid[0][0]) * z)) {
  1098. fprintf(stderr, "Random OID %lu test failed, decoded values wrong\n", z);
  1099. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[0][x]);
  1100. fprintf(stderr, "\n\n Got \n\n");
  1101. for (x = 0; x < z; x++) fprintf(stderr, "%lu\n", oid[1][x]);
  1102. return 1;
  1103. }
  1104. }
  1105. /* IA5 string */
  1106. x = sizeof(buf[0]);
  1107. DO(der_encode_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), buf[0], &x));
  1108. if (x != sizeof(rsa_ia5_der) || memcmp(buf[0], rsa_ia5_der, x)) {
  1109. fprintf(stderr, "IA5 encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_ia5_der));
  1110. return 1;
  1111. }
  1112. DO(der_length_ia5_string(rsa_ia5, strlen((char*)rsa_ia5), &y));
  1113. if (y != x) {
  1114. fprintf(stderr, "IA5 length failed to match: %lu, %lu\n", x, y);
  1115. return 1;
  1116. }
  1117. y = sizeof(buf[1]);
  1118. DO(der_decode_ia5_string(buf[0], x, buf[1], &y));
  1119. if (y != strlen((char*)rsa_ia5) || memcmp(buf[1], rsa_ia5, strlen((char*)rsa_ia5))) {
  1120. fprintf(stderr, "DER IA5 failed test vector\n");
  1121. return 1;
  1122. }
  1123. /* Printable string */
  1124. x = sizeof(buf[0]);
  1125. DO(der_encode_printable_string(rsa_printable, strlen((char*)rsa_printable), buf[0], &x));
  1126. if (x != sizeof(rsa_printable_der) || memcmp(buf[0], rsa_printable_der, x)) {
  1127. fprintf(stderr, "PRINTABLE encode failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_printable_der));
  1128. return 1;
  1129. }
  1130. DO(der_length_printable_string(rsa_printable, strlen((char*)rsa_printable), &y));
  1131. if (y != x) {
  1132. fprintf(stderr, "printable length failed to match: %lu, %lu\n", x, y);
  1133. return 1;
  1134. }
  1135. y = sizeof(buf[1]);
  1136. DO(der_decode_printable_string(buf[0], x, buf[1], &y));
  1137. if (y != strlen((char*)rsa_printable) || memcmp(buf[1], rsa_printable, strlen((char*)rsa_printable))) {
  1138. fprintf(stderr, "DER printable failed test vector\n");
  1139. return 1;
  1140. }
  1141. /* Test UTC time */
  1142. x = sizeof(buf[0]);
  1143. DO(der_encode_utctime((ltc_utctime*)&rsa_time1, buf[0], &x));
  1144. if (x != sizeof(rsa_time1_der) || memcmp(buf[0], rsa_time1_der, x)) {
  1145. fprintf(stderr, "UTCTIME encode of rsa_time1 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  1146. fprintf(stderr, "\n\n");
  1147. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  1148. fprintf(stderr, "\n");
  1149. return 1;
  1150. }
  1151. DO(der_length_utctime((ltc_utctime*)&rsa_time1, &y));
  1152. if (y != x) {
  1153. fprintf(stderr, "UTCTIME length failed to match for rsa_time1: %lu, %lu\n", x, y);
  1154. return 1;
  1155. }
  1156. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  1157. if (y != x || memcmp(&rsa_time1, &tmp_time, sizeof(ltc_utctime))) {
  1158. fprintf(stderr, "UTCTIME decode failed for rsa_time1: %lu %lu\n", x, y);
  1159. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  1160. tmp_time.YY,
  1161. tmp_time.MM,
  1162. tmp_time.DD,
  1163. tmp_time.hh,
  1164. tmp_time.mm,
  1165. tmp_time.ss,
  1166. tmp_time.off_dir,
  1167. tmp_time.off_mm,
  1168. tmp_time.off_hh);
  1169. return 1;
  1170. }
  1171. x = sizeof(buf[0]);
  1172. DO(der_encode_utctime((ltc_utctime*)&rsa_time2, buf[0], &x));
  1173. if (x != sizeof(rsa_time2_der) || memcmp(buf[0], rsa_time2_der, x)) {
  1174. fprintf(stderr, "UTCTIME encode of rsa_time2 failed: %lu, %lu\n", x, (unsigned long)sizeof(rsa_time1_der));
  1175. fprintf(stderr, "\n\n");
  1176. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
  1177. fprintf(stderr, "\n");
  1178. return 1;
  1179. }
  1180. DO(der_length_utctime((ltc_utctime*)&rsa_time2, &y));
  1181. if (y != x) {
  1182. fprintf(stderr, "UTCTIME length failed to match for rsa_time2: %lu, %lu\n", x, y);
  1183. return 1;
  1184. }
  1185. DO(der_decode_utctime(buf[0], &y, &tmp_time));
  1186. if (y != x || memcmp(&rsa_time2, &tmp_time, sizeof(ltc_utctime))) {
  1187. fprintf(stderr, "UTCTIME decode failed for rsa_time2: %lu %lu\n", x, y);
  1188. fprintf(stderr, "\n\n%u %u %u %u %u %u %u %u %u\n\n",
  1189. tmp_time.YY,
  1190. tmp_time.MM,
  1191. tmp_time.DD,
  1192. tmp_time.hh,
  1193. tmp_time.mm,
  1194. tmp_time.ss,
  1195. tmp_time.off_dir,
  1196. tmp_time.off_mm,
  1197. tmp_time.off_hh);
  1198. return 1;
  1199. }
  1200. /* UTF 8 */
  1201. /* encode it */
  1202. x = sizeof(utf8_buf);
  1203. DO(der_encode_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), utf8_buf, &x));
  1204. DO(der_length_utf8_string(utf8_1, sizeof(utf8_1) / sizeof(utf8_1[0]), &y));
  1205. if (x != sizeof(utf8_1_der) || memcmp(utf8_buf, utf8_1_der, x) || x != y) {
  1206. fprintf(stderr, "DER UTF8_1 encoded to %lu bytes\n", x);
  1207. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]);
  1208. fprintf(stderr, "\n");
  1209. return 1;
  1210. }
  1211. /* decode it */
  1212. y = sizeof(utf8_out) / sizeof(utf8_out[0]);
  1213. DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
  1214. if (y != (sizeof(utf8_1) / sizeof(utf8_1[0])) || memcmp(utf8_1, utf8_out, y * sizeof(wchar_t))) {
  1215. fprintf(stderr, "DER UTF8_1 decoded to %lu wchar_t\n", y);
  1216. for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]);
  1217. fprintf(stderr, "\n");
  1218. return 1;
  1219. }
  1220. /* encode it */
  1221. x = sizeof(utf8_buf);
  1222. DO(der_encode_utf8_string(utf8_2, sizeof(utf8_2) / sizeof(utf8_2[0]), utf8_buf, &x));
  1223. if (x != sizeof(utf8_2_der) || memcmp(utf8_buf, utf8_2_der, x)) {
  1224. fprintf(stderr, "DER UTF8_2 encoded to %lu bytes\n", x);
  1225. for (y = 0; y < x; y++) fprintf(stderr, "%02x ", (unsigned)utf8_buf[y]);
  1226. fprintf(stderr, "\n");
  1227. return 1;
  1228. }
  1229. /* decode it */
  1230. y = sizeof(utf8_out) / sizeof(utf8_out[0]);
  1231. DO(der_decode_utf8_string(utf8_buf, x, utf8_out, &y));
  1232. if (y != (sizeof(utf8_2) / sizeof(utf8_2[0])) || memcmp(utf8_2, utf8_out, y * sizeof(wchar_t))) {
  1233. fprintf(stderr, "DER UTF8_2 decoded to %lu wchar_t\n", y);
  1234. for (x = 0; x < y; x++) fprintf(stderr, "%04lx ", (unsigned long)utf8_out[x]);
  1235. fprintf(stderr, "\n");
  1236. return 1;
  1237. }
  1238. der_set_test();
  1239. der_flexi_test();
  1240. return der_choice_test();
  1241. }
  1242. #endif
  1243. /* $Source$ */
  1244. /* $Revision$ */
  1245. /* $Date$ */