RevocationCredential.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef ZT_REVOCATION_HPP
  14. #define ZT_REVOCATION_HPP
  15. #include "Constants.hpp"
  16. #include "Credential.hpp"
  17. #include "Address.hpp"
  18. #include "C25519.hpp"
  19. #include "Utils.hpp"
  20. #include "Identity.hpp"
  21. /**
  22. * Flag: fast propagation via rumor mill algorithm
  23. */
  24. #define ZT_REVOCATION_FLAG_FAST_PROPAGATE 0x1ULL
  25. #define ZT_REVOCATION_MARSHAL_SIZE_MAX (4 + 4 + 8 + 4 + 4 + 8 + 8 + 5 + 5 + 1 + 1 + 2 + ZT_SIGNATURE_BUFFER_SIZE + 2)
  26. namespace ZeroTier {
  27. class RuntimeEnvironment;
  28. /**
  29. * Revocation certificate to instantaneously revoke a COM, capability, or tag
  30. */
  31. class RevocationCredential : public Credential
  32. {
  33. friend class Credential;
  34. public:
  35. static constexpr ZT_CredentialType credentialType() noexcept
  36. { return ZT_CREDENTIAL_TYPE_REVOCATION; }
  37. ZT_INLINE RevocationCredential() noexcept
  38. { memoryZero(this); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  39. /**
  40. * @param i ID (arbitrary for revocations, currently random)
  41. * @param nwid Network ID
  42. * @param cid Credential ID being revoked (0 for all or for COMs, which lack IDs)
  43. * @param thr Revocation time threshold before which credentials will be revoked
  44. * @param fl Flags
  45. * @param tgt Target node whose credential(s) are being revoked
  46. * @param ct Credential type being revoked
  47. */
  48. ZT_INLINE RevocationCredential(const uint32_t i, const uint64_t nwid, const uint32_t cid, const uint64_t thr, const uint64_t fl, const Address &tgt, const ZT_CredentialType ct) noexcept: // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  49. m_id(i),
  50. m_credentialId(cid),
  51. m_networkId(nwid),
  52. m_threshold(thr),
  53. m_flags(fl),
  54. m_target(tgt),
  55. m_signedBy(),
  56. m_type(ct),
  57. m_signatureLength(0)
  58. {
  59. }
  60. ZT_INLINE uint32_t id() const noexcept
  61. { return m_id; }
  62. ZT_INLINE uint32_t credentialId() const noexcept
  63. { return m_credentialId; }
  64. ZT_INLINE uint64_t networkId() const noexcept
  65. { return m_networkId; }
  66. ZT_INLINE int64_t threshold() const noexcept
  67. { return m_threshold; }
  68. ZT_INLINE const Address &target() const noexcept
  69. { return m_target; }
  70. ZT_INLINE const Address &signer() const noexcept
  71. { return m_signedBy; }
  72. ZT_INLINE ZT_CredentialType typeBeingRevoked() const noexcept
  73. { return m_type; }
  74. ZT_INLINE const uint8_t *signature() const noexcept
  75. { return m_signature; }
  76. ZT_INLINE unsigned int signatureLength() const noexcept
  77. { return m_signatureLength; }
  78. ZT_INLINE bool fastPropagate() const noexcept
  79. { return ((m_flags & ZT_REVOCATION_FLAG_FAST_PROPAGATE) != 0); }
  80. /**
  81. * @param signer Signing identity, must have private key
  82. * @return True if signature was successful
  83. */
  84. bool sign(const Identity &signer) noexcept;
  85. /**
  86. * Verify this revocation's signature
  87. *
  88. * @param RR Runtime environment to provide for peer lookup, etc.
  89. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  90. */
  91. ZT_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR, void *tPtr) const noexcept
  92. { return s_verify(RR, tPtr, *this); }
  93. static constexpr int marshalSizeMax() noexcept
  94. { return ZT_REVOCATION_MARSHAL_SIZE_MAX; }
  95. int marshal(uint8_t data[ZT_REVOCATION_MARSHAL_SIZE_MAX], bool forSign = false) const noexcept;
  96. int unmarshal(const uint8_t *restrict data, int len) noexcept;
  97. private:
  98. uint32_t m_id;
  99. uint32_t m_credentialId;
  100. uint64_t m_networkId;
  101. int64_t m_threshold;
  102. uint64_t m_flags;
  103. Address m_target;
  104. Address m_signedBy;
  105. ZT_CredentialType m_type;
  106. unsigned int m_signatureLength;
  107. uint8_t m_signature[ZT_SIGNATURE_BUFFER_SIZE];
  108. };
  109. } // namespace ZeroTier
  110. #endif