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