FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/Modules/_ssl.c at master · pylee/python · GitHub
pylee
/
python
Public
forked from
glix/python
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
Modules
/
_ssl.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
1638 lines (1424 loc) · 42 KB
Breadcrumbs
python
/
Modules
/
_ssl.c
Copy path
File metadata and controls
1638 lines (1424 loc) · 42 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 what about SSL_MODE_AUTO_RETRY?
*/
#include
"Python.h"
#ifdef
WITH_THREAD
#include
"pythread.h"
#define
PySSL_BEGIN_ALLOW_THREADS
{ \
PyThreadState *_save = NULL; \
if (_ssl_locks_count>0) {_save = PyEval_SaveThread();}
#define
PySSL_BLOCK_THREADS
if (_ssl_locks_count>0){PyEval_RestoreThread(_save)};
#define
PySSL_UNBLOCK_THREADS
if (_ssl_locks_count>0){_save = PyEval_SaveThread()};
#define
PySSL_END_ALLOW_THREADS
if (_ssl_locks_count>0){PyEval_RestoreThread(_save);} \
}
#else
/* no WITH_THREAD */
#define
PySSL_BEGIN_ALLOW_THREADS
#define
PySSL_BLOCK_THREADS
#define
PySSL_UNBLOCK_THREADS
#define
PySSL_END_ALLOW_THREADS
#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_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
,
PY_SSL_VERSION_SSL23
,
PY_SSL_VERSION_TLS1
,
};
/* Include symbols from _socket module */
#include
"socketmodule.h"
#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"
/* SSL error object */
static
PyObject
*
PySSLErrorObject
;
#ifdef
WITH_THREAD
/* serves as a flag to see whether we've initialized the SSL thread support. */
/* 0 means no, greater than 0 means yes */
static
unsigned
int
_ssl_locks_count
=
0
;
#endif
/* def WITH_THREAD */
/* SSL socket object */
#define
X509_NAME_MAXLEN
256
/* RAND_* APIs got added to OpenSSL in 0.9.5 */
#if
OPENSSL_VERSION_NUMBER
>=
0x0090500fL
# define
HAVE_OPENSSL_RAND
1
#else
# undef
HAVE_OPENSSL_RAND
#endif
typedef
struct
{
PyObject_HEAD
PySocketSockObject
*
Socket
;
/* Socket on which we're layered */
SSL_CTX
*
ctx
;
SSL
*
ssl
;
X509
*
peer_cert
;
char
server
[
X509_NAME_MAXLEN
];
char
issuer
[
X509_NAME_MAXLEN
];
}
PySSLObject
;
static
PyTypeObject
PySSL_Type
;
static
PyObject
*
PySSL_SSLwrite
(
PySSLObject
*
self
,
PyObject
*
args
);
static
PyObject
*
PySSL_SSLread
(
PySSLObject
*
self
,
PyObject
*
args
);
static
int
check_socket_and_wait_for_timeout
(
PySocketSockObject
*
s
,
int
writing
);
static
PyObject
*
PySSL_peercert
(
PySSLObject
*
self
,
PyObject
*
args
);
static
PyObject
*
PySSL_cipher
(
PySSLObject
*
self
);
#define
PySSLObject_Check
(
v
) (Py_TYPE(v) == &PySSL_Type)
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
STRINGIFY1
(
x
) #x
#define
STRINGIFY2
(
x
) STRINGIFY1(x)
#define
ERRSTR1
(
x
,
y
,
z
) (x ":" y ": " z)
#define
ERRSTR
(
x
) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x)
/* XXX It might be helpful to augment the error message generated
below with the name of the SSL function that generated the error.
I expect it's obvious most of the time.
*/
static
PyObject
*
PySSL_SetError
(
PySSLObject
*
obj
,
int
ret
,
char
*
filename
,
int
lineno
)
{
PyObject
*
v
;
char
buf
[
2048
];
char
*
errstr
;
int
err
;
enum
py_ssl_error
p
=
PY_SSL_ERROR_NONE
;
assert
(
ret
<=
0
);
if
(
obj
->
ssl
!=
NULL
) {
err
=
SSL_get_error
(
obj
->
ssl
,
ret
);
switch
(
err
) {
case
SSL_ERROR_ZERO_RETURN
:
errstr
=
"TLS/SSL connection has been closed"
;
p
=
PY_SSL_ERROR_ZERO_RETURN
;
break
;
case
SSL_ERROR_WANT_READ
:
errstr
=
"The operation did not complete (read)"
;
p
=
PY_SSL_ERROR_WANT_READ
;
break
;
case
SSL_ERROR_WANT_WRITE
:
p
=
PY_SSL_ERROR_WANT_WRITE
;
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
:
{
unsigned long
e
=
ERR_get_error
();
if
(
e
==
0
) {
if
(
ret
==
0
||
!
obj
->
Socket
) {
p
=
PY_SSL_ERROR_EOF
;
errstr
=
"EOF occurred in violation of protocol"
;
}
else
if
(
ret
==
-1
) {
/* underlying BIO reported an I/O error */
return
obj
->
Socket
->
errorhandler
();
}
else
{
/* possible? */
p
=
PY_SSL_ERROR_SYSCALL
;
errstr
=
"Some I/O error occurred"
;
}
}
else
{
p
=
PY_SSL_ERROR_SYSCALL
;
/* XXX Protected by global interpreter lock */
errstr
=
ERR_error_string
(
e
,
NULL
);
}
break
;
}
case
SSL_ERROR_SSL
:
{
unsigned long
e
=
ERR_get_error
();
p
=
PY_SSL_ERROR_SSL
;
if
(
e
!=
0
)
/* XXX Protected by global interpreter lock */
errstr
=
ERR_error_string
(
e
,
NULL
);
else
{
/* possible? */
errstr
=
"A failure in the SSL library occurred"
;
}
break
;
}
default
:
p
=
PY_SSL_ERROR_INVALID_ERROR_CODE
;
errstr
=
"Invalid error code"
;
}
}
else
{
errstr
=
ERR_error_string
(
ERR_peek_last_error
(),
NULL
);
}
PyOS_snprintf
(
buf
,
sizeof
(
buf
),
"_ssl.c:%d: %s"
,
lineno
,
errstr
);
v
=
Py_BuildValue
(
"(is)"
,
p
,
buf
);
if
(
v
!=
NULL
) {
PyErr_SetObject
(
PySSLErrorObject
,
v
);
Py_DECREF
(
v
);
}
return
NULL
;
}
static
PyObject
*
_setSSLError
(
char
*
errstr
,
int
errcode
,
char
*
filename
,
int
lineno
) {
char
buf
[
2048
];
PyObject
*
v
;
if
(
errstr
==
NULL
) {
errcode
=
ERR_peek_last_error
();
errstr
=
ERR_error_string
(
errcode
,
NULL
);
}
PyOS_snprintf
(
buf
,
sizeof
(
buf
),
"_ssl.c:%d: %s"
,
lineno
,
errstr
);
v
=
Py_BuildValue
(
"(is)"
,
errcode
,
buf
);
if
(
v
!=
NULL
) {
PyErr_SetObject
(
PySSLErrorObject
,
v
);
Py_DECREF
(
v
);
}
return
NULL
;
}
static
PySSLObject
*
newPySSLObject
(
PySocketSockObject
*
Sock
,
char
*
key_file
,
char
*
cert_file
,
enum
py_ssl_server_or_client
socket_type
,
enum
py_ssl_cert_requirements
certreq
,
enum
py_ssl_version
proto_version
,
char
*
cacerts_file
)
{
PySSLObject
*
self
;
char
*
errstr
=
NULL
;
int
ret
;
int
verification_mode
;
self
=
PyObject_New
(
PySSLObject
,
&
PySSL_Type
);
/* Create new object */
if
(
self
==
NULL
)
return
NULL
;
memset
(
self
->
server
,
'\0'
,
sizeof
(
char
)
*
X509_NAME_MAXLEN
);
memset
(
self
->
issuer
,
'\0'
,
sizeof
(
char
)
*
X509_NAME_MAXLEN
);
self
->
peer_cert
=
NULL
;
self
->
ssl
=
NULL
;
self
->
ctx
=
NULL
;
self
->
Socket
=
NULL
;
/* Make sure the SSL error state is initialized */
(
void
)
ERR_get_state
();
ERR_clear_error
();
if
((
key_file
&&
!
cert_file
)
||
(!
key_file
&&
cert_file
)) {
errstr
=
ERRSTR
(
"Both the key & certificate files "
"must be specified"
);
goto
fail
;
}
if
((
socket_type
==
PY_SSL_SERVER
)
&&
((
key_file
==
NULL
)
||
(
cert_file
==
NULL
))) {
errstr
=
ERRSTR
(
"Both the key & certificate files "
"must be specified for server-side operation"
);
goto
fail
;
}
PySSL_BEGIN_ALLOW_THREADS
if
(
proto_version
==
PY_SSL_VERSION_TLS1
)
self
->
ctx
=
SSL_CTX_new
(
TLSv1_method
());
/* Set up context */
else
if
(
proto_version
==
PY_SSL_VERSION_SSL3
)
self
->
ctx
=
SSL_CTX_new
(
SSLv3_method
());
/* Set up context */
else
if
(
proto_version
==
PY_SSL_VERSION_SSL2
)
self
->
ctx
=
SSL_CTX_new
(
SSLv2_method
());
/* Set up context */
else
if
(
proto_version
==
PY_SSL_VERSION_SSL23
)
self
->
ctx
=
SSL_CTX_new
(
SSLv23_method
());
/* Set up context */
PySSL_END_ALLOW_THREADS
if
(
self
->
ctx
==
NULL) {
errstr
=
ERRSTR
(
"Invalid SSL protocol variant specified."
);
goto
fail
;
}
if
(
certreq
!=
PY_SSL_CERT_NONE
) {
if
(
cacerts_file
==
NULL
) {
errstr
=
ERRSTR
(
"No root certificates specified for "
"verification of other-side certificates."
);
goto
fail
;
}
else
{
PySSL_BEGIN_ALLOW_THREADS
ret
=
SSL_CTX_load_verify_locations
(
self
->
ctx
,
cacerts_file
,
NULL
);
PySSL_END_ALLOW_THREADS
if
(
ret
!=
1
) {
_setSSLError
(
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
fail
;
}
}
}
if
(
key_file
) {
PySSL_BEGIN_ALLOW_THREADS
ret
=
SSL_CTX_use_PrivateKey_file
(
self
->
ctx
,
key_file
,
SSL_FILETYPE_PEM
);
PySSL_END_ALLOW_THREADS
if
(
ret
!=
1
) {
_setSSLError
(
NULL
,
ret
,
__FILE__
,
__LINE__
);
goto
fail
;
}
PySSL_BEGIN_ALLOW_THREADS
ret
=
SSL_CTX_use_certificate_chain_file
(
self
->
ctx
,
cert_file
);
PySSL_END_ALLOW_THREADS
if
(
ret
!=
1
) {
/*
fprintf(stderr, "ret is %d, errcode is %lu, %lu, with file \"%s\"\n",
ret, ERR_peek_error(), ERR_peek_last_error(), cert_file);
*/
if
(
ERR_peek_last_error
()
!=
0
) {
_setSSLError
(
NULL
,
ret
,
__FILE__
,
__LINE__
);
goto
fail
;
}
}
}
/* ssl compatibility */
SSL_CTX_set_options
(
self
->
ctx
,
SSL_OP_ALL
);
verification_mode
=
SSL_VERIFY_NONE
;
if
(
certreq
==
PY_SSL_CERT_OPTIONAL
)
verification_mode
=
SSL_VERIFY_PEER
;
else
if
(
certreq
==
PY_SSL_CERT_REQUIRED
)
verification_mode
=
(
SSL_VERIFY_PEER
|
SSL_VERIFY_FAIL_IF_NO_PEER_CERT
);
SSL_CTX_set_verify
(
self
->
ctx
,
verification_mode
,
NULL
);
/* set verify lvl */
PySSL_BEGIN_ALLOW_THREADS
self
->
ssl
=
SSL_new
(
self
->
ctx
);
/* New ssl struct */
PySSL_END_ALLOW_THREADS
SSL_set_fd
(
self
->
ssl
,
Sock
->
sock_fd
);
/* Set the socket for SSL */
/* 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_timeout
>=
0.0
) {
/* Set both the read and write BIO's to non-blocking mode */
BIO_set_nbio
(
SSL_get_rbio
(
self
->
ssl
),
1
);
BIO_set_nbio
(
SSL_get_wbio
(
self
->
ssl
),
1
);
}
PySSL_BEGIN_ALLOW_THREADS
if
(
socket_type
==
PY_SSL_CLIENT
)
SSL_set_connect_state
(
self
-
>
ssl
);
else
SSL_set_accept_state
(
self
->
ssl
);
PySSL_END_ALLOW_THREADS
self
->
Socket
=
Sock
;
Py_INCREF
(
self
->
Socket
);
return
self
;
fail
:
if
(
errstr
)
PyErr_SetString
(
PySSLErrorObject
,
errstr
);
Py_DECREF
(
self
);
return
NULL
;
}
static
PyObject
*
PySSL_sslwrap
(
PyObject
*
self
,
PyObject
*
args
)
{
PySocketSockObject
*
Sock
;
int
server_side
=
0
;
int
verification_mode
=
PY_SSL_CERT_NONE
;
int
protocol
=
PY_SSL_VERSION_SSL23
;
char
*
key_file
=
NULL
;
char
*
cert_file
=
NULL
;
char
*
cacerts_file
=
NULL
;
if
(!
PyArg_ParseTuple
(
args
,
"O!i|zziiz:sslwrap"
,
PySocketModule
.
Sock_Type
,
&
Sock
,
&
server_side
,
&
key_file
,
&
cert_file
,
&
verification_mode
,
&
protocol
,
&
cacerts_file
))
return
NULL
;
/*
fprintf(stderr,
"server_side is %d, keyfile %p, certfile %p, verify_mode %d, "
"protocol %d, certs %p\n",
server_side, key_file, cert_file, verification_mode,
protocol, cacerts_file);
*/
return
(
PyObject
*
)
newPySSLObject
(
Sock
,
key_file
,
cert_file
,
server_side
,
verification_mode
,
protocol
,
cacerts_file
);
}
PyDoc_STRVAR
(
ssl_doc
,
"sslwrap(socket, server_side, [keyfile, certfile, certs_mode, protocol,\n"
" cacertsfile]) -> sslobject"
);
/* SSL object methods */
static
PyObject
*
PySSL_SSLdo_handshake
(
PySSLObject
*
self
)
{
int
ret
;
int
err
;
int
sockstate
;
/* Actually negotiate SSL connection */
/* XXX If SSL_do_handshake() returns 0, it's also a failure. */
sockstate
=
0
;
do
{
PySSL_BEGIN_ALLOW_THREADS
ret
=
SSL_do_handshake
(
self
->
ssl
);
err
=
SSL_get_error
(
self
->
ssl
,
ret
);
PySSL_END_ALLOW_THREADS
if
(
PyErr_CheckSignals
()) {
return
NULL
;
}
if
(
err
==
SSL_ERROR_WANT_READ
) {
sockstate
=
check_socket_and_wait_for_timeout
(
self
->
Socket
,
0
);
}
else
if
(
err
==
SSL_ERROR_WANT_WRITE
) {
sockstate
=
check_socket_and_wait_for_timeout
(
self
->
Socket
,
1
);
}
else
{
sockstate
=
SOCKET_OPERATION_OK
;
}
if
(
sockstate
==
SOCKET_HAS_TIMED_OUT
) {
PyErr_SetString
(
PySSLErrorObject
,
ERRSTR
(
"The handshake operation timed out"
));
return
NULL
;
}
else
if
(
sockstate
==
SOCKET_HAS_BEEN_CLOSED
) {
PyErr_SetString
(
PySSLErrorObject
,
ERRSTR
(
"Underlying socket has been closed."
));
return
NULL
;
}
else
if
(
sockstate
==
SOCKET_TOO_LARGE_FOR_SELECT
) {
PyErr_SetString
(
PySSLErrorObject
,
ERRSTR
(
"Underlying socket too large for select()."
));
return
NULL
;
}
else
if
(
sockstate
==
SOCKET_IS_NONBLOCKING
) {
break
;
}
}
while
(
err
==
SSL_ERROR_WANT_READ
||
err
==
SSL_ERROR_WANT_WRITE
);
if
(
ret
<
1
)
return
PySSL_SetError
(
self
,
ret
,
__FILE__
,
__LINE__
);
self
->
ssl
->
debug
=
1
;
if
(
self
->
peer_cert
)
X509_free
(
self
->
peer_cert
);
PySSL_BEGIN_ALLOW_THREADS
if
((
self
->
peer_cert
=
SSL_get_peer_certificate
(
self
->
ssl
))) {
X509_NAME_oneline
(
X509_get_subject_name
(
self
->
peer_cert
),
self
->
server
,
X509_NAME_MAXLEN
);
X509_NAME_oneline
(
X509_get_issuer_name
(
self
->
peer_cert
),
self
->
issuer
,
X509_NAME_MAXLEN
);
}
PySSL_END_ALLOW_THREADS
Py_INCREF
(
Py_None
);
return
Py_None
;
}
static
PyObject
*
PySSL_server
(
PySSLObject
*
self
)
{
return
PyString_FromString
(
self
->
server
);
}
static
PyObject
*
PySSL_issuer
(
PySSLObject
*
self
)
{
return
PyString_FromString
(
self
->
issuer
);
}
static
PyObject
*
_create_tuple_for_attribute
(
ASN1_OBJECT
*
name
,
ASN1_STRING
*
value
) {
char
namebuf
[
X509_NAME_MAXLEN
];
int
buflen
;
PyObject
*
name_obj
;
PyObject
*
value_obj
;
PyObject
*
attr
;
unsigned
char
*
valuebuf
=
NULL
;
buflen
=
OBJ_obj2txt
(
namebuf
,
sizeof
(
namebuf
),
name
,
0
);
if
(
buflen
<
0
) {
_setSSLError
(
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
fail
;
}
name_obj
=
PyString_FromStringAndSize
(
namebuf
,
buflen
);
if
(
name_obj
==
NULL
)
goto
fail
;
buflen
=
ASN1_STRING_to_UTF8
(
&
valuebuf
,
value
);
if
(
buflen
<
0
) {
_setSSLError
(
NULL
,
0
,
__FILE__
,
__LINE__
);
Py_DECREF
(
name_obj
);
goto
fail
;
}
value_obj
=
PyUnicode_DecodeUTF8
((
char
*
)
valuebuf
,
buflen
,
"strict"
);
OPENSSL_free
(
valuebuf
);
if
(
value_obj
==
NULL
) {
Py_DECREF
(
name_obj
);
goto
fail
;
}
attr
=
PyTuple_New
(
2
);
if
(
attr
==
NULL
) {
Py_DECREF
(
name_obj
);
Py_DECREF
(
value_obj
);
goto
fail
;
}
PyTuple_SET_ITEM
(
attr
,
0
,
name_obj
);
PyTuple_SET_ITEM
(
attr
,
1
,
value_obj
);
return
attr
;
fail
:
return
NULL
;
}
static
PyObject
*
_create_tuple_for_X509_NAME
(
X509_NAME
*
xname
)
{
PyObject
*
dn
=
NULL
;
/* tuple which represents the "distinguished name" */
PyObject
*
rdn
=
NULL
;
/* tuple to hold a "relative distinguished name" */
PyObject
*
rdnt
;
PyObject
*
attr
=
NULL
;
/* tuple to hold an attribute */
int
entry_count
=
X509_NAME_entry_count
(
xname
);
X509_NAME_ENTRY
*
entry
;
ASN1_OBJECT
*
name
;
ASN1_STRING
*
value
;
int
index_counter
;
int
rdn_level
=
-1
;
int
retcode
;
dn
=
PyList_New
(
0
);
if
(
dn
==
NULL
)
return
NULL
;
/* now create another tuple to hold the top-level RDN */
rdn
=
PyList_New
(
0
);
if
(
rdn
==
NULL
)
goto
fail0
;
for
(
index_counter
=
0
;
index_counter
<
entry_count
;
index_counter
++
)
{
entry
=
X509_NAME_get_entry
(
xname
,
index_counter
);
/* check to see if we've gotten to a new RDN */
if
(
rdn_level
>=
0
) {
if
(
rdn_level
!=
entry
->
set
) {
/* yes, new RDN */
/* add old RDN to DN */
rdnt
=
PyList_AsTuple
(
rdn
);
Py_DECREF
(
rdn
);
if
(
rdnt
==
NULL
)
goto
fail0
;
retcode
=
PyList_Append
(
dn
,
rdnt
);
Py_DECREF
(
rdnt
);
if
(
retcode
<
0
)
goto
fail0
;
/* create new RDN */
rdn
=
PyList_New
(
0
);
if
(
rdn
==
NULL
)
goto
fail0
;
}
}
rdn_level
=
entry
->
set
;
/* now add this attribute to the current RDN */
name
=
X509_NAME_ENTRY_get_object
(
entry
);
value
=
X509_NAME_ENTRY_get_data
(
entry
);
attr
=
_create_tuple_for_attribute
(
name
,
value
);
/*
fprintf(stderr, "RDN level %d, attribute %s: %s\n",
entry->set,
PyString_AS_STRING(PyTuple_GET_ITEM(attr, 0)),
PyString_AS_STRING(PyTuple_GET_ITEM(attr, 1)));
*/
if
(
attr
==
NULL
)
goto
fail1
;
retcode
=
PyList_Append
(
rdn
,
attr
);
Py_DECREF
(
attr
);
if
(
retcode
<
0
)
goto
fail1
;
}
/* now, there's typically a dangling RDN */
if
((
rdn
!=
NULL
)
&&
(
PyList_Size
(
rdn
)
>
0
)) {
rdnt
=
PyList_AsTuple
(
rdn
);
Py_DECREF
(
rdn
);
if
(
rdnt
==
NULL
)
goto
fail0
;
retcode
=
PyList_Append
(
dn
,
rdnt
);
Py_DECREF
(
rdnt
);
if
(
retcode
<
0
)
goto
fail0
;
}
/* convert list to tuple */
rdnt
=
PyList_AsTuple
(
dn
);
Py_DECREF
(
dn
);
if
(
rdnt
==
NULL
)
return
NULL
;
return
rdnt
;
fail1
:
Py_XDECREF
(
rdn
);
fail0
:
Py_XDECREF
(
dn
);
return
NULL
;
}
static
PyObject
*
_get_peer_alt_names
(
X509
*
certificate
) {
/* this code follows the procedure outlined in
OpenSSL's crypto/x509v3/v3_prn.c:X509v3_EXT_print()
function to extract the STACK_OF(GENERAL_NAME),
then iterates through the stack to add the
names. */
int
i
,
j
;
PyObject
*
peer_alt_names
=
Py_None
;
PyObject
*
v
,
*
t
;
X509_EXTENSION
*
ext
=
NULL
;
GENERAL_NAMES
*
names
=
NULL
;
GENERAL_NAME
*
name
;
X509V3_EXT_METHOD
*
method
;
BIO
*
biobuf
=
NULL
;
char
buf
[
2048
];
char
*
vptr
;
int
len
;
const
unsigned
char
*
p
;
if
(
certificate
==
NULL
)
return
peer_alt_names
;
/* get a memory buffer */
biobuf
=
BIO_new
(
BIO_s_mem
());
i
=
0
;
while
((
i
=
X509_get_ext_by_NID
(
certificate
,
NID_subject_alt_name
,
i
)) >=
0
) {
if
(
peer_alt_names
==
Py_None
) {
peer_alt_names
=
PyList_New
(
0
);
if
(
peer_alt_names
==
NULL
)
goto
fail
;
}
/* now decode the altName */
ext
=
X509_get_ext
(
certificate
,
i
);
if
(!(
method
=
X509V3_EXT_get
(
ext
))) {
PyErr_SetString
(
PySSLErrorObject
,
ERRSTR
(
"No method for internalizing subjectAltName!"
));
goto
fail
;
}
p
=
ext
->
value
->
data
;
if
(
method
->
it
)
names
=
(
GENERAL_NAMES
*
) (
ASN1_item_d2i
(
NULL
,
&
p
,
ext
->
value
->
length
,
ASN1_ITEM_ptr
(
method
->
it
)));
else
names
=
(
GENERAL_NAMES
*
) (
method
->
d2i
(
NULL
,
&
p
,
ext
->
value
->
length
));
for
(
j
=
0
;
j
<
sk_GENERAL_NAME_num
(
names
);
j
++
) {
/* get a rendering of each name in the set of names */
name
=
sk_GENERAL_NAME_value
(
names
,
j
);
if
(
name
->
type
==
GEN_DIRNAME
) {
/* we special-case DirName as a tuple of tuples of attributes */
t
=
PyTuple_New
(
2
);
if
(
t
==
NULL
) {
goto
fail
;
}
v
=
PyString_FromString
(
"DirName"
);
if
(
v
==
NULL
) {
Py_DECREF
(
t
);
goto
fail
;
}
PyTuple_SET_ITEM
(
t
,
0
,
v
);
v
=
_create_tuple_for_X509_NAME
(
name
->
d
.
dirn
);
if
(
v
==
NULL
) {
Py_DECREF
(
t
);
goto
fail
;
}
PyTuple_SET_ITEM
(
t
,
1
,
v
);
}
else
{
/* for everything else, we use the OpenSSL print form */
(
void
)
BIO_reset
(
biobuf
);
GENERAL_NAME_print
(
biobuf
,
name
);
len
=
BIO_gets
(
biobuf
,
buf
,
sizeof
(
buf
)
-
1
);
if
(
len
<
0
) {
_setSSLError
(
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
fail
;
}
vptr
=
strchr
(
buf
,
':'
);
if
(
vptr
==
NULL
)
goto
fail
;
t
=
PyTuple_New
(
2
);
if
(
t
==
NULL
)
goto
fail
;
v
=
PyString_FromStringAndSize
(
buf
, (
vptr
-
buf
));
if
(
v
==
NULL
) {
Py_DECREF
(
t
);
goto
fail
;
}
PyTuple_SET_ITEM
(
t
,
0
,
v
);
v
=
PyString_FromStringAndSize
((
vptr
+
1
), (
len
-
(
vptr
-
buf
+
1
)));
if
(
v
==
NULL
) {
Py_DECREF
(
t
);
goto
fail
;
}
PyTuple_SET_ITEM
(
t
,
1
,
v
);
}
/* and add that rendering to the list */
if
(
PyList_Append
(
peer_alt_names
,
t
)
<
0
) {
Py_DECREF
(
t
);
goto
fail
;
}
Py_DECREF
(
t
);
}
}
BIO_free
(
biobuf
);
if
(
peer_alt_names
!=
Py_None
) {
v
=
PyList_AsTuple
(
peer_alt_names
);
Py_DECREF
(
peer_alt_names
);
return
v
;
}
else
{
return
peer_alt_names
;
}
fail
:
if
(
biobuf
!=
NULL
)
BIO_free
(
biobuf
);
if
(
peer_alt_names
!=
Py_None
) {
Py_XDECREF
(
peer_alt_names
);
}
return
NULL
;
}
static
PyObject
*
_decode_certificate
(
X509
*
certificate
,
int
verbose
) {
PyObject
*
retval
=
NULL
;
BIO
*
biobuf
=
NULL
;
PyObject
*
peer
;
PyObject
*
peer_alt_names
=
NULL
;
PyObject
*
issuer
;
PyObject
*
version
;
PyObject
*
sn_obj
;
ASN1_INTEGER
*
serialNumber
;
char
buf
[
2048
];
int
len
;
ASN1_TIME
*
notBefore
,
*
notAfter
;
PyObject
*
pnotBefore
,
*
pnotAfter
;
retval
=
PyDict_New
();
if
(
retval
==
NULL
)
return
NULL
;
peer
=
_create_tuple_for_X509_NAME
(
X509_get_subject_name
(
certificate
));
if
(
peer
==
NULL
)
goto
fail0
;
if
(
PyDict_SetItemString
(
retval
, (
const
char
*
)
"subject"
,
peer
)
<
0
) {
Py_DECREF
(
peer
);
goto
fail0
;
}
Py_DECREF
(
peer
);
if
(
verbose
) {
issuer
=
_create_tuple_for_X509_NAME
(
X509_get_issuer_name
(
certificate
));
if
(
issuer
==
NULL
)
goto
fail0
;
if
(
PyDict_SetItemString
(
retval
, (
const
char
*
)
"issuer"
,
issuer
)
<
0
) {
Py_DECREF
(
issuer
);
goto
fail0
;
}
Py_DECREF
(
issuer
);
version
=
PyInt_FromLong
(
X509_get_version
(
certificate
)
+
1
);
if
(
PyDict_SetItemString
(
retval
,
"version"
,
version
)
<
0
) {
Py_DECREF
(
version
);
goto
fail0
;
}
Py_DECREF
(
version
);
}
/* get a memory buffer */
biobuf
=
BIO_new
(
BIO_s_mem
());
if
(
verbose
) {
(
void
)
BIO_reset
(
biobuf
);
serialNumber
=
X509_get_serialNumber
(
certificate
);
/* should not exceed 20 octets, 160 bits, so buf is big enough */
i2a_ASN1_INTEGER
(
biobuf
,
serialNumber
);
len
=
BIO_gets
(
biobuf
,
buf
,
sizeof
(
buf
)
-
1
);
if
(
len
<
0
) {
_setSSLError
(
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
fail1
;
}
sn_obj
=
PyString_FromStringAndSize
(
buf
,
len
);
if
(
sn_obj
==
NULL
)
goto
fail1
;
if
(
PyDict_SetItemString
(
retval
,
"serialNumber"
,
sn_obj
)
<
0
) {
Py_DECREF
(
sn_obj
);
goto
fail1
;
}
Py_DECREF
(
sn_obj
);
(
void
)
BIO_reset
(
biobuf
);
notBefore
=
X509_get_notBefore
(
certificate
);
ASN1_TIME_print
(
biobuf
,
notBefore
);
len
=
BIO_gets
(
biobuf
,
buf
,
sizeof
(
buf
)
-
1
);
if
(
len
<
0
) {
_setSSLError
(
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
fail1
;
}
pnotBefore
=
PyString_FromStringAndSize
(
buf
,
len
);
if
(
pnotBefore
==
NULL
)
goto
fail1
;
if
(
PyDict_SetItemString
(
retval
,
"notBefore"
,
pnotBefore
)
<
0
) {
Py_DECREF
(
pnotBefore
);
goto
fail1
;
}
Py_DECREF
(
pnotBefore
);
}
(
void
)
BIO_reset
(
biobuf
);
notAfter
=
X509_get_notAfter
(
certificate
);
ASN1_TIME_print
(
biobuf
,
notAfter
);
len
=
BIO_gets
(
biobuf
,
buf
,
sizeof
(
buf
)
-
1
);
if
(
len
<
0
) {
_setSSLError
(
NULL
,
0
,
__FILE__
,
__LINE__
);
goto
fail1
;
}
pnotAfter
=
PyString_FromStringAndSize
(
buf
,
len
);
if
(
pnotAfter
==
NULL
)
goto
fail1
;
if
(
PyDict_SetItemString
(
retval
,
"notAfter"
,
pnotAfter
)
<
0
) {
Py_DECREF
(
pnotAfter
);
goto
fail1
;
}
Py_DECREF
(
pnotAfter
);
/* Now look for subjectAltName */
peer_alt_names
=
_get_peer_alt_names
(
certificate
);
if
(
peer_alt_names
==
NULL
)
goto
fail1
;
else
if
(
peer_alt_names
!=
Py_None
) {
if
(
PyDict_SetItemString
(
retval
,
"subjectAltName"
,
peer_alt_names
)
<
0
) {
Py_DECREF
(
peer_alt_names
);
goto
fail1
;
}
Py_DECREF
(
peer_alt_names
);
}
BIO_free
(
biobuf
);
return
retval
;
fail1
:
if
(
biobuf
!=
NULL
)
BIO_free
(
biobuf
);
fail0
:
Py_XDECREF
(
retval
);
return
NULL
;
}
static
PyObject
*
PySSL_test_decode_certificate
(
PyObject
*
mod
,
PyObject
*
args
) {
PyObject
*
retval
=
NULL
;
char
*
filename
=
NULL
;
X509
*
x
=
NULL
;
BIO
*
cert
;
int
verbose
=
1
;
if
(!
PyArg_ParseTuple
(
args
,
"s|i:test_decode_certificate"
,
&
filename
,
&
verbose
))
return
NULL
;
if
((
cert
=
BIO_new
(
BIO_s_file
()))
==
NULL
) {
PyErr_SetString
(
PySSLErrorObject
,
"Can't malloc memory to read file"
);
goto
fail0
;
}
if
(
BIO_read_filename
(
cert
,
filename
) <=
0
) {
PyErr_SetString
(
PySSLErrorObject
,
"Can't open file"
);
goto
fail0
;
}
x
=
PEM_read_bio_X509_AUX
(
cert
,
NULL
,
NULL
,
NULL
);
if
(
x
==
NULL
) {
PyErr_SetString
(
PySSLErrorObject
,
"Error decoding PEM-encoded file"
);
goto
fail0
;
}
retval
=
_decode_certificate
(
x
,
verbose
);
fail0
:
if
(
cert
!=
NULL
)
BIO_free
(
cert
);
return
retval
;
}
static
PyObject
*
PySSL_peercert
(
PySSLObject
*
self
,
PyObject
*
args
)
{
PyObject
*
retval
=
NULL
;
int
len
;
int
verification
;
PyObject
*
binary_mode
=
Py_None
;
if
(!
PyArg_ParseTuple
(
args
,
"|O:peer_certificate"
,
&
binary_mode
))
return
NULL
;
if
(!
self
->
peer_cert
)
Py_RETURN_NONE
;
if
(
PyObject_IsTrue
(
binary_mode
)) {
/* return cert in DER-encoded format */
unsigned
char
*
bytes_buf
=
NULL
;
bytes_buf
=
NULL
;
len
=
i2d_X509
(
self
->
peer_cert
,
&
bytes_buf
);
if
(
len
<
0
) {
PySSL_SetError
(
self
,
len
,
__FILE__
,
__LINE__
);
return
NULL
;
}
retval
=
PyString_FromStringAndSize
((
const
char
*
)
bytes_buf
,
len
);
OPENSSL_free
(
bytes_buf
);
return
retval
;
}
else
{
verification
=
SSL_CTX_get_verify_mode
(
self
->
ctx
);
if
((
verification
&
SSL_VERIFY_PEER
)
==
0
)
return
PyDict_New
();
else
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL