소스 검색

Merge pull request #841 from paullouisageneau/openssl-upgrade-deprecated

Remove calls to functions deprecated in OpenSSL 3.0
Paul-Louis Ageneau 2 년 전
부모
커밋
3c466590ad
4개의 변경된 파일54개의 추가작업 그리고 39개의 파일을 삭제
  1. 23 11
      src/impl/certificate.cpp
  2. 18 14
      src/impl/dtlstransport.cpp
  3. 0 7
      src/impl/tls.hpp
  4. 13 7
      src/impl/tlstransport.cpp

+ 23 - 11
src/impl/certificate.cpp

@@ -314,6 +314,10 @@ Certificate::credentials() const {
 
 
 #else // OPENSSL
 #else // OPENSSL
 
 
+#include <openssl/bn.h>
+#include <openssl/ec.h>
+#include <openssl/rsa.h>
+
 namespace {
 namespace {
 
 
 // Dummy password callback that copies the password from user data
 // Dummy password callback that copies the password from user data
@@ -376,12 +380,12 @@ Certificate Certificate::Generate(CertificateType type, const string &commonName
 	PLOG_DEBUG << "Generating certificate (OpenSSL)";
 	PLOG_DEBUG << "Generating certificate (OpenSSL)";
 
 
 	shared_ptr<X509> x509(X509_new(), X509_free);
 	shared_ptr<X509> x509(X509_new(), X509_free);
-	shared_ptr<EVP_PKEY> pkey(EVP_PKEY_new(), EVP_PKEY_free);
 	unique_ptr<BIGNUM, decltype(&BN_free)> serial_number(BN_new(), BN_free);
 	unique_ptr<BIGNUM, decltype(&BN_free)> serial_number(BN_new(), BN_free);
 	unique_ptr<X509_NAME, decltype(&X509_NAME_free)> name(X509_NAME_new(), X509_NAME_free);
 	unique_ptr<X509_NAME, decltype(&X509_NAME_free)> name(X509_NAME_new(), X509_NAME_free);
-	if (!x509 || !pkey || !serial_number || !name)
+	if (!x509 || !serial_number || !name)
 		throw std::runtime_error("Unable to allocate structures for certificate generation");
 		throw std::runtime_error("Unable to allocate structures for certificate generation");
 
 
+	shared_ptr<EVP_PKEY> pkey;
 	switch (type) {
 	switch (type) {
 	// RFC 8827 WebRTC Security Architecture 6.5. Communications Security
 	// RFC 8827 WebRTC Security Architecture 6.5. Communications Security
 	// All implementations MUST support DTLS 1.2 with the TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
 	// All implementations MUST support DTLS 1.2 with the TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
@@ -390,35 +394,44 @@ Certificate Certificate::Generate(CertificateType type, const string &commonName
 	case CertificateType::Default:
 	case CertificateType::Default:
 	case CertificateType::Ecdsa: {
 	case CertificateType::Ecdsa: {
 		PLOG_VERBOSE << "Generating ECDSA P-256 key pair";
 		PLOG_VERBOSE << "Generating ECDSA P-256 key pair";
-
+#if OPENSSL_VERSION_NUMBER >= 0x30000000
+		pkey = shared_ptr<EVP_PKEY>(EVP_EC_gen("P-256"), EVP_PKEY_free);
+#else
+		pkey = shared_ptr<EVP_PKEY>(EVP_PKEY_new(), EVP_PKEY_free);
 		unique_ptr<EC_KEY, decltype(&EC_KEY_free)> ecc(
 		unique_ptr<EC_KEY, decltype(&EC_KEY_free)> ecc(
 		    EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), EC_KEY_free);
 		    EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), EC_KEY_free);
-		if (!ecc)
+		if (!pkey || !ecc)
 			throw std::runtime_error("Unable to allocate structure for ECDSA P-256 key pair");
 			throw std::runtime_error("Unable to allocate structure for ECDSA P-256 key pair");
 
 
 		EC_KEY_set_asn1_flag(ecc.get(), OPENSSL_EC_NAMED_CURVE); // Set ASN1 OID
 		EC_KEY_set_asn1_flag(ecc.get(), OPENSSL_EC_NAMED_CURVE); // Set ASN1 OID
 		if (!EC_KEY_generate_key(ecc.get()) ||
 		if (!EC_KEY_generate_key(ecc.get()) ||
 		    !EVP_PKEY_assign_EC_KEY(pkey.get(),
 		    !EVP_PKEY_assign_EC_KEY(pkey.get(),
 		                            ecc.release())) // the key will be freed when pkey is freed
 		                            ecc.release())) // the key will be freed when pkey is freed
+#endif
+		if (!pkey)
 			throw std::runtime_error("Unable to generate ECDSA P-256 key pair");
 			throw std::runtime_error("Unable to generate ECDSA P-256 key pair");
 
 
 		break;
 		break;
 	}
 	}
 	case CertificateType::Rsa: {
 	case CertificateType::Rsa: {
 		PLOG_VERBOSE << "Generating RSA key pair";
 		PLOG_VERBOSE << "Generating RSA key pair";
-
-		const int bits = 2048;
-		const unsigned int e = 65537; // 2^16 + 1
-
+		const unsigned int bits = 2048;
+#if OPENSSL_VERSION_NUMBER >= 0x30000000
+		pkey = shared_ptr<EVP_PKEY>(EVP_RSA_gen(bits), EVP_PKEY_free);
+#else
+		pkey = shared_ptr<EVP_PKEY>(EVP_PKEY_new(), EVP_PKEY_free);
 		unique_ptr<RSA, decltype(&RSA_free)> rsa(RSA_new(), RSA_free);
 		unique_ptr<RSA, decltype(&RSA_free)> rsa(RSA_new(), RSA_free);
 		unique_ptr<BIGNUM, decltype(&BN_free)> exponent(BN_new(), BN_free);
 		unique_ptr<BIGNUM, decltype(&BN_free)> exponent(BN_new(), BN_free);
-		if (!rsa || !exponent)
+		if (!pkey || !rsa || !exponent)
 			throw std::runtime_error("Unable to allocate structures for RSA key pair");
 			throw std::runtime_error("Unable to allocate structures for RSA key pair");
 
 
+		const unsigned int e = 65537;               // 2^16 + 1
 		if (!BN_set_word(exponent.get(), e) ||
 		if (!BN_set_word(exponent.get(), e) ||
 		    !RSA_generate_key_ex(rsa.get(), bits, exponent.get(), NULL) ||
 		    !RSA_generate_key_ex(rsa.get(), bits, exponent.get(), NULL) ||
 		    !EVP_PKEY_assign_RSA(pkey.get(),
 		    !EVP_PKEY_assign_RSA(pkey.get(),
 		                         rsa.release())) // the key will be freed when pkey is freed
 		                         rsa.release())) // the key will be freed when pkey is freed
+#endif
+		if (!pkey)
 			throw std::runtime_error("Unable to generate RSA key pair");
 			throw std::runtime_error("Unable to generate RSA key pair");
 
 
 		break;
 		break;
@@ -436,8 +449,7 @@ Certificate Certificate::Generate(CertificateType type, const string &commonName
 
 
 	if (!X509_gmtime_adj(X509_getm_notBefore(x509.get()), 3600 * -1) ||
 	if (!X509_gmtime_adj(X509_getm_notBefore(x509.get()), 3600 * -1) ||
 	    !X509_gmtime_adj(X509_getm_notAfter(x509.get()), 3600 * 24 * 365) ||
 	    !X509_gmtime_adj(X509_getm_notAfter(x509.get()), 3600 * 24 * 365) ||
-	    !X509_set_version(x509.get(), 1) ||
-	    !BN_pseudo_rand(serial_number.get(), serialSize, 0, 0) ||
+	    !X509_set_version(x509.get(), 1) || !BN_rand(serial_number.get(), serialSize, 0, 0) ||
 	    !BN_to_ASN1_INTEGER(serial_number.get(), X509_get_serialNumber(x509.get())) ||
 	    !BN_to_ASN1_INTEGER(serial_number.get(), X509_get_serialNumber(x509.get())) ||
 	    !X509_NAME_add_entry_by_NID(name.get(), NID_commonName, MBSTRING_UTF8, commonNameBytes, -1,
 	    !X509_NAME_add_entry_by_NID(name.get(), NID_commonName, MBSTRING_UTF8, commonNameBytes, -1,
 	                                -1, 0) ||
 	                                -1, 0) ||

+ 18 - 14
src/impl/dtlstransport.cpp

@@ -525,12 +525,14 @@ void DtlsTransport::doRecv() {
 			while (true) {
 			while (true) {
 				auto ret = mbedtls_ssl_handshake(&mSsl);
 				auto ret = mbedtls_ssl_handshake(&mSsl);
 				if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
 				if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
-				ThreadPool::Instance().schedule(mTimerSetAt + milliseconds(mFinMs), [weak_this = weak_from_this()]() {
-					if (auto locked = weak_this.lock())
-						locked->doRecv();
-					});
-				return;
-				} else if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS || ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
+					ThreadPool::Instance().schedule(mTimerSetAt + milliseconds(mFinMs),
+					                                [weak_this = weak_from_this()]() {
+						                                if (auto locked = weak_this.lock())
+							                                locked->doRecv();
+					                                });
+					return;
+				} else if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ||
+				           ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
 					continue;
 					continue;
 				}
 				}
 
 
@@ -710,8 +712,7 @@ DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, certificate_ptr cer
 
 
 		SSL_CTX_set_min_proto_version(mCtx, DTLS1_VERSION);
 		SSL_CTX_set_min_proto_version(mCtx, DTLS1_VERSION);
 		SSL_CTX_set_read_ahead(mCtx, 1);
 		SSL_CTX_set_read_ahead(mCtx, 1);
-		// sent the dtls close_notify alert
-		// SSL_CTX_set_quiet_shutdown(mCtx, 1);
+		SSL_CTX_set_quiet_shutdown(mCtx, 0); // sent the dtls close_notify alert
 		SSL_CTX_set_info_callback(mCtx, InfoCallback);
 		SSL_CTX_set_info_callback(mCtx, InfoCallback);
 
 
 		SSL_CTX_set_verify(mCtx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
 		SSL_CTX_set_verify(mCtx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
@@ -721,10 +722,18 @@ DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, certificate_ptr cer
 		openssl::check(SSL_CTX_set_cipher_list(mCtx, "ALL:!LOW:!EXP:!RC4:!MD5:@STRENGTH"),
 		openssl::check(SSL_CTX_set_cipher_list(mCtx, "ALL:!LOW:!EXP:!RC4:!MD5:@STRENGTH"),
 		               "Failed to set SSL priorities");
 		               "Failed to set SSL priorities");
 
 
+#if OPENSSL_VERSION_NUMBER >= 0x30000000
+		openssl::check(SSL_CTX_set1_groups_list(mCtx, "P-256"), "Failed to set SSL groups");
+#else
+		auto ecdh = unique_ptr<EC_KEY, decltype(&EC_KEY_free)>(
+		    EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), EC_KEY_free);
+		SSL_CTX_set_tmp_ecdh(mCtx, ecdh.get());
+		SSL_CTX_set_options(mCtx, SSL_OP_SINGLE_ECDH_USE);
+#endif
+
 		auto [x509, pkey] = mCertificate->credentials();
 		auto [x509, pkey] = mCertificate->credentials();
 		SSL_CTX_use_certificate(mCtx, x509);
 		SSL_CTX_use_certificate(mCtx, x509);
 		SSL_CTX_use_PrivateKey(mCtx, pkey);
 		SSL_CTX_use_PrivateKey(mCtx, pkey);
-
 		openssl::check(SSL_CTX_check_private_key(mCtx), "SSL local private key check failed");
 		openssl::check(SSL_CTX_check_private_key(mCtx), "SSL local private key check failed");
 
 
 		mSsl = SSL_new(mCtx);
 		mSsl = SSL_new(mCtx);
@@ -747,11 +756,6 @@ DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, certificate_ptr cer
 		BIO_set_data(mOutBio, this);
 		BIO_set_data(mOutBio, this);
 		SSL_set_bio(mSsl, mInBio, mOutBio);
 		SSL_set_bio(mSsl, mInBio, mOutBio);
 
 
-		auto ecdh = unique_ptr<EC_KEY, decltype(&EC_KEY_free)>(
-		    EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), EC_KEY_free);
-		SSL_set_options(mSsl, SSL_OP_SINGLE_ECDH_USE);
-		SSL_set_tmp_ecdh(mSsl, ecdh.get());
-
 		// RFC 8827: The DTLS-SRTP protection profile SRTP_AES128_CM_HMAC_SHA1_80 MUST be supported
 		// RFC 8827: The DTLS-SRTP protection profile SRTP_AES128_CM_HMAC_SHA1_80 MUST be supported
 		// See https://www.rfc-editor.org/rfc/rfc8827.html#section-6.5 Warning:
 		// See https://www.rfc-editor.org/rfc/rfc8827.html#section-6.5 Warning:
 		// SSL_set_tlsext_use_srtp() returns 0 on success and 1 on error
 		// SSL_set_tlsext_use_srtp() returns 0 on success and 1 on error

+ 0 - 7
src/impl/tls.hpp

@@ -68,18 +68,11 @@ std::shared_ptr<mbedtls_x509_crt> new_x509_crt();
 #include <winsock2.h>
 #include <winsock2.h>
 #endif
 #endif
 
 
-#ifndef OPENSSL_API_COMPAT
-#define OPENSSL_API_COMPAT 0x10100000L
-#endif
-
 #include <openssl/ssl.h>
 #include <openssl/ssl.h>
 
 
 #include <openssl/bio.h>
 #include <openssl/bio.h>
-#include <openssl/bn.h>
-#include <openssl/ec.h>
 #include <openssl/err.h>
 #include <openssl/err.h>
 #include <openssl/pem.h>
 #include <openssl/pem.h>
-#include <openssl/rsa.h>
 #include <openssl/x509.h>
 #include <openssl/x509.h>
 
 
 #ifndef BIO_EOF
 #ifndef BIO_EOF

+ 13 - 7
src/impl/tlstransport.cpp

@@ -418,7 +418,8 @@ void TlsTransport::doRecv() {
 				auto ret = mbedtls_ssl_handshake(&mSsl);
 				auto ret = mbedtls_ssl_handshake(&mSsl);
 				if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
 				if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
 					return;
 					return;
-				} else if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS || ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
+				} else if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ||
+				           ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
 					continue;
 					continue;
 				}
 				}
 
 
@@ -443,7 +444,8 @@ void TlsTransport::doRecv() {
 
 
 				if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
 				if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
 					return;
 					return;
-				} else if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS || ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
+				} else if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ||
+				           ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
 					continue;
 					continue;
 				}
 				}
 				mbedtls::check(ret);
 				mbedtls::check(ret);
@@ -540,6 +542,15 @@ TlsTransport::TlsTransport(variant<shared_ptr<TcpTransport>, shared_ptr<HttpProx
 		openssl::check(SSL_CTX_set_cipher_list(mCtx, "ALL:!LOW:!EXP:!RC4:!MD5:@STRENGTH"),
 		openssl::check(SSL_CTX_set_cipher_list(mCtx, "ALL:!LOW:!EXP:!RC4:!MD5:@STRENGTH"),
 		               "Failed to set SSL priorities");
 		               "Failed to set SSL priorities");
 
 
+#if OPENSSL_VERSION_NUMBER >= 0x30000000
+		openssl::check(SSL_CTX_set1_groups_list(mCtx, "P-256"), "Failed to set SSL groups");
+#else
+		auto ecdh = unique_ptr<EC_KEY, decltype(&EC_KEY_free)>(
+		    EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), EC_KEY_free);
+		SSL_CTX_set_tmp_ecdh(mCtx, ecdh.get());
+		SSL_CTX_set_options(mCtx, SSL_OP_SINGLE_ECDH_USE);
+#endif
+
 		if (certificate) {
 		if (certificate) {
 			auto [x509, pkey] = certificate->credentials();
 			auto [x509, pkey] = certificate->credentials();
 			SSL_CTX_use_certificate(mCtx, x509);
 			SSL_CTX_use_certificate(mCtx, x509);
@@ -582,11 +593,6 @@ TlsTransport::TlsTransport(variant<shared_ptr<TcpTransport>, shared_ptr<HttpProx
 		BIO_set_mem_eof_return(mOutBio, BIO_EOF);
 		BIO_set_mem_eof_return(mOutBio, BIO_EOF);
 		SSL_set_bio(mSsl, mInBio, mOutBio);
 		SSL_set_bio(mSsl, mInBio, mOutBio);
 
 
-		auto ecdh = unique_ptr<EC_KEY, decltype(&EC_KEY_free)>(
-		    EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), EC_KEY_free);
-		SSL_set_options(mSsl, SSL_OP_SINGLE_ECDH_USE);
-		SSL_set_tmp_ecdh(mSsl, ecdh.get());
-
 	} catch (...) {
 	} catch (...) {
 		if (mSsl)
 		if (mSsl)
 			SSL_free(mSsl);
 			SSL_free(mSsl);