Revocation.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "Address.hpp"
  27. #include "C25519.hpp"
  28. #include "Utils.hpp"
  29. #include "Buffer.hpp"
  30. #include "Identity.hpp"
  31. /**
  32. * Flag: fast propagation via rumor mill algorithm
  33. */
  34. #define ZT_REVOCATION_FLAG_FAST_PROPAGATE 0x1ULL
  35. namespace ZeroTier {
  36. class RuntimeEnvironment;
  37. /**
  38. * Revocation certificate to instantaneously revoke a COM, capability, or tag
  39. */
  40. class Revocation
  41. {
  42. public:
  43. /**
  44. * Credential type being revoked
  45. */
  46. enum CredentialType
  47. {
  48. CREDENTIAL_TYPE_NULL = 0,
  49. CREDENTIAL_TYPE_COM = 1, // CertificateOfMembership
  50. CREDENTIAL_TYPE_CAPABILITY = 2,
  51. CREDENTIAL_TYPE_TAG = 3,
  52. CREDENTIAL_TYPE_COO = 4 // CertificateOfOwnership
  53. };
  54. Revocation()
  55. {
  56. memset(this,0,sizeof(Revocation));
  57. }
  58. /**
  59. * @param i ID (arbitrary for revocations, currently random)
  60. * @param nwid Network ID
  61. * @param cid Credential ID being revoked (0 for all or for COMs, which lack IDs)
  62. * @param thr Revocation time threshold before which credentials will be revoked
  63. * @param fl Flags
  64. * @param tgt Target node whose credential(s) are being revoked
  65. * @param ct Credential type being revoked
  66. */
  67. Revocation(const uint64_t i,const uint64_t nwid,const uint64_t cid,const uint64_t thr,const uint64_t fl,const Address &tgt,const CredentialType ct) :
  68. _id(i),
  69. _networkId(nwid),
  70. _credentialId(cid),
  71. _threshold(thr),
  72. _flags(fl),
  73. _target(tgt),
  74. _signedBy(),
  75. _type(ct) {}
  76. inline uint64_t id() const { return _id; }
  77. inline uint64_t networkId() const { return _networkId; }
  78. inline uint64_t credentialId() const { return _credentialId; }
  79. inline uint64_t threshold() const { return _threshold; }
  80. inline const Address &target() const { return _target; }
  81. inline const Address &signer() const { return _signedBy; }
  82. inline CredentialType type() const { return _type; }
  83. inline bool fastPropagate() const { return ((_flags & ZT_REVOCATION_FLAG_FAST_PROPAGATE) != 0); }
  84. /**
  85. * @param signer Signing identity, must have private key
  86. * @return True if signature was successful
  87. */
  88. inline bool sign(const Identity &signer)
  89. {
  90. if (signer.hasPrivate()) {
  91. Buffer<sizeof(Revocation) + 64> tmp;
  92. _signedBy = signer.address();
  93. this->serialize(tmp,true);
  94. _signature = signer.sign(tmp.data(),tmp.size());
  95. return true;
  96. }
  97. return false;
  98. }
  99. /**
  100. * Verify this revocation's signature
  101. *
  102. * @param RR Runtime environment to provide for peer lookup, etc.
  103. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or chain
  104. */
  105. int verify(const RuntimeEnvironment *RR) const;
  106. template<unsigned int C>
  107. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  108. {
  109. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  110. b.append(_id);
  111. b.append(_networkId);
  112. b.append(_credentialId);
  113. b.append(_threshold);
  114. b.append(_flags);
  115. _target.appendTo(b);
  116. _signedBy.appendTo(b);
  117. b.append((uint8_t)_type);
  118. if (!forSign) {
  119. b.append((uint8_t)1); // 1 == Ed25519 signature
  120. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  121. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  122. }
  123. // This is the size of any additional fields, currently 0.
  124. b.append((uint16_t)0);
  125. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  126. }
  127. template<unsigned int C>
  128. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  129. {
  130. memset(this,0,sizeof(Revocation));
  131. unsigned int p = startAt;
  132. _id = b.template at<uint64_t>(p); p += 8;
  133. _networkId = b.template at<uint64_t>(p); p += 8;
  134. _credentialId = b.template at<uint64_t>(p); p += 8;
  135. _threshold = b.template at<uint64_t>(p); p += 8;
  136. _flags = b.template at<uint64_t>(p); p += 8;
  137. _target.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  138. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  139. _type = (CredentialType)b[p++];
  140. if (b[p++] == 1) {
  141. if (b.template at<uint16_t>(p) == ZT_C25519_SIGNATURE_LEN) {
  142. p += 2;
  143. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
  144. p += ZT_C25519_SIGNATURE_LEN;
  145. } else throw std::runtime_error("invalid signature");
  146. } else {
  147. p += 2 + b.template at<uint16_t>(p);
  148. }
  149. p += 2 + b.template at<uint16_t>(p);
  150. if (p > b.size())
  151. throw std::runtime_error("extended field overflow");
  152. return (p - startAt);
  153. }
  154. private:
  155. uint64_t _id;
  156. uint64_t _networkId;
  157. uint64_t _credentialId;
  158. uint64_t _threshold;
  159. uint64_t _flags;
  160. Address _target;
  161. Address _signedBy;
  162. CredentialType _type;
  163. C25519::Signature _signature;
  164. };
  165. } // namespace ZeroTier
  166. #endif