Certificate.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Certificate.h"
  7. #include <AK/Debug.h>
  8. #include <AK/IPv4Address.h>
  9. #include <LibCrypto/ASN1/ASN1.h>
  10. #include <LibCrypto/ASN1/DER.h>
  11. #include <LibCrypto/ASN1/PEM.h>
  12. namespace TLS {
  13. constexpr static Array<int, 4>
  14. common_name_oid { 2, 5, 4, 3 },
  15. country_name_oid { 2, 5, 4, 6 },
  16. locality_name_oid { 2, 5, 4, 7 },
  17. organization_name_oid { 2, 5, 4, 10 },
  18. organizational_unit_name_oid { 2, 5, 4, 11 };
  19. constexpr static Array<int, 7>
  20. rsa_encryption_oid { 1, 2, 840, 113549, 1, 1, 1 },
  21. rsa_md5_encryption_oid { 1, 2, 840, 113549, 1, 1, 4 },
  22. rsa_sha1_encryption_oid { 1, 2, 840, 113549, 1, 1, 5 },
  23. rsa_sha256_encryption_oid { 1, 2, 840, 113549, 1, 1, 11 },
  24. rsa_sha384_encryption_oid { 1, 2, 840, 113549, 1, 1, 12 },
  25. rsa_sha512_encryption_oid { 1, 2, 840, 113549, 1, 1, 13 };
  26. constexpr static Array<int, 4>
  27. key_usage_oid { 2, 5, 29, 15 },
  28. subject_alternative_name_oid { 2, 5, 29, 17 },
  29. basic_constraints_oid { 2, 5, 29, 19 };
  30. Optional<Certificate> Certificate::parse_asn1(ReadonlyBytes buffer, bool)
  31. {
  32. #define ENTER_SCOPE_WITHOUT_TYPECHECK(scope) \
  33. do { \
  34. if (auto result = decoder.enter(); result.is_error()) { \
  35. dbgln_if(TLS_DEBUG, "Failed to enter object (" scope "): {}", result.error()); \
  36. return {}; \
  37. } \
  38. } while (0)
  39. #define ENTER_SCOPE_OR_FAIL(kind_name, scope) \
  40. do { \
  41. if (auto tag = decoder.peek(); tag.is_error() || tag.value().kind != Crypto::ASN1::Kind::kind_name) { \
  42. if constexpr (TLS_DEBUG) { \
  43. if (tag.is_error()) \
  44. dbgln(scope " data was invalid: {}", tag.error()); \
  45. else \
  46. dbgln(scope " data was not of kind " #kind_name); \
  47. } \
  48. return {}; \
  49. } \
  50. ENTER_SCOPE_WITHOUT_TYPECHECK(scope); \
  51. } while (0)
  52. #define EXIT_SCOPE(scope) \
  53. do { \
  54. if (auto error = decoder.leave(); error.is_error()) { \
  55. dbgln_if(TLS_DEBUG, "Error while exiting scope " scope ": {}", error.error()); \
  56. return {}; \
  57. } \
  58. } while (0)
  59. #define ENSURE_OBJECT_KIND(_kind_name, scope) \
  60. do { \
  61. if (auto tag = decoder.peek(); tag.is_error() || tag.value().kind != Crypto::ASN1::Kind::_kind_name) { \
  62. if constexpr (TLS_DEBUG) { \
  63. if (tag.is_error()) \
  64. dbgln(scope " data was invalid: {}", tag.error()); \
  65. else \
  66. dbgln(scope " data was not of kind " #_kind_name ", it was {}", Crypto::ASN1::kind_name(tag.value().kind)); \
  67. } \
  68. return {}; \
  69. } \
  70. } while (0)
  71. #define READ_OBJECT_OR_FAIL(kind_name, type_name, value_name, scope) \
  72. auto value_name##_result = decoder.read<type_name>(Crypto::ASN1::Class::Universal, Crypto::ASN1::Kind::kind_name); \
  73. if (value_name##_result.is_error()) { \
  74. dbgln_if(TLS_DEBUG, scope " read of kind " #kind_name " failed: {}", value_name##_result.error()); \
  75. return {}; \
  76. } \
  77. auto value_name = value_name##_result.release_value();
  78. #define DROP_OBJECT_OR_FAIL(scope) \
  79. do { \
  80. if (auto error = decoder.drop(); error.is_error()) { \
  81. dbgln_if(TLS_DEBUG, scope " read failed: {}", error.error()); \
  82. } \
  83. } while (0)
  84. Certificate certificate;
  85. auto copy_buffer_result = ByteBuffer::copy(buffer.data(), buffer.size());
  86. if (copy_buffer_result.is_error())
  87. return {};
  88. certificate.original_asn1 = copy_buffer_result.release_value();
  89. Crypto::ASN1::Decoder decoder { buffer };
  90. // Certificate ::= Sequence {
  91. // certificate TBSCertificate,
  92. // signature_algorithm AlgorithmIdentifier,
  93. // signature_value BitString
  94. // }
  95. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate");
  96. // TBSCertificate ::= Sequence {
  97. // version (0) EXPLICIT Version DEFAULT v1,
  98. // serial_number CertificateSerialNumber,
  99. // signature AlgorithmIdentifier,
  100. // issuer Name,
  101. // validity Validity,
  102. // subject Name,
  103. // subject_public_key_info SubjectPublicKeyInfo,
  104. // issuer_unique_id (1) IMPLICIT UniqueIdentifier OPTIONAL (if present, version > v1),
  105. // subject_unique_id (2) IMPLICIT UniqueIdentifier OPTIONAL (if present, version > v1),
  106. // extensions (3) EXPLICIT Extensions OPTIONAL (if present, version > v2)
  107. // }
  108. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate");
  109. // version
  110. {
  111. // Version :: Integer { v1(0), v2(1), v3(2) } (Optional)
  112. if (auto tag = decoder.peek(); !tag.is_error() && tag.value().type == Crypto::ASN1::Type::Constructed) {
  113. ENTER_SCOPE_WITHOUT_TYPECHECK("Certificate::version");
  114. READ_OBJECT_OR_FAIL(Integer, Crypto::UnsignedBigInteger, value, "Certificate::version");
  115. if (!(value < 3)) {
  116. dbgln_if(TLS_DEBUG, "Certificate::version Invalid value for version: {}", value.to_base_deprecated(10));
  117. return {};
  118. }
  119. certificate.version = value.words()[0];
  120. EXIT_SCOPE("Certificate::version");
  121. } else {
  122. certificate.version = 0;
  123. }
  124. }
  125. // serial_number
  126. {
  127. // CertificateSerialNumber :: Integer
  128. READ_OBJECT_OR_FAIL(Integer, Crypto::UnsignedBigInteger, value, "Certificate::serial_number");
  129. certificate.serial_number = move(value);
  130. }
  131. auto parse_algorithm_identifier = [&](CertificateKeyAlgorithm& field) -> Optional<bool> {
  132. // AlgorithmIdentifier ::= Sequence {
  133. // algorithm ObjectIdentifier,
  134. // parameters ANY OPTIONAL
  135. // }
  136. ENTER_SCOPE_OR_FAIL(Sequence, "AlgorithmIdentifier");
  137. READ_OBJECT_OR_FAIL(ObjectIdentifier, Vector<int>, identifier, "AlgorithmIdentifier::algorithm");
  138. if (identifier == rsa_encryption_oid)
  139. field = CertificateKeyAlgorithm ::RSA_RSA;
  140. else if (identifier == rsa_md5_encryption_oid)
  141. field = CertificateKeyAlgorithm ::RSA_MD5;
  142. else if (identifier == rsa_sha1_encryption_oid)
  143. field = CertificateKeyAlgorithm ::RSA_SHA1;
  144. else if (identifier == rsa_sha256_encryption_oid)
  145. field = CertificateKeyAlgorithm ::RSA_SHA256;
  146. else if (identifier == rsa_sha384_encryption_oid)
  147. field = CertificateKeyAlgorithm ::RSA_SHA384;
  148. else if (identifier == rsa_sha512_encryption_oid)
  149. field = CertificateKeyAlgorithm ::RSA_SHA512;
  150. else
  151. return {};
  152. EXIT_SCOPE("AlgorithmIdentifier");
  153. return true;
  154. };
  155. // signature
  156. {
  157. if (!parse_algorithm_identifier(certificate.algorithm).has_value())
  158. return {};
  159. }
  160. auto parse_name = [&](auto& name_struct) -> Optional<bool> {
  161. // Name ::= Choice {
  162. // rdn_sequence RDNSequence
  163. // } // NOTE: since this is the only alternative, there's no index
  164. // RDNSequence ::= Sequence OF RelativeDistinguishedName
  165. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::issuer/subject");
  166. // RelativeDistinguishedName ::= Set OF AttributeTypeAndValue
  167. // AttributeTypeAndValue ::= Sequence {
  168. // type AttributeType,
  169. // value AttributeValue
  170. // }
  171. // AttributeType ::= ObjectIdentifier
  172. // AttributeValue ::= Any
  173. while (!decoder.eof()) {
  174. // Parse only the required fields, and ignore the rest.
  175. ENTER_SCOPE_OR_FAIL(Set, "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName");
  176. while (!decoder.eof()) {
  177. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue");
  178. ENSURE_OBJECT_KIND(ObjectIdentifier, "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue::type");
  179. if (auto type_identifier_or_error = decoder.read<Vector<int>>(); !type_identifier_or_error.is_error()) {
  180. // Figure out what type of identifier this is
  181. auto& identifier = type_identifier_or_error.value();
  182. if (identifier == common_name_oid) {
  183. READ_OBJECT_OR_FAIL(PrintableString, StringView, name,
  184. "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue::Value");
  185. name_struct.subject = name;
  186. } else if (identifier == country_name_oid) {
  187. READ_OBJECT_OR_FAIL(PrintableString, StringView, name,
  188. "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue::Value");
  189. name_struct.country = name;
  190. } else if (identifier == locality_name_oid) {
  191. READ_OBJECT_OR_FAIL(PrintableString, StringView, name,
  192. "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue::Value");
  193. name_struct.location = name;
  194. } else if (identifier == organization_name_oid) {
  195. READ_OBJECT_OR_FAIL(PrintableString, StringView, name,
  196. "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue::Value");
  197. name_struct.entity = name;
  198. } else if (identifier == organizational_unit_name_oid) {
  199. READ_OBJECT_OR_FAIL(PrintableString, StringView, name,
  200. "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue::Value");
  201. name_struct.unit = name;
  202. }
  203. } else {
  204. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue::type data was invalid: {}", type_identifier_or_error.error());
  205. return {};
  206. }
  207. EXIT_SCOPE("Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName::$::AttributeTypeAndValue");
  208. }
  209. EXIT_SCOPE("Certificate::TBSCertificate::issuer/subject::$::RelativeDistinguishedName");
  210. }
  211. EXIT_SCOPE("Certificate::TBSCertificate::issuer/subject");
  212. return true;
  213. };
  214. // issuer
  215. {
  216. if (!parse_name(certificate.issuer).has_value())
  217. return {};
  218. }
  219. // validity
  220. {
  221. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Validity");
  222. auto parse_time = [&](Core::DateTime& datetime) -> Optional<bool> {
  223. // Time ::= Choice {
  224. // utc_time UTCTime,
  225. // general_time GeneralizedTime
  226. // }
  227. auto tag = decoder.peek();
  228. if (tag.is_error()) {
  229. dbgln_if(1, "Certificate::TBSCertificate::Validity::$::Time failed to read tag: {}", tag.error());
  230. return {};
  231. };
  232. if (tag.value().kind == Crypto::ASN1::Kind::UTCTime) {
  233. READ_OBJECT_OR_FAIL(UTCTime, StringView, time, "Certificate::TBSCertificate::Validity::$");
  234. auto result = Crypto::ASN1::parse_utc_time(time);
  235. if (!result.has_value()) {
  236. dbgln_if(1, "Certificate::TBSCertificate::Validity::$::Time Invalid UTC Time: {}", time);
  237. return {};
  238. }
  239. datetime = result.release_value();
  240. return true;
  241. }
  242. if (tag.value().kind == Crypto::ASN1::Kind::GeneralizedTime) {
  243. READ_OBJECT_OR_FAIL(UTCTime, StringView, time, "Certificate::TBSCertificate::Validity::$");
  244. auto result = Crypto::ASN1::parse_generalized_time(time);
  245. if (!result.has_value()) {
  246. dbgln_if(1, "Certificate::TBSCertificate::Validity::$::Time Invalid Generalized Time: {}", time);
  247. return {};
  248. }
  249. datetime = result.release_value();
  250. return true;
  251. }
  252. dbgln_if(1, "Unrecognised Time format {}", Crypto::ASN1::kind_name(tag.value().kind));
  253. return {};
  254. };
  255. if (!parse_time(certificate.not_before).has_value())
  256. return {};
  257. if (!parse_time(certificate.not_after).has_value())
  258. return {};
  259. EXIT_SCOPE("Certificate::TBSCertificate::Validity");
  260. }
  261. // subject
  262. {
  263. if (!parse_name(certificate.subject).has_value())
  264. return {};
  265. }
  266. // subject_public_key_info
  267. {
  268. // SubjectPublicKeyInfo ::= Sequence {
  269. // algorithm AlgorithmIdentifier,
  270. // subject_public_key BitString
  271. // }
  272. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::subject_public_key_info");
  273. if (!parse_algorithm_identifier(certificate.key_algorithm).has_value())
  274. return {};
  275. READ_OBJECT_OR_FAIL(BitString, Crypto::ASN1::BitStringView, value, "Certificate::TBSCertificate::subject_public_key_info::subject_public_key_info");
  276. // Note: Once we support other kinds of keys, make sure to check the kind here!
  277. auto key = Crypto::PK::RSA::parse_rsa_key(value.raw_bytes());
  278. if (!key.public_key.length()) {
  279. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::subject_public_key_info::subject_public_key_info: Invalid key");
  280. return {};
  281. }
  282. certificate.public_key = move(key.public_key);
  283. EXIT_SCOPE("Certificate::TBSCertificate::subject_public_key_info");
  284. }
  285. auto parse_unique_identifier = [&]() -> Optional<bool> {
  286. if (certificate.version == 0)
  287. return true;
  288. auto tag = decoder.peek();
  289. if (tag.is_error()) {
  290. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::*::UniqueIdentifier could not read tag: {}", tag.error());
  291. return {};
  292. }
  293. // The spec says to just ignore these.
  294. if (static_cast<u8>(tag.value().kind) == 1 || static_cast<u8>(tag.value().kind) == 2)
  295. DROP_OBJECT_OR_FAIL("UniqueIdentifier");
  296. return true;
  297. };
  298. // issuer_unique_identifier
  299. {
  300. if (!parse_unique_identifier().has_value())
  301. return {};
  302. }
  303. // subject_unique_identifier
  304. {
  305. if (!parse_unique_identifier().has_value())
  306. return {};
  307. }
  308. // extensions
  309. {
  310. if (certificate.version == 2) {
  311. auto tag = decoder.peek();
  312. if (tag.is_error()) {
  313. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::*::UniqueIdentifier could not read tag: {}", tag.error());
  314. return {};
  315. }
  316. if (static_cast<u8>(tag.value().kind) == 3) {
  317. // Extensions ::= Sequence OF Extension
  318. // Extension ::= Sequence {
  319. // extension_id ObjectIdentifier,
  320. // critical Boolean DEFAULT false,
  321. // extension_value OctetString (DER-encoded)
  322. // }
  323. ENTER_SCOPE_WITHOUT_TYPECHECK("Certificate::TBSCertificate::Extensions(IMPLICIT)");
  324. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions");
  325. while (!decoder.eof()) {
  326. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions::$::Extension");
  327. READ_OBJECT_OR_FAIL(ObjectIdentifier, Vector<int>, extension_id, "Certificate::TBSCertificate::Extensions::$::Extension::extension_id");
  328. bool is_critical = false;
  329. if (auto tag = decoder.peek(); !tag.is_error() && tag.value().kind == Crypto::ASN1::Kind::Boolean) {
  330. // Read the 'critical' property
  331. READ_OBJECT_OR_FAIL(Boolean, bool, critical, "Certificate::TBSCertificate::Extensions::$::Extension::critical");
  332. is_critical = critical;
  333. }
  334. READ_OBJECT_OR_FAIL(OctetString, StringView, extension_value, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value");
  335. // Figure out what this extension is.
  336. if (extension_id == subject_alternative_name_oid) {
  337. Crypto::ASN1::Decoder decoder { extension_value.bytes() };
  338. // SubjectAlternativeName ::= GeneralNames
  339. // GeneralNames ::= Sequence OF GeneralName
  340. // GeneralName ::= CHOICE {
  341. // other_name (0) OtherName,
  342. // rfc_822_name (1) IA5String,
  343. // dns_name (2) IA5String,
  344. // x400Address (3) ORAddress,
  345. // directory_name (4) Name,
  346. // edi_party_name (5) EDIPartyName,
  347. // uri (6) IA5String,
  348. // ip_address (7) OctetString,
  349. // registered_id (8) ObjectIdentifier,
  350. // }
  351. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName");
  352. while (!decoder.eof()) {
  353. auto tag = decoder.peek();
  354. if (tag.is_error()) {
  355. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$ could not read tag: {}", tag.error());
  356. return {};
  357. }
  358. auto tag_value = static_cast<u8>(tag.value().kind);
  359. switch (tag_value) {
  360. case 0:
  361. // OtherName
  362. // We don't know how to use this.
  363. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::OtherName");
  364. break;
  365. case 1:
  366. // RFC 822 name
  367. // We don't know how to use this.
  368. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::RFC822Name");
  369. break;
  370. case 2: {
  371. // DNS Name
  372. READ_OBJECT_OR_FAIL(IA5String, StringView, name, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::Name");
  373. certificate.SAN.append(name);
  374. break;
  375. }
  376. case 3:
  377. // x400Address
  378. // We don't know how to use this.
  379. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::X400Address");
  380. break;
  381. case 4:
  382. // Directory name
  383. // We don't know how to use this.
  384. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::DirectoryName");
  385. break;
  386. case 5:
  387. // edi party name
  388. // We don't know how to use this.
  389. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::EDIPartyName");
  390. break;
  391. case 6: {
  392. // URI
  393. READ_OBJECT_OR_FAIL(IA5String, StringView, name, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::URI");
  394. certificate.SAN.append(name);
  395. break;
  396. }
  397. case 7: {
  398. // IP Address
  399. READ_OBJECT_OR_FAIL(OctetString, StringView, ip_addr_sv, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::IPAddress");
  400. IPv4Address ip_addr { ip_addr_sv.bytes().data() };
  401. certificate.SAN.append(ip_addr.to_deprecated_string());
  402. break;
  403. }
  404. case 8:
  405. // Registered ID
  406. // We can't handle these.
  407. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::RegisteredID");
  408. break;
  409. default:
  410. dbgln_if(TLS_DEBUG, "Unknown tag in SAN choice {}", tag_value);
  411. if (is_critical)
  412. return {};
  413. else
  414. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::???");
  415. }
  416. }
  417. } else if (extension_id == key_usage_oid) {
  418. // RFC5280 section 4.2.1.3: The keyCertSign bit is asserted when the subject public key is used
  419. // for verifying signatures on public key certificates. If the keyCertSign bit is asserted,
  420. // then the cA bit in the basic constraints extension MUST also be asserted.
  421. Crypto::ASN1::Decoder decoder { extension_value.bytes() };
  422. READ_OBJECT_OR_FAIL(BitString, Crypto::ASN1::BitStringView, usage, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::KeyUsage");
  423. // keyCertSign (5)
  424. certificate.is_allowed_to_sign_certificate = usage.get(5);
  425. } else if (extension_id == basic_constraints_oid) {
  426. // RFC5280 section 4.2.1.9: The cA boolean indicates whether the certified public key may be
  427. // used to verify certificate signatures. If the cA boolean is not asserted, then the keyCertSign
  428. // bit in the key usage extension MUST NOT be asserted. If the basic constraints extension is
  429. // not present in a version 3 certificate, or the extension is present but the cA boolean is
  430. // not asserted, then the certified public key MUST NOT be used to verify certificate signatures.
  431. Crypto::ASN1::Decoder decoder { extension_value.bytes() };
  432. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::BasicConstraints");
  433. if (auto tag = decoder.peek(); !tag.is_error() && tag.value().kind == Crypto::ASN1::Kind::Boolean) {
  434. READ_OBJECT_OR_FAIL(Boolean, bool, is_certificate_authority, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::BasicConstraints::cA");
  435. certificate.is_certificate_authority = is_certificate_authority;
  436. if (auto tag = decoder.peek(); !tag.is_error() && tag.value().kind == Crypto::ASN1::Kind::Integer) {
  437. READ_OBJECT_OR_FAIL(Integer, Crypto::UnsignedBigInteger, path_length_constraint, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::BasicConstraints::pathLenConstraint");
  438. certificate.path_length_constraint = path_length_constraint.to_u64();
  439. }
  440. }
  441. } else {
  442. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::Extensions::$::Extension::extension_id: unknown extension {} (critical: {})", extension_id, is_critical);
  443. if (is_critical)
  444. return {};
  445. }
  446. EXIT_SCOPE("Certificate::TBSCertificate::Extensions::$::Extension");
  447. }
  448. EXIT_SCOPE("Certificate::TBSCertificate::Extensions");
  449. EXIT_SCOPE("Certificate::TBSCertificate::Extensions(IMPLICIT)");
  450. }
  451. }
  452. }
  453. EXIT_SCOPE("Certificate::TBSCertificate");
  454. // signature_algorithm
  455. {
  456. if (!parse_algorithm_identifier(certificate.signature_algorithm).has_value())
  457. return {};
  458. }
  459. // signature_value
  460. {
  461. READ_OBJECT_OR_FAIL(BitString, Crypto::ASN1::BitStringView, value, "Certificate");
  462. auto signature_data_result = ByteBuffer::copy(value.raw_bytes());
  463. if (signature_data_result.is_error()) {
  464. dbgln("Certificate::signature_value: out of memory");
  465. return {};
  466. }
  467. certificate.signature_value = signature_data_result.release_value();
  468. }
  469. EXIT_SCOPE("Certificate");
  470. dbgln_if(TLS_DEBUG, "Certificate issued for {} by {}", certificate.subject.subject, certificate.issuer.subject);
  471. return certificate;
  472. #undef DROP_OBJECT_OR_FAIL
  473. #undef ENSURE_OBJECT_KIND
  474. #undef ENTER_SCOPE_OR_FAIL
  475. #undef ENTER_SCOPE_WITHOUT_TYPECHECK
  476. #undef EXIT_SCOPE
  477. #undef READ_OBJECT_OR_FAIL
  478. }
  479. }