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
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Change Date: 2023-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"
|
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"
|
2019-07-17 15:52:08 +00:00
|
|
|
#include "ECC384.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-09-11 23:10:06 +00:00
|
|
|
#define ZT_IDENTITY_STRING_BUFFER_LENGTH 1024
|
2017-07-06 23:11:11 +00:00
|
|
|
|
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:
|
2019-08-09 04:23:16 +00:00
|
|
|
/**
|
|
|
|
* Identity type -- numeric values of these enums are protocol constants
|
|
|
|
*/
|
2019-07-17 15:52:08 +00:00
|
|
|
enum Type
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2019-08-20 20:32:23 +00:00
|
|
|
C25519 = ZT_CRYPTO_ALG_C25519, // Type 0 -- Curve25519 and Ed25519 (1.x and 2.x, default)
|
2019-09-09 17:44:31 +00:00
|
|
|
P384 = ZT_CRYPTO_ALG_P384 // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+)
|
2019-07-17 15:52:08 +00:00
|
|
|
};
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE Identity() { memset(reinterpret_cast<void *>(this),0,sizeof(Identity)); }
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE Identity(const char *str)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
if (!fromString(str))
|
2017-07-17 21:21:09 +00:00
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
template<unsigned int C>
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE Identity(const Buffer<C> &b,unsigned int startAt = 0) { deserialize(b,startAt); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE ~Identity() { Utils::burn(reinterpret_cast<void *>(this),sizeof(Identity)); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-08-20 20:32:23 +00:00
|
|
|
/**
|
|
|
|
* Set identity to NIL value (all zero)
|
|
|
|
*/
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE void zero() { memset(reinterpret_cast<void *>(this),0,sizeof(Identity)); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
/**
|
|
|
|
* @return Identity type
|
|
|
|
*/
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE Type type() const { return _type; }
|
2019-07-17 15:52:08 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Generate a new identity (address, key pair)
|
2013-09-16 13:20:59 +00:00
|
|
|
*
|
|
|
|
* This is a time consuming operation.
|
2019-08-20 20:32:23 +00:00
|
|
|
*
|
2019-07-17 15:52:08 +00:00
|
|
|
* @param t Type of identity to generate
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2019-07-17 15:52:08 +00:00
|
|
|
void generate(const Type t);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE bool hasPrivate() const { return _hasPrivate; }
|
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
|
|
|
|
*/
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE bool sha512PrivateKey(void *const sha) const
|
2015-10-14 21:12:12 +00:00
|
|
|
{
|
2019-07-17 15:52:08 +00:00
|
|
|
if (_hasPrivate) {
|
|
|
|
switch(_type) {
|
|
|
|
case C25519:
|
2019-08-20 20:32:23 +00:00
|
|
|
SHA512(sha,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
|
2019-07-17 15:52:08 +00:00
|
|
|
return true;
|
|
|
|
case P384:
|
2019-09-09 17:44:31 +00:00
|
|
|
SHA512(sha,&_priv,sizeof(_priv));
|
2019-08-07 16:20:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-21 16:27:45 +00:00
|
|
|
/**
|
|
|
|
* Compute a 128-bit short hash of this identity's public key
|
|
|
|
*
|
|
|
|
* This is the first 128 bits of a SHA384 hash and is the hash used
|
|
|
|
* in VERB_WILL_RELAY to report reachability.
|
|
|
|
*
|
|
|
|
* @param h 128-bit buffer to receive hash (must be 16 bytes in size)
|
|
|
|
*/
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE void publicKeyHash128(void *const h) const
|
2019-08-21 16:27:45 +00:00
|
|
|
{
|
|
|
|
uint8_t tmp[48];
|
|
|
|
switch(_type) {
|
|
|
|
case C25519:
|
|
|
|
SHA384(tmp,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
|
|
|
|
break;
|
|
|
|
case P384:
|
2019-09-09 17:44:31 +00:00
|
|
|
SHA384(tmp,&_pub,sizeof(_pub));
|
2019-08-21 16:27:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(int i=0;i<16;++i)
|
|
|
|
((uint8_t *)h)[i] = tmp[i];
|
|
|
|
}
|
|
|
|
|
2013-09-16 17:57:57 +00:00
|
|
|
/**
|
|
|
|
* Sign a message with this identity (private key required)
|
|
|
|
*
|
2019-07-17 15:52:08 +00:00
|
|
|
* The signature buffer should be large enough for the largest
|
|
|
|
* signature, which is currently 96 bytes.
|
2019-08-20 20:32:23 +00:00
|
|
|
*
|
2013-09-16 17:57:57 +00:00
|
|
|
* @param data Data to sign
|
|
|
|
* @param len Length of data
|
2019-07-17 15:52:08 +00:00
|
|
|
* @param sig Buffer to receive signature
|
|
|
|
* @param siglen Length of buffer
|
|
|
|
* @return Number of bytes actually written to sig or 0 on error
|
2013-09-16 17:57:57 +00:00
|
|
|
*/
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE unsigned int sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
|
2019-08-14 22:17:06 +00:00
|
|
|
{
|
2019-08-26 19:17:23 +00:00
|
|
|
if (_hasPrivate) {
|
|
|
|
switch(_type) {
|
2019-08-20 20:32:23 +00:00
|
|
|
|
2019-08-26 19:17:23 +00:00
|
|
|
case C25519:
|
|
|
|
if (siglen >= ZT_C25519_SIGNATURE_LEN) {
|
|
|
|
C25519::sign(_priv.c25519,_pub.c25519,data,len,sig);
|
|
|
|
return ZT_C25519_SIGNATURE_LEN;
|
|
|
|
}
|
2019-08-20 20:32:23 +00:00
|
|
|
|
2019-08-26 19:17:23 +00:00
|
|
|
case P384:
|
|
|
|
if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
|
2019-09-09 17:44:31 +00:00
|
|
|
// Signature hash includes the C25519/Ed25519 public key after the message.
|
|
|
|
// This is an added guard against divorcing these two bound keys.
|
2019-08-26 19:17:23 +00:00
|
|
|
uint8_t h[48];
|
|
|
|
SHA384(h,data,len,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
|
|
|
|
ECC384ECDSASign(_priv.p384,h,(uint8_t *)sig);
|
|
|
|
return ZT_ECC384_SIGNATURE_SIZE;
|
|
|
|
}
|
2019-08-21 16:27:45 +00:00
|
|
|
|
2019-08-26 19:17:23 +00:00
|
|
|
}
|
2019-08-14 22:17:06 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-20 20:32:23 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE bool verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
|
2019-08-14 22:17:06 +00:00
|
|
|
{
|
|
|
|
switch(_type) {
|
|
|
|
case C25519:
|
2019-08-20 20:32:23 +00:00
|
|
|
return C25519::verify(_pub.c25519,data,len,sig,siglen);
|
2019-08-14 22:17:06 +00:00
|
|
|
case P384:
|
|
|
|
if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
|
2019-08-26 19:17:23 +00:00
|
|
|
uint8_t h[48];
|
|
|
|
SHA384(h,data,len,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
|
2019-08-20 20:32:23 +00:00
|
|
|
return ECC384ECDSAVerify(_pub.p384,h,(const uint8_t *)sig);
|
2019-08-14 22:17:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-20 20:32:23 +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?
|
|
|
|
*/
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE bool agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const
|
2019-08-14 22:17:06 +00:00
|
|
|
{
|
2019-08-20 20:32:23 +00:00
|
|
|
uint8_t rawkey[128];
|
|
|
|
uint8_t h[64];
|
2019-08-14 22:17:06 +00:00
|
|
|
if (_hasPrivate) {
|
2019-08-20 22:34:30 +00:00
|
|
|
if (_type == C25519) {
|
|
|
|
if ((id._type == C25519)||(id._type == P384)) {
|
|
|
|
// If we are a C25519 key we can agree with another C25519 key or with only the
|
|
|
|
// C25519 portion of a type 1 P-384 key.
|
2019-08-20 20:32:23 +00:00
|
|
|
C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
|
|
|
|
SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
|
2019-08-20 22:34:30 +00:00
|
|
|
memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
|
2019-08-14 22:17:06 +00:00
|
|
|
return true;
|
2019-08-20 22:34:30 +00:00
|
|
|
}
|
|
|
|
} else if (_type == P384) {
|
|
|
|
if (id._type == P384) {
|
|
|
|
// Perform key agreement over both curves for the same reason that C25519 public
|
|
|
|
// keys are included in P-384 signature inputs: to bind the keys together so
|
|
|
|
// that a type 1 identity with the same C25519 public key (and therefore address)
|
|
|
|
// but a different P-384 key will not work.
|
|
|
|
C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
|
|
|
|
ECC384ECDH(id._pub.p384,_priv.p384,rawkey + ZT_C25519_SHARED_KEY_LEN);
|
|
|
|
SHA384(h,rawkey,ZT_C25519_SHARED_KEY_LEN + ZT_ECC384_SHARED_SECRET_SIZE);
|
2019-08-21 16:27:45 +00:00
|
|
|
memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
|
2019-08-20 22:34:30 +00:00
|
|
|
return true;
|
|
|
|
} else if (id._type == C25519) {
|
|
|
|
// If the other identity is a C25519 identity we can agree using only that type.
|
|
|
|
C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
|
|
|
|
SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
|
|
|
|
memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-14 22:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-20 20:32:23 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return This identity's address
|
|
|
|
*/
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE const Address &address() const { return _address; }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-09-08 02:15:21 +00:00
|
|
|
/**
|
|
|
|
* Attempt to generate an older type identity from a newer type
|
|
|
|
*
|
|
|
|
* If this identity has its private key this is not transferred to
|
|
|
|
* the downgraded identity.
|
|
|
|
*
|
|
|
|
* @param dest Destination to fill with downgraded identity
|
|
|
|
* @param toType Desired identity type
|
|
|
|
*/
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE bool downgrade(Identity &dest,const Type toType)
|
2019-09-08 02:15:21 +00:00
|
|
|
{
|
|
|
|
if ((_type == P384)&&(toType == C25519)) {
|
|
|
|
dest._address = _address;
|
|
|
|
dest._type = C25519;
|
|
|
|
dest._hasPrivate = false;
|
|
|
|
memcpy(dest._pub.c25519,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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>
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE void serialize(Buffer<C> &b,bool includePrivate = false) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-07-25 17:24:39 +00:00
|
|
|
_address.appendTo(b);
|
2019-07-17 15:52:08 +00:00
|
|
|
switch(_type) {
|
2019-08-07 22:06:03 +00:00
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
case C25519:
|
|
|
|
b.append((uint8_t)C25519);
|
2019-08-20 20:32:23 +00:00
|
|
|
b.append(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
|
2019-07-17 15:52:08 +00:00
|
|
|
if ((_hasPrivate)&&(includePrivate)) {
|
|
|
|
b.append((uint8_t)ZT_C25519_PRIVATE_KEY_LEN);
|
2019-08-20 20:32:23 +00:00
|
|
|
b.append(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
|
2019-07-17 15:52:08 +00:00
|
|
|
} else {
|
|
|
|
b.append((uint8_t)0);
|
|
|
|
}
|
|
|
|
break;
|
2019-08-07 22:06:03 +00:00
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
case P384:
|
|
|
|
b.append((uint8_t)P384);
|
2019-08-20 20:32:23 +00:00
|
|
|
b.append(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
|
|
|
|
b.append(_pub.p384,ZT_ECC384_PUBLIC_KEY_SIZE);
|
2019-09-09 17:44:31 +00:00
|
|
|
b.append(_pub.p384s,ZT_ECC384_SIGNATURE_SIZE);
|
2019-07-17 15:52:08 +00:00
|
|
|
if ((_hasPrivate)&&(includePrivate)) {
|
2019-08-20 20:32:23 +00:00
|
|
|
b.append((uint8_t)(ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE));
|
|
|
|
b.append(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
|
|
|
|
b.append(_priv.p384,ZT_ECC384_PRIVATE_KEY_SIZE);
|
2019-07-17 15:52:08 +00:00
|
|
|
} else {
|
|
|
|
b.append((uint8_t)0);
|
|
|
|
}
|
|
|
|
break;
|
2019-08-07 22:06:03 +00:00
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
}
|
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>
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2019-07-17 15:52:08 +00:00
|
|
|
_hasPrivate = false;
|
2013-07-04 20:56:19 +00:00
|
|
|
unsigned int p = startAt;
|
2019-07-17 15:52:08 +00:00
|
|
|
unsigned int pkl;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
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;
|
|
|
|
|
2019-08-20 20:32:23 +00:00
|
|
|
switch((_type = (Type)b[p++])) {
|
2019-08-07 22:06:03 +00:00
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
case C25519:
|
2019-08-20 20:32:23 +00:00
|
|
|
memcpy(_pub.c25519,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN);
|
2019-07-17 15:52:08 +00:00
|
|
|
p += ZT_C25519_PUBLIC_KEY_LEN;
|
|
|
|
pkl = (unsigned int)b[p++];
|
|
|
|
if (pkl) {
|
|
|
|
if (pkl != ZT_C25519_PRIVATE_KEY_LEN)
|
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
|
|
|
|
_hasPrivate = true;
|
2019-08-20 20:32:23 +00:00
|
|
|
memcpy(_priv.c25519,b.field(p,ZT_C25519_PRIVATE_KEY_LEN),ZT_C25519_PRIVATE_KEY_LEN);
|
2019-07-17 15:52:08 +00:00
|
|
|
p += ZT_C25519_PRIVATE_KEY_LEN;
|
|
|
|
} else {
|
|
|
|
_hasPrivate = false;
|
|
|
|
}
|
|
|
|
break;
|
2019-08-07 22:06:03 +00:00
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
case P384:
|
2019-08-20 20:32:23 +00:00
|
|
|
memcpy(_pub.c25519,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN);
|
|
|
|
p += ZT_C25519_PUBLIC_KEY_LEN;
|
|
|
|
memcpy(_pub.p384,b.field(p,ZT_ECC384_PUBLIC_KEY_SIZE),ZT_ECC384_PUBLIC_KEY_SIZE);
|
2019-07-17 15:52:08 +00:00
|
|
|
p += ZT_ECC384_PUBLIC_KEY_SIZE;
|
2019-09-09 17:44:31 +00:00
|
|
|
memcpy(_pub.p384s,b.field(p,ZT_ECC384_SIGNATURE_SIZE),ZT_ECC384_SIGNATURE_SIZE);
|
|
|
|
p += ZT_ECC384_SIGNATURE_SIZE;
|
2019-07-17 15:52:08 +00:00
|
|
|
pkl = (unsigned int)b[p++];
|
|
|
|
if (pkl) {
|
2019-08-20 20:32:23 +00:00
|
|
|
if (pkl != (ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE))
|
2019-07-17 15:52:08 +00:00
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
|
|
|
|
_hasPrivate = true;
|
2019-08-20 20:32:23 +00:00
|
|
|
memcpy(_priv.c25519,b.field(p,ZT_C25519_PRIVATE_KEY_LEN),ZT_C25519_PRIVATE_KEY_LEN);
|
|
|
|
p += ZT_C25519_PRIVATE_KEY_LEN;
|
|
|
|
memcpy(_priv.p384,b.field(p,ZT_ECC384_PRIVATE_KEY_SIZE),ZT_ECC384_PRIVATE_KEY_SIZE);
|
2019-07-17 15:52:08 +00:00
|
|
|
p += ZT_ECC384_PRIVATE_KEY_SIZE;
|
|
|
|
} else {
|
|
|
|
_hasPrivate = false;
|
|
|
|
}
|
|
|
|
break;
|
2019-08-07 22:06:03 +00:00
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
default:
|
|
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
|
2019-08-07 22:06:03 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if this identity contains something
|
|
|
|
*/
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE operator bool() const { return (_address); }
|
2017-07-17 21:21:09 +00:00
|
|
|
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE bool operator==(const Identity &id) const
|
2019-07-17 15:52:08 +00:00
|
|
|
{
|
|
|
|
if ((_address == id._address)&&(_type == id._type)) {
|
|
|
|
switch(_type) {
|
|
|
|
case C25519:
|
2019-08-20 20:32:23 +00:00
|
|
|
return (memcmp(_pub.c25519,id._pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) == 0);
|
2019-07-17 15:52:08 +00:00
|
|
|
case P384:
|
2019-09-09 17:44:31 +00:00
|
|
|
return (memcmp(&_pub,&id._pub,sizeof(_pub)) == 0);
|
2019-07-17 15:52:08 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-09 17:44:31 +00:00
|
|
|
ZT_ALWAYS_INLINE bool operator<(const Identity &id) const
|
2019-07-17 15:52:08 +00:00
|
|
|
{
|
|
|
|
if (_address < id._address)
|
|
|
|
return true;
|
|
|
|
if (_address == id._address) {
|
|
|
|
if ((int)_type < (int)id._type)
|
|
|
|
return true;
|
|
|
|
if (_type == id._type) {
|
|
|
|
switch(_type) {
|
|
|
|
case C25519:
|
2019-08-20 20:32:23 +00:00
|
|
|
return (memcmp(_pub.c25519,id._pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) < 0);
|
2019-07-17 15:52:08 +00:00
|
|
|
case P384:
|
2019-09-09 17:44:31 +00:00
|
|
|
return (memcmp(&_pub,&id._pub,sizeof(_pub)) < 0);
|
2019-07-17 15:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-27 03:18:28 +00:00
|
|
|
ZT_ALWAYS_INLINE bool operator!=(const Identity &id) const { return !(*this == id); }
|
|
|
|
ZT_ALWAYS_INLINE bool operator>(const Identity &id) const { return (id < *this); }
|
|
|
|
ZT_ALWAYS_INLINE bool operator<=(const Identity &id) const { return !(id < *this); }
|
|
|
|
ZT_ALWAYS_INLINE bool operator>=(const Identity &id) const { return !(*this < id); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-08-29 15:00:54 +00:00
|
|
|
ZT_ALWAYS_INLINE unsigned long hashCode() const { return ((unsigned long)_address.toInt() + (unsigned long)_pub.c25519[0] + (unsigned long)_pub.c25519[1] + (unsigned long)_pub.c25519[2]); }
|
2019-07-17 15:52:08 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
private:
|
|
|
|
Address _address;
|
2019-07-17 15:52:08 +00:00
|
|
|
Type _type;
|
|
|
|
bool _hasPrivate;
|
2019-08-20 20:32:23 +00:00
|
|
|
ZT_PACKED_STRUCT(struct { // don't re-order these
|
|
|
|
uint8_t c25519[ZT_C25519_PRIVATE_KEY_LEN];
|
|
|
|
uint8_t p384[ZT_ECC384_PRIVATE_KEY_SIZE];
|
|
|
|
}) _priv;
|
|
|
|
ZT_PACKED_STRUCT(struct { // don't re-order these
|
|
|
|
uint8_t c25519[ZT_C25519_PUBLIC_KEY_LEN];
|
|
|
|
uint8_t p384[ZT_ECC384_PUBLIC_KEY_SIZE];
|
2019-09-09 17:44:31 +00:00
|
|
|
uint8_t p384s[ZT_ECC384_SIGNATURE_SIZE]; // signature of type 0 key with p384
|
2019-08-20 20:32:23 +00:00
|
|
|
}) _pub;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|