Identity.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <cstdio>
  16. #include <cstdlib>
  17. #include "Constants.hpp"
  18. #include "Utils.hpp"
  19. #include "Address.hpp"
  20. #include "C25519.hpp"
  21. #include "SHA512.hpp"
  22. #include "ECC384.hpp"
  23. #define ZT_IDENTITY_STRING_BUFFER_LENGTH 1024
  24. #define ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE (ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE + ZT_C25519_SIGNATURE_LEN + ZT_ECC384_SIGNATURE_SIZE)
  25. #define ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE (ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE)
  26. #define ZT_IDENTITY_MARSHAL_SIZE_MAX (ZT_ADDRESS_LENGTH + 4 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE)
  27. namespace ZeroTier {
  28. /**
  29. * A ZeroTier identity
  30. *
  31. * An identity consists of a public key, a 40-bit ZeroTier address computed
  32. * from that key in a collision-resistant fashion, and a self-signature.
  33. *
  34. * The address derivation algorithm makes it computationally very expensive to
  35. * search for a different public key that duplicates an existing address. (See
  36. * code for deriveAddress() for this algorithm.)
  37. */
  38. class Identity
  39. {
  40. public:
  41. /**
  42. * Identity type -- numeric values of these enums are protocol constants
  43. */
  44. enum Type
  45. {
  46. C25519 = ZT_CRYPTO_ALG_C25519, // Type 0 -- Curve25519 and Ed25519 (1.x and 2.x, default)
  47. P384 = ZT_CRYPTO_ALG_P384 // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+)
  48. };
  49. ZT_ALWAYS_INLINE Identity() { memset(reinterpret_cast<void *>(this),0,sizeof(Identity)); }
  50. ZT_ALWAYS_INLINE ~Identity() { Utils::burn(reinterpret_cast<void *>(&this->_priv),sizeof(this->_priv)); }
  51. /**
  52. * Construct identity from string
  53. *
  54. * If the identity is not basically valid (no deep checking is done) the result will
  55. * be a null identity.
  56. *
  57. * @param str Identity in canonical string format
  58. */
  59. explicit ZT_ALWAYS_INLINE Identity(const char *str) { fromString(str); }
  60. /**
  61. * Set identity to NIL value (all zero)
  62. */
  63. ZT_ALWAYS_INLINE void zero() { memset(reinterpret_cast<void *>(this),0,sizeof(Identity)); }
  64. /**
  65. * @return Identity type (undefined if identity is null or invalid)
  66. */
  67. ZT_ALWAYS_INLINE Type type() const { return _type; }
  68. /**
  69. * Generate a new identity (address, key pair)
  70. *
  71. * This is a time consuming operation taking up to 5-10 seconds on some slower systems.
  72. *
  73. * @param t Type of identity to generate
  74. */
  75. void generate(Type t);
  76. /**
  77. * Check the validity of this identity's pairing of key to address
  78. *
  79. * @return True if validation check passes
  80. */
  81. bool locallyValidate() const;
  82. /**
  83. * @return True if this identity contains a private key
  84. */
  85. ZT_ALWAYS_INLINE bool hasPrivate() const { return _hasPrivate; }
  86. /**
  87. * @return 384-bit/48-byte hash of this identity's public key(s)
  88. */
  89. ZT_ALWAYS_INLINE const uint8_t *hash() const
  90. {
  91. if (_hash[0] == 0)
  92. const_cast<Identity *>(this)->_computeHash();
  93. return reinterpret_cast<const uint8_t *>(_hash);
  94. }
  95. /**
  96. * Compute a hash of this identity's public and private keys
  97. *
  98. * @param h Buffer to store SHA384 hash
  99. */
  100. void hashWithPrivate(uint8_t h[48]) const;
  101. /**
  102. * Sign a message with this identity (private key required)
  103. *
  104. * The signature buffer should be large enough for the largest
  105. * signature, which is currently 96 bytes.
  106. *
  107. * @param data Data to sign
  108. * @param len Length of data
  109. * @param sig Buffer to receive signature
  110. * @param siglen Length of buffer
  111. * @return Number of bytes actually written to sig or 0 on error
  112. */
  113. unsigned int sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const;
  114. /**
  115. * Verify a message signature against this identity
  116. *
  117. * @param data Data to check
  118. * @param len Length of data
  119. * @param signature Signature bytes
  120. * @param siglen Length of signature in bytes
  121. * @return True if signature validates and data integrity checks
  122. */
  123. bool verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const;
  124. /**
  125. * Shortcut method to perform key agreement with another identity
  126. *
  127. * This identity must have a private key. (Check hasPrivate())
  128. *
  129. * @param id Identity to agree with
  130. * @param key Result parameter to fill with key bytes
  131. * @return Was agreement successful?
  132. */
  133. bool agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const;
  134. /**
  135. * @return This identity's address
  136. */
  137. ZT_ALWAYS_INLINE const Address &address() const { return _address; }
  138. /**
  139. * Serialize to a more human-friendly string
  140. *
  141. * @param includePrivate If true, include private key (if it exists)
  142. * @param buf Buffer to store string
  143. * @return ASCII string representation of identity (pointer to buf)
  144. */
  145. char *toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const;
  146. /**
  147. * Deserialize a human-friendly string
  148. *
  149. * Note: validation is for the format only. The locallyValidate() method
  150. * must be used to check signature and address/key correspondence.
  151. *
  152. * @param str String to deserialize
  153. * @return True if deserialization appears successful
  154. */
  155. bool fromString(const char *str);
  156. /**
  157. * @return True if this identity contains something
  158. */
  159. explicit ZT_ALWAYS_INLINE operator bool() const { return (_address); }
  160. ZT_ALWAYS_INLINE bool operator==(const Identity &id) const
  161. {
  162. if ((_address == id._address)&&(_type == id._type)) {
  163. switch(_type) {
  164. case C25519: return (memcmp(_pub.c25519,id._pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) == 0);
  165. // case P384:
  166. default: return (memcmp(&_pub,&id._pub,sizeof(_pub)) == 0);
  167. }
  168. }
  169. return false;
  170. }
  171. ZT_ALWAYS_INLINE bool operator<(const Identity &id) const
  172. {
  173. if (_address < id._address)
  174. return true;
  175. if (_address == id._address) {
  176. if ((int)_type < (int)id._type)
  177. return true;
  178. if (_type == id._type) {
  179. switch(_type) {
  180. case C25519: return (memcmp(_pub.c25519,id._pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) < 0);
  181. // case P384:
  182. default: return (memcmp(&_pub,&id._pub,sizeof(_pub)) < 0);
  183. }
  184. }
  185. }
  186. return false;
  187. }
  188. ZT_ALWAYS_INLINE bool operator!=(const Identity &id) const { return !(*this == id); }
  189. ZT_ALWAYS_INLINE bool operator>(const Identity &id) const { return (id < *this); }
  190. ZT_ALWAYS_INLINE bool operator<=(const Identity &id) const { return !(id < *this); }
  191. ZT_ALWAYS_INLINE bool operator>=(const Identity &id) const { return !(*this < id); }
  192. 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]); }
  193. static ZT_ALWAYS_INLINE int marshalSizeMax() { return ZT_IDENTITY_MARSHAL_SIZE_MAX; }
  194. int marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],bool includePrivate = false) const;
  195. int unmarshal(const uint8_t *data,int len);
  196. private:
  197. void _computeHash(); // recompute _hash
  198. Address _address;
  199. uint64_t _hash[6]; // hash of public key memo-ized for performance, recalculated when _hash[0] == 0
  200. Type _type; // _type determines which fields in _priv and _pub are used
  201. bool _hasPrivate;
  202. ZT_PACKED_STRUCT(struct { // don't re-order these
  203. uint8_t c25519[ZT_C25519_PRIVATE_KEY_LEN];
  204. uint8_t p384[ZT_ECC384_PRIVATE_KEY_SIZE];
  205. }) _priv;
  206. ZT_PACKED_STRUCT(struct { // don't re-order these
  207. uint8_t c25519[ZT_C25519_PUBLIC_KEY_LEN]; // Curve25519 and Ed25519 public keys
  208. uint8_t p384[ZT_ECC384_PUBLIC_KEY_SIZE]; // NIST P-384 public key
  209. uint8_t c25519s[ZT_C25519_SIGNATURE_LEN]; // signature of both keys with ed25519
  210. uint8_t p384s[ZT_ECC384_SIGNATURE_SIZE]; // signature of both keys with p384
  211. }) _pub;
  212. };
  213. } // namespace ZeroTier
  214. #endif