2016-08-02 20:36:17 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2019-01-14 18:25:53 +00:00
|
|
|
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
|
2016-08-02 20:36:17 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-01-14 18:25:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-28 03:47:25 +00:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
2016-08-02 20:36:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZT_TAG_HPP
|
|
|
|
#define ZT_TAG_HPP
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
2017-04-04 15:07:38 +00:00
|
|
|
#include "Credential.hpp"
|
2016-08-02 20:36:17 +00:00
|
|
|
#include "C25519.hpp"
|
|
|
|
#include "Address.hpp"
|
|
|
|
#include "Identity.hpp"
|
|
|
|
#include "Buffer.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tag that can be associated with members and matched in rules
|
|
|
|
*
|
|
|
|
* Capabilities group rules, while tags group members subject to those
|
|
|
|
* rules. Tag values can be matched in rules, and tags relevant to a
|
|
|
|
* capability are presented along with it.
|
|
|
|
*
|
|
|
|
* E.g. a capability might be "can speak Samba/CIFS within your
|
|
|
|
* department." This cap might have a rule to allow TCP/137 but
|
|
|
|
* only if a given tag ID's value matches between two peers. The
|
|
|
|
* capability is what members can do, while the tag is who they are.
|
|
|
|
* Different departments might have tags with the same ID but different
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
* Unlike capabilities tags are signed only by the issuer and are never
|
2018-06-08 00:25:27 +00:00
|
|
|
* transferable.
|
2016-08-02 20:36:17 +00:00
|
|
|
*/
|
2017-04-04 15:07:38 +00:00
|
|
|
class Tag : public Credential
|
2016-08-02 20:36:17 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-04 15:07:38 +00:00
|
|
|
static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_TAG; }
|
|
|
|
|
2016-08-02 20:36:17 +00:00
|
|
|
Tag()
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(Tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param nwid Network ID
|
2016-08-09 00:33:26 +00:00
|
|
|
* @param ts Timestamp
|
2016-08-02 20:36:17 +00:00
|
|
|
* @param issuedTo Address to which this tag was issued
|
|
|
|
* @param id Tag ID
|
|
|
|
* @param value Tag value
|
|
|
|
*/
|
2017-10-04 19:01:17 +00:00
|
|
|
Tag(const uint64_t nwid,const int64_t ts,const Address &issuedTo,const uint32_t id,const uint32_t value) :
|
2016-08-02 20:36:17 +00:00
|
|
|
_id(id),
|
|
|
|
_value(value),
|
2017-04-04 15:07:38 +00:00
|
|
|
_networkId(nwid),
|
|
|
|
_ts(ts),
|
2016-08-02 20:36:17 +00:00
|
|
|
_issuedTo(issuedTo),
|
|
|
|
_signedBy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t id() const { return _id; }
|
2016-08-05 22:02:01 +00:00
|
|
|
inline const uint32_t &value() const { return _value; }
|
2017-04-04 15:07:38 +00:00
|
|
|
inline uint64_t networkId() const { return _networkId; }
|
2017-10-04 19:01:17 +00:00
|
|
|
inline int64_t timestamp() const { return _ts; }
|
2016-08-02 20:36:17 +00:00
|
|
|
inline const Address &issuedTo() const { return _issuedTo; }
|
|
|
|
inline const Address &signedBy() const { return _signedBy; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sign this tag
|
|
|
|
*
|
|
|
|
* @param signer Signing identity, must have private key
|
|
|
|
* @return True if signature was successful
|
|
|
|
*/
|
|
|
|
inline bool sign(const Identity &signer)
|
|
|
|
{
|
2016-09-23 23:08:38 +00:00
|
|
|
if (signer.hasPrivate()) {
|
|
|
|
Buffer<sizeof(Tag) + 64> tmp;
|
2016-08-02 20:36:17 +00:00
|
|
|
_signedBy = signer.address();
|
|
|
|
this->serialize(tmp,true);
|
|
|
|
_signature = signer.sign(tmp.data(),tmp.size());
|
|
|
|
return true;
|
2016-09-23 23:08:38 +00:00
|
|
|
}
|
2016-08-02 20:36:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check this tag's signature
|
|
|
|
*
|
|
|
|
* @param RR Runtime environment to allow identity lookup for signedBy
|
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-08-04 16:02:35 +00:00
|
|
|
* @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or tag
|
2016-08-02 20:36:17 +00:00
|
|
|
*/
|
2017-03-28 00:03:17 +00:00
|
|
|
int verify(const RuntimeEnvironment *RR,void *tPtr) const;
|
2016-08-02 20:36:17 +00:00
|
|
|
|
|
|
|
template<unsigned int C>
|
|
|
|
inline void serialize(Buffer<C> &b,const bool forSign = false) const
|
|
|
|
{
|
|
|
|
if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
|
|
|
|
|
2016-09-23 23:08:38 +00:00
|
|
|
b.append(_networkId);
|
2016-08-09 00:33:26 +00:00
|
|
|
b.append(_ts);
|
2016-08-02 20:36:17 +00:00
|
|
|
b.append(_id);
|
|
|
|
b.append(_value);
|
2016-08-09 15:32:42 +00:00
|
|
|
|
2016-08-02 20:36:17 +00:00
|
|
|
_issuedTo.appendTo(b);
|
|
|
|
_signedBy.appendTo(b);
|
|
|
|
if (!forSign) {
|
|
|
|
b.append((uint8_t)1); // 1 == Ed25519
|
|
|
|
b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
|
|
|
|
b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
|
2016-08-02 20:36:17 +00:00
|
|
|
b.append((uint16_t)0); // length of additional fields, currently 0
|
|
|
|
|
|
|
|
if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned int C>
|
|
|
|
inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
|
|
|
|
{
|
|
|
|
unsigned int p = startAt;
|
|
|
|
|
2017-02-23 19:47:36 +00:00
|
|
|
memset(this,0,sizeof(Tag));
|
|
|
|
|
2016-09-23 23:08:38 +00:00
|
|
|
_networkId = b.template at<uint64_t>(p); p += 8;
|
2016-08-09 00:33:26 +00:00
|
|
|
_ts = b.template at<uint64_t>(p); p += 8;
|
2016-08-02 20:36:17 +00:00
|
|
|
_id = b.template at<uint32_t>(p); p += 4;
|
2016-08-09 15:32:42 +00:00
|
|
|
|
2016-08-02 20:36:17 +00:00
|
|
|
_value = b.template at<uint32_t>(p); p += 4;
|
2016-08-09 15:32:42 +00:00
|
|
|
|
2016-08-02 20:36:17 +00:00
|
|
|
_issuedTo.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-02-04 21:17:00 +00:00
|
|
|
if (b[p++] == 1) {
|
|
|
|
if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN)
|
2017-07-17 21:21:09 +00:00
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
|
2017-02-04 21:17:00 +00:00
|
|
|
p += 2;
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
|
2017-02-04 21:17:00 +00:00
|
|
|
} else {
|
|
|
|
p += 2 + b.template at<uint16_t>(p);
|
|
|
|
}
|
2016-08-02 20:36:17 +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-08-02 20:36:17 +00:00
|
|
|
|
|
|
|
return (p - startAt);
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:02:35 +00:00
|
|
|
// Provides natural sort order by ID
|
|
|
|
inline bool operator<(const Tag &t) const { return (_id < t._id); }
|
|
|
|
|
|
|
|
inline bool operator==(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) == 0); }
|
|
|
|
inline bool operator!=(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) != 0); }
|
|
|
|
|
2016-09-23 23:08:38 +00:00
|
|
|
// For searching sorted arrays or lists of Tags by ID
|
|
|
|
struct IdComparePredicate
|
|
|
|
{
|
|
|
|
inline bool operator()(const Tag &a,const Tag &b) const { return (a.id() < b.id()); }
|
|
|
|
inline bool operator()(const uint32_t a,const Tag &b) const { return (a < b.id()); }
|
|
|
|
inline bool operator()(const Tag &a,const uint32_t b) const { return (a.id() < b); }
|
|
|
|
inline bool operator()(const Tag *a,const Tag *b) const { return (a->id() < b->id()); }
|
|
|
|
inline bool operator()(const Tag *a,const Tag &b) const { return (a->id() < b.id()); }
|
|
|
|
inline bool operator()(const Tag &a,const Tag *b) const { return (a.id() < b->id()); }
|
|
|
|
inline bool operator()(const uint32_t a,const Tag *b) const { return (a < b->id()); }
|
|
|
|
inline bool operator()(const Tag *a,const uint32_t b) const { return (a->id() < b); }
|
|
|
|
inline bool operator()(const uint32_t a,const uint32_t b) const { return (a < b); }
|
|
|
|
};
|
|
|
|
|
2016-08-02 20:36:17 +00:00
|
|
|
private:
|
|
|
|
uint32_t _id;
|
|
|
|
uint32_t _value;
|
2017-04-04 15:07:38 +00:00
|
|
|
uint64_t _networkId;
|
2017-10-04 19:01:17 +00:00
|
|
|
int64_t _ts;
|
2016-08-02 20:36:17 +00:00
|
|
|
Address _issuedTo;
|
|
|
|
Address _signedBy;
|
|
|
|
C25519::Signature _signature;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|