2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2024-03-19 21:38:48 +00:00
|
|
|
* Change Date: 2026-01-01
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2013-07-04 20:56:19 +00:00
|
|
|
|
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>
|
|
|
|
|
2013-09-16 13:20:59 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Address.hpp"
|
2024-09-15 21:34:01 +00:00
|
|
|
#include "ECC.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
|
|
|
|
2017-07-06 23:11:11 +00:00
|
|
|
#define ZT_IDENTITY_STRING_BUFFER_LENGTH 384
|
|
|
|
|
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() :
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey((ECC::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),
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey((id._privateKey) ? new ECC::Private(*(id._privateKey)) : (ECC::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-06 23:11:11 +00:00
|
|
|
Identity(const char *str) :
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey((ECC::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!fromString(str)) {
|
2017-07-17 21:21:09 +00:00
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
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) :
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey((ECC::Private *)0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
deserialize(b,startAt);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Identity()
|
|
|
|
{
|
2017-07-06 17:25:36 +00:00
|
|
|
if (_privateKey) {
|
2024-09-15 21:34:01 +00:00
|
|
|
Utils::burn(_privateKey,sizeof(ECC::Private));
|
2017-07-06 17:25:36 +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) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!_privateKey) {
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey = new ECC::Private();
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2013-09-16 13:20:59 +00:00
|
|
|
*_privateKey = *(id._privateKey);
|
|
|
|
} else {
|
|
|
|
delete _privateKey;
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey = (ECC::Private *)0;
|
2013-09-16 13:20:59 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2024-09-15 21:34:01 +00:00
|
|
|
inline bool hasPrivate() const { return (_privateKey != (ECC::Private *)0); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2021-09-21 02:05:39 +00:00
|
|
|
/**
|
|
|
|
* Compute a SHA384 hash of this identity's address and public key(s).
|
2024-09-15 20:33:41 +00:00
|
|
|
*
|
2021-09-21 02:05:39 +00:00
|
|
|
* @param sha384buf Buffer with 48 bytes of space to receive hash
|
|
|
|
*/
|
|
|
|
inline void publicKeyHash(void *sha384buf) const
|
|
|
|
{
|
|
|
|
uint8_t address[ZT_ADDRESS_LENGTH];
|
|
|
|
_address.copyTo(address, ZT_ADDRESS_LENGTH);
|
2024-09-15 20:33:41 +00:00
|
|
|
SHA384(sha384buf, address, ZT_ADDRESS_LENGTH, _publicKey.data, ZT_ECC_PUBLIC_KEY_SET_LEN);
|
2021-09-21 02:05:39 +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) {
|
2024-09-15 20:33:41 +00:00
|
|
|
SHA512(sha,_privateKey->data,ZT_ECC_PRIVATE_KEY_SET_LEN);
|
2015-10-14 21:12:12 +00:00
|
|
|
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
|
|
|
|
*/
|
2024-09-15 21:34:01 +00:00
|
|
|
inline ECC::Signature sign(const void *data,unsigned int len) const
|
2013-09-16 17:57:57 +00:00
|
|
|
{
|
2023-05-01 18:48:16 +00:00
|
|
|
if (_privateKey) {
|
2024-09-15 21:34:01 +00:00
|
|
|
return ECC::sign(*_privateKey,_publicKey,data,len);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2017-07-17 21:21:09 +00:00
|
|
|
throw ZT_EXCEPTION_PRIVATE_KEY_REQUIRED;
|
2013-09-16 17:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2024-09-15 20:33:41 +00:00
|
|
|
if (siglen != ZT_ECC_SIGNATURE_LEN) {
|
2013-09-16 17:57:57 +00:00
|
|
|
return false;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2024-09-15 21:34:01 +00:00
|
|
|
return ECC::verify(_publicKey,data,len,signature);
|
2013-09-16 17:57:57 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2024-09-15 21:34:01 +00:00
|
|
|
inline bool verify(const void *data,unsigned int len,const ECC::Signature &signature) const
|
2013-10-04 16:24:21 +00:00
|
|
|
{
|
2024-09-15 21:34:01 +00:00
|
|
|
return ECC::verify(_publicKey,data,len,signature);
|
2013-10-04 16:24:21 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return Was agreement successful?
|
|
|
|
*/
|
2020-09-15 00:44:21 +00:00
|
|
|
inline bool agree(const Identity &id,void *const key) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-09-16 13:20:59 +00:00
|
|
|
if (_privateKey) {
|
2024-09-15 21:34:01 +00:00
|
|
|
ECC::agree(*_privateKey,id._publicKey,key,ZT_SYMMETRIC_KEY_SIZE);
|
2013-09-16 13:20:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return This identity's address
|
|
|
|
*/
|
2017-07-17 21:21:09 +00:00
|
|
|
inline const Address &address() const { return _address; }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2024-09-15 20:33:41 +00:00
|
|
|
b.append(_publicKey.data,ZT_ECC_PUBLIC_KEY_SET_LEN);
|
2013-09-16 13:20:59 +00:00
|
|
|
if ((_privateKey)&&(includePrivate)) {
|
2024-09-15 20:33:41 +00:00
|
|
|
b.append((unsigned char)ZT_ECC_PRIVATE_KEY_SET_LEN);
|
|
|
|
b.append(_privateKey->data,ZT_ECC_PRIVATE_KEY_SET_LEN);
|
2023-05-01 18:48:16 +00:00
|
|
|
} else {
|
|
|
|
b.append((unsigned char)0);
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey = (ECC::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;
|
|
|
|
|
2023-05-01 18:48:16 +00:00
|
|
|
if (b[p++] != 0) {
|
2017-07-17 21:21:09 +00:00
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2024-09-15 20:33:41 +00:00
|
|
|
memcpy(_publicKey.data,b.field(p,ZT_ECC_PUBLIC_KEY_SET_LEN),ZT_ECC_PUBLIC_KEY_SET_LEN);
|
|
|
|
p += ZT_ECC_PUBLIC_KEY_SET_LEN;
|
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) {
|
2024-09-15 20:33:41 +00:00
|
|
|
if (privateKeyLength != ZT_ECC_PRIVATE_KEY_SET_LEN) {
|
2017-07-17 21:21:09 +00:00
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2024-09-15 21:34:01 +00:00
|
|
|
_privateKey = new ECC::Private();
|
2024-09-15 20:33:41 +00:00
|
|
|
memcpy(_privateKey->data,b.field(p,ZT_ECC_PRIVATE_KEY_SET_LEN),ZT_ECC_PRIVATE_KEY_SET_LEN);
|
|
|
|
p += ZT_ECC_PRIVATE_KEY_SET_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)
|
2017-07-06 23:11:11 +00:00
|
|
|
* @param buf Buffer to store string
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return ASCII string representation of identity
|
|
|
|
*/
|
2017-07-06 23:11:11 +00:00
|
|
|
char *toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2016-08-09 20:14:38 +00:00
|
|
|
/**
|
|
|
|
* @return C25519 public key
|
|
|
|
*/
|
2024-09-15 21:34:01 +00:00
|
|
|
inline const ECC::Public &publicKey() const { return _publicKey; }
|
2016-08-09 20:14:38 +00:00
|
|
|
|
2016-08-23 18:57:56 +00:00
|
|
|
/**
|
|
|
|
* @return C25519 key pair (only returns valid pair if private key is present in this Identity object)
|
|
|
|
*/
|
2024-09-15 21:34:01 +00:00
|
|
|
inline const ECC::Pair privateKeyPair() const
|
2016-08-23 18:57:56 +00:00
|
|
|
{
|
2024-09-15 21:34:01 +00:00
|
|
|
ECC::Pair pair;
|
2016-08-23 18:57:56 +00:00
|
|
|
pair.pub = _publicKey;
|
2023-05-01 18:48:16 +00:00
|
|
|
if (_privateKey) {
|
2016-08-23 18:57:56 +00:00
|
|
|
pair.priv = *_privateKey;
|
2023-05-01 18:48:16 +00:00
|
|
|
} else {
|
2024-09-15 20:33:41 +00:00
|
|
|
memset(pair.priv.data,0,ZT_ECC_PRIVATE_KEY_SET_LEN);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-08-23 18:57:56 +00:00
|
|
|
return pair;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return True if this identity contains something
|
|
|
|
*/
|
2017-07-17 21:21:09 +00:00
|
|
|
inline operator bool() const { return (_address); }
|
|
|
|
|
2024-09-15 20:33:41 +00:00
|
|
|
inline bool operator==(const Identity &id) const { return ((_address == id._address)&&(memcmp(_publicKey.data,id._publicKey.data,ZT_ECC_PUBLIC_KEY_SET_LEN) == 0)); }
|
|
|
|
inline bool operator<(const Identity &id) const { return ((_address < id._address)||((_address == id._address)&&(memcmp(_publicKey.data,id._publicKey.data,ZT_ECC_PUBLIC_KEY_SET_LEN) < 0))); }
|
2017-07-17 21:21:09 +00:00
|
|
|
inline bool operator!=(const Identity &id) const { return !(*this == id); }
|
|
|
|
inline bool operator>(const Identity &id) const { return (id < *this); }
|
|
|
|
inline bool operator<=(const Identity &id) const { return !(id < *this); }
|
|
|
|
inline bool operator>=(const Identity &id) const { return !(*this < id); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Address _address;
|
2024-09-15 21:34:01 +00:00
|
|
|
ECC::Public _publicKey;
|
|
|
|
ECC::Private *_privateKey;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|