Skip to content

Commit df09fa3

Browse files
authored
Merge pull request #853 from shinrich/ts-4619
TS-4619: intermediate chain loading can miss certificates.
2 parents 41c267d + 0ea0f21 commit df09fa3

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

iocore/net/SSLUtils.cc

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -170,31 +170,39 @@ SSL_locking_callback(int mode, int type, const char *file, int line)
170170
}
171171
#endif
172172

173-
#ifndef SSL_CTX_add0_chain_cert
174173
static bool
175-
SSL_CTX_add_extra_chain_cert_file(SSL_CTX *ctx, const char *chainfile)
174+
SSL_CTX_add_extra_chain_cert_bio(SSL_CTX *ctx, BIO *bio)
176175
{
177176
X509 *cert;
178-
scoped_BIO bio(BIO_new_file(chainfile, "r"));
179177

180178
for (;;) {
181-
cert = PEM_read_bio_X509_AUX(bio.get(), NULL, NULL, NULL);
179+
cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL);
182180

183181
if (!cert) {
184182
// No more the certificates in this file.
185183
break;
186184
}
187185

188-
// This transfers ownership of the cert (X509) to the SSL context, if successful.
186+
// This transfers ownership of the cert (X509) to the SSL context, if successful.
187+
#ifdef SSL_CTX_add0_chain_cert
188+
if (!SSL_CTX_add0_chain_cert(ctx, cert)) {
189+
#else
189190
if (!SSL_CTX_add_extra_chain_cert(ctx, cert)) {
191+
#endif
190192
X509_free(cert);
191193
return false;
192194
}
193195
}
194196

195197
return true;
196198
}
197-
#endif
199+
200+
static bool
201+
SSL_CTX_add_extra_chain_cert_file(SSL_CTX *ctx, const char *chainfile)
202+
{
203+
scoped_BIO bio(BIO_new_file(chainfile, "r"));
204+
return SSL_CTX_add_extra_chain_cert_bio(ctx, bio);
205+
}
198206

199207
bool
200208
ssl_session_timed_out(SSL_SESSION *session)
@@ -1626,17 +1634,7 @@ SSLInitServerContext(const SSLConfigParams *params, const ssl_user_config &sslMu
16261634
SSLConfigParams::load_ssl_file_cb(completeServerCertPath, CONFIG_FLAG_UNVERSIONED);
16271635
}
16281636
// Load up any additional chain certificates
1629-
X509 *ca;
1630-
while ((ca = PEM_read_bio_X509(bio.get(), NULL, 0, NULL))) {
1631-
#ifdef SSL_CTX_add0_chain_cert
1632-
if (!SSL_CTX_add0_chain_cert(ctx, ca)) {
1633-
#else
1634-
if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
1635-
#endif
1636-
X509_free(ca);
1637-
goto fail;
1638-
}
1639-
}
1637+
SSL_CTX_add_extra_chain_cert_bio(ctx, bio);
16401638

16411639
const char *keyPath = key_tok.getNext();
16421640
if (!SSLPrivateKeyHandler(ctx, params, completeServerCertPath, keyPath)) {
@@ -1651,15 +1649,7 @@ SSLInitServerContext(const SSLConfigParams *params, const ssl_user_config &sslMu
16511649
if (params->serverCertChainFilename) {
16521650
ats_scoped_str completeServerCertChainPath(
16531651
Layout::relative_to(params->serverCertPathOnly, params->serverCertChainFilename));
1654-
#ifdef SSL_CTX_add0_chain_cert
1655-
scoped_BIO bio(BIO_new_file(completeServerCertChainPath, "r"));
1656-
X509 *intermediate_cert = PEM_read_bio_X509(bio.get(), NULL, 0, NULL);
1657-
if (!intermediate_cert || !SSL_CTX_add0_chain_cert(ctx, intermediate_cert)) {
1658-
if (intermediate_cert)
1659-
X509_free(intermediate_cert);
1660-
#else
16611652
if (!SSL_CTX_add_extra_chain_cert_file(ctx, completeServerCertChainPath)) {
1662-
#endif
16631653
SSLError("failed to load global certificate chain from %s", (const char *)completeServerCertChainPath);
16641654
goto fail;
16651655
}
@@ -1672,15 +1662,7 @@ SSLInitServerContext(const SSLConfigParams *params, const ssl_user_config &sslMu
16721662
if (sslMultCertSettings.ca) {
16731663
const char *ca_name = ca_tok.getNext();
16741664
ats_scoped_str completeServerCertChainPath(Layout::relative_to(params->serverCertPathOnly, ca_name));
1675-
#ifdef SSL_CTX_add0_chain_cert
1676-
scoped_BIO bio(BIO_new_file(completeServerCertChainPath, "r"));
1677-
X509 *intermediate_cert = PEM_read_bio_X509(bio.get(), NULL, 0, NULL);
1678-
if (!intermediate_cert || !SSL_CTX_add0_chain_cert(ctx, intermediate_cert)) {
1679-
if (intermediate_cert)
1680-
X509_free(intermediate_cert);
1681-
#else
16821665
if (!SSL_CTX_add_extra_chain_cert_file(ctx, completeServerCertChainPath)) {
1683-
#endif
16841666
SSLError("failed to load certificate chain from %s", (const char *)completeServerCertChainPath);
16851667
goto fail;
16861668
}

0 commit comments

Comments
 (0)