DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
X509Certificate2.cpp
Go to the documentation of this file.
1#include "pch.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
21namespace 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) {
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
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
91 return m_pCert;
92 }
93
96 return m_pKey;
97 }
98
99 }
100 }
101 }
102 }
103}
Defines the exception thrown when an invalid argument is provided to a method.
The exception that is thrown when an I/O error occurs.
Represents an X.509 certificate and private key per RFC 5280.
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
The exception that is thrown when an I/O error occurs.
Definition IOException.h:17
~X509Certificate2()
Releases OpenSSL certificate and private key resources.
void * GetInternalCert() const
Gets the internal OpenSSL X509* pointer.
void * GetInternalKey() const
Gets the internal OpenSSL EVP_PKEY* pointer.
X509Certificate2(const String &certPath, const String &keyPath)
Initializes a new instance of X509Certificate2 from PEM certificate and private key files.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
const char * GetRawString() const
Definition String.cpp:230