Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a7c641d

Browse files
committed
bpo-18233: Only support getpeercertchain(validate=True) with OpenSSL 1.1.0+
1 parent 4243cc6 commit a7c641d

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
@@ -2169,24 +2169,34 @@ def test_getpeercertchain(self):
21692169
try:
21702170
peer_cert = s.getpeercert()
21712171
peer_cert_bin = s.getpeercert(True)
2172-
chain = s.getpeercertchain()
2173-
chain_bin = s.getpeercertchain(True)
2172+
if IS_OPENSSL_1_1_0:
2173+
chain = s.getpeercertchain()
2174+
chain_bin = s.getpeercertchain(True)
2175+
else:
2176+
self.assertRaisesRegex(
2177+
Exception, r'only supported by OpenSSL 1\.1\.0',
2178+
s.getpeercertchain)
2179+
self.assertRaisesRegex(
2180+
Exception, r'only supported by OpenSSL 1\.1\.0',
2181+
s.getpeercertchain, True)
21742182
chain_no_validate = s.getpeercertchain(validate=False)
21752183
chain_bin_no_validate = s.getpeercertchain(True, False)
21762184
finally:
21772185
self.assertTrue(peer_cert)
2178-
self.assertEqual(len(chain), 2)
21792186
self.assertTrue(peer_cert_bin)
2180-
self.assertEqual(len(chain_bin), 2)
2187+
if IS_OPENSSL_1_1_0:
2188+
self.assertEqual(len(chain), 2)
2189+
self.assertEqual(len(chain_bin), 2)
21812190

21822191
# ca cert
21832192
ca_certs = ctx.get_ca_certs()
21842193
self.assertEqual(len(ca_certs), 1)
21852194
test_get_ca_certsert = ca_certs[0]
21862195
ca_cert_bin = ctx.get_ca_certs(True)[0]
21872196

2188-
self.assertEqual(chain, (peer_cert, test_get_ca_certsert))
2189-
self.assertEqual(chain_bin, (peer_cert_bin, ca_cert_bin))
2197+
if IS_OPENSSL_1_1_0:
2198+
self.assertEqual(chain, (peer_cert, test_get_ca_certsert))
2199+
self.assertEqual(chain_bin, (peer_cert_bin, ca_cert_bin))
21902200
self.assertEqual(chain_no_validate, (peer_cert,))
21912201
self.assertEqual(chain_bin_no_validate, (peer_cert_bin,))
21922202

Modules/_ssl.c

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,57 +2133,11 @@ _ssl__SSLSocket_getpeercertchain_impl(PySSLSocket *self, int binary_mode,
21332133
return NULL;
21342134
}
21352135
#else
2136-
X509 *peer_cert = SSL_get_peer_certificate(self->ssl);
2137-
if (peer_cert == NULL)
2138-
Py_RETURN_NONE;
2139-
2140-
STACK_OF(X509) *chain = SSL_get_peer_cert_chain(self->ssl);
2141-
if (chain == NULL) {
2142-
X509_free(peer_cert);
2143-
Py_RETURN_NONE;
2144-
}
2145-
X509_STORE_CTX *store_ctx;
2146-
2147-
/* Initialize a store context with store (for root CA certs), the
2148-
* peer's cert and the peer's chain with intermediate CA certs. */
2149-
if ((store_ctx = X509_STORE_CTX_new()) == NULL) {
2150-
X509_free(peer_cert);
2151-
_setSSLError(NULL, 0, __FILE__, __LINE__);
2152-
return NULL;
2153-
}
2154-
2155-
if (!X509_STORE_CTX_init(store_ctx,
2156-
SSL_CTX_get_cert_store(self->ctx->ctx),
2157-
peer_cert, chain)) {
2158-
#ifdef SSL_R_CERTIFICATE_VERIFY_FAILED
2159-
long e = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_CERTIFICATE_VERIFY_FAILED);
2160-
#else
2161-
long e = ERR_PACK(ERR_LIB_SSL, 0, 134);
2162-
#endif
2163-
fill_and_set_sslerror(self, PySSLCertVerificationErrorObject, PY_SSL_ERROR_SSL, NULL, __LINE__, e);
2164-
X509_free(peer_cert);
2165-
X509_STORE_CTX_free(store_ctx);
2166-
goto end;
2167-
}
2168-
X509_free(peer_cert);
2169-
2170-
/* Validate peer cert using its intermediate CA certs and the
2171-
* context's root CA certs. */
2172-
if (X509_verify_cert(store_ctx) <= 0) {
2173-
// _setX509StoreContextError(self, store_ctx, __FILE__, __LINE__);
2174-
#ifdef SSL_R_CERTIFICATE_VERIFY_FAILED
2175-
long e = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_CERTIFICATE_VERIFY_FAILED);
2176-
#else
2177-
long e = ERR_PACK(ERR_LIB_SSL, 0, 134);
2178-
#endif
2179-
fill_and_set_sslerror(self, PySSLCertVerificationErrorObject, PY_SSL_ERROR_SSL, NULL, __LINE__, e);
2180-
X509_STORE_CTX_free(store_ctx);
2181-
goto end;
2182-
}
2183-
2184-
/* Get chain from store context */
2185-
peer_chain = X509_STORE_CTX_get1_chain(store_ctx);
2186-
X509_STORE_CTX_free(store_ctx);
2136+
PyErr_SetString(
2137+
PyExc_Exception,
2138+
"Getting verified certificate chains with SSL_get0_verified_chain"
2139+
" is only supported by OpenSSL 1.1.0 and later");
2140+
return NULL;
21872141
#endif
21882142
} else {
21892143
peer_chain = SSL_get_peer_cert_chain(self->ssl);
@@ -2220,11 +2174,6 @@ _ssl__SSLSocket_getpeercertchain_impl(PySSLSocket *self, int binary_mode,
22202174
}
22212175

22222176
end:
2223-
#ifndef OPENSSL_VERSION_1_1
2224-
if (validate && (peer_chain != NULL)) {
2225-
sk_X509_pop_free(peer_chain, X509_free);
2226-
}
2227-
#endif
22282177
return retval;
22292178
}
22302179

0 commit comments

Comments
 (0)