Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include "TrustedCertificates.h"
  2.  
  3. #include <stdexcept>
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstring>
  7.  
  8. namespace TrustedCertificates
  9. {
  10. namespace
  11. {
  12. template <char... cs>
  13. constexpr auto operator"" _bin()
  14. {
  15. struct HexadecimalNumberChecker
  16. {
  17. bool status{ true };
  18. int count{ 0 };
  19. constexpr HexadecimalNumberChecker operator +(char c) const
  20. {
  21. if (count == 0) { return { status && c == '0', count + 1 }; }
  22. if (count == 1) { return { status && (c == 'x' || c == 'X'), count + 1 }; }
  23. return { status && (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'), count + 1 };
  24. }
  25. constexpr operator bool() const { return status && count % 2 == 0; }
  26. };
  27. constexpr bool check = (HexadecimalNumberChecker() + ... + cs);
  28. static_assert(check, "Only hexadecimal number are accepted.");
  29. if (!check) { throw std::invalid_argument("Only hexadecimal number are accepted."); }
  30.  
  31. constexpr std::array<char, sizeof...(cs)> data{ cs... };
  32. std::array<std::uint8_t, sizeof...(cs) / 2 - 1> result{};
  33. auto nibble = [](char input)
  34. {
  35. if (input >= '0' && input <= '9') return input - '0';
  36. if (input >= 'A' && input <= 'F') return input - 'A' + 10;
  37. if (input >= 'a' && input <= 'f') return input - 'a' + 10;
  38. return 0;
  39. };
  40. for (size_t i = 0; i < result.size(); i++)
  41. {
  42. result[i] = nibble(data[2 + i * 2]) << 4 | nibble(data[2 + i * 2 + 1]);
  43. }
  44. return result;
  45. }
  46.  
  47. template <class T, size_t SZ>
  48. constexpr bool is_sorted(const T (&x)[SZ])
  49. {
  50. for (size_t i = 1; i < SZ; i++)
  51. {
  52. if (x[i] < x[i - 1]) { return false; }
  53. }
  54. return true;
  55. }
  56. }
  57.  
  58. bool IsRegistered(const uint8_t sha1[20], const uint8_t md5[16])
  59. {
  60. struct CertEntry
  61. {
  62. std::array<uint8_t, 20> sha1{};
  63. std::array<uint8_t, 16> md5{};
  64. constexpr bool operator <(const CertEntry& e) const
  65. {
  66. for (size_t i = 0; i < sha1.size(); i++) { if (sha1[i] != e.sha1[i]) { return sha1[i] < e.sha1[i]; } }
  67. for (size_t i = 0; i < md5.size(); i++) { if (md5[i] != e.md5[i]) { return md5[i] < e.md5[i]; } }
  68. return false;
  69. }
  70. };
  71.  
  72. CertEntry e{};
  73. memcpy(e.sha1.data(), sha1, 20);
  74. memcpy(e.md5.data(), md5, 16);
  75.  
  76. constexpr static CertEntry db[] =
  77. {
  78. #define CERT_ENTRY(SHA1,MD5) [](){ constexpr CertEntry x = { 0x ## SHA1 ##_bin, 0x ## MD5 ## _bin }; return x; }()
  79. #include "trusted_auth_root_hash_list.txt"
  80. #undef CERT_ENTRY
  81. };
  82.  
  83. static_assert(is_sorted(db), "db must be sorted.");
  84. return std::binary_search(std::begin(db), std::end(db), e);
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement