userver: userver/crypto/public_key.hpp Source File
Loading...
Searching...
No Matches
public_key.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/crypto/public_key.hpp
4/// @brief @copybrief crypto::PublicKey
5
6#include <memory>
7#include <string_view>
8
9#include <userver/crypto/basic_types.hpp>
10#include <userver/utils/strong_typedef.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace crypto {
15
16class Certificate;
17
18/// @ingroup userver_universal userver_containers
19///
20/// Loaded into memory public key
21class PublicKey {
22 public:
23 using NativeType = EVP_PKEY;
24
25 /// Modulus wrapper
26 using ModulusView = utils::StrongTypedef<class ModulusTag, std::string_view>;
27
28 /// Exponent wrapper
30 utils::StrongTypedef<class ExponentTag, std::string_view>;
31
32 using CoordinateView =
33 utils::StrongTypedef<class CoordinateTag, std::string_view>;
34
35 using CurveTypeView =
36 utils::StrongTypedef<class CurveTypeTag, std::string_view>;
37
38 PublicKey() = default;
39
40 NativeType* GetNative() const noexcept { return pkey_.get(); }
41 explicit operator bool() const noexcept { return !!pkey_; }
42
43 /// Accepts a string that contains a certificate or public key, checks that
44 /// it's correct, loads it into OpenSSL structures and returns as a
45 /// PublicKey variable.
46 ///
47 /// @throw crypto::KeyParseError if failed to load the key.
48 static PublicKey LoadFromString(std::string_view key);
49
50 /// Extracts PublicKey from certificate.
51 ///
52 /// @throw crypto::KeyParseError if failed to load the key.
54
55 /// Creates RSA PublicKey from components
56 ///
57 /// @throw crypto::KeyParseError if failed to load the key.
58 static PublicKey LoadRSAFromComponents(ModulusView modulus,
59 ExponentView exponent);
60
61 /// Creates EC PublicKey from components
62 ///
63 /// @throw crypto::KeyParseError if failed to load the key.
64 static PublicKey LoadECFromComponents(CurveTypeView curve, CoordinateView x,
65 CoordinateView y);
66
67 private:
68 explicit PublicKey(std::shared_ptr<NativeType> pkey)
69 : pkey_(std::move(pkey)) {}
70
71 std::shared_ptr<NativeType> pkey_;
72};
73
74} // namespace crypto
75
76USERVER_NAMESPACE_END