userver: userver/crypto/certificate.hpp Source File
Loading...
Searching...
No Matches
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
45 /// Returns Subject
46 std::string GetSubject() const;
47
48private:
49 explicit Certificate(std::shared_ptr<NativeType> cert) : cert_(std::move(cert)) {}
50
51 std::shared_ptr<NativeType> cert_;
52};
53
54using CertificatesChain = std::list<Certificate>;
55
56/// Accepts a string that contains a chain of certificates (primary and intermediate), checks that
57/// it's correct, loads it into OpenSSL structures and returns as a
58/// list of 'Certificate's.
59///
60/// @throw crypto::KeyParseError if failed to load the certificate.
61CertificatesChain LoadCertificatesChainFromString(std::string_view chain);
62
63} // namespace crypto
64
65USERVER_NAMESPACE_END