RevocationCredential.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "RevocationCredential.hpp"
  14. namespace ZeroTier {
  15. bool RevocationCredential::sign(const Identity &signer) noexcept
  16. {
  17. uint8_t buf[ZT_REVOCATION_MARSHAL_SIZE_MAX + 32];
  18. if (signer.hasPrivate()) {
  19. m_signedBy = signer.address();
  20. m_signatureLength = signer.sign(buf, (unsigned int)marshal(buf, true), m_signature, sizeof(m_signature));
  21. return true;
  22. }
  23. return false;
  24. }
  25. int RevocationCredential::marshal(uint8_t data[ZT_REVOCATION_MARSHAL_SIZE_MAX], bool forSign) const noexcept
  26. {
  27. int p = 0;
  28. if (forSign) {
  29. for (int k = 0; k < 8; ++k)
  30. data[p++] = 0x7f;
  31. }
  32. Utils::storeBigEndian< uint32_t >(data + p, 0);
  33. p += 4;
  34. Utils::storeBigEndian< uint32_t >(data + p, m_id);
  35. p += 4;
  36. Utils::storeBigEndian< uint64_t >(data + p, m_networkId);
  37. p += 8;
  38. Utils::storeBigEndian< uint32_t >(data + p, 0);
  39. p += 4;
  40. Utils::storeBigEndian< uint32_t >(data + p, m_credentialId);
  41. p += 4;
  42. Utils::storeBigEndian< uint64_t >(data + p, (uint64_t)m_threshold);
  43. p += 8;
  44. Utils::storeBigEndian< uint64_t >(data + p, m_flags);
  45. p += 8;
  46. m_target.copyTo(data + p);
  47. p += ZT_ADDRESS_LENGTH;
  48. m_signedBy.copyTo(data + p);
  49. p += ZT_ADDRESS_LENGTH;
  50. data[p++] = (uint8_t)m_type;
  51. if (!forSign) {
  52. data[p++] = 1;
  53. Utils::storeBigEndian< uint16_t >(data + p, (uint16_t)m_signatureLength);
  54. Utils::copy(data + p, m_signature, m_signatureLength);
  55. p += (int)m_signatureLength;
  56. }
  57. data[p++] = 0;
  58. data[p++] = 0;
  59. if (forSign) {
  60. for (int k = 0; k < 8; ++k)
  61. data[p++] = 0x7f;
  62. }
  63. return p;
  64. }
  65. int RevocationCredential::unmarshal(const uint8_t *restrict data, const int len) noexcept
  66. {
  67. if (len < 54)
  68. return -1;
  69. // 4 bytes reserved
  70. m_id = Utils::loadBigEndian< uint32_t >(data + 4);
  71. m_networkId = Utils::loadBigEndian< uint64_t >(data + 8);
  72. // 4 bytes reserved
  73. m_credentialId = Utils::loadBigEndian< uint32_t >(data + 20);
  74. m_threshold = (int64_t)Utils::loadBigEndian< uint64_t >(data + 24);
  75. m_flags = Utils::loadBigEndian< uint64_t >(data + 32);
  76. m_target.setTo(data + 40);
  77. m_signedBy.setTo(data + 45);
  78. m_type = (ZT_CredentialType)data[50];
  79. // 1 byte reserved
  80. m_signatureLength = Utils::loadBigEndian< uint16_t >(data + 52);
  81. int p = 54 + (int)m_signatureLength;
  82. if ((m_signatureLength > ZT_SIGNATURE_BUFFER_SIZE) || (p > len))
  83. return -1;
  84. Utils::copy(m_signature, data + 54, m_signatureLength);
  85. if ((p + 2) > len)
  86. return -1;
  87. p += 2 + Utils::loadBigEndian< uint16_t >(data + p);
  88. if (p > len)
  89. return -1;
  90. return p;
  91. }
  92. } // namespace ZeroTier