Revocation.hpp 3.8 KB

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