Identity.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "Containers.hpp"
  24. #define ZT_IDENTITY_STRING_BUFFER_LENGTH 1024
  25. #define ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE (1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + ZT_ECC384_PUBLIC_KEY_SIZE)
  26. #define ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE (ZT_C25519_COMBINED_PRIVATE_KEY_SIZE + ZT_ECC384_PRIVATE_KEY_SIZE)
  27. #define ZT_IDENTITY_MARSHAL_SIZE_MAX (ZT_ADDRESS_LENGTH + 4 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE)
  28. namespace ZeroTier {
  29. /**
  30. * A ZeroTier identity
  31. *
  32. * Identities currently come in two types: type 0 identities based on just Curve25519
  33. * and Ed25519 and type 1 identities that include both a 25519 key pair and a NIST P-384
  34. * key pair. Type 1 identities use P-384 for signatures but use both key pairs at once
  35. * (hashing both keys together) for key agreement with other type 1 identities, and can
  36. * agree with type 0 identities using only Curve25519.
  37. *
  38. * Type 1 identities are better in many ways but type 0 will remain the default until
  39. * 1.x nodes are pretty much dead in the wild.
  40. */
  41. class Identity : public TriviallyCopyable
  42. {
  43. public:
  44. /**
  45. * Identity type -- numeric values of these enums are protocol constants
  46. */
  47. enum Type
  48. {
  49. C25519 = ZT_CRYPTO_ALG_C25519, // Type 0 -- Curve25519 and Ed25519 (1.x and 2.x, default)
  50. P384 = ZT_CRYPTO_ALG_P384 // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+)
  51. };
  52. /**
  53. * A nil/empty identity instance
  54. */
  55. static const Identity NIL;
  56. ZT_INLINE Identity() noexcept // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  57. {
  58. Utils::memoryLock(this,sizeof(Identity));
  59. memoryZero(this);
  60. }
  61. /**
  62. * Construct identity from string
  63. *
  64. * If the identity is not basically valid (no deep checking is done) the result will
  65. * be a null identity.
  66. *
  67. * @param str Identity in canonical string format
  68. */
  69. explicit ZT_INLINE Identity(const char *str) // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  70. {
  71. Utils::memoryLock(this,sizeof(Identity));
  72. fromString(str);
  73. }
  74. ZT_INLINE ~Identity()
  75. {
  76. Utils::memoryUnlock(this,sizeof(Identity));
  77. Utils::burn(reinterpret_cast<void *>(&this->m_priv), sizeof(this->m_priv));
  78. }
  79. /**
  80. * Set identity to NIL value (all zero)
  81. */
  82. ZT_INLINE void zero() noexcept { memoryZero(this); }
  83. /**
  84. * @return Identity type (undefined if identity is null or invalid)
  85. */
  86. ZT_INLINE Type type() const noexcept { return m_type; }
  87. /**
  88. * Generate a new identity (address, key pair)
  89. *
  90. * This is a time consuming operation taking up to 5-10 seconds on some slower systems.
  91. *
  92. * @param t Type of identity to generate
  93. * @return False if there was an error such as type being an invalid value
  94. */
  95. bool generate(Type t);
  96. /**
  97. * Check the validity of this identity's address
  98. *
  99. * For type 0 identities this is slightly time consuming. For type 1 identities it's
  100. * instantaneous. It should be done when a new identity is accepted for the very first
  101. * time.
  102. *
  103. * @return True if validation check passes
  104. */
  105. bool locallyValidate() const noexcept;
  106. /**
  107. * @return True if this identity contains a private key
  108. */
  109. ZT_INLINE bool hasPrivate() const noexcept { return m_hasPrivate; }
  110. /**
  111. * @return This identity's address
  112. */
  113. ZT_INLINE Address address() const noexcept { return Address(m_fp.m_cfp.address); }
  114. /**
  115. * @return Full fingerprint of this identity (address plus SHA384 of keys)
  116. */
  117. ZT_INLINE const Fingerprint &fingerprint() const noexcept { return m_fp; }
  118. /**
  119. * Compute a hash of this identity's public and private keys.
  120. *
  121. * If there is no private key or the identity is NIL the buffer is filled with zero.
  122. *
  123. * @param h Buffer to store SHA384 hash
  124. */
  125. void hashWithPrivate(uint8_t h[ZT_FINGERPRINT_HASH_SIZE]) const;
  126. /**
  127. * Sign a message with this identity (private key required)
  128. *
  129. * The signature buffer should be large enough for the largest
  130. * signature, which is currently 96 bytes.
  131. *
  132. * @param data Data to sign
  133. * @param len Length of data
  134. * @param sig Buffer to receive signature
  135. * @param siglen Length of buffer
  136. * @return Number of bytes actually written to sig or 0 on error
  137. */
  138. unsigned int sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const;
  139. /**
  140. * Verify a message signature against this identity
  141. *
  142. * @param data Data to check
  143. * @param len Length of data
  144. * @param signature Signature bytes
  145. * @param siglen Length of signature in bytes
  146. * @return True if signature validates and data integrity checks
  147. */
  148. bool verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const;
  149. /**
  150. * Shortcut method to perform key agreement with another identity
  151. *
  152. * This identity must have a private key. (Check hasPrivate())
  153. *
  154. * @param id Identity to agree with
  155. * @param key Result parameter to fill with key bytes
  156. * @return Was agreement successful?
  157. */
  158. bool agree(const Identity &id,uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) const;
  159. /**
  160. * Serialize to a more human-friendly string
  161. *
  162. * @param includePrivate If true, include private key (if it exists)
  163. * @param buf Buffer to store string
  164. * @return ASCII string representation of identity (pointer to buf)
  165. */
  166. char *toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const;
  167. ZT_INLINE String toString(const bool includePrivate = false) const { char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]; toString(includePrivate,buf); return String(buf); }
  168. /**
  169. * Deserialize a human-friendly string
  170. *
  171. * Note: validation is for the format only. The locallyValidate() method
  172. * must be used to check signature and address/key correspondence.
  173. *
  174. * @param str String to deserialize
  175. * @return True if deserialization appears successful
  176. */
  177. bool fromString(const char *str);
  178. /**
  179. * @return True if this identity contains something
  180. */
  181. explicit ZT_INLINE operator bool() const noexcept { return (m_fp); }
  182. ZT_INLINE unsigned long hashCode() const noexcept { return m_fp.hashCode(); }
  183. ZT_INLINE bool operator==(const Identity &id) const noexcept { return (m_fp == id.m_fp); }
  184. ZT_INLINE bool operator!=(const Identity &id) const noexcept { return !(*this == id); }
  185. ZT_INLINE bool operator<(const Identity &id) const noexcept { return (m_fp < id.m_fp); }
  186. ZT_INLINE bool operator>(const Identity &id) const noexcept { return (id < *this); }
  187. ZT_INLINE bool operator<=(const Identity &id) const noexcept { return !(id < *this); }
  188. ZT_INLINE bool operator>=(const Identity &id) const noexcept { return !(*this < id); }
  189. static constexpr int marshalSizeMax() noexcept { return ZT_IDENTITY_MARSHAL_SIZE_MAX; }
  190. int marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],bool includePrivate = false) const noexcept;
  191. int unmarshal(const uint8_t *data,int len) noexcept;
  192. private:
  193. void m_computeHash();
  194. Fingerprint m_fp;
  195. uint8_t m_priv[ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE];
  196. uint8_t m_pub[ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  197. Type m_type; // _type determines which fields in _priv and _pub are used
  198. bool m_hasPrivate;
  199. };
  200. } // namespace ZeroTier
  201. #endif