Revocation.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: 2024-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 "Revocation.hpp"
  14. namespace ZeroTier {
  15. bool Revocation::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 Revocation::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); p += 4;
  33. Utils::storeBigEndian<uint32_t>(data + p, m_id); p += 4;
  34. Utils::storeBigEndian<uint64_t>(data + p, m_networkId); p += 8;
  35. Utils::storeBigEndian<uint32_t>(data + p,0); p += 4;
  36. Utils::storeBigEndian<uint32_t>(data + p, m_credentialId); p += 4;
  37. Utils::storeBigEndian<uint64_t>(data + p,(uint64_t)m_threshold); p += 8;
  38. Utils::storeBigEndian<uint64_t>(data + p, m_flags); p += 8;
  39. m_target.copyTo(data + p); p += ZT_ADDRESS_LENGTH;
  40. m_signedBy.copyTo(data + p); p += ZT_ADDRESS_LENGTH;
  41. data[p++] = (uint8_t)m_type;
  42. if (!forSign) {
  43. data[p++] = 1;
  44. Utils::storeBigEndian<uint16_t>(data + p,(uint16_t)m_signatureLength);
  45. Utils::copy(data + p, m_signature, m_signatureLength);
  46. p += (int)m_signatureLength;
  47. }
  48. data[p++] = 0;
  49. data[p++] = 0;
  50. if (forSign) {
  51. for(int k=0;k<8;++k)
  52. data[p++] = 0x7f;
  53. }
  54. return p;
  55. }
  56. int Revocation::unmarshal(const uint8_t *restrict data,const int len) noexcept
  57. {
  58. if (len < 54)
  59. return -1;
  60. // 4 bytes reserved
  61. m_id = Utils::loadBigEndian<uint32_t>(data + 4);
  62. m_networkId = Utils::loadBigEndian<uint64_t>(data + 8);
  63. // 4 bytes reserved
  64. m_credentialId = Utils::loadBigEndian<uint32_t>(data + 20);
  65. m_threshold = (int64_t)Utils::loadBigEndian<uint64_t>(data + 24);
  66. m_flags = Utils::loadBigEndian<uint64_t>(data + 32);
  67. m_target.setTo(data + 40);
  68. m_signedBy.setTo(data + 45);
  69. m_type = (ZT_CredentialType)data[50];
  70. // 1 byte reserved
  71. m_signatureLength = Utils::loadBigEndian<uint16_t>(data + 52);
  72. int p = 54 + (int)m_signatureLength;
  73. if ((m_signatureLength > ZT_SIGNATURE_BUFFER_SIZE) || (p > len))
  74. return -1;
  75. Utils::copy(m_signature, data + 54, m_signatureLength);
  76. if ((p + 2) > len)
  77. return -1;
  78. p += 2 + Utils::loadBigEndian<uint16_t>(data + p);
  79. if (p > len)
  80. return -1;
  81. return p;
  82. }
  83. } // namespace ZeroTier