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