FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Modules/_ssl.c at main · python/cpython · GitHub
cpython/Modules/_ssl.c at main · python/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
37.4k
Star
77.5k
Code
Issues
5k+
Pull requests
2.7k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Modules
/
_ssl.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
7519 lines (6630 loc) · 218 KB
Breadcrumbs
cpython
/
Modules
/
_ssl.c
Copy path
File metadata and controls
7519 lines (6630 loc) · 218 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* SSL socket module
SSL support based on patches by Brian E Gallew and Laszlo Kovacs.
Re-worked a bit by Bill Janssen to add server-side support and
certificate decoding. Chris Stawarz contributed some non-blocking
patches.
This module is imported by ssl.py. It should *not* be used
directly.
XXX should partial writes be enabled, SSL_MODE_ENABLE_PARTIAL_WRITE?
XXX integrate several "shutdown modes" as suggested in
http://bugs.python.org/issue8108#msg102867 ?
*/
#ifndef
Py_BUILD_CORE_BUILTIN
# define
Py_BUILD_CORE_MODULE
1
#endif
/* Don't warn about deprecated functions, */
#ifndef
OPENSSL_API_COMPAT
// 0x10101000L == 1.1.1, 30000 == 3.0.0
#define
OPENSSL_API_COMPAT
0x10101000L
#endif
#define
OPENSSL_NO_DEPRECATED
1
#include
"Python.h"
#include
"pycore_critical_section.h"
// Py_BEGIN_CRITICAL_SECTION()
#include
"pycore_fileutils.h"
// _PyIsSelectable_fd()
#include
"pycore_long.h"
// _PyLong_UnsignedLongLong_Converter()
#include
"pycore_pyerrors.h"
// _PyErr_ChainExceptions1()
#include
"pycore_time.h"
// _PyDeadline_Init()
#include
"pycore_tuple.h"
// _PyTuple_FromPair
/* Include symbols from _socket module */
#include
"socketmodule.h"
#ifdef
MS_WINDOWS
# include
<wincrypt.h>
#endif
#include
"_ssl.h"
#include
"_openssl_mem.h"
/* Redefined below for Windows debug builds after important #includes */
#define
_PySSL_FIX_ERRNO
#define
PySSL_BEGIN_ALLOW_THREADS_S
(
save
) \
do { (save) = PyEval_SaveThread(); } while(0)
#define
PySSL_END_ALLOW_THREADS_S
(
save
) \
do { PyEval_RestoreThread(save); _PySSL_FIX_ERRNO; } while(0)
#define
PySSL_BEGIN_ALLOW_THREADS
(
self
) { \
PyThreadState *_save = NULL; \
PySSL_BEGIN_ALLOW_THREADS_S(_save); \
PyMutex_Lock(&(self)->tstate_mutex);
#define
PySSL_END_ALLOW_THREADS
(
self
) \
PyMutex_Unlock(&(self)->tstate_mutex); \
PySSL_END_ALLOW_THREADS_S(_save); }
#if
defined(
HAVE_POLL_H
)
#include
<poll.h>
#elif
defined(
HAVE_SYS_POLL_H
)
#include
<sys/poll.h>
#endif
/* Include OpenSSL header files */
#include
"openssl/rsa.h"
#include
"openssl/crypto.h"
#include
"openssl/x509.h"
#include
"openssl/x509v3.h"
#include
"openssl/pem.h"
#include
"openssl/ssl.h"
#include
"openssl/err.h"
#include
"openssl/rand.h"
#include
"openssl/bio.h"
#include
"openssl/dh.h"
#ifndef
OPENSSL_THREADS
# error
"OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL"
#endif
struct
py_ssl_error_code
{
const
char
*
mnemonic
;
int
library
,
reason
;
};
struct
py_ssl_library_code
{
const
char
*
library
;
int
code
;
};
#if
defined(
MS_WINDOWS
)
&&
defined(
Py_DEBUG
)
/* Debug builds on Windows rely on getting errno directly from OpenSSL.
* However, because it uses a different CRT, we need to transfer the
* value of errno from OpenSSL into our debug CRT.
*
* Don't be fooled - this is horribly ugly code. The only reasonable
* alternative is to do both debug and release builds of OpenSSL, which
* requires much uglier code to transform their automatically generated
* makefile. This is the lesser of all the evils.
*/
static
void
_PySSLFixErrno
(
void
) {
HMODULE
ucrtbase
=
GetModuleHandleW
(
L"ucrtbase.dll"
);
if
(!
ucrtbase
) {
/* If ucrtbase.dll is not loaded but the SSL DLLs are, we likely
* have a catastrophic failure, but this function is not the
* place to raise it. */
return
;
}
typedef
int
*
(__stdcall
*
errno_func
)(
void
);
errno_func
ssl_errno
=
(
errno_func
)
GetProcAddress
(
ucrtbase
,
"_errno"
);
if
(
ssl_errno
) {
errno
=
*
ssl_errno
();
*
ssl_errno
()
=
0
;
}
else
{
errno
=
ENOTRECOVERABLE
;
}
}
#undef
_PySSL_FIX_ERRNO
#define
_PySSL_FIX_ERRNO
_PySSLFixErrno()
#endif
/* Include generated data (error codes) */
/* See Tools/ssl/make_ssl_data.py for notes on adding a new version. */
#if
(
OPENSSL_VERSION_NUMBER
>=
0x40000000L
)
# include
"_ssl_data_40.h"
#elif
(
OPENSSL_VERSION_NUMBER
>=
0x30401000L
)
# include
"_ssl_data_36.h"
#elif
(
OPENSSL_VERSION_NUMBER
>=
0x30100000L
)
# include
"_ssl_data_340.h"
#elif
(
OPENSSL_VERSION_NUMBER
>=
0x30000000L
)
# include
"_ssl_data_300.h"
#elif
(
OPENSSL_VERSION_NUMBER
>=
0x10101000L
)
# include
"_ssl_data_111.h"
#else
# error
Unsupported OpenSSL version
#endif
#if
(
OPENSSL_VERSION_NUMBER
>=
0x40000000L
)
# define
OPENSSL_NO_SSL3
# define
OPENSSL_NO_TLS1
# define
OPENSSL_NO_TLS1_1
# define
OPENSSL_NO_TLS1_2
# define
OPENSSL_NO_SSL3_METHOD
# define
OPENSSL_NO_TLS1_METHOD
# define
OPENSSL_NO_TLS1_1_METHOD
# define
OPENSSL_NO_TLS1_2_METHOD
#endif
/* OpenSSL API 1.1.0+ does not include version methods */
#ifndef
OPENSSL_NO_SSL3_METHOD
extern
const
SSL_METHOD
*
SSLv3_method
(
void
);
#endif
#ifndef
OPENSSL_NO_TLS1_METHOD
extern
const
SSL_METHOD
*
TLSv1_method
(
void
);
#endif
#ifndef
OPENSSL_NO_TLS1_1_METHOD
extern
const
SSL_METHOD
*
TLSv1_1_method
(
void
);
#endif
#ifndef
OPENSSL_NO_TLS1_2_METHOD
extern
const
SSL_METHOD
*
TLSv1_2_method
(
void
);
#endif
#ifndef
INVALID_SOCKET
/* MS defines this */
#define
INVALID_SOCKET
(-1)
#endif
/* Default cipher suites */
#ifndef
PY_SSL_DEFAULT_CIPHERS
#define
PY_SSL_DEFAULT_CIPHERS
1
#endif
#if
PY_SSL_DEFAULT_CIPHERS
==
0
#ifndef
PY_SSL_DEFAULT_CIPHER_STRING
#error
"Py_SSL_DEFAULT_CIPHERS 0 needs Py_SSL_DEFAULT_CIPHER_STRING"
#endif
#ifndef
PY_SSL_MIN_PROTOCOL
#define
PY_SSL_MIN_PROTOCOL
TLS1_2_VERSION
#endif
#elif
PY_SSL_DEFAULT_CIPHERS
==
1
/* Python custom selection of sensible cipher suites
* @SECLEVEL=2: security level 2 with 112 bits minimum security (e.g. 2048 bits RSA key)
* ECDH+*: enable ephemeral elliptic curve Diffie-Hellman
* DHE+*: fallback to ephemeral finite field Diffie-Hellman
* encryption order: AES AEAD (GCM), ChaCha AEAD, AES CBC
* !aNULL:!eNULL: really no NULL ciphers
* !aDSS: no authentication with discrete logarithm DSA algorithm
* !SHA1: no weak SHA1 MAC
* !AESCCM: no CCM mode, it's uncommon and slow
*
* Based on Hynek's excellent blog post (update 2021-02-11)
* https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
*/
#define
PY_SSL_DEFAULT_CIPHER_STRING
"@SECLEVEL=2:ECDH+AESGCM:ECDH+CHACHA20:ECDH+AES:DHE+AES:!aNULL:!eNULL:!aDSS:!SHA1:!AESCCM"
#ifndef
PY_SSL_MIN_PROTOCOL
#define
PY_SSL_MIN_PROTOCOL
TLS1_2_VERSION
#endif
#elif
PY_SSL_DEFAULT_CIPHERS
==
2
/* Ignored in SSLContext constructor, only used to as _ssl.DEFAULT_CIPHER_STRING */
#define
PY_SSL_DEFAULT_CIPHER_STRING
SSL_DEFAULT_CIPHER_LIST
#else
#error
"Unsupported PY_SSL_DEFAULT_CIPHERS"
#endif
#if
defined(
SSL_VERIFY_POST_HANDSHAKE
)
&&
defined(
TLS1_3_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1_3
)
#define
PySSL_HAVE_POST_HS_AUTH
#endif
enum
py_ssl_error
{
/* these mirror ssl.h */
PY_SSL_ERROR_NONE
,
PY_SSL_ERROR_SSL
,
PY_SSL_ERROR_WANT_READ
,
PY_SSL_ERROR_WANT_WRITE
,
PY_SSL_ERROR_WANT_X509_LOOKUP
,
PY_SSL_ERROR_SYSCALL
,
/* look at error stack/return value/errno */
PY_SSL_ERROR_ZERO_RETURN
,
PY_SSL_ERROR_WANT_CONNECT
,
/* start of non ssl.h errorcodes */
PY_SSL_ERROR_EOF
,
/* special case of SSL_ERROR_SYSCALL */
PY_SSL_ERROR_NO_SOCKET
,
/* socket has been GC'd */
PY_SSL_ERROR_INVALID_ERROR_CODE
};
enum
py_ssl_server_or_client
{
PY_SSL_CLIENT
,
PY_SSL_SERVER
};
enum
py_ssl_cert_requirements
{
PY_SSL_CERT_NONE
,
PY_SSL_CERT_OPTIONAL
,
PY_SSL_CERT_REQUIRED
};
enum
py_ssl_version
{
PY_SSL_VERSION_SSL2
,
PY_SSL_VERSION_SSL3
=
1
,
PY_SSL_VERSION_TLS
,
/* SSLv23 */
PY_SSL_VERSION_TLS1
,
PY_SSL_VERSION_TLS1_1
,
PY_SSL_VERSION_TLS1_2
,
PY_SSL_VERSION_TLS_CLIENT
=
0x10
,
PY_SSL_VERSION_TLS_SERVER
,
};
enum
py_proto_version
{
PY_PROTO_MINIMUM_SUPPORTED
=
-2
,
PY_PROTO_SSLv3
=
SSL3_VERSION
,
PY_PROTO_TLSv1
=
TLS1_VERSION
,
PY_PROTO_TLSv1_1
=
TLS1_1_VERSION
,
PY_PROTO_TLSv1_2
=
TLS1_2_VERSION
,
#if
defined(
TLS1_3_VERSION
)
PY_PROTO_TLSv1_3
=
TLS1_3_VERSION
,
#else
PY_PROTO_TLSv1_3
=
0x304
,
#endif
PY_PROTO_MAXIMUM_SUPPORTED
=
-1
,
/* OpenSSL has no dedicated API to set the minimum version to the maximum
* available version, and the other way around. We have to figure out the
* minimum and maximum available version on our own and hope for the best.
*/
#if
defined(
SSL3_VERSION
)
&&
!defined(
OPENSSL_NO_SSL3
)
PY_PROTO_MINIMUM_AVAILABLE
=
PY_PROTO_SSLv3
,
#elif
defined(
TLS1_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1
)
PY_PROTO_MINIMUM_AVAILABLE
=
PY_PROTO_TLSv1
,
#elif
defined(
TLS1_1_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1_1
)
PY_PROTO_MINIMUM_AVAILABLE
=
PY_PROTO_TLSv1_1
,
#elif
defined(
TLS1_2_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1_2
)
PY_PROTO_MINIMUM_AVAILABLE
=
PY_PROTO_TLSv1_2
,
#elif
defined(
TLS1_3_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1_3
)
PY_PROTO_MINIMUM_AVAILABLE
=
PY_PROTO_TLSv1_3
,
#else
#error
"PY_PROTO_MINIMUM_AVAILABLE not found"
#endif
#if
defined(
TLS1_3_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1_3
)
PY_PROTO_MAXIMUM_AVAILABLE
=
PY_PROTO_TLSv1_3
,
#elif
defined(
TLS1_2_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1_2
)
PY_PROTO_MAXIMUM_AVAILABLE
=
PY_PROTO_TLSv1_2
,
#elif
defined(
TLS1_1_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1_1
)
PY_PROTO_MAXIMUM_AVAILABLE
=
PY_PROTO_TLSv1_1
,
#elif
defined(
TLS1_VERSION
)
&&
!defined(
OPENSSL_NO_TLS1
)
PY_PROTO_MAXIMUM_AVAILABLE
=
PY_PROTO_TLSv1
,
#elif
defined(
SSL3_VERSION
)
&&
!defined(
OPENSSL_NO_SSL3
)
PY_PROTO_MAXIMUM_AVAILABLE
=
PY_PROTO_SSLv3
,
#else
#error
"PY_PROTO_MAXIMUM_AVAILABLE not found"
#endif
};
/* SSL socket object */
#define
X509_NAME_MAXLEN
256
/* In case of 'tls-unique' it will be 12 bytes for TLS, 36 bytes for
* older SSL, but let's be safe */
#define
PySSL_CB_MAXLEN
128
typedef
struct
{
PyObject_HEAD
SSL_CTX
*
ctx
;
unsigned
char
*
alpn_protocols
;
unsigned
int
alpn_protocols_len
;
PyObject
*
set_sni_cb
;
int
check_hostname
;
int
protocol
;
#if
defined(
PySSL_HAVE_POST_HS_AUTH
)
int
post_handshake_auth
;
#endif
PyObject
*
msg_cb
;
PyObject
*
keylog_filename
;
// can be anything accepted by Py_fopen()
BIO
*
keylog_bio
;
/* Cached module state, also used in SSLSocket and SSLSession code. */
_sslmodulestate
*
state
;
#ifndef
OPENSSL_NO_PSK
PyObject
*
psk_client_callback
;
PyObject
*
psk_server_callback
;
#endif
/* Lock to synchronize calls when the thread state is detached.
See also gh-134698. */
PyMutex
tstate_mutex
;
}
PySSLContext
;
#define
PySSLContext_CAST
(
op
) ((PySSLContext *)(op))
typedef
struct
{
int
ssl
;
/* last seen error from SSL */
int
c
;
/* last seen error from libc */
#ifdef
MS_WINDOWS
int
ws
;
/* last seen error from winsock */
#endif
}
_PySSLError
;
typedef
struct
{
PyObject_HEAD
PyObject
*
Socket
;
/* weakref to socket on which we're layered */
SSL
*
ssl
;
PySSLContext
*
ctx
;
/* weakref to SSL context */
char
shutdown_seen_zero
;
enum
py_ssl_server_or_client
socket_type
;
PyObject
*
owner
;
/* weakref to Python level "owner" passed to servername callback */
PyObject
*
server_hostname
;
// gh-148292: If non-zero, read(), sendfile(), write() and do_handshake()
// methods raise SSLEOFError without calling the underlying OpenSSL
// function. Set to 1 on PY_SSL_ERROR_EOF error.
//
// On OpenSSL 4, if SSL_read_ex() fails with
// SSL_R_UNEXPECTED_EOF_WHILE_READING, the following SSL_read_ex() call
// fails with a generic protocol error (ERR_peek_last_error() returns 0).
// Use got_eof_error to have the same behavior on OpenSSL 4 and newer and
// on OpenSSL 3 and older.
int
got_eof_error
;
}
PySSLSocket
;
#define
PySSLSocket_CAST
(
op
) ((PySSLSocket *)(op))
typedef
struct
{
PyObject_HEAD
BIO
*
bio
;
int
eof_written
;
}
PySSLMemoryBIO
;
#define
PySSLMemoryBIO_CAST
(
op
) ((PySSLMemoryBIO *)(op))
typedef
struct
{
PyObject_HEAD
SSL_SESSION
*
session
;
PySSLContext
*
ctx
;
}
PySSLSession
;
#define
PySSLSession_CAST
(
op
) ((PySSLSession *)(op))
static
inline
_PySSLError
_PySSL_errno
(
int
failed
,
const
SSL
*
ssl
,
int
retcode
)
{
_PySSLError
err
=
{
0
};
if
(
failed
) {
#ifdef
MS_WINDOWS
err
.
ws
=
WSAGetLastError
();
_PySSL_FIX_ERRNO
;
#endif
err
.
c
=
errno
;
err
.
ssl
=
SSL_get_error
(
ssl
,
retcode
);
}
return
err
;
}
/*[clinic input]
module _ssl
class _ssl._SSLContext "PySSLContext *" "get_state_type(type)->PySSLContext_Type"
class _ssl._SSLSocket "PySSLSocket *" "get_state_type(type)->PySSLSocket_Type"
class _ssl.MemoryBIO "PySSLMemoryBIO *" "get_state_type(type)->PySSLMemoryBIO_Type"
class _ssl.SSLSession "PySSLSession *" "get_state_type(type)->PySSLSession_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d293bed8bae240fd]*/
#include
"clinic/_ssl.c.h"
static
int
PySSL_select
(
PySocketSockObject
*
s
,
int
writing
,
PyTime_t
timeout
);
typedef
enum
{
SOCKET_IS_NONBLOCKING
,
SOCKET_IS_BLOCKING
,
SOCKET_HAS_TIMED_OUT
,
SOCKET_HAS_BEEN_CLOSED
,
SOCKET_TOO_LARGE_FOR_SELECT
,
SOCKET_OPERATION_OK
}
timeout_state
;
/* Wrap error strings with filename and line # */
#define
ERRSTR1
(
x
,
y
,
z
) (x ":" y ": " z)
#define
ERRSTR
(
x
) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x)
/* If sock is NULL, use a timeout of 0 second */
#define
GET_SOCKET_TIMEOUT
(
sock
) \
((sock != NULL) ? (sock)->sock_timeout : 0)
#include
"_ssl/debughelpers.c"
/*
* SSL errors.
*/
PyDoc_STRVAR
(
SSLError_doc
,
"An error occurred in the SSL implementation."
);
PyDoc_STRVAR
(
SSLCertVerificationError_doc
,
"A certificate could not be verified."
);
PyDoc_STRVAR
(
SSLZeroReturnError_doc
,
"SSL/TLS session closed cleanly."
);
PyDoc_STRVAR
(
SSLWantReadError_doc
,
"Non-blocking SSL socket needs to read more data\n"
"before the requested operation can be completed."
);
PyDoc_STRVAR
(
SSLWantWriteError_doc
,
"Non-blocking SSL socket needs to write more data\n"
"before the requested operation can be completed."
);
PyDoc_STRVAR
(
SSLSyscallError_doc
,
"System error when attempting SSL operation."
);
PyDoc_STRVAR
(
SSLEOFError_doc
,
"SSL/TLS connection terminated abruptly."
);
static
PyObject
*
SSLError_str
(
PyObject
*
op
)
{
PyOSErrorObject
*
self
=
(
PyOSErrorObject
*
)
op
;
if
(
self
->
strerror
!=
NULL
&&
PyUnicode_Check
(
self
->
strerror
)) {
return
Py_NewRef
(
self
->
strerror
);
}
else
return
PyObject_Str
(
self
->
args
);
}
static
PyType_Slot
sslerror_type_slots
[]
=
{
{
Py_tp_doc
, (
void
*
)
SSLError_doc
},
{
Py_tp_str
,
SSLError_str
},
{
0
,
0
},
};
static
PyType_Spec
sslerror_type_spec
=
{
.
name
=
"ssl.SSLError"
,
.
basicsize
=
sizeof
(
PyOSErrorObject
),
.
flags
=
(
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_BASETYPE
|
Py_TPFLAGS_IMMUTABLETYPE
),
.
slots
=
sslerror_type_slots
};
static
void
fill_and_set_sslerror
(
_sslmodulestate
*
state
,
PySSLSocket
*
sslsock
,
PyObject
*
type
,
int
ssl_errno
,
const
char
*
errstr
,
int
lineno
,
unsigned long
errcode
)
{
PyObject
*
err_value
=
NULL
,
*
reason_obj
=
NULL
,
*
lib_obj
=
NULL
;
PyObject
*
verify_obj
=
NULL
,
*
verify_code_obj
=
NULL
;
PyObject
*
init_value
,
*
msg
,
*
key
;
PyUnicodeWriter
*
writer
=
NULL
;
if
(
ssl_errno
==
PY_SSL_ERROR_EOF
&&
sslsock
!=
NULL
) {
sslsock
->
got_eof_error
=
1
;
}
if
(
errcode
!=
0
) {
int
lib
,
reason
;
lib
=
ERR_GET_LIB
(
errcode
);
reason
=
ERR_GET_REASON
(
errcode
);
key
=
Py_BuildValue
(
"ii"
,
lib
,
reason
);
if
(
key
==
NULL
)
goto
fail
;
reason_obj
=
PyDict_GetItemWithError
(
state
->
err_codes_to_names
,
key
);
Py_DECREF
(
key
);
if
(
reason_obj
==
NULL
&&
PyErr_Occurred
()) {
goto
fail
;
}
key
=
PyLong_FromLong
(
lib
);
if
(
key
==
NULL
)
goto
fail
;
lib_obj
=
PyDict_GetItemWithError
(
state
->
lib_codes_to_names
,
key
);
Py_DECREF
(
key
);
if
(
lib_obj
==
NULL
&&
PyErr_Occurred
()) {
goto
fail
;
}
if
(
errstr
==
NULL
) {
errstr
=
ERR_reason_error_string
(
errcode
);
}
}
/* verify code for cert validation error */
if
((
sslsock
!=
NULL
)
&&
(
type
==
state
->
PySSLCertVerificationErrorObject
)) {
const
char
*
verify_str
=
NULL
;
long
verify_code
;
verify_code
=
SSL_get_verify_result
(
sslsock
->
ssl
);
verify_code_obj
=
PyLong_FromLong
(
verify_code
);
if
(
verify_code_obj
==
NULL
) {
goto
fail
;
}
switch
(
verify_code
) {
case
X509_V_ERR_HOSTNAME_MISMATCH
:
verify_obj
=
PyUnicode_FromFormat
(
"Hostname mismatch, certificate is not valid for '%S'."
,
sslsock
->
server_hostname
);
break
;
case
X509_V_ERR_IP_ADDRESS_MISMATCH
:
verify_obj
=
PyUnicode_FromFormat
(
"IP address mismatch, certificate is not valid for '%S'."
,
sslsock
->
server_hostname
);
break
;
default
:
verify_str
=
X509_verify_cert_error_string
(
verify_code
);
if
(
verify_str
!=
NULL
) {
verify_obj
=
PyUnicode_FromString
(
verify_str
);
}
else
{
verify_obj
=
Py_NewRef
(
Py_None
);
}
break
;
}
if
(
verify_obj
==
NULL
) {
goto
fail
;
}
}
// Format message roughly as:
// [lib_obj: reason_obj] errstr: verify_obj (_ssl.c:lineno)
// with parts missing/replaced if unavailable
writer
=
PyUnicodeWriter_Create
(
64
);
if
(!
writer
) {
goto
fail
;
}
if
(
lib_obj
) {
if
(
PyUnicodeWriter_Format
(
writer
,
"[%S"
,
lib_obj
)
<
0
) {
goto
fail
;
}
if
(
reason_obj
) {
if
(
PyUnicodeWriter_Format
(
writer
,
": %S"
,
reason_obj
)
<
0
) {
goto
fail
;
}
}
if
(
PyUnicodeWriter_WriteASCII
(
writer
,
"] "
,
2
)
<
0
) {
goto
fail
;
}
}
if
(
errstr
) {
if
(
PyUnicodeWriter_Format
(
writer
,
"%s"
,
errstr
)
<
0
) {
goto
fail
;
}
}
else
{
if
(
PyUnicodeWriter_Format
(
writer
,
"unknown error (0x%lx)"
,
errcode
)
<
0
) {
goto
fail
;
}
}
if
(
verify_obj
) {
if
(
PyUnicodeWriter_Format
(
writer
,
": %S"
,
verify_obj
)
<
0
) {
goto
fail
;
}
}
if
(
PyUnicodeWriter_Format
(
writer
,
" (_ssl.c:%d)"
,
lineno
)
<
0
) {
goto
fail
;
}
msg
=
PyUnicodeWriter_Finish
(
writer
);
writer
=
NULL
;
if
(!
msg
) {
goto
fail
;
}
init_value
=
Py_BuildValue
(
"iN"
,
ERR_GET_REASON
(
ssl_errno
),
msg
);
if
(
init_value
==
NULL
)
goto
fail
;
err_value
=
PyObject_CallObject
(
type
,
init_value
);
Py_DECREF
(
init_value
);
if
(
err_value
==
NULL
)
goto
fail
;
if
(
reason_obj
==
NULL
)
reason_obj
=
Py_None
;
if
(
PyObject_SetAttr
(
err_value
,
state
->
str_reason
,
reason_obj
))
goto
fail
;
if
(
lib_obj
==
NULL
)
lib_obj
=
Py_None
;
if
(
PyObject_SetAttr
(
err_value
,
state
->
str_library
,
lib_obj
))
goto
fail
;
if
((
sslsock
!=
NULL
)
&&
(
type
==
state
->
PySSLCertVerificationErrorObject
)) {
/* Only set verify code / message for SSLCertVerificationError */
if
(
PyObject_SetAttr
(
err_value
,
state
->
str_verify_code
,
verify_code_obj
))
goto
fail
;
if
(
PyObject_SetAttr
(
err_value
,
state
->
str_verify_message
,
verify_obj
))
goto
fail
;
}
PyErr_SetObject
(
type
,
err_value
);
fail
:
Py_XDECREF
(
err_value
);
Py_XDECREF
(
verify_code_obj
);
Py_XDECREF
(
verify_obj
);
PyUnicodeWriter_Discard
(
writer
);
}
static
void
set_eof_error
(
PySSLSocket
*
sslsock
)
{
_sslmodulestate
*
state
=
get_state_sock
(
sslsock
);
fill_and_set_sslerror
(
state
,
sslsock
,
state
->
PySSLEOFErrorObject
,
PY_SSL_ERROR_EOF
,
"EOF occurred in violation of protocol"
,
__LINE__
,
0
);
}
// Set the appropriate SSL error exception.
// err - error information from SSL and libc
// exc - if not NULL, an exception from _debughelpers.c callback to be chained
static
PyObject
*
PySSL_SetError
(
PySSLSocket
*
sslsock
,
_PySSLError
err
,
PyObject
*
exc
,
const
char
*
filename
,
int
lineno
)
{
PyObject
*
type
;
char
*
errstr
=
NULL
;
enum
py_ssl_error
p
=
PY_SSL_ERROR_NONE
;
unsigned long
e
=
0
;
assert
(
sslsock
!=
NULL
);
_sslmodulestate
*
state
=
get_state_sock
(
sslsock
);
type
=
state
->
PySSLErrorObject
;
// ERR functions are thread local, no need to lock them.
e
=
ERR_peek_last_error
();
if
(
sslsock
->
ssl
!=
NULL
) {
switch
(
err
.
ssl
) {
case
SSL_ERROR_ZERO_RETURN
:
errstr
=
"TLS/SSL connection has been closed (EOF)"
;
type
=
state
->
PySSLZeroReturnErrorObject
;
p
=
PY_SSL_ERROR_ZERO_RETURN
;
break
;
case
SSL_ERROR_WANT_READ
:
errstr
=
"The operation did not complete (read)"
;
type
=
state
->
PySSLWantReadErrorObject
;
p
=
PY_SSL_ERROR_WANT_READ
;
break
;
case
SSL_ERROR_WANT_WRITE
:
p
=
PY_SSL_ERROR_WANT_WRITE
;
type
=
state
->
PySSLWantWriteErrorObject
;
errstr
=
"The operation did not complete (write)"
;
break
;
case
SSL_ERROR_WANT_X509_LOOKUP
:
p
=
PY_SSL_ERROR_WANT_X509_LOOKUP
;
errstr
=
"The operation did not complete (X509 lookup)"
;
break
;
case
SSL_ERROR_WANT_CONNECT
:
p
=
PY_SSL_ERROR_WANT_CONNECT
;
errstr
=
"The operation did not complete (connect)"
;
break
;
case
SSL_ERROR_SYSCALL
:
{
if
(
e
==
0
) {
/* underlying BIO reported an I/O error */
ERR_clear_error
();
#ifdef
MS_WINDOWS
if
(
err
.
ws
) {
return
PyErr_SetFromWindowsErr
(
err
.
ws
);
}
#endif
if
(
err
.
c
) {
errno
=
err
.
c
;
return
PyErr_SetFromErrno
(
PyExc_OSError
);
}
else
{
p
=
PY_SSL_ERROR_EOF
;
type
=
state
->
PySSLEOFErrorObject
;
errstr
=
"EOF occurred in violation of protocol"
;
}
}
else
{
if
(
ERR_GET_LIB
(
e
)
==
ERR_LIB_SSL
&&
ERR_GET_REASON
(
e
)
==
SSL_R_CERTIFICATE_VERIFY_FAILED
) {
type
=
state
->
PySSLCertVerificationErrorObject
;
}
if
(
ERR_GET_LIB
(
e
)
==
ERR_LIB_SYS
) {
// A system error is being reported; reason is set to errno
errno
=
ERR_GET_REASON
(
e
);
return
PyErr_SetFromErrno
(
PyExc_OSError
);
}
p
=
PY_SSL_ERROR_SYSCALL
;
}
break
;
}
case
SSL_ERROR_SSL
:
{
p
=
PY_SSL_ERROR_SSL
;
if
(
e
==
0
) {
/* possible? */
errstr
=
"A failure in the SSL library occurred"
;
}
if
(
ERR_GET_LIB
(
e
)
==
ERR_LIB_SSL
&&
ERR_GET_REASON
(
e
)
==
SSL_R_CERTIFICATE_VERIFY_FAILED
) {
type
=
state
->
PySSLCertVerificationErrorObject
;
}
#if
defined(
SSL_R_UNEXPECTED_EOF_WHILE_READING
)
/* OpenSSL 3.0 changed transport EOF from SSL_ERROR_SYSCALL with
* zero return value to SSL_ERROR_SSL with a special error code. */
if
(
ERR_GET_LIB
(
e
)
==
ERR_LIB_SSL
&&
ERR_GET_REASON
(
e
)
==
SSL_R_UNEXPECTED_EOF_WHILE_READING
) {
p
=
PY_SSL_ERROR_EOF
;
type
=
state
->
PySSLEOFErrorObject
;
errstr
=
"EOF occurred in violation of protocol"
;
}
#endif
if
(
ERR_GET_LIB
(
e
)
==
ERR_LIB_SYS
) {
// A system error is being reported; reason is set to errno
errno
=
ERR_GET_REASON
(
e
);
return
PyErr_SetFromErrno
(
PyExc_OSError
);
}
break
;
}
default
:
p
=
PY_SSL_ERROR_INVALID_ERROR_CODE
;
errstr
=
"Invalid error code"
;
}
}
fill_and_set_sslerror
(
state
,
sslsock
,
type
,
p
,
errstr
,
lineno
,
e
);
ERR_clear_error
();
_PyErr_ChainExceptions1
(
exc
);
// chain any exceptions from callbacks
return
NULL
;
}
static
PyObject
*
_setSSLError
(
_sslmodulestate
*
state
,
const
char
*
errstr
,
int
errcode
,
const
char
*
filename
,
int
lineno
)
{
if
(
errstr
==
NULL
)
errcode
=
ERR_peek_last_error
();
else
errcode
=
0
;
fill_and_set_sslerror
(
state
,
NULL
,
state
->
PySSLErrorObject
,
errcode
,
errstr
,
lineno
,
errcode
);
ERR_clear_error
();
return
NULL
;
}
static
int
_ssl_deprecated
(
const
char
*
msg
,
int
stacklevel
) {
return
PyErr_WarnEx
(
PyExc_DeprecationWarning
,
msg
,
stacklevel
);
}
#define
PY_SSL_DEPRECATED
(
name
,
stacklevel
,
ret
) \
if (_ssl_deprecated((name), (stacklevel)) == -1) return (ret)
// Get the socket from a PySSLSocket, if it has one.
// Stores a strong reference in out_sock.
static
int
get_socket
(
PySSLSocket
*
obj
,
PySocketSockObject
*
*
out_sock
,
const
char
*
filename
,
int
lineno
)
{
if
(!
obj
->
Socket
) {
*
out_sock
=
NULL
;
return
0
;
}
PySocketSockObject
*
sock
;
int
res
=
PyWeakref_GetRef
(
obj
->
Socket
, (
PyObject
*
*
)
&
sock
);
if
(
res
==
0
||
sock
->
sock_fd
==
INVALID_SOCKET
) {
_setSSLError
(
get_state_sock
(
obj
),
"Underlying socket connection gone"
,
PY_SSL_ERROR_NO_SOCKET
,
filename
,
lineno
);
*
out_sock
=
NULL
;
return
-1
;
}
if
(
sock
!=
NULL
) {
/* just in case the blocking state of the socket has been changed */
int
nonblocking
=
(
sock
->
sock_timeout
>=
0
);
BIO_set_nbio
(
SSL_get_rbio
(
obj
->
ssl
),
nonblocking
);
BIO_set_nbio
(
SSL_get_wbio
(
obj
->
ssl
),
nonblocking
);
}
*
out_sock
=
sock
;
return
res
;
}
/*
* SSL objects
*/
static
int
_ssl_configure_hostname
(
PySSLSocket
*
self
,
const
char
*
server_hostname
)
{
int
retval
=
-1
;
ASN1_OCTET_STRING
*
ip
;
PyObject
*
hostname
;
size_t
len
;
assert
(
server_hostname
);
/* Disable OpenSSL's special mode with leading dot in hostname:
* When name starts with a dot (e.g ".example.com"), it will be
* matched by a certificate valid for any sub-domain of name.
*/
len
=
strlen
(
server_hostname
);
if
(
len
==
0
||
*
server_hostname
==
'.'
) {
PyErr_SetString
(
PyExc_ValueError
,
"server_hostname cannot be an empty string or start with a "
"leading dot."
);
return
retval
;
}
/* inet_pton is not available on all platforms. */
ip
=
a2i_IPADDRESS
(
server_hostname
);
if
(
ip
==
NULL
) {
ERR_clear_error
();
}
hostname
=
PyUnicode_Decode
(
server_hostname
,
len
,
"ascii"
,
"strict"
);
if
(
hostname
==
NULL
) {
goto
error
;
}
self
->
server_hostname
=
hostname
;
/* Only send SNI extension for non-IP hostnames */
if
(
ip
==
NULL
) {
if
(!
SSL_set_tlsext_host_name
(
self
->
ssl
,
server_hostname
)) {
_setSSLError
(
get_state_sock
(
self
),
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
error
;
}
}
if
(
self
->
ctx
->
check_hostname
) {
X509_VERIFY_PARAM
*
ssl_verification_params
=
SSL_get0_param
(
self
->
ssl
);
if
(
ip
==
NULL
) {
if
(!
X509_VERIFY_PARAM_set1_host
(
ssl_verification_params
,
server_hostname
,
strlen
(
server_hostname
))) {
_setSSLError
(
get_state_sock
(
self
),
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
error
;
}
}
else
{
if
(!
X509_VERIFY_PARAM_set1_ip
(
ssl_verification_params
,
ASN1_STRING_get0_data
(
ip
),
ASN1_STRING_length
(
ip
))) {
_setSSLError
(
get_state_sock
(
self
),
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
error
;
}
}
}
retval
=
0
;
error
:
if
(
ip
!=
NULL
) {
ASN1_OCTET_STRING_free
(
ip
);
}
return
retval
;
}
static
PySSLSocket
*
newPySSLSocket
(
PySSLContext
*
sslctx
,
PySocketSockObject
*
sock
,
enum
py_ssl_server_or_client
socket_type
,
char
*
server_hostname
,
PyObject
*
owner
,
PyObject
*
session
,
PySSLMemoryBIO
*
inbio
,
PySSLMemoryBIO
*
outbio
)
{
PySSLSocket
*
self
;
SSL_CTX
*
ctx
=
sslctx
->
ctx
;
if
((
socket_type
==
PY_SSL_SERVER
)
&&
(
sslctx
->
protocol
==
PY_SSL_VERSION_TLS_CLIENT
)) {
_setSSLError
(
get_state_ctx
(
sslctx
),
"Cannot create a server socket with a "
"PROTOCOL_TLS_CLIENT context"
,
0
,
__FILE__
,
__LINE__
);
return
NULL
;
}
if
((
socket_type
==
PY_SSL_CLIENT
)
&&
(
sslctx
->
protocol
==
PY_SSL_VERSION_TLS_SERVER
)) {
_setSSLError
(
get_state_ctx
(
sslctx
),
"Cannot create a client socket with a "
"PROTOCOL_TLS_SERVER context"
,
0
,
__FILE__
,
__LINE__
);
return
NULL
;
}
self
=
PyObject_GC_New
(
PySSLSocket
,
get_state_ctx
(
sslctx
)
->
PySSLSocket_Type
);
if
(
self
==
NULL
)
return
NULL
;
self
->
ssl
=
NULL
;
self
->
Socket
=
NULL
;
self
->
ctx
=
(
PySSLContext
*
)
Py_NewRef
(
sslctx
);
self
->
shutdown_seen_zero
=
0
;
self
->
owner
=
NULL
;
self
->
server_hostname
=
NULL
;
self
->
got_eof_error
=
0
;
/* Make sure the SSL error state is initialized */
ERR_clear_error
();
PySSL_BEGIN_ALLOW_THREADS
(
sslctx
)
self
->
ssl
=
SSL_new
(
ctx
);
PySSL_END_ALLOW_THREADS
(
sslctx
)
if
(
self
->
ssl
==
NULL) {
_setSSLError
(
get_state_ctx
(
sslctx
),
NULL
,
0
,
__FILE__
,
__LINE__
);
Py_DECREF
(
self
);
return
NULL
;
}
if
(
socket_type
==
PY_SSL_SERVER
) {
#define
SID_CTX
"Python"
/* Set the session id context (server-side only) */
SSL_set_session_id_context
(
self
->
ssl
, (
const
unsigned
char
*
)
SID_CTX
,
sizeof
(
SID_CTX
));
#undef
SID_CTX
}
/* bpo43522 and OpenSSL < 1.1.1l: copy hostflags manually */
#if
OPENSSL_VERSION_NUMBER
<
0x101010cf
X509_VERIFY_PARAM
*
ssl_verification_params
=
SSL_get0_param
(
self
->
ssl
);
X509_VERIFY_PARAM
*
ssl_ctx_verification_params
=
SSL_CTX_get0_param
(
ctx
);
unsigned
int
ssl_ctx_host_flags
=
X509_VERIFY_PARAM_get_hostflags
(
ssl_ctx_verification_params
);
X509_VERIFY_PARAM_set_hostflags
(
ssl_verification_params
,
ssl_ctx_host_flags
);
#endif
SSL_set_app_data
(
self
->
ssl
,
self
);
if
(
sock
) {
SSL_set_fd
(
self
->
ssl
,
Py_SAFE_DOWNCAST
(
sock
->
sock_fd
,
SOCKET_T
,
int
));
}
else
{
/* BIOs are reference counted and SSL_set_bio borrows our reference.
* To prevent a double free in memory_bio_dealloc() we need to take an
* extra reference here. */
BIO_up_ref
(
inbio
->
bio
);
BIO_up_ref
(
outbio
->
bio
);
SSL_set_bio
(
self
->
ssl
,
inbio
->
bio
,
outbio
->
bio
);
}
SSL_set_mode
(
self
->
ssl
,
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
|
SSL_MODE_AUTO_RETRY
);
#if
defined(
PySSL_HAVE_POST_HS_AUTH
)
if
(
sslctx
->
post_handshake_auth
==
1
) {
if
(
socket_type
==
PY_SSL_SERVER
) {
/* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE.
* Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and
* only in combination with SSL_VERIFY_PEER flag. */
int
mode
=
SSL_get_verify_mode
(
self
->
ssl
);
if
(
mode
&
SSL_VERIFY_PEER
) {
mode
|=
SSL_VERIFY_POST_HANDSHAKE
;
SSL_set_verify
(
self
->
ssl
,
mode
,
NULL
);
}
}
else
{
/* client socket */
SSL_set_post_handshake_auth
(
self
->
ssl
,
1
);
}
}
#endif
if
(
server_hostname
!=
NULL
) {
if
(
_ssl_configure_hostname
(
self
,
server_hostname
)
<
0
) {
Py_DECREF
(
self
);
return
NULL
;
}
}
/* If the socket is in non-blocking mode or timeout mode, set the BIO
* to non-blocking mode (blocking is the default)
*/
if
(
sock
&&
sock
->
sock_timeout
>=
0
) {
BIO_set_nbio
(
SSL_get_rbio
(
self
->
ssl
),
1
);
BIO_set_nbio
(
SSL_get_wbio
(
self
->
ssl
),
1
);
}
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL