Identity.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "Buffer.hpp"
  22. #include "SHA512.hpp"
  23. #include "ECC384.hpp"
  24. #define ZT_IDENTITY_STRING_BUFFER_LENGTH 1024
  25. #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)
  26. #define ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE (ZT_C25519_PRIVATE_KEY_LEN + 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. * An identity consists of a public key, a 40-bit ZeroTier address computed
  33. * from that key in a collision-resistant fashion, and a self-signature.
  34. *
  35. * The address derivation algorithm makes it computationally very expensive to
  36. * search for a different public key that duplicates an existing address. (See
  37. * code for deriveAddress() for this algorithm.)
  38. */
  39. class Identity
  40. {
  41. public:
  42. /**
  43. * Identity type -- numeric values of these enums are protocol constants
  44. */
  45. enum Type
  46. {
  47. C25519 = ZT_CRYPTO_ALG_C25519, // Type 0 -- Curve25519 and Ed25519 (1.x and 2.x, default)
  48. P384 = ZT_CRYPTO_ALG_P384 // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+)
  49. };
  50. ZT_ALWAYS_INLINE Identity() { memset(reinterpret_cast<void *>(this),0,sizeof(Identity)); }
  51. ZT_ALWAYS_INLINE ~Identity() { Utils::burn(reinterpret_cast<void *>(&this->_priv),sizeof(this->_priv)); }
  52. /**
  53. * Construct identity from string
  54. *
  55. * If the identity is not basically valid (no deep checking is done) the result will
  56. * be a null identity.
  57. *
  58. * @param str Identity in canonical string format
  59. */
  60. explicit ZT_ALWAYS_INLINE Identity(const char *str) { fromString(str); }
  61. /**
  62. * Set identity to NIL value (all zero)
  63. */
  64. ZT_ALWAYS_INLINE void zero() { memset(reinterpret_cast<void *>(this),0,sizeof(Identity)); }
  65. /**
  66. * @return Identity type (undefined if identity is null or invalid)
  67. */
  68. ZT_ALWAYS_INLINE Type type() const { return _type; }
  69. /**
  70. * Generate a new identity (address, key pair)
  71. *
  72. * This is a time consuming operation taking up to 5-10 seconds on some slower systems.
  73. *
  74. * @param t Type of identity to generate
  75. */
  76. void generate(Type t);
  77. /**
  78. * Check the validity of this identity's pairing of key to address
  79. *
  80. * @return True if validation check passes
  81. */
  82. bool locallyValidate() const;
  83. /**
  84. * @return True if this identity contains a private key
  85. */
  86. ZT_ALWAYS_INLINE bool hasPrivate() const { return _hasPrivate; }
  87. /**
  88. * This generates a SHA384 hash of this identity's keys.
  89. *
  90. * @param h Buffer to receive SHA384 of public key(s)
  91. * @param includePrivate If true, hash private key(s) as well
  92. */
  93. bool hash(uint8_t h[48],bool includePrivate = false) const;
  94. /**
  95. * Sign a message with this identity (private key required)
  96. *
  97. * The signature buffer should be large enough for the largest
  98. * signature, which is currently 96 bytes.
  99. *
  100. * @param data Data to sign
  101. * @param len Length of data
  102. * @param sig Buffer to receive signature
  103. * @param siglen Length of buffer
  104. * @return Number of bytes actually written to sig or 0 on error
  105. */
  106. unsigned int sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const;
  107. /**
  108. * Verify a message signature against this identity
  109. *
  110. * @param data Data to check
  111. * @param len Length of data
  112. * @param signature Signature bytes
  113. * @param siglen Length of signature in bytes
  114. * @return True if signature validates and data integrity checks
  115. */
  116. bool verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const;
  117. /**
  118. * Shortcut method to perform key agreement with another identity
  119. *
  120. * This identity must have a private key. (Check hasPrivate())
  121. *
  122. * @param id Identity to agree with
  123. * @param key Result parameter to fill with key bytes
  124. * @return Was agreement successful?
  125. */
  126. bool agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const;
  127. /**
  128. * @return This identity's address
  129. */
  130. ZT_ALWAYS_INLINE const Address &address() const { return _address; }
  131. /**
  132. * Serialize this identity (binary)
  133. *
  134. * @param b Destination buffer to append to
  135. * @param includePrivate If true, include private key component (if present) (default: false)
  136. */
  137. template<unsigned int C>
  138. inline void serialize(Buffer<C> &b,bool includePrivate = false) const
  139. {
  140. _address.appendTo(b);
  141. switch(_type) {
  142. case C25519:
  143. b.append((uint8_t)C25519);
  144. b.append(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  145. if ((_hasPrivate)&&(includePrivate)) {
  146. b.append((uint8_t)ZT_C25519_PRIVATE_KEY_LEN);
  147. b.append(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  148. } else {
  149. b.append((uint8_t)0);
  150. }
  151. break;
  152. case P384:
  153. b.append((uint8_t)P384);
  154. b.append(&_pub,ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE + ZT_C25519_SIGNATURE_LEN + ZT_ECC384_SIGNATURE_SIZE);
  155. if ((_hasPrivate)&&(includePrivate)) {
  156. b.append((uint8_t)(ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE));
  157. b.append(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  158. b.append(_priv.p384,ZT_ECC384_PRIVATE_KEY_SIZE);
  159. } else {
  160. b.append((uint8_t)0);
  161. }
  162. b.append((uint8_t)0); // size of additional fields (should have included such a thing in v0!)
  163. break;
  164. }
  165. }
  166. /**
  167. * Deserialize a binary serialized identity
  168. *
  169. * If an exception is thrown, the Identity object is left in an undefined
  170. * state and should not be used.
  171. *
  172. * @param b Buffer containing serialized data
  173. * @param startAt Index within buffer of serialized data (default: 0)
  174. * @return Length of serialized data read from buffer
  175. */
  176. template<unsigned int C>
  177. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  178. {
  179. _hasPrivate = false;
  180. unsigned int p = startAt;
  181. unsigned int pkl;
  182. _address.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  183. p += ZT_ADDRESS_LENGTH;
  184. switch((_type = (Type)b[p++])) {
  185. case C25519:
  186. memcpy(_pub.c25519,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN);
  187. p += ZT_C25519_PUBLIC_KEY_LEN;
  188. pkl = (unsigned int)b[p++];
  189. if (pkl) {
  190. if (pkl != ZT_C25519_PRIVATE_KEY_LEN)
  191. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  192. _hasPrivate = true;
  193. memcpy(_priv.c25519,b.field(p,ZT_C25519_PRIVATE_KEY_LEN),ZT_C25519_PRIVATE_KEY_LEN);
  194. p += ZT_C25519_PRIVATE_KEY_LEN;
  195. } else {
  196. _hasPrivate = false;
  197. }
  198. break;
  199. case P384:
  200. memcpy(&_pub,b.field(p,ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE + ZT_C25519_SIGNATURE_LEN + ZT_ECC384_SIGNATURE_SIZE),ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE + ZT_C25519_SIGNATURE_LEN + ZT_ECC384_SIGNATURE_SIZE);
  201. p += ZT_C25519_PUBLIC_KEY_LEN + ZT_ECC384_PUBLIC_KEY_SIZE + ZT_C25519_SIGNATURE_LEN + ZT_ECC384_SIGNATURE_SIZE;
  202. pkl = (unsigned int)b[p++];
  203. if (pkl) {
  204. if (pkl != (ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE))
  205. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  206. _hasPrivate = true;
  207. memcpy(_priv.c25519,b.field(p,ZT_C25519_PRIVATE_KEY_LEN),ZT_C25519_PRIVATE_KEY_LEN);
  208. p += ZT_C25519_PRIVATE_KEY_LEN;
  209. memcpy(_priv.p384,b.field(p,ZT_ECC384_PRIVATE_KEY_SIZE),ZT_ECC384_PRIVATE_KEY_SIZE);
  210. p += ZT_ECC384_PRIVATE_KEY_SIZE;
  211. } else {
  212. _hasPrivate = false;
  213. }
  214. p += b.template at<uint8_t>(p) + 2;
  215. break;
  216. default:
  217. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  218. }
  219. return (p - startAt);
  220. }
  221. /**
  222. * Serialize to a more human-friendly string
  223. *
  224. * @param includePrivate If true, include private key (if it exists)
  225. * @param buf Buffer to store string
  226. * @return ASCII string representation of identity (pointer to buf)
  227. */
  228. char *toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const;
  229. /**
  230. * Deserialize a human-friendly string
  231. *
  232. * Note: validation is for the format only. The locallyValidate() method
  233. * must be used to check signature and address/key correspondence.
  234. *
  235. * @param str String to deserialize
  236. * @return True if deserialization appears successful
  237. */
  238. bool fromString(const char *str);
  239. /**
  240. * @return True if this identity contains something
  241. */
  242. explicit ZT_ALWAYS_INLINE operator bool() const { return (_address); }
  243. ZT_ALWAYS_INLINE bool operator==(const Identity &id) const
  244. {
  245. if ((_address == id._address)&&(_type == id._type)) {
  246. switch(_type) {
  247. case C25519: return (memcmp(_pub.c25519,id._pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) == 0);
  248. // case P384:
  249. default: return (memcmp(&_pub,&id._pub,sizeof(_pub)) == 0);
  250. }
  251. }
  252. return false;
  253. }
  254. ZT_ALWAYS_INLINE bool operator<(const Identity &id) const
  255. {
  256. if (_address < id._address)
  257. return true;
  258. if (_address == id._address) {
  259. if ((int)_type < (int)id._type)
  260. return true;
  261. if (_type == id._type) {
  262. switch(_type) {
  263. case C25519: return (memcmp(_pub.c25519,id._pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) < 0);
  264. // case P384:
  265. default: return (memcmp(&_pub,&id._pub,sizeof(_pub)) < 0);
  266. }
  267. }
  268. }
  269. return false;
  270. }
  271. ZT_ALWAYS_INLINE bool operator!=(const Identity &id) const { return !(*this == id); }
  272. ZT_ALWAYS_INLINE bool operator>(const Identity &id) const { return (id < *this); }
  273. ZT_ALWAYS_INLINE bool operator<=(const Identity &id) const { return !(id < *this); }
  274. ZT_ALWAYS_INLINE bool operator>=(const Identity &id) const { return !(*this < id); }
  275. 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]); }
  276. // Marshal interface ///////////////////////////////////////////////////////
  277. static ZT_ALWAYS_INLINE int marshalSizeMax() { return ZT_IDENTITY_MARSHAL_SIZE_MAX; }
  278. inline int marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool includePrivate = false) const
  279. {
  280. _address.copyTo(data,ZT_ADDRESS_LENGTH);
  281. switch(_type) {
  282. case C25519:
  283. data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
  284. memcpy(data + ZT_ADDRESS_LENGTH + 1,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  285. if ((includePrivate)&&(_hasPrivate)) {
  286. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = ZT_C25519_PRIVATE_KEY_LEN;
  287. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  288. return (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN);
  289. }
  290. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = 0;
  291. return (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1);
  292. case P384:
  293. data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
  294. memcpy(data + ZT_ADDRESS_LENGTH + 1,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  295. if ((includePrivate)&&(_hasPrivate)) {
  296. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_C25519_PRIVATE_KEY_LEN + ZT_ECC384_PRIVATE_KEY_SIZE;
  297. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,&_priv,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  298. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE] = 0;
  299. return (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE + 1);
  300. }
  301. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  302. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1] = 0;
  303. return (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 2);
  304. }
  305. return -1;
  306. }
  307. inline int unmarshal(const uint8_t *restrict data,const int len)
  308. {
  309. if (len < (ZT_ADDRESS_LENGTH + 1))
  310. return -1;
  311. unsigned int privlen;
  312. switch((_type = (Type)data[ZT_ADDRESS_LENGTH])) {
  313. case C25519:
  314. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1))
  315. return -1;
  316. memcpy(_pub.c25519,data + ZT_ADDRESS_LENGTH + 1,ZT_C25519_PUBLIC_KEY_LEN);
  317. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN];
  318. if (privlen == ZT_C25519_PRIVATE_KEY_LEN) {
  319. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN))
  320. return -1;
  321. _hasPrivate = true;
  322. memcpy(_priv.c25519,data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,ZT_C25519_PRIVATE_KEY_LEN);
  323. return (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN);
  324. } else if (privlen == 0) {
  325. _hasPrivate = false;
  326. return (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1);
  327. }
  328. break;
  329. case P384:
  330. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 2))
  331. return -1;
  332. memcpy(&_pub,data + ZT_ADDRESS_LENGTH + 1,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  333. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  334. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  335. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE + 1))
  336. return -1;
  337. _hasPrivate = true;
  338. memcpy(&_priv,data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  339. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE];
  340. if (len < (privlen + (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE + 1)))
  341. return -1;
  342. return (int)(privlen + (unsigned int)(ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE + 1));
  343. } else if (privlen == 0) {
  344. _hasPrivate = false;
  345. return (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 2);
  346. }
  347. break;
  348. }
  349. return -1;
  350. }
  351. ////////////////////////////////////////////////////////////////////////////
  352. private:
  353. Address _address;
  354. Type _type; // _type determines which fields in _priv and _pub are used
  355. bool _hasPrivate;
  356. ZT_PACKED_STRUCT(struct { // don't re-order these
  357. uint8_t c25519[ZT_C25519_PRIVATE_KEY_LEN];
  358. uint8_t p384[ZT_ECC384_PRIVATE_KEY_SIZE];
  359. }) _priv;
  360. ZT_PACKED_STRUCT(struct { // don't re-order these
  361. uint8_t c25519[ZT_C25519_PUBLIC_KEY_LEN]; // Curve25519 and Ed25519 public keys
  362. uint8_t p384[ZT_ECC384_PUBLIC_KEY_SIZE]; // NIST P-384 public key
  363. uint8_t c25519s[ZT_C25519_SIGNATURE_LEN]; // signature of both keys with ed25519
  364. uint8_t p384s[ZT_ECC384_SIGNATURE_SIZE]; // signature of both keys with p384
  365. }) _pub;
  366. };
  367. } // namespace ZeroTier
  368. #endif