Certificate.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_SHA512 = 0x0d,
  23. };
  24. class Certificate {
  25. public:
  26. u16 version { 0 };
  27. CertificateKeyAlgorithm algorithm { CertificateKeyAlgorithm::Unsupported };
  28. CertificateKeyAlgorithm key_algorithm { CertificateKeyAlgorithm::Unsupported };
  29. CertificateKeyAlgorithm ec_algorithm { CertificateKeyAlgorithm::Unsupported };
  30. ByteBuffer exponent {};
  31. Crypto::PK::RSAPublicKey<Crypto::UnsignedBigInteger> public_key {};
  32. Crypto::PK::RSAPrivateKey<Crypto::UnsignedBigInteger> private_key {};
  33. struct Name {
  34. String country;
  35. String state;
  36. String location;
  37. String entity;
  38. String subject;
  39. String unit;
  40. } issuer, subject;
  41. Core::DateTime not_before;
  42. Core::DateTime not_after;
  43. Vector<String> SAN;
  44. u8* ocsp { nullptr };
  45. Crypto::UnsignedBigInteger serial_number;
  46. ByteBuffer sign_key {};
  47. ByteBuffer fingerprint {};
  48. ByteBuffer der {};
  49. ByteBuffer data {};
  50. static Optional<Certificate> parse_asn1(ReadonlyBytes, bool client_cert = false);
  51. bool is_valid() const;
  52. };
  53. class DefaultRootCACertificates {
  54. public:
  55. DefaultRootCACertificates();
  56. const Vector<Certificate>& certificates() const { return m_ca_certificates; }
  57. static DefaultRootCACertificates& the() { return s_the; }
  58. private:
  59. static AK::Singleton<DefaultRootCACertificates> s_the;
  60. Vector<Certificate> m_ca_certificates;
  61. };
  62. }
  63. using TLS::Certificate;
  64. using TLS::DefaultRootCACertificates;