@@ -1562,6 +1562,8 @@ def __init__(self, context, socket=None):
15621562 _lib .SSL_set_mode (self ._ssl , _lib .SSL_MODE_AUTO_RETRY )
15631563 self ._context = context
15641564 self ._app_data = None
1565+ self ._verify_helper = None
1566+ self ._verify_callback = None
15651567
15661568 # References to strings used for Next Protocol Negotiation. OpenSSL's
15671569 # header files suggest that these might get copied at some point, but
@@ -1609,6 +1611,8 @@ def __getattr__(self, name):
16091611 return getattr (self ._socket , name )
16101612
16111613 def _raise_ssl_error (self , ssl , result ):
1614+ if self ._verify_helper is not None :
1615+ self ._verify_helper .raise_if_problem ()
16121616 if self ._context ._verify_helper is not None :
16131617 self ._context ._verify_helper .raise_if_problem ()
16141618 if self ._context ._npn_advertise_helper is not None :
@@ -2497,6 +2501,70 @@ def request_ocsp(self):
24972501 )
24982502 _openssl_assert (rc == 1 )
24992503
2504+ def set_verify (self , mode , callback ):
2505+ """
2506+ Set the verification flags for this Connection object to *mode* and specify
2507+ that *callback* should be used for verification callbacks.
2508+
2509+ While a Connection will inherit the verification config from its Context,
2510+ it is also possible to change it once the Connection has been instantiated
2511+ already.
2512+
2513+ :param mode: The verify mode, this should be one of
2514+ :const:`VERIFY_NONE` and :const:`VERIFY_PEER`. If
2515+ :const:`VERIFY_PEER` is used, *mode* can be OR:ed with
2516+ :const:`VERIFY_FAIL_IF_NO_PEER_CERT` and
2517+ :const:`VERIFY_CLIENT_ONCE` to further control the behaviour.
2518+ :param callback: The Python callback to use. This should take five
2519+ arguments: A Connection object, an X509 object, and three integer
2520+ variables, which are in turn potential error number, error depth
2521+ and return code. *callback* should return True if verification
2522+ passes and False otherwise.
2523+ :return: None
2524+
2525+ See SSL_set_verify(3SSL) for further details.
2526+ """
2527+ if not isinstance (mode , integer_types ):
2528+ raise TypeError ("mode must be an integer" )
2529+
2530+ if not callable (callback ):
2531+ raise TypeError ("callback must be callable" )
2532+
2533+ self ._verify_helper = _VerifyHelper (callback )
2534+ self ._verify_callback = self ._verify_helper .callback
2535+ _lib .SSL_set_verify (self ._ssl , mode , self ._verify_callback )
2536+
2537+ def set_verify_depth (self , depth ):
2538+ """
2539+ Set the maximum depth for the certificate chain verification that shall
2540+ be allowed for this Connection object.
2541+
2542+ :param depth: An integer specifying the verify depth
2543+ :return: None
2544+ """
2545+ if not isinstance (depth , integer_types ):
2546+ raise TypeError ("depth must be an integer" )
2547+
2548+ _lib .SSL_set_verify_depth (self ._ssl , depth )
2549+
2550+ def get_verify_mode (self ):
2551+ """
2552+ Retrieve the Connection object's verify mode, as set by
2553+ :meth:`set_verify`.
2554+
2555+ :return: The verify mode
2556+ """
2557+ return _lib .SSL_get_verify_mode (self ._ssl )
2558+
2559+ def get_verify_depth (self ):
2560+ """
2561+ Retrieve the Connection object's verify depth, as set by
2562+ :meth:`set_verify_depth`.
2563+
2564+ :return: The verify depth
2565+ """
2566+ return _lib .SSL_get_verify_depth (self ._ssl )
2567+
25002568
25012569# This is similar to the initialization calls at the end of OpenSSL/crypto.py
25022570# but is exercised mostly by the Context initializer.
0 commit comments