Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Providers/OpenSSL/PrivateKey.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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"
14
15#include "PrivateKey.h"
16
17using namespace Stroika::Foundation;
19using namespace Stroika::Foundation::Cryptography;
20using namespace Stroika::Foundation::Cryptography::Providers;
21using namespace Stroika::Foundation::Cryptography::Providers::OpenSSL;
22using namespace Stroika::Foundation::Debug;
23
24using Memory::BLOB;
25using std::byte;
26
27// Comment this in to turn on aggressive noisy DbgTrace in this module
28// #define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
29
30#if qStroika_HasComponent_OpenSSL
31
32namespace {
33 // https://linux.die.net/man/3/bio_s_mem
34 struct BIO2String_ {
35 BIO* b{nullptr};
36 BIO2String_ ()
37 {
38 b = ::BIO_new (::BIO_s_mem ());
39 }
40 BIO2String_ (const BIO2String_&) = delete;
42 {
43 BUF_MEM* p;
44 BIO_get_mem_ptr (b, &p);
45 return String{span{p->data, p->length}}; // assume ascii???;
46 }
47 ~BIO2String_ ()
48 {
49 ::BIO_free (b);
50 }
51 };
52}
53
54namespace {
55 struct Rep_ : OpenSSL::PrivateKey::IRep {
56
57 OpenSSL::PrivateKey::LibRepType fKey_;
58
59 Rep_ () = delete;
60 Rep_ (const Rep_&) = delete;
61 Rep_ (Rep_&&) = default;
62 Rep_ (OpenSSL::PrivateKey::LibRepType&& p)
63 : fKey_{move (p)}
64 {
65 }
66 virtual EVP_PKEY* Get_EVP_PKEY () const override
67 {
68 return fKey_.get ();
69 }
70 virtual int GetType () const override
71 {
72 return ::EVP_PKEY_get_base_id (fKey_.get ());
73 }
74 virtual unsigned int GetBits () const override
75 {
76 return ::EVP_PKEY_get_bits (fKey_.get ());
77 }
78 virtual String GetPrintSummary () const override
79 {
81 // https://docs.openssl.org/3.0/man3/EVP_PKEY_print_private/
82 {
83 BIO2String_ b;
84 if (::EVP_PKEY_print_public (b.b, fKey_.get (), 0, nullptr) == 1) {
85 sb << b.ToString ();
86 }
87 }
88 {
89 BIO2String_ b;
90 if (::EVP_PKEY_print_private (b.b, fKey_.get (), 0, nullptr) == 1) {
91 sb << b.ToString ();
92 }
93 }
94 {
95 BIO2String_ b;
96 if (::EVP_PKEY_print_params (b.b, fKey_.get (), 0, nullptr) == 1) {
97 sb << b.ToString ();
98 }
99 }
100 return sb;
101 }
102 };
103}
104#endif
105
106#if qStroika_HasComponent_OpenSSL
107auto OpenSSL::PrivateKey::New (LibRepType&& p) -> Ptr
108{
109 return make_shared<Rep_> (move (p));
110}
111#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