summaryrefslogtreecommitdiff
path: root/attic/CertificateOfTrust.cpp
blob: e85a91dfa1c9ee71d8470a4bfb3979c98826cd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2016  ZeroTier, Inc.  https://www.zerotier.com/
 *
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "CertificateOfTrust.hpp"

#include "RuntimeEnvironment.hpp"
#include "Topology.hpp"
#include "Switch.hpp"

namespace ZeroTier {

bool CertificateOfTrust::create(uint64_t ts,uint64_t rls,const Identity &iss,const Identity &tgt,Level l)
{
	if ((!iss)||(!iss.hasPrivate()))
		return false;

	_timestamp = ts;
	_roles = rls;
	_issuer = iss.address();
	_target = tgt;
	_level = l;

	Buffer<sizeof(Identity) + 64> tmp;
	tmp.append(_timestamp);
	tmp.append(_roles);
	_issuer.appendTo(tmp);
	_target.serialize(tmp,false);
	tmp.append((uint16_t)_level);
	_signature = iss.sign(tmp.data(),tmp.size());

	return true;
}

int CertificateOfTrust::verify(const RuntimeEnvironment *RR) const
{
	const Identity id(RR->topology->getIdentity(_issuer));
	if (!id) {
		RR->sw->requestWhois(_issuer);
		return 1;
	}

	Buffer<sizeof(Identity) + 64> tmp;
	tmp.append(_timestamp);
	tmp.append(_roles);
	_issuer.appendTo(tmp);
	_target.serialize(tmp,false);
	tmp.append((uint16_t)_level);

	return (id.verify(tmp.data(),tmp.size(),_signature) ? 0 : -1);
}

} // namespace ZeroTier