der_tests.c 51 KB

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