2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2016-01-12 22:04:55 +00:00
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-04 20:56:19 +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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_IDENTITY_HPP
|
|
|
|
#define ZT_IDENTITY_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
|
|
|
|
2013-09-16 13:20:59 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Array.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Address.hpp"
|
2013-09-16 13:20:59 +00:00
|
|
|
#include "C25519.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Buffer.hpp"
|
2015-10-14 21:12:12 +00:00
|
|
|
#include "SHA512.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A ZeroTier identity
|
|
|
|
*
|
|
|
|
* An identity consists of a public key, a 40-bit ZeroTier address computed
|
|
|
|
* from that key in a collision-resistant fashion, and a self-signature.
|
|
|
|
*
|
|
|
|
* The address derivation algorithm makes it computationally very expensive to
|
|
|
|
* search for a different public key that duplicates an existing address. (See
|
|
|
|
* code for deriveAddress() for this algorithm.)
|
|
|
|
*/
|
|
|
|
class Identity
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Identity() :
|
2013-09-16 13:20:59 +00:00
|
|
|
_privateKey((C25519::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Identity(const Identity &id) :
|
|
|
|
_address(id._address),
|
2013-09-16 13:20:59 +00:00
|
|
|
_publicKey(id._publicKey),
|
|
|
|
_privateKey((id._privateKey) ? new C25519::Private(*(id._privateKey)) : (C25519::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Identity(const char *str)
|
|
|
|
throw(std::invalid_argument) :
|
2013-09-16 13:20:59 +00:00
|
|
|
_privateKey((C25519::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
if (!fromString(str))
|
2013-08-02 18:25:23 +00:00
|
|
|
throw std::invalid_argument(std::string("invalid string-serialized identity: ") + str);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Identity(const std::string &str)
|
|
|
|
throw(std::invalid_argument) :
|
2013-09-16 13:20:59 +00:00
|
|
|
_privateKey((C25519::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
if (!fromString(str))
|
2013-08-02 18:25:23 +00:00
|
|
|
throw std::invalid_argument(std::string("invalid string-serialized identity: ") + str);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned int C>
|
2015-10-14 21:12:12 +00:00
|
|
|
Identity(const Buffer<C> &b,unsigned int startAt = 0) :
|
2013-09-16 13:20:59 +00:00
|
|
|
_privateKey((C25519::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
deserialize(b,startAt);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Identity()
|
|
|
|
{
|
2013-09-16 13:20:59 +00:00
|
|
|
delete _privateKey;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Identity &operator=(const Identity &id)
|
|
|
|
{
|
|
|
|
_address = id._address;
|
2013-09-16 13:20:59 +00:00
|
|
|
_publicKey = id._publicKey;
|
|
|
|
if (id._privateKey) {
|
|
|
|
if (!_privateKey)
|
|
|
|
_privateKey = new C25519::Private();
|
|
|
|
*_privateKey = *(id._privateKey);
|
|
|
|
} else {
|
|
|
|
delete _privateKey;
|
|
|
|
_privateKey = (C25519::Private *)0;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a new identity (address, key pair)
|
2013-09-16 13:20:59 +00:00
|
|
|
*
|
|
|
|
* This is a time consuming operation.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
void generate();
|
|
|
|
|
|
|
|
/**
|
2013-10-05 11:00:55 +00:00
|
|
|
* Check the validity of this identity's pairing of key to address
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return True if validation check passes
|
|
|
|
*/
|
2013-10-05 11:00:55 +00:00
|
|
|
bool locallyValidate() const;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-16 13:20:59 +00:00
|
|
|
* @return True if this identity contains a private key
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-09-16 13:20:59 +00:00
|
|
|
inline bool hasPrivate() const throw() { return (_privateKey != (C25519::Private *)0); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-10-14 21:12:12 +00:00
|
|
|
/**
|
|
|
|
* Compute the SHA512 hash of our private key (if we have one)
|
|
|
|
*
|
|
|
|
* @param sha Buffer to receive SHA512 (MUST be ZT_SHA512_DIGEST_LEN (64) bytes in length)
|
|
|
|
* @return True on success, false if no private key
|
|
|
|
*/
|
|
|
|
inline bool sha512PrivateKey(void *sha) const
|
|
|
|
{
|
|
|
|
if (_privateKey) {
|
|
|
|
SHA512::hash(sha,_privateKey->data,ZT_C25519_PRIVATE_KEY_LEN);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-16 17:57:57 +00:00
|
|
|
/**
|
|
|
|
* Sign a message with this identity (private key required)
|
|
|
|
*
|
|
|
|
* @param data Data to sign
|
|
|
|
* @param len Length of data
|
|
|
|
*/
|
|
|
|
inline C25519::Signature sign(const void *data,unsigned int len) const
|
|
|
|
throw(std::runtime_error)
|
|
|
|
{
|
|
|
|
if (_privateKey)
|
|
|
|
return C25519::sign(*_privateKey,_publicKey,data,len);
|
|
|
|
throw std::runtime_error("sign() requires a private key");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify a message signature against this identity
|
|
|
|
*
|
|
|
|
* @param data Data to check
|
|
|
|
* @param len Length of data
|
|
|
|
* @param signature Signature bytes
|
|
|
|
* @param siglen Length of signature in bytes
|
|
|
|
* @return True if signature validates and data integrity checks
|
|
|
|
*/
|
|
|
|
inline bool verify(const void *data,unsigned int len,const void *signature,unsigned int siglen) const
|
|
|
|
{
|
|
|
|
if (siglen != ZT_C25519_SIGNATURE_LEN)
|
|
|
|
return false;
|
|
|
|
return C25519::verify(_publicKey,data,len,signature);
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:24:21 +00:00
|
|
|
/**
|
|
|
|
* Verify a message signature against this identity
|
|
|
|
*
|
|
|
|
* @param data Data to check
|
|
|
|
* @param len Length of data
|
|
|
|
* @param signature Signature
|
|
|
|
* @return True if signature validates and data integrity checks
|
|
|
|
*/
|
|
|
|
inline bool verify(const void *data,unsigned int len,const C25519::Signature &signature) const
|
|
|
|
{
|
|
|
|
return C25519::verify(_publicKey,data,len,signature);
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Shortcut method to perform key agreement with another identity
|
|
|
|
*
|
2013-09-16 13:20:59 +00:00
|
|
|
* This identity must have a private key. (Check hasPrivate())
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* @param id Identity to agree with
|
|
|
|
* @param key Result parameter to fill with key bytes
|
|
|
|
* @param klen Length of key in bytes
|
|
|
|
* @return Was agreement successful?
|
|
|
|
*/
|
|
|
|
inline bool agree(const Identity &id,void *key,unsigned int klen) const
|
|
|
|
{
|
2013-09-16 13:20:59 +00:00
|
|
|
if (_privateKey) {
|
|
|
|
C25519::agree(*_privateKey,id._publicKey,key,klen);
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return This identity's address
|
|
|
|
*/
|
|
|
|
inline const Address &address() const throw() { return _address; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize this identity (binary)
|
2016-01-12 22:04:55 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param b Destination buffer to append to
|
|
|
|
* @param includePrivate If true, include private key component (if present) (default: false)
|
|
|
|
* @throws std::out_of_range Buffer too small
|
|
|
|
*/
|
|
|
|
template<unsigned int C>
|
|
|
|
inline void serialize(Buffer<C> &b,bool includePrivate = false) const
|
|
|
|
{
|
2013-07-25 17:24:39 +00:00
|
|
|
_address.appendTo(b);
|
2016-08-02 20:36:17 +00:00
|
|
|
b.append((uint8_t)0); // C25519/Ed25519 identity type
|
2014-01-21 21:07:22 +00:00
|
|
|
b.append(_publicKey.data,(unsigned int)_publicKey.size());
|
2013-09-16 13:20:59 +00:00
|
|
|
if ((_privateKey)&&(includePrivate)) {
|
2013-09-16 17:57:57 +00:00
|
|
|
b.append((unsigned char)_privateKey->size());
|
2014-01-21 21:07:22 +00:00
|
|
|
b.append(_privateKey->data,(unsigned int)_privateKey->size());
|
2013-07-04 20:56:19 +00:00
|
|
|
} else b.append((unsigned char)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize a binary serialized identity
|
|
|
|
*
|
|
|
|
* If an exception is thrown, the Identity object is left in an undefined
|
|
|
|
* state and should not be used.
|
|
|
|
*
|
|
|
|
* @param b Buffer containing serialized data
|
|
|
|
* @param startAt Index within buffer of serialized data (default: 0)
|
|
|
|
* @return Length of serialized data read from buffer
|
2013-10-05 10:00:47 +00:00
|
|
|
* @throws std::out_of_range Serialized data invalid
|
2013-07-04 20:56:19 +00:00
|
|
|
* @throws std::invalid_argument Serialized data invalid
|
|
|
|
*/
|
|
|
|
template<unsigned int C>
|
|
|
|
inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
|
|
|
|
{
|
2013-09-16 13:20:59 +00:00
|
|
|
delete _privateKey;
|
|
|
|
_privateKey = (C25519::Private *)0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
unsigned int p = startAt;
|
|
|
|
|
2013-07-29 20:18:29 +00:00
|
|
|
_address.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
|
2013-07-04 20:56:19 +00:00
|
|
|
p += ZT_ADDRESS_LENGTH;
|
|
|
|
|
2016-08-02 20:36:17 +00:00
|
|
|
if (b[p++] != 0)
|
2013-10-21 14:29:44 +00:00
|
|
|
throw std::invalid_argument("unsupported identity type");
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-01-21 21:07:22 +00:00
|
|
|
memcpy(_publicKey.data,b.field(p,(unsigned int)_publicKey.size()),(unsigned int)_publicKey.size());
|
|
|
|
p += (unsigned int)_publicKey.size();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-10-21 14:29:44 +00:00
|
|
|
unsigned int privateKeyLength = (unsigned int)b[p++];
|
|
|
|
if (privateKeyLength) {
|
|
|
|
if (privateKeyLength != ZT_C25519_PRIVATE_KEY_LEN)
|
|
|
|
throw std::invalid_argument("invalid private key");
|
2013-09-16 13:20:59 +00:00
|
|
|
_privateKey = new C25519::Private();
|
2013-09-16 17:57:57 +00:00
|
|
|
memcpy(_privateKey->data,b.field(p,ZT_C25519_PRIVATE_KEY_LEN),ZT_C25519_PRIVATE_KEY_LEN);
|
2013-09-16 13:20:59 +00:00
|
|
|
p += ZT_C25519_PRIVATE_KEY_LEN;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (p - startAt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize to a more human-friendly string
|
|
|
|
*
|
|
|
|
* @param includePrivate If true, include private key (if it exists)
|
|
|
|
* @return ASCII string representation of identity
|
|
|
|
*/
|
|
|
|
std::string toString(bool includePrivate) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize a human-friendly string
|
|
|
|
*
|
|
|
|
* Note: validation is for the format only. The locallyValidate() method
|
|
|
|
* must be used to check signature and address/key correspondence.
|
2016-01-12 22:04:55 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param str String to deserialize
|
|
|
|
* @return True if deserialization appears successful
|
|
|
|
*/
|
|
|
|
bool fromString(const char *str);
|
|
|
|
inline bool fromString(const std::string &str) { return fromString(str.c_str()); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if this identity contains something
|
|
|
|
*/
|
2013-09-16 13:20:59 +00:00
|
|
|
inline operator bool() const throw() { return (_address); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-09-16 13:20:59 +00:00
|
|
|
inline bool operator==(const Identity &id) const throw() { return ((_address == id._address)&&(_publicKey == id._publicKey)); }
|
|
|
|
inline bool operator<(const Identity &id) const throw() { return ((_address < id._address)||((_address == id._address)&&(_publicKey < id._publicKey))); }
|
2013-07-04 20:56:19 +00:00
|
|
|
inline bool operator!=(const Identity &id) const throw() { return !(*this == id); }
|
|
|
|
inline bool operator>(const Identity &id) const throw() { return (id < *this); }
|
|
|
|
inline bool operator<=(const Identity &id) const throw() { return !(id < *this); }
|
|
|
|
inline bool operator>=(const Identity &id) const throw() { return !(*this < id); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Address _address;
|
2013-09-16 13:20:59 +00:00
|
|
|
C25519::Public _publicKey;
|
|
|
|
C25519::Private *_privateKey;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|