Certificate.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. namespace ZeroTier {
  16. void Certificate::clear()
  17. {
  18. Utils::zero< sizeof(ZT_Certificate) >((ZT_Certificate *)this);
  19. m_identities.clear();
  20. m_locators.clear();
  21. m_strings.clear();
  22. m_serials.clear();
  23. m_subjectIdentities.clear();
  24. m_subjectNetworks.clear();
  25. m_updateUrls.clear();
  26. m_subjectCertificates.clear();
  27. }
  28. Certificate &Certificate::operator=(const ZT_Certificate &apiCert)
  29. {
  30. clear();
  31. Utils::copy< sizeof(ZT_Certificate) >((ZT_Certificate *)this, &apiCert);
  32. return *this;
  33. }
  34. Certificate &Certificate::operator=(const Certificate &cert)
  35. {
  36. *this = *((const ZT_Certificate *)(&cert));
  37. // Zero these since we must explicitly attach all the objects from
  38. // the other certificate to copy them into our containers.
  39. this->subject.identityCount = 0;
  40. this->subject.networkCount = 0;
  41. this->subject.certificateCount = 0;
  42. for (unsigned int i = 0; i < cert.subject.identityCount; ++i) {
  43. if (cert.subject.identities[i].identity) {
  44. if (cert.subject.identities[i].locator)
  45. addSubjectNode(*reinterpret_cast<const Identity *>(cert.subject.identities[i].identity), *reinterpret_cast<const Locator *>(cert.subject.identities[i].locator));
  46. else addSubjectNode(*reinterpret_cast<const Identity *>(cert.subject.identities[i].identity));
  47. }
  48. }
  49. for (unsigned int i = 0; i < cert.subject.networkCount; ++i) {
  50. if (cert.subject.networks[i].id)
  51. addSubjectNetwork(cert.subject.networks[i].id, cert.subject.networks[i].controller);
  52. }
  53. for (unsigned int i = 0; i < cert.subject.certificateCount; ++i) {
  54. if (cert.subject.certificates[i])
  55. addSubjectCertificate(cert.subject.certificates[i]);
  56. }
  57. if (cert.issuer) {
  58. m_identities.push_back(*reinterpret_cast<const Identity *>(cert.issuer));
  59. this->issuer = reinterpret_cast<ZT_Identity *>(&(m_identities.back()));
  60. }
  61. if (cert.updateUrls) {
  62. for (unsigned int i = 0; i < cert.updateUrlCount; ++i) {
  63. if (cert.updateUrls[i])
  64. addUpdateUrl(cert.updateUrls[i]);
  65. }
  66. }
  67. return *this;
  68. }
  69. ZT_Certificate_Identity *Certificate::addSubjectNode(const Identity &id)
  70. {
  71. // Enlarge array of ZT_Certificate_Identity structs and set pointer to potentially reallocated array.
  72. m_subjectIdentities.resize(++this->subject.identityCount);
  73. this->subject.identities = m_subjectIdentities.data();
  74. // Store a local copy of the actual identity.
  75. m_identities.push_back(id);
  76. // Set ZT_Certificate_Identity struct fields to point to local copy of identity.
  77. m_subjectIdentities.back().identity = reinterpret_cast<ZT_Identity *>(&(m_identities.back()));
  78. m_subjectIdentities.back().locator = nullptr;
  79. return &(m_subjectIdentities.back());
  80. }
  81. ZT_Certificate_Identity *Certificate::addSubjectNode(const Identity &id, const Locator &loc)
  82. {
  83. // Add identity as above.
  84. ZT_Certificate_Identity *const n = addSubjectNode(id);
  85. // Store local copy of locator.
  86. m_locators.push_back(loc);
  87. // Set pointer to stored local copy of locator.
  88. n->locator = reinterpret_cast<ZT_Locator *>(&(m_locators.back()));
  89. return n;
  90. }
  91. ZT_Certificate_Network *Certificate::addSubjectNetwork(const uint64_t id, const ZT_Fingerprint &controller)
  92. {
  93. // Enlarge array of ZT_Certificate_Network and set pointer to potentially reallocated array.
  94. m_subjectNetworks.resize(++this->subject.networkCount);
  95. this->subject.networks = m_subjectNetworks.data();
  96. // Set fields in new ZT_Certificate_Network structure.
  97. m_subjectNetworks.back().id = id;
  98. Utils::copy< sizeof(ZT_Fingerprint) >(&(m_subjectNetworks.back().controller), &controller);
  99. return &(m_subjectNetworks.back());
  100. }
  101. void Certificate::addSubjectCertificate(const uint8_t serialNo[ZT_SHA384_DIGEST_SIZE])
  102. {
  103. // Store local copy of serial in m_serials container.
  104. m_serials.push_back(Blob< ZT_SHA384_DIGEST_SIZE >(serialNo));
  105. // Enlarge array of uint8_t pointers, set new pointer to local copy of serial, and set
  106. // certificates to point to potentially reallocated array.
  107. m_subjectCertificates.resize(++this->subject.certificateCount);
  108. m_subjectCertificates.back() = m_serials.back().data;
  109. this->subject.certificates = m_subjectCertificates.data();
  110. }
  111. void Certificate::addUpdateUrl(const char *url)
  112. {
  113. // Store local copy of URL.
  114. m_strings.push_back(url);
  115. // Add pointer to local copy to pointer array and update C structure to point to
  116. // potentially reallocated array.
  117. m_updateUrls.push_back(m_strings.back().c_str());
  118. this->updateUrls = m_updateUrls.data();
  119. this->updateUrlCount = (unsigned int)m_updateUrls.size();
  120. }
  121. Vector< uint8_t > Certificate::encode(const bool omitSignature) const
  122. {
  123. char tmp[256];
  124. Vector< uint8_t > enc;
  125. Dictionary d;
  126. // A Dictionary is used to encode certificates as it's a common and extensible
  127. // format. Custom packed formats are used for credentials as these are smaller
  128. // and faster to marshal/unmarshal.
  129. d.add("f", this->flags);
  130. d.add("v0", this->validity[0]);
  131. d.add("v1", this->validity[1]);
  132. d.add("mP", (uint64_t)this->maxPathLength);
  133. d.add("s.i$", (uint64_t)this->subject.identityCount);
  134. for (unsigned int i = 0; i < this->subject.identityCount; ++i) {
  135. if (this->subject.identities[i].identity)
  136. d.addO(Dictionary::arraySubscript(tmp, "s.i$.i", i), *reinterpret_cast<const Identity *>(this->subject.identities[i].identity));
  137. if (this->subject.identities[i].locator)
  138. d.addO(Dictionary::arraySubscript(tmp, "s.i$.l", i), *reinterpret_cast<const Locator *>(this->subject.identities[i].locator));
  139. }
  140. d.add("s.n$", (uint64_t)this->subject.networkCount);
  141. for (unsigned int i = 0; i < this->subject.networkCount; ++i) {
  142. d.add(Dictionary::arraySubscript(tmp, "s.n$.i", i), this->subject.networks[i].id);
  143. Fingerprint fp(this->subject.networks[i].controller);
  144. d.addO(Dictionary::arraySubscript(tmp, "s.n$.c", i), fp);
  145. }
  146. d.add("s.c$", (uint64_t)this->subject.certificateCount);
  147. for (unsigned int i = 0; i < this->subject.certificateCount; ++i) {
  148. if (this->subject.certificates[i])
  149. d[Dictionary::arraySubscript(tmp, "s.c$", i)].assign(this->subject.certificates[i], this->subject.certificates[i] + ZT_SHA384_DIGEST_SIZE);
  150. }
  151. d.add("s.n.c", this->subject.name.country);
  152. d.add("s.n.o", this->subject.name.organization);
  153. d.add("s.n.u", this->subject.name.unit);
  154. d.add("s.n.l", this->subject.name.locality);
  155. d.add("s.n.p", this->subject.name.province);
  156. d.add("s.n.sA", this->subject.name.streetAddress);
  157. d.add("s.n.pC", this->subject.name.postalCode);
  158. d.add("s.n.cN", this->subject.name.commonName);
  159. d.add("s.n.sN", this->subject.name.serialNo);
  160. d.add("s.n.e", this->subject.name.email);
  161. d.add("s.n.ur", this->subject.name.url);
  162. if (this->issuer)
  163. d.addO("i", *reinterpret_cast<const Identity *>(this->issuer));
  164. d.add("iN.c", this->issuerName.country);
  165. d.add("iN.o", this->issuerName.organization);
  166. d.add("iN.u", this->issuerName.unit);
  167. d.add("iN.l", this->issuerName.locality);
  168. d.add("iN.p", this->issuerName.province);
  169. d.add("iN.sA", this->issuerName.streetAddress);
  170. d.add("iN.pC", this->issuerName.postalCode);
  171. d.add("iN.cN", this->issuerName.commonName);
  172. d.add("iN.sN", this->issuerName.serialNo);
  173. d.add("iN.e", this->issuerName.email);
  174. d.add("iN.ur", this->issuerName.url);
  175. d.add("u$", (uint64_t)this->updateUrlCount);
  176. if (this->updateUrls) {
  177. for (unsigned int i = 0; i < this->updateUrlCount; ++i)
  178. d.add(Dictionary::arraySubscript(tmp, "u$", i), this->updateUrls[i]);
  179. }
  180. if ((!omitSignature) && (this->signatureSize > 0) && (this->signatureSize <= sizeof(this->signature)))
  181. d["si"].assign(this->signature, this->signature + this->signatureSize);
  182. d.encode(enc);
  183. return enc;
  184. }
  185. bool Certificate::decode(const Vector< uint8_t > &data)
  186. {
  187. char tmp[256], tmp2[ZT_CERTIFICATE_MAX_STRING_LENGTH + 1];
  188. clear();
  189. Dictionary d;
  190. if (!d.decode(data.data(), (unsigned int)data.size()))
  191. return false;
  192. this->flags = d.getUI("f");
  193. this->validity[0] = (int64_t)d.getUI("v0");
  194. this->validity[1] = (int64_t)d.getUI("v1");
  195. this->maxPathLength = (unsigned int)d.getUI("mP");
  196. unsigned int cnt = (unsigned int)d.getUI("s.i$");
  197. for (unsigned int i = 0; i < cnt; ++i) {
  198. const Vector< uint8_t > &identityData = d[Dictionary::arraySubscript(tmp, "s.i$.i", i)];
  199. if (identityData.empty())
  200. return false;
  201. Identity id;
  202. if (id.unmarshal(identityData.data(), (unsigned int)identityData.size()) <= 0)
  203. return false;
  204. const Vector< uint8_t > &locatorData = d[Dictionary::arraySubscript(tmp, "s.i$.l", i)];
  205. if (!locatorData.empty()) {
  206. Locator loc;
  207. if (loc.unmarshal(locatorData.data(), (unsigned int)locatorData.size()) <= 0)
  208. return false;
  209. this->addSubjectNode(id, loc);
  210. } else {
  211. this->addSubjectNode(id);
  212. }
  213. }
  214. cnt = (unsigned int)d.getUI("s.n$");
  215. for (unsigned int i = 0; i < cnt; ++i) {
  216. const uint64_t nwid = d.getUI(Dictionary::arraySubscript(tmp, "s.n$.i", i));
  217. const Vector< uint8_t > &fingerprintData = d[Dictionary::arraySubscript(tmp, "s.n$.c", i)];
  218. if ((nwid == 0) || (fingerprintData.empty()))
  219. return false;
  220. Fingerprint fp;
  221. if (fp.unmarshal(fingerprintData.data(), (unsigned int)fingerprintData.size()) <= 0)
  222. return false;
  223. this->addSubjectNetwork(nwid, fp);
  224. }
  225. cnt = (unsigned int)d.getUI("s.c$");
  226. for (unsigned int i=0;i<cnt;++i) {
  227. const Vector< uint8_t > &serial = d[Dictionary::arraySubscript(tmp, "s.c$", i)];
  228. if (serial.size() != ZT_SHA384_DIGEST_SIZE)
  229. return false;
  230. this->addSubjectCertificate(serial.data());
  231. }
  232. d.getS("s.n.c", this->subject.name.country, sizeof(this->subject.name.country));
  233. d.getS("s.n.o", this->subject.name.organization, sizeof(this->subject.name.organization));
  234. d.getS("s.n.u", this->subject.name.unit, sizeof(this->subject.name.unit));
  235. d.getS("s.n.l", this->subject.name.locality, sizeof(this->subject.name.locality));
  236. d.getS("s.n.p", this->subject.name.province, sizeof(this->subject.name.province));
  237. d.getS("s.n.sA", this->subject.name.streetAddress, sizeof(this->subject.name.streetAddress));
  238. d.getS("s.n.pC", this->subject.name.postalCode, sizeof(this->subject.name.postalCode));
  239. d.getS("s.n.cN", this->subject.name.commonName, sizeof(this->subject.name.commonName));
  240. d.getS("s.n.sN", this->subject.name.serialNo, sizeof(this->subject.name.serialNo));
  241. d.getS("s.n.e", this->subject.name.email, sizeof(this->subject.name.email));
  242. d.getS("s.n.ur", this->subject.name.url, sizeof(this->subject.name.url));
  243. const Vector< uint8_t > &issuerData = d["i"];
  244. if (!issuerData.empty()) {
  245. Identity id;
  246. if (id.unmarshal(issuerData.data(), (int)issuerData.size()) > 0) {
  247. m_identities.push_back(id);
  248. this->issuer = reinterpret_cast<const Identity *>(&(m_identities.back()));
  249. }
  250. }
  251. d.getS("iN.c", this->issuerName.country, sizeof(this->issuerName.country));
  252. d.getS("iN.o", this->issuerName.organization, sizeof(this->issuerName.organization));
  253. d.getS("iN.u", this->issuerName.unit, sizeof(this->issuerName.unit));
  254. d.getS("iN.l", this->issuerName.locality, sizeof(this->issuerName.locality));
  255. d.getS("iN.p", this->issuerName.province, sizeof(this->issuerName.province));
  256. d.getS("iN.sA", this->issuerName.streetAddress, sizeof(this->issuerName.streetAddress));
  257. d.getS("iN.pC", this->issuerName.postalCode, sizeof(this->issuerName.postalCode));
  258. d.getS("iN.cN", this->issuerName.commonName, sizeof(this->issuerName.commonName));
  259. d.getS("iN.sN", this->issuerName.serialNo, sizeof(this->issuerName.serialNo));
  260. d.getS("iN.e", this->issuerName.email, sizeof(this->issuerName.email));
  261. d.getS("iN.ur", this->issuerName.url, sizeof(this->issuerName.url));
  262. cnt = (unsigned int)d.getUI("u$");
  263. for (unsigned int i = 0; i < cnt; ++i) {
  264. const char *const url = d.getS(Dictionary::arraySubscript(tmp, "u$", i), tmp2, sizeof(tmp2));
  265. if (url)
  266. addUpdateUrl(tmp2);
  267. else return false;
  268. }
  269. const Vector< uint8_t > &sig = d["si"];
  270. if (sig.size() > sizeof(this->signature))
  271. return false;
  272. Utils::copy(this->signature, sig.data(), (unsigned int)sig.size());
  273. this->signatureSize = (unsigned int)sig.size();
  274. Vector< uint8_t > enc(encode(true));
  275. SHA384(this->serialNo, enc.data(), (unsigned int)enc.size());
  276. return true;
  277. }
  278. bool Certificate::sign(const Identity &issuer)
  279. {
  280. Vector< uint8_t > enc(encode(true));
  281. SHA384(this->serialNo, enc.data(), (unsigned int)enc.size());
  282. return (this->signatureSize = issuer.sign(enc.data(), (unsigned int)enc.size(), this->signature, sizeof(this->signature))) > 0;
  283. }
  284. bool Certificate::verify() const
  285. {
  286. try {
  287. if (this->issuer) {
  288. Vector< uint8_t > enc(encode(true));
  289. return reinterpret_cast<const Identity *>(this->issuer)->verify(enc.data(), (unsigned int)enc.size(), this->signature, this->signatureSize);
  290. }
  291. } catch ( ... ) {}
  292. return false;
  293. }
  294. } // namespace ZeroTier