Index: webrtc/base/opensslidentity.cc |
diff --git a/webrtc/base/opensslidentity.cc b/webrtc/base/opensslidentity.cc |
index 9c2112e157c0dc71099b57a764c58c4e48d4c28b..d86974569c40f58948a811e840871d410cc9f3ad 100644 |
--- a/webrtc/base/opensslidentity.cc |
+++ b/webrtc/base/opensslidentity.cc |
@@ -181,6 +181,28 @@ void OpenSSLKeyPair::AddReference() { |
#endif |
} |
+std::string OpenSSLKeyPair::PrivateKeyToPemString() const { |
+ BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
+ if (!temp_memory_bio) { |
+ LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
+ RTC_NOTREACHED(); |
+ return ""; |
+ } |
+ if (!PEM_write_bio_PrivateKey( |
+ temp_memory_bio, pkey_, nullptr, nullptr, 0, nullptr, nullptr)) { |
+ LOG_F(LS_ERROR) << "Failed to read private key"; |
+ BIO_free(temp_memory_bio); |
+ RTC_NOTREACHED(); |
+ return ""; |
+ } |
+ BIO_write(temp_memory_bio, "\0", 1); |
+ char* buffer; |
+ BIO_get_mem_data(temp_memory_bio, &buffer); |
+ std::string priv_key_str = buffer; |
+ BIO_free(temp_memory_bio); |
+ return priv_key_str; |
+} |
+ |
#if !defined(NDEBUG) |
// Print a certificate to the log, for debugging. |
static void PrintCert(X509* x509) { |
@@ -475,6 +497,10 @@ bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) { |
return true; |
} |
+std::string OpenSSLIdentity::PrivateKeyToPemString() const { |
+ return key_pair_->PrivateKeyToPemString(); |
+} |
+ |
} // namespace rtc |
#endif // HAVE_OPENSSL_SSL_H |