Stroika Library 3.0d23x
 
Loading...
Searching...
No Matches
Providers/OpenSSL/PrivateKey.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#if qStroika_HasComponent_OpenSSL
7#include <openssl/evp.h>
8#include <openssl/pem.h>
9#include <openssl/ssl.h>
10#endif
11
13#include "Stroika/Foundation/Execution/Exceptions.h"
15
16#include "PrivateKey.h"
17
18using namespace Stroika::Foundation;
20using namespace Stroika::Foundation::Cryptography;
21using namespace Stroika::Foundation::Cryptography::Providers;
22using namespace Stroika::Foundation::Cryptography::Providers::OpenSSL;
23using namespace Stroika::Foundation::Debug;
24
25using Memory::BLOB;
26using std::byte;
27
28// Comment this in to turn on aggressive noisy DbgTrace in this module
29// #define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
30
31#if qStroika_HasComponent_OpenSSL
32
33namespace {
34 // https://linux.die.net/man/3/bio_s_mem
35 struct BIO2String_ {
36 BIO* b{nullptr};
37 BIO2String_ ()
38 {
39 b = ::BIO_new (::BIO_s_mem ());
40 }
41 BIO2String_ (const BIO2String_&) = delete;
43 {
44 BUF_MEM* p;
45 BIO_get_mem_ptr (b, &p);
46 return String{span{p->data, p->length}}; // assume ascii???;
47 }
48 ~BIO2String_ ()
49 {
50 ::BIO_free (b);
51 }
52 };
53}
54
55namespace {
56 struct Rep_ : OpenSSL::PrivateKey::IRep {
57
58 OpenSSL::PrivateKey::LibRepType fKey_;
59
60 Rep_ () = delete;
61 Rep_ (const Rep_&) = delete;
62 Rep_ (Rep_&&) = default;
63 Rep_ (OpenSSL::PrivateKey::LibRepType&& p)
64 : fKey_{move (p)}
65 {
66 }
67 virtual EVP_PKEY* Get_EVP_PKEY () const override
68 {
69 return fKey_.get ();
70 }
71 virtual int GetType () const override
72 {
73 return ::EVP_PKEY_get_base_id (fKey_.get ());
74 }
75 virtual unsigned int GetBits () const override
76 {
77 return ::EVP_PKEY_get_bits (fKey_.get ());
78 }
79 virtual String GetPrintSummary () const override
80 {
82 // https://docs.openssl.org/3.0/man3/EVP_PKEY_print_private/
83 {
84 BIO2String_ b;
85 if (::EVP_PKEY_print_public (b.b, fKey_.get (), 0, nullptr) == 1) {
86 sb << b.ToString ();
87 }
88 }
89 {
90 BIO2String_ b;
91 if (::EVP_PKEY_print_private (b.b, fKey_.get (), 0, nullptr) == 1) {
92 sb << b.ToString ();
93 }
94 }
95 {
96 BIO2String_ b;
97 if (::EVP_PKEY_print_params (b.b, fKey_.get (), 0, nullptr) == 1) {
98 sb << b.ToString ();
99 }
100 }
101 return sb;
102 }
103 };
104}
105#endif
106
107#if qStroika_HasComponent_OpenSSL
108auto OpenSSL::PrivateKey::New (LibRepType&& p) -> Ptr
109{
110 return Memory::MakeSharedPtr<Rep_> (move (p));
111}
112#endif
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
String ToString(T &&t, ARGS... args)
Return a debug-friendly, display version of the argument: not guaranteed parsable or usable except fo...
Definition ToString.inl:465