userver: userver/crypto/certificate.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
certificate.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/crypto/certificate.hpp
4/// @brief @copybrief crypto::Certificate
5
6#include <list>
7#include <memory>
8#include <optional>
9#include <string>
10#include <string_view>
11
12#include <userver/crypto/basic_types.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace crypto {
17
18/// @ingroup userver_universal userver_containers
19///
20/// Loaded into memory X509 certificate
22public:
23 using NativeType = X509;
24
25 Certificate() = default;
26
27 NativeType* GetNative() const noexcept { return cert_.get(); }
28 explicit operator bool() const noexcept { return !!cert_; }
29
30 /// Returns a PEM-encoded representation of stored certificate.
31 ///
32 /// @throw crypto::SerializationError if serialization fails.
33 std::optional<std::string> GetPemString() const;
34
35 /// Accepts a string that contains a certificate, checks that
36 /// it's correct, loads it into OpenSSL structures and returns as a
37 /// Certificate variable.
38 ///
39 /// @throw crypto::KeyParseError if failed to load the certificate.
40 static Certificate LoadFromString(std::string_view certificate);
41 /// Loads the certificate and skips the meta information in it
42 /// @throw crypto::KeyParseError if failed to load the certificate.
43 static Certificate LoadFromStringSkippingAttributes(std::string_view certificate);
44
45private:
46 explicit Certificate(std::shared_ptr<NativeType> cert) : cert_(std::move(cert)) {}
47
48 std::shared_ptr<NativeType> cert_;
49};
50
51using CertificatesChain = std::list<Certificate>;
52
53/// Accepts a string that contains a chain of certificates (primary and intermediate), checks that
54/// it's correct, loads it into OpenSSL structures and returns as a
55/// list of 'Certificate's.
56///
57/// @throw crypto::KeyParseError if failed to load the certificate.
58CertificatesChain LoadCertificatesChainFromString(std::string_view chain);
59
60} // namespace crypto
61
62USERVER_NAMESPACE_END