DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
X509Certificate2.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/Security/Cryptography/X509Certificates/X509Certificate2.h
"
3
#include "
System/ArgumentException.h
"
4
#include "
System/IOException.h
"
5
#include <cstdio>
6
7
#include <mutex>
8
9
#if defined(_WIN32)
10
#include <openssl/ssl.h>
11
#include <openssl/pem.h>
12
#include <openssl/x509.h>
13
#include <openssl/err.h>
14
#else
15
#include <openssl/ssl.h>
16
#include <openssl/pem.h>
17
#include <openssl/x509.h>
18
#include <openssl/err.h>
19
#endif
20
21
namespace
DotNetDupe
{
22
namespace
System
{
23
namespace
Security
{
24
namespace
Cryptography
{
25
namespace
X509Certificates
{
26
27
static
std::once_flag
s_cryptoInitOnce
;
28
29
static
void
InitializeCrypto
() {
31
std::call_once(
s_cryptoInitOnce
, []() {
32
OpenSSL_add_all_algorithms();
33
ERR_load_crypto_strings();
34
});
35
}
36
37
static
X509*
LoadPemCert
(
const
String
& certPath) {
39
BIO* pCertBio = BIO_new_file(certPath.
GetRawString
(),
"r"
);
40
if
(!pCertBio)
throw
IO::IOException
(
"Could not open certificate file."
);
41
43
X509* pCert = PEM_read_bio_X509(pCertBio,
nullptr
,
nullptr
,
nullptr
);
44
BIO_free(pCertBio);
45
if
(!pCert)
throw
ArgumentException
(
"Invalid PEM certificate."
);
46
return
pCert;
47
}
48
49
static
EVP_PKEY*
LoadPemKey
(
const
String
& keyPath) {
51
BIO* pKeyBio = BIO_new_file(keyPath.
GetRawString
(),
"r"
);
52
if
(!pKeyBio)
throw
IO::IOException
(
"Could not open private key file."
);
53
55
EVP_PKEY* pKey = PEM_read_bio_PrivateKey(pKeyBio,
nullptr
,
nullptr
,
nullptr
);
56
BIO_free(pKeyBio);
57
if
(!pKey)
throw
ArgumentException
(
"Invalid PEM private key."
);
58
return
pKey;
59
}
60
61
X509Certificate2::X509Certificate2
(
const
String
& certPath,
const
String
& keyPath)
62
: m_pCert(nullptr), m_pKey(nullptr) {
64
InitializeCrypto
();
65
67
X509* pCert =
LoadPemCert
(certPath);
68
try
{
69
m_pKey =
LoadPemKey
(keyPath);
70
m_pCert = pCert;
71
}
catch
(...) {
73
X509_free(pCert);
74
throw
;
75
}
76
}
77
78
X509Certificate2::~X509Certificate2
() {
80
if
(m_pCert) {
81
X509_free(
static_cast<
X509*
>
(m_pCert));
82
}
84
if
(m_pKey) {
85
EVP_PKEY_free(
static_cast<
EVP_PKEY*
>
(m_pKey));
86
}
87
}
88
89
void
*
X509Certificate2::GetInternalCert
()
const
{
91
return
m_pCert;
92
}
93
94
void
*
X509Certificate2::GetInternalKey
()
const
{
96
return
m_pKey;
97
}
98
99
}
100
}
101
}
102
}
103
}
ArgumentException.h
Defines the exception thrown when an invalid argument is provided to a method.
IOException.h
The exception that is thrown when an I/O error occurs.
X509Certificate2.h
Represents an X.509 certificate and private key per RFC 5280.
DotNetDupe::System::ArgumentException::ArgumentException
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
Definition
ArgumentException.h:20
DotNetDupe::System::IO::IOException
The exception that is thrown when an I/O error occurs.
Definition
IOException.h:17
DotNetDupe::System::Security::Cryptography::X509Certificates::X509Certificate2::~X509Certificate2
~X509Certificate2()
Releases OpenSSL certificate and private key resources.
Definition
X509Certificate2.cpp:78
DotNetDupe::System::Security::Cryptography::X509Certificates::X509Certificate2::GetInternalCert
void * GetInternalCert() const
Gets the internal OpenSSL X509* pointer.
Definition
X509Certificate2.cpp:89
DotNetDupe::System::Security::Cryptography::X509Certificates::X509Certificate2::GetInternalKey
void * GetInternalKey() const
Gets the internal OpenSSL EVP_PKEY* pointer.
Definition
X509Certificate2.cpp:94
DotNetDupe::System::Security::Cryptography::X509Certificates::X509Certificate2::X509Certificate2
X509Certificate2(const String &certPath, const String &keyPath)
Initializes a new instance of X509Certificate2 from PEM certificate and private key files.
Definition
X509Certificate2.cpp:61
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::GetRawString
const char * GetRawString() const
Definition
String.cpp:230
DotNetDupe::System::Security::Cryptography::X509Certificates
Definition
X509Certificate2.h:14
DotNetDupe::System::Security::Cryptography::X509Certificates::LoadPemKey
static EVP_PKEY * LoadPemKey(const String &keyPath)
Definition
X509Certificate2.cpp:49
DotNetDupe::System::Security::Cryptography::X509Certificates::InitializeCrypto
static void InitializeCrypto()
Definition
X509Certificate2.cpp:29
DotNetDupe::System::Security::Cryptography::X509Certificates::LoadPemCert
static X509 * LoadPemCert(const String &certPath)
Definition
X509Certificate2.cpp:37
DotNetDupe::System::Security::Cryptography::X509Certificates::s_cryptoInitOnce
static std::once_flag s_cryptoInitOnce
Definition
X509Certificate2.cpp:27
DotNetDupe::System::Security::Cryptography
Definition
HMACSHA256.h:13
DotNetDupe::System::Security
Definition
HMACSHA256.h:12
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
X509Certificate2.cpp
Generated by
1.18.0