|
@@ -141,14 +141,9 @@ string make_fingerprint(gnutls_x509_crt_t crt) {
|
|
|
return oss.str();
|
|
|
}
|
|
|
|
|
|
-shared_ptr<Certificate> make_certificate(const string &commonName) {
|
|
|
- static std::unordered_map<string, shared_ptr<Certificate>> cache;
|
|
|
- static std::mutex cacheMutex;
|
|
|
-
|
|
|
- std::lock_guard lock(cacheMutex);
|
|
|
- if (auto it = cache.find(commonName); it != cache.end())
|
|
|
- return it->second;
|
|
|
+namespace {
|
|
|
|
|
|
+certificate_ptr make_certificate_impl(string commonName) {
|
|
|
std::unique_ptr<gnutls_x509_crt_t, decltype(&delete_crt)> crt(create_crt(), delete_crt);
|
|
|
std::unique_ptr<gnutls_x509_privkey_t, decltype(&delete_privkey)> privkey(create_privkey(),
|
|
|
delete_privkey);
|
|
@@ -174,11 +169,11 @@ shared_ptr<Certificate> make_certificate(const string &commonName) {
|
|
|
check_gnutls(gnutls_x509_crt_sign2(*crt, *crt, *privkey, GNUTLS_DIG_SHA256, 0),
|
|
|
"Unable to auto-sign certificate");
|
|
|
|
|
|
- auto certificate = std::make_shared<Certificate>(*crt, *privkey);
|
|
|
- cache.emplace(std::make_pair(commonName, certificate));
|
|
|
- return certificate;
|
|
|
+ return std::make_shared<Certificate>(*crt, *privkey);
|
|
|
}
|
|
|
|
|
|
+} // namespace
|
|
|
+
|
|
|
} // namespace rtc
|
|
|
|
|
|
#else
|
|
@@ -236,15 +231,9 @@ string make_fingerprint(X509 *x509) {
|
|
|
return oss.str();
|
|
|
}
|
|
|
|
|
|
+namespace {
|
|
|
|
|
|
-shared_ptr<Certificate> make_certificate(const string &commonName) {
|
|
|
- static std::unordered_map<string, shared_ptr<Certificate>> cache;
|
|
|
- static std::mutex cacheMutex;
|
|
|
-
|
|
|
- std::lock_guard lock(cacheMutex);
|
|
|
- if (auto it = cache.find(commonName); it != cache.end())
|
|
|
- return it->second;
|
|
|
-
|
|
|
+certificate_ptr make_certificate_impl(string commonName) {
|
|
|
shared_ptr<X509> x509(X509_new(), X509_free);
|
|
|
shared_ptr<EVP_PKEY> pkey(EVP_PKEY_new(), EVP_PKEY_free);
|
|
|
|
|
@@ -281,12 +270,32 @@ shared_ptr<Certificate> make_certificate(const string &commonName) {
|
|
|
if (!X509_sign(x509.get(), pkey.get(), EVP_sha256()))
|
|
|
throw std::runtime_error("Unable to auto-sign certificate");
|
|
|
|
|
|
- auto certificate = std::make_shared<Certificate>(x509, pkey);
|
|
|
- cache.emplace(std::make_pair(commonName, certificate));
|
|
|
- return certificate;
|
|
|
+ return std::make_shared<Certificate>(x509, pkey);
|
|
|
}
|
|
|
|
|
|
+} // namespace
|
|
|
+
|
|
|
} // namespace rtc
|
|
|
|
|
|
#endif
|
|
|
|
|
|
+// Common for GnuTLS and OpenSSL
|
|
|
+
|
|
|
+namespace rtc {
|
|
|
+
|
|
|
+future_certificate_ptr make_certificate(string commonName) {
|
|
|
+ static std::unordered_map<string, future_certificate_ptr> cache;
|
|
|
+ static std::mutex cacheMutex;
|
|
|
+
|
|
|
+ std::lock_guard lock(cacheMutex);
|
|
|
+
|
|
|
+ if (auto it = cache.find(commonName); it != cache.end())
|
|
|
+ return it->second;
|
|
|
+
|
|
|
+ auto future = std::async(make_certificate_impl, commonName);
|
|
|
+ auto shared = future.share();
|
|
|
+ cache.emplace(std::move(commonName), shared);
|
|
|
+ return shared;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace rtc
|