Certificate.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. // self issued
  309. {
  310. certificate.is_self_issued = certificate.issuer_identifier_string() == certificate.subject_identifier_string();
  311. }
  312. // extensions
  313. {
  314. if (certificate.version == 2) {
  315. auto tag = decoder.peek();
  316. if (tag.is_error()) {
  317. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::*::UniqueIdentifier could not read tag: {}", tag.error());
  318. return {};
  319. }
  320. if (static_cast<u8>(tag.value().kind) == 3) {
  321. // Extensions ::= Sequence OF Extension
  322. // Extension ::= Sequence {
  323. // extension_id ObjectIdentifier,
  324. // critical Boolean DEFAULT false,
  325. // extension_value OctetString (DER-encoded)
  326. // }
  327. ENTER_SCOPE_WITHOUT_TYPECHECK("Certificate::TBSCertificate::Extensions(IMPLICIT)");
  328. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions");
  329. while (!decoder.eof()) {
  330. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions::$::Extension");
  331. READ_OBJECT_OR_FAIL(ObjectIdentifier, Vector<int>, extension_id, "Certificate::TBSCertificate::Extensions::$::Extension::extension_id");
  332. bool is_critical = false;
  333. if (auto tag = decoder.peek(); !tag.is_error() && tag.value().kind == Crypto::ASN1::Kind::Boolean) {
  334. // Read the 'critical' property
  335. READ_OBJECT_OR_FAIL(Boolean, bool, critical, "Certificate::TBSCertificate::Extensions::$::Extension::critical");
  336. is_critical = critical;
  337. }
  338. READ_OBJECT_OR_FAIL(OctetString, StringView, extension_value, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value");
  339. // Figure out what this extension is.
  340. if (extension_id == subject_alternative_name_oid) {
  341. Crypto::ASN1::Decoder decoder { extension_value.bytes() };
  342. // SubjectAlternativeName ::= GeneralNames
  343. // GeneralNames ::= Sequence OF GeneralName
  344. // GeneralName ::= CHOICE {
  345. // other_name (0) OtherName,
  346. // rfc_822_name (1) IA5String,
  347. // dns_name (2) IA5String,
  348. // x400Address (3) ORAddress,
  349. // directory_name (4) Name,
  350. // edi_party_name (5) EDIPartyName,
  351. // uri (6) IA5String,
  352. // ip_address (7) OctetString,
  353. // registered_id (8) ObjectIdentifier,
  354. // }
  355. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName");
  356. while (!decoder.eof()) {
  357. auto tag = decoder.peek();
  358. if (tag.is_error()) {
  359. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$ could not read tag: {}", tag.error());
  360. return {};
  361. }
  362. auto tag_value = static_cast<u8>(tag.value().kind);
  363. switch (tag_value) {
  364. case 0:
  365. // OtherName
  366. // We don't know how to use this.
  367. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::OtherName");
  368. break;
  369. case 1:
  370. // RFC 822 name
  371. // We don't know how to use this.
  372. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::RFC822Name");
  373. break;
  374. case 2: {
  375. // DNS Name
  376. READ_OBJECT_OR_FAIL(IA5String, StringView, name, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::Name");
  377. certificate.SAN.append(name);
  378. break;
  379. }
  380. case 3:
  381. // x400Address
  382. // We don't know how to use this.
  383. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::X400Address");
  384. break;
  385. case 4:
  386. // Directory name
  387. // We don't know how to use this.
  388. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::DirectoryName");
  389. break;
  390. case 5:
  391. // edi party name
  392. // We don't know how to use this.
  393. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::EDIPartyName");
  394. break;
  395. case 6: {
  396. // URI
  397. READ_OBJECT_OR_FAIL(IA5String, StringView, name, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::URI");
  398. certificate.SAN.append(name);
  399. break;
  400. }
  401. case 7: {
  402. // IP Address
  403. READ_OBJECT_OR_FAIL(OctetString, StringView, ip_addr_sv, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::IPAddress");
  404. IPv4Address ip_addr { ip_addr_sv.bytes().data() };
  405. certificate.SAN.append(ip_addr.to_deprecated_string());
  406. break;
  407. }
  408. case 8:
  409. // Registered ID
  410. // We can't handle these.
  411. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::RegisteredID");
  412. break;
  413. default:
  414. dbgln_if(TLS_DEBUG, "Unknown tag in SAN choice {}", tag_value);
  415. if (is_critical)
  416. return {};
  417. else
  418. DROP_OBJECT_OR_FAIL("Certificate::TBSCertificate::Extensions::$::Extension::extension_value::SubjectAlternativeName::$::???");
  419. }
  420. }
  421. } else if (extension_id == key_usage_oid) {
  422. // RFC5280 section 4.2.1.3: The keyCertSign bit is asserted when the subject public key is used
  423. // for verifying signatures on public key certificates. If the keyCertSign bit is asserted,
  424. // then the cA bit in the basic constraints extension MUST also be asserted.
  425. Crypto::ASN1::Decoder decoder { extension_value.bytes() };
  426. READ_OBJECT_OR_FAIL(BitString, Crypto::ASN1::BitStringView, usage, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::KeyUsage");
  427. // keyCertSign (5)
  428. certificate.is_allowed_to_sign_certificate = usage.get(5);
  429. } else if (extension_id == basic_constraints_oid) {
  430. // RFC5280 section 4.2.1.9: The cA boolean indicates whether the certified public key may be
  431. // used to verify certificate signatures. If the cA boolean is not asserted, then the keyCertSign
  432. // bit in the key usage extension MUST NOT be asserted. If the basic constraints extension is
  433. // not present in a version 3 certificate, or the extension is present but the cA boolean is
  434. // not asserted, then the certified public key MUST NOT be used to verify certificate signatures.
  435. Crypto::ASN1::Decoder decoder { extension_value.bytes() };
  436. ENTER_SCOPE_OR_FAIL(Sequence, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::BasicConstraints");
  437. if (auto tag = decoder.peek(); !tag.is_error() && tag.value().kind == Crypto::ASN1::Kind::Boolean) {
  438. READ_OBJECT_OR_FAIL(Boolean, bool, is_certificate_authority, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::BasicConstraints::cA");
  439. certificate.is_certificate_authority = is_certificate_authority;
  440. if (auto tag = decoder.peek(); !tag.is_error() && tag.value().kind == Crypto::ASN1::Kind::Integer) {
  441. READ_OBJECT_OR_FAIL(Integer, Crypto::UnsignedBigInteger, path_length_constraint, "Certificate::TBSCertificate::Extensions::$::Extension::extension_value::BasicConstraints::pathLenConstraint");
  442. certificate.path_length_constraint = path_length_constraint.to_u64();
  443. }
  444. }
  445. } else {
  446. dbgln_if(TLS_DEBUG, "Certificate::TBSCertificate::Extensions::$::Extension::extension_id: unknown extension {} (critical: {})", extension_id, is_critical);
  447. if (is_critical)
  448. return {};
  449. }
  450. EXIT_SCOPE("Certificate::TBSCertificate::Extensions::$::Extension");
  451. }
  452. EXIT_SCOPE("Certificate::TBSCertificate::Extensions");
  453. EXIT_SCOPE("Certificate::TBSCertificate::Extensions(IMPLICIT)");
  454. }
  455. }
  456. }
  457. EXIT_SCOPE("Certificate::TBSCertificate");
  458. // signature_algorithm
  459. {
  460. if (!parse_algorithm_identifier(certificate.signature_algorithm).has_value())
  461. return {};
  462. }
  463. // signature_value
  464. {
  465. READ_OBJECT_OR_FAIL(BitString, Crypto::ASN1::BitStringView, value, "Certificate");
  466. auto signature_data_result = ByteBuffer::copy(value.raw_bytes());
  467. if (signature_data_result.is_error()) {
  468. dbgln("Certificate::signature_value: out of memory");
  469. return {};
  470. }
  471. certificate.signature_value = signature_data_result.release_value();
  472. }
  473. EXIT_SCOPE("Certificate");
  474. dbgln_if(TLS_DEBUG, "Certificate issued for {} by {}", certificate.subject.subject, certificate.issuer.subject);
  475. return certificate;
  476. #undef DROP_OBJECT_OR_FAIL
  477. #undef ENSURE_OBJECT_KIND
  478. #undef ENTER_SCOPE_OR_FAIL
  479. #undef ENTER_SCOPE_WITHOUT_TYPECHECK
  480. #undef EXIT_SCOPE
  481. #undef READ_OBJECT_OR_FAIL
  482. }
  483. }