`pqconly` selected a type-1 identity and then behaved exactly like `hybrid` at runtime: every handshake gate tested only `pqcMode() != CLASSIC`, so nothing distinguished the two modes and no non-capable peer was ever refused. Measured on the two-host lab, a `pqconly` node carried traffic with a fully classical peer at 0% loss. Add `Peer::pqconlyPeerRejected()` - true when this node is exactly `pqconly`, the peer did not advertise the HELLO capability bit, and its role is not PLANET/MOON (all four Earth roots are `pqcCapable:false` and enforcing against them would cut the node off the root topology). Three verbs are gated, and the third is load-bearing. The first implementation gated only `VERB_NETWORK_CONFIG` and `VERB_NETWORK_CREDENTIALS` and still let the classical peer join at 0% loss, because membership is granted by the *serve* direction: a classic peer asks the pqconly node's controller for its config, and the node enforcing the policy was the one handing it over. Gating `VERB_NETWORK_CONFIG_REQUEST` before the controller is consulted closes it. The rejection counter had been incrementing in the unrelated direction throughout, which is what made the false assurance possible. `hybrid` keeps its silent classical fallback - that is its contract - but now logs a downgrade WARNING once per peer, re-armed on recovery, so a downgraded link is no longer invisible. The gate keys on `_hybridCapable`, not `hybridSessionKeyInstalled()`: the encapsulating peer stages its hybrid key until the confirm tag arrives, so gating on the installed key would reject a healthy PQ peer for the entire staging window. Measured 2026-10-08, two-host lab, clean rebuild, selftest exit 0: - `pqconly` vs classical: network now sits at REQUESTING_CONFIGURATION with assignedAddresses [] and 100% loss, down from OK at 0% loss. - `pqconly` vs `pqconly`: 0% loss both directions, no false rejection.
680 lines
22 KiB
C++
680 lines
22 KiB
C++
// PQC capability mode (local.conf settings.pqcMode / Identity::generate)
|
|
#define ZT_PQC_MODE_CLASSIC 0 // off: C25519 key agreement only
|
|
#define ZT_PQC_MODE_HYBRID 1 // hybrid: C25519 agreement with ML-KEM-768 mixed in
|
|
#define ZT_PQC_MODE_PQCONLY 2 // pqconly: same PQ material as hybrid, but enforces the peer requirement (see Peer::pqconlyPeerRejected)
|
|
|
|
/*
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
|
*
|
|
* Use of this software is governed by the Business Source License included
|
|
* in the LICENSE.TXT file in the project's root directory.
|
|
*
|
|
* Change Date: 2026-01-01
|
|
*
|
|
* 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.
|
|
*/
|
|
/****/
|
|
|
|
#ifndef ZT_IDENTITY_HPP
|
|
#define ZT_IDENTITY_HPP
|
|
|
|
#include "Address.hpp"
|
|
#include "Buffer.hpp"
|
|
#include "C25519.hpp"
|
|
#include "Constants.hpp"
|
|
#include "PQHybrid.hpp"
|
|
#include "SHA512.hpp"
|
|
#include "Utils.hpp"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define ZT_IDENTITY_STRING_BUFFER_LENGTH 19456
|
|
|
|
// Identity type byte (binary and string form, field 1)
|
|
#define ZT_IDENTITY_TYPE_C25519 0
|
|
#define ZT_IDENTITY_TYPE_PQ_HYBRID 1
|
|
|
|
// Serialized length of type 1 PQ key material (all four fields always present)
|
|
#define ZT_IDENTITY_PQ_LENGTH (PQHybrid::MLKEM768_PK_LEN + PQHybrid::MLKEM768_SK_LEN + PQHybrid::MLDSA65_PK_LEN + PQHybrid::MLDSA65_SK_LEN)
|
|
|
|
// Maximum binary serialized length of a type 1 identity with private keys
|
|
// (address + type byte + C25519 pub + private length byte + private key +
|
|
// all four PQ keys). Public-only serialization is smaller (PQ public halves).
|
|
#define ZT_IDENTITY_MAX_BINARY_LENGTH (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN + ZT_IDENTITY_PQ_LENGTH)
|
|
|
|
// Maximum binary serialized length of a public-only (no private key) type 1
|
|
// identity: address + type byte + C25519 pub + zero private length byte +
|
|
// ML-KEM-768 and ML-DSA-65 public keys.
|
|
#define ZT_IDENTITY_MAX_PUBLIC_BINARY_LENGTH (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + PQHybrid::MLKEM768_PK_LEN + PQHybrid::MLDSA65_PK_LEN)
|
|
|
|
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:
|
|
/**
|
|
* Post-quantum key material for identity type 1 (ML-KEM-768 + ML-DSA-65).
|
|
* All four fields are always present in a type 1 identity; owning an
|
|
* instance implies a full keypair (pub+priv). Identities without private
|
|
* key material carry only pqPublicKey()/pqSignaturePublicKey().
|
|
*/
|
|
struct PQKeys {
|
|
uint8_t mlkemPk[PQHybrid::MLKEM768_PK_LEN];
|
|
uint8_t mlkemSk[PQHybrid::MLKEM768_SK_LEN];
|
|
uint8_t mldsaPk[PQHybrid::MLDSA65_PK_LEN];
|
|
uint8_t mldsaSk[PQHybrid::MLDSA65_SK_LEN];
|
|
};
|
|
|
|
Identity() : _privateKey((C25519::Private*)0), _pq((PQKeys*)0)
|
|
{
|
|
}
|
|
|
|
Identity(const Identity& id) : _address(id._address), _publicKey(id._publicKey), _privateKey((id._privateKey) ? new C25519::Private(*(id._privateKey)) : (C25519::Private*)0), _pq((id._pq) ? new PQKeys(*(id._pq)) : (PQKeys*)0)
|
|
{
|
|
}
|
|
|
|
Identity(const char* str) : _privateKey((C25519::Private*)0), _pq((PQKeys*)0)
|
|
{
|
|
if (! fromString(str)) {
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
|
|
}
|
|
}
|
|
|
|
template <unsigned int C> Identity(const Buffer<C>& b, unsigned int startAt = 0) : _privateKey((C25519::Private*)0), _pq((PQKeys*)0)
|
|
{ deserialize(b, startAt); }
|
|
|
|
~Identity()
|
|
{
|
|
if (_privateKey) {
|
|
Utils::burn(_privateKey, sizeof(C25519::Private));
|
|
delete _privateKey;
|
|
}
|
|
if (_pq) {
|
|
Utils::burn(_pq, sizeof(PQKeys));
|
|
delete _pq;
|
|
}
|
|
}
|
|
|
|
inline Identity &operator=(const Identity &id)
|
|
{
|
|
_address = id._address;
|
|
_publicKey = id._publicKey;
|
|
if (id._privateKey) {
|
|
if (!_privateKey) {
|
|
_privateKey = new C25519::Private();
|
|
}
|
|
*_privateKey = *(id._privateKey);
|
|
} else {
|
|
delete _privateKey;
|
|
_privateKey = (C25519::Private *)0;
|
|
}
|
|
if (id._pq) {
|
|
if (! _pq) {
|
|
_pq = new PQKeys();
|
|
}
|
|
*_pq = *(id._pq);
|
|
}
|
|
else {
|
|
if (_pq) {
|
|
Utils::burn(_pq, sizeof(PQKeys));
|
|
delete _pq;
|
|
_pq = (PQKeys*)0;
|
|
}
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* Generate a new identity (address, C25519 key pair, and ML-KEM-768 +
|
|
* ML-DSA-65 key pairs when @param pq is true)
|
|
*
|
|
* This is a time consuming operation.
|
|
*
|
|
* @param pq If true, also generate post-quantum key material (identity type 1)
|
|
* @param pqcMode PQC capability mode: 0=classic, 1=hybrid, 2=pqc_only
|
|
*/
|
|
void generate(bool pq = false, int pqcMode = 1);
|
|
|
|
/**
|
|
* Check the validity of this identity's pairing of key to address
|
|
*
|
|
* @return True if validation check passes
|
|
*/
|
|
bool locallyValidate() const;
|
|
|
|
/**
|
|
* @return True if this identity contains a private key
|
|
*/
|
|
inline bool hasPrivate() const { return (_privateKey != (C25519::Private *)0); }
|
|
|
|
/**
|
|
* @return True if this identity is type 1 (carries ML-KEM-768 + ML-DSA-65 public keys)
|
|
*/
|
|
inline bool hasPQ() const
|
|
{ return (_pq != (PQKeys*)0); }
|
|
|
|
/**
|
|
* @return ML-KEM-768 + ML-DSA-65 public keys, or null if not a type 1 identity
|
|
*/
|
|
inline const PQKeys* pqKeys() const
|
|
{ return _pq; }
|
|
|
|
/**
|
|
* Attach public PQ keys learned out of band (VERB_PQ_KEYS) to an otherwise
|
|
* classic identity, turning it into the type 1 identity it really is.
|
|
*
|
|
* HELLO only ever carries the classic projection (see serializeClassic), so
|
|
* a peer's Identity starts out type 0. Anything that verifies a type 2
|
|
* CertificateOfMembership reads the controller's identity back through
|
|
* Topology::getIdentity(), so without this the signature can never be
|
|
* checked. Existing PQ material is replaced.
|
|
*
|
|
* @param mlkemPk ML-KEM-768 public key
|
|
* @param mldsaPk ML-DSA-65 public key
|
|
*/
|
|
inline void attachPQPublicKeys(const uint8_t* mlkemPk, const uint8_t* mldsaPk)
|
|
{
|
|
if (! _pq) {
|
|
_pq = new PQKeys;
|
|
memset(_pq, 0, sizeof(PQKeys));
|
|
}
|
|
memcpy(_pq->mlkemPk, mlkemPk, PQHybrid::MLKEM768_PK_LEN);
|
|
memcpy(_pq->mldsaPk, mldsaPk, PQHybrid::MLDSA65_PK_LEN);
|
|
}
|
|
|
|
/**
|
|
* Build the public half (public keys only) of a type 1 identity's PQ material.
|
|
*
|
|
* @param out Destination receiving public key material
|
|
* @return True if this identity has PQ material
|
|
*/
|
|
inline bool pqPublicKeys(PQKeys& out) const
|
|
{
|
|
if (! _pq) {
|
|
return false;
|
|
}
|
|
memcpy(out.mlkemPk, _pq->mlkemPk, PQHybrid::MLKEM768_PK_LEN);
|
|
memset(out.mlkemSk, 0, PQHybrid::MLKEM768_SK_LEN);
|
|
memcpy(out.mldsaPk, _pq->mldsaPk, PQHybrid::MLDSA65_PK_LEN);
|
|
memset(out.mldsaSk, 0, PQHybrid::MLDSA65_SK_LEN);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param pk Destination for ML-KEM-768 public key bytes (or null)
|
|
* @return True if this identity carries a post-quantum key exchange key
|
|
*/
|
|
inline bool pqKemKey(uint8_t* pk) const
|
|
{
|
|
if (! _pq) {
|
|
return false;
|
|
}
|
|
if (pk) {
|
|
memcpy(pk, _pq->mlkemPk, PQHybrid::MLKEM768_PK_LEN);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param pk Destination for ML-DSA-65 public key bytes (or null)
|
|
* @return True if this identity carries a post-quantum signature key
|
|
*/
|
|
inline bool pqSigKey(uint8_t* pk) const
|
|
{
|
|
if (! _pq) {
|
|
return false;
|
|
}
|
|
if (pk) {
|
|
memcpy(pk, _pq->mldsaPk, PQHybrid::MLDSA65_PK_LEN);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return True if this is a type 1 (PQ hybrid) identity carrying usable
|
|
* ML-DSA-65 private key material (public-only identities return false)
|
|
*/
|
|
inline bool hasPQPrivate() const
|
|
{ return ((_pq != (PQKeys*)0) && (_privateKey != (C25519::Private*)0)); }
|
|
|
|
/**
|
|
* Sign message with this identity's ML-DSA-65 private key (V14 double-sign
|
|
* half). Caller must supply exactly PQHybrid::MLDSA65_SIG_LEN bytes.
|
|
*
|
|
* @param data Data to sign
|
|
* @param len Length of data
|
|
* @param sig Destination for the ML-DSA-65 signature
|
|
* @return True on success
|
|
* @throws ZT_EXCEPTION_PRIVATE_KEY_REQUIRED if no PQ private key present
|
|
*/
|
|
inline bool pqSign(const void* data, unsigned int len, uint8_t* sig) const
|
|
{
|
|
if (! hasPQPrivate()) {
|
|
throw ZT_EXCEPTION_PRIVATE_KEY_REQUIRED;
|
|
}
|
|
return PQHybrid::sign(_pq->mldsaSk, data, len, sig);
|
|
}
|
|
|
|
/**
|
|
* Verify an ML-DSA-65 signature made with this identity's PQ signature key.
|
|
*
|
|
* @param data Data that was signed
|
|
* @param len Length of data
|
|
* @param sig ML-DSA-65 signature (PQHybrid::MLDSA65_SIG_LEN bytes)
|
|
* @return True if signature validates, false if not or no PQ key present
|
|
*/
|
|
inline bool pqVerify(const void* data, unsigned int len, const uint8_t* sig) const
|
|
{
|
|
if (! _pq) {
|
|
return false;
|
|
}
|
|
return PQHybrid::verify(_pq->mldsaPk, data, len, sig);
|
|
}
|
|
|
|
/**
|
|
* Compute a SHA384 hash of this identity's address and public key(s).
|
|
*
|
|
* @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);
|
|
SHA384(sha384buf, address, ZT_ADDRESS_LENGTH, _publicKey.data, ZT_C25519_PUBLIC_KEY_LEN);
|
|
}
|
|
|
|
/**
|
|
* 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(sha,_privateKey->data,ZT_C25519_PRIVATE_KEY_LEN);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
if (_privateKey) {
|
|
return C25519::sign(*_privateKey,_publicKey,data,len);
|
|
}
|
|
throw ZT_EXCEPTION_PRIVATE_KEY_REQUIRED;
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* Shortcut method to perform key agreement with another identity
|
|
*
|
|
* This identity must have a private key. (Check hasPrivate())
|
|
*
|
|
* @param id Identity to agree with
|
|
* @param key Result parameter to fill with key bytes
|
|
* @return Was agreement successful?
|
|
*/
|
|
inline bool agree(const Identity &id,void *const key) const
|
|
{
|
|
if (_privateKey) {
|
|
C25519::agree(*_privateKey,id._publicKey,key,ZT_SYMMETRIC_KEY_SIZE);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Hybrid post-quantum key agreement, encapsulate side (V13):
|
|
* key = HMAC-SHA-384(X25519-identity-agree || ML-KEM-768 encaps to peer static pk).
|
|
* Static-static: no ephemeral exchange.
|
|
*
|
|
* This identity must have a private key. The peer's ML-KEM-768 public key is
|
|
* passed separately because peers advertise PQ material in a separate
|
|
* message (Packet::VERB_PQ_KEYS) rather than inside their HELLO identity.
|
|
*
|
|
* @param id Peer identity (classical part; supplies the C25519 public key)
|
|
* @param peerMlkemPk Peer ML-KEM-768 public key, PQHybrid::MLKEM768_PK_LEN bytes
|
|
* @param ct Result buffer of PQHybrid::MLKEM768_CT_LEN bytes for the ML-KEM-768 ciphertext
|
|
* @param key Result buffer of ZT_SYMMETRIC_KEY_SIZE bytes for the hybrid key
|
|
* @return Was agreement successful?
|
|
*/
|
|
inline bool agreeHybridEncaps(const Identity& id, const uint8_t* const peerMlkemPk, void* const ct, void* const key) const
|
|
{
|
|
if ((! _privateKey) || (! peerMlkemPk)) {
|
|
return false;
|
|
}
|
|
uint8_t classical[ZT_SYMMETRIC_KEY_SIZE];
|
|
uint8_t pq[PQHybrid::MLKEM768_SS_LEN];
|
|
if (! PQHybrid::encaps(peerMlkemPk, reinterpret_cast<uint8_t*>(ct), pq)) {
|
|
return false;
|
|
}
|
|
C25519::agree(*_privateKey, id._publicKey, classical, ZT_SYMMETRIC_KEY_SIZE);
|
|
PQHybrid::hybridKey(classical, pq, reinterpret_cast<uint8_t*>(key));
|
|
Utils::burn(classical, sizeof(classical));
|
|
Utils::burn(pq, sizeof(pq));
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Hybrid post-quantum key agreement, decapsulate side (V13):
|
|
* recompute key from the peer's ML-KEM-768 ciphertext with this identity's secret.
|
|
*
|
|
* This identity must be type 1 with a private key; the peer's identity is
|
|
* the classic (type 0) projection published in its HELLO.
|
|
*
|
|
* @param id Peer identity (classical public key)
|
|
* @param ct ML-KEM-768 ciphertext of PQHybrid::MLKEM768_CT_LEN bytes
|
|
* @param key Result buffer of ZT_SYMMETRIC_KEY_SIZE bytes for the hybrid key
|
|
* @return Was agreement successful?
|
|
*/
|
|
inline bool agreeHybridDecaps(const Identity& id, const void* const ct, void* const key) const
|
|
{
|
|
if ((! _privateKey) || (! _pq)) {
|
|
return false;
|
|
}
|
|
uint8_t classical[ZT_SYMMETRIC_KEY_SIZE];
|
|
uint8_t pq[PQHybrid::MLKEM768_SS_LEN];
|
|
if (! PQHybrid::decaps(_pq->mlkemSk, reinterpret_cast<const uint8_t*>(ct), pq)) {
|
|
return false;
|
|
}
|
|
C25519::agree(*_privateKey, id._publicKey, classical, ZT_SYMMETRIC_KEY_SIZE);
|
|
PQHybrid::hybridKey(classical, pq, reinterpret_cast<uint8_t*>(key));
|
|
Utils::burn(classical, sizeof(classical));
|
|
Utils::burn(pq, sizeof(pq));
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return This identity's address
|
|
*/
|
|
inline const Address &address() const { return _address; }
|
|
|
|
/**
|
|
* Serialize this identity (binary)
|
|
*
|
|
* @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
|
|
{
|
|
_address.appendTo(b);
|
|
b.append((uint8_t)((_pq) ? ZT_IDENTITY_TYPE_PQ_HYBRID : ZT_IDENTITY_TYPE_C25519));
|
|
b.append(_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN);
|
|
if ((_privateKey)&&(includePrivate)) {
|
|
b.append((unsigned char)ZT_C25519_PRIVATE_KEY_LEN);
|
|
b.append(_privateKey->data,ZT_C25519_PRIVATE_KEY_LEN);
|
|
} else {
|
|
b.append((unsigned char)0);
|
|
}
|
|
if (_pq) {
|
|
// Public ML-KEM + ML-DSA keys always go on the wire; the private
|
|
// halves follow only when the C25519 private key is included.
|
|
b.append(_pq->mlkemPk, PQHybrid::MLKEM768_PK_LEN);
|
|
b.append(_pq->mldsaPk, PQHybrid::MLDSA65_PK_LEN);
|
|
if ((_privateKey) && (includePrivate)) {
|
|
b.append(_pq->mlkemSk, PQHybrid::MLKEM768_SK_LEN);
|
|
b.append(_pq->mldsaSk, PQHybrid::MLDSA65_SK_LEN);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Serialize the classic (type 0) projection of this identity
|
|
*
|
|
* Emits only address, type byte 0 and the X25519 public key -- the exact
|
|
* wire form a vanilla ZeroTier peer parses. A type 1 identity's PQ material
|
|
* is deliberately omitted: HELLO and ERROR(IDENTITY_COLLISION) must stay
|
|
* parseable by unmodified peers, and PQ keys travel out of band in
|
|
* Packet::VERB_PQ_KEYS instead.
|
|
*
|
|
* @param b Destination buffer to append to
|
|
* @throws std::out_of_range Buffer too small
|
|
*/
|
|
template<unsigned int C>
|
|
inline void serializeClassic(Buffer<C> &b) const
|
|
{
|
|
_address.appendTo(b);
|
|
b.append((uint8_t)ZT_IDENTITY_TYPE_C25519);
|
|
b.append(_publicKey.data, ZT_C25519_PUBLIC_KEY_LEN);
|
|
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
|
|
* @throws std::out_of_range Serialized data invalid
|
|
* @throws std::invalid_argument Serialized data invalid
|
|
*/
|
|
template<unsigned int C>
|
|
inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
|
|
{
|
|
delete _privateKey;
|
|
_privateKey = (C25519::Private *)0;
|
|
if (_pq) {
|
|
Utils::burn(_pq, sizeof(PQKeys));
|
|
delete _pq;
|
|
_pq = (PQKeys*)0;
|
|
}
|
|
|
|
unsigned int p = startAt;
|
|
|
|
_address.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
|
|
p += ZT_ADDRESS_LENGTH;
|
|
|
|
// V10: a type 0 parser rejects type bytes other than 0 cleanly.
|
|
const unsigned int identityType = (unsigned int)b[p++];
|
|
if ((identityType != ZT_IDENTITY_TYPE_C25519) && (identityType != ZT_IDENTITY_TYPE_PQ_HYBRID)) {
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
|
|
}
|
|
|
|
memcpy(_publicKey.data,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN);
|
|
p += ZT_C25519_PUBLIC_KEY_LEN;
|
|
|
|
unsigned int privateKeyLength = (unsigned int)b[p++];
|
|
if (privateKeyLength) {
|
|
if (privateKeyLength != ZT_C25519_PRIVATE_KEY_LEN) {
|
|
throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
|
|
}
|
|
_privateKey = new C25519::Private();
|
|
memcpy(_privateKey->data,b.field(p,ZT_C25519_PRIVATE_KEY_LEN),ZT_C25519_PRIVATE_KEY_LEN);
|
|
p += ZT_C25519_PRIVATE_KEY_LEN;
|
|
}
|
|
|
|
if (identityType == ZT_IDENTITY_TYPE_PQ_HYBRID) {
|
|
_pq = new PQKeys();
|
|
memcpy(_pq->mlkemPk, b.field(p, PQHybrid::MLKEM768_PK_LEN), PQHybrid::MLKEM768_PK_LEN);
|
|
p += PQHybrid::MLKEM768_PK_LEN;
|
|
memcpy(_pq->mldsaPk, b.field(p, PQHybrid::MLDSA65_PK_LEN), PQHybrid::MLDSA65_PK_LEN);
|
|
p += PQHybrid::MLDSA65_PK_LEN;
|
|
if (_privateKey) {
|
|
memcpy(_pq->mlkemSk, b.field(p, PQHybrid::MLKEM768_SK_LEN), PQHybrid::MLKEM768_SK_LEN);
|
|
p += PQHybrid::MLKEM768_SK_LEN;
|
|
memcpy(_pq->mldsaSk, b.field(p, PQHybrid::MLDSA65_SK_LEN), PQHybrid::MLDSA65_SK_LEN);
|
|
p += PQHybrid::MLDSA65_SK_LEN;
|
|
}
|
|
else {
|
|
memset(_pq->mlkemSk, 0, PQHybrid::MLKEM768_SK_LEN);
|
|
memset(_pq->mldsaSk, 0, PQHybrid::MLDSA65_SK_LEN);
|
|
}
|
|
}
|
|
|
|
return (p - startAt);
|
|
}
|
|
|
|
/**
|
|
* Serialize to a more human-friendly string
|
|
*
|
|
* @param includePrivate If true, include private key (if it exists)
|
|
* @param buf Buffer to store string
|
|
* @return ASCII string representation of identity
|
|
*/
|
|
char *toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) 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.
|
|
*
|
|
* @param str String to deserialize
|
|
* @return True if deserialization appears successful
|
|
*/
|
|
bool fromString(const char *str);
|
|
|
|
/**
|
|
* @return C25519 public key
|
|
*/
|
|
inline const C25519::Public &publicKey() const { return _publicKey; }
|
|
|
|
/**
|
|
* @return C25519 key pair (only returns valid pair if private key is present in this Identity object)
|
|
*/
|
|
inline const C25519::Pair privateKeyPair() const
|
|
{
|
|
C25519::Pair pair;
|
|
pair.pub = _publicKey;
|
|
if (_privateKey) {
|
|
pair.priv = *_privateKey;
|
|
} else {
|
|
memset(pair.priv.data,0,ZT_C25519_PRIVATE_KEY_LEN);
|
|
}
|
|
return pair;
|
|
}
|
|
|
|
/**
|
|
* @return True if this identity contains something
|
|
*/
|
|
inline operator bool() const { return (_address); }
|
|
|
|
// Two identities are equal only if address, C25519 public key, and PQ
|
|
// public keys all match. Address derivation hashes X25519 material only
|
|
// (V10), so the same X25519 key may be re-published with different PQ
|
|
// keys; those are distinct identities and must not compare equal.
|
|
inline bool operator==(const Identity& id) const
|
|
{
|
|
if ((_address != id._address) || (memcmp(_publicKey.data, id._publicKey.data, ZT_C25519_PUBLIC_KEY_LEN) != 0)) {
|
|
return false;
|
|
}
|
|
if ((_pq == (PQKeys*)0) && (id._pq == (PQKeys*)0)) {
|
|
return true;
|
|
}
|
|
if ((_pq == (PQKeys*)0) || (id._pq == (PQKeys*)0)) {
|
|
return false;
|
|
}
|
|
return ((memcmp(_pq->mlkemPk, id._pq->mlkemPk, PQHybrid::MLKEM768_PK_LEN) == 0) && (memcmp(_pq->mldsaPk, id._pq->mldsaPk, PQHybrid::MLDSA65_PK_LEN) == 0));
|
|
}
|
|
|
|
/**
|
|
* @return True if the two identities match on the material a HELLO carries
|
|
*
|
|
* HELLO and ERROR(IDENTITY_COLLISION) carry only the classic projection
|
|
* (see serializeClassic), so a receiver never sees a type 1 identity's
|
|
* PQ keys. The Peer it builds is therefore permanently type 0 while the
|
|
* sender is type 1, and operator== would report a false collision on every
|
|
* subsequent HELLO, dropping the peer before its capability bit is read.
|
|
* Compare only what HELLO actually transports.
|
|
*
|
|
* PQ key rotation stays detectable: VERB_PQ_KEYS compares the keys
|
|
* themselves (see Identity::pqKeysDiffer).
|
|
*/
|
|
inline bool sameClassicalIdentity(const Identity& id) const
|
|
{
|
|
return ((_address == id._address) && (memcmp(_publicKey.data, id._publicKey.data, ZT_C25519_PUBLIC_KEY_LEN) == 0));
|
|
}
|
|
inline bool operator<(const Identity& id) const
|
|
{
|
|
if (_address < id._address) {
|
|
return true;
|
|
}
|
|
if (id._address < _address) {
|
|
return false;
|
|
}
|
|
int c = memcmp(_publicKey.data, id._publicKey.data, ZT_C25519_PUBLIC_KEY_LEN);
|
|
if (c != 0) {
|
|
return (c < 0);
|
|
}
|
|
if (_pq == (PQKeys*)0) {
|
|
return (id._pq != (PQKeys*)0);
|
|
}
|
|
if (id._pq == (PQKeys*)0) {
|
|
return false;
|
|
}
|
|
c = memcmp(_pq->mlkemPk, id._pq->mlkemPk, PQHybrid::MLKEM768_PK_LEN);
|
|
if (c != 0) {
|
|
return (c < 0);
|
|
}
|
|
return (memcmp(_pq->mldsaPk, id._pq->mldsaPk, PQHybrid::MLDSA65_PK_LEN) < 0);
|
|
}
|
|
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); }
|
|
|
|
private:
|
|
Address _address;
|
|
C25519::Public _publicKey;
|
|
C25519::Private *_privateKey;
|
|
PQKeys* _pq;
|
|
};
|
|
|
|
} // namespace ZeroTier
|
|
|
|
#endif
|