Certificate.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 "Certificate.hpp"
  14. #include "SHA512.hpp"
  15. #include "ECC384.hpp"
  16. namespace ZeroTier {
  17. Certificate::Certificate() noexcept
  18. {
  19. ZT_Certificate *const sup = this;
  20. Utils::zero< sizeof(ZT_Certificate) >(sup);
  21. }
  22. Certificate::Certificate(const ZT_Certificate &apiCert)
  23. {
  24. ZT_Certificate *const sup = this;
  25. Utils::copy< sizeof(ZT_Certificate) >(sup, &apiCert);
  26. }
  27. Certificate::Certificate(const Certificate &cert)
  28. { *this = cert; }
  29. Certificate::~Certificate()
  30. {}
  31. Certificate &Certificate::operator=(const ZT_Certificate &cert)
  32. {
  33. m_clear();
  34. ZT_Certificate *const sup = this;
  35. Utils::copy< sizeof(ZT_Certificate) >(sup, &cert);
  36. // Zero these since we must explicitly attach all the objects from
  37. // the other certificate to copy them into our containers.
  38. this->subject.identities = nullptr;
  39. this->subject.identityCount = 0;
  40. this->subject.networks = nullptr;
  41. this->subject.networkCount = 0;
  42. this->subject.certificates = nullptr;
  43. this->subject.certificateCount = 0;
  44. this->subject.updateUrls = nullptr;
  45. this->subject.updateUrlCount = 0;
  46. this->subject.uniqueId = nullptr;
  47. this->subject.uniqueIdProofSignature = nullptr;
  48. this->subject.uniqueIdSize = 0;
  49. this->subject.uniqueIdProofSignatureSize = 0;
  50. this->extendedAttributes = nullptr;
  51. this->extendedAttributesSize = 0;
  52. this->issuer = nullptr;
  53. this->signature = nullptr;
  54. this->signatureSize = 0;
  55. for (unsigned int i = 0; i < cert.subject.identityCount; ++i) {
  56. if (cert.subject.identities[i].identity) {
  57. if (cert.subject.identities[i].locator)
  58. addSubjectIdentity(*reinterpret_cast<const Identity *>(cert.subject.identities[i].identity), *reinterpret_cast<const Locator *>(cert.subject.identities[i].locator));
  59. else addSubjectIdentity(*reinterpret_cast<const Identity *>(cert.subject.identities[i].identity));
  60. }
  61. }
  62. for (unsigned int i = 0; i < cert.subject.networkCount; ++i) {
  63. if (cert.subject.networks[i].id)
  64. addSubjectNetwork(cert.subject.networks[i].id, cert.subject.networks[i].controller);
  65. }
  66. for (unsigned int i = 0; i < cert.subject.certificateCount; ++i) {
  67. if (cert.subject.certificates[i])
  68. addSubjectCertificate(cert.subject.certificates[i]);
  69. }
  70. if (cert.subject.updateUrls) {
  71. for (unsigned int i = 0; i < cert.subject.updateUrlCount; ++i) {
  72. if (cert.subject.updateUrls[i])
  73. addSubjectUpdateUrl(cert.subject.updateUrls[i]);
  74. }
  75. }
  76. if ((cert.subject.uniqueId) && (cert.subject.uniqueIdSize > 0)) {
  77. m_subjectUniqueId.assign(cert.subject.uniqueId, cert.subject.uniqueId + cert.subject.uniqueIdSize);
  78. this->subject.uniqueId = m_subjectUniqueId.data();
  79. this->subject.uniqueIdSize = (unsigned int)m_subjectUniqueId.size();
  80. }
  81. if ((cert.subject.uniqueIdProofSignature) && (cert.subject.uniqueIdProofSignatureSize > 0)) {
  82. m_subjectUniqueIdProofSignature.assign(cert.subject.uniqueIdProofSignature, cert.subject.uniqueIdProofSignature + cert.subject.uniqueIdProofSignatureSize);
  83. this->subject.uniqueIdProofSignature = m_subjectUniqueIdProofSignature.data();
  84. this->subject.uniqueIdProofSignatureSize = (unsigned int)m_subjectUniqueIdProofSignature.size();
  85. }
  86. if (cert.issuer) {
  87. m_identities.push_back(*reinterpret_cast<const Identity *>(cert.issuer));
  88. this->issuer = &(m_identities.back());
  89. }
  90. if ((cert.extendedAttributes) && (cert.extendedAttributesSize > 0)) {
  91. m_extendedAttributes.assign(cert.extendedAttributes, cert.extendedAttributes + cert.extendedAttributesSize);
  92. this->extendedAttributes = m_extendedAttributes.data();
  93. this->extendedAttributesSize = (unsigned int)m_extendedAttributes.size();
  94. }
  95. if ((cert.signature) && (cert.signatureSize > 0)) {
  96. m_signature.assign(cert.signature, cert.signature + cert.signatureSize);
  97. this->signature = m_signature.data();
  98. this->signatureSize = (unsigned int)m_signature.size();
  99. }
  100. return *this;
  101. }
  102. ZT_Certificate_Identity *Certificate::addSubjectIdentity(const Identity &id)
  103. {
  104. // Enlarge array of ZT_Certificate_Identity structs and set pointer to potentially reallocated array.
  105. m_subjectIdentities.resize(++this->subject.identityCount);
  106. this->subject.identities = m_subjectIdentities.data();
  107. // Store a local copy of the actual identity.
  108. m_identities.push_back(id);
  109. // Set ZT_Certificate_Identity struct fields to point to local copy of identity.
  110. m_subjectIdentities.back().identity = &(m_identities.back());
  111. m_subjectIdentities.back().locator = nullptr;
  112. return &(m_subjectIdentities.back());
  113. }
  114. ZT_Certificate_Identity *Certificate::addSubjectIdentity(const Identity &id, const Locator &loc)
  115. {
  116. // Add identity as above.
  117. ZT_Certificate_Identity *const n = addSubjectIdentity(id);
  118. // Store local copy of locator.
  119. m_locators.push_back(loc);
  120. // Set pointer to stored local copy of locator.
  121. n->locator = &(m_locators.back());
  122. return n;
  123. }
  124. ZT_Certificate_Network *Certificate::addSubjectNetwork(const uint64_t id, const ZT_Fingerprint &controller)
  125. {
  126. // Enlarge array of ZT_Certificate_Network and set pointer to potentially reallocated array.
  127. m_subjectNetworks.resize(++this->subject.networkCount);
  128. this->subject.networks = m_subjectNetworks.data();
  129. // Set fields in new ZT_Certificate_Network structure.
  130. m_subjectNetworks.back().id = id;
  131. Utils::copy< sizeof(ZT_Fingerprint) >(&(m_subjectNetworks.back().controller), &controller);
  132. return &(m_subjectNetworks.back());
  133. }
  134. void Certificate::addSubjectCertificate(const uint8_t serialNo[ZT_SHA384_DIGEST_SIZE])
  135. {
  136. // Store local copy of serial in m_serials container.
  137. m_serials.push_back(SHA384Hash(serialNo));
  138. // Enlarge array of uint8_t pointers, set new pointer to local copy of serial, and set
  139. // certificates to point to potentially reallocated array.
  140. m_subjectCertificates.resize(++this->subject.certificateCount);
  141. m_subjectCertificates.back() = m_serials.back().bytes();
  142. this->subject.certificates = m_subjectCertificates.data();
  143. }
  144. void Certificate::addSubjectUpdateUrl(const char *url)
  145. {
  146. // Store local copy of URL.
  147. m_strings.push_back(url);
  148. // Add pointer to local copy to pointer array and update C structure to point to
  149. // potentially reallocated array.
  150. m_updateUrls.push_back(m_strings.back().c_str());
  151. this->subject.updateUrls = m_updateUrls.data();
  152. this->subject.updateUrlCount = (unsigned int)m_updateUrls.size();
  153. }
  154. void Certificate::setExtendedAttributes(const Dictionary &x)
  155. {
  156. m_extendedAttributes.clear();
  157. x.encode(m_extendedAttributes);
  158. this->extendedAttributes = m_extendedAttributes.data();
  159. this->extendedAttributesSize = (unsigned int)m_extendedAttributes.size();
  160. }
  161. Vector< uint8_t > Certificate::encode(const bool omitSignature) const
  162. {
  163. Vector< uint8_t > enc;
  164. Dictionary d;
  165. // A Dictionary is used to encode certificates as it's a common and extensible
  166. // format. Custom packed formats are used for credentials as these are smaller
  167. // and faster to marshal/unmarshal.
  168. if (this->flags != 0)
  169. d.add("f", this->flags);
  170. d.add("t", (uint64_t)this->timestamp);
  171. d.add("v#0", (uint64_t)this->validity[0]);
  172. d.add("v#1", (uint64_t)this->validity[1]);
  173. if ((this->extendedAttributes) && (this->extendedAttributesSize > 0))
  174. d["x"].assign(this->extendedAttributes, this->extendedAttributes + this->extendedAttributesSize);
  175. d.add("mP", (uint64_t)this->maxPathLength);
  176. m_encodeSubject(this->subject, d, false);
  177. if (this->issuer)
  178. d.addO("i", *reinterpret_cast<const Identity *>(this->issuer));
  179. if (this->issuerName.country[0])
  180. d.add("iN.c", this->issuerName.country);
  181. if (this->issuerName.organization[0])
  182. d.add("iN.o", this->issuerName.organization);
  183. if (this->issuerName.unit[0])
  184. d.add("iN.u", this->issuerName.unit);
  185. if (this->issuerName.locality[0])
  186. d.add("iN.l", this->issuerName.locality);
  187. if (this->issuerName.province[0])
  188. d.add("iN.p", this->issuerName.province);
  189. if (this->issuerName.streetAddress[0])
  190. d.add("iN.sA", this->issuerName.streetAddress);
  191. if (this->issuerName.postalCode[0])
  192. d.add("iN.pC", this->issuerName.postalCode);
  193. if (this->issuerName.commonName[0])
  194. d.add("iN.cN", this->issuerName.commonName);
  195. if (this->issuerName.serialNo[0])
  196. d.add("iN.sN", this->issuerName.serialNo);
  197. if (this->issuerName.email[0])
  198. d.add("iN.e", this->issuerName.email);
  199. if (this->issuerName.url[0])
  200. d.add("iN.ur", this->issuerName.url);
  201. if (this->issuerName.host[0])
  202. d.add("iN.h", this->issuerName.host);
  203. if ((this->extendedAttributes) && (this->extendedAttributesSize > 0))
  204. d["x"].assign(this->extendedAttributes, this->extendedAttributes + this->extendedAttributesSize);
  205. if ((!omitSignature) && (this->signatureSize > 0) && (this->signature))
  206. d["si"].assign(this->signature, this->signature + this->signatureSize);
  207. d.encode(enc);
  208. return enc;
  209. }
  210. bool Certificate::decode(const Vector< uint8_t > &data)
  211. {
  212. char tmp[256], tmp2[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  213. m_clear();
  214. Dictionary d;
  215. if (!d.decode(data.data(), (unsigned int)data.size()))
  216. return false;
  217. this->flags = d.getUI("f");
  218. this->timestamp = (int64_t)d.getUI("t");
  219. this->validity[0] = (int64_t)d.getUI("v#0");
  220. this->validity[1] = (int64_t)d.getUI("v#1");
  221. this->maxPathLength = (unsigned int)d.getUI("mP");
  222. this->subject.timestamp = (int64_t)d.getUI("s.t");
  223. unsigned int cnt = (unsigned int)d.getUI("s.i$");
  224. for (unsigned int i = 0; i < cnt; ++i) {
  225. const Vector< uint8_t > &identityData = d[Dictionary::arraySubscript(tmp, "s.i$.i", i)];
  226. if (identityData.empty())
  227. return false;
  228. Identity id;
  229. if (id.unmarshal(identityData.data(), (unsigned int)identityData.size()) <= 0)
  230. return false;
  231. const Vector< uint8_t > &locatorData = d[Dictionary::arraySubscript(tmp, "s.i$.l", i)];
  232. if (!locatorData.empty()) {
  233. Locator loc;
  234. if (loc.unmarshal(locatorData.data(), (unsigned int)locatorData.size()) <= 0)
  235. return false;
  236. this->addSubjectIdentity(id, loc);
  237. } else {
  238. this->addSubjectIdentity(id);
  239. }
  240. }
  241. cnt = (unsigned int)d.getUI("s.n$");
  242. for (unsigned int i = 0; i < cnt; ++i) {
  243. const uint64_t nwid = d.getUI(Dictionary::arraySubscript(tmp, "s.n$.i", i));
  244. const Vector< uint8_t > &fingerprintData = d[Dictionary::arraySubscript(tmp, "s.n$.c", i)];
  245. if ((nwid == 0) || (fingerprintData.empty()))
  246. return false;
  247. Fingerprint fp;
  248. if (fp.unmarshal(fingerprintData.data(), (unsigned int)fingerprintData.size()) <= 0)
  249. return false;
  250. this->addSubjectNetwork(nwid, fp);
  251. }
  252. cnt = (unsigned int)d.getUI("s.c$");
  253. for (unsigned int i = 0; i < cnt; ++i) {
  254. const Vector< uint8_t > &serial = d[Dictionary::arraySubscript(tmp, "s.c$", i)];
  255. if (serial.size() != ZT_SHA384_DIGEST_SIZE)
  256. return false;
  257. this->addSubjectCertificate(serial.data());
  258. }
  259. cnt = (unsigned int)d.getUI("s.u$");
  260. for (unsigned int i = 0; i < cnt; ++i)
  261. addSubjectUpdateUrl(d.getS(Dictionary::arraySubscript(tmp, "s.u$", i), tmp, sizeof(tmp)));
  262. d.getS("s.n.sN", this->subject.name.serialNo, sizeof(this->subject.name.serialNo));
  263. d.getS("s.n.cN", this->subject.name.commonName, sizeof(this->subject.name.commonName));
  264. d.getS("s.n.c", this->subject.name.country, sizeof(this->subject.name.country));
  265. d.getS("s.n.o", this->subject.name.organization, sizeof(this->subject.name.organization));
  266. d.getS("s.n.u", this->subject.name.unit, sizeof(this->subject.name.unit));
  267. d.getS("s.n.l", this->subject.name.locality, sizeof(this->subject.name.locality));
  268. d.getS("s.n.p", this->subject.name.province, sizeof(this->subject.name.province));
  269. d.getS("s.n.sA", this->subject.name.streetAddress, sizeof(this->subject.name.streetAddress));
  270. d.getS("s.n.pC", this->subject.name.postalCode, sizeof(this->subject.name.postalCode));
  271. d.getS("s.n.e", this->subject.name.email, sizeof(this->subject.name.email));
  272. d.getS("s.n.ur", this->subject.name.url, sizeof(this->subject.name.url));
  273. d.getS("s.n.h", this->subject.name.host, sizeof(this->subject.name.host));
  274. m_subjectUniqueId = d["s.uI"];
  275. if (!m_subjectUniqueId.empty()) {
  276. this->subject.uniqueId = m_subjectUniqueId.data();
  277. this->subject.uniqueIdSize = (unsigned int)m_subjectUniqueId.size();
  278. }
  279. m_subjectUniqueIdProofSignature = d["s.uS"];
  280. if (!m_subjectUniqueIdProofSignature.empty()) {
  281. this->subject.uniqueIdProofSignature = m_subjectUniqueIdProofSignature.data();
  282. this->subject.uniqueIdProofSignatureSize = (unsigned int)m_subjectUniqueIdProofSignature.size();
  283. }
  284. const Vector< uint8_t > &issuerData = d["i"];
  285. if (!issuerData.empty()) {
  286. Identity id;
  287. if (id.unmarshal(issuerData.data(), (int)issuerData.size()) > 0) {
  288. m_identities.push_back(id);
  289. this->issuer = reinterpret_cast<const Identity *>(&(m_identities.back()));
  290. }
  291. }
  292. d.getS("iN.sN", this->issuerName.serialNo, sizeof(this->issuerName.serialNo));
  293. d.getS("iN.cN", this->issuerName.commonName, sizeof(this->issuerName.commonName));
  294. d.getS("iN.c", this->issuerName.country, sizeof(this->issuerName.country));
  295. d.getS("iN.o", this->issuerName.organization, sizeof(this->issuerName.organization));
  296. d.getS("iN.u", this->issuerName.unit, sizeof(this->issuerName.unit));
  297. d.getS("iN.l", this->issuerName.locality, sizeof(this->issuerName.locality));
  298. d.getS("iN.p", this->issuerName.province, sizeof(this->issuerName.province));
  299. d.getS("iN.sA", this->issuerName.streetAddress, sizeof(this->issuerName.streetAddress));
  300. d.getS("iN.pC", this->issuerName.postalCode, sizeof(this->issuerName.postalCode));
  301. d.getS("iN.e", this->issuerName.email, sizeof(this->issuerName.email));
  302. d.getS("iN.ur", this->issuerName.url, sizeof(this->issuerName.url));
  303. d.getS("iN.h", this->issuerName.host, sizeof(this->issuerName.host));
  304. cnt = (unsigned int)d.getUI("u$");
  305. for (unsigned int i = 0; i < cnt; ++i) {
  306. const char *const url = d.getS(Dictionary::arraySubscript(tmp, "u$", i), tmp2, sizeof(tmp2));
  307. if (url)
  308. addSubjectUpdateUrl(tmp2);
  309. else return false;
  310. }
  311. m_extendedAttributes = d["x"];
  312. if (!m_extendedAttributes.empty()) {
  313. this->extendedAttributes = m_extendedAttributes.data();
  314. this->extendedAttributesSize = (unsigned int)m_extendedAttributes.size();
  315. }
  316. m_signature = d["si"];
  317. if (!m_signature.empty()) {
  318. this->signature = m_signature.data();
  319. this->signatureSize = (unsigned int)m_signature.size();
  320. }
  321. const Vector< uint8_t > enc(encode(true));
  322. SHA384(this->serialNo, enc.data(), (unsigned int)enc.size());
  323. return true;
  324. }
  325. bool Certificate::sign(const Identity &issuer)
  326. {
  327. Vector< uint8_t > enc(encode(true));
  328. SHA384(this->serialNo, enc.data(), (unsigned int)enc.size());
  329. uint8_t sig[ZT_SIGNATURE_BUFFER_SIZE];
  330. const unsigned int sigSize = issuer.sign(enc.data(), (unsigned int)enc.size(), sig, sizeof(sig));
  331. if (sigSize > 0) {
  332. m_signature.assign(sig, sig + sigSize);
  333. this->signature = m_signature.data();
  334. this->signatureSize = sigSize;
  335. return true;
  336. }
  337. m_signature.clear();
  338. this->signature = nullptr;
  339. this->signatureSize = 0;
  340. return false;
  341. }
  342. ZT_CertificateError Certificate::verify() const
  343. {
  344. try {
  345. if (this->issuer) {
  346. const Vector< uint8_t > enc(encode(true));
  347. if (!reinterpret_cast<const Identity *>(this->issuer)->verify(enc.data(), (unsigned int)enc.size(), this->signature, this->signatureSize))
  348. return ZT_CERTIFICATE_ERROR_INVALID_PRIMARY_SIGNATURE;
  349. } else {
  350. return ZT_CERTIFICATE_ERROR_INVALID_PRIMARY_SIGNATURE;
  351. }
  352. if (this->subject.uniqueIdProofSignatureSize > 0) {
  353. if (
  354. (this->subject.uniqueIdProofSignatureSize != ZT_ECC384_SIGNATURE_SIZE) ||
  355. (this->subject.uniqueIdSize != (ZT_ECC384_PUBLIC_KEY_SIZE + 1)) ||
  356. (this->subject.uniqueId[0] != ZT_CERTIFICATE_UNIQUE_ID_PUBLIC_KEY_TYPE_NIST_P_384))
  357. return ZT_CERTIFICATE_ERROR_INVALID_UNIQUE_ID_PROOF;
  358. Dictionary tmp;
  359. m_encodeSubject(this->subject, tmp, true);
  360. Vector< uint8_t > enc;
  361. tmp.encode(enc);
  362. uint8_t h[ZT_SHA384_DIGEST_SIZE];
  363. SHA384(h, enc.data(), (unsigned int)enc.size());
  364. if (!ECC384ECDSAVerify(this->subject.uniqueId + 1, h, this->subject.uniqueIdProofSignature))
  365. return ZT_CERTIFICATE_ERROR_INVALID_UNIQUE_ID_PROOF;
  366. } else if (this->subject.uniqueIdSize > 0) {
  367. return ZT_CERTIFICATE_ERROR_INVALID_UNIQUE_ID_PROOF;
  368. }
  369. for (unsigned int i = 0; i < this->subject.identityCount; ++i) {
  370. if (!this->subject.identities[i].identity)
  371. return ZT_CERTIFICATE_ERROR_MISSING_REQUIRED_FIELDS;
  372. if (!reinterpret_cast<const Identity *>(this->subject.identities[i].identity)->locallyValidate())
  373. return ZT_CERTIFICATE_ERROR_INVALID_IDENTITY;
  374. if (this->subject.identities[i].locator) {
  375. if (!reinterpret_cast<const Locator *>(this->subject.identities[i].locator)->verify(*reinterpret_cast<const Identity *>(this->subject.identities[i].identity)))
  376. return ZT_CERTIFICATE_ERROR_INVALID_COMPONENT_SIGNATURE;
  377. }
  378. }
  379. for (unsigned int i = 0; i < this->subject.networkCount; ++i) {
  380. if (!this->subject.networks[i].id)
  381. return ZT_CERTIFICATE_ERROR_MISSING_REQUIRED_FIELDS;
  382. }
  383. if (this->subject.updateUrlCount) {
  384. if (!this->subject.updateUrls)
  385. return ZT_CERTIFICATE_ERROR_INVALID_FORMAT;
  386. for (unsigned int i = 0; i < this->subject.updateUrlCount; ++i) {
  387. if (!this->subject.updateUrls[i])
  388. return ZT_CERTIFICATE_ERROR_MISSING_REQUIRED_FIELDS;
  389. }
  390. } else if (this->subject.updateUrls) {
  391. return ZT_CERTIFICATE_ERROR_INVALID_FORMAT;
  392. }
  393. } catch (...) {}
  394. return ZT_CERTIFICATE_ERROR_NONE;
  395. }
  396. bool Certificate::setSubjectUniqueId(const uint8_t uniqueId[ZT_CERTIFICATE_UNIQUE_ID_SIZE_TYPE_NIST_P_384], const uint8_t uniqueIdPrivate[ZT_CERTIFICATE_UNIQUE_ID_PRIVATE_KEY_SIZE_TYPE_NIST_P_384])
  397. {
  398. m_subjectUniqueId.assign(uniqueId, uniqueId + ZT_CERTIFICATE_UNIQUE_ID_SIZE_TYPE_NIST_P_384);
  399. this->subject.uniqueId = m_subjectUniqueId.data();
  400. this->subject.uniqueIdSize = ZT_CERTIFICATE_UNIQUE_ID_SIZE_TYPE_NIST_P_384;
  401. Dictionary d;
  402. m_encodeSubject(this->subject, d, true);
  403. Vector< uint8_t > enc;
  404. d.encode(enc);
  405. uint8_t h[ZT_SHA384_DIGEST_SIZE];
  406. SHA384(h, enc.data(), (unsigned int)enc.size());
  407. m_subjectUniqueIdProofSignature.resize(ZT_ECC384_SIGNATURE_SIZE);
  408. ECC384ECDSASign(uniqueIdPrivate, h, m_subjectUniqueIdProofSignature.data());
  409. this->subject.uniqueIdProofSignature = m_subjectUniqueIdProofSignature.data();
  410. this->subject.uniqueIdProofSignatureSize = ZT_ECC384_SIGNATURE_SIZE;
  411. return true;
  412. }
  413. void Certificate::m_clear()
  414. {
  415. ZT_Certificate *const sup = this;
  416. Utils::zero< sizeof(ZT_Certificate) >(sup);
  417. m_identities.clear();
  418. m_locators.clear();
  419. m_strings.clear();
  420. m_serials.clear();
  421. m_subjectIdentities.clear();
  422. m_subjectNetworks.clear();
  423. m_updateUrls.clear();
  424. m_subjectCertificates.clear();
  425. m_extendedAttributes.clear();
  426. m_subjectUniqueId.clear();
  427. m_subjectUniqueIdProofSignature.clear();
  428. m_signature.clear();
  429. }
  430. void Certificate::m_encodeSubject(const ZT_Certificate_Subject &s, Dictionary &d, bool omitUniqueIdProofSignature)
  431. {
  432. char tmp[64];
  433. d.add("s.t", (uint64_t)s.timestamp);
  434. d.add("s.i$", (uint64_t)s.identityCount);
  435. for (unsigned int i = 0; i < s.identityCount; ++i) {
  436. if (s.identities[i].identity)
  437. d.addO(Dictionary::arraySubscript(tmp, "s.i$.i", i), *reinterpret_cast<const Identity *>(s.identities[i].identity));
  438. if (s.identities[i].locator)
  439. d.addO(Dictionary::arraySubscript(tmp, "s.i$.l", i), *reinterpret_cast<const Locator *>(s.identities[i].locator));
  440. }
  441. d.add("s.n$", (uint64_t)s.networkCount);
  442. for (unsigned int i = 0; i < s.networkCount; ++i) {
  443. d.add(Dictionary::arraySubscript(tmp, "s.n$.i", i), s.networks[i].id);
  444. Fingerprint fp(s.networks[i].controller);
  445. d.addO(Dictionary::arraySubscript(tmp, "s.n$.c", i), fp);
  446. }
  447. d.add("s.c$", (uint64_t)s.certificateCount);
  448. for (unsigned int i = 0; i < s.certificateCount; ++i) {
  449. if (s.certificates[i])
  450. d[Dictionary::arraySubscript(tmp, "s.c$", i)].assign(s.certificates[i], s.certificates[i] + ZT_SHA384_DIGEST_SIZE);
  451. }
  452. d.add("s.u$", (uint64_t)s.updateUrlCount);
  453. if (s.updateUrls) {
  454. for (unsigned int i = 0; i < s.updateUrlCount; ++i)
  455. d.add(Dictionary::arraySubscript(tmp, "s.u$", i), s.updateUrls[i]);
  456. }
  457. if (s.name.country[0])
  458. d.add("s.n.c", s.name.country);
  459. if (s.name.organization[0])
  460. d.add("s.n.o", s.name.organization);
  461. if (s.name.unit[0])
  462. d.add("s.n.u", s.name.unit);
  463. if (s.name.locality[0])
  464. d.add("s.n.l", s.name.locality);
  465. if (s.name.province[0])
  466. d.add("s.n.p", s.name.province);
  467. if (s.name.streetAddress[0])
  468. d.add("s.n.sA", s.name.streetAddress);
  469. if (s.name.postalCode[0])
  470. d.add("s.n.pC", s.name.postalCode);
  471. if (s.name.commonName[0])
  472. d.add("s.n.cN", s.name.commonName);
  473. if (s.name.serialNo[0])
  474. d.add("s.n.sN", s.name.serialNo);
  475. if (s.name.email[0])
  476. d.add("s.n.e", s.name.email);
  477. if (s.name.url[0])
  478. d.add("s.n.ur", s.name.url);
  479. if (s.name.host[0])
  480. d.add("s.n.h", s.name.host);
  481. if ((s.uniqueIdSize > 0) && (s.uniqueId != nullptr))
  482. d["s.uI"].assign(s.uniqueId, s.uniqueId + s.uniqueIdSize);
  483. if ((!omitUniqueIdProofSignature) && (s.uniqueIdProofSignatureSize > 0) && (s.uniqueIdProofSignature != nullptr))
  484. d["s.uS"].assign(s.uniqueIdProofSignature, s.uniqueIdProofSignature + s.uniqueIdProofSignatureSize);
  485. }
  486. } // namespace ZeroTier