Revocation.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_REVOCATION_HPP
  19. #define ZT_REVOCATION_HPP
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include "Constants.hpp"
  25. #include "../include/ZeroTierOne.h"
  26. #include "Credential.hpp"
  27. #include "Address.hpp"
  28. #include "C25519.hpp"
  29. #include "Utils.hpp"
  30. #include "Buffer.hpp"
  31. #include "Identity.hpp"
  32. /**
  33. * Flag: fast propagation via rumor mill algorithm
  34. */
  35. #define ZT_REVOCATION_FLAG_FAST_PROPAGATE 0x1ULL
  36. namespace ZeroTier {
  37. class RuntimeEnvironment;
  38. /**
  39. * Revocation certificate to instantaneously revoke a COM, capability, or tag
  40. */
  41. class Revocation : public Credential
  42. {
  43. public:
  44. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_REVOCATION; }
  45. Revocation()
  46. {
  47. memset(this,0,sizeof(Revocation));
  48. }
  49. /**
  50. * @param i ID (arbitrary for revocations, currently random)
  51. * @param nwid Network ID
  52. * @param cid Credential ID being revoked (0 for all or for COMs, which lack IDs)
  53. * @param thr Revocation time threshold before which credentials will be revoked
  54. * @param fl Flags
  55. * @param tgt Target node whose credential(s) are being revoked
  56. * @param ct Credential type being revoked
  57. */
  58. 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 Credential::Type ct) :
  59. _id(i),
  60. _credentialId(cid),
  61. _networkId(nwid),
  62. _threshold(thr),
  63. _flags(fl),
  64. _target(tgt),
  65. _signedBy(),
  66. _type(ct) {}
  67. inline uint32_t id() const { return _id; }
  68. inline uint32_t credentialId() const { return _credentialId; }
  69. inline uint64_t networkId() const { return _networkId; }
  70. inline uint64_t threshold() const { return _threshold; }
  71. inline const Address &target() const { return _target; }
  72. inline const Address &signer() const { return _signedBy; }
  73. inline Credential::Type type() const { return _type; }
  74. inline bool fastPropagate() const { return ((_flags & ZT_REVOCATION_FLAG_FAST_PROPAGATE) != 0); }
  75. /**
  76. * @param signer Signing identity, must have private key
  77. * @return True if signature was successful
  78. */
  79. inline bool sign(const Identity &signer)
  80. {
  81. if (signer.hasPrivate()) {
  82. Buffer<sizeof(Revocation) + 64> tmp;
  83. _signedBy = signer.address();
  84. this->serialize(tmp,true);
  85. _signature = signer.sign(tmp.data(),tmp.size());
  86. return true;
  87. }
  88. return false;
  89. }
  90. /**
  91. * Verify this revocation's signature
  92. *
  93. * @param RR Runtime environment to provide for peer lookup, etc.
  94. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  95. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or chain
  96. */
  97. int verify(const RuntimeEnvironment *RR,void *tPtr) const;
  98. template<unsigned int C>
  99. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  100. {
  101. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  102. b.append((uint32_t)0); // 4 unused bytes, currently set to 0
  103. b.append(_id);
  104. b.append(_networkId);
  105. b.append((uint32_t)0); // 4 unused bytes, currently set to 0
  106. b.append(_credentialId);
  107. b.append(_threshold);
  108. b.append(_flags);
  109. _target.appendTo(b);
  110. _signedBy.appendTo(b);
  111. b.append((uint8_t)_type);
  112. if (!forSign) {
  113. b.append((uint8_t)1); // 1 == Ed25519 signature
  114. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  115. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  116. }
  117. // This is the size of any additional fields, currently 0.
  118. b.append((uint16_t)0);
  119. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  120. }
  121. template<unsigned int C>
  122. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  123. {
  124. memset(this,0,sizeof(Revocation));
  125. unsigned int p = startAt;
  126. p += 4; // 4 bytes, currently unused
  127. _id = b.template at<uint32_t>(p); p += 4;
  128. _networkId = b.template at<uint64_t>(p); p += 8;
  129. p += 4; // 4 bytes, currently unused
  130. _credentialId = b.template at<uint32_t>(p); p += 4;
  131. _threshold = b.template at<uint64_t>(p); p += 8;
  132. _flags = b.template at<uint64_t>(p); p += 8;
  133. _target.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  134. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  135. _type = (Credential::Type)b[p++];
  136. if (b[p++] == 1) {
  137. if (b.template at<uint16_t>(p) == ZT_C25519_SIGNATURE_LEN) {
  138. p += 2;
  139. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
  140. p += ZT_C25519_SIGNATURE_LEN;
  141. } else throw std::runtime_error("invalid signature");
  142. } else {
  143. p += 2 + b.template at<uint16_t>(p);
  144. }
  145. p += 2 + b.template at<uint16_t>(p);
  146. if (p > b.size())
  147. throw std::runtime_error("extended field overflow");
  148. return (p - startAt);
  149. }
  150. private:
  151. uint32_t _id;
  152. uint32_t _credentialId;
  153. uint64_t _networkId;
  154. uint64_t _threshold;
  155. uint64_t _flags;
  156. Address _target;
  157. Address _signedBy;
  158. Credential::Type _type;
  159. C25519::Signature _signature;
  160. };
  161. } // namespace ZeroTier
  162. #endif