Skip to content

Commit b657b46

Browse files
committed
WL#15135 second set of post-push fixes
Add more stub functions for platforms with older versions of OpenSSL. Improve handling of environment variables in TLS Search Path. Disable testTlsKeyManager-t on systems with OpenSSL 1.0.x. Change-Id: Ide1662bc17cd365e7c270b42082ccaeb8b870ecb
1 parent 88d1edb commit b657b46

File tree

5 files changed

+83
-35
lines changed

5 files changed

+83
-35
lines changed

storage/ndb/include/util/ndb_openssl3_compat.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ EVP_PKEY * EVP_EC_generate(const char * curve);
4444

4545
#endif /* OPENSSL_VERSION_NUMBER */
4646

47-
/* These stub functions allow NodeCertificate.cpp to compile with old OpenSSL */
47+
/* These stub functions allow NDB TLS code to compile with OpenSSL 1.0.x */
4848
#if OPENSSL_VERSION_NUMBER < NDB_TLS_MINIMUM_OPENSSL
4949
#include <openssl/x509.h>
5050
#include <openssl/x509v3.h>
@@ -53,18 +53,24 @@ const ASN1_INTEGER *X509_get0_serialNumber(const X509 *);
5353
#ifndef X509_getm_notBefore
5454
#define X509_getm_notBefore X509_get_notBefore
5555
#define X509_getm_notAfter X509_get_notAfter
56+
#define __NEED_STUB_ASN1_TIME_GET0_FUNCTIONS 1
57+
const ASN1_TIME *X509_get0_notBefore(const X509 *);
58+
const ASN1_TIME *X509_get0_notAfter(const X509 *);
5659
#endif
5760

58-
EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *);
59-
void X509_get0_signature(const ASN1_BIT_STRING **, const X509_ALGOR **,
60-
const X509 *);
61+
EVP_PKEY *X509_get0_pubkey(X509 *);
62+
EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *);
63+
inline void X509_get0_signature(const ASN1_BIT_STRING **, const X509_ALGOR **,
64+
const X509 *) {}
6165
int X509_get_signature_info(X509 *, int *, int *, int *, uint32_t *);
6266
X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *,
6367
X509V3_CTX *, int, const char *);
6468
int ASN1_TIME_to_tm(const ASN1_TIME *, struct tm *);
6569
int EVP_PKEY_up_ref(EVP_PKEY *);
6670
int X509_up_ref(X509 *);
6771

72+
inline SSL_METHOD * TLS_method() { return nullptr; }
73+
6874
#endif
6975

7076
#endif /* NDB_PORTLIB_OPENSSL_COMPAT_H */

storage/ndb/src/common/util/NodeCertificate.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ static void expand(BaseString & result, const BaseString & path, int envStart) {
8484
c = item[envEnd];
8585
}
8686

87-
BaseString envVar = path.substr(envStart + 1, envEnd);
8887
if(envStart > 0)
8988
result.assign(path.substr(0, envStart));
90-
result.append(getenv(envVar.c_str()));
89+
if(envEnd - envStart > 1) {
90+
BaseString envVar = path.substr(envStart + 1, envEnd);
91+
result.append(getenv(envVar.c_str()));
92+
} else {
93+
result.append('$');
94+
}
9195
result.append(path.substr(envEnd));
9296
}
9397

@@ -112,6 +116,10 @@ TlsSearchPath::TlsSearchPath(const char * path_str) {
112116
expand(expansion, m_path[i], envStart);
113117
if(expansion.length())
114118
m_path[i] = expansion;
119+
else {
120+
m_path.erase(i);
121+
i--;
122+
}
115123
}
116124
}
117125
}
@@ -600,7 +608,7 @@ size_t Certificate::get_common_name(X509 * cert, char * buf, size_t len) {
600608

601609
int Certificate::get_signature_prefix(X509 * cert) {
602610
int prefix = 0;
603-
const ASN1_BIT_STRING * sig;
611+
const ASN1_BIT_STRING * sig = nullptr;
604612
const X509_ALGOR * algorithm;
605613
X509_get0_signature(& sig, & algorithm, cert);
606614
if(sig && sig->data)
@@ -1624,7 +1632,7 @@ inline bool test_expansion(const char * path, const char * expansion) {
16241632
}
16251633

16261634
static int search_path_test() {
1627-
BaseString pathStr(isWin32 ? "$HOMEPATH" : "$HOME");
1635+
BaseString pathStr("$TMPDIR");
16281636
pathStr.append(TlsSearchPath::Separator);
16291637
pathStr.append(MYSQL_DATADIR);
16301638
pathStr.append(TlsSearchPath::Separator);
@@ -1670,19 +1678,32 @@ static int search_path_test() {
16701678
if(searchPath2.size() != 1) return 13;
16711679
if(! searchPath2.first_writable()) return 14;
16721680

1681+
/* If the character following $ is not alnum or _, do not expand
1682+
$VAR expands correctly if VAR is set
1683+
$VAR expands to nothing if VAR is not set
1684+
a:my$SUFFIX expands to a:my if SUFFIX is not set
1685+
a:$VAR:b expands to a:b if $VAR is not set
1686+
*/
16731687
if(! test_expansion("$", "$")) return 15;
16741688
if(! test_expansion("$$", "$$")) return 16;
16751689
if(! test_expansion("$#", "$#")) return 17;
16761690
if(isWin32) {
1677-
if(! test_expansion("f;abc$", "f;abc")) return 18;
1691+
if(! test_expansion("f;abc$", "f;abc$")) return 18;
16781692
if(! test_expansion("a;$;b", "a;$;b")) return 19;
16791693
if(! test_expansion("a;$", "a;$")) return 20;
1680-
_putenv("DRIVE=A");
1681-
if(! test_expansion("$DRIVE:/tls", "A:/tls")) return 21;
1694+
_putenv("ARMAGOGLYPOD=A");
1695+
if(! test_expansion("$ARMAGOGLYPOD:/tls", "A:/tls")) return 21;
1696+
_putenv("ARMAGOGLYPOD=");
1697+
if(! test_expansion("$ARMAGOGLYPOD", "")) return 22;
1698+
if(! test_expansion("a;$ARMAGOGLYPOD;b", "a;b")) return 23;
1699+
if(! test_expansion("a;my$ARMAGOGLYPOD", "a;my")) return 24;
16821700
} else {
1683-
if(! test_expansion("f:abc$", "f:abc")) return 18;
1701+
if(! test_expansion("f:abc$", "f:abc$")) return 18;
16841702
if(! test_expansion("a:$:b", "a:$:b")) return 19;
16851703
if(! test_expansion("a:$", "a:$")) return 20;
1704+
if(! test_expansion("$ARMAGOGLYPOD", "")) return 22;
1705+
if(! test_expansion("a:$ARMAGOGLYPOD:b", "a:b")) return 23;
1706+
if(! test_expansion("a:my$ARMAGOGLYPOD", "a:my")) return 24;
16861707
}
16871708

16881709
return 0;

storage/ndb/src/common/util/TlsKeyManager.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ static constexpr const char * cipher_list =
4848
"TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256:TLS_AES_128_CCM_8_SHA256:"
4949
"ECDHE-ECDSA-AES128-GCM-SHA256";
5050

51-
static int error_callback(const char *str, size_t, void * vp) {
52-
intptr_t r = reinterpret_cast<intptr_t>(vp);
53-
g_eventLogger->error("NDB TLS [%" PRIuPTR "]: %s", r, str);
54-
return 0;
55-
}
56-
57-
static void log_openssl_errors(intptr_t r) {
58-
ERR_print_errors_cb(error_callback, reinterpret_cast<void *>(r));
59-
}
60-
6151
TlsKeyManager::TlsKeyManager() {
6252
NdbMutex_Init(& m_cert_table_mutex);
6353
}
@@ -88,17 +78,28 @@ void TlsKeyManager::log_error() const {
8878
TlsKeyError::message(m_error), m_path_string);
8979
}
9080

91-
9281
#if OPENSSL_VERSION_NUMBER < NDB_TLS_MINIMUM_OPENSSL
93-
void TlsKeyManager::init(const char *, int node_id, Node::Type, UserType)
94-
{
95-
if(node_id > 0)
96-
g_eventLogger->error("NDB TLS: OpenSSL version '%s' is not supported",
97-
OPENSSL_VERSION_TEXT);
98-
}
82+
83+
void TlsKeyManager::init(int, const NodeCertificate *) { }
84+
85+
void TlsKeyManager::init(const char *, int, int, bool) { }
86+
87+
void TlsKeyManager::init(const char *, int, Node::Type, UserType) { }
88+
89+
void TlsKeyManager::init(int, struct stack_st_X509 *, struct evp_pkey_st *) { }
9990

10091
#else
10192

93+
static int error_callback(const char *str, size_t, void * vp) {
94+
intptr_t r = reinterpret_cast<intptr_t>(vp);
95+
g_eventLogger->error("NDB TLS [%" PRIuPTR "]: %s", r, str);
96+
return 0;
97+
}
98+
99+
static void log_openssl_errors(intptr_t r) {
100+
ERR_print_errors_cb(error_callback, reinterpret_cast<void *>(r));
101+
}
102+
102103
void TlsKeyManager::init(const char * tls_search_path, int node_id,
103104
int ndb_node_type, bool is_primary) {
104105
UserType userType = is_primary ? Primary : SecondaryApi;
@@ -137,7 +138,6 @@ void TlsKeyManager::init(const char * tls_search_path, int node_id,
137138
"(node id %d)", node_id);
138139
}
139140
}
140-
#endif
141141

142142
/* Versions of init() used by test harness */
143143
void TlsKeyManager::init(int node_id, STACK_OF(X509) * certs, EVP_PKEY * key) {
@@ -250,6 +250,8 @@ void TlsKeyManager::initialize_context() {
250250
cert_table_set(m_node_id, m_node_cert.cert());
251251
}
252252

253+
#endif
254+
253255
int TlsKeyManager::on_verify(int result, X509_STORE_CTX * store) {
254256
/* If result is 0, verification has failed, and this callback is
255257
our opportunity to write a log message.

storage/ndb/src/common/util/ndb_openssl3_compat.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,18 @@ const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x) {
107107
return X509_get_serialNumber(const_cast<X509 *>(x));
108108
}
109109

110-
EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *csr) {
111-
EVP_PKEY * key = X509_REQ_get_pubkey(csr);
110+
EVP_PKEY *X509_get0_pubkey(X509 *x) {
111+
EVP_PKEY * key = X509_get_pubkey(x);
112112
if(key)
113113
EVP_PKEY_free(key);
114114
return key;
115115
}
116116

117-
void X509_get0_signature(const ASN1_BIT_STRING ** sig, const X509_ALGOR ** alg,
118-
const X509 * x) {
119-
X509_get0_signature(sig, const_cast<X509_ALGOR **>(alg), x);
117+
EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *csr) {
118+
EVP_PKEY * key = X509_REQ_get_pubkey(csr);
119+
if(key)
120+
EVP_PKEY_free(key);
121+
return key;
120122
}
121123

122124
int X509_get_signature_info(X509 *, int *, int *, int *, uint32_t *) {
@@ -141,5 +143,14 @@ int X509_up_ref(X509 *) {
141143
return 0;
142144
}
143145

146+
#ifdef __NEED_STUB_ASN1_TIME_GET0_FUNCTIONS
147+
const ASN1_TIME *X509_get0_notBefore(const X509 *x) {
148+
return X509_get_notBefore(x);
149+
}
150+
151+
const ASN1_TIME *X509_get0_notAfter(const X509 *x) {
152+
return X509_get_notAfter(x);
153+
}
154+
#endif // __NEED_STUB_ASN1_TIME_GET0_FUNCTIONS
144155

145156
#endif

storage/ndb/src/common/util/testTlsKeyManager.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ int main(int argc, char** argv) {
723723
int r = opts.handle_options();
724724
if(r) return r;
725725

726+
#if OPENSSL_VERSION_NUMBER >= NDB_TLS_MINIMUM_OPENSSL
727+
726728
Test::CertAuthority ca;
727729
ca.sign(ca); // self-signed
728730

@@ -746,6 +748,12 @@ int main(int argc, char** argv) {
746748

747749
test_key_replace(ca);
748750

751+
#else
752+
753+
printf("Test disabled: OpenSSL version too old.\n");
754+
755+
#endif
756+
749757
ndb_end(0);
750758
return exit_status();
751759
}

0 commit comments

Comments
 (0)