Revocation.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #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 Revocation : public Credential
  32. {
  33. friend class Credential;
  34. public:
  35. static constexpr ZT_CredentialType credentialType() noexcept { return ZT_CREDENTIAL_TYPE_REVOCATION; }
  36. ZT_INLINE Revocation() noexcept { memoryZero(this); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  37. /**
  38. * @param i ID (arbitrary for revocations, currently random)
  39. * @param nwid Network ID
  40. * @param cid Credential ID being revoked (0 for all or for COMs, which lack IDs)
  41. * @param thr Revocation time threshold before which credentials will be revoked
  42. * @param fl Flags
  43. * @param tgt Target node whose credential(s) are being revoked
  44. * @param ct Credential type being revoked
  45. */
  46. ZT_INLINE Revocation(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)
  47. m_id(i),
  48. m_credentialId(cid),
  49. m_networkId(nwid),
  50. m_threshold(thr),
  51. m_flags(fl),
  52. m_target(tgt),
  53. m_signedBy(),
  54. m_type(ct),
  55. m_signatureLength(0)
  56. {
  57. }
  58. ZT_INLINE uint32_t id() const noexcept { return m_id; }
  59. ZT_INLINE uint32_t credentialId() const noexcept { return m_credentialId; }
  60. ZT_INLINE uint64_t networkId() const noexcept { return m_networkId; }
  61. ZT_INLINE int64_t threshold() const noexcept { return m_threshold; }
  62. ZT_INLINE const Address &target() const noexcept { return m_target; }
  63. ZT_INLINE const Address &signer() const noexcept { return m_signedBy; }
  64. ZT_INLINE ZT_CredentialType typeBeingRevoked() const noexcept { return m_type; }
  65. ZT_INLINE const uint8_t *signature() const noexcept { return m_signature; }
  66. ZT_INLINE unsigned int signatureLength() const noexcept { return m_signatureLength; }
  67. ZT_INLINE bool fastPropagate() const noexcept { return ((m_flags & ZT_REVOCATION_FLAG_FAST_PROPAGATE) != 0); }
  68. /**
  69. * @param signer Signing identity, must have private key
  70. * @return True if signature was successful
  71. */
  72. bool sign(const Identity &signer) noexcept;
  73. /**
  74. * Verify this revocation's signature
  75. *
  76. * @param RR Runtime environment to provide for peer lookup, etc.
  77. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  78. */
  79. ZT_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const noexcept { return _verify(RR,tPtr,*this); }
  80. static constexpr int marshalSizeMax() noexcept { return ZT_REVOCATION_MARSHAL_SIZE_MAX; }
  81. int marshal(uint8_t data[ZT_REVOCATION_MARSHAL_SIZE_MAX],bool forSign = false) const noexcept;
  82. int unmarshal(const uint8_t *restrict data,int len) noexcept;
  83. private:
  84. uint32_t m_id;
  85. uint32_t m_credentialId;
  86. uint64_t m_networkId;
  87. int64_t m_threshold;
  88. uint64_t m_flags;
  89. Address m_target;
  90. Address m_signedBy;
  91. ZT_CredentialType m_type;
  92. unsigned int m_signatureLength;
  93. uint8_t m_signature[ZT_SIGNATURE_BUFFER_SIZE];
  94. };
  95. } // namespace ZeroTier
  96. #endif