der_test.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. *******************************************************************************
  3. \file der_test.c
  4. \brief Tests for DER encoding rules
  5. \project bee2/test
  6. \created 2021.04.12
  7. \version 2022.11.01
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/der.h>
  13. #include <bee2/core/hex.h>
  14. #include <bee2/core/mem.h>
  15. #include <bee2/core/str.h>
  16. #include <bee2/core/util.h>
  17. /*
  18. *******************************************************************************
  19. Тестирование
  20. - used [Kaliski B. A Layman's Guide to a Subset of ASN.1, BER, and DER]
  21. http://luca.ntop.org/Teaching/Appunti/asn1.html
  22. *******************************************************************************
  23. */
  24. #define derStep(step, count)\
  25. {\
  26. size_t t = (step);\
  27. if (t == SIZE_MAX)\
  28. return FALSE;\
  29. (count) += t;\
  30. }\
  31. #define derStep2(step, ptr, count)\
  32. {\
  33. size_t t = (step);\
  34. if (t == SIZE_MAX)\
  35. return FALSE;\
  36. (ptr) += t, (count) -= t;\
  37. }\
  38. bool_t derTest()
  39. {
  40. octet buf[1024];
  41. size_t count, len;
  42. u32 tag;
  43. union {
  44. size_t size;
  45. octet oct[128];
  46. octet* ptr;
  47. char oid[16];
  48. char str[16];
  49. } val;
  50. // TL
  51. if ((count = derTLEnc(0, 0x7F21, 1000000)) != 6 ||
  52. count > sizeof(buf) ||
  53. derTLEnc(buf, 0x7F21, 1000000) != count ||
  54. derTLDec(&tag, &len, buf, count) != 6 ||
  55. tag != 0x7F21 || len != 1000000)
  56. return FALSE;
  57. // SIZE(0)
  58. if ((count = derSIZEEnc(0, 0)) != 3 ||
  59. count > sizeof(buf) ||
  60. derSIZEEnc(buf, 0) != 3 ||
  61. !hexEq(buf, "020100") ||
  62. derTLDec(&tag, &len, buf, 1024) != 2 ||
  63. tag != 2 || len != 1 ||
  64. derSIZEDec(&val.size, buf, sizeof(buf)) != 3 ||
  65. val.size != 0)
  66. return FALSE;
  67. // SIZE[APPLICATION 41](0)
  68. if ((count = derTSIZEEnc(0, tag = 0x5F29, 0)) != 4 ||
  69. count > sizeof(buf) ||
  70. derTSIZEEnc(buf, tag, 0) != 4 ||
  71. !hexEq(buf, "5F290100") ||
  72. derTSIZEDec(&val.size, buf, sizeof(buf), tag) != 4 ||
  73. val.size != 0 ||
  74. derTSIZEDec(&val.size, buf, sizeof(buf), tag + 1) != SIZE_MAX)
  75. return FALSE;
  76. // SIZE(127)
  77. if ((count = derSIZEEnc(0, 127)) != 3 ||
  78. count > sizeof(buf) ||
  79. derSIZEEnc(buf, 127) != 3 ||
  80. !hexEq(buf, "02017F") ||
  81. derSIZEDec(&val.size, buf, sizeof(buf)) != 3 ||
  82. val.size != 127)
  83. return FALSE;
  84. // SIZE(128)
  85. if ((count = derSIZEEnc(0, 128)) != 4 ||
  86. count > sizeof(buf) ||
  87. derSIZEEnc(buf, 128) != 4 ||
  88. !hexEq(buf, "02020080") ||
  89. derSIZEDec(&val.size, buf, sizeof(buf)) != 4 ||
  90. val.size != 128)
  91. return FALSE;
  92. // SIZE(256)
  93. if ((count = derSIZEEnc(0, 256)) != 4 ||
  94. count > sizeof(buf) ||
  95. derSIZEEnc(buf, 256) != 4 ||
  96. !hexEq(buf, "02020100") ||
  97. derSIZEDec(&val.size, buf, sizeof(buf)) != 4 ||
  98. val.size != 256)
  99. return FALSE;
  100. // NULL
  101. if ((count = derNULLEnc(0)) != 2 ||
  102. count > sizeof(buf) ||
  103. derNULLEnc(buf) != 2 ||
  104. !hexEq(buf, "0500") ||
  105. derNULLDec(buf, sizeof(buf)) != 2)
  106. return FALSE;
  107. // BIT
  108. hexTo(val.oct, "0123456789ABCDEF");
  109. if ((count = derBITEnc(0, val.oct, 61)) != 11 ||
  110. count > sizeof(buf) ||
  111. derBITEnc(buf, val.oct, 61) != 11 ||
  112. !hexEq(buf, "0309030123456789ABCDE8") ||
  113. derBITDec2(val.oct, buf, sizeof(buf), 61) != 11 ||
  114. !hexEq(val.oct, "0123456789ABCDE8") ||
  115. derBITDec2(val.oct, buf, sizeof(buf), 62) != SIZE_MAX ||
  116. derBITDec2(val.oct, buf, 6, 61) != SIZE_MAX ||
  117. derBITDec(val.oct, &len, buf, sizeof(buf)) != 11 || len != 61 ||
  118. !hexEq(val.oct, "0123456789ABCDE8") ||
  119. derBITEnc(buf, val.oct, 64) != 11 ||
  120. !hexEq(buf, "0309000123456789ABCDE8") ||
  121. derBITDec2(val.oct, buf, sizeof(buf), 64) != 11 ||
  122. !hexEq(val.oct, "0123456789ABCDE8") ||
  123. derBITDec2(val.oct, buf, sizeof(buf), 63) != SIZE_MAX ||
  124. derBITDec2(val.oct, buf, 6, 64) != SIZE_MAX ||
  125. derBITDec(val.oct, &len, buf, sizeof(buf)) != 11 || len != 64)
  126. return FALSE;
  127. // OCT
  128. hexTo(val.oct, "0123456789ABCDEF");
  129. if ((count = derOCTEnc(0, val.oct, 8)) != 10 ||
  130. count > sizeof(buf) ||
  131. derOCTEnc(buf, val.oct, 8) != 10 ||
  132. !hexEq(buf, "04080123456789ABCDEF") ||
  133. derOCTDec3(buf, sizeof(buf), val.oct, 8) != 10 ||
  134. derOCTDec2(val.oct, buf, sizeof(buf), 8) != 10 ||
  135. !hexEq(val.oct, "0123456789ABCDEF") ||
  136. derOCTDec2(0, buf, sizeof(buf), 8) != 10 ||
  137. derOCTDec(val.oct, &len, buf, sizeof(buf)) != 10 || len != 8 ||
  138. !hexEq(val.oct, "0123456789ABCDEF") ||
  139. derOCTDec(0, &len, buf, sizeof(buf)) != 10 || len != 8 ||
  140. derOCTDec(val.oct, 0, buf, sizeof(buf)) != 10 ||
  141. !hexEq(val.oct, "0123456789ABCDEF") ||
  142. derOCTDec(0, 0, buf, sizeof(buf)) != 10)
  143. return FALSE;
  144. // OID
  145. if ((count = derOIDEnc(0, "1.2.840.113549")) != 8 ||
  146. count > sizeof(buf) ||
  147. derOIDEnc(buf, "1.2.840.113549") != 8 ||
  148. !hexEq(buf, "06062A864886F70D") ||
  149. derOIDDec(0, 0, buf, sizeof(buf)) != 8 ||
  150. derOIDDec(val.oid, 0, buf, sizeof(buf)) != 8 ||
  151. !strEq(val.oid, "1.2.840.113549") ||
  152. derOIDDec(val.oid, &len, buf, sizeof(buf)) != 8 || len != 14 ||
  153. !strEq(val.oid, "1.2.840.113549") ||
  154. derOIDDec(0, &len, buf, sizeof(buf)) != 8 || len != 14 ||
  155. !strEq(val.oid, "1.2.840.113549") ||
  156. derOIDDec2(buf, sizeof(buf), val.oid) != 8)
  157. return FALSE;
  158. // PSTR
  159. if ((count = derTPSTREnc(0, 0x42, "BYCA0000")) != 10 ||
  160. count > sizeof(buf) ||
  161. derTPSTREnc(buf, 0x42, "BYCA0000") != 10 ||
  162. !hexEq(buf, "42084259434130303030") ||
  163. derTPSTRDec(0, 0, buf, sizeof(buf), 0x42) != 10 ||
  164. derPSTRDec(0, 0, buf, sizeof(buf)) != SIZE_MAX ||
  165. derTPSTRDec(val.str, 0, buf, sizeof(buf), 0x42) != 10 ||
  166. !strEq(val.str, "BYCA0000") ||
  167. derTPSTRDec(val.str, &len, buf, sizeof(buf), 0x42) != 10 ||
  168. !strEq(val.oid, "BYCA0000") || len != 8 ||
  169. derTPSTRDec(0, &len, buf, sizeof(buf), 0x42) != 10 || len != 8)
  170. return FALSE;
  171. // Seq1 ::= SEQUENCE { nothing NULL }
  172. {
  173. der_anchor_t Seq1[1];
  174. octet* ptr;
  175. // определить длину кода
  176. count = 0;
  177. derStep(derSEQEncStart(Seq1, 0, count), count);
  178. derStep(derNULLEnc(0), count);
  179. derStep(derSEQEncStop(0, count, Seq1), count);
  180. if (count != 4 || count > sizeof(buf))
  181. return FALSE;
  182. // кодировать
  183. count = 0;
  184. derStep(derSEQEncStart(Seq1, buf, count), count);
  185. derStep(derNULLEnc(buf + count), count);
  186. derStep(derSEQEncStop(buf + count, count, Seq1), count);
  187. if (count != 4 || !hexEq(buf, "30020500"))
  188. return FALSE;
  189. // проверить
  190. if (!derIsValid(buf, count) || !derIsValid2(buf, count, 0x30))
  191. return FALSE;
  192. // декодировать
  193. derStep2(derSEQDecStart(Seq1, ptr = buf, count), ptr, count);
  194. derStep2(derNULLDec(ptr, count), ptr, count);
  195. derStep2(derSEQDecStop(ptr, Seq1), ptr, count);
  196. if (count != 0)
  197. return FALSE;
  198. }
  199. // Seq2 ::= SEQUENCE { octet OCTET STRING(130) }
  200. {
  201. der_anchor_t Seq2[1];
  202. octet* ptr;
  203. // подготовить строку октетов
  204. memSetZero(val.oct, 127);
  205. // кодировать
  206. count = 0;
  207. derStep(derSEQEncStart(Seq2, buf, count), count);
  208. derStep(derOCTEnc(buf + count, val.oct, 127), count);
  209. derStep(derSEQEncStop(buf + count, count, Seq2), count);
  210. if (count != 132 || !hexEq(buf, "308181047F0000"))
  211. return FALSE;
  212. // декодировать
  213. derStep2(derSEQDecStart(Seq2, ptr = buf, count), ptr, count);
  214. derStep2(derOCTDec3(ptr, count, val.oct, 127), ptr, count);
  215. derStep2(derSEQDecStop(ptr, Seq2), ptr, count);
  216. if (count != 0)
  217. return FALSE;
  218. }
  219. // все нормально
  220. return TRUE;
  221. }