oid_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. *******************************************************************************
  3. \file oid_test.c
  4. \brief Tests for object identifiers
  5. \project bee2/test
  6. \created 2013.04.01
  7. \version 2021.07.07
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/mem.h>
  13. #include <bee2/core/hex.h>
  14. #include <bee2/core/oid.h>
  15. #include <bee2/core/str.h>
  16. /*
  17. *******************************************************************************
  18. Тестирование
  19. \used http://www.viathinksoft.de/~daniel-marschall/asn.1/oid_facts.html
  20. *******************************************************************************
  21. */
  22. bool_t oidTest()
  23. {
  24. octet buf[1024];
  25. char str[2048];
  26. char str1[2048];
  27. size_t count;
  28. // length octet 0x00
  29. hexTo(buf, "060000");
  30. if (oidFromDER(0, buf, 3) != SIZE_MAX)
  31. return FALSE;
  32. // length octet 0x80
  33. hexTo(buf, "068000");
  34. if (oidFromDER(0, buf, 3) != SIZE_MAX)
  35. return FALSE;
  36. // length octet 0xFF
  37. hexTo(buf, "06FF00");
  38. if (oidFromDER(0, buf, 3) != SIZE_MAX)
  39. return FALSE;
  40. // invalid type
  41. hexTo(buf, "080100");
  42. if (oidFromDER(0, buf, 3) != SIZE_MAX)
  43. return FALSE;
  44. // illegal padding
  45. hexTo(buf, "06070180808080807F");
  46. if (oidFromDER(0, buf, 9) != SIZE_MAX)
  47. return FALSE;
  48. hexTo(buf, "06028001");
  49. if (oidFromDER(0, buf, 4) != SIZE_MAX)
  50. return FALSE;
  51. hexTo(buf, "0602807F");
  52. if (oidFromDER(0, buf, 4) != SIZE_MAX)
  53. return FALSE;
  54. // MacOS errors
  55. hexTo(buf, "06028100");
  56. if (oidFromDER(str, buf, 4) == SIZE_MAX || !strEq(str, "2.48"))
  57. return FALSE;
  58. hexTo(buf, "06028101");
  59. if (oidFromDER(str, buf, 4) == SIZE_MAX || !strEq(str, "2.49"))
  60. return FALSE;
  61. hexTo(buf, "06028837");
  62. if (oidFromDER(str, buf, 4) == SIZE_MAX || !strEq(str, "2.999"))
  63. return FALSE;
  64. // OpenSSL errors
  65. count = oidToDER(buf, "2.65500");
  66. if (count == SIZE_MAX || oidFromDER(str, buf, count) == SIZE_MAX ||
  67. !strEq(str, "2.65500"))
  68. return FALSE;
  69. // overflow
  70. hexTo(buf, "060981B1D1AF85ECA8804F");
  71. if (oidFromDER(0, buf, 11) != SIZE_MAX)
  72. return FALSE;
  73. if (oidIsValid("2.5.4.4294967299"))
  74. return FALSE;
  75. // belt-hash
  76. count = oidToDER(buf, "1.2.112.0.2.0.34.101.31.81");
  77. if (count != 11 || !hexEq(buf, "06092A7000020022651F51"))
  78. return FALSE;
  79. if (oidFromDER(str, buf, count - 1) != SIZE_MAX)
  80. return FALSE;
  81. if (oidFromDER(0, buf, count + 1) != SIZE_MAX)
  82. return FALSE;
  83. // длинная длина
  84. strCopy(str1, "1.2.3456.78910.11121314.15161718.19202122.23242526."
  85. "27282930.31323334.35363738.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18."
  86. "19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42."
  87. "43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66."
  88. "19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42."
  89. "43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66."
  90. "19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42."
  91. "43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66");
  92. count = oidToDER(buf, str1);
  93. if (count == SIZE_MAX ||
  94. oidFromDER(str, buf, count) != strLen(str1) ||
  95. !strEq(str, str1))
  96. return FALSE;
  97. str1[strLen(str1)] = '.';
  98. strCopy(str1 + strLen(str) + 1, str);
  99. count = oidToDER(buf, str1);
  100. if (count == SIZE_MAX ||
  101. oidFromDER(str, buf, count) != strLen(str1) ||
  102. !strEq(str, str1))
  103. return FALSE;
  104. // все нормально
  105. return TRUE;
  106. }