btok_test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. *******************************************************************************
  3. \file btok_test.c
  4. \brief Tests for STB 34.101.79 (btok) helpers
  5. \project bee2/test
  6. \created 2022.07.07
  7. \version 2022.07.18
  8. \license This program is released under the GNU General Public License
  9. version 3. See Copyright Notices in bee2/info.h.
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/hex.h>
  13. #include <bee2/core/mem.h>
  14. #include <bee2/core/prng.h>
  15. #include <bee2/core/str.h>
  16. #include <bee2/core/util.h>
  17. #include <bee2/crypto/belt.h>
  18. #include <bee2/crypto/bign.h>
  19. #include <bee2/crypto/btok.h>
  20. #include <stdio.h>
  21. static bool_t btokCVCTest()
  22. {
  23. octet echo[256];
  24. btok_cvc_t cvc0[1];
  25. btok_cvc_t cvc1[1];
  26. btok_cvc_t cvc2[1];
  27. bign_params params[1];
  28. octet privkey0[64];
  29. octet privkey1[48];
  30. octet privkey2[32];
  31. octet cert0[400]; size_t cert0_len;
  32. octet cert1[400]; size_t cert1_len;
  33. octet cert2[400]; size_t cert2_len;
  34. // запустить ГПСЧ
  35. prngEchoStart(echo, beltH(), 256);
  36. // определить максимальную длину сертификата
  37. memSetZero(cvc0, sizeof(btok_cvc_t));
  38. strCopy(cvc0->authority, "BYCA00000000");
  39. strCopy(cvc0->holder, "BYCA00000000");
  40. hexTo(cvc0->from, "020200070007");
  41. hexTo(cvc0->until, "090900070007");
  42. memSet(cvc0->hat_eid, 0xEE, sizeof(cvc0->hat_eid));
  43. memSet(cvc0->hat_esign, 0x77, sizeof(cvc0->hat_esign));
  44. cvc0->pubkey_len = 128;
  45. if (btokCVCCheck(cvc0) == ERR_OK)
  46. return FALSE;
  47. if (bignStdParams(params, "1.2.112.0.2.0.34.101.45.3.3") != ERR_OK ||
  48. bignGenKeypair(privkey0, cvc0->pubkey, params, prngEchoStepR,
  49. echo) != ERR_OK ||
  50. btokCVCCheck(cvc0) != ERR_OK)
  51. return FALSE;
  52. if (btokCVCWrap(0, 0, cvc0, privkey0, 64) != ERR_OK)
  53. return FALSE;
  54. cvc0->pubkey_len = 0;
  55. if (btokCVCWrap(0, &cert0_len, cvc0, privkey0, 64) != ERR_OK)
  56. return FALSE;
  57. ASSERT(cert0_len == 365);
  58. // выпустить cert0
  59. memSetZero(cvc0->authority, sizeof(cvc0->authority));
  60. strCopy(cvc0->authority, "BYCA0000");
  61. memSetZero(cvc0->holder, sizeof(cvc0->holder));
  62. strCopy(cvc0->holder, "BYCA0000");
  63. if (btokCVCWrap(cert0, &cert0_len, cvc0, privkey0, 64) != ERR_OK)
  64. return FALSE;
  65. ASSERT(cert0_len < 365);
  66. // разобрать cert0
  67. if (btokCVCUnwrap(cvc1, cert0, cert0_len, 0, 0) != ERR_OK ||
  68. btokCVCUnwrap(cvc1, cert0, cert0_len, cvc0->pubkey,
  69. cvc0->pubkey_len) != ERR_OK ||
  70. !memEq(cvc0, cvc1, sizeof(btok_cvc_t)) ||
  71. btokCVCMatch(cert0, cert0_len, privkey0, 64) != ERR_OK)
  72. return FALSE;
  73. // составить и проверить cvc1
  74. memSetZero(cvc1, sizeof(btok_cvc_t));
  75. strCopy(cvc1->authority, "BYCA0000");
  76. strCopy(cvc1->holder, "BYCA1000");
  77. hexTo(cvc1->from, "020200070102");
  78. hexTo(cvc1->until, "020201010300");
  79. memSet(cvc1->hat_eid, 0xDD, sizeof(cvc1->hat_eid));
  80. memSet(cvc1->hat_esign, 0x33, sizeof(cvc1->hat_esign));
  81. cvc1->pubkey_len = 96;
  82. if (bignStdParams(params, "1.2.112.0.2.0.34.101.45.3.2") != ERR_OK ||
  83. bignGenKeypair(privkey1, cvc1->pubkey, params, prngEchoStepR,
  84. echo) != ERR_OK ||
  85. btokCVCCheck(cvc1) != ERR_OK)
  86. return FALSE;
  87. // создать pre-cert1 (запрос на выпуск сертификата)
  88. if (btokCVCWrap(0, &cert1_len, cvc1, privkey1, 48) != ERR_OK)
  89. return FALSE;
  90. ASSERT(cert1_len <= sizeof(cert1));
  91. if (btokCVCWrap(cert1, 0, cvc1, privkey1, 48) != ERR_OK)
  92. return FALSE;
  93. // разобрать pre-cert1:
  94. // - извлечь открытый ключ,
  95. // - проверить подпись,
  96. // - проверить соответствие authority <=> holder
  97. if (btokCVCUnwrap(cvc1, cert1, cert1_len, 0, 0) != ERR_OK ||
  98. btokCVCUnwrap(cvc2, cert1, cert1_len, cvc1->pubkey,
  99. cvc1->pubkey_len) != ERR_OK ||
  100. !memEq(cvc1, cvc2, sizeof(btok_cvc_t)) ||
  101. !strEq(cvc1->authority, cvc0->holder))
  102. return FALSE;
  103. // создать cert1
  104. if (btokCVCWrap(0, &cert1_len, cvc1, privkey0, 64) != ERR_OK)
  105. return FALSE;
  106. ASSERT(cert1_len <= sizeof(cert1));
  107. if (btokCVCWrap(cert1, &cert1_len, cvc1, privkey0, 64) != ERR_OK)
  108. return FALSE;
  109. // составить cvc2
  110. memSetZero(cvc2, sizeof(btok_cvc_t));
  111. strCopy(cvc2->authority, "BYCA1000");
  112. strCopy(cvc2->holder, "590082394654");
  113. hexTo(cvc2->from, "020200070102");
  114. hexTo(cvc2->until, "030901020301");
  115. memSet(cvc2->hat_eid, 0x88, sizeof(cvc2->hat_eid));
  116. memSet(cvc2->hat_esign, 0x11, sizeof(cvc2->hat_esign));
  117. cvc2->pubkey_len = 64;
  118. if (bignStdParams(params, "1.2.112.0.2.0.34.101.45.3.1") != ERR_OK ||
  119. bignGenKeypair(privkey2, cvc2->pubkey, params, prngEchoStepR,
  120. echo) != ERR_OK ||
  121. btokCVCCheck(cvc2) != ERR_OK)
  122. return FALSE;
  123. // выпустить cert2
  124. if (btokCVCIss(cert2, &cert2_len, cvc2, cert1, cert1_len - 1,
  125. privkey1, 48) == ERR_OK ||
  126. btokCVCIss(cert2, &cert2_len, cvc2, cert1, cert1_len,
  127. privkey1, 48 + 1) == ERR_OK ||
  128. btokCVCIss(cert2, &cert2_len, cvc2, cert1, cert1_len,
  129. privkey1, 48) != ERR_OK)
  130. return FALSE;
  131. ASSERT(cert2_len <= sizeof(cert2));
  132. // проверить сертификаты
  133. if (btokCVCVal(cert1, cert1_len, cert0, cert0_len, 0) != ERR_OK ||
  134. btokCVCVal(cert2, cert2_len, cert1, cert1_len, 0) != ERR_OK ||
  135. btokCVCVal(cert2, cert2_len, cert1, cert1_len, cvc0->from) == ERR_OK ||
  136. btokCVCVal2(cvc1, cert1, cert1_len, cvc0, 0) != ERR_OK ||
  137. btokCVCVal2(cvc2, cert2, cert2_len, cvc1, 0) != ERR_OK ||
  138. btokCVCVal2(cvc2, cert2, cert2_len, cvc1, cvc0->until) == ERR_OK)
  139. return FALSE;
  140. // все хорошо
  141. return TRUE;
  142. }
  143. bool_t btokTest()
  144. {
  145. return btokCVCTest();
  146. }