Revocation.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 ZT_ALWAYS_INLINE ZT_CredentialType credentialType() { return ZT_CREDENTIAL_TYPE_REVOCATION; }
  40. ZT_ALWAYS_INLINE Revocation() :
  41. _id(0),
  42. _credentialId(0),
  43. _networkId(0),
  44. _threshold(0),
  45. _flags(0),
  46. _target(),
  47. _signedBy(),
  48. _type(ZT_CREDENTIAL_TYPE_NULL),
  49. _signatureLength(0)
  50. {
  51. }
  52. /**
  53. * @param i ID (arbitrary for revocations, currently random)
  54. * @param nwid Network ID
  55. * @param cid Credential ID being revoked (0 for all or for COMs, which lack IDs)
  56. * @param thr Revocation time threshold before which credentials will be revoked
  57. * @param fl Flags
  58. * @param tgt Target node whose credential(s) are being revoked
  59. * @param ct Credential type being revoked
  60. */
  61. ZT_ALWAYS_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) :
  62. _id(i),
  63. _credentialId(cid),
  64. _networkId(nwid),
  65. _threshold(thr),
  66. _flags(fl),
  67. _target(tgt),
  68. _signedBy(),
  69. _type(ct),
  70. _signatureLength(0)
  71. {
  72. }
  73. ZT_ALWAYS_INLINE uint32_t id() const { return _id; }
  74. ZT_ALWAYS_INLINE uint32_t credentialId() const { return _credentialId; }
  75. ZT_ALWAYS_INLINE uint64_t networkId() const { return _networkId; }
  76. ZT_ALWAYS_INLINE int64_t threshold() const { return _threshold; }
  77. ZT_ALWAYS_INLINE const Address &target() const { return _target; }
  78. ZT_ALWAYS_INLINE const Address &signer() const { return _signedBy; }
  79. ZT_ALWAYS_INLINE ZT_CredentialType typeBeingRevoked() const { return _type; }
  80. ZT_ALWAYS_INLINE const uint8_t *signature() const { return _signature; }
  81. ZT_ALWAYS_INLINE unsigned int signatureLength() const { return _signatureLength; }
  82. ZT_ALWAYS_INLINE bool fastPropagate() const { return ((_flags & ZT_REVOCATION_FLAG_FAST_PROPAGATE) != 0); }
  83. /**
  84. * @param signer Signing identity, must have private key
  85. * @return True if signature was successful
  86. */
  87. bool sign(const Identity &signer);
  88. /**
  89. * Verify this revocation's signature
  90. *
  91. * @param RR Runtime environment to provide for peer lookup, etc.
  92. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  93. */
  94. ZT_ALWAYS_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const { return _verify(RR,tPtr,*this); }
  95. static ZT_ALWAYS_INLINE int marshalSizeMax() { return ZT_REVOCATION_MARSHAL_SIZE_MAX; }
  96. int marshal(uint8_t data[ZT_REVOCATION_MARSHAL_SIZE_MAX],bool forSign = false) const;
  97. int unmarshal(const uint8_t *restrict data,const int len);
  98. private:
  99. uint32_t _id;
  100. uint32_t _credentialId;
  101. uint64_t _networkId;
  102. int64_t _threshold;
  103. uint64_t _flags;
  104. Address _target;
  105. Address _signedBy;
  106. ZT_CredentialType _type;
  107. unsigned int _signatureLength;
  108. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  109. };
  110. } // namespace ZeroTier
  111. #endif