Blame SOURCES/0002-Use-OpenSSL-1.0-API.patch

f1d589
From 9b10377b668466b6d464883361c9ebb5bd499fc4 Mon Sep 17 00:00:00 2001
d744d0
From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= <jstanek@redhat.com>
f1d589
Date: Tue, 10 May 2022 14:45:36 +0200
d744d0
Subject: [PATCH] Use OpenSSL 1.0 API
d744d0
MIME-Version: 1.0
d744d0
Content-Type: text/plain; charset=UTF-8
d744d0
Content-Transfer-Encoding: 8bit
d744d0
d744d0
- Pass non-const pointer to BIO_new
d744d0
d744d0
  In legacy OpenSSL, the method parameter for BIO_new is not marked const,
d744d0
  although the function does not need it to be mutable.
d744d0
  This is likely an oversight in the interface.
d744d0
d744d0
  The provided "fix" is potentially dangerous,
d744d0
  as casting away `const`-ness is potentially an undefined behaviour.
d744d0
  Since the code around assumes it is constant anyway,
d744d0
  it *should* be fine here – but use with care.
d744d0
d744d0
- Remove const-classifier for SSL_SESSION callback argument
d744d0
d744d0
  In legacy OpenSSL, the parameter is expected to be mutable.
d744d0
  Using `const` prevents passing the method as a function pointer
d744d0
  to other OpenSSL API functions.
d744d0
b24b2a
- Provide GetCipherValue wrapper for non-const strings
b24b2a
f1d589
- Use non-const name getter for X509 objects
f1d589
d744d0
- Sanitize inputs into PBKDF2
d744d0
2509f6
- Setup ECDHE curve negotiation
2509f6
2509f6
  - SSL_OP_SINGLE_ECDH_USE is no-op and default in OpenSSL 1.1,
2509f6
    but should be specified in OpenSSL 1.0
2509f6
  - SSL_CTX_set_ecdh_auto() is presumably the same case;
2509f6
    does not even exist in OpenSSL 1.1
2509f6
2509f6
  Without this setup, ECDHE curve negotiation is broken: rhbz#1910749
2509f6
d744d0
Signed-off-by: Jan Staněk <jstanek@redhat.com>
d744d0
---
2509f6
 src/node_crypto.cc        | 30 ++++++++++++++++++++++++++++--
b24b2a
 src/node_crypto.h         |  4 ++++
b24b2a
 src/node_crypto_bio.cc    |  4 ++++
f1d589
 src/node_crypto_common.cc | 12 ++++++++++--
f1d589
 4 files changed, 46 insertions(+), 4 deletions(-)
d744d0
d744d0
diff --git a/src/node_crypto.cc b/src/node_crypto.cc
2509f6
index e472892b68..0cc97f99ea 100644
d744d0
--- a/src/node_crypto.cc
d744d0
+++ b/src/node_crypto.cc
d744d0
@@ -123,7 +123,11 @@ template int SSLWrap<TLSWrap>::SetCACerts(SecureContext* sc);
d744d0
 template void SSLWrap<TLSWrap>::MemoryInfo(MemoryTracker* tracker) const;
d744d0
 template SSL_SESSION* SSLWrap<TLSWrap>::GetSessionCallback(
d744d0
     SSL* s,
d744d0
+#if OPENSSL_IS_LEGACY
d744d0
+    unsigned char *key,
d744d0
+#else
d744d0
     const unsigned char* key,
d744d0
+#endif
d744d0
     int len,
d744d0
     int* copy);
d744d0
 template int SSLWrap<TLSWrap>::NewSessionCallback(SSL* s,
2509f6
@@ -1189,6 +1193,10 @@ void SecureContext::SetECDHCurve(const FunctionCallbackInfo<Value>& args) {
2509f6
   THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "ECDH curve name");
2509f6
 
2509f6
   node::Utf8Value curve(env->isolate(), args[0]);
2509f6
+#if OPENSSL_IS_LEGACY
2509f6
+  SSL_CTX_set_options(sc->ctx_.get(), SSL_OP_SINGLE_ECDH_USE);
2509f6
+  SSL_CTX_set_ecdh_auto(sc->ctx_.get(), 1);
2509f6
+#endif
2509f6
 
2509f6
   if (strcmp(*curve, "auto") == 0)
2509f6
     return;
2509f6
@@ -1746,7 +1754,11 @@ void SSLWrap<Base>::ConfigureSecureContext(SecureContext* sc) {
d744d0
 
d744d0
 template <class Base>
d744d0
 SSL_SESSION* SSLWrap<Base>::GetSessionCallback(SSL* s,
d744d0
+#if OPENSSL_IS_LEGACY
d744d0
+                                               unsigned char* key,
d744d0
+#else
d744d0
                                                const unsigned char* key,
d744d0
+#endif
d744d0
                                                int len,
d744d0
                                                int* copy) {
d744d0
   Base* w = static_cast<Base*>(SSL_get_app_data(s));
2509f6
@@ -5921,9 +5933,23 @@ struct PBKDF2Job : public CryptoJob {
d744d0
   }
d744d0
 
d744d0
   inline void DoThreadPoolWork() override {
d744d0
-    auto salt_data = reinterpret_cast<const unsigned char*>(salt.data());
d744d0
+    static const char * const empty = "";
d744d0
+
d744d0
+    auto pass_data = reinterpret_cast<const char *>(empty);
d744d0
+    auto pass_size = int(0);
d744d0
+    auto salt_data = reinterpret_cast<const unsigned char *>(empty);
d744d0
+    auto salt_size = int(0);
d744d0
+
d744d0
+    if (pass.size() > 0) {
d744d0
+      pass_data = pass.data(), pass_size = pass.size();
d744d0
+    }
d744d0
+    if (salt.size() > 0) {
d744d0
+      salt_data = reinterpret_cast<const unsigned char *>(salt.data());
d744d0
+      salt_size = salt.size();
d744d0
+    }
d744d0
+
d744d0
     const bool ok =
d744d0
-        PKCS5_PBKDF2_HMAC(pass.data(), pass.size(), salt_data, salt.size(),
d744d0
+        PKCS5_PBKDF2_HMAC(pass_data, pass_size, salt_data, salt_size,
d744d0
                           iteration_count, digest, keybuf_size, keybuf_data);
d744d0
     success = Just(ok);
d744d0
     Cleanse();
d744d0
diff --git a/src/node_crypto.h b/src/node_crypto.h
2509f6
index 7bce7706a9..780e1893f4 100644
d744d0
--- a/src/node_crypto.h
d744d0
+++ b/src/node_crypto.h
b24b2a
@@ -234,7 +234,11 @@ class SSLWrap {
d744d0
   static void AddMethods(Environment* env, v8::Local<v8::FunctionTemplate> t);
d744d0
 
d744d0
   static SSL_SESSION* GetSessionCallback(SSL* s,
d744d0
+#if OPENSSL_IS_LEGACY
d744d0
+                                         unsigned char* key,
d744d0
+#else // OPENSSL_IS_LEGACY
d744d0
                                          const unsigned char* key,
d744d0
+#endif // OPENSSL_IS_LEGACY
d744d0
                                          int len,
d744d0
                                          int* copy);
d744d0
   static int NewSessionCallback(SSL* s, SSL_SESSION* sess);
d744d0
diff --git a/src/node_crypto_bio.cc b/src/node_crypto_bio.cc
d744d0
index 55f5e8a5a3..c2a44fdb86 100644
d744d0
--- a/src/node_crypto_bio.cc
d744d0
+++ b/src/node_crypto_bio.cc
d744d0
@@ -31,7 +31,11 @@ namespace node {
d744d0
 namespace crypto {
d744d0
 
d744d0
 BIOPointer NodeBIO::New(Environment* env) {
d744d0
+#if OPENSSL_IS_LEGACY
d744d0
+  BIOPointer bio(BIO_new(const_cast<BIO_METHOD *>(GetMethod())));
d744d0
+#else
d744d0
   BIOPointer bio(BIO_new(GetMethod()));
d744d0
+#endif
d744d0
   if (bio && env != nullptr)
d744d0
     NodeBIO::FromBIO(bio.get())->env_ = env;
d744d0
   return bio;
b24b2a
diff --git a/src/node_crypto_common.cc b/src/node_crypto_common.cc
f1d589
index 8682e88642..2aab7477a3 100644
b24b2a
--- a/src/node_crypto_common.cc
b24b2a
+++ b/src/node_crypto_common.cc
f1d589
@@ -324,6 +324,14 @@ MaybeLocal<Value> GetCipherValue(Environment* env,
b24b2a
 
b24b2a
   return OneByteString(env->isolate(), getstr(cipher));
b24b2a
 }
b24b2a
+MaybeLocal<Value> GetCipherValue(Environment* env,
b24b2a
+    const SSL_CIPHER* cipher,
b24b2a
+    char* (*getstr)(const SSL_CIPHER* cipher)) {
b24b2a
+  if (cipher == nullptr) {
b24b2a
+    return Undefined(env->isolate());
b24b2a
+  }
b24b2a
+  return OneByteString(env->isolate(), const_cast<const char *>(getstr(cipher)));
b24b2a
+}
b24b2a
 
b24b2a
 MaybeLocal<Value> GetCipherName(Environment* env, const SSL_CIPHER* cipher) {
b24b2a
   return GetCipherValue(env, cipher, SSL_CIPHER_get_name);
f1d589
@@ -844,7 +852,7 @@ v8::MaybeLocal<v8::Value> GetInfoAccessString(
f1d589
   return ToV8Value(env, bio);
f1d589
 }
f1d589
 
f1d589
-template <X509_NAME* get_name(const X509*)>
f1d589
+template <X509_NAME* get_name(X509*)>
f1d589
 static MaybeLocal<Value> GetX509NameObject(Environment* env, X509* cert) {
f1d589
   X509_NAME* name = get_name(cert);
f1d589
   CHECK_NOT_NULL(name);
f1d589
@@ -867,7 +875,7 @@ static MaybeLocal<Value> GetX509NameObject(Environment* env, X509* cert) {
f1d589
     // anyway, and multi-value RDNs are rare, i.e., the vast majority of
f1d589
     // Relative Distinguished Names contains a single type-value pair only.
f1d589
     const ASN1_OBJECT* type = X509_NAME_ENTRY_get_object(entry);
f1d589
-    const ASN1_STRING* value = X509_NAME_ENTRY_get_data(entry);
f1d589
+    ASN1_STRING* value = X509_NAME_ENTRY_get_data(entry);
f1d589
 
f1d589
     // If OpenSSL knows the type, use the short name of the type as the key, and
f1d589
     // the numeric representation of the type's OID otherwise.
d744d0
-- 
f1d589
2.36.1
d744d0