Revocation.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_REVOCATION_HPP
  27. #define ZT_REVOCATION_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdint.h>
  32. #include "Constants.hpp"
  33. #include "../include/ZeroTierOne.h"
  34. #include "Credential.hpp"
  35. #include "Address.hpp"
  36. #include "C25519.hpp"
  37. #include "Utils.hpp"
  38. #include "Buffer.hpp"
  39. #include "Identity.hpp"
  40. /**
  41. * Flag: fast propagation via rumor mill algorithm
  42. */
  43. #define ZT_REVOCATION_FLAG_FAST_PROPAGATE 0x1ULL
  44. namespace ZeroTier {
  45. class RuntimeEnvironment;
  46. /**
  47. * Revocation certificate to instantaneously revoke a COM, capability, or tag
  48. */
  49. class Revocation : public Credential
  50. {
  51. public:
  52. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_REVOCATION; }
  53. Revocation() :
  54. _id(0),
  55. _credentialId(0),
  56. _networkId(0),
  57. _threshold(0),
  58. _flags(0),
  59. _target(),
  60. _signedBy(),
  61. _type(Credential::CREDENTIAL_TYPE_NULL)
  62. {
  63. memset(_signature.data,0,sizeof(_signature.data));
  64. }
  65. /**
  66. * @param i ID (arbitrary for revocations, currently random)
  67. * @param nwid Network ID
  68. * @param cid Credential ID being revoked (0 for all or for COMs, which lack IDs)
  69. * @param thr Revocation time threshold before which credentials will be revoked
  70. * @param fl Flags
  71. * @param tgt Target node whose credential(s) are being revoked
  72. * @param ct Credential type being revoked
  73. */
  74. 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) :
  75. _id(i),
  76. _credentialId(cid),
  77. _networkId(nwid),
  78. _threshold(thr),
  79. _flags(fl),
  80. _target(tgt),
  81. _signedBy(),
  82. _type(ct)
  83. {
  84. memset(_signature.data,0,sizeof(_signature.data));
  85. }
  86. inline uint32_t id() const { return _id; }
  87. inline uint32_t credentialId() const { return _credentialId; }
  88. inline uint64_t networkId() const { return _networkId; }
  89. inline int64_t threshold() const { return _threshold; }
  90. inline const Address &target() const { return _target; }
  91. inline const Address &signer() const { return _signedBy; }
  92. inline Credential::Type type() const { return _type; }
  93. inline bool fastPropagate() const { return ((_flags & ZT_REVOCATION_FLAG_FAST_PROPAGATE) != 0); }
  94. /**
  95. * @param signer Signing identity, must have private key
  96. * @return True if signature was successful
  97. */
  98. inline bool sign(const Identity &signer)
  99. {
  100. if (signer.hasPrivate()) {
  101. Buffer<sizeof(Revocation) + 64> tmp;
  102. _signedBy = signer.address();
  103. this->serialize(tmp,true);
  104. _signature = signer.sign(tmp.data(),tmp.size());
  105. return true;
  106. }
  107. return false;
  108. }
  109. /**
  110. * Verify this revocation's signature
  111. *
  112. * @param RR Runtime environment to provide for peer lookup, etc.
  113. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  114. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or chain
  115. */
  116. int verify(const RuntimeEnvironment *RR,void *tPtr) const;
  117. template<unsigned int C>
  118. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  119. {
  120. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  121. b.append((uint32_t)0); // 4 unused bytes, currently set to 0
  122. b.append(_id);
  123. b.append(_networkId);
  124. b.append((uint32_t)0); // 4 unused bytes, currently set to 0
  125. b.append(_credentialId);
  126. b.append(_threshold);
  127. b.append(_flags);
  128. _target.appendTo(b);
  129. _signedBy.appendTo(b);
  130. b.append((uint8_t)_type);
  131. if (!forSign) {
  132. b.append((uint8_t)1); // 1 == Ed25519 signature
  133. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  134. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  135. }
  136. // This is the size of any additional fields, currently 0.
  137. b.append((uint16_t)0);
  138. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  139. }
  140. template<unsigned int C>
  141. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  142. {
  143. *this = Revocation();
  144. unsigned int p = startAt;
  145. p += 4; // 4 bytes, currently unused
  146. _id = b.template at<uint32_t>(p); p += 4;
  147. _networkId = b.template at<uint64_t>(p); p += 8;
  148. p += 4; // 4 bytes, currently unused
  149. _credentialId = b.template at<uint32_t>(p); p += 4;
  150. _threshold = b.template at<uint64_t>(p); p += 8;
  151. _flags = b.template at<uint64_t>(p); p += 8;
  152. _target.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  153. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  154. _type = (Credential::Type)b[p++];
  155. if (b[p++] == 1) {
  156. if (b.template at<uint16_t>(p) == ZT_C25519_SIGNATURE_LEN) {
  157. p += 2;
  158. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
  159. p += ZT_C25519_SIGNATURE_LEN;
  160. } else throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  161. } else {
  162. p += 2 + b.template at<uint16_t>(p);
  163. }
  164. p += 2 + b.template at<uint16_t>(p);
  165. if (p > b.size())
  166. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  167. return (p - startAt);
  168. }
  169. private:
  170. uint32_t _id;
  171. uint32_t _credentialId;
  172. uint64_t _networkId;
  173. int64_t _threshold;
  174. uint64_t _flags;
  175. Address _target;
  176. Address _signedBy;
  177. Credential::Type _type;
  178. C25519::Signature _signature;
  179. };
  180. } // namespace ZeroTier
  181. #endif