Identity.cpp 21 KB

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