Identity.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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.address = address; // 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. const Address addr(m_fp.hash);
  159. if (!addr.isReserved()) {
  160. m_fp.address = addr;
  161. break;
  162. }
  163. }
  164. } break;
  165. default:
  166. return false;
  167. }
  168. return true;
  169. }
  170. bool Identity::locallyValidate() const noexcept
  171. {
  172. try {
  173. if ((m_fp) && ((!Address(m_fp.address).isReserved()))) {
  174. switch (m_type) {
  175. case C25519: {
  176. uint8_t digest[64];
  177. char *const genmem = (char *) malloc(ZT_V0_IDENTITY_GEN_MEMORY);
  178. if (!genmem)
  179. return false;
  180. identityV0ProofOfWorkFrankenhash(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, digest, genmem);
  181. free(genmem);
  182. return ((Address(digest + 59) == m_fp.address) && (digest[0] < 17));
  183. }
  184. case P384: {
  185. if (Address(m_fp.hash) != m_fp.address)
  186. return false;
  187. return identityV1ProofOfWorkCriteria(m_pub, sizeof(m_pub));
  188. }
  189. }
  190. }
  191. } catch (...) {}
  192. return false;
  193. }
  194. void Identity::hashWithPrivate(uint8_t h[ZT_FINGERPRINT_HASH_SIZE]) const
  195. {
  196. if (m_hasPrivate) {
  197. switch (m_type) {
  198. case C25519:
  199. SHA384(h, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  200. break;
  201. case P384:
  202. SHA384(h, m_pub, sizeof(m_pub), m_priv, sizeof(m_priv));
  203. break;
  204. }
  205. return;
  206. }
  207. Utils::zero<48>(h);
  208. }
  209. unsigned int Identity::sign(const void *data, unsigned int len, void *sig, unsigned int siglen) const
  210. {
  211. if (m_hasPrivate) {
  212. switch (m_type) {
  213. case C25519:
  214. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  215. C25519::sign(m_priv, m_pub, data, len, sig);
  216. return ZT_C25519_SIGNATURE_LEN;
  217. }
  218. case P384:
  219. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  220. // SECURITY: signatures also include the public keys to further enforce their coupling.
  221. uint8_t h[48];
  222. SHA384(h, data, len, m_pub, sizeof(m_pub));
  223. ECC384ECDSASign(m_priv + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, h, (uint8_t *) sig);
  224. return ZT_ECC384_SIGNATURE_SIZE;
  225. }
  226. }
  227. }
  228. return 0;
  229. }
  230. bool Identity::verify(const void *data, unsigned int len, const void *sig, unsigned int siglen) const
  231. {
  232. switch (m_type) {
  233. case C25519:
  234. return C25519::verify(m_pub, data, len, sig, siglen);
  235. case P384:
  236. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  237. uint8_t h[48];
  238. SHA384(h, data, len, m_pub, sizeof(m_pub));
  239. return ECC384ECDSAVerify(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, h, (const uint8_t *) sig);
  240. }
  241. break;
  242. }
  243. return false;
  244. }
  245. bool Identity::agree(const Identity &id, uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) const
  246. {
  247. uint8_t rawkey[128];
  248. uint8_t h[64];
  249. if (m_hasPrivate) {
  250. if (m_type == C25519) {
  251. if ((id.m_type == C25519) || (id.m_type == P384)) {
  252. // If we are a C25519 key we can agree with another C25519 key or with only the
  253. // C25519 portion of a type 1 P-384 key.
  254. C25519::agree(m_priv, id.m_pub, rawkey);
  255. SHA512(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE);
  256. Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
  257. return true;
  258. }
  259. } else if (m_type == P384) {
  260. if (id.m_type == P384) {
  261. // For another P384 identity we execute DH agreement with BOTH keys and then
  262. // hash the results together. For those (cough FIPS cough) who only consider
  263. // P384 to be kosher, the C25519 secret can be considered a "salt"
  264. // or something. For those who don't trust P384 this means the privacy of
  265. // your traffic is also protected by C25519.
  266. C25519::agree(m_priv, id.m_pub, rawkey);
  267. 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);
  268. SHA384(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE);
  269. Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
  270. return true;
  271. } else if (id.m_type == C25519) {
  272. // If the other identity is a C25519 identity we can agree using only that type.
  273. C25519::agree(m_priv, id.m_pub, rawkey);
  274. SHA512(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE);
  275. Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
  276. return true;
  277. }
  278. }
  279. }
  280. return false;
  281. }
  282. char *Identity::toString(bool includePrivate, char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  283. {
  284. char *p = buf;
  285. Address(m_fp.address).toString(p);
  286. p += 10;
  287. *(p++) = ':';
  288. switch (m_type) {
  289. case C25519: {
  290. *(p++) = '0';
  291. *(p++) = ':';
  292. Utils::hex(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, p);
  293. p += ZT_C25519_COMBINED_PUBLIC_KEY_SIZE * 2;
  294. if ((m_hasPrivate) && (includePrivate)) {
  295. *(p++) = ':';
  296. Utils::hex(m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, p);
  297. p += ZT_C25519_COMBINED_PRIVATE_KEY_SIZE * 2;
  298. }
  299. *p = (char) 0;
  300. return buf;
  301. }
  302. case P384: {
  303. *(p++) = '1';
  304. *(p++) = ':';
  305. int el = Utils::b32e(m_pub, sizeof(m_pub), p, (int) (ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t) (p - buf)));
  306. if (el <= 0) return nullptr;
  307. p += el;
  308. if ((m_hasPrivate) && (includePrivate)) {
  309. *(p++) = ':';
  310. el = Utils::b32e(m_priv, sizeof(m_priv), p, (int) (ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t) (p - buf)));
  311. if (el <= 0) return nullptr;
  312. p += el;
  313. }
  314. *p = (char) 0;
  315. return buf;
  316. }
  317. }
  318. return nullptr;
  319. }
  320. bool Identity::fromString(const char *str)
  321. {
  322. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  323. memoryZero(this);
  324. if ((!str) || (!Utils::scopy(tmp, sizeof(tmp), str)))
  325. return false;
  326. int fno = 0;
  327. char *saveptr = nullptr;
  328. for (char *f = Utils::stok(tmp, ":", &saveptr);((f) && (fno < 4));f = Utils::stok(nullptr, ":", &saveptr)) {
  329. switch (fno++) {
  330. case 0:
  331. m_fp.address = Utils::hexStrToU64(f) & ZT_ADDRESS_MASK;
  332. if (Address(m_fp.address).isReserved())
  333. return false;
  334. break;
  335. case 1:
  336. if ((f[0] == '0') && (!f[1])) {
  337. m_type = C25519;
  338. } else if ((f[0] == '1') && (!f[1])) {
  339. m_type = P384;
  340. } else {
  341. return false;
  342. }
  343. break;
  344. case 2:
  345. switch (m_type) {
  346. case C25519:
  347. if (Utils::unhex(f, strlen(f), m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE) != ZT_C25519_COMBINED_PUBLIC_KEY_SIZE)
  348. return false;
  349. break;
  350. case P384:
  351. if (Utils::b32d(f, m_pub, sizeof(m_pub)) != sizeof(m_pub))
  352. return false;
  353. break;
  354. }
  355. break;
  356. case 3:
  357. if (strlen(f) > 1) {
  358. switch (m_type) {
  359. case C25519:
  360. if (Utils::unhex(f, strlen(f), m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) != ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
  361. return false;
  362. } else {
  363. m_hasPrivate = true;
  364. }
  365. break;
  366. case P384:
  367. if (Utils::b32d(f, m_priv, sizeof(m_priv)) != sizeof(m_priv)) {
  368. return false;
  369. } else {
  370. m_hasPrivate = true;
  371. }
  372. break;
  373. }
  374. break;
  375. }
  376. }
  377. }
  378. if (fno < 3)
  379. return false;
  380. m_computeHash();
  381. return !((m_type == P384) && (Address(m_fp.hash) != m_fp.address));
  382. }
  383. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX], const bool includePrivate) const noexcept
  384. {
  385. Address(m_fp.address).copyTo(data);
  386. switch (m_type) {
  387. case C25519:
  388. data[ZT_ADDRESS_LENGTH] = (uint8_t) C25519;
  389. Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1, m_pub);
  390. if ((includePrivate) && (m_hasPrivate)) {
  391. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  392. Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1, m_priv);
  393. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  394. } else {
  395. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = 0;
  396. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1;
  397. }
  398. case P384:
  399. data[ZT_ADDRESS_LENGTH] = (uint8_t) P384;
  400. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1, m_pub);
  401. if ((includePrivate) && (m_hasPrivate)) {
  402. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  403. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1, m_priv);
  404. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  405. } else {
  406. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  407. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  408. }
  409. }
  410. return -1;
  411. }
  412. int Identity::unmarshal(const uint8_t *data, const int len) noexcept
  413. {
  414. memoryZero(this);
  415. if (len < (1 + ZT_ADDRESS_LENGTH))
  416. return -1;
  417. m_fp.address = Address(data);
  418. unsigned int privlen;
  419. switch ((m_type = (Type) data[ZT_ADDRESS_LENGTH])) {
  420. case C25519:
  421. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1))
  422. return -1;
  423. Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(m_pub, data + ZT_ADDRESS_LENGTH + 1);
  424. m_computeHash();
  425. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE];
  426. if (privlen == ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
  427. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE))
  428. return -1;
  429. m_hasPrivate = true;
  430. Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(m_priv, data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1);
  431. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
  432. } else if (privlen == 0) {
  433. m_hasPrivate = false;
  434. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1;
  435. }
  436. break;
  437. case P384:
  438. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  439. return -1;
  440. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(m_pub, data + ZT_ADDRESS_LENGTH + 1);
  441. m_computeHash(); // this sets the address for P384
  442. if (Address(m_fp.hash) != m_fp.address) // this sanity check is possible with V1 identities
  443. return -1;
  444. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  445. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  446. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  447. return -1;
  448. m_hasPrivate = true;
  449. Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(&m_priv, data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1);
  450. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  451. } else if (privlen == 0) {
  452. m_hasPrivate = false;
  453. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  454. }
  455. break;
  456. }
  457. return -1;
  458. }
  459. void Identity::m_computeHash()
  460. {
  461. switch (m_type) {
  462. default:
  463. m_fp.zero();
  464. break;
  465. case C25519:
  466. SHA384(m_fp.m_cfp.hash, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE);
  467. break;
  468. case P384:
  469. SHA384(m_fp.m_cfp.hash, m_pub, sizeof(m_pub));
  470. break;
  471. }
  472. }
  473. } // namespace ZeroTier
  474. extern "C" {
  475. ZT_Identity *ZT_Identity_new(enum ZT_IdentityType type)
  476. {
  477. if ((type != ZT_IDENTITY_TYPE_C25519) && (type != ZT_IDENTITY_TYPE_P384))
  478. return nullptr;
  479. try {
  480. ZeroTier::Identity *const id = new ZeroTier::Identity();
  481. id->generate((ZeroTier::Identity::Type) type);
  482. return reinterpret_cast<ZT_Identity *>(id);
  483. } catch (...) {
  484. return nullptr;
  485. }
  486. }
  487. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  488. {
  489. if (!idStr)
  490. return nullptr;
  491. try {
  492. ZeroTier::Identity *const id = new ZeroTier::Identity();
  493. if (!id->fromString(idStr)) {
  494. delete id;
  495. return nullptr;
  496. }
  497. return reinterpret_cast<ZT_Identity *>(id);
  498. } catch (...) {
  499. return nullptr;
  500. }
  501. }
  502. int ZT_Identity_validate(const ZT_Identity *id)
  503. {
  504. if (!id)
  505. return 0;
  506. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  507. }
  508. unsigned int ZT_Identity_sign(const ZT_Identity *id, const void *data, unsigned int len, void *signature, unsigned int signatureBufferLength)
  509. {
  510. if (!id)
  511. return 0;
  512. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  513. return 0;
  514. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data, len, signature, signatureBufferLength);
  515. }
  516. int ZT_Identity_verify(const ZT_Identity *id, const void *data, unsigned int len, const void *signature, unsigned int sigLen)
  517. {
  518. if ((!id) || (!signature) || (!sigLen))
  519. return 0;
  520. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data, len, signature, sigLen) ? 1 : 0;
  521. }
  522. enum ZT_IdentityType ZT_Identity_type(const ZT_Identity *id)
  523. {
  524. if (!id)
  525. return (ZT_IdentityType) 0;
  526. return (enum ZT_IdentityType) reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  527. }
  528. char *ZT_Identity_toString(const ZT_Identity *id, char *buf, int capacity, int includePrivate)
  529. {
  530. if ((!id) || (!buf) || (capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  531. return nullptr;
  532. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0, buf);
  533. return buf;
  534. }
  535. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  536. {
  537. if (!id)
  538. return 0;
  539. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  540. }
  541. uint64_t ZT_Identity_address(const ZT_Identity *id)
  542. {
  543. if (!id)
  544. return 0;
  545. return reinterpret_cast<const ZeroTier::Identity *>(id)->address();
  546. }
  547. const ZT_Fingerprint *ZT_Identity_fingerprint(const ZT_Identity *id)
  548. {
  549. if (!id)
  550. return nullptr;
  551. return &(reinterpret_cast<const ZeroTier::Identity *>(id)->fingerprint());
  552. }
  553. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  554. {
  555. if (id)
  556. delete reinterpret_cast<ZeroTier::Identity *>(id);
  557. }
  558. }