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 {
22public:
23 using NativeType = EVP_PKEY;
24
25 /// Modulus wrapper
26 using ModulusView = utils::StrongTypedef<class ModulusTag, std::string_view>;
27
28 /// Exponent wrapper
29 using ExponentView = utils::StrongTypedef<class ExponentTag, std::string_view>;
30
31 using CoordinateView = utils::StrongTypedef<class CoordinateTag, std::string_view>;
32
33 using CurveTypeView = utils::StrongTypedef<class CurveTypeTag, std::string_view>;
34
35 PublicKey() = default;
36
37 NativeType* GetNative() const noexcept { return pkey_.get(); }
38 explicit operator bool() const noexcept { return !!pkey_; }
39
40 /// Accepts a string that contains a certificate or public key, checks that
41 /// it's correct, loads it into OpenSSL structures and returns as a
42 /// PublicKey variable.
43 ///
44 /// @throw crypto::KeyParseError if failed to load the key.
45 static PublicKey LoadFromString(std::string_view key);
46
47 /// Extracts PublicKey from certificate.
48 ///
49 /// @throw crypto::KeyParseError if failed to load the key.
51
52 /// Creates RSA PublicKey from components
53 ///
54 /// @throw crypto::KeyParseError if failed to load the key.
55 static PublicKey LoadRSAFromComponents(ModulusView modulus, ExponentView exponent);
56
57 /// Creates EC PublicKey from components
58 ///
59 /// @throw crypto::KeyParseError if failed to load the key.
60 static PublicKey LoadECFromComponents(CurveTypeView curve, CoordinateView x, CoordinateView y);
61
62private:
63 explicit PublicKey(std::shared_ptr<NativeType> pkey) : pkey_(std::move(pkey)) {}
64
65 std::shared_ptr<NativeType> pkey_;
66};
67
68} // namespace crypto
69
70USERVER_NAMESPACE_END