Skip to content

Commit df65d40

Browse files
committed
Only support getpeercertchain(validate=True) with OpenSSL 1.1.0+
1 parent 8586656 commit df65d40

File tree

2 files changed

+21
-62
lines changed

2 files changed

+21
-62
lines changed

Lib/test/test_ssl.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,24 +2146,34 @@ def test_getpeercertchain(self):
21462146
try:
21472147
peer_cert = s.getpeercert()
21482148
peer_cert_bin = s.getpeercert(True)
2149-
chain = s.getpeercertchain()
2150-
chain_bin = s.getpeercertchain(True)
2149+
if IS_OPENSSL_1_1_0:
2150+
chain = s.getpeercertchain()
2151+
chain_bin = s.getpeercertchain(True)
2152+
else:
2153+
self.assertRaisesRegex(
2154+
Exception, r'only supported by OpenSSL 1\.1\.0',
2155+
s.getpeercertchain)
2156+
self.assertRaisesRegex(
2157+
Exception, r'only supported by OpenSSL 1\.1\.0',
2158+
s.getpeercertchain, True)
21512159
chain_no_validate = s.getpeercertchain(validate=False)
21522160
chain_bin_no_validate = s.getpeercertchain(True, False)
21532161
finally:
21542162
self.assertTrue(peer_cert)
2155-
self.assertEqual(len(chain), 2)
21562163
self.assertTrue(peer_cert_bin)
2157-
self.assertEqual(len(chain_bin), 2)
2164+
if IS_OPENSSL_1_1_0:
2165+
self.assertEqual(len(chain), 2)
2166+
self.assertEqual(len(chain_bin), 2)
21582167

21592168
# ca cert
21602169
ca_certs = ctx.get_ca_certs()
21612170
self.assertEqual(len(ca_certs), 1)
21622171
test_get_ca_certsert = ca_certs[0]
21632172
ca_cert_bin = ctx.get_ca_certs(True)[0]
21642173

2165-
self.assertEqual(chain, (peer_cert, test_get_ca_certsert))
2166-
self.assertEqual(chain_bin, (peer_cert_bin, ca_cert_bin))
2174+
if IS_OPENSSL_1_1_0:
2175+
self.assertEqual(chain, (peer_cert, test_get_ca_certsert))
2176+
self.assertEqual(chain_bin, (peer_cert_bin, ca_cert_bin))
21672177
self.assertEqual(chain_no_validate, (peer_cert,))
21682178
self.assertEqual(chain_bin_no_validate, (peer_cert_bin,))
21692179

Modules/_ssl.c

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,57 +2119,11 @@ _ssl__SSLSocket_getpeercertchain_impl(PySSLSocket *self, int binary_mode,
21192119
return NULL;
21202120
}
21212121
#else
2122-
X509 *peer_cert = SSL_get_peer_certificate(self->ssl);
2123-
if (peer_cert == NULL)
2124-
Py_RETURN_NONE;
2125-
2126-
STACK_OF(X509) *chain = SSL_get_peer_cert_chain(self->ssl);
2127-
if (chain == NULL) {
2128-
X509_free(peer_cert);
2129-
Py_RETURN_NONE;
2130-
}
2131-
X509_STORE_CTX *store_ctx;
2132-
2133-
/* Initialize a store context with store (for root CA certs), the
2134-
* peer's cert and the peer's chain with intermediate CA certs. */
2135-
if ((store_ctx = X509_STORE_CTX_new()) == NULL) {
2136-
X509_free(peer_cert);
2137-
_setSSLError(NULL, 0, __FILE__, __LINE__);
2138-
return NULL;
2139-
}
2140-
2141-
if (!X509_STORE_CTX_init(store_ctx,
2142-
SSL_CTX_get_cert_store(self->ctx->ctx),
2143-
peer_cert, chain)) {
2144-
#ifdef SSL_R_CERTIFICATE_VERIFY_FAILED
2145-
long e = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_CERTIFICATE_VERIFY_FAILED);
2146-
#else
2147-
long e = ERR_PACK(ERR_LIB_SSL, 0, 134);
2148-
#endif
2149-
fill_and_set_sslerror(self, PySSLCertVerificationErrorObject, PY_SSL_ERROR_SSL, NULL, __LINE__, e);
2150-
X509_free(peer_cert);
2151-
X509_STORE_CTX_free(store_ctx);
2152-
goto end;
2153-
}
2154-
X509_free(peer_cert);
2155-
2156-
/* Validate peer cert using its intermediate CA certs and the
2157-
* context's root CA certs. */
2158-
if (X509_verify_cert(store_ctx) <= 0) {
2159-
// _setX509StoreContextError(self, store_ctx, __FILE__, __LINE__);
2160-
#ifdef SSL_R_CERTIFICATE_VERIFY_FAILED
2161-
long e = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_CERTIFICATE_VERIFY_FAILED);
2162-
#else
2163-
long e = ERR_PACK(ERR_LIB_SSL, 0, 134);
2164-
#endif
2165-
fill_and_set_sslerror(self, PySSLCertVerificationErrorObject, PY_SSL_ERROR_SSL, NULL, __LINE__, e);
2166-
X509_STORE_CTX_free(store_ctx);
2167-
goto end;
2168-
}
2169-
2170-
/* Get chain from store context */
2171-
peer_chain = X509_STORE_CTX_get1_chain(store_ctx);
2172-
X509_STORE_CTX_free(store_ctx);
2122+
PyErr_SetString(
2123+
PyExc_Exception,
2124+
"Getting verified certificate chains with SSL_get0_verified_chain"
2125+
" is only supported by OpenSSL 1.1.0 and later");
2126+
return NULL;
21732127
#endif
21742128
} else {
21752129
peer_chain = SSL_get_peer_cert_chain(self->ssl);
@@ -2201,11 +2155,6 @@ _ssl__SSLSocket_getpeercertchain_impl(PySSLSocket *self, int binary_mode,
22012155
}
22022156

22032157
end:
2204-
#ifndef OPENSSL_VERSION_1_1
2205-
if (validate && (peer_chain != NULL)) {
2206-
sk_X509_pop_free(peer_chain, X509_free);
2207-
}
2208-
#endif
22092158
return retval;
22102159
}
22112160

0 commit comments

Comments
 (0)