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