Certificate.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2020-2023, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Forward.h>
  9. #include <AK/Optional.h>
  10. #include <AK/Types.h>
  11. #include <LibCore/ConfigFile.h>
  12. #include <LibCore/DateTime.h>
  13. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  14. #include <LibCrypto/PK/RSA.h>
  15. #include <LibTLS/Extensions.h>
  16. namespace TLS {
  17. constexpr static Array<int, 7>
  18. rsa_encryption_oid { 1, 2, 840, 113549, 1, 1, 1 },
  19. rsa_md5_encryption_oid { 1, 2, 840, 113549, 1, 1, 4 },
  20. rsa_sha1_encryption_oid { 1, 2, 840, 113549, 1, 1, 5 },
  21. rsa_sha256_encryption_oid { 1, 2, 840, 113549, 1, 1, 11 },
  22. rsa_sha384_encryption_oid { 1, 2, 840, 113549, 1, 1, 12 },
  23. rsa_sha512_encryption_oid { 1, 2, 840, 113549, 1, 1, 13 },
  24. rsa_sha224_encryption_oid { 1, 2, 840, 113549, 1, 1, 14 },
  25. ecdsa_with_sha224_encryption_oid { 1, 2, 840, 10045, 4, 3, 1 },
  26. ecdsa_with_sha256_encryption_oid { 1, 2, 840, 10045, 4, 3, 2 },
  27. ecdsa_with_sha384_encryption_oid { 1, 2, 840, 10045, 4, 3, 3 },
  28. ecdsa_with_sha512_encryption_oid { 1, 2, 840, 10045, 4, 3, 4 },
  29. ec_public_key_encryption_oid { 1, 2, 840, 10045, 2, 1 };
  30. constexpr static Array<Array<int, 7>, 9> known_algorithm_identifiers {
  31. rsa_encryption_oid,
  32. rsa_md5_encryption_oid,
  33. rsa_sha1_encryption_oid,
  34. rsa_sha256_encryption_oid,
  35. rsa_sha384_encryption_oid,
  36. rsa_sha512_encryption_oid,
  37. ecdsa_with_sha256_encryption_oid,
  38. ecdsa_with_sha384_encryption_oid,
  39. ec_public_key_encryption_oid
  40. };
  41. constexpr static Array<int, 7>
  42. curve_ansip384r1 { 1, 3, 132, 0, 34 },
  43. curve_prime256 { 1, 2, 840, 10045, 3, 1, 7 };
  44. constexpr static Array<Array<int, 7>, 9> known_curve_identifiers {
  45. curve_ansip384r1,
  46. curve_prime256
  47. };
  48. constexpr static Array<int, 4>
  49. key_usage_oid { 2, 5, 29, 15 },
  50. subject_alternative_name_oid { 2, 5, 29, 17 },
  51. issuer_alternative_name_oid { 2, 5, 29, 18 },
  52. basic_constraints_oid { 2, 5, 29, 19 };
  53. #define _ENUM(key, value) key,
  54. #define __ENUM_OBJECT_CLASS \
  55. _ENUM(ApplicationProcess, "2.5.6.11"sv) \
  56. _ENUM(Country, "2.5.6.2"sv) \
  57. _ENUM(DcObject, "1.3.6.1.4.1.1466.344"sv) \
  58. _ENUM(Device, "2.5.6.14"sv) \
  59. _ENUM(GroupOfNames, "2.5.6.9"sv) \
  60. _ENUM(GroupOfUniqueNames, "2.5.6.17"sv) \
  61. _ENUM(Locality, "2.5.6.3"sv) \
  62. _ENUM(Organization, "2.5.6.4"sv) \
  63. _ENUM(OrganizationalPerson, "2.5.6.7"sv) \
  64. _ENUM(OrganizationalRole, "2.5.6.8"sv) \
  65. _ENUM(OrganizationalUnit, "2.5.6.5"sv) \
  66. _ENUM(Person, "2.5.6.6"sv) \
  67. _ENUM(ResidentialPerson, "2.5.6.10"sv) \
  68. _ENUM(UidObject, "1.3.6.1.1.3.1"sv)
  69. // NOTE: Type = O
  70. // NOTE: This list is not exhaustive. If more members are needed, find them at the link below.
  71. // https://www.iana.org/assignments/ldap-parameters/ldap-parameters.xhtml#ldap-parameters-3
  72. enum class ObjectClass {
  73. __ENUM_OBJECT_CLASS
  74. };
  75. #define __ENUM_ATTRIBUTE_TYPE \
  76. _ENUM(BusinessCategory, "2.5.4.15"sv) \
  77. _ENUM(C, "2.5.4.6"sv) \
  78. _ENUM(Cn, "2.5.4.3"sv) \
  79. _ENUM(Dc, "0.9.2342.19200300.100.1.25"sv) \
  80. _ENUM(Description, "2.5.4.13"sv) \
  81. _ENUM(DestinationIndicator, "2.5.4.27"sv) \
  82. _ENUM(DistinguishedName, "2.5.4.49"sv) \
  83. _ENUM(DnQualifier, "2.5.4.46"sv) \
  84. _ENUM(EnhancedSearchGuide, "2.5.4.47"sv) \
  85. _ENUM(Email, "1.2.840.113549.1.9.1"sv) \
  86. _ENUM(FacsimileTelephoneNumber, "2.5.4.23"sv) \
  87. _ENUM(GenerationQualifier, "2.5.4.44"sv) \
  88. _ENUM(GivenName, "2.5.4.42"sv) \
  89. _ENUM(HouseIdentifier, "2.5.4.51"sv) \
  90. _ENUM(Initials, "2.5.4.43"sv) \
  91. _ENUM(InternationalISDNNumber, "2.5.4.25"sv) \
  92. _ENUM(L, "2.5.4.7"sv) \
  93. _ENUM(Member, "2.5.4.31"sv) \
  94. _ENUM(Name, "2.5.4.41"sv) \
  95. _ENUM(O, "2.5.4.10"sv) \
  96. _ENUM(Ou, "2.5.4.11"sv) \
  97. _ENUM(Owner, "2.5.4.32"sv) \
  98. _ENUM(PhysicalDeliveryOfficeName, "2.5.4.19"sv) \
  99. _ENUM(PostalAddress, "2.5.4.16"sv) \
  100. _ENUM(PostalCode, "2.5.4.17"sv) \
  101. _ENUM(PostOfficeBox, "2.5.4.18"sv) \
  102. _ENUM(PreferredDeliveryMethod, "2.5.4.28"sv) \
  103. _ENUM(RegisteredAddress, "2.5.4.26"sv) \
  104. _ENUM(RoleOccupant, "2.5.4.33"sv) \
  105. _ENUM(SearchGuide, "2.5.4.14"sv) \
  106. _ENUM(SeeAlso, "2.5.4.34"sv) \
  107. _ENUM(SerialNumber, "2.5.4.5"sv) \
  108. _ENUM(Sn, "2.5.4.4"sv) \
  109. _ENUM(St, "2.5.4.8"sv) \
  110. _ENUM(Street, "2.5.4.9"sv) \
  111. _ENUM(Surname, "2.5.4.4"sv) \
  112. _ENUM(TelephoneNumber, "2.5.4.20"sv) \
  113. _ENUM(TeletexTerminalIdentifier, "2.5.4.22"sv) \
  114. _ENUM(TelexNumber, "2.5.4.21"sv) \
  115. _ENUM(Title, "2.5.4.12"sv) \
  116. _ENUM(Uid, "0.9.2342.19200300.100.1.1"sv) \
  117. _ENUM(UniqueMember, "2.5.4.50"sv) \
  118. _ENUM(UserPassword, "2.5.4.35"sv) \
  119. _ENUM(X121Address, "2.5.4.24"sv) \
  120. _ENUM(X500UniqueIdentifier, "2.5.4.45"sv)
  121. // NOTE: Type = A
  122. // NOTE: This list is not exhaustive. If more members are needed, find them at the link below.
  123. // https://www.iana.org/assignments/ldap-parameters/ldap-parameters.xhtml#ldap-parameters-3
  124. enum class AttributeType {
  125. __ENUM_ATTRIBUTE_TYPE
  126. };
  127. #undef _ENUM
  128. constexpr static StringView enum_value(ObjectClass object_class)
  129. {
  130. #define _ENUM(key, value) \
  131. case ObjectClass::key: \
  132. return value;
  133. switch (object_class) {
  134. __ENUM_OBJECT_CLASS
  135. }
  136. return "Unknown"sv;
  137. #undef _ENUM
  138. #undef __ENUM_OBJECT_CLASS
  139. }
  140. constexpr static StringView enum_value(AttributeType object_class)
  141. {
  142. #define _ENUM(key, value) \
  143. case AttributeType::key: \
  144. return value;
  145. switch (object_class) {
  146. __ENUM_ATTRIBUTE_TYPE
  147. }
  148. return "Unknown"sv;
  149. #undef _ENUM
  150. #undef __ENUM_ATTRIBUTE_TYPE
  151. }
  152. struct AlgorithmIdentifier {
  153. AlgorithmIdentifier()
  154. {
  155. }
  156. explicit AlgorithmIdentifier(Vector<int, 9> identifier)
  157. : identifier(identifier)
  158. {
  159. }
  160. Vector<int, 9> identifier;
  161. SupportedGroup ec_parameters {};
  162. };
  163. struct BasicConstraints {
  164. bool is_certificate_authority;
  165. Crypto::UnsignedBigInteger path_length_constraint;
  166. };
  167. class RelativeDistinguishedName {
  168. public:
  169. ErrorOr<String> to_string() const;
  170. ErrorOr<AK::HashSetResult> set(String key, String value)
  171. {
  172. return m_members.try_set(key, value);
  173. }
  174. Optional<String> get(StringView key) const
  175. {
  176. return m_members.get(key);
  177. }
  178. Optional<String> get(AttributeType key) const
  179. {
  180. return m_members.get(enum_value(key));
  181. }
  182. Optional<String> get(ObjectClass key) const
  183. {
  184. return m_members.get(enum_value(key));
  185. }
  186. String common_name() const
  187. {
  188. auto entry = get(AttributeType::Cn);
  189. if (entry.has_value()) {
  190. return entry.value();
  191. }
  192. return String();
  193. }
  194. String organizational_unit()
  195. {
  196. auto entry = get(AttributeType::Ou);
  197. if (entry.has_value()) {
  198. return entry.value();
  199. }
  200. return String();
  201. }
  202. private:
  203. HashMap<String, String> m_members;
  204. };
  205. struct Validity {
  206. Core::DateTime not_before;
  207. Core::DateTime not_after;
  208. };
  209. class SubjectPublicKey {
  210. public:
  211. Crypto::PK::RSAPublicKey<Crypto::UnsignedBigInteger> rsa;
  212. AlgorithmIdentifier algorithm;
  213. ByteBuffer raw_key;
  214. };
  215. class Certificate {
  216. public:
  217. u16 version { 0 };
  218. AlgorithmIdentifier algorithm;
  219. SubjectPublicKey public_key;
  220. ByteBuffer exponent {};
  221. Crypto::PK::RSAPrivateKey<Crypto::UnsignedBigInteger> private_key {};
  222. RelativeDistinguishedName issuer, subject;
  223. Validity validity {};
  224. Vector<String> SAN;
  225. Vector<String> IAN;
  226. u8* ocsp { nullptr };
  227. Crypto::UnsignedBigInteger serial_number;
  228. ByteBuffer sign_key {};
  229. ByteBuffer fingerprint {};
  230. ByteBuffer der {};
  231. ByteBuffer data {};
  232. AlgorithmIdentifier signature_algorithm;
  233. ByteBuffer signature_value {};
  234. ByteBuffer original_asn1 {};
  235. ByteBuffer tbs_asn1 {};
  236. bool is_allowed_to_sign_certificate { false };
  237. bool is_certificate_authority { false };
  238. Optional<size_t> path_length_constraint {};
  239. bool is_self_issued { false };
  240. static ErrorOr<Certificate> parse_certificate(ReadonlyBytes, bool client_cert = false);
  241. bool is_self_signed();
  242. bool is_valid() const;
  243. private:
  244. Optional<bool> m_is_self_signed;
  245. };
  246. class DefaultRootCACertificates {
  247. public:
  248. DefaultRootCACertificates();
  249. Vector<Certificate> const& certificates() const { return m_ca_certificates; }
  250. static ErrorOr<Vector<Certificate>> parse_pem_root_certificate_authorities(ByteBuffer&);
  251. static ErrorOr<Vector<Certificate>> load_certificates(Span<ByteString> custom_cert_paths = {});
  252. static DefaultRootCACertificates& the();
  253. static void set_default_certificate_paths(Span<ByteString> paths);
  254. private:
  255. Vector<Certificate> m_ca_certificates;
  256. };
  257. }
  258. using TLS::Certificate;
  259. using TLS::DefaultRootCACertificates;