Identity.cpp 21 KB

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