Identity.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. #include "Constants.hpp"
  14. #include "Identity.hpp"
  15. #include "SHA512.hpp"
  16. #include "Salsa20.hpp"
  17. #include "Poly1305.hpp"
  18. #include "Utils.hpp"
  19. #include "Endpoint.hpp"
  20. #include "Locator.hpp"
  21. #include <algorithm>
  22. namespace ZeroTier {
  23. namespace {
  24. // This is the memory-intensive hash function used to compute v0 identities from v0 public keys.
  25. #define ZT_V0_IDENTITY_GEN_MEMORY 2097152
  26. void identityV0ProofOfWorkFrankenhash(const void *const publicKey, unsigned int publicKeyBytes, void *const digest, void *const genmem) noexcept
  27. {
  28. // Digest publicKey[] to obtain initial digest
  29. SHA512(digest, publicKey, publicKeyBytes);
  30. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  31. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  32. // but is not what we want for sequential memory-hardness.
  33. Utils::zero< ZT_V0_IDENTITY_GEN_MEMORY >(genmem);
  34. Salsa20 s20(digest, (char *)digest + 32);
  35. s20.crypt20((char *)genmem, (char *)genmem, 64);
  36. for (unsigned long i = 64; i < ZT_V0_IDENTITY_GEN_MEMORY; i += 64) {
  37. unsigned long k = i - 64;
  38. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  39. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  40. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  41. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  42. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  43. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  44. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  45. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  46. s20.crypt20((char *)genmem + i, (char *)genmem + i, 64);
  47. }
  48. // Render final digest using genmem as a lookup table
  49. for (unsigned long i = 0; i < (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  50. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
  51. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
  52. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  53. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  54. ((uint64_t *)digest)[idx1] = tmp;
  55. s20.crypt20(digest, digest, 64);
  56. }
  57. }
  58. struct identityV0ProofOfWorkCriteria
  59. {
  60. ZT_INLINE identityV0ProofOfWorkCriteria(unsigned char *sb, char *gm) noexcept: digest(sb), genmem(gm)
  61. {}
  62. ZT_INLINE bool operator()(const uint8_t pub[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE]) const noexcept
  63. {
  64. identityV0ProofOfWorkFrankenhash(pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, digest, genmem);
  65. return (digest[0] < 17);
  66. }
  67. unsigned char *digest;
  68. char *genmem;
  69. };
  70. #define ZT_IDENTITY_V1_POW_MEMORY_SIZE 131072
  71. struct p_CompareLittleEndian
  72. {
  73. #if __BYTE_ORDER == __BIG_ENDIAN
  74. ZT_INLINE bool operator()(const uint64_t a,const uint64_t b) const noexcept { return Utils::swapBytes(a) < Utils::swapBytes(b); }
  75. #else
  76. ZT_INLINE bool operator()(const uint64_t a, const uint64_t b) const noexcept
  77. { return a < b; }
  78. #endif
  79. };
  80. // This is a simpler memory-intensive frankenhash for V1 identity generation.
  81. bool identityV1ProofOfWorkCriteria(const void *in, const unsigned int len)
  82. {
  83. uint64_t w[ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8];
  84. // Fill work buffer with pseudorandom bytes using a construction that should be
  85. // relatively hostile to GPU acceleration. GPUs usually implement branching by
  86. // executing all branches and then selecting the answer, which means this
  87. // construction should require a GPU to do ~3X the work of a CPU per iteration.
  88. SHA512(w, in, len);
  89. for (unsigned int i = 8, j = 0; i < (ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8);) {
  90. uint64_t *const ww = w + i;
  91. const uint64_t *const wp = w + j;
  92. i += 8;
  93. j += 8;
  94. if ((wp[0] & 7U) == 0) {
  95. SHA512(ww, wp, 64);
  96. } else if ((wp[1] & 15U) == 0) {
  97. ww[0] = Utils::hton(Utils::ntoh(wp[0]) % 4503599627370101ULL);
  98. ww[1] = Utils::hton(Utils::ntoh(wp[1]) % 4503599627370161ULL);
  99. ww[2] = Utils::hton(Utils::ntoh(wp[2]) % 4503599627370227ULL);
  100. ww[3] = Utils::hton(Utils::ntoh(wp[3]) % 4503599627370287ULL);
  101. ww[4] = Utils::hton(Utils::ntoh(wp[4]) % 4503599627370299ULL);
  102. ww[5] = Utils::hton(Utils::ntoh(wp[5]) % 4503599627370323ULL);
  103. ww[6] = Utils::hton(Utils::ntoh(wp[6]) % 4503599627370353ULL);
  104. ww[7] = Utils::hton(Utils::ntoh(wp[7]) % 4503599627370449ULL);
  105. SHA384(ww, wp, 128);
  106. } else {
  107. Salsa20(wp, wp + 4).crypt12(wp, ww, 64);
  108. }
  109. }
  110. // Sort 64-bit integers (little-endian) into ascending order and compute a
  111. // cryptographic checksum. Sorting makes the order of values dependent on all
  112. // other values, making a speed competitive implementation that skips on the
  113. // memory requirement extremely hard.
  114. std::sort(w, w + (ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8), p_CompareLittleEndian());
  115. Poly1305::compute(w, w, ZT_IDENTITY_V1_POW_MEMORY_SIZE, w);
  116. // PoW criteria passed if this is true. The value 1093 was chosen experimentally
  117. // to yield a good average performance balancing fast setup with intentional
  118. // identity collision resistance.
  119. return (Utils::ntoh(w[0]) % 1000U) == 0;
  120. }
  121. } // anonymous namespace
  122. const Identity Identity::NIL;
  123. bool Identity::generate(const Type t)
  124. {
  125. m_type = t;
  126. m_hasPrivate = true;
  127. switch (t) {
  128. case C25519: {
  129. // Generate C25519/Ed25519 key pair whose hash satisfies a "hashcash" criterion and generate the
  130. // address from the last 40 bits of this hash. This is different from the fingerprint hash for V0.
  131. uint8_t digest[64];
  132. char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  133. Address address;
  134. do {
  135. C25519::generateSatisfying(identityV0ProofOfWorkCriteria(digest, genmem), m_pub, m_priv);
  136. address.setTo(digest + 59);
  137. } while (address.isReserved());
  138. delete[] genmem;
  139. m_fp.address = address; // address comes from PoW hash for type 0 identities
  140. m_computeHash();
  141. }
  142. break;
  143. case P384: {
  144. for (;;) {
  145. // Loop until we pass the PoW criteria. The nonce is only 8 bits, so generate
  146. // some new key material every time it wraps. The ECC384 generator is slightly
  147. // faster so use that one.
  148. m_pub[0] = 0; // zero nonce
  149. C25519::generateCombined(m_pub + 1, m_priv + 1);
  150. ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  151. for (;;) {
  152. if (identityV1ProofOfWorkCriteria(m_pub, sizeof(m_pub)))
  153. break;
  154. if (++m_pub[0] == 0)
  155. ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  156. }
  157. // If we passed PoW then check that the address is valid, otherwise loop
  158. // back around and run the whole process again.
  159. m_computeHash();
  160. const Address addr(m_fp.hash);
  161. if (!addr.isReserved()) {
  162. m_fp.address = addr;
  163. break;
  164. }
  165. }
  166. }
  167. break;
  168. default:
  169. return false;
  170. }
  171. return true;
  172. }
  173. bool Identity::locallyValidate() const noexcept
  174. {
  175. try {
  176. if ((m_fp) && ((!Address(m_fp.address).isReserved()))) {
  177. switch (m_type) {
  178. case C25519: {
  179. uint8_t digest[64];
  180. char *const genmem = (char *)malloc(ZT_V0_IDENTITY_GEN_MEMORY);
  181. if (!genmem)
  182. return false;
  183. identityV0ProofOfWorkFrankenhash(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, digest, genmem);
  184. free(genmem);
  185. return ((Address(digest + 59) == m_fp.address) && (digest[0] < 17));
  186. }
  187. case P384: {
  188. if (Address(m_fp.hash) != m_fp.address)
  189. return false;
  190. return identityV1ProofOfWorkCriteria(m_pub, sizeof(m_pub));
  191. }
  192. }
  193. }
  194. } catch (...) {}
  195. return false;
  196. }
  197. void Identity::hashWithPrivate(uint8_t h[ZT_FINGERPRINT_HASH_SIZE]) const
  198. {
  199. if (m_hasPrivate) {
  200. switch (m_type) {
  201. case C25519:
  202. SHA384(h, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  203. return;
  204. case P384:
  205. SHA384(h, m_pub, sizeof(m_pub), m_priv, sizeof(m_priv));
  206. return;
  207. }
  208. }
  209. Utils::zero< ZT_FINGERPRINT_HASH_SIZE >(h);
  210. }
  211. unsigned int Identity::sign(const void *data, unsigned int len, void *sig, unsigned int siglen) const
  212. {
  213. if (m_hasPrivate) {
  214. switch (m_type) {
  215. case C25519:
  216. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  217. C25519::sign(m_priv, m_pub, data, len, sig);
  218. return ZT_C25519_SIGNATURE_LEN;
  219. }
  220. case P384:
  221. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  222. // SECURITY: signatures also include the public keys to further enforce their coupling.
  223. static_assert(ZT_ECC384_SIGNATURE_HASH_SIZE == ZT_SHA384_DIGEST_SIZE, "weird!");
  224. uint8_t h[ZT_ECC384_SIGNATURE_HASH_SIZE];
  225. SHA384(h, data, len, m_pub, ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  226. ECC384ECDSASign(m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, h, (uint8_t *)sig);
  227. return ZT_ECC384_SIGNATURE_SIZE;
  228. }
  229. }
  230. }
  231. return 0;
  232. }
  233. bool Identity::verify(const void *data, unsigned int len, const void *sig, unsigned int siglen) const
  234. {
  235. switch (m_type) {
  236. case C25519:
  237. return C25519::verify(m_pub, data, len, sig, siglen);
  238. case P384:
  239. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  240. uint8_t h[ZT_ECC384_SIGNATURE_HASH_SIZE];
  241. SHA384(h, data, len, m_pub, ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  242. return ECC384ECDSAVerify(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, h, (const uint8_t *)sig);
  243. }
  244. break;
  245. }
  246. return false;
  247. }
  248. bool Identity::agree(const Identity &id, uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) const
  249. {
  250. uint8_t rawkey[128], h[64];
  251. if (likely(m_hasPrivate)) {
  252. if ((m_type == C25519) || (id.m_type == C25519)) {
  253. // If we are a C25519 key we can agree with another C25519 key or with only the
  254. // C25519 portion of a type 1 P-384 key.
  255. C25519::agree(m_priv, id.m_pub, rawkey);
  256. SHA512(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE);
  257. Utils::copy< ZT_SYMMETRIC_KEY_SIZE >(key, h);
  258. return true;
  259. } else if ((m_type == P384) && (id.m_type == P384)) {
  260. // For another P384 identity we execute DH agreement with BOTH keys and then
  261. // hash the results together. For those (cough FIPS cough) who only consider
  262. // P384 to be kosher, the C25519 secret can be considered a "salt"
  263. // or something. For those who don't trust P384 this means the privacy of
  264. // your traffic is also protected by C25519.
  265. C25519::agree(m_priv, id.m_pub, rawkey);
  266. ECC384ECDH(id.m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, rawkey + ZT_C25519_ECDH_SHARED_SECRET_SIZE);
  267. SHA384(key, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE);
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. char *Identity::toString(bool includePrivate, char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  274. {
  275. char *p = buf;
  276. Address(m_fp.address).toString(p);
  277. p += 10;
  278. *(p++) = ':';
  279. switch (m_type) {
  280. case C25519: {
  281. *(p++) = '0';
  282. *(p++) = ':';
  283. Utils::hex(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, p);
  284. p += ZT_C25519_COMBINED_PUBLIC_KEY_SIZE * 2;
  285. if ((m_hasPrivate) && (includePrivate)) {
  286. *(p++) = ':';
  287. Utils::hex(m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, p);
  288. p += ZT_C25519_COMBINED_PRIVATE_KEY_SIZE * 2;
  289. }
  290. *p = (char)0;
  291. return buf;
  292. }
  293. case P384: {
  294. *(p++) = '1';
  295. *(p++) = ':';
  296. int el = Utils::b32e(m_pub, sizeof(m_pub), p, (int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  297. if (el <= 0) return nullptr;
  298. p += el;
  299. if ((m_hasPrivate) && (includePrivate)) {
  300. *(p++) = ':';
  301. el = Utils::b32e(m_priv, sizeof(m_priv), p, (int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  302. if (el <= 0) return nullptr;
  303. p += el;
  304. }
  305. *p = (char)0;
  306. return buf;
  307. }
  308. }
  309. return nullptr;
  310. }
  311. bool Identity::fromString(const char *str)
  312. {
  313. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  314. memoryZero(this);
  315. if ((!str) || (!Utils::scopy(tmp, sizeof(tmp), str)))
  316. return false;
  317. int fno = 0;
  318. char *saveptr = nullptr;
  319. for (char *f = Utils::stok(tmp, ":", &saveptr); ((f) && (fno < 4)); f = Utils::stok(nullptr, ":", &saveptr)) {
  320. switch (fno++) {
  321. case 0:
  322. m_fp.address = Utils::hexStrToU64(f) & ZT_ADDRESS_MASK;
  323. if (Address(m_fp.address).isReserved())
  324. return false;
  325. break;
  326. case 1:
  327. if ((f[0] == '0') && (!f[1])) {
  328. m_type = C25519;
  329. } else if ((f[0] == '1') && (!f[1])) {
  330. m_type = P384;
  331. } else {
  332. return false;
  333. }
  334. break;
  335. case 2:
  336. switch (m_type) {
  337. case C25519:
  338. if (Utils::unhex(f, strlen(f), m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE) != ZT_C25519_COMBINED_PUBLIC_KEY_SIZE)
  339. return false;
  340. break;
  341. case P384:
  342. if (Utils::b32d(f, m_pub, sizeof(m_pub)) != sizeof(m_pub))
  343. return false;
  344. break;
  345. }
  346. break;
  347. case 3:
  348. if (strlen(f) > 1) {
  349. switch (m_type) {
  350. case C25519:
  351. if (Utils::unhex(f, strlen(f), m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) != ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
  352. return false;
  353. } else {
  354. m_hasPrivate = true;
  355. }
  356. break;
  357. case P384:
  358. if (Utils::b32d(f, m_priv, sizeof(m_priv)) != sizeof(m_priv)) {
  359. return false;
  360. } else {
  361. m_hasPrivate = true;
  362. }
  363. break;
  364. }
  365. break;
  366. }
  367. }
  368. }
  369. if (fno < 3)
  370. return false;
  371. m_computeHash();
  372. return !((m_type == P384) && (Address(m_fp.hash) != m_fp.address));
  373. }
  374. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX], const bool includePrivate) const noexcept
  375. {
  376. Address(m_fp.address).copyTo(data);
  377. switch (m_type) {
  378. case C25519:
  379. data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
  380. Utils::copy< ZT_C25519_COMBINED_PUBLIC_KEY_SIZE >(data + ZT_ADDRESS_LENGTH + 1, m_pub);
  381. if ((includePrivate) && (m_hasPrivate)) {
  382. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  383. Utils::copy< ZT_C25519_COMBINED_PRIVATE_KEY_SIZE >(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1, m_priv);
  384. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  385. } else {
  386. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = 0;
  387. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1;
  388. }
  389. case P384:
  390. data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
  391. Utils::copy< ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE >(data + ZT_ADDRESS_LENGTH + 1, m_pub);
  392. if ((includePrivate) && (m_hasPrivate)) {
  393. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  394. Utils::copy< ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE >(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1, m_priv);
  395. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  396. } else {
  397. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  398. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  399. }
  400. }
  401. return -1;
  402. }
  403. int Identity::unmarshal(const uint8_t *data, const int len) noexcept
  404. {
  405. memoryZero(this);
  406. if (len < (1 + ZT_ADDRESS_LENGTH))
  407. return -1;
  408. m_fp.address = Address(data);
  409. unsigned int privlen;
  410. switch ((m_type = (Type)data[ZT_ADDRESS_LENGTH])) {
  411. case C25519:
  412. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1))
  413. return -1;
  414. Utils::copy< ZT_C25519_COMBINED_PUBLIC_KEY_SIZE >(m_pub, data + ZT_ADDRESS_LENGTH + 1);
  415. m_computeHash();
  416. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE];
  417. if (privlen == ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
  418. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE))
  419. return -1;
  420. m_hasPrivate = true;
  421. Utils::copy< ZT_C25519_COMBINED_PRIVATE_KEY_SIZE >(m_priv, data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1);
  422. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  423. } else if (privlen == 0) {
  424. m_hasPrivate = false;
  425. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1;
  426. }
  427. break;
  428. case P384:
  429. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  430. return -1;
  431. Utils::copy< ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE >(m_pub, data + ZT_ADDRESS_LENGTH + 1);
  432. m_computeHash(); // this sets the address for P384
  433. if (Address(m_fp.hash) != m_fp.address) // this sanity check is possible with V1 identities
  434. return -1;
  435. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  436. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  437. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  438. return -1;
  439. m_hasPrivate = true;
  440. Utils::copy< ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE >(&m_priv, data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1);
  441. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  442. } else if (privlen == 0) {
  443. m_hasPrivate = false;
  444. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  445. }
  446. break;
  447. }
  448. return -1;
  449. }
  450. void Identity::m_computeHash()
  451. {
  452. switch (m_type) {
  453. default:
  454. m_fp.zero();
  455. break;
  456. case C25519:
  457. SHA384(m_fp.hash, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE);
  458. break;
  459. case P384:
  460. SHA384(m_fp.hash, m_pub, ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  461. break;
  462. }
  463. }
  464. } // namespace ZeroTier
  465. extern "C" {
  466. ZT_Identity *ZT_Identity_new(enum ZT_IdentityType type)
  467. {
  468. if ((type != ZT_IDENTITY_TYPE_C25519) && (type != ZT_IDENTITY_TYPE_P384))
  469. return nullptr;
  470. try {
  471. ZeroTier::Identity *const id = new ZeroTier::Identity();
  472. id->generate((ZeroTier::Identity::Type)type);
  473. return reinterpret_cast<ZT_Identity *>(id);
  474. } catch (...) {
  475. return nullptr;
  476. }
  477. }
  478. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  479. {
  480. if (!idStr)
  481. return nullptr;
  482. try {
  483. ZeroTier::Identity *const id = new ZeroTier::Identity();
  484. if (!id->fromString(idStr)) {
  485. delete id;
  486. return nullptr;
  487. }
  488. return reinterpret_cast<ZT_Identity *>(id);
  489. } catch (...) {
  490. return nullptr;
  491. }
  492. }
  493. int ZT_Identity_validate(const ZT_Identity *id)
  494. {
  495. if (!id)
  496. return 0;
  497. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  498. }
  499. unsigned int ZT_Identity_sign(const ZT_Identity *id, const void *data, unsigned int len, void *signature, unsigned int signatureBufferLength)
  500. {
  501. if (!id)
  502. return 0;
  503. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  504. return 0;
  505. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data, len, signature, signatureBufferLength);
  506. }
  507. int ZT_Identity_verify(const ZT_Identity *id, const void *data, unsigned int len, const void *signature, unsigned int sigLen)
  508. {
  509. if ((!id) || (!signature) || (!sigLen))
  510. return 0;
  511. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data, len, signature, sigLen) ? 1 : 0;
  512. }
  513. enum ZT_IdentityType ZT_Identity_type(const ZT_Identity *id)
  514. {
  515. if (!id)
  516. return (ZT_IdentityType)0;
  517. return (enum ZT_IdentityType)reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  518. }
  519. char *ZT_Identity_toString(const ZT_Identity *id, char *buf, int capacity, int includePrivate)
  520. {
  521. if ((!id) || (!buf) || (capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  522. return nullptr;
  523. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0, buf);
  524. return buf;
  525. }
  526. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  527. {
  528. if (!id)
  529. return 0;
  530. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  531. }
  532. uint64_t ZT_Identity_address(const ZT_Identity *id)
  533. {
  534. if (!id)
  535. return 0;
  536. return reinterpret_cast<const ZeroTier::Identity *>(id)->address();
  537. }
  538. const ZT_Fingerprint *ZT_Identity_fingerprint(const ZT_Identity *id)
  539. {
  540. if (!id)
  541. return nullptr;
  542. return &(reinterpret_cast<const ZeroTier::Identity *>(id)->fingerprint());
  543. }
  544. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  545. {
  546. if (id)
  547. delete reinterpret_cast<ZeroTier::Identity *>(id);
  548. }
  549. }