mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-07-06 04:51:46 -07:00
clang-format
This commit is contained in:
parent
d45f280cb7
commit
ba2a4a605c
140 changed files with 19214 additions and 17403 deletions
|
@ -14,21 +14,20 @@
|
|||
#ifndef ZT_CERTIFICATEOFMEMBERSHIP_HPP
|
||||
#define ZT_CERTIFICATEOFMEMBERSHIP_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
|
||||
#include "Address.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "C25519.hpp"
|
||||
#include "Constants.hpp"
|
||||
#include "Credential.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "Address.hpp"
|
||||
#include "C25519.hpp"
|
||||
#include "Identity.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Maximum number of qualifiers allowed in a COM (absolute max: 65535)
|
||||
*/
|
||||
|
@ -64,10 +63,12 @@ class RuntimeEnvironment;
|
|||
* This is a memcpy()'able structure and is safe (in a crash sense) to modify
|
||||
* without locks.
|
||||
*/
|
||||
class CertificateOfMembership : public Credential
|
||||
{
|
||||
public:
|
||||
static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_COM; }
|
||||
class CertificateOfMembership : public Credential {
|
||||
public:
|
||||
static inline Credential::Type credentialType()
|
||||
{
|
||||
return Credential::CREDENTIAL_TYPE_COM;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reserved qualifier IDs
|
||||
|
@ -78,8 +79,7 @@ public:
|
|||
* Addition of new required fields requires that code in hasRequiredFields
|
||||
* be updated as well.
|
||||
*/
|
||||
enum ReservedId
|
||||
{
|
||||
enum ReservedId {
|
||||
/**
|
||||
* Timestamp of certificate
|
||||
*/
|
||||
|
@ -101,8 +101,9 @@ public:
|
|||
/**
|
||||
* Create an empty certificate of membership
|
||||
*/
|
||||
CertificateOfMembership() :
|
||||
_qualifierCount(0) {}
|
||||
CertificateOfMembership() : _qualifierCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create from required fields common to all networks
|
||||
|
@ -112,7 +113,7 @@ public:
|
|||
* @param nwid Network ID
|
||||
* @param issuedTo Certificate recipient
|
||||
*/
|
||||
CertificateOfMembership(uint64_t timestamp,uint64_t timestampMaxDelta,uint64_t nwid,const Identity &issuedTo);
|
||||
CertificateOfMembership(uint64_t timestamp, uint64_t timestampMaxDelta, uint64_t nwid, const Identity& issuedTo);
|
||||
|
||||
/**
|
||||
* Create from binary-serialized COM in buffer
|
||||
|
@ -120,28 +121,33 @@ public:
|
|||
* @param b Buffer to deserialize from
|
||||
* @param startAt Position to start in buffer
|
||||
*/
|
||||
template<unsigned int C>
|
||||
CertificateOfMembership(const Buffer<C> &b,unsigned int startAt = 0)
|
||||
template <unsigned int C> CertificateOfMembership(const Buffer<C>& b, unsigned int startAt = 0)
|
||||
{
|
||||
deserialize(b,startAt);
|
||||
deserialize(b, startAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if there's something here
|
||||
*/
|
||||
inline operator bool() const { return (_qualifierCount != 0); }
|
||||
inline operator bool() const
|
||||
{
|
||||
return (_qualifierCount != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Credential ID, always 0 for COMs
|
||||
*/
|
||||
inline uint32_t id() const { return 0; }
|
||||
inline uint32_t id() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Timestamp for this cert and maximum delta for timestamp
|
||||
*/
|
||||
inline int64_t timestamp() const
|
||||
{
|
||||
for(unsigned int i=0;i<_qualifierCount;++i) {
|
||||
for (unsigned int i = 0; i < _qualifierCount; ++i) {
|
||||
if (_qualifiers[i].id == COM_RESERVED_ID_TIMESTAMP) {
|
||||
return _qualifiers[i].value;
|
||||
}
|
||||
|
@ -154,7 +160,7 @@ public:
|
|||
*/
|
||||
inline Address issuedTo() const
|
||||
{
|
||||
for(unsigned int i=0;i<_qualifierCount;++i) {
|
||||
for (unsigned int i = 0; i < _qualifierCount; ++i) {
|
||||
if (_qualifiers[i].id == COM_RESERVED_ID_ISSUED_TO) {
|
||||
return Address(_qualifiers[i].value);
|
||||
}
|
||||
|
@ -167,7 +173,7 @@ public:
|
|||
*/
|
||||
inline uint64_t networkId() const
|
||||
{
|
||||
for(unsigned int i=0;i<_qualifierCount;++i) {
|
||||
for (unsigned int i = 0; i < _qualifierCount; ++i) {
|
||||
if (_qualifiers[i].id == COM_RESERVED_ID_NETWORK_ID) {
|
||||
return _qualifiers[i].value;
|
||||
}
|
||||
|
@ -189,7 +195,7 @@ public:
|
|||
* @param otherIdentity Identity of other node
|
||||
* @return True if certs agree and 'other' may be communicated with
|
||||
*/
|
||||
bool agreesWith(const CertificateOfMembership &other, const Identity &otherIdentity) const;
|
||||
bool agreesWith(const CertificateOfMembership& other, const Identity& otherIdentity) const;
|
||||
|
||||
/**
|
||||
* Sign this certificate
|
||||
|
@ -197,7 +203,7 @@ public:
|
|||
* @param with Identity to sign with, must include private key
|
||||
* @return True if signature was successful
|
||||
*/
|
||||
bool sign(const Identity &with);
|
||||
bool sign(const Identity& with);
|
||||
|
||||
/**
|
||||
* Verify this COM and its signature
|
||||
|
@ -206,36 +212,40 @@ public:
|
|||
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
||||
* @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
|
||||
*/
|
||||
int verify(const RuntimeEnvironment *RR,void *tPtr) const;
|
||||
int verify(const RuntimeEnvironment* RR, void* tPtr) const;
|
||||
|
||||
/**
|
||||
* @return True if signed
|
||||
*/
|
||||
inline bool isSigned() const { return (_signedBy); }
|
||||
inline bool isSigned() const
|
||||
{
|
||||
return (_signedBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Address that signed this certificate or null address if none
|
||||
*/
|
||||
inline const Address &signedBy() const { return _signedBy; }
|
||||
inline const Address& signedBy() const
|
||||
{
|
||||
return _signedBy;
|
||||
}
|
||||
|
||||
template<unsigned int C>
|
||||
inline void serialize(Buffer<C> &b) const
|
||||
template <unsigned int C> inline void serialize(Buffer<C>& b) const
|
||||
{
|
||||
b.append((uint8_t)1);
|
||||
b.append((uint16_t)_qualifierCount);
|
||||
for(unsigned int i=0;i<_qualifierCount;++i) {
|
||||
for (unsigned int i = 0; i < _qualifierCount; ++i) {
|
||||
b.append(_qualifiers[i].id);
|
||||
b.append(_qualifiers[i].value);
|
||||
b.append(_qualifiers[i].maxDelta);
|
||||
}
|
||||
_signedBy.appendTo(b);
|
||||
if (_signedBy) {
|
||||
b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
|
||||
b.append(_signature.data, ZT_C25519_SIGNATURE_LEN);
|
||||
}
|
||||
}
|
||||
|
||||
template<unsigned int C>
|
||||
inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
|
||||
template <unsigned int C> inline unsigned int deserialize(const Buffer<C>& b, unsigned int startAt = 0)
|
||||
{
|
||||
unsigned int p = startAt;
|
||||
|
||||
|
@ -249,11 +259,12 @@ public:
|
|||
unsigned int numq = b.template at<uint16_t>(p);
|
||||
p += sizeof(uint16_t);
|
||||
uint64_t lastId = 0;
|
||||
for(unsigned int i=0;i<numq;++i) {
|
||||
for (unsigned int i = 0; i < numq; ++i) {
|
||||
const uint64_t qid = b.template at<uint64_t>(p);
|
||||
if (qid < lastId) {
|
||||
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_BAD_ENCODING;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
lastId = qid;
|
||||
}
|
||||
if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
|
||||
|
@ -262,23 +273,24 @@ public:
|
|||
_qualifiers[_qualifierCount].maxDelta = b.template at<uint64_t>(p + 16);
|
||||
p += 24;
|
||||
++_qualifierCount;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
|
||||
}
|
||||
}
|
||||
|
||||
_signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
|
||||
_signedBy.setTo(b.field(p, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
|
||||
p += ZT_ADDRESS_LENGTH;
|
||||
|
||||
if (_signedBy) {
|
||||
memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
|
||||
memcpy(_signature.data, b.field(p, ZT_C25519_SIGNATURE_LEN), ZT_C25519_SIGNATURE_LEN);
|
||||
p += ZT_C25519_SIGNATURE_LEN;
|
||||
}
|
||||
|
||||
return (p - startAt);
|
||||
}
|
||||
|
||||
inline bool operator==(const CertificateOfMembership &c) const
|
||||
inline bool operator==(const CertificateOfMembership& c) const
|
||||
{
|
||||
if (_signedBy != c._signedBy) {
|
||||
return false;
|
||||
|
@ -286,25 +298,32 @@ public:
|
|||
if (_qualifierCount != c._qualifierCount) {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i=0;i<_qualifierCount;++i) {
|
||||
const _Qualifier &a = _qualifiers[i];
|
||||
const _Qualifier &b = c._qualifiers[i];
|
||||
if ((a.id != b.id)||(a.value != b.value)||(a.maxDelta != b.maxDelta)) {
|
||||
for (unsigned int i = 0; i < _qualifierCount; ++i) {
|
||||
const _Qualifier& a = _qualifiers[i];
|
||||
const _Qualifier& b = c._qualifiers[i];
|
||||
if ((a.id != b.id) || (a.value != b.value) || (a.maxDelta != b.maxDelta)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (memcmp(_signature.data,c._signature.data,ZT_C25519_SIGNATURE_LEN) == 0);
|
||||
return (memcmp(_signature.data, c._signature.data, ZT_C25519_SIGNATURE_LEN) == 0);
|
||||
}
|
||||
inline bool operator!=(const CertificateOfMembership &c) const { return (!(*this == c)); }
|
||||
|
||||
private:
|
||||
struct _Qualifier
|
||||
inline bool operator!=(const CertificateOfMembership& c) const
|
||||
{
|
||||
_Qualifier() : id(0),value(0),maxDelta(0) {}
|
||||
return (! (*this == c));
|
||||
}
|
||||
|
||||
private:
|
||||
struct _Qualifier {
|
||||
_Qualifier() : id(0), value(0), maxDelta(0)
|
||||
{
|
||||
}
|
||||
uint64_t id;
|
||||
uint64_t value;
|
||||
uint64_t maxDelta;
|
||||
inline bool operator<(const _Qualifier &q) const { return (id < q.id); } // sort order
|
||||
inline bool operator<(const _Qualifier& q) const
|
||||
{
|
||||
return (id < q.id);
|
||||
} // sort order
|
||||
};
|
||||
|
||||
Address _signedBy;
|
||||
|
@ -313,6 +332,6 @@ private:
|
|||
C25519::Signature _signature;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue