@@ -1562,6 +1562,8 @@ def __init__(self, context, socket=None):
1562
1562
_lib .SSL_set_mode (self ._ssl , _lib .SSL_MODE_AUTO_RETRY )
1563
1563
self ._context = context
1564
1564
self ._app_data = None
1565
+ self ._verify_helper = None
1566
+ self ._verify_callback = None
1565
1567
1566
1568
# References to strings used for Next Protocol Negotiation. OpenSSL's
1567
1569
# header files suggest that these might get copied at some point, but
@@ -1609,6 +1611,8 @@ def __getattr__(self, name):
1609
1611
return getattr (self ._socket , name )
1610
1612
1611
1613
def _raise_ssl_error (self , ssl , result ):
1614
+ if self ._verify_helper is not None :
1615
+ self ._verify_helper .raise_if_problem ()
1612
1616
if self ._context ._verify_helper is not None :
1613
1617
self ._context ._verify_helper .raise_if_problem ()
1614
1618
if self ._context ._npn_advertise_helper is not None :
@@ -2497,6 +2501,70 @@ def request_ocsp(self):
2497
2501
)
2498
2502
_openssl_assert (rc == 1 )
2499
2503
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
+
2500
2568
2501
2569
# This is similar to the initialization calls at the end of OpenSSL/crypto.py
2502
2570
# but is exercised mostly by the Context initializer.
0 commit comments