2016-09-23 23:08:38 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2016-09-23 23:08:38 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2016-09-23 23:08:38 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Change Date: 2023-01-01
|
2016-09-23 23:08:38 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2016-09-23 23:08:38 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2016-09-23 23:08:38 +00:00
|
|
|
|
|
|
|
#ifndef ZT_REVOCATION_HPP
|
|
|
|
#define ZT_REVOCATION_HPP
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
|
|
|
#include "../include/ZeroTierOne.h"
|
2017-04-04 15:07:38 +00:00
|
|
|
#include "Credential.hpp"
|
2016-09-23 23:08:38 +00:00
|
|
|
#include "Address.hpp"
|
|
|
|
#include "C25519.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Buffer.hpp"
|
|
|
|
#include "Identity.hpp"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag: fast propagation via rumor mill algorithm
|
|
|
|
*/
|
|
|
|
#define ZT_REVOCATION_FLAG_FAST_PROPAGATE 0x1ULL
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Revocation certificate to instantaneously revoke a COM, capability, or tag
|
|
|
|
*/
|
2017-04-04 15:07:38 +00:00
|
|
|
class Revocation : public Credential
|
2016-09-23 23:08:38 +00:00
|
|
|
{
|
2019-08-14 21:55:37 +00:00
|
|
|
friend class Credential;
|
|
|
|
|
2016-09-23 23:08:38 +00:00
|
|
|
public:
|
2017-04-04 15:07:38 +00:00
|
|
|
static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_REVOCATION; }
|
2016-09-23 23:08:38 +00:00
|
|
|
|
2019-08-14 19:48:45 +00:00
|
|
|
inline Revocation() :
|
2019-03-25 18:45:32 +00:00
|
|
|
_id(0),
|
|
|
|
_credentialId(0),
|
|
|
|
_networkId(0),
|
|
|
|
_threshold(0),
|
|
|
|
_flags(0),
|
|
|
|
_target(),
|
|
|
|
_signedBy(),
|
2019-07-17 15:52:08 +00:00
|
|
|
_type(Credential::CREDENTIAL_TYPE_NULL),
|
|
|
|
_signatureLength(0)
|
2016-09-23 23:08:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-13 13:53:23 +00:00
|
|
|
/**
|
|
|
|
* @param i ID (arbitrary for revocations, currently random)
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param cid Credential ID being revoked (0 for all or for COMs, which lack IDs)
|
|
|
|
* @param thr Revocation time threshold before which credentials will be revoked
|
|
|
|
* @param fl Flags
|
|
|
|
* @param tgt Target node whose credential(s) are being revoked
|
|
|
|
* @param ct Credential type being revoked
|
|
|
|
*/
|
2019-08-14 19:48:45 +00:00
|
|
|
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 Credential::Type ct) :
|
2016-09-23 23:08:38 +00:00
|
|
|
_id(i),
|
|
|
|
_credentialId(cid),
|
2017-04-04 15:07:38 +00:00
|
|
|
_networkId(nwid),
|
2016-09-23 23:08:38 +00:00
|
|
|
_threshold(thr),
|
|
|
|
_flags(fl),
|
|
|
|
_target(tgt),
|
|
|
|
_signedBy(),
|
2019-07-17 15:52:08 +00:00
|
|
|
_type(ct),
|
|
|
|
_signatureLength(0)
|
2019-03-25 19:58:43 +00:00
|
|
|
{
|
|
|
|
}
|
2016-09-23 23:08:38 +00:00
|
|
|
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE uint32_t id() const { return _id; }
|
|
|
|
ZT_ALWAYS_INLINE uint32_t credentialId() const { return _credentialId; }
|
|
|
|
ZT_ALWAYS_INLINE uint64_t networkId() const { return _networkId; }
|
|
|
|
ZT_ALWAYS_INLINE int64_t threshold() const { return _threshold; }
|
|
|
|
ZT_ALWAYS_INLINE const Address &target() const { return _target; }
|
|
|
|
ZT_ALWAYS_INLINE const Address &signer() const { return _signedBy; }
|
|
|
|
ZT_ALWAYS_INLINE Credential::Type type() const { return _type; }
|
|
|
|
ZT_ALWAYS_INLINE const uint8_t *signature() const { return _signature; }
|
|
|
|
ZT_ALWAYS_INLINE unsigned int signatureLength() const { return _signatureLength; }
|
|
|
|
ZT_ALWAYS_INLINE bool fastPropagate() const { return ((_flags & ZT_REVOCATION_FLAG_FAST_PROPAGATE) != 0); }
|
2016-09-23 23:08:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param signer Signing identity, must have private key
|
|
|
|
* @return True if signature was successful
|
|
|
|
*/
|
2019-08-14 21:55:37 +00:00
|
|
|
inline bool sign(const Identity &signer)
|
|
|
|
{
|
|
|
|
if (signer.hasPrivate()) {
|
|
|
|
Buffer<sizeof(Revocation) + 64> tmp;
|
|
|
|
_signedBy = signer.address();
|
|
|
|
this->serialize(tmp,true);
|
|
|
|
_signatureLength = signer.sign(tmp.data(),tmp.size(),_signature,sizeof(_signature));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-23 23:08:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify this revocation's signature
|
|
|
|
*
|
|
|
|
* @param RR Runtime environment to provide for peer lookup, etc.
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2016-09-23 23:08:38 +00:00
|
|
|
*/
|
2019-08-14 21:55:37 +00:00
|
|
|
inline Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const { return _verify(RR,tPtr,*this); }
|
2016-09-23 23:08:38 +00:00
|
|
|
|
|
|
|
template<unsigned int C>
|
|
|
|
inline void serialize(Buffer<C> &b,const bool forSign = false) const
|
|
|
|
{
|
|
|
|
if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
|
|
|
|
|
2017-04-04 15:07:38 +00:00
|
|
|
b.append((uint32_t)0); // 4 unused bytes, currently set to 0
|
2016-09-23 23:08:38 +00:00
|
|
|
b.append(_id);
|
|
|
|
b.append(_networkId);
|
2017-04-04 15:07:38 +00:00
|
|
|
b.append((uint32_t)0); // 4 unused bytes, currently set to 0
|
2016-09-23 23:08:38 +00:00
|
|
|
b.append(_credentialId);
|
|
|
|
b.append(_threshold);
|
|
|
|
b.append(_flags);
|
|
|
|
_target.appendTo(b);
|
|
|
|
_signedBy.appendTo(b);
|
|
|
|
b.append((uint8_t)_type);
|
|
|
|
|
|
|
|
if (!forSign) {
|
2019-07-17 15:52:08 +00:00
|
|
|
b.append((uint8_t)1);
|
|
|
|
b.append((uint16_t)_signatureLength);
|
|
|
|
b.append(_signature,_signatureLength);
|
2016-09-23 23:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is the size of any additional fields, currently 0.
|
|
|
|
b.append((uint16_t)0);
|
|
|
|
|
|
|
|
if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned int C>
|
|
|
|
inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
|
|
|
|
{
|
2019-03-25 19:53:31 +00:00
|
|
|
*this = Revocation();
|
2016-09-23 23:08:38 +00:00
|
|
|
|
|
|
|
unsigned int p = startAt;
|
|
|
|
|
2017-04-04 15:07:38 +00:00
|
|
|
p += 4; // 4 bytes, currently unused
|
|
|
|
_id = b.template at<uint32_t>(p); p += 4;
|
2016-09-23 23:08:38 +00:00
|
|
|
_networkId = b.template at<uint64_t>(p); p += 8;
|
2017-04-04 15:07:38 +00:00
|
|
|
p += 4; // 4 bytes, currently unused
|
|
|
|
_credentialId = b.template at<uint32_t>(p); p += 4;
|
2016-09-23 23:08:38 +00:00
|
|
|
_threshold = b.template at<uint64_t>(p); p += 8;
|
|
|
|
_flags = b.template at<uint64_t>(p); p += 8;
|
|
|
|
_target.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
|
|
|
|
_signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
|
2017-04-04 15:07:38 +00:00
|
|
|
_type = (Credential::Type)b[p++];
|
2016-09-23 23:08:38 +00:00
|
|
|
|
|
|
|
if (b[p++] == 1) {
|
2019-07-17 15:52:08 +00:00
|
|
|
_signatureLength = b.template at<uint16_t>(p);
|
|
|
|
if (_signatureLength > sizeof(_signature))
|
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
|
|
|
|
memcpy(_signature,b.field(p,_signatureLength),_signatureLength);
|
2017-02-04 21:17:00 +00:00
|
|
|
} else {
|
|
|
|
p += 2 + b.template at<uint16_t>(p);
|
2016-09-23 23:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p += 2 + b.template at<uint16_t>(p);
|
|
|
|
if (p > b.size())
|
2017-07-17 21:21:09 +00:00
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
|
2016-09-23 23:08:38 +00:00
|
|
|
|
|
|
|
return (p - startAt);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-04-04 15:07:38 +00:00
|
|
|
uint32_t _id;
|
|
|
|
uint32_t _credentialId;
|
2016-09-23 23:08:38 +00:00
|
|
|
uint64_t _networkId;
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t _threshold;
|
2016-09-23 23:08:38 +00:00
|
|
|
uint64_t _flags;
|
|
|
|
Address _target;
|
|
|
|
Address _signedBy;
|
2017-04-04 15:07:38 +00:00
|
|
|
Credential::Type _type;
|
2019-07-17 15:52:08 +00:00
|
|
|
unsigned int _signatureLength;
|
|
|
|
uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
|
2016-09-23 23:08:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|