Certificate.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2020, 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/DateTime.h>
  13. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  14. #include <LibCrypto/PK/RSA.h>
  15. namespace TLS {
  16. enum class CertificateKeyAlgorithm {
  17. Unsupported = 0x00,
  18. RSA_RSA = 0x01,
  19. RSA_MD5 = 0x04,
  20. RSA_SHA1 = 0x05,
  21. RSA_SHA256 = 0x0b,
  22. RSA_SHA384 = 0x0c,
  23. RSA_SHA512 = 0x0d,
  24. };
  25. class Certificate {
  26. public:
  27. u16 version { 0 };
  28. CertificateKeyAlgorithm algorithm { CertificateKeyAlgorithm::Unsupported };
  29. CertificateKeyAlgorithm key_algorithm { CertificateKeyAlgorithm::Unsupported };
  30. CertificateKeyAlgorithm ec_algorithm { CertificateKeyAlgorithm::Unsupported };
  31. ByteBuffer exponent {};
  32. Crypto::PK::RSAPublicKey<Crypto::UnsignedBigInteger> public_key {};
  33. Crypto::PK::RSAPrivateKey<Crypto::UnsignedBigInteger> private_key {};
  34. struct Name {
  35. String country;
  36. String state;
  37. String location;
  38. String entity;
  39. String subject;
  40. String unit;
  41. } issuer, subject;
  42. Core::DateTime not_before;
  43. Core::DateTime not_after;
  44. Vector<String> SAN;
  45. u8* ocsp { nullptr };
  46. Crypto::UnsignedBigInteger serial_number;
  47. ByteBuffer sign_key {};
  48. ByteBuffer fingerprint {};
  49. ByteBuffer der {};
  50. ByteBuffer data {};
  51. CertificateKeyAlgorithm signature_algorithm { CertificateKeyAlgorithm::Unsupported };
  52. ByteBuffer signature_value {};
  53. ByteBuffer original_asn1 {};
  54. bool is_allowed_to_sign_certificate { false };
  55. bool is_certificate_authority { false };
  56. Optional<size_t> path_length_constraint {};
  57. static Optional<Certificate> parse_asn1(ReadonlyBytes, bool client_cert = false);
  58. bool is_valid() const;
  59. String subject_identifier_string() const
  60. {
  61. StringBuilder cert_name;
  62. if (!subject.country.is_empty()) {
  63. cert_name.append("/C=");
  64. cert_name.append(subject.country);
  65. }
  66. if (!subject.state.is_empty()) {
  67. cert_name.append("/ST=");
  68. cert_name.append(subject.state);
  69. }
  70. if (!subject.location.is_empty()) {
  71. cert_name.append("/L=");
  72. cert_name.append(subject.location);
  73. }
  74. if (!subject.entity.is_empty()) {
  75. cert_name.append("/O=");
  76. cert_name.append(subject.entity);
  77. }
  78. if (!subject.unit.is_empty()) {
  79. cert_name.append("/OU=");
  80. cert_name.append(subject.unit);
  81. }
  82. if (!subject.subject.is_empty()) {
  83. cert_name.append("/CN=");
  84. cert_name.append(subject.subject);
  85. }
  86. return cert_name.build();
  87. }
  88. String issuer_identifier_string() const
  89. {
  90. StringBuilder cert_name;
  91. if (!issuer.country.is_empty()) {
  92. cert_name.append("/C=");
  93. cert_name.append(issuer.country);
  94. }
  95. if (!issuer.state.is_empty()) {
  96. cert_name.append("/ST=");
  97. cert_name.append(issuer.state);
  98. }
  99. if (!issuer.location.is_empty()) {
  100. cert_name.append("/L=");
  101. cert_name.append(issuer.location);
  102. }
  103. if (!issuer.entity.is_empty()) {
  104. cert_name.append("/O=");
  105. cert_name.append(issuer.entity);
  106. }
  107. if (!issuer.unit.is_empty()) {
  108. cert_name.append("/OU=");
  109. cert_name.append(issuer.unit);
  110. }
  111. if (!issuer.subject.is_empty()) {
  112. cert_name.append("/CN=");
  113. cert_name.append(issuer.subject);
  114. }
  115. return cert_name.build();
  116. }
  117. };
  118. class DefaultRootCACertificates {
  119. public:
  120. DefaultRootCACertificates();
  121. Vector<Certificate> const& certificates() const { return m_ca_certificates; }
  122. static DefaultRootCACertificates& the() { return s_the; }
  123. private:
  124. static Singleton<DefaultRootCACertificates> s_the;
  125. Vector<Certificate> m_ca_certificates;
  126. };
  127. }
  128. using TLS::Certificate;
  129. using TLS::DefaultRootCACertificates;