2
0

Certificate.cpp 23 KB

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