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 <memory>
7#include <optional>
8#include <string>
9#include <string_view>
10
11#include <userver/crypto/basic_types.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace crypto {
16
17/// @ingroup userver_universal userver_containers
18///
19/// Loaded into memory X509 certificate
21 public:
22 using NativeType = X509;
23
24 Certificate() = default;
25
26 NativeType* GetNative() const noexcept { return cert_.get(); }
27 explicit operator bool() const noexcept { return !!cert_; }
28
29 /// Returns a PEM-encoded representation of stored certificate.
30 ///
31 /// @throw crypto::SerializationError if serialization fails.
32 std::optional<std::string> GetPemString() const;
33
34 /// Accepts a string that contains a certificate, checks that
35 /// it's correct, loads it into OpenSSL structures and returns as a
36 /// Certificate variable.
37 ///
38 /// @throw crypto::KeyParseError if failed to load the certificate.
39 static Certificate LoadFromString(std::string_view certificate);
40
41 private:
42 explicit Certificate(std::shared_ptr<NativeType> cert)
43 : cert_(std::move(cert)) {}
44
45 std::shared_ptr<NativeType> cert_;
46};
47
48} // namespace crypto
49
50USERVER_NAMESPACE_END