Repository navigation
Expand file tree
/
Copy pathssl.rst
More file actions
3030 lines (2155 loc) · 114 KB
/
Copy pathssl.rst
File metadata and controls
3030 lines (2155 loc) · 114 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
:mod:`!ssl` --- TLS/SSL wrapper for socket objects
==================================================
.. module:: ssl
:synopsis: TLS/SSL wrapper for socket objects
**Source code:** :source:`Lib/ssl.py`
.. index:: single: OpenSSL; (use in module ssl)
.. index:: TLS, SSL, Transport Layer Security, Secure Sockets Layer
--------------
This module provides access to Transport Layer Security (often known as "Secure
Sockets Layer") encryption and peer authentication facilities for network
sockets, both client-side and server-side. This module uses the OpenSSL
library.
.. include:: ../includes/optional-module.rst
.. note::
Some behavior may be platform dependent, since calls are made to the
operating system socket APIs. The installed version of OpenSSL may also
cause variations in behavior. For example, TLSv1.3 comes with OpenSSL version
1.1.1.
.. warning::
Don't use this module without reading the :ref:`ssl-security`. Doing so
may lead to a false sense of security, as the default settings of the
ssl module are not necessarily appropriate for your application.
.. include:: ../includes/wasm-notavail.rst
This section documents the objects and functions in the ``ssl`` module; for more
general information about TLS, SSL, and certificates, the reader is referred to
the documents in the "See Also" section at the bottom.
This module provides a class, :class:`ssl.SSLSocket`, which is derived from the
:class:`socket.socket` type, and provides a socket-like wrapper that also
encrypts and decrypts the data going over the socket with SSL. It supports
additional methods such as :meth:`getpeercert`, which retrieves the
certificate of the other side of the connection, :meth:`cipher`, which
retrieves the cipher being used for the secure connection or
:meth:`get_verified_chain`, :meth:`get_unverified_chain` which retrieves
certificate chain.
For more sophisticated applications, the :class:`ssl.SSLContext` class
helps manage settings and certificates, which can then be inherited
by SSL sockets created through the :meth:`SSLContext.wrap_socket` method.
.. versionchanged:: 3.5.3
Updated to support linking with OpenSSL 1.1.0
.. versionchanged:: 3.6
OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported.
In the future the ssl module will require at least OpenSSL 1.0.2 or
1.1.0.
.. versionchanged:: 3.10
:pep:`644` has been implemented. The ssl module requires OpenSSL 1.1.1
or newer.
Use of deprecated constants and functions result in deprecation warnings.
Functions, constants, and exceptions
------------------------------------
Socket creation
^^^^^^^^^^^^^^^
Instances of :class:`SSLSocket` must be created using the
:meth:`SSLContext.wrap_socket` method. The helper function
:func:`create_default_context` returns a new context with secure default
settings.
Client socket example with default context and IPv4/IPv6 dual stack::
import socket
import ssl
hostname = 'www.python.org'
context = ssl.create_default_context()
with socket.create_connection((hostname, 443)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
print(ssock.version())
Client socket example with custom context and IPv4::
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations('path/to/cabundle.pem')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
ssock.connect((hostname, 443))
print(ssock.version())
Server socket example listening on localhost IPv4::
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('/path/to/certchain.pem', '/path/to/private.key')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
sock.bind(('127.0.0.1', 8443))
sock.listen(5)
with context.wrap_socket(sock, server_side=True) as ssock:
conn, addr = ssock.accept()
...
Context creation
^^^^^^^^^^^^^^^^
A convenience function helps create :class:`SSLContext` objects for common
purposes.
.. function:: create_default_context(purpose=Purpose.SERVER_AUTH, *,\
cafile=None, capath=None, cadata=None)
Return a new :class:`SSLContext` object with default settings for
the given *purpose*. The settings are chosen by the :mod:`!ssl` module,
and usually represent a higher security level than when calling the
:class:`SSLContext` constructor directly.
*cafile*, *capath*, *cadata* represent optional CA certificates to
trust for certificate verification, as in
:meth:`SSLContext.load_verify_locations`. If all three are
:const:`None`, this function can choose to trust the system's default
CA certificates instead.
The settings are: :data:`PROTOCOL_TLS_CLIENT` or
:data:`PROTOCOL_TLS_SERVER`, :data:`OP_NO_SSLv2`, and :data:`OP_NO_SSLv3`
with high encryption cipher suites without RC4 and
without unauthenticated cipher suites. Passing :const:`~Purpose.SERVER_AUTH`
as *purpose* sets :data:`~SSLContext.verify_mode` to :data:`CERT_REQUIRED`
and either loads CA certificates (when at least one of *cafile*, *capath* or
*cadata* is given) or uses :meth:`SSLContext.load_default_certs` to load
default CA certificates.
When :attr:`~SSLContext.keylog_filename` is supported and the environment
variable :envvar:`SSLKEYLOGFILE` is set, :func:`create_default_context`
enables key logging.
The default settings for this context include
:data:`VERIFY_X509_PARTIAL_CHAIN` and :data:`VERIFY_X509_STRICT`.
These make the underlying OpenSSL implementation behave more like
a conforming implementation of :rfc:`5280`, in exchange for a small
amount of incompatibility with older X.509 certificates.
.. note::
The protocol, options, cipher and other settings may change to more
restrictive values anytime without prior deprecation. The values
represent a fair balance between compatibility and security.
If your application needs specific settings, you should create a
:class:`SSLContext` and apply the settings yourself.
.. note::
If you find that when certain older clients or servers attempt to connect
with a :class:`SSLContext` created by this function that they get an error
stating "Protocol or cipher suite mismatch", it may be that they only
support SSL3.0 which this function excludes using the
:data:`OP_NO_SSLv3`. SSL3.0 is widely considered to be `completely broken
<https://en.wikipedia.org/wiki/POODLE>`_. If you still wish to continue to
use this function but still allow SSL 3.0 connections you can re-enable
them using::
ctx = ssl.create_default_context(Purpose.CLIENT_AUTH)
ctx.options &= ~ssl.OP_NO_SSLv3
.. note::
This context enables :data:`VERIFY_X509_STRICT` by default, which
may reject pre-:rfc:`5280` or malformed certificates that the
underlying OpenSSL implementation otherwise would accept. While disabling
this is not recommended, you can do so using::
ctx = ssl.create_default_context()
ctx.verify_flags &= ~ssl.VERIFY_X509_STRICT
.. versionadded:: 3.4
.. versionchanged:: 3.4.4
RC4 was dropped from the default cipher string.
.. versionchanged:: 3.6
ChaCha20/Poly1305 was added to the default cipher string.
3DES was dropped from the default cipher string.
.. versionchanged:: 3.8
Support for key logging to :envvar:`SSLKEYLOGFILE` was added.
.. versionchanged:: 3.10
The context now uses :data:`PROTOCOL_TLS_CLIENT` or
:data:`PROTOCOL_TLS_SERVER` protocol instead of generic
:data:`PROTOCOL_TLS`.
.. versionchanged:: 3.13
The context now uses :data:`VERIFY_X509_PARTIAL_CHAIN` and
:data:`VERIFY_X509_STRICT` in its default verify flags.
Signature algorithms
^^^^^^^^^^^^^^^^^^^^
.. function:: get_sigalgs()
Return a list of available TLS signature algorithm names used
by servers to complete the TLS handshake or clients requesting
certificate-based authentication. For example::
>>> ssl.get_sigalgs() # doctest: +SKIP
['ecdsa_secp256r1_sha256', 'ecdsa_secp384r1_sha384', ...]
These names can be used when building string values to pass to the
:meth:`SSLContext.set_client_sigalgs` and
:meth:`SSLContext.set_server_sigalgs` methods.
.. versionadded:: 3.15
Exceptions
^^^^^^^^^^
.. exception:: SSLError
Raised to signal an error from the underlying SSL implementation
(currently provided by the OpenSSL library). This signifies some
problem in the higher-level encryption and authentication layer that's
superimposed on the underlying network connection. This error
is a subtype of :exc:`OSError`. The error code and message of
:exc:`SSLError` instances are provided by the OpenSSL library.
.. versionchanged:: 3.3
:exc:`SSLError` used to be a subtype of :exc:`socket.error`.
.. attribute:: library
A string mnemonic designating the OpenSSL submodule in which the error
occurred, such as ``SSL``, ``PEM`` or ``X509``. The range of possible
values depends on the OpenSSL version.
.. versionadded:: 3.3
.. attribute:: reason
A string mnemonic designating the reason this error occurred, for
example ``CERTIFICATE_VERIFY_FAILED``. The range of possible
values depends on the OpenSSL version.
.. versionadded:: 3.3
.. exception:: SSLZeroReturnError
A subclass of :exc:`SSLError` raised when trying to read or write and
the SSL connection has been closed cleanly. Note that this doesn't
mean that the underlying transport (read TCP) has been closed.
.. versionadded:: 3.3
.. exception:: SSLWantReadError
A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket
<ssl-nonblocking>` when trying to read or write data, but more data needs
to be received on the underlying TCP transport before the request can be
fulfilled.
.. versionadded:: 3.3
.. exception:: SSLWantWriteError
A subclass of :exc:`SSLError` raised by a :ref:`non-blocking SSL socket
<ssl-nonblocking>` when trying to read or write data, but more data needs
to be sent on the underlying TCP transport before the request can be
fulfilled.
.. versionadded:: 3.3
.. exception:: SSLSyscallError
A subclass of :exc:`SSLError` raised when a system error was encountered
while trying to fulfill an operation on a SSL socket. Unfortunately,
there is no easy way to inspect the original errno number.
.. versionadded:: 3.3
.. exception:: SSLEOFError
A subclass of :exc:`SSLError` raised when the SSL connection has been
terminated abruptly. Generally, you shouldn't try to reuse the underlying
transport when this error is encountered.
.. versionadded:: 3.3
.. exception:: SSLCertVerificationError
A subclass of :exc:`SSLError` raised when certificate validation has
failed.
.. versionadded:: 3.7
.. attribute:: verify_code
A numeric error number that denotes the verification error.
.. attribute:: verify_message
A human readable string of the verification error.
.. exception:: CertificateError
An alias for :exc:`SSLCertVerificationError`.
.. versionchanged:: 3.7
The exception is now an alias for :exc:`SSLCertVerificationError`.
Random generation
^^^^^^^^^^^^^^^^^
.. function:: RAND_bytes(num, /)
Return *num* cryptographically strong pseudo-random bytes. Raises an
:class:`SSLError` if the PRNG has not been seeded with enough data or if the
operation is not supported by the current RAND method. :func:`RAND_status`
can be used to check the status of the PRNG and :func:`RAND_add` can be used
to seed the PRNG.
For almost all applications :func:`os.urandom` is preferable.
Read the Wikipedia article, `Cryptographically secure pseudorandom number
generator (CSPRNG)
<https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator>`_,
to get the requirements of a cryptographically strong generator.
.. versionadded:: 3.3
.. function:: RAND_status()
Return ``True`` if the SSL pseudo-random number generator has been seeded
with 'enough' randomness, and ``False`` otherwise. Use :func:`ssl.RAND_add`
to increase the randomness of the pseudo-random number generator.
.. function:: RAND_add(bytes, entropy, /)
Mix the given *bytes* into the SSL pseudo-random number generator. The
parameter *entropy* (a float) is a lower bound on the entropy contained in
string (so you can always use ``0.0``). See :rfc:`1750` for more
information on sources of entropy.
.. versionchanged:: 3.5
Writable :term:`bytes-like object` is now accepted.
Certificate handling
^^^^^^^^^^^^^^^^^^^^
.. testsetup::
import ssl
.. function:: cert_time_to_seconds(cert_time)
Return the time in seconds since the epoch, given the ``cert_time``
string representing the "notBefore" or "notAfter" date from a
certificate in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C
locale).
Here's an example:
.. doctest:: newcontext
>>> import ssl
>>> import datetime as dt
>>> timestamp = ssl.cert_time_to_seconds("Jan 5 09:34:43 2018 GMT")
>>> timestamp # doctest: +SKIP
1515144883
>>> print(dt.datetime.fromtimestamp(timestamp, dt.UTC)) # doctest: +SKIP
2018-01-05 09:34:43+00:00
"notBefore" or "notAfter" dates must use GMT (:rfc:`5280`).
.. versionchanged:: 3.5
Interpret the input time as a time in UTC as specified by 'GMT'
timezone in the input string. Local timezone was used
previously. Return an integer (no fractions of a second in the
input format)
.. function:: get_server_certificate(addr, ssl_version=PROTOCOL_TLS_CLIENT, \
ca_certs=None[, timeout])
Given the address ``addr`` of an SSL-protected server, as a (*hostname*,
*port-number*) pair, fetches the server's certificate, and returns it as a
PEM-encoded string. If ``ssl_version`` is specified, uses that version of
the SSL protocol to attempt to connect to the server. If *ca_certs* is
specified, it should be a file containing a list of root certificates, the
same format as used for the *cafile* parameter in
:meth:`SSLContext.load_verify_locations`. The call will attempt to validate the
server certificate against that set of root certificates, and will fail
if the validation attempt fails. A timeout can be specified with the
``timeout`` parameter.
.. versionchanged:: 3.3
This function is now IPv6-compatible.
.. versionchanged:: 3.5
The default *ssl_version* is changed from :data:`PROTOCOL_SSLv3` to
:data:`PROTOCOL_TLS` for maximum compatibility with modern servers.
.. versionchanged:: 3.10
The *timeout* parameter was added.
.. function:: DER_cert_to_PEM_cert(der_cert_bytes)
Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded
string version of the same certificate.
.. function:: PEM_cert_to_DER_cert(pem_cert_string)
Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of
bytes for that same certificate.
.. function:: get_default_verify_paths()
Returns a named tuple with paths to OpenSSL's default cafile and capath.
The paths are the same as used by
:meth:`SSLContext.set_default_verify_paths`. The return value is a
:term:`named tuple` ``DefaultVerifyPaths``:
* :attr:`cafile` - resolved path to cafile or ``None`` if the file doesn't exist,
* :attr:`capath` - resolved path to capath or ``None`` if the directory doesn't exist,
* :attr:`openssl_cafile_env` - OpenSSL's environment key that points to a cafile,
* :attr:`openssl_cafile` - hard coded path to a cafile,
* :attr:`openssl_capath_env` - OpenSSL's environment key that points to a capath,
* :attr:`openssl_capath` - hard coded path to a capath directory
.. versionadded:: 3.4
.. function:: enum_certificates(store_name)
Retrieve certificates from Windows' system cert store. *store_name* may be
one of ``CA``, ``ROOT`` or ``MY``. Windows may provide additional cert
stores, too.
The function returns a list of (cert_bytes, encoding_type, trust) tuples.
The encoding_type specifies the encoding of cert_bytes. It is either
``'x509_asn'`` for X.509 ASN.1 data or ``'pkcs_7_asn'`` for
PKCS#7 ASN.1 data. Trust specifies the purpose of the certificate as a set
of OIDS or exactly ``True`` if the certificate is trustworthy for all
purposes.
Example::
>>> ssl.enum_certificates("CA")
[(b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}),
(b'data...', 'x509_asn', True)]
.. availability:: Windows.
.. versionadded:: 3.4
.. function:: enum_crls(store_name)
Retrieve CRLs from Windows' system cert store. *store_name* may be
one of ``CA``, ``ROOT`` or ``MY``. Windows may provide additional cert
stores, too.
The function returns a list of (cert_bytes, encoding_type) tuples.
The encoding_type specifies the encoding of cert_bytes. It is either
``'x509_asn'`` for X.509 ASN.1 data or ``'pkcs_7_asn'`` for
PKCS#7 ASN.1 data.
.. availability:: Windows.
.. versionadded:: 3.4
Constants
^^^^^^^^^
All constants are now :class:`enum.IntEnum` or :class:`enum.IntFlag` collections.
.. versionadded:: 3.6
.. data:: CERT_NONE
Possible value for :attr:`SSLContext.verify_mode`.
Except for :const:`PROTOCOL_TLS_CLIENT`,
it is the default mode. With client-side sockets, just about any
cert is accepted. Validation errors, such as untrusted or expired cert,
are ignored and do not abort the TLS/SSL handshake.
In server mode, no certificate is requested from the client, so the client
does not send any for client cert authentication.
See the discussion of :ref:`ssl-security` below.
.. data:: CERT_OPTIONAL
Possible value for :attr:`SSLContext.verify_mode`.
In client mode, :const:`CERT_OPTIONAL`
has the same meaning as :const:`CERT_REQUIRED`. It is recommended to
use :const:`CERT_REQUIRED` for client-side sockets instead.
In server mode, a client certificate request is sent to the client. The
client may either ignore the request or send a certificate in order
perform TLS client cert authentication. If the client chooses to send
a certificate, it is verified. Any verification error immediately aborts
the TLS handshake.
Use of this setting requires a valid set of CA certificates to
be passed to :meth:`SSLContext.load_verify_locations`.
.. data:: CERT_REQUIRED
Possible value for :attr:`SSLContext.verify_mode`.
In this mode, certificates are
required from the other side of the socket connection; an :class:`SSLError`
will be raised if no certificate is provided, or if its validation fails.
This mode is **not** sufficient to verify a certificate in client mode as
it does not match hostnames. :attr:`~SSLContext.check_hostname` must be
enabled as well to verify the authenticity of a cert.
:const:`PROTOCOL_TLS_CLIENT` uses :const:`CERT_REQUIRED` and
enables :attr:`~SSLContext.check_hostname` by default.
With server socket, this mode provides mandatory TLS client cert
authentication. A client certificate request is sent to the client and
the client must provide a valid and trusted certificate.
Use of this setting requires a valid set of CA certificates to
be passed to :meth:`SSLContext.load_verify_locations`.
.. class:: VerifyMode
:class:`enum.IntEnum` collection of CERT_* constants.
.. versionadded:: 3.6
.. data:: VERIFY_DEFAULT
Possible value for :attr:`SSLContext.verify_flags`. In this mode, certificate
revocation lists (CRLs) are not checked. By default OpenSSL does neither
require nor verify CRLs.
.. versionadded:: 3.4
.. data:: VERIFY_CRL_CHECK_LEAF
Possible value for :attr:`SSLContext.verify_flags`. In this mode, only the
peer cert is checked but none of the intermediate CA certificates. The mode
requires a valid CRL that is signed by the peer cert's issuer (its direct
ancestor CA). If no proper CRL has been loaded with
:attr:`SSLContext.load_verify_locations`, validation will fail.
.. versionadded:: 3.4
.. data:: VERIFY_CRL_CHECK_CHAIN
Possible value for :attr:`SSLContext.verify_flags`. In this mode, CRLs of
all certificates in the peer cert chain are checked.
.. versionadded:: 3.4
.. data:: VERIFY_X509_STRICT
Possible value for :attr:`SSLContext.verify_flags` to disable workarounds
for broken X.509 certificates.
.. versionadded:: 3.4
.. data:: VERIFY_ALLOW_PROXY_CERTS
Possible value for :attr:`SSLContext.verify_flags` to enables proxy
certificate verification.
.. versionadded:: 3.10
.. data:: VERIFY_X509_TRUSTED_FIRST
Possible value for :attr:`SSLContext.verify_flags`. It instructs OpenSSL to
prefer trusted certificates when building the trust chain to validate a
certificate. This flag is enabled by default.
.. versionadded:: 3.4.4
.. data:: VERIFY_X509_PARTIAL_CHAIN
Possible value for :attr:`SSLContext.verify_flags`. It instructs OpenSSL to
accept intermediate CAs in the trust store to be treated as trust-anchors,
in the same way as the self-signed root CA certificates. This makes it
possible to trust certificates issued by an intermediate CA without having
to trust its ancestor root CA.
.. versionadded:: 3.10
.. class:: VerifyFlags
:class:`enum.IntFlag` collection of VERIFY_* constants.
.. versionadded:: 3.6
.. data:: PROTOCOL_TLS
Selects the highest protocol version that both the client and server support.
Despite the name, this option can select both "SSL" and "TLS" protocols.
.. versionadded:: 3.6
.. deprecated:: 3.10
TLS clients and servers require different default settings for secure
communication. The generic TLS protocol constant is deprecated in
favor of :data:`PROTOCOL_TLS_CLIENT` and :data:`PROTOCOL_TLS_SERVER`.
.. data:: PROTOCOL_TLS_CLIENT
Auto-negotiate the highest protocol version that both the client and
server support, and configure the context client-side connections. The
protocol enables :data:`CERT_REQUIRED` and
:attr:`~SSLContext.check_hostname` by default.
.. versionadded:: 3.6
.. data:: PROTOCOL_TLS_SERVER
Auto-negotiate the highest protocol version that both the client and
server support, and configure the context server-side connections.
.. versionadded:: 3.6
.. data:: PROTOCOL_SSLv23
Alias for :data:`PROTOCOL_TLS`.
.. deprecated:: 3.6
Use :data:`PROTOCOL_TLS` instead.
.. data:: PROTOCOL_SSLv3
Selects SSL version 3 as the channel encryption protocol.
This protocol is not available if OpenSSL is compiled with the
``no-ssl3`` option.
.. warning::
SSL version 3 is insecure. Its use is highly discouraged.
.. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols. Use the default
protocol :data:`PROTOCOL_TLS_SERVER` or :data:`PROTOCOL_TLS_CLIENT`
with :attr:`SSLContext.minimum_version` and
:attr:`SSLContext.maximum_version` instead.
.. data:: PROTOCOL_TLSv1
Selects TLS version 1.0 as the channel encryption protocol.
.. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols.
.. data:: PROTOCOL_TLSv1_1
Selects TLS version 1.1 as the channel encryption protocol.
Available only with openssl version 1.0.1+.
.. versionadded:: 3.4
.. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols.
.. data:: PROTOCOL_TLSv1_2
Selects TLS version 1.2 as the channel encryption protocol.
Available only with openssl version 1.0.1+.
.. versionadded:: 3.4
.. deprecated:: 3.6
OpenSSL has deprecated all version specific protocols.
.. data:: OP_ALL
Enables workarounds for various bugs present in other SSL implementations.
This option is set by default. It does not necessarily set the same
flags as OpenSSL's ``SSL_OP_ALL`` constant.
.. versionadded:: 3.2
.. data:: OP_NO_SSLv2
Prevents an SSLv2 connection. This option is only applicable in
conjunction with :const:`PROTOCOL_TLS`. It prevents the peers from
choosing SSLv2 as the protocol version.
.. versionadded:: 3.2
.. deprecated:: 3.6
SSLv2 is deprecated
.. data:: OP_NO_SSLv3
Prevents an SSLv3 connection. This option is only applicable in
conjunction with :const:`PROTOCOL_TLS`. It prevents the peers from
choosing SSLv3 as the protocol version.
.. versionadded:: 3.2
.. deprecated:: 3.6
SSLv3 is deprecated
.. data:: OP_NO_TLSv1
Prevents a TLSv1 connection. This option is only applicable in
conjunction with :const:`PROTOCOL_TLS`. It prevents the peers from
choosing TLSv1 as the protocol version.
.. versionadded:: 3.2
.. deprecated:: 3.7
The option is deprecated since OpenSSL 1.1.0, use the new
:attr:`SSLContext.minimum_version` and
:attr:`SSLContext.maximum_version` instead.
.. data:: OP_NO_TLSv1_1
Prevents a TLSv1.1 connection. This option is only applicable in conjunction
with :const:`PROTOCOL_TLS`. It prevents the peers from choosing TLSv1.1 as
the protocol version. Available only with openssl version 1.0.1+.
.. versionadded:: 3.4
.. deprecated:: 3.7
The option is deprecated since OpenSSL 1.1.0.
.. data:: OP_NO_TLSv1_2
Prevents a TLSv1.2 connection. This option is only applicable in conjunction
with :const:`PROTOCOL_TLS`. It prevents the peers from choosing TLSv1.2 as
the protocol version. Available only with openssl version 1.0.1+.
.. versionadded:: 3.4
.. deprecated:: 3.7
The option is deprecated since OpenSSL 1.1.0.
.. data:: OP_NO_TLSv1_3
Prevents a TLSv1.3 connection. This option is only applicable in conjunction
with :const:`PROTOCOL_TLS`. It prevents the peers from choosing TLSv1.3 as
the protocol version. TLS 1.3 is available with OpenSSL 1.1.1 or later.
When Python has been compiled against an older version of OpenSSL, the
flag defaults to *0*.
.. versionadded:: 3.6.3
.. deprecated:: 3.7
The option is deprecated since OpenSSL 1.1.0. It was added to 2.7.15 and
3.6.3 for backwards compatibility with OpenSSL 1.0.2.
.. data:: OP_NO_RENEGOTIATION
Disable all renegotiation in TLSv1.2 and earlier. Do not send
HelloRequest messages, and ignore renegotiation requests via ClientHello.
This option is only available with OpenSSL 1.1.0h and later.
.. versionadded:: 3.7
.. data:: OP_CIPHER_SERVER_PREFERENCE
Use the server's cipher ordering preference, rather than the client's.
This option has no effect on client sockets and SSLv2 server sockets.
.. versionadded:: 3.3
.. data:: OP_SINGLE_DH_USE
Prevents reuse of the same DH key for distinct SSL sessions. This
improves forward secrecy but requires more computational resources.
This option only applies to server sockets.
.. versionadded:: 3.3
.. data:: OP_SINGLE_ECDH_USE
Prevents reuse of the same ECDH key for distinct SSL sessions. This
improves forward secrecy but requires more computational resources.
This option only applies to server sockets.
.. versionadded:: 3.3
.. data:: OP_ENABLE_MIDDLEBOX_COMPAT
Send dummy Change Cipher Spec (CCS) messages in TLS 1.3 handshake to make
a TLS 1.3 connection look more like a TLS 1.2 connection.
This option is only available with OpenSSL 1.1.1 and later.
.. versionadded:: 3.8
.. data:: OP_NO_COMPRESSION
Disable compression on the SSL channel. This is useful if the application
protocol supports its own compression scheme.
.. versionadded:: 3.3
.. class:: Options
:class:`enum.IntFlag` collection of OP_* constants.
.. data:: OP_NO_TICKET
Prevent client side from requesting a session ticket.
.. versionadded:: 3.6
.. data:: OP_IGNORE_UNEXPECTED_EOF
Ignore unexpected shutdown of TLS connections.
This option is only available with OpenSSL 3.0.0 and later.
.. versionadded:: 3.10
.. data:: OP_ENABLE_KTLS
Enable the use of the kernel TLS. To benefit from the feature, OpenSSL must
have been compiled with support for it, and the negotiated cipher suites and
extensions must be supported by it (a list of supported ones may vary by
platform and kernel version).
Note that with enabled kernel TLS some cryptographic operations are
performed by the kernel directly and not via any available OpenSSL
Providers. This might be undesirable if, for example, the application
requires all cryptographic operations to be performed by the FIPS provider.
This option is only available with OpenSSL 3.0.0 and later.
.. versionadded:: 3.12
.. data:: OP_LEGACY_SERVER_CONNECT
Allow legacy insecure renegotiation between OpenSSL and unpatched servers
only.
.. versionadded:: 3.12
.. data:: HAS_ALPN
Whether the OpenSSL library has built-in support for the *Application-Layer
Protocol Negotiation* TLS extension as described in :rfc:`7301`.
.. versionadded:: 3.5
.. data:: HAS_NEVER_CHECK_COMMON_NAME
Whether the OpenSSL library has built-in support not checking subject
common name and :attr:`SSLContext.hostname_checks_common_name` is
writeable.
.. versionadded:: 3.7
.. data:: HAS_ECDH
Whether the OpenSSL library has built-in support for the Elliptic Curve-based
Diffie-Hellman key exchange. This should be true unless the feature was
explicitly disabled by the distributor.
.. versionadded:: 3.3
.. data:: HAS_SNI
Whether the OpenSSL library has built-in support for the *Server Name
Indication* extension (as defined in :rfc:`6066`).
.. versionadded:: 3.2
.. data:: HAS_NPN
Whether the OpenSSL library has built-in support for the *Next Protocol
Negotiation* as described in the `Application Layer Protocol
Negotiation <https://en.wikipedia.org/wiki/Application-Layer_Protocol_Negotiation>`_.
When true, you can use the :meth:`SSLContext.set_npn_protocols` method to advertise
which protocols you want to support.
.. versionadded:: 3.3
.. data:: HAS_SSLv2
Whether the OpenSSL library has built-in support for the SSL 2.0 protocol.
.. versionadded:: 3.7
.. data:: HAS_SSLv3
Whether the OpenSSL library has built-in support for the SSL 3.0 protocol.
.. versionadded:: 3.7
.. data:: HAS_TLSv1
Whether the OpenSSL library has built-in support for the TLS 1.0 protocol.
.. versionadded:: 3.7
.. data:: HAS_TLSv1_1
Whether the OpenSSL library has built-in support for the TLS 1.1 protocol.
.. versionadded:: 3.7
.. data:: HAS_TLSv1_2
Whether the OpenSSL library has built-in support for the TLS 1.2 protocol.
.. versionadded:: 3.7
.. data:: HAS_TLSv1_3
Whether the OpenSSL library has built-in support for the TLS 1.3 protocol.
.. versionadded:: 3.7
.. data:: HAS_PSK
Whether the OpenSSL library has built-in support for TLS-PSK.
.. versionadded:: 3.13
.. data:: HAS_PSK_TLS13
Whether the OpenSSL library has built-in support for External PSKs in TLS
1.3 as described in :rfc:`9258`.
.. versionadded:: 3.15
.. data:: HAS_PHA
Whether the OpenSSL library has built-in support for TLS-PHA.
.. versionadded:: 3.14
.. data:: CHANNEL_BINDING_TYPES
List of supported TLS channel binding types. Strings in this list
can be used as arguments to :meth:`SSLSocket.get_channel_binding`.
.. versionadded:: 3.3
.. data:: OPENSSL_VERSION
The version string of the OpenSSL library loaded by the interpreter::
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k 26 Jan 2017'
.. versionadded:: 3.2
.. data:: OPENSSL_VERSION_INFO
A tuple of five integers representing version information about the
OpenSSL library::
>>> ssl.OPENSSL_VERSION_INFO
(1, 0, 2, 11, 15)
.. versionadded:: 3.2
.. data:: OPENSSL_VERSION_NUMBER
The raw version number of the OpenSSL library, as a single integer::
>>> ssl.OPENSSL_VERSION_NUMBER
268443839
>>> hex(ssl.OPENSSL_VERSION_NUMBER)