der_test.c 52 KB

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