Identity.cpp 21 KB

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