Identity.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 "AES.hpp"
  18. #include "Utils.hpp"
  19. #include <cstring>
  20. #include <cstdint>
  21. #include <algorithm>
  22. namespace ZeroTier {
  23. namespace {
  24. // --------------------------------------------------------------------------------------------------------------------
  25. // This is the memory-intensive hash function used to compute v0 identities
  26. // from v0 public keys.
  27. #define ZT_V0_IDENTITY_GEN_MEMORY 2097152
  28. static void _computeMemoryHardHash(const void *const publicKey,unsigned int publicKeyBytes,void *const digest,void *const genmem) noexcept
  29. {
  30. // Digest publicKey[] to obtain initial digest
  31. SHA512(digest,publicKey,publicKeyBytes);
  32. // Initialize genmem[] using Salsa20 in a CBC-like configuration since
  33. // ordinary Salsa20 is randomly seek-able. This is good for a cipher
  34. // but is not what we want for sequential memory-hardness.
  35. memset(genmem,0,ZT_V0_IDENTITY_GEN_MEMORY);
  36. Salsa20 s20(digest,(char *)digest + 32);
  37. s20.crypt20((char *)genmem,(char *)genmem,64);
  38. for(unsigned long i=64;i<ZT_V0_IDENTITY_GEN_MEMORY;i+=64) {
  39. unsigned long k = i - 64;
  40. *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
  41. *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
  42. *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
  43. *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
  44. *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
  45. *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
  46. *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
  47. *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
  48. s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
  49. }
  50. // Render final digest using genmem as a lookup table
  51. for(unsigned long i=0;i<(ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
  52. unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
  53. unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
  54. uint64_t tmp = ((uint64_t *)genmem)[idx2];
  55. ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
  56. ((uint64_t *)digest)[idx1] = tmp;
  57. s20.crypt20(digest,digest,64);
  58. }
  59. }
  60. struct _v0_identity_generate_cond
  61. {
  62. ZT_ALWAYS_INLINE _v0_identity_generate_cond() noexcept {}
  63. ZT_ALWAYS_INLINE _v0_identity_generate_cond(unsigned char *sb,char *gm) noexcept : digest(sb),genmem(gm) {}
  64. ZT_ALWAYS_INLINE bool operator()(const uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN]) const noexcept
  65. {
  66. _computeMemoryHardHash(pub,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  67. return (digest[0] < 17);
  68. }
  69. unsigned char *digest;
  70. char *genmem;
  71. };
  72. // --------------------------------------------------------------------------------------------------------------------
  73. } // anonymous namespace
  74. const Identity Identity::NIL;
  75. bool Identity::generate(const Type t)
  76. {
  77. uint8_t digest[64];
  78. _type = t;
  79. _hasPrivate = true;
  80. _hash[0] = 0; // force hash recompute
  81. switch(t) {
  82. case C25519: {
  83. char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  84. do {
  85. C25519::generateSatisfying(_v0_identity_generate_cond(digest,genmem),_pub.c25519,_priv.c25519);
  86. _address.setTo(digest + 59); // last 5 bytes are address
  87. } while (_address.isReserved());
  88. delete[] genmem;
  89. } break;
  90. case P384: {
  91. do {
  92. C25519::generate(_pub.c25519,_priv.c25519);
  93. ECC384GenerateKey(_pub.p384,_priv.p384);
  94. // This is just an intentionally complex hash function for use with a simple hashcash
  95. // design to slow down identity generation as a defense in depth against brute force
  96. // collision searches. V0 used a somewhat more overkill memory intensive design that's
  97. // not really necessary and makes verifications too slow, so V1 uses this instead.
  98. SHA384(digest,&_pub,sizeof(_pub));
  99. AES c(digest);
  100. SHA384(digest,digest,48);
  101. std::sort(digest,digest + 48);
  102. c.encrypt(digest,digest);
  103. c.encrypt(digest + 16,digest + 16);
  104. c.encrypt(digest + 32,digest + 32);
  105. SHA384(digest,digest,48);
  106. if (digest[47] != 0)
  107. continue;
  108. _address.setTo(digest);
  109. } while (_address.isReserved());
  110. } break;
  111. default:
  112. return false;
  113. }
  114. return true;
  115. }
  116. bool Identity::locallyValidate() const
  117. {
  118. if ((_address.isReserved())||(!_address))
  119. return false;
  120. switch (_type) {
  121. case C25519:
  122. try {
  123. uint8_t digest[64];
  124. char *genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  125. _computeMemoryHardHash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  126. delete [] genmem;
  127. return ((_address == Address(digest + 59))&&(!_address.isReserved())&&(digest[0] < 17));
  128. } catch ( ... ) {}
  129. return false;
  130. case P384: {
  131. const uint8_t *hash = this->hash();
  132. return ((hash[47] == 0)&&(Address(hash) == _address));
  133. }
  134. default:
  135. return false;
  136. }
  137. }
  138. const uint8_t *Identity::hash() const
  139. {
  140. uint8_t *const hash = const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(_hash));
  141. switch(_type) {
  142. default:
  143. memset(hash,0,48);
  144. break;
  145. case C25519:
  146. if (_hash[0] == 0)
  147. SHA384(hash,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  148. break;
  149. case P384:
  150. if (_hash[0] == 0) {
  151. SHA384(hash,&_pub,sizeof(_pub));
  152. AES c(hash);
  153. std::sort(hash,hash + 48);
  154. c.encrypt(hash,hash);
  155. c.encrypt(hash + 16,hash + 16);
  156. c.encrypt(hash + 32,hash + 32);
  157. SHA384(hash,hash,48);
  158. }
  159. break;
  160. }
  161. return hash;
  162. }
  163. void Identity::hashWithPrivate(uint8_t h[48]) const
  164. {
  165. switch(_type) {
  166. case C25519: SHA384(h,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN); break;
  167. case P384: SHA384(h,&_pub,sizeof(_pub),&_priv,sizeof(_priv)); break;
  168. default: memset(h,0,48);
  169. }
  170. }
  171. unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
  172. {
  173. if (_hasPrivate) {
  174. switch(_type) {
  175. case C25519:
  176. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  177. C25519::sign(_priv.c25519,_pub.c25519,data,len,sig);
  178. return ZT_C25519_SIGNATURE_LEN;
  179. }
  180. case P384:
  181. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  182. // When signing with P-384 we also include the C25519 public key in the hash.
  183. uint8_t h[48];
  184. SHA384(h,data,len,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  185. ECC384ECDSASign(_priv.p384,h,(uint8_t *)sig);
  186. return ZT_ECC384_SIGNATURE_SIZE;
  187. }
  188. }
  189. }
  190. return 0;
  191. }
  192. bool Identity::verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
  193. {
  194. switch(_type) {
  195. case C25519:
  196. return C25519::verify(_pub.c25519,data,len,sig,siglen);
  197. case P384:
  198. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  199. uint8_t h[48];
  200. SHA384(h,data,len,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  201. return ECC384ECDSAVerify(_pub.p384,h,(const uint8_t *)sig);
  202. }
  203. break;
  204. }
  205. return false;
  206. }
  207. bool Identity::agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const
  208. {
  209. uint8_t rawkey[128];
  210. uint8_t h[64];
  211. if (_hasPrivate) {
  212. if (_type == C25519) {
  213. if ((id._type == C25519)||(id._type == P384)) {
  214. // If we are a C25519 key we can agree with another C25519 key or with only the
  215. // C25519 portion of a type 1 P-384 key.
  216. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  217. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  218. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  219. return true;
  220. }
  221. } else if (_type == P384) {
  222. if (id._type == P384) {
  223. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  224. ECC384ECDH(id._pub.p384,_priv.p384,rawkey + ZT_C25519_SHARED_KEY_LEN);
  225. SHA384(h,rawkey,ZT_C25519_SHARED_KEY_LEN + ZT_ECC384_SHARED_SECRET_SIZE);
  226. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  227. return true;
  228. } else if (id._type == C25519) {
  229. // If the other identity is a C25519 identity we can agree using only that type.
  230. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  231. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  232. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  233. return true;
  234. }
  235. }
  236. }
  237. return false;
  238. }
  239. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  240. {
  241. switch(_type) {
  242. case C25519: {
  243. char *p = buf;
  244. Utils::hex10(_address.toInt(),p);
  245. p += 10;
  246. *(p++) = ':';
  247. *(p++) = '0';
  248. *(p++) = ':';
  249. Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
  250. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  251. if ((_hasPrivate)&&(includePrivate)) {
  252. *(p++) = ':';
  253. Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
  254. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  255. }
  256. *p = (char)0;
  257. return buf;
  258. }
  259. case P384: {
  260. char *p = buf;
  261. Utils::hex10(_address.toInt(),p);
  262. p += 10;
  263. *(p++) = ':';
  264. *(p++) = '1';
  265. *(p++) = ':';
  266. int el = Utils::b32e((const uint8_t *)(&_pub),sizeof(_pub),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  267. if (el <= 0) return nullptr;
  268. p += el;
  269. if ((_hasPrivate)&&(includePrivate)) {
  270. *(p++) = ':';
  271. el = Utils::b32e((const uint8_t *)(&_priv),sizeof(_priv),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  272. if (el <= 0) return nullptr;
  273. p += el;
  274. }
  275. *p = (char)0;
  276. return buf;
  277. }
  278. }
  279. return nullptr;
  280. }
  281. bool Identity::fromString(const char *str)
  282. {
  283. _hasPrivate = false;
  284. _hash[0] = 0; // force hash recompute
  285. if (!str) {
  286. _address.zero();
  287. return false;
  288. }
  289. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  290. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  291. _address.zero();
  292. return false;
  293. }
  294. int fno = 0;
  295. char *saveptr = (char *)0;
  296. for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
  297. switch(fno++) {
  298. case 0:
  299. _address = Address(Utils::hexStrToU64(f));
  300. if (_address.isReserved()) {
  301. _address.zero();
  302. return false;
  303. }
  304. break;
  305. case 1:
  306. if ((f[0] == '0')&&(!f[1])) {
  307. _type = C25519;
  308. } else if ((f[0] == '1')&&(!f[1])) {
  309. _type = P384;
  310. } else {
  311. _address.zero();
  312. return false;
  313. }
  314. break;
  315. case 2:
  316. switch(_type) {
  317. case C25519:
  318. if (Utils::unhex(f,strlen(f),_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  319. _address.zero();
  320. return false;
  321. }
  322. break;
  323. case P384:
  324. if (Utils::b32d(f,(uint8_t *)(&_pub),sizeof(_pub)) != sizeof(_pub)) {
  325. _address.zero();
  326. return false;
  327. }
  328. break;
  329. }
  330. break;
  331. case 3:
  332. if (strlen(f) > 1) {
  333. switch(_type) {
  334. case C25519:
  335. if (Utils::unhex(f,strlen(f),_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  336. _address.zero();
  337. return false;
  338. } else {
  339. _hasPrivate = true;
  340. }
  341. break;
  342. case P384:
  343. if (Utils::b32d(f,(uint8_t *)(&_priv),sizeof(_priv)) != sizeof(_priv)) {
  344. _address.zero();
  345. return false;
  346. } else {
  347. _hasPrivate = true;
  348. }
  349. break;
  350. }
  351. break;
  352. }
  353. }
  354. }
  355. if (fno < 3) {
  356. _address.zero();
  357. return false;
  358. }
  359. return true;
  360. }
  361. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool includePrivate) const noexcept
  362. {
  363. _address.copyTo(data);
  364. switch(_type) {
  365. case C25519:
  366. data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
  367. memcpy(data + ZT_ADDRESS_LENGTH + 1,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  368. if ((includePrivate)&&(_hasPrivate)) {
  369. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = ZT_C25519_PRIVATE_KEY_LEN;
  370. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  371. return (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN);
  372. }
  373. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = 0;
  374. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  375. case P384:
  376. data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
  377. memcpy(data + 1 + ZT_ADDRESS_LENGTH,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  378. if ((includePrivate)&&(_hasPrivate)) {
  379. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  380. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,&_priv,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  381. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  382. }
  383. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  384. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  385. }
  386. return -1;
  387. }
  388. int Identity::unmarshal(const uint8_t *data,const int len) noexcept
  389. {
  390. if (len < (ZT_ADDRESS_LENGTH + 1))
  391. return -1;
  392. _hash[0] = 0; // force hash recompute
  393. unsigned int privlen;
  394. switch((_type = (Type)data[ZT_ADDRESS_LENGTH])) {
  395. case C25519:
  396. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1))
  397. return -1;
  398. memcpy(_pub.c25519,data + ZT_ADDRESS_LENGTH + 1,ZT_C25519_PUBLIC_KEY_LEN);
  399. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN];
  400. if (privlen == ZT_C25519_PRIVATE_KEY_LEN) {
  401. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN))
  402. return -1;
  403. _hasPrivate = true;
  404. memcpy(_priv.c25519,data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,ZT_C25519_PRIVATE_KEY_LEN);
  405. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  406. } else if (privlen == 0) {
  407. _hasPrivate = false;
  408. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  409. }
  410. break;
  411. case P384:
  412. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  413. return -1;
  414. memcpy(&_pub,data + ZT_ADDRESS_LENGTH + 1,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  415. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  416. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  417. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  418. return -1;
  419. _hasPrivate = true;
  420. memcpy(&_priv,data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  421. if (!this->locallyValidate()) // for P384 we do this always
  422. return -1;
  423. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  424. } else if (privlen == 0) {
  425. _hasPrivate = false;
  426. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  427. }
  428. break;
  429. }
  430. return -1;
  431. }
  432. } // namespace ZeroTier
  433. extern "C" {
  434. ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type)
  435. {
  436. if ((type != ZT_IDENTITY_TYPE_C25519)&&(type != ZT_IDENTITY_TYPE_P384))
  437. return nullptr;
  438. try {
  439. ZeroTier::Identity *id = new ZeroTier::Identity();
  440. id->generate((ZeroTier::Identity::Type)type);
  441. return reinterpret_cast<ZT_Identity *>(id);
  442. } catch ( ... ) {
  443. return nullptr;
  444. }
  445. }
  446. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  447. {
  448. if (!idStr)
  449. return nullptr;
  450. try {
  451. ZeroTier::Identity *id = new ZeroTier::Identity();
  452. if (!id->fromString(idStr)) {
  453. delete id;
  454. return nullptr;
  455. }
  456. return reinterpret_cast<ZT_Identity *>(id);
  457. } catch ( ... ) {
  458. return nullptr;
  459. }
  460. }
  461. int ZT_Identity_validate(const ZT_Identity *id)
  462. {
  463. if (!id)
  464. return 0;
  465. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  466. }
  467. unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength)
  468. {
  469. if (!id)
  470. return 0;
  471. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  472. return 0;
  473. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data,len,signature,signatureBufferLength);
  474. }
  475. int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen)
  476. {
  477. if ((!id)||(!signature)||(!sigLen))
  478. return 0;
  479. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data,len,signature,sigLen) ? 1 : 0;
  480. }
  481. enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id)
  482. {
  483. if (!id)
  484. return (ZT_Identity_Type)0;
  485. return (enum ZT_Identity_Type)reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  486. }
  487. char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate)
  488. {
  489. if ((!id)||(!buf)||(capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  490. return nullptr;
  491. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0,buf);
  492. return buf;
  493. }
  494. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  495. {
  496. if (!id)
  497. return 0;
  498. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  499. }
  500. uint64_t ZT_Identity_address(const ZT_Identity *id)
  501. {
  502. if (!id)
  503. return 0;
  504. return reinterpret_cast<const ZeroTier::Identity *>(id)->address().toInt();
  505. }
  506. void ZT_Identity_hash(const ZT_Identity *id,uint8_t h[48],int includePrivate)
  507. {
  508. if (includePrivate)
  509. reinterpret_cast<const ZeroTier::Identity *>(id)->hashWithPrivate(h);
  510. else memcpy(h,reinterpret_cast<const ZeroTier::Identity *>(id)->hash(),48);
  511. }
  512. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  513. {
  514. if (id)
  515. delete reinterpret_cast<ZeroTier::Identity *>(id);
  516. }
  517. }