Identity.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_IDENTITY_HPP
  14. #define ZT_IDENTITY_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "Address.hpp"
  18. #include "C25519.hpp"
  19. #include "SHA512.hpp"
  20. #include "ECC384.hpp"
  21. #include "TriviallyCopyable.hpp"
  22. #include "Fingerprint.hpp"
  23. #include <cstdio>
  24. #include <cstdlib>
  25. #include <cstring>
  26. #define ZT_IDENTITY_STRING_BUFFER_LENGTH 1024
  27. #define ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE (1 + ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE)
  28. #define ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE (ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE)
  29. #define ZT_IDENTITY_MARSHAL_SIZE_MAX (ZT_ADDRESS_LENGTH + 4 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE)
  30. namespace ZeroTier {
  31. /**
  32. * A ZeroTier identity
  33. *
  34. * Identities currently come in two types: type 0 identities based on just Curve25519
  35. * and Ed25519 and type 1 identities that include both a 25519 key pair and a NIST P-384
  36. * key pair. Type 1 identities use P-384 for signatures but use both key pairs at once
  37. * (hashing both keys together) for key agreement with other type 1 identities, and can
  38. * agree with type 0 identities using only Curve25519.
  39. *
  40. * Type 1 identities are better in many ways but type 0 will remain the default until
  41. * 1.x nodes are pretty much dead in the wild.
  42. */
  43. class Identity : public TriviallyCopyable
  44. {
  45. public:
  46. /**
  47. * Identity type -- numeric values of these enums are protocol constants
  48. */
  49. enum Type
  50. {
  51. C25519 = ZT_CRYPTO_ALG_C25519, // Type 0 -- Curve25519 and Ed25519 (1.x and 2.x, default)
  52. P384 = ZT_CRYPTO_ALG_P384 // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+)
  53. };
  54. /**
  55. * A nil/empty identity instance
  56. */
  57. static const Identity NIL;
  58. ZT_INLINE Identity() noexcept { memoryZero(this); }
  59. ZT_INLINE ~Identity() { Utils::burn(reinterpret_cast<void *>(&this->_priv),sizeof(this->_priv)); }
  60. /**
  61. * Construct identity from string
  62. *
  63. * If the identity is not basically valid (no deep checking is done) the result will
  64. * be a null identity.
  65. *
  66. * @param str Identity in canonical string format
  67. */
  68. explicit ZT_INLINE Identity(const char *str) { fromString(str); }
  69. /**
  70. * Set identity to NIL value (all zero)
  71. */
  72. ZT_INLINE void zero() noexcept { memoryZero(this); }
  73. /**
  74. * @return Identity type (undefined if identity is null or invalid)
  75. */
  76. ZT_INLINE Type type() const noexcept { return _type; }
  77. /**
  78. * Generate a new identity (address, key pair)
  79. *
  80. * This is a time consuming operation taking up to 5-10 seconds on some slower systems.
  81. *
  82. * @param t Type of identity to generate
  83. * @return False if there was an error such as type being an invalid value
  84. */
  85. bool generate(Type t);
  86. /**
  87. * Check the validity of this identity's address
  88. *
  89. * For type 0 identities this is slightly time consuming. For type 1 identities it's
  90. * instantaneous. It should be done when a new identity is accepted for the very first
  91. * time.
  92. *
  93. * @return True if validation check passes
  94. */
  95. bool locallyValidate() const noexcept;
  96. /**
  97. * @return True if this identity contains a private key
  98. */
  99. ZT_INLINE bool hasPrivate() const noexcept { return _hasPrivate; }
  100. /**
  101. * Get a 384-bit hash of this identity's public key(s)
  102. *
  103. * The hash returned by this function differs by identity type. For C25519 (type 0)
  104. * identities this returns a simple SHA384 of the public key, which is NOT the same
  105. * as the hash used to generate the address. For type 1 C25519+P384 identities this
  106. * returns the same compoound SHA384 hash that is used for purposes of hashcash
  107. * and address computation. This difference is because the v0 hash is expensive while
  108. * the v1 hash is fast.
  109. *
  110. * @return Hash of public key(s)
  111. */
  112. ZT_INLINE const Fingerprint &fingerprint() const noexcept { return _fp; }
  113. /**
  114. * Compute a hash of this identity's public and private keys.
  115. *
  116. * If there is no private key or the identity is NIL the buffer is filled with zero.
  117. *
  118. * @param h Buffer to store SHA384 hash
  119. */
  120. void hashWithPrivate(uint8_t h[ZT_IDENTITY_HASH_SIZE]) const;
  121. /**
  122. * Sign a message with this identity (private key required)
  123. *
  124. * The signature buffer should be large enough for the largest
  125. * signature, which is currently 96 bytes.
  126. *
  127. * @param data Data to sign
  128. * @param len Length of data
  129. * @param sig Buffer to receive signature
  130. * @param siglen Length of buffer
  131. * @return Number of bytes actually written to sig or 0 on error
  132. */
  133. unsigned int sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const;
  134. /**
  135. * Verify a message signature against this identity
  136. *
  137. * @param data Data to check
  138. * @param len Length of data
  139. * @param signature Signature bytes
  140. * @param siglen Length of signature in bytes
  141. * @return True if signature validates and data integrity checks
  142. */
  143. bool verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const;
  144. /**
  145. * Shortcut method to perform key agreement with another identity
  146. *
  147. * This identity must have a private key. (Check hasPrivate())
  148. *
  149. * @param id Identity to agree with
  150. * @param key Result parameter to fill with key bytes
  151. * @return Was agreement successful?
  152. */
  153. bool agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const;
  154. /**
  155. * @return This identity's address
  156. */
  157. ZT_INLINE Address address() const noexcept { return _address; }
  158. /**
  159. * Serialize to a more human-friendly string
  160. *
  161. * @param includePrivate If true, include private key (if it exists)
  162. * @param buf Buffer to store string
  163. * @return ASCII string representation of identity (pointer to buf)
  164. */
  165. char *toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const;
  166. /**
  167. * Deserialize a human-friendly string
  168. *
  169. * Note: validation is for the format only. The locallyValidate() method
  170. * must be used to check signature and address/key correspondence.
  171. *
  172. * @param str String to deserialize
  173. * @return True if deserialization appears successful
  174. */
  175. bool fromString(const char *str);
  176. /**
  177. * @return True if this identity contains something
  178. */
  179. explicit ZT_INLINE operator bool() const noexcept { return (_address); }
  180. ZT_INLINE unsigned long hashCode() const noexcept { return _fp.hashCode(); }
  181. ZT_INLINE bool operator==(const Identity &id) const noexcept { return (_fp == id._fp); }
  182. ZT_INLINE bool operator!=(const Identity &id) const noexcept { return !(*this == id); }
  183. ZT_INLINE bool operator<(const Identity &id) const noexcept { return (_fp < id._fp); }
  184. ZT_INLINE bool operator>(const Identity &id) const noexcept { return (id < *this); }
  185. ZT_INLINE bool operator<=(const Identity &id) const noexcept { return !(id < *this); }
  186. ZT_INLINE bool operator>=(const Identity &id) const noexcept { return !(*this < id); }
  187. static constexpr int marshalSizeMax() noexcept { return ZT_IDENTITY_MARSHAL_SIZE_MAX; }
  188. int marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],bool includePrivate = false) const noexcept;
  189. int unmarshal(const uint8_t *data,int len) noexcept;
  190. private:
  191. void _computeHash();
  192. Address _address;
  193. Fingerprint _fp;
  194. ZT_PACKED_STRUCT(struct { // do not re-order these fields
  195. uint8_t c25519[ZT_C25519_PRIVATE_KEY_LEN];
  196. uint8_t p384[ZT_ECC384_PRIVATE_KEY_SIZE];
  197. }) _priv;
  198. ZT_PACKED_STRUCT(struct { // do not re-order these fields
  199. uint8_t nonce; // nonce for PoW generate/verify
  200. uint8_t c25519[ZT_C25519_PUBLIC_KEY_LEN]; // Curve25519 and Ed25519 public keys
  201. uint8_t p384[ZT_ECC384_PUBLIC_KEY_SIZE]; // NIST P-384 public key
  202. }) _pub;
  203. Type _type; // _type determines which fields in _priv and _pub are used
  204. bool _hasPrivate;
  205. };
  206. } // namespace ZeroTier
  207. #endif