Identity.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. switch(t) {
  81. case C25519: {
  82. char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  83. do {
  84. C25519::generateSatisfying(_v0_identity_generate_cond(digest,genmem),_pub.c25519,_priv.c25519);
  85. _address.setTo(digest + 59); // last 5 bytes are address
  86. } while (_address.isReserved());
  87. delete[] genmem;
  88. _computeHash();
  89. } break;
  90. case P384: {
  91. AES c;
  92. do {
  93. C25519::generate(_pub.c25519,_priv.c25519);
  94. ECC384GenerateKey(_pub.p384,_priv.p384);
  95. SHA384(digest,&_pub,sizeof(_pub));
  96. c.init(digest);
  97. c.encrypt(digest,digest);
  98. c.encrypt(digest + 16,digest + 16);
  99. c.encrypt(digest + 32,digest + 32);
  100. if (digest[47] != 0)
  101. continue;
  102. _address.setTo(digest);
  103. } while (_address.isReserved());
  104. _hash.set(digest); // P384 uses the same hash for hash() and address generation
  105. } break;
  106. default:
  107. return false;
  108. }
  109. return true;
  110. }
  111. bool Identity::locallyValidate() const
  112. {
  113. if ((_address.isReserved())||(!_address))
  114. return false;
  115. switch (_type) {
  116. case C25519:
  117. try {
  118. uint8_t digest[64];
  119. char *genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
  120. _computeMemoryHardHash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
  121. delete [] genmem;
  122. return ((_address == Address(digest + 59))&&(!_address.isReserved())&&(digest[0] < 17));
  123. } catch ( ... ) {}
  124. return false;
  125. case P384:
  126. return ((_hash[47] == 0)&&(Address(_hash.data()) == _address));
  127. }
  128. return false;
  129. }
  130. void Identity::hashWithPrivate(uint8_t h[ZT_IDENTITY_HASH_SIZE]) const
  131. {
  132. if (_hasPrivate) {
  133. switch (_type) {
  134. case C25519:
  135. SHA384(h,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  136. break;
  137. case P384:
  138. SHA384(h,&_pub,sizeof(_pub),&_priv,sizeof(_priv));
  139. break;
  140. }
  141. return;
  142. }
  143. memset(h,0,48);
  144. }
  145. unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
  146. {
  147. if (_hasPrivate) {
  148. switch(_type) {
  149. case C25519:
  150. if (siglen >= ZT_C25519_SIGNATURE_LEN) {
  151. C25519::sign(_priv.c25519,_pub.c25519,data,len,sig);
  152. return ZT_C25519_SIGNATURE_LEN;
  153. }
  154. case P384:
  155. if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
  156. // For P384 we sign SHA384(data | public keys) for added defense against any attack
  157. // that attempted to decouple the two keys in some way. Otherwise this has no impact
  158. // on the security of the signature (unless SHA384 had some serious flaw).
  159. uint8_t h[48];
  160. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  161. ECC384ECDSASign(_priv.p384,h,(uint8_t *)sig);
  162. return ZT_ECC384_SIGNATURE_SIZE;
  163. }
  164. }
  165. }
  166. return 0;
  167. }
  168. bool Identity::verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
  169. {
  170. switch(_type) {
  171. case C25519:
  172. return C25519::verify(_pub.c25519,data,len,sig,siglen);
  173. case P384:
  174. if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
  175. uint8_t h[48];
  176. SHA384(h,data,len,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  177. return ECC384ECDSAVerify(_pub.p384,h,(const uint8_t *)sig);
  178. }
  179. break;
  180. }
  181. return false;
  182. }
  183. bool Identity::agree(const Identity &id,uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) const
  184. {
  185. uint8_t rawkey[128];
  186. uint8_t h[64];
  187. if (_hasPrivate) {
  188. if (_type == C25519) {
  189. if ((id._type == C25519)||(id._type == P384)) {
  190. // If we are a C25519 key we can agree with another C25519 key or with only the
  191. // C25519 portion of a type 1 P-384 key.
  192. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  193. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  194. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  195. return true;
  196. }
  197. } else if (_type == P384) {
  198. if (id._type == P384) {
  199. // For another P384 identity we execute DH agreement with BOTH keys and then
  200. // hash the results together. For those (cough FIPS cough) who only consider
  201. // P384 to be kosher, the C25519 secret can be considered a "salt"
  202. // or something. For those who don't trust P384 this means the privacy of
  203. // your traffic is also protected by C25519.
  204. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  205. ECC384ECDH(id._pub.p384,_priv.p384,rawkey + ZT_C25519_SHARED_KEY_LEN);
  206. SHA384(h,rawkey,ZT_C25519_SHARED_KEY_LEN + ZT_ECC384_SHARED_SECRET_SIZE);
  207. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  208. return true;
  209. } else if (id._type == C25519) {
  210. // If the other identity is a C25519 identity we can agree using only that type.
  211. C25519::agree(_priv.c25519,id._pub.c25519,rawkey);
  212. SHA512(h,rawkey,ZT_C25519_SHARED_KEY_LEN);
  213. memcpy(key,h,ZT_PEER_SECRET_KEY_LENGTH);
  214. return true;
  215. }
  216. }
  217. }
  218. return false;
  219. }
  220. char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
  221. {
  222. char *p = buf;
  223. _address.toString(p);
  224. p += 10;
  225. *(p++) = ':';
  226. switch(_type) {
  227. case C25519: {
  228. *(p++) = '0';
  229. *(p++) = ':';
  230. Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
  231. p += ZT_C25519_PUBLIC_KEY_LEN * 2;
  232. if ((_hasPrivate)&&(includePrivate)) {
  233. *(p++) = ':';
  234. Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
  235. p += ZT_C25519_PRIVATE_KEY_LEN * 2;
  236. }
  237. *p = (char)0;
  238. return buf;
  239. }
  240. case P384: {
  241. *(p++) = '1';
  242. *(p++) = ':';
  243. int el = Utils::b32e((const uint8_t *)(&_pub),sizeof(_pub),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  244. if (el <= 0) return nullptr;
  245. p += el;
  246. if ((_hasPrivate)&&(includePrivate)) {
  247. *(p++) = ':';
  248. el = Utils::b32e((const uint8_t *)(&_priv),sizeof(_priv),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
  249. if (el <= 0) return nullptr;
  250. p += el;
  251. }
  252. *p = (char)0;
  253. return buf;
  254. }
  255. }
  256. return nullptr;
  257. }
  258. bool Identity::fromString(const char *str)
  259. {
  260. _hash.zero();
  261. _hasPrivate = false;
  262. if (!str) {
  263. _address.zero();
  264. return false;
  265. }
  266. char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
  267. if (!Utils::scopy(tmp,sizeof(tmp),str)) {
  268. _address.zero();
  269. return false;
  270. }
  271. int fno = 0;
  272. char *saveptr = (char *)0;
  273. for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
  274. switch(fno++) {
  275. case 0:
  276. _address = Address(Utils::hexStrToU64(f));
  277. if (_address.isReserved()) {
  278. _address.zero();
  279. return false;
  280. }
  281. break;
  282. case 1:
  283. if ((f[0] == '0')&&(!f[1])) {
  284. _type = C25519;
  285. } else if ((f[0] == '1')&&(!f[1])) {
  286. _type = P384;
  287. } else {
  288. _address.zero();
  289. return false;
  290. }
  291. break;
  292. case 2:
  293. switch(_type) {
  294. case C25519:
  295. if (Utils::unhex(f,strlen(f),_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
  296. _address.zero();
  297. return false;
  298. }
  299. break;
  300. case P384:
  301. if (Utils::b32d(f,(uint8_t *)(&_pub),sizeof(_pub)) != sizeof(_pub)) {
  302. _address.zero();
  303. return false;
  304. }
  305. break;
  306. }
  307. break;
  308. case 3:
  309. if (strlen(f) > 1) {
  310. switch(_type) {
  311. case C25519:
  312. if (Utils::unhex(f,strlen(f),_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
  313. _address.zero();
  314. return false;
  315. } else {
  316. _hasPrivate = true;
  317. }
  318. break;
  319. case P384:
  320. if (Utils::b32d(f,(uint8_t *)(&_priv),sizeof(_priv)) != sizeof(_priv)) {
  321. _address.zero();
  322. return false;
  323. } else {
  324. _hasPrivate = true;
  325. }
  326. break;
  327. }
  328. break;
  329. }
  330. }
  331. }
  332. if (fno < 3) {
  333. _address.zero();
  334. return false;
  335. }
  336. _computeHash();
  337. return true;
  338. }
  339. int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool includePrivate) const noexcept
  340. {
  341. _address.copyTo(data);
  342. switch(_type) {
  343. case C25519:
  344. data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
  345. memcpy(data + ZT_ADDRESS_LENGTH + 1,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  346. if ((includePrivate)&&(_hasPrivate)) {
  347. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = ZT_C25519_PRIVATE_KEY_LEN;
  348. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN);
  349. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  350. } else {
  351. data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN] = 0;
  352. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  353. }
  354. case P384:
  355. data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
  356. memcpy(data + ZT_ADDRESS_LENGTH + 1,&_pub,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  357. if ((includePrivate)&&(_hasPrivate)) {
  358. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  359. memcpy(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,&_priv,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  360. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  361. } else {
  362. data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
  363. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  364. }
  365. }
  366. return -1;
  367. }
  368. int Identity::unmarshal(const uint8_t *data,const int len) noexcept
  369. {
  370. _hash.zero();
  371. _hasPrivate = false;
  372. if (len < (ZT_ADDRESS_LENGTH + 1))
  373. return -1;
  374. unsigned int privlen;
  375. switch((_type = (Type)data[ZT_ADDRESS_LENGTH])) {
  376. case C25519:
  377. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1))
  378. return -1;
  379. memcpy(_pub.c25519,data + ZT_ADDRESS_LENGTH + 1,ZT_C25519_PUBLIC_KEY_LEN);
  380. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN];
  381. if (privlen == ZT_C25519_PRIVATE_KEY_LEN) {
  382. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN))
  383. return -1;
  384. _hasPrivate = true;
  385. memcpy(_priv.c25519,data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1,ZT_C25519_PRIVATE_KEY_LEN);
  386. _computeHash();
  387. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1 + ZT_C25519_PRIVATE_KEY_LEN;
  388. } else if (privlen == 0) {
  389. _hasPrivate = false;
  390. _computeHash();
  391. return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_PUBLIC_KEY_LEN + 1;
  392. }
  393. break;
  394. case P384:
  395. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
  396. return -1;
  397. memcpy(&_pub,data + ZT_ADDRESS_LENGTH + 1,ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE);
  398. privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE];
  399. if (privlen == ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE) {
  400. if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE))
  401. return -1;
  402. _hasPrivate = true;
  403. memcpy(&_priv,data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE);
  404. _computeHash();
  405. if (!this->locallyValidate()) // for P384 we do this always
  406. return -1;
  407. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
  408. } else if (privlen == 0) {
  409. _hasPrivate = false;
  410. _computeHash();
  411. if (!this->locallyValidate()) // for P384 we do this always
  412. return -1;
  413. return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1;
  414. }
  415. break;
  416. }
  417. return -1;
  418. }
  419. void Identity::_computeHash()
  420. {
  421. switch(_type) {
  422. default:
  423. _hash.zero();
  424. break;
  425. case C25519:
  426. SHA384(_hash.data(),_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
  427. break;
  428. case P384:
  429. if (!_hash) {
  430. uint8_t *const h = _hash.data();
  431. SHA384(h,&_pub,sizeof(_pub));
  432. AES c(h);
  433. c.encrypt(h,h);
  434. c.encrypt(h + 16,h + 16);
  435. c.encrypt(h + 32,h + 32);
  436. }
  437. break;
  438. }
  439. }
  440. } // namespace ZeroTier
  441. extern "C" {
  442. ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type)
  443. {
  444. if ((type != ZT_IDENTITY_TYPE_C25519)&&(type != ZT_IDENTITY_TYPE_P384))
  445. return nullptr;
  446. try {
  447. ZeroTier::Identity *const id = new ZeroTier::Identity();
  448. id->generate((ZeroTier::Identity::Type)type);
  449. return reinterpret_cast<ZT_Identity *>(id);
  450. } catch ( ... ) {
  451. return nullptr;
  452. }
  453. }
  454. ZT_Identity *ZT_Identity_fromString(const char *idStr)
  455. {
  456. if (!idStr)
  457. return nullptr;
  458. try {
  459. ZeroTier::Identity *const id = new ZeroTier::Identity();
  460. if (!id->fromString(idStr)) {
  461. delete id;
  462. return nullptr;
  463. }
  464. return reinterpret_cast<ZT_Identity *>(id);
  465. } catch ( ... ) {
  466. return nullptr;
  467. }
  468. }
  469. int ZT_Identity_validate(const ZT_Identity *id)
  470. {
  471. if (!id)
  472. return 0;
  473. return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
  474. }
  475. unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength)
  476. {
  477. if (!id)
  478. return 0;
  479. if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
  480. return 0;
  481. return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data,len,signature,signatureBufferLength);
  482. }
  483. int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen)
  484. {
  485. if ((!id)||(!signature)||(!sigLen))
  486. return 0;
  487. return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data,len,signature,sigLen) ? 1 : 0;
  488. }
  489. enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id)
  490. {
  491. if (!id)
  492. return (ZT_Identity_Type)0;
  493. return (enum ZT_Identity_Type)reinterpret_cast<const ZeroTier::Identity *>(id)->type();
  494. }
  495. char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate)
  496. {
  497. if ((!id)||(!buf)||(capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
  498. return nullptr;
  499. reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0,buf);
  500. return buf;
  501. }
  502. int ZT_Identity_hasPrivate(const ZT_Identity *id)
  503. {
  504. if (!id)
  505. return 0;
  506. return reinterpret_cast<const ZeroTier::Identity *>(id)->hasPrivate() ? 1 : 0;
  507. }
  508. uint64_t ZT_Identity_address(const ZT_Identity *id)
  509. {
  510. if (!id)
  511. return 0;
  512. return reinterpret_cast<const ZeroTier::Identity *>(id)->address().toInt();
  513. }
  514. void ZT_Identity_hash(const ZT_Identity *id,uint8_t h[48],int includePrivate)
  515. {
  516. if (includePrivate)
  517. reinterpret_cast<const ZeroTier::Identity *>(id)->hashWithPrivate(h);
  518. else memcpy(h,reinterpret_cast<const ZeroTier::Identity *>(id)->hash().data(),ZT_IDENTITY_HASH_SIZE);
  519. }
  520. ZT_SDK_API void ZT_Identity_delete(ZT_Identity *id)
  521. {
  522. if (id)
  523. delete reinterpret_cast<ZeroTier::Identity *>(id);
  524. }
  525. }