2013-10-07 19:29:03 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +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/
|
2013-10-07 19:29:03 +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.
|
2013-10-07 19:29:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CertificateOfMembership.hpp"
|
2016-08-04 16:02:35 +00:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "Topology.hpp"
|
|
|
|
#include "Switch.hpp"
|
2016-08-04 17:39:28 +00:00
|
|
|
#include "Network.hpp"
|
2017-08-23 23:42:17 +00:00
|
|
|
#include "Node.hpp"
|
2013-10-07 19:29:03 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
void CertificateOfMembership::setQualifier(uint64_t id,uint64_t value,uint64_t maxDelta)
|
|
|
|
{
|
|
|
|
_signedBy.zero();
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
for(unsigned int i=0;i<_qualifierCount;++i) {
|
|
|
|
if (_qualifiers[i].id == id) {
|
|
|
|
_qualifiers[i].value = value;
|
|
|
|
_qualifiers[i].maxDelta = maxDelta;
|
2013-10-07 19:29:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
|
|
|
|
_qualifiers[_qualifierCount].id = id;
|
|
|
|
_qualifiers[_qualifierCount].value = value;
|
|
|
|
_qualifiers[_qualifierCount].maxDelta = maxDelta;
|
|
|
|
++_qualifierCount;
|
|
|
|
std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
|
|
|
|
}
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
|
|
|
|
|
2013-10-07 19:29:03 +00:00
|
|
|
std::string CertificateOfMembership::toString() const
|
|
|
|
{
|
2017-07-06 23:11:11 +00:00
|
|
|
char tmp[ZT_NETWORK_COM_MAX_QUALIFIERS * 32];
|
2013-10-07 19:29:03 +00:00
|
|
|
std::string s;
|
|
|
|
|
2013-10-07 20:13:52 +00:00
|
|
|
s.append("1:"); // COM_UINT64_ED25519
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
uint64_t *const buf = new uint64_t[_qualifierCount * 3];
|
2013-10-07 19:29:03 +00:00
|
|
|
try {
|
|
|
|
unsigned int ptr = 0;
|
2016-04-12 19:11:34 +00:00
|
|
|
for(unsigned int i=0;i<_qualifierCount;++i) {
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].id);
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].value);
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
2017-07-06 23:11:11 +00:00
|
|
|
s.append(Utils::hex(buf,ptr * sizeof(uint64_t),tmp));
|
2013-10-07 19:29:03 +00:00
|
|
|
delete [] buf;
|
|
|
|
} catch ( ... ) {
|
|
|
|
delete [] buf;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.push_back(':');
|
|
|
|
|
2017-07-06 23:11:11 +00:00
|
|
|
s.append(_signedBy.toString(tmp));
|
2013-10-07 19:29:03 +00:00
|
|
|
|
|
|
|
if (_signedBy) {
|
|
|
|
s.push_back(':');
|
2018-01-27 01:00:37 +00:00
|
|
|
s.append(Utils::hex(_signature.data,ZT_C25519_SIGNATURE_LEN,tmp));
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CertificateOfMembership::fromString(const char *s)
|
|
|
|
{
|
2016-04-12 19:11:34 +00:00
|
|
|
_qualifierCount = 0;
|
2013-10-07 19:29:03 +00:00
|
|
|
_signedBy.zero();
|
2018-01-27 01:00:37 +00:00
|
|
|
memset(_signature.data,0,ZT_C25519_SIGNATURE_LEN);
|
2013-10-07 19:29:03 +00:00
|
|
|
|
2013-10-24 20:57:26 +00:00
|
|
|
if (!*s)
|
|
|
|
return;
|
|
|
|
|
2013-10-07 19:29:03 +00:00
|
|
|
unsigned int colonAt = 0;
|
|
|
|
while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
|
|
|
|
|
2013-10-07 20:13:52 +00:00
|
|
|
if (!((colonAt == 1)&&(s[0] == '1'))) // COM_UINT64_ED25519?
|
|
|
|
return;
|
|
|
|
|
|
|
|
s += colonAt + 1;
|
|
|
|
colonAt = 0;
|
|
|
|
while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
|
|
|
|
|
2013-10-07 19:29:03 +00:00
|
|
|
if (colonAt) {
|
2016-04-12 19:11:34 +00:00
|
|
|
const unsigned int buflen = colonAt / 2;
|
|
|
|
char *const buf = new char[buflen];
|
2013-10-07 19:29:03 +00:00
|
|
|
unsigned int bufactual = Utils::unhex(s,colonAt,buf,buflen);
|
|
|
|
char *bufptr = buf;
|
|
|
|
try {
|
|
|
|
while (bufactual >= 24) {
|
2016-04-12 19:11:34 +00:00
|
|
|
if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
|
|
|
|
_qualifiers[_qualifierCount].id = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
|
|
|
|
_qualifiers[_qualifierCount].value = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
|
|
|
|
_qualifiers[_qualifierCount].maxDelta = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
|
|
|
|
++_qualifierCount;
|
|
|
|
} else {
|
|
|
|
bufptr += 24;
|
|
|
|
}
|
2013-10-07 19:29:03 +00:00
|
|
|
bufactual -= 24;
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
delete [] buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s[colonAt]) {
|
|
|
|
s += colonAt + 1;
|
|
|
|
colonAt = 0;
|
|
|
|
while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
|
|
|
|
|
|
|
|
if (colonAt) {
|
|
|
|
char addrbuf[ZT_ADDRESS_LENGTH];
|
|
|
|
if (Utils::unhex(s,colonAt,addrbuf,sizeof(addrbuf)) == ZT_ADDRESS_LENGTH)
|
|
|
|
_signedBy.setTo(addrbuf,ZT_ADDRESS_LENGTH);
|
|
|
|
|
|
|
|
if ((_signedBy)&&(s[colonAt])) {
|
|
|
|
s += colonAt + 1;
|
|
|
|
colonAt = 0;
|
|
|
|
while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
|
|
|
|
if (colonAt) {
|
2018-01-27 01:00:37 +00:00
|
|
|
if (Utils::unhex(s,colonAt,_signature.data,ZT_C25519_SIGNATURE_LEN) != ZT_C25519_SIGNATURE_LEN)
|
2013-10-07 19:29:03 +00:00
|
|
|
_signedBy.zero();
|
2016-04-12 19:11:34 +00:00
|
|
|
} else {
|
|
|
|
_signedBy.zero();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_signedBy.zero();
|
|
|
|
}
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
#endif // ZT_SUPPORT_OLD_STYLE_NETCONF
|
|
|
|
|
2013-10-07 19:29:03 +00:00
|
|
|
bool CertificateOfMembership::agreesWith(const CertificateOfMembership &other) const
|
|
|
|
{
|
2016-04-12 19:11:34 +00:00
|
|
|
unsigned int myidx = 0;
|
|
|
|
unsigned int otheridx = 0;
|
2013-10-07 19:29:03 +00:00
|
|
|
|
2016-08-04 17:18:33 +00:00
|
|
|
if ((_qualifierCount == 0)||(other._qualifierCount == 0))
|
|
|
|
return false;
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
while (myidx < _qualifierCount) {
|
2013-10-07 19:29:03 +00:00
|
|
|
// Fail if we're at the end of other, since this means the field is
|
|
|
|
// missing.
|
2016-04-12 19:11:34 +00:00
|
|
|
if (otheridx >= other._qualifierCount)
|
2013-10-07 19:29:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Seek to corresponding tuple in other, ignoring tuples that
|
|
|
|
// we may not have. If we run off the end of other, the tuple is
|
|
|
|
// missing. This works because tuples are sorted by ID.
|
|
|
|
while (other._qualifiers[otheridx].id != _qualifiers[myidx].id) {
|
|
|
|
++otheridx;
|
2016-04-12 19:11:34 +00:00
|
|
|
if (otheridx >= other._qualifierCount)
|
2013-10-07 19:29:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare to determine if the absolute value of the difference
|
|
|
|
// between these two parameters is within our maxDelta.
|
2013-10-16 21:47:26 +00:00
|
|
|
const uint64_t a = _qualifiers[myidx].value;
|
|
|
|
const uint64_t b = other._qualifiers[myidx].value;
|
|
|
|
if (((a >= b) ? (a - b) : (b - a)) > _qualifiers[myidx].maxDelta)
|
|
|
|
return false;
|
2013-10-07 19:29:03 +00:00
|
|
|
|
|
|
|
++myidx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateOfMembership::sign(const Identity &with)
|
|
|
|
{
|
2016-08-04 16:02:35 +00:00
|
|
|
uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
|
2013-10-07 19:29:03 +00:00
|
|
|
unsigned int ptr = 0;
|
2016-04-12 19:11:34 +00:00
|
|
|
for(unsigned int i=0;i<_qualifierCount;++i) {
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].id);
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].value);
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_signature = with.sign(buf,ptr * sizeof(uint64_t));
|
|
|
|
_signedBy = with.address();
|
|
|
|
return true;
|
|
|
|
} catch ( ... ) {
|
|
|
|
_signedBy.zero();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
int CertificateOfMembership::verify(const RuntimeEnvironment *RR,void *tPtr) const
|
2013-10-07 19:29:03 +00:00
|
|
|
{
|
2016-08-04 17:39:28 +00:00
|
|
|
if ((!_signedBy)||(_signedBy != Network::controllerFor(networkId()))||(_qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
|
2016-08-04 16:02:35 +00:00
|
|
|
return -1;
|
2013-10-07 19:29:03 +00:00
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
const Identity id(RR->topology->getIdentity(tPtr,_signedBy));
|
2016-08-04 16:02:35 +00:00
|
|
|
if (!id) {
|
2017-08-23 23:42:17 +00:00
|
|
|
RR->sw->requestWhois(tPtr,RR->node->now(),_signedBy);
|
2016-08-04 16:02:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
|
2013-10-07 19:29:03 +00:00
|
|
|
unsigned int ptr = 0;
|
2016-04-12 19:11:34 +00:00
|
|
|
for(unsigned int i=0;i<_qualifierCount;++i) {
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].id);
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].value);
|
|
|
|
buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
2016-08-04 16:02:35 +00:00
|
|
|
return (id.verify(buf,ptr * sizeof(uint64_t),_signature) ? 0 : -1);
|
2013-10-07 19:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|