Skip to content

Commit 22cf50b

Browse files
committed
bpo-31432: Clarify CERT_NONE/OPTIONAL/REQUIRED doc
The documentation for CERT_NONE, CERT_OPTIONAL, and CERT_REQUIRED were misleading and partly wrong. It fails to explain that OpenSSL behaves differently in client and server mode. Also OpenSSL does validate the cert chain everytime. With SSL_VERIFY_NONE a validation error is not fatal in client mode and does not request a client cert in server mode. Also discourage people from using CERT_OPTIONAL in client mode. Signed-off-by: Christian Heimes <[email protected]>
1 parent 55bfe69 commit 22cf50b

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

Doc/library/ssl.rst

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,20 +543,28 @@ Constants
543543
.. data:: CERT_NONE
544544

545545
Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
546-
parameter to :func:`wrap_socket`. In this mode (the default), no
547-
certificates will be required from the other side of the socket connection.
548-
If a certificate is received from the other end, no attempt to validate it
549-
is made.
546+
parameter to :func:`wrap_socket`. Except for :const:`PROTOCOL_TLS_CLIENT`,
547+
it is the default mode. With client-side sockets, just about any
548+
cert is accepted. Validation errors, such as untrusted or expired cert,
549+
are ignored and do not abort the TLS/SSL handshake.
550+
551+
In server mode, no certificate is requested from the client, so the client
552+
does not send any for client cert authentication.
550553

551554
See the discussion of :ref:`ssl-security` below.
552555

553556
.. data:: CERT_OPTIONAL
554557

555558
Possible value for :attr:`SSLContext.verify_mode`, or the ``cert_reqs``
556-
parameter to :func:`wrap_socket`. In this mode no certificates will be
557-
required from the other side of the socket connection; but if they
558-
are provided, validation will be attempted and an :class:`SSLError`
559-
will be raised on failure.
559+
parameter to :func:`wrap_socket`. In client mode, :const:`CERT_OPTIONAL`
560+
has the same meaning as :const:`CERT_REQUIRED`. It is recommended to
561+
use :const:`CERT_REQUIRED` for client-side sockets instead.
562+
563+
In server mode, a client certificate request is sent to the client. The
564+
client may either ignore the request or send a certificate in order
565+
perform TLS client cert authentication. If the client chooses to send
566+
a certificate, it is verified. Any verification error immediately aborts
567+
the TLS handshake.
560568

561569
Use of this setting requires a valid set of CA certificates to
562570
be passed, either to :meth:`SSLContext.load_verify_locations` or as a
@@ -568,6 +576,15 @@ Constants
568576
parameter to :func:`wrap_socket`. In this mode, certificates are
569577
required from the other side of the socket connection; an :class:`SSLError`
570578
will be raised if no certificate is provided, or if its validation fails.
579+
This mode is **not** sufficient to verify a certificate in client mode as
580+
it does not match hostnames. :attr:`~SSLContext.check_hostname` must be
581+
enabled as well to verify the authenticity of a cert.
582+
:const:`PROTOCOL_TLS_CLIENT` uses :const:`CERT_REQUIRED` and
583+
enables :attr:`~SSLContext.check_hostname` by default.
584+
585+
With server socket, this mode provides mandatory TLS client cert
586+
authentication. A client certificate request is sent to the client and
587+
the client must provide a valid and trusted certificate.
571588

572589
Use of this setting requires a valid set of CA certificates to
573590
be passed, either to :meth:`SSLContext.load_verify_locations` or as a

Lib/test/test_ssl.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4025,7 +4025,9 @@ def test_session_handling(self):
40254025
self.assertTrue(session)
40264026
with self.assertRaises(TypeError) as e:
40274027
s.session = object
4028-
self.assertEqual(str(e.exception), 'Value is not a SSLSession.')
4028+
self.assertEqual(
4029+
str(e.exception), 'Value is not an SSLSession.'
4030+
)
40294031

40304032
with client_context.wrap_socket(socket.socket(),
40314033
server_hostname=hostname) as s:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Clarify meaning of CERT_NONE, CERT_OPTIONAL, and CERT_REQUIRED flags for
2+
ssl.SSLContext.verify_mode.

Modules/_ssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,7 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
20662066
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
20672067
#endif
20682068
} else {
2069-
PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext");
2069+
PyErr_SetString(PyExc_TypeError, "The value must be an SSLContext.");
20702070
return -1;
20712071
}
20722072

@@ -2725,7 +2725,7 @@ static int PySSL_set_session(PySSLSocket *self, PyObject *value,
27252725
int result;
27262726

27272727
if (!PySSLSession_Check(value)) {
2728-
PyErr_SetString(PyExc_TypeError, "Value is not a SSLSession.");
2728+
PyErr_SetString(PyExc_TypeError, "Value is not an SSLSession.");
27292729
return -1;
27302730
}
27312731
pysess = (PySSLSession *)value;

0 commit comments

Comments
 (0)