pypy /pypy/module/_ssl/interp_ssl.py

Language Python Lines 1790
MD5 Hash a84fe082c5650c7027d865efe13e4241 Estimated Cost $39,419 (why?)
Repository https://bitbucket.org/pypy/pypy/ View Raw File View Project SPDX
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
from rpython.rlib import rpoll, rsocket, rthread, rweakref, rgc
from rpython.rlib.rarithmetic import intmask, widen, r_uint
from rpython.rlib.ropenssl import *
from pypy.module._socket import interp_socket
from rpython.rlib._rsocket_rffi import MAX_FD_SIZE
from rpython.rlib.rposix import get_saved_errno
from rpython.rlib.rweakref import RWeakValueDictionary
from rpython.rlib.objectmodel import specialize, compute_unique_id
from rpython.rtyper.lltypesystem import lltype, rffi

from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError, oefmt, wrap_oserror
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.module._ssl.ssl_data import (
    LIBRARY_CODES_TO_NAMES, ERROR_CODES_TO_NAMES)


# user defined constants
X509_NAME_MAXLEN = 256
# these mirror ssl.h
PY_SSL_ERROR_NONE, PY_SSL_ERROR_SSL = 0, 1
PY_SSL_ERROR_WANT_READ, PY_SSL_ERROR_WANT_WRITE = 2, 3
PY_SSL_ERROR_WANT_X509_LOOKUP = 4
PY_SSL_ERROR_SYSCALL = 5  # look at error stack/return value/errno
PY_SSL_ERROR_ZERO_RETURN, PY_SSL_ERROR_WANT_CONNECT = 6, 7
# start of non ssl.h errorcodes
PY_SSL_ERROR_EOF = 8  # special case of SSL_ERROR_SYSCALL
PY_SSL_ERROR_INVALID_ERROR_CODE = 9

PY_SSL_CERT_NONE, PY_SSL_CERT_OPTIONAL, PY_SSL_CERT_REQUIRED = 0, 1, 2

PY_SSL_CLIENT, PY_SSL_SERVER = 0, 1

(PY_SSL_VERSION_SSL2, PY_SSL_VERSION_SSL3,
 PY_SSL_VERSION_SSL23, PY_SSL_VERSION_TLS1, PY_SSL_VERSION_TLS1_1,
 PY_SSL_VERSION_TLS1_2) = range(6)

SOCKET_IS_NONBLOCKING, SOCKET_IS_BLOCKING = 0, 1
SOCKET_HAS_TIMED_OUT, SOCKET_HAS_BEEN_CLOSED = 2, 3
SOCKET_TOO_LARGE_FOR_SELECT, SOCKET_OPERATION_OK = 4, 5

HAVE_RPOLL = 'poll' in dir(rpoll)

constants = {}
constants["SSL_ERROR_ZERO_RETURN"] = PY_SSL_ERROR_ZERO_RETURN
constants["SSL_ERROR_WANT_READ"] = PY_SSL_ERROR_WANT_READ
constants["SSL_ERROR_WANT_WRITE"] = PY_SSL_ERROR_WANT_WRITE
constants["SSL_ERROR_WANT_X509_LOOKUP"] = PY_SSL_ERROR_WANT_X509_LOOKUP
constants["SSL_ERROR_SYSCALL"] = PY_SSL_ERROR_SYSCALL
constants["SSL_ERROR_SSL"] = PY_SSL_ERROR_SSL
constants["SSL_ERROR_WANT_CONNECT"] = PY_SSL_ERROR_WANT_CONNECT
constants["SSL_ERROR_EOF"] = PY_SSL_ERROR_EOF
constants["SSL_ERROR_INVALID_ERROR_CODE"] = PY_SSL_ERROR_INVALID_ERROR_CODE

constants["CERT_NONE"]     = PY_SSL_CERT_NONE
constants["CERT_OPTIONAL"] = PY_SSL_CERT_OPTIONAL
constants["CERT_REQUIRED"] = PY_SSL_CERT_REQUIRED

constants["VERIFY_DEFAULT"] = 0
constants["VERIFY_CRL_CHECK_LEAF"] = X509_V_FLAG_CRL_CHECK
constants["VERIFY_CRL_CHECK_CHAIN"] = X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL
constants["VERIFY_X509_STRICT"] = X509_V_FLAG_X509_STRICT

constants["HAS_SNI"] = HAS_SNI
constants["HAS_TLS_UNIQUE"] = HAVE_OPENSSL_FINISHED
constants["HAS_ECDH"] = not OPENSSL_NO_ECDH
constants["HAS_NPN"] = HAS_NPN
constants["HAS_ALPN"] = HAS_ALPN

if not OPENSSL_NO_SSL2:
    constants["PROTOCOL_SSLv2"]  = PY_SSL_VERSION_SSL2
if not OPENSSL_NO_SSL3:
    constants["PROTOCOL_SSLv3"]  = PY_SSL_VERSION_SSL3
constants["PROTOCOL_SSLv23"] = PY_SSL_VERSION_SSL23
constants["PROTOCOL_TLSv1"]  = PY_SSL_VERSION_TLS1
if HAVE_TLSv1_2:
    constants["PROTOCOL_TLSv1_1"] = PY_SSL_VERSION_TLS1_1
    constants["OP_NO_TLSv1_1"] = SSL_OP_NO_TLSv1_1
    constants["PROTOCOL_TLSv1_2"] = PY_SSL_VERSION_TLS1_2
    constants["OP_NO_TLSv1_2"] = SSL_OP_NO_TLSv1_2

constants["OP_ALL"] = SSL_OP_ALL &~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
constants["OP_NO_SSLv2"] = SSL_OP_NO_SSLv2
constants["OP_NO_SSLv3"] = SSL_OP_NO_SSLv3
constants["OP_NO_TLSv1"] = SSL_OP_NO_TLSv1
constants["OP_CIPHER_SERVER_PREFERENCE"] = SSL_OP_CIPHER_SERVER_PREFERENCE
constants["OP_SINGLE_DH_USE"] = SSL_OP_SINGLE_DH_USE
constants["OP_SINGLE_ECDH_USE"] = SSL_OP_SINGLE_ECDH_USE
if SSL_OP_NO_COMPRESSION is not None:
    constants["OP_NO_COMPRESSION"] = SSL_OP_NO_COMPRESSION

constants["OPENSSL_VERSION_NUMBER"] = OPENSSL_VERSION_NUMBER
ver = OPENSSL_VERSION_NUMBER
ver, status = divmod(ver, 16)
ver, patch  = divmod(ver, 256)
ver, fix    = divmod(ver, 256)
ver, minor  = divmod(ver, 256)
ver, major  = divmod(ver, 256)
version_info = (major, minor, fix, patch, status)
constants["OPENSSL_VERSION_INFO"] = version_info
constants["_OPENSSL_API_VERSION"] = version_info
constants["OPENSSL_VERSION"] = SSLEAY_VERSION


def ssl_error(space, msg, errno=0, w_errtype=None, errcode=0):
    reason_str = None
    lib_str = None
    if errcode:
        err_lib = libssl_ERR_GET_LIB(errcode)
        err_reason = libssl_ERR_GET_REASON(errcode)
        reason_str = ERROR_CODES_TO_NAMES.get((err_lib, err_reason), None)
        lib_str = LIBRARY_CODES_TO_NAMES.get(err_lib, None)
        msg = rffi.charp2str(libssl_ERR_reason_error_string(errcode))
    if not msg:
        msg = "unknown error"
    if reason_str and lib_str:
        msg = "[%s: %s] %s" % (lib_str, reason_str, msg)
    elif lib_str:
        msg = "[%s] %s" % (lib_str, msg)

    w_exception_class = w_errtype or get_exception_class(space, 'w_sslerror')
    if errno or errcode:
        w_exception = space.call_function(w_exception_class,
                                          space.wrap(errno), space.wrap(msg))
    else:
        w_exception = space.call_function(w_exception_class, space.wrap(msg))
    space.setattr(w_exception, space.wrap("reason"),
                  space.wrap(reason_str) if reason_str else space.w_None)
    space.setattr(w_exception, space.wrap("library"),
                  space.wrap(lib_str) if lib_str else space.w_None)
    return OperationError(w_exception_class, w_exception)

class SSLNpnProtocols(object):

    def __init__(self, ctx, protos):
        self.protos = protos
        self.buf, self.bufflag = rffi.get_nonmovingbuffer(protos)
        NPN_STORAGE.set(rffi.cast(lltype.Unsigned, self.buf), self)

        # set both server and client callbacks, because the context
        # can be used to create both types of sockets
        libssl_SSL_CTX_set_next_protos_advertised_cb(
            ctx, self.advertiseNPN_cb, self.buf)
        libssl_SSL_CTX_set_next_proto_select_cb(
            ctx, self.selectNPN_cb, self.buf)

    def __del__(self):
        rffi.free_nonmovingbuffer(
            self.protos, self.buf, self.bufflag)

    @staticmethod
    def advertiseNPN_cb(s, data_ptr, len_ptr, args):
        npn = NPN_STORAGE.get(rffi.cast(lltype.Unsigned, args))
        if npn and npn.protos:
            data_ptr[0] = npn.buf
            len_ptr[0] = rffi.cast(rffi.UINT, len(npn.protos))
        else:
            data_ptr[0] = lltype.nullptr(rffi.CCHARP.TO)
            len_ptr[0] = rffi.cast(rffi.UINT, 0)

        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_OK)

    @staticmethod
    def selectNPN_cb(s, out_ptr, outlen_ptr, server, server_len, args):
        npn = NPN_STORAGE.get(rffi.cast(lltype.Unsigned, args))
        if npn and npn.protos:
            client = npn.buf
            client_len = len(npn.protos)
        else:
            client = lltype.nullptr(rffi.CCHARP.TO)
            client_len = 0

        libssl_SSL_select_next_proto(out_ptr, outlen_ptr,
                                     server, server_len,
                                     client, client_len)
        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_OK)


class SSLAlpnProtocols(object):

    def __init__(self, ctx, protos):
        self.protos = protos
        self.buf, self.bufflag = rffi.get_nonmovingbuffer(protos)
        ALPN_STORAGE.set(rffi.cast(lltype.Unsigned, self.buf), self)

        with rffi.scoped_str2charp(protos) as protos_buf:
            if libssl_SSL_CTX_set_alpn_protos(
                    ctx, rffi.cast(rffi.UCHARP, protos_buf), len(protos)):
                raise MemoryError
        libssl_SSL_CTX_set_alpn_select_cb(
            ctx, self.selectALPN_cb, self.buf)

    def __del__(self):
        rffi.free_nonmovingbuffer(
            self.protos, self.buf, self.bufflag)

    @staticmethod
    def selectALPN_cb(s, out_ptr, outlen_ptr, client, client_len, args):
        alpn = ALPN_STORAGE.get(rffi.cast(lltype.Unsigned, args))
        if alpn and alpn.protos:
            server = alpn.buf
            server_len = len(alpn.protos)
        else:
            server = lltype.nullptr(rffi.CCHARP.TO)
            server_len = 0

        ret = libssl_SSL_select_next_proto(out_ptr, outlen_ptr,
                                           server, server_len,
                                           client, client_len)
        if ret != OPENSSL_NPN_NEGOTIATED:
            return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_NOACK)
        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_OK)


NPN_STORAGE = RWeakValueDictionary(r_uint, SSLNpnProtocols)
ALPN_STORAGE = RWeakValueDictionary(r_uint, SSLAlpnProtocols)

SOCKET_STORAGE = RWeakValueDictionary(int, W_Root)


if HAVE_OPENSSL_RAND:
    # helper routines for seeding the SSL PRNG
    @unwrap_spec(string=str, entropy=float)
    def RAND_add(space, string, entropy):
        """RAND_add(string, entropy)


        Mix string into the OpenSSL PRNG state.  entropy (a float) is a lower
        bound on the entropy contained in string."""
        with rffi.scoped_nonmovingbuffer(string) as buf:
            libssl_RAND_add(buf, len(string), entropy)

    def RAND_status(space):
        """RAND_status() -> 0 or 1

        Returns 1 if the OpenSSL PRNG has been seeded with enough data
        and 0 if not.  It is necessary to seed the PRNG with RAND_add()
        on some platforms before using the ssl() function."""

        res = libssl_RAND_status()
        return space.wrap(res)

    if HAVE_OPENSSL_RAND_EGD:
        @unwrap_spec(path=str)
        def RAND_egd(space, path):
            """RAND_egd(path) -> bytes

            Queries the entropy gather daemon (EGD) on socket path.  Returns number
            of bytes read.  Raises socket.sslerror if connection to EGD fails or
            if it does provide enough data to seed PRNG."""
            with rffi.scoped_str2charp(path) as socket_path:
                bytes = libssl_RAND_egd(socket_path)
            if bytes == -1:
                raise ssl_error(space,
                                "EGD connection failed or EGD did not return "
                                "enough data to seed the PRNG")
            return space.wrap(bytes)
    else:
        # Dummy func for platforms missing RAND_egd(). Most likely LibreSSL.
        @unwrap_spec(path=str)
        def RAND_egd(space, path):
            raise ssl_error(space, "RAND_egd unavailable")


class _SSLSocket(W_Root):
    @staticmethod
    def descr_new(space, w_ctx, w_sock, socket_type,
                  w_hostname=None, w_ssl_sock=None):
        self = _SSLSocket()

        self.space = space
        self.w_ctx = w_ctx
        self.peer_cert = lltype.nullptr(X509.TO)
        self.shutdown_seen_zero = False
        self.handshake_done = False

        sock_fd = space.int_w(space.call_method(w_sock, "fileno"))
        self.ssl = libssl_SSL_new(w_ctx.ctx)  # new ssl struct

        self.register_finalizer(space)

        index = compute_unique_id(self)
        libssl_SSL_set_app_data(self.ssl, rffi.cast(rffi.VOIDP, index))
        SOCKET_STORAGE.set(index, self)

        libssl_SSL_set_fd(self.ssl, sock_fd)  # set the socket for SSL
        # The ACCEPT_MOVING_WRITE_BUFFER flag is necessary because the address
        # of a str object may be changed by the garbage collector.
        libssl_SSL_set_mode(
            self.ssl, SSL_MODE_AUTO_RETRY | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)

        if not space.is_none(w_hostname):
            if space.isinstance_w(w_hostname, space.w_unicode):
                w_hostname = space.call_method(w_hostname, "encode",
                                               space.wrap("idna"))
            libssl_SSL_set_tlsext_host_name(
                self.ssl, space.bytes_w(w_hostname))

        # If the socket is in non-blocking mode or timeout mode, set the BIO
        # to non-blocking mode (blocking is the default)
        w_timeout = space.call_method(w_sock, "gettimeout")
        has_timeout = not space.is_none(w_timeout)
        if has_timeout:
            # Set both the read and write BIO's to non-blocking mode
            libssl_BIO_set_nbio(libssl_SSL_get_rbio(self.ssl), 1)
            libssl_BIO_set_nbio(libssl_SSL_get_wbio(self.ssl), 1)

        if socket_type == PY_SSL_CLIENT:
            libssl_SSL_set_connect_state(self.ssl)
        else:
            libssl_SSL_set_accept_state(self.ssl)

        self.socket_type = socket_type
        self.w_socket = w_sock
        if w_ssl_sock:
            self.ssl_sock_weakref_w = rweakref.ref(w_ssl_sock)
        else:
            self.ssl_sock_weakref_w = None
        return self

    def _finalize_(self):
        peer_cert = self.peer_cert
        if peer_cert:
            self.peer_cert = lltype.nullptr(X509.TO)
            libssl_X509_free(peer_cert)
        ssl = self.ssl
        if ssl:
            self.ssl = lltype.nullptr(SSL.TO)
            libssl_SSL_free(ssl)

    @unwrap_spec(data='bufferstr')
    def write(self, space, data):
        """write(s) -> len

        Writes the string s into the SSL object.  Returns the number
        of bytes written."""
        self._refresh_nonblocking(space)

        sockstate = checkwait(space, self.w_socket, True)
        if sockstate == SOCKET_HAS_TIMED_OUT:
            raise ssl_error(space, "The write operation timed out")
        elif sockstate == SOCKET_HAS_BEEN_CLOSED:
            raise ssl_error(space, "Underlying socket has been closed.")
        elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT:
            raise ssl_error(space, "Underlying socket too large for select().")

        num_bytes = 0
        while True:
            err = 0

            num_bytes = libssl_SSL_write(self.ssl, data, len(data))
            err = libssl_SSL_get_error(self.ssl, num_bytes)

            if err == SSL_ERROR_WANT_READ:
                sockstate = checkwait(space, self.w_socket, False)
            elif err == SSL_ERROR_WANT_WRITE:
                sockstate = checkwait(space, self.w_socket, True)
            else:
                sockstate = SOCKET_OPERATION_OK

            if sockstate == SOCKET_HAS_TIMED_OUT:
                raise ssl_error(space, "The write operation timed out")
            elif sockstate == SOCKET_HAS_BEEN_CLOSED:
                raise ssl_error(space, "Underlying socket has been closed.")
            elif sockstate == SOCKET_IS_NONBLOCKING:
                break

            if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE:
                continue
            else:
                break

        if num_bytes > 0:
            return space.wrap(num_bytes)
        else:
            raise _ssl_seterror(space, self, num_bytes)

    def pending(self, space):
        """pending() -> count

        Returns the number of already decrypted bytes available for read,
        pending on the connection."""
        count = libssl_SSL_pending(self.ssl)
        if count < 0:
            raise _ssl_seterror(space, self, count)
        return space.wrap(count)

    @unwrap_spec(num_bytes=int)
    def read(self, space, num_bytes, w_buffer=None):
        """read([len]) -> string

        Read up to len bytes from the SSL socket."""
        count = libssl_SSL_pending(self.ssl)
        if not count:
            sockstate = checkwait(space, self.w_socket, False)
            if sockstate == SOCKET_HAS_TIMED_OUT:
                raise ssl_error(space, "The read operation timed out")
            elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT:
                raise ssl_error(space,
                                "Underlying socket too large for select().")
            elif sockstate == SOCKET_HAS_BEEN_CLOSED:
                if libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN:
                    return space.wrap('')
                raise ssl_error(space,
                                "Socket closed without SSL shutdown handshake")

        if w_buffer:
            rwbuffer = space.getarg_w('w*', w_buffer)
            num_bytes = min(num_bytes, rwbuffer.getlength())
        else:
            rwbuffer = None

        with rffi.scoped_alloc_buffer(num_bytes) as buf:
            while True:
                err = 0

                count = libssl_SSL_read(self.ssl, buf.raw, num_bytes)
                err = libssl_SSL_get_error(self.ssl, count)

                if err == SSL_ERROR_WANT_READ:
                    sockstate = checkwait(space, self.w_socket, False)
                elif err == SSL_ERROR_WANT_WRITE:
                    sockstate = checkwait(space, self.w_socket, True)
                elif (err == SSL_ERROR_ZERO_RETURN and
                        libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN):
                    if rwbuffer:
                        return space.wrap(0)
                    else:
                        return space.wrap("")
                else:
                    sockstate = SOCKET_OPERATION_OK

                if sockstate == SOCKET_HAS_TIMED_OUT:
                    raise ssl_error(space, "The read operation timed out")
                elif sockstate == SOCKET_IS_NONBLOCKING:
                    break

                if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE:
                    continue
                else:
                    break

            if count <= 0:
                raise _ssl_seterror(space, self, count)

            result = buf.str(count)

        if rwbuffer:
            rwbuffer.setslice(0, result)
            return space.wrap(count)
        else:
            return space.wrap(result)

    def _refresh_nonblocking(self, space):
        # just in case the blocking state of the socket has been changed
        w_timeout = space.call_method(self.w_socket, "gettimeout")
        nonblocking = not space.is_w(w_timeout, space.w_None)
        libssl_BIO_set_nbio(libssl_SSL_get_rbio(self.ssl), nonblocking)
        libssl_BIO_set_nbio(libssl_SSL_get_wbio(self.ssl), nonblocking)

    def do_handshake(self, space):
        self._refresh_nonblocking(space)

        # Actually negotiate SSL connection
        # XXX If SSL_do_handshake() returns 0, it's also a failure.
        while True:
            ret = libssl_SSL_do_handshake(self.ssl)
            err = libssl_SSL_get_error(self.ssl, ret)
            # XXX PyErr_CheckSignals()
            if err == SSL_ERROR_WANT_READ:
                sockstate = checkwait(space, self.w_socket, False)
            elif err == SSL_ERROR_WANT_WRITE:
                sockstate = checkwait(space, self.w_socket, True)
            else:
                sockstate = SOCKET_OPERATION_OK
            if sockstate == SOCKET_HAS_TIMED_OUT:
                raise ssl_error(space, "The handshake operation timed out")
            elif sockstate == SOCKET_HAS_BEEN_CLOSED:
                raise ssl_error(space, "Underlying socket has been closed.")
            elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT:
                raise ssl_error(space,
                                "Underlying socket too large for select().")
            elif sockstate == SOCKET_IS_NONBLOCKING:
                break

            if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE:
                continue
            else:
                break

        if ret <= 0:
            raise _ssl_seterror(space, self, ret)

        if self.peer_cert:
            libssl_X509_free(self.peer_cert)
        self.peer_cert = libssl_SSL_get_peer_certificate(self.ssl)
        self.handshake_done = True

    def shutdown(self, space):
        # Guard against closed socket
        w_fileno = space.call_method(self.w_socket, "fileno")
        if space.int_w(w_fileno) < 0:
            raise ssl_error(space, "Underlying socket has been closed")

        self._refresh_nonblocking(space)

        zeros = 0
        while True:
            # Disable read-ahead so that unwrap can work correctly.
            # Otherwise OpenSSL might read in too much data,
            # eating clear text data that happens to be
            # transmitted after the SSL shutdown.
            # Should be safe to call repeatedly everytime this
            # function is used and the shutdown_seen_zero != 0
            # condition is met.
            if self.shutdown_seen_zero:
                libssl_SSL_set_read_ahead(self.ssl, 0)
            ret = libssl_SSL_shutdown(self.ssl)

            # if err == 1, a secure shutdown with SSL_shutdown() is complete
            if ret > 0:
                break
            if ret == 0:
                # Don't loop endlessly; instead preserve legacy
                # behaviour of trying SSL_shutdown() only twice.
                # This looks necessary for OpenSSL < 0.9.8m
                zeros += 1
                if zeros > 1:
                    break
                # Shutdown was sent, now try receiving
                self.shutdown_seen_zero = True
                continue

            # Possibly retry shutdown until timeout or failure
            ssl_err = libssl_SSL_get_error(self.ssl, ret)
            if ssl_err == SSL_ERROR_WANT_READ:
                sockstate = checkwait(space, self.w_socket, False)
            elif ssl_err == SSL_ERROR_WANT_WRITE:
                sockstate = checkwait(space, self.w_socket, True)
            else:
                break

            if sockstate == SOCKET_HAS_TIMED_OUT:
                if ssl_err == SSL_ERROR_WANT_READ:
                    raise ssl_error(space, "The read operation timed out")
                else:
                    raise ssl_error(space, "The write operation timed out")
            elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT:
                raise ssl_error(space,
                                "Underlying socket too large for select().")
            elif sockstate != SOCKET_OPERATION_OK:
                # Retain the SSL error code
                break

        if ret < 0:
            raise _ssl_seterror(space, self, ret)

        return self.w_socket

    def cipher(self, space):
        if not self.ssl:
            return space.w_None
        current = libssl_SSL_get_current_cipher(self.ssl)
        if not current:
            return space.w_None

        name = libssl_SSL_CIPHER_get_name(current)
        w_name = space.wrap(rffi.charp2str(name)) if name else space.w_None

        proto = libssl_SSL_CIPHER_get_version(current)
        w_proto = space.wrap(rffi.charp2str(proto)) if proto else space.w_None

        bits = libssl_SSL_CIPHER_get_bits(current,
                                          lltype.nullptr(rffi.INTP.TO))
        w_bits = space.newint(bits)
        return space.newtuple([w_name, w_proto, w_bits])

    @unwrap_spec(der=bool)
    def peer_certificate(self, space, der=False):
        """peer_certificate([der=False]) -> certificate

        Returns the certificate for the peer.  If no certificate was
        provided, returns None.  If a certificate was provided, but not
        validated, returns an empty dictionary.  Otherwise returns a
        dict containing information about the peer certificate.

        If the optional argument is True, returns a DER-encoded copy of
        the peer certificate, or None if no certificate was provided.
        This will return the certificate even if it wasn't validated.
        """
        if not self.handshake_done:
            raise oefmt(space.w_ValueError, "hanshake not done yet")
        if not self.peer_cert:
            return space.w_None

        if der:
            # return cert in DER-encoded format
            return _certificate_to_der(space, self.peer_cert)
        else:
            verification = libssl_SSL_CTX_get_verify_mode(
                libssl_SSL_get_SSL_CTX(self.ssl))
            if not verification & SSL_VERIFY_PEER:
                return space.newdict()
            else:
                return _decode_certificate(space, self.peer_cert)

    def selected_npn_protocol(self, space):
        if not HAS_NPN:
            raise oefmt(space.w_NotImplementedError,
                        "The NPN extension requires OpenSSL 1.0.1 or later.")
        with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as out_ptr:
            with lltype.scoped_alloc(rffi.UINTP.TO, 1) as len_ptr:
                libssl_SSL_get0_next_proto_negotiated(self.ssl,
                                                      out_ptr, len_ptr)
                if out_ptr[0]:
                    return space.wrap(
                        rffi.charpsize2str(out_ptr[0], intmask(len_ptr[0])))

    def selected_alpn_protocol(self, space):
        if not HAS_ALPN:
            raise oefmt(space.w_NotImplementedError,
                        "The ALPN extension requires OpenSSL 1.0.2 or later.")
        with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as out_ptr:
            with lltype.scoped_alloc(rffi.UINTP.TO, 1) as len_ptr:
                libssl_SSL_get0_alpn_selected(self.ssl,
                                              out_ptr, len_ptr)
                if out_ptr[0]:
                    return space.wrap(
                        rffi.charpsize2str(out_ptr[0], intmask(len_ptr[0])))

    def compression_w(self, space):
        if not self.ssl:
            return space.w_None
        comp_method = libssl_SSL_get_current_compression(self.ssl)
        if not comp_method or intmask(comp_method[0].c_type) == NID_undef:
            return space.w_None
        short_name = libssl_OBJ_nid2sn(comp_method[0].c_type)
        if not short_name:
            return space.w_None
        return space.wrap(rffi.charp2str(short_name))

    def version_w(self, space):
        if not self.ssl:
            return space.w_None
        version = libssl_SSL_get_version(self.ssl)
        if not version:
            return space.w_None
        return space.wrap(rffi.charp2str(version))

    def tls_unique_cb_w(self, space):
        """Returns the 'tls-unique' channel binding data, as defined by RFC 5929.
        If the TLS handshake is not yet complete, None is returned"""

        # In case of 'tls-unique' it will be 12 bytes for TLS, 36
        # bytes for older SSL, but let's be safe
        CB_MAXLEN = 128

        with lltype.scoped_alloc(rffi.CCHARP.TO, CB_MAXLEN) as buf:
            if (libssl_SSL_session_reused(self.ssl) ^
                (self.socket_type == PY_SSL_CLIENT)):
                # if session is resumed XOR we are the client
                length = libssl_SSL_get_finished(self.ssl, buf, CB_MAXLEN)
            else:
                # if a new session XOR we are the server
                length = libssl_SSL_get_peer_finished(self.ssl, buf, CB_MAXLEN)

            if length > 0:
                return space.newbytes(rffi.charpsize2str(buf, intmask(length)))

    def descr_get_context(self, space):
        return self.w_ctx

    def descr_set_context(self, space, w_ctx):
        ctx = space.interp_w(_SSLContext, w_ctx)
        if not HAS_SNI:
            raise oefmt(space.w_NotImplementedError,
                        "setting a socket's context "
                        "is not supported by your OpenSSL library")
        self.w_ctx = w_ctx
        libssl_SSL_set_SSL_CTX(self.ssl, ctx.ctx)


_SSLSocket.typedef = TypeDef(
    "_ssl._SSLSocket",

    do_handshake=interp2app(_SSLSocket.do_handshake),
    write=interp2app(_SSLSocket.write),
    read=interp2app(_SSLSocket.read),
    pending=interp2app(_SSLSocket.pending),
    peer_certificate=interp2app(_SSLSocket.peer_certificate),
    cipher=interp2app(_SSLSocket.cipher),
    shutdown=interp2app(_SSLSocket.shutdown),
    selected_npn_protocol = interp2app(_SSLSocket.selected_npn_protocol),
    selected_alpn_protocol = interp2app(_SSLSocket.selected_alpn_protocol),
    compression = interp2app(_SSLSocket.compression_w),
    version = interp2app(_SSLSocket.version_w),
    tls_unique_cb = interp2app(_SSLSocket.tls_unique_cb_w),
    context=GetSetProperty(_SSLSocket.descr_get_context,
                           _SSLSocket.descr_set_context),
)

def _certificate_to_der(space, certificate):
    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
        buf_ptr[0] = lltype.nullptr(rffi.CCHARP.TO)
        length = libssl_i2d_X509(certificate, buf_ptr)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        try:
            return space.newbytes(rffi.charpsize2str(buf_ptr[0], length))
        finally:
            libssl_OPENSSL_free(buf_ptr[0])

def _decode_certificate(space, certificate):
    w_retval = space.newdict()

    w_peer = _create_tuple_for_X509_NAME(
        space, libssl_X509_get_subject_name(certificate))
    space.setitem(w_retval, space.wrap("subject"), w_peer)

    w_issuer = _create_tuple_for_X509_NAME(
        space, libssl_X509_get_issuer_name(certificate))
    space.setitem(w_retval, space.wrap("issuer"), w_issuer)

    space.setitem(w_retval, space.wrap("version"),
                  space.wrap(libssl_X509_get_version(certificate) + 1))

    biobuf = libssl_BIO_new(libssl_BIO_s_mem())
    try:

        libssl_BIO_reset(biobuf)
        serialNumber = libssl_X509_get_serialNumber(certificate)
        libssl_i2a_ASN1_INTEGER(biobuf, serialNumber)
        # should not exceed 20 octets, 160 bits, so buf is big enough
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)

            w_serial = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("serialNumber"), w_serial)

        libssl_BIO_reset(biobuf)
        notBefore = libssl_X509_get_notBefore(certificate)
        libssl_ASN1_TIME_print(biobuf, notBefore)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notBefore"), w_date)

        libssl_BIO_reset(biobuf)
        notAfter = libssl_X509_get_notAfter(certificate)
        libssl_ASN1_TIME_print(biobuf, notAfter)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notAfter"), w_date)
    finally:
        libssl_BIO_free(biobuf)

    # Now look for subjectAltName
    w_alt_names = _get_peer_alt_names(space, certificate)
    if w_alt_names is not space.w_None:
        space.setitem(w_retval, space.wrap("subjectAltName"), w_alt_names)

    # Authority Information Access: OCSP URIs
    w_ocsp = _get_aia_uri(space, certificate, NID_ad_OCSP)
    if not space.is_none(w_ocsp):
        space.setitem(w_retval, space.wrap("OCSP"), w_ocsp)
    w_issuers = _get_aia_uri(space, certificate, NID_ad_ca_issuers)
    if not space.is_none(w_issuers):
        space.setitem(w_retval, space.wrap("caIssuers"), w_issuers)

    # CDP (CRL distribution points)
    w_cdp = _get_crl_dp(space, certificate)
    if not space.is_none(w_cdp):
        space.setitem(w_retval, space.wrap("crlDistributionPoints"), w_cdp)

    return w_retval


def _create_tuple_for_X509_NAME(space, xname):
    entry_count = libssl_X509_NAME_entry_count(xname)
    dn_w = []
    rdn_w = []
    rdn_level = -1
    for index in range(entry_count):
        entry = libssl_X509_NAME_get_entry(xname, index)
        # check to see if we've gotten to a new RDN
        entry_level = intmask(entry[0].c_set)
        if rdn_level >= 0:
            if rdn_level != entry_level:
                # yes, new RDN
                # add old RDN to DN
                dn_w.append(space.newtuple(list(rdn_w)))
                rdn_w = []
        rdn_level = entry_level

        # Now add this attribute to the current RDN
        name = libssl_X509_NAME_ENTRY_get_object(entry)
        value = libssl_X509_NAME_ENTRY_get_data(entry)
        attr = _create_tuple_for_attribute(space, name, value)
        rdn_w.append(attr)

    # Now, there is typically a dangling RDN
    if rdn_w:
        dn_w.append(space.newtuple(list(rdn_w)))
    return space.newtuple(list(dn_w))


def _get_peer_alt_names(space, 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.

    if not certificate:
        return space.w_None

    # get a memory buffer
    biobuf = libssl_BIO_new(libssl_BIO_s_mem())

    try:
        alt_names_w = []
        i = -1
        while True:
            i = libssl_X509_get_ext_by_NID(
                certificate, NID_subject_alt_name, i)
            if i < 0:
                break

            # now decode the altName
            ext = libssl_X509_get_ext(certificate, i)
            method = libssl_X509V3_EXT_get(ext)
            if not method:
                raise ssl_error(space,
                                "No method for internalizing subjectAltName!'")

            with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as p_ptr:
                p_ptr[0] = ext[0].c_value.c_data
                length = intmask(ext[0].c_value.c_length)
                null = lltype.nullptr(rffi.VOIDP.TO)
                if method[0].c_it:
                    names = rffi.cast(GENERAL_NAMES, libssl_ASN1_item_d2i(
                        null, p_ptr, length,
                        libssl_ASN1_ITEM_ptr(method[0].c_it)))
                else:
                    names = rffi.cast(GENERAL_NAMES, method[0].c_d2i(
                        null, p_ptr, length))

            try:
                for j in range(libssl_sk_GENERAL_NAME_num(names)):
                    # Get a rendering of each name in the set of names

                    name = libssl_sk_GENERAL_NAME_value(names, j)
                    gntype = intmask(name.c_type)
                    if gntype == GEN_DIRNAME:
                        # we special-case DirName as a tuple of tuples of
                        # attributes
                        dirname = libssl_pypy_GENERAL_NAME_dirn(name)
                        w_t = space.newtuple([
                            space.wrap("DirName"),
                            _create_tuple_for_X509_NAME(space, dirname)
                            ])
                    elif gntype in (GEN_EMAIL, GEN_DNS, GEN_URI):
                        # GENERAL_NAME_print() doesn't handle NULL bytes in
                        # ASN1_string correctly, CVE-2013-4238
                        if gntype == GEN_EMAIL:
                            v = space.wrap("email")
                        elif gntype == GEN_DNS:
                            v = space.wrap("DNS")
                        elif gntype == GEN_URI:
                            v = space.wrap("URI")
                        else:
                            assert False
                        as_ = libssl_pypy_GENERAL_NAME_dirn(name)
                        as_ = rffi.cast(ASN1_STRING, as_)
                        buf = libssl_ASN1_STRING_data(as_)
                        length = libssl_ASN1_STRING_length(as_)
                        w_t = space.newtuple([
                            v, space.wrap(rffi.charpsize2str(buf, length))])
                    else:
                        # for everything else, we use the OpenSSL print form
                        if gntype not in (GEN_OTHERNAME, GEN_X400, GEN_EDIPARTY,
                                          GEN_IPADD, GEN_RID):
                            space.warn(space.wrap("Unknown general name type"),
                                       space.w_RuntimeWarning)
                        libssl_BIO_reset(biobuf)
                        libssl_GENERAL_NAME_print(biobuf, name)
                        with lltype.scoped_alloc(rffi.CCHARP.TO, 2048) as buf:
                            length = libssl_BIO_gets(biobuf, buf, 2047)
                            if length < 0:
                                raise _ssl_seterror(space, None, 0)

                            v = rffi.charpsize2str(buf, length)
                        v1, v2 = v.split(':', 1)
                        w_t = space.newtuple([space.wrap(v1),
                                              space.wrap(v2)])

                    alt_names_w.append(w_t)
            finally:
                libssl_pypy_GENERAL_NAME_pop_free(names)
    finally:
        libssl_BIO_free(biobuf)

    if alt_names_w:
        return space.newtuple(list(alt_names_w))
    else:
        return space.w_None


def _create_tuple_for_attribute(space, name, value):
    with lltype.scoped_alloc(rffi.CCHARP.TO, X509_NAME_MAXLEN) as buf:
        length = libssl_OBJ_obj2txt(buf, X509_NAME_MAXLEN, name, 0)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_name = space.wrap(rffi.charpsize2str(buf, length))

    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
        length = libssl_ASN1_STRING_to_UTF8(buf_ptr, value)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        try:
            w_value = space.newbytes(rffi.charpsize2str(buf_ptr[0], length))
            w_value = space.call_method(w_value, "decode", space.wrap("utf-8"))
        finally:
            libssl_OPENSSL_free(buf_ptr[0])

    return space.newtuple([w_name, w_value])


def _get_aia_uri(space, certificate, nid):
    info = rffi.cast(AUTHORITY_INFO_ACCESS, libssl_X509_get_ext_d2i(
        certificate, NID_info_access, None, None))
    try:
        if not info or libssl_sk_ACCESS_DESCRIPTION_num(info) == 0:
            return

        result_w = []
        for i in range(libssl_sk_ACCESS_DESCRIPTION_num(info)):
            ad = libssl_sk_ACCESS_DESCRIPTION_value(info, i)
            if libssl_OBJ_obj2nid(ad[0].c_method) != nid:
                continue

            name = ad[0].c_location
            gntype = intmask(name.c_type)
            if gntype != GEN_URI:
                continue
            uri = libssl_pypy_GENERAL_NAME_uri(name)
            length = intmask(uri.c_length)
            s_uri = rffi.charpsize2str(uri.c_data, length)
            result_w.append(space.wrap(s_uri))
        return space.newtuple(result_w[:])
    finally:
        libssl_AUTHORITY_INFO_ACCESS_free(info)

def _get_crl_dp(space, certificate):
    if OPENSSL_VERSION_NUMBER >= 0x10001000:
        # Calls x509v3_cache_extensions and sets up crldp
        libssl_X509_check_ca(certificate)
        dps = certificate[0].c_crldp
    else:
        dps = rffi.cast(stack_st_DIST_POINT, libssl_X509_get_ext_d2i(
            certificate, NID_crl_distribution_points, None, None))
    if not dps:
        return None

    try:
        cdp_w = []
        for i in range(libssl_sk_DIST_POINT_num(dps)):
            dp = libssl_sk_DIST_POINT_value(dps, i)
            gns = libssl_pypy_DIST_POINT_fullname(dp)

            for j in range(libssl_sk_GENERAL_NAME_num(gns)):
                name = libssl_sk_GENERAL_NAME_value(gns, j)
                gntype = intmask(name.c_type)
                if gntype != GEN_URI:
                    continue
                uri = libssl_pypy_GENERAL_NAME_uri(name)
                length = intmask(uri.c_length)
                s_uri = rffi.charpsize2str(uri.c_data, length)
                cdp_w.append(space.wrap(s_uri))
    finally:
        if OPENSSL_VERSION_NUMBER < 0x10001000:
            libssl_sk_DIST_POINT_free(dps)
    return space.newtuple(cdp_w[:])

def checkwait(space, w_sock, writing):
    """If the socket has a timeout, do a select()/poll() on the socket.
    The argument writing indicates the direction.
    Returns one of the possibilities in the timeout_state enum (above)."""

    w_timeout = space.call_method(w_sock, "gettimeout")
    if space.is_w(w_timeout, space.w_None):
        return SOCKET_IS_BLOCKING
    elif space.float_w(w_timeout) == 0.0:
        return SOCKET_IS_NONBLOCKING
    sock_timeout = space.float_w(w_timeout)

    sock_fd = space.int_w(space.call_method(w_sock, "fileno"))

    # guard against closed socket
    if sock_fd < 0:
        return SOCKET_HAS_BEEN_CLOSED

    # see if the socket is ready

    # Prefer poll, if available, since you can poll() any fd
    # which can't be done with select().
    if HAVE_RPOLL:
        if writing:
            fddict = {sock_fd: rpoll.POLLOUT}
        else:
            fddict = {sock_fd: rpoll.POLLIN}

        # socket's timeout is in seconds, poll's timeout in ms
        timeout = int(sock_timeout * 1000 + 0.5)
        try:
            ready = rpoll.poll(fddict, timeout)
        except rpoll.PollError as e:
            message = e.get_msg()
            raise ssl_error(space, message, e.errno)
    else:
        if MAX_FD_SIZE is not None and sock_fd >= MAX_FD_SIZE:
            return SOCKET_TOO_LARGE_FOR_SELECT

        try:
            if writing:
                r, w, e = rpoll.select([], [sock_fd], [], sock_timeout)
                ready = w
            else:
                r, w, e = rpoll.select([sock_fd], [], [], sock_timeout)
                ready = r
        except rpoll.SelectError as e:
            message = e.get_msg()
            raise ssl_error(space, message, e.errno)
    if ready:
        return SOCKET_OPERATION_OK
    else:
        return SOCKET_HAS_TIMED_OUT


def _ssl_seterror(space, ss, ret):
    assert ret <= 0

    errcode = libssl_ERR_peek_last_error()

    if ss is None:
        return ssl_error(space, None, errcode=errcode)
    elif ss.ssl:
        err = libssl_SSL_get_error(ss.ssl, ret)
    else:
        err = SSL_ERROR_SSL
    w_errtype = None
    errstr = ""
    errval = 0

    if err == SSL_ERROR_ZERO_RETURN:
        w_errtype = get_exception_class(space, 'w_sslzeroreturnerror')
        errstr = "TLS/SSL connection has been closed"
        errval = PY_SSL_ERROR_ZERO_RETURN
    elif err == SSL_ERROR_WANT_READ:
        w_errtype = get_exception_class(space, 'w_sslwantreaderror')
        errstr = "The operation did not complete (read)"
        errval = PY_SSL_ERROR_WANT_READ
    elif err == SSL_ERROR_WANT_WRITE:
        w_errtype = get_exception_class(space, 'w_sslwantwriteerror')
        errstr = "The operation did not complete (write)"
        errval = PY_SSL_ERROR_WANT_WRITE
    elif err == SSL_ERROR_WANT_X509_LOOKUP:
        errstr = "The operation did not complete (X509 lookup)"
        errval = PY_SSL_ERROR_WANT_X509_LOOKUP
    elif err == SSL_ERROR_WANT_CONNECT:
        errstr = "The operation did not complete (connect)"
        errval = PY_SSL_ERROR_WANT_CONNECT
    elif err == SSL_ERROR_SYSCALL:
        e = libssl_ERR_get_error()
        if e == 0:
            if ret == 0 or space.is_w(ss.w_socket, space.w_None):
                errstr = "EOF occurred in violation of protocol"
                errval = PY_SSL_ERROR_EOF
            elif ret == -1:
                # the underlying BIO reported an I/0 error
                error = rsocket.last_error()
                return interp_socket.converted_error(space, error)
            else:
                w_errtype = get_exception_class(space, 'w_sslsyscallerror')
                errstr = "Some I/O error occurred"
                errval = PY_SSL_ERROR_SYSCALL
        else:
            errstr = rffi.charp2str(libssl_ERR_error_string(e, None))
            errval = PY_SSL_ERROR_SYSCALL
    elif err == SSL_ERROR_SSL:
        errval = PY_SSL_ERROR_SSL
        if errcode != 0:
            errstr = rffi.charp2str(libssl_ERR_error_string(errcode, None))
        else:
            errstr = "A failure in the SSL library occurred"
    else:
        errstr = "Invalid error code"
        errval = PY_SSL_ERROR_INVALID_ERROR_CODE

    return ssl_error(space, errstr, errval, w_errtype=w_errtype,
                     errcode=errcode)

def SSLError_descr_str(space, w_exc):
    w_strerror = space.getattr(w_exc, space.wrap("strerror"))
    if not space.is_none(w_strerror):
        return w_strerror
    return space.str(space.getattr(w_exc, space.wrap("args")))


class Cache:
    def __init__(self, space):
        w_socketerror = interp_socket.get_error(space, "error")
        self.w_sslerror = space.new_exception_class(
            "_ssl.SSLError", w_socketerror)
        space.setattr(self.w_sslerror, space.wrap('__str__'),
                      space.wrap(interp2app(SSLError_descr_str)))
        self.w_sslzeroreturnerror = space.new_exception_class(
            "_ssl.SSLZeroReturnError", self.w_sslerror)
        self.w_sslwantreaderror = space.new_exception_class(
            "_ssl.SSLWantReadError", self.w_sslerror)
        self.w_sslwantwriteerror = space.new_exception_class(
            "_ssl.SSLWantWriteError", self.w_sslerror)
        self.w_sslsyscallerror = space.new_exception_class(
            "_ssl.SSLSyscallError", self.w_sslerror)
        self.w_ssleoferror = space.new_exception_class(
            "_ssl.SSLEOFError", self.w_sslerror)


@specialize.memo()
def get_exception_class(space, name):
    return getattr(space.fromcache(Cache), name)


@unwrap_spec(filename=str)
def _test_decode_cert(space, filename):
    cert = libssl_BIO_new(libssl_BIO_s_file())
    if not cert:
        raise ssl_error(space, "Can't malloc memory to read file")

    try:
        if libssl_BIO_read_filename(cert, filename) <= 0:
            raise ssl_error(space, "Can't open file")

        x = libssl_PEM_read_bio_X509_AUX(cert, None, None, None)
        if not x:
            raise ssl_error(space, "Error decoding PEM-encoded file")

        try:
            return _decode_certificate(space, x)
        finally:
            libssl_X509_free(x)
    finally:
        libssl_BIO_free(cert)


# Data structure for the password callbacks
class PasswordInfo(object):
    w_callable = None
    password = None
    operationerror = None
PWINFO_STORAGE = {}

def _password_callback(buf, size, rwflag, userdata):
    index = rffi.cast(lltype.Signed, userdata)
    pw_info = PWINFO_STORAGE.get(index, None)
    if not pw_info:
        return rffi.cast(rffi.INT, -1)
    space = pw_info.space
    password = ""
    if pw_info.w_callable:
        try:
            w_result = space.call_function(pw_info.w_callable)
            try:
                password = pw_info.space.bufferstr_w(w_result)
            except OperationError as e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(space.w_TypeError,
                            "password callback must return a string")
        except OperationError as e:
            pw_info.operationerror = e
            return rffi.cast(rffi.INT, -1)
    else:
        password = pw_info.password
    size = widen(size)
    if len(password) > size:
        pw_info.operationerror = oefmt(
            space.w_ValueError,
            "password cannot be longer than %d bytes", size)
        return rffi.cast(rffi.INT, -1)
    for i, c in enumerate(password):
        buf[i] = c
    return rffi.cast(rffi.INT, len(password))

class ServernameCallback(object):
    w_ctx = None
    space = None
SERVERNAME_CALLBACKS = RWeakValueDictionary(int, ServernameCallback)

def _servername_callback(ssl, ad, arg):
    struct = SERVERNAME_CALLBACKS.get(rffi.cast(lltype.Signed, arg))
    w_ctx = struct.w_ctx
    space = struct.space
    w_callback = struct.w_set_hostname
    if not w_ctx.servername_callback:
        # Possible race condition.
        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_OK)
    # The high-level ssl.SSLSocket object
    index = rffi.cast(lltype.Signed, libssl_SSL_get_app_data(ssl))
    w_ssl = SOCKET_STORAGE.get(index)
    assert isinstance(w_ssl, _SSLSocket)
    if w_ssl.ssl_sock_weakref_w is not None:
        w_ssl_socket = w_ssl.ssl_sock_weakref_w()
    else:
        w_ssl_socket = space.w_None
    if space.is_none(w_ssl_socket):
        ad[0] = rffi.cast(rffi.INT, SSL_AD_INTERNAL_ERROR)
        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_ALERT_FATAL)

    servername = libssl_SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)
    try:
        if not servername:
            w_result = space.call_function(w_callback,
                                           w_ssl_socket, space.w_None, w_ctx)

        else:
            w_servername = space.newbytes(rffi.charp2str(servername))
            try:
                w_servername_idna = space.call_method(
                    w_servername, 'decode', space.wrap('idna'))
            except OperationError as e:
                e.write_unraisable(space, "undecodable server name")
                ad[0] = rffi.cast(rffi.INT, SSL_AD_INTERNAL_ERROR)
                return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_ALERT_FATAL)

            w_result = space.call_function(w_callback,
                                           w_ssl_socket,
                                           w_servername_idna, w_ctx)
    except OperationError as e:
        e.write_unraisable(space, "in servername callback")
        ad[0] = rffi.cast(rffi.INT, SSL_AD_HANDSHAKE_FAILURE)
        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_ALERT_FATAL)

    if space.is_none(w_result):
        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_OK)
    else:
        try:
            ad[0] = rffi.cast(rffi.INT, space.int_w(w_result))
        except OperationError as e:
            e.write_unraisable(space, "servername callback result")
            ad[0] = rffi.cast(rffi.INT, SSL_AD_INTERNAL_ERROR)
        return rffi.cast(rffi.INT, SSL_TLSEXT_ERR_ALERT_FATAL)


class _SSLContext(W_Root):
    @staticmethod
    @unwrap_spec(protocol=int)
    def descr_new(space, w_subtype, protocol):
        if protocol == PY_SSL_VERSION_TLS1:
            method = libssl_TLSv1_method()
        elif protocol == PY_SSL_VERSION_SSL3 and not OPENSSL_NO_SSL3:
            method = libssl_SSLv3_method()
        elif protocol == PY_SSL_VERSION_SSL2 and not OPENSSL_NO_SSL2:
            method = libssl_SSLv2_method()
        elif protocol == PY_SSL_VERSION_SSL23:
            method = libssl_SSLv23_method()
        elif protocol == PY_SSL_VERSION_TLS1_1 and HAVE_TLSv1_2:
            method = libssl_TLSv1_1_method()
        elif protocol == PY_SSL_VERSION_TLS1_2 and HAVE_TLSv1_2:
            method = libssl_TLSv1_2_method()
        else:
            raise oefmt(space.w_ValueError, "invalid protocol version")
        ctx = libssl_SSL_CTX_new(method)
        if not ctx:
            raise ssl_error(space, "failed to allocate SSL context")

        rgc.add_memory_pressure(10 * 1024 * 1024)
        self = space.allocate_instance(_SSLContext, w_subtype)
        self.ctx = ctx
        self.check_hostname = False
        self.register_finalizer(space)
        options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
        if protocol != PY_SSL_VERSION_SSL2:
            options |= SSL_OP_NO_SSLv2
        libssl_SSL_CTX_set_options(ctx, options)

        if not OPENSSL_NO_ECDH:
            # Allow automatic ECDH curve selection (on
            # OpenSSL 1.0.2+), or use prime256v1 by default.
            # This is Apache mod_ssl's initialization
            # policy, so we should be safe.
            if libssl_SSL_CTX_set_ecdh_auto:
                libssl_SSL_CTX_set_ecdh_auto(self.ctx, 1)
            else:
                key = libssl_EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)
                if not key:
                    raise _ssl_seterror(space, None, 0)
                try:
                    libssl_SSL_CTX_set_tmp_ecdh(self.ctx, key)
                finally:
                    libssl_EC_KEY_free(key)

        return self

    def _finalize_(self):
        ctx = self.ctx
        if ctx:
            self.ctx = lltype.nullptr(SSL_CTX.TO)
            libssl_SSL_CTX_free(ctx)

    @unwrap_spec(server_side=int)
    def descr_wrap_socket(self, space, w_sock, server_side, w_server_hostname=None, w_ssl_sock=None):
        return _SSLSocket.descr_new(space, self, w_sock, server_side, w_server_hostname, w_ssl_sock)

    @unwrap_spec(cipherlist=str)
    def descr_set_ciphers(self, space, cipherlist):
        ret = libssl_SSL_CTX_set_cipher_list(self.ctx, cipherlist)
        if ret == 0:
            libssl_ERR_clear_error()
            raise ssl_error(space, "No cipher can be selected.")

    def session_stats_w(self, space):
        w_stats = space.newdict()
        for name, ssl_func in SSL_CTX_STATS:
            w_value = space.wrap(ssl_func(self.ctx))
            space.setitem_str(w_stats, name, w_value)
        return w_stats

    def descr_set_default_verify_paths(self, space):
        if not libssl_SSL_CTX_set_default_verify_paths(self.ctx):
            raise ssl_error(space, "")

    def descr_get_options(self, space):
        return space.newlong(libssl_SSL_CTX_get_options(self.ctx))

    def descr_set_options(self, space, w_new_opts):
        new_opts = space.int_w(w_new_opts)
        opts = libssl_SSL_CTX_get_options(self.ctx)
        clear = opts & ~new_opts
        set = ~opts & new_opts
        if clear:
            if HAVE_SSL_CTX_CLEAR_OPTIONS:
                libssl_SSL_CTX_clear_options(self.ctx, clear)
            else:
                raise oefmt(space.w_ValueError,
                            "can't clear options before OpenSSL 0.9.8m")
        if set:
            libssl_SSL_CTX_set_options(self.ctx, set)

    def descr_get_verify_mode(self, space):
        mode = libssl_SSL_CTX_get_verify_mode(self.ctx)
        if mode == SSL_VERIFY_NONE:
            return space.newlong(PY_SSL_CERT_NONE)
        elif mode == SSL_VERIFY_PEER:
            return space.newlong(PY_SSL_CERT_OPTIONAL)
        elif mode == SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT:
            return space.newlong(PY_SSL_CERT_REQUIRED)
        raise ssl_error(space, "invalid return value from SSL_CTX_get_verify_mode")

    def descr_set_verify_mode(self, space, w_mode):
        n = space.int_w(w_mode)
        if n == PY_SSL_CERT_NONE:
            mode = SSL_VERIFY_NONE
        elif n == PY_SSL_CERT_OPTIONAL:
            mode = SSL_VERIFY_PEER
        elif n == PY_SSL_CERT_REQUIRED:
            mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT
        else:
            raise oefmt(space.w_ValueError,
                        "invalid value for verify_mode")
        if mode == SSL_VERIFY_NONE and self.check_hostname:
            raise oefmt(space.w_ValueError,
                        "Cannot set verify_mode to CERT_NONE when "
                        "check_hostname is enabled.")
        libssl_SSL_CTX_set_verify(self.ctx, mode, None)

    def descr_get_verify_flags(self, space):
        store = libssl_SSL_CTX_get_cert_store(self.ctx)
        flags = libssl_X509_VERIFY_PARAM_get_flags(store[0].c_param)
        return space.wrap(flags)

    def descr_set_verify_flags(self, space, w_obj):
        new_flags = space.int_w(w_obj)
        store = libssl_SSL_CTX_get_cert_store(self.ctx)
        flags = libssl_X509_VERIFY_PARAM_get_flags(store[0].c_param)
        flags_clear = flags & ~new_flags
        flags_set = ~flags & new_flags
        if flags_clear and not libssl_X509_VERIFY_PARAM_clear_flags(
                store[0].c_param, flags_clear):
            raise _ssl_seterror(space, None, 0)
        if flags_set and not libssl_X509_VERIFY_PARAM_set_flags(
                store[0].c_param, flags_set):
            raise _ssl_seterror(space, None, 0)

    def descr_get_check_hostname(self, space):
        return space.newbool(self.check_hostname)

    def descr_set_check_hostname(self, space, w_obj):
        check_hostname = space.is_true(w_obj)
        if check_hostname and libssl_SSL_CTX_get_verify_mode(self.ctx) == SSL_VERIFY_NONE:
            raise oefmt(space.w_ValueError,
                        "check_hostname needs a SSL context with either "
                        "CERT_OPTIONAL or CERT_REQUIRED")
        self.check_hostname = check_hostname

    def load_cert_chain_w(self, space, w_certfile, w_keyfile=None,
                          w_password=None):
        if space.is_none(w_certfile):
            certfile = None
        else:
            certfile = space.str_w(w_certfile)
        if space.is_none(w_keyfile):
            keyfile = certfile
        else:
            keyfile = space.str_w(w_keyfile)
        pw_info = PasswordInfo()
        pw_info.space = space
        index = -1
        if not space.is_none(w_password):
            index = rthread.get_ident()
            PWINFO_STORAGE[index] = pw_info

            if space.is_true(space.callable(w_password)):
                pw_info.w_callable = w_password
            else:
                try:
                    pw_info.password = space.bufferstr_w(w_password)
                except OperationError as e:
                    if not e.match(space, space.w_TypeError):
                        raise
                    raise oefmt(space.w_TypeError,
                                "password should be a string or callable")

            libssl_SSL_CTX_set_default_passwd_cb(
                self.ctx, _password_callback)
            libssl_SSL_CTX_set_default_passwd_cb_userdata(
                self.ctx, rffi.cast(rffi.VOIDP, index))

        try:
            ret = libssl_SSL_CTX_use_certificate_chain_file(self.ctx, certfile)
            if ret != 1:
                if pw_info.operationerror:
                    libssl_ERR_clear_error()
                    raise pw_info.operationerror
                errno = get_saved_errno()
                if errno:
                    libssl_ERR_clear_error()
                    raise wrap_oserror(space, OSError(errno, ''),
                                       exception_name = 'w_IOError')
                else:
                    raise _ssl_seterror(space, None, -1)

            ret = libssl_SSL_CTX_use_PrivateKey_file(self.ctx, keyfile,
                                                     SSL_FILETYPE_PEM)
            if ret != 1:
                if pw_info.operationerror:
                    libssl_ERR_clear_error()
                    raise pw_info.operationerror
                errno = get_saved_errno()
                if errno:
                    libssl_ERR_clear_error()
                    raise wrap_oserror(space, OSError(errno, ''),
                                       exception_name = 'w_IOError')
                else:
                    raise _ssl_seterror(space, None, -1)

            ret = libssl_SSL_CTX_check_private_key(self.ctx)
            if ret != 1:
                raise _ssl_seterror(space, None, -1)
        finally:
            if index >= 0:
                del PWINFO_STORAGE[index]
            libssl_SSL_CTX_set_default_passwd_cb(
                self.ctx, lltype.nullptr(pem_password_cb.TO))
            libssl_SSL_CTX_set_default_passwd_cb_userdata(
                self.ctx, None)

    @unwrap_spec(filepath=str)
    def load_dh_params_w(self, space, filepath):
        bio = libssl_BIO_new_file(filepath, "r")
        if not bio:
            errno = get_saved_errno()
            libssl_ERR_clear_error()
            raise wrap_oserror(space, OSError(errno, ''),
                               exception_name = 'w_IOError')
        try:
            dh = libssl_PEM_read_bio_DHparams(bio, None, None, None)
        finally:
            libssl_BIO_free(bio)
        if not dh:
            errno = get_saved_errno()
            if errno != 0:
                libssl_ERR_clear_error()
                raise wrap_oserror(space, OSError(errno, ''))
            else:
                raise _ssl_seterror(space, None, 0)
        try:
            if libssl_SSL_CTX_set_tmp_dh(self.ctx, dh) == 0:
                raise _ssl_seterror(space, None, 0)
        finally:
            libssl_DH_free(dh)

    def load_verify_locations_w(self, space, w_cafile=None, w_capath=None,
                                w_cadata=None):
        if space.is_none(w_cafile):
            cafile = None
        else:
            cafile = space.str_w(w_cafile)
        if space.is_none(w_capath):
            capath = None
        else:
            capath = space.str_w(w_capath)
        if space.is_none(w_cadata):
            cadata = None
            ca_file_type = -1
        else:
            if not space.isinstance_w(w_cadata, space.w_unicode):
                ca_file_type = SSL_FILETYPE_ASN1
                cadata = space.bufferstr_w(w_cadata)
            else:
                ca_file_type = SSL_FILETYPE_PEM
                try:
                    cadata = space.unicode_w(w_cadata).encode('ascii')
                except UnicodeEncodeError:
                    raise oefmt(space.w_TypeError,
                                "cadata should be a ASCII string or a "
                                "bytes-like object")
        if cafile is None and capath is None and cadata is None:
            raise oefmt(space.w_TypeError,
                        "cafile and capath cannot be both omitted")
        # load from cadata
        if cadata is not None:
            with rffi.scoped_nonmovingbuffer(cadata) as buf:
                self._add_ca_certs(space, buf, len(cadata), ca_file_type)

        # load cafile or capath
        if cafile is not None or capath is not None:
            ret = libssl_SSL_CTX_load_verify_locations(
                self.ctx, cafile, capath)
            if ret != 1:
                errno = get_saved_errno()
                if errno:
                    libssl_ERR_clear_error()
                    raise wrap_oserror(space, OSError(errno, ''),
                                       exception_name = 'w_IOError')
                else:
                    raise _ssl_seterror(space, None, -1)

    def _add_ca_certs(self, space, data, size, ca_file_type):
        biobuf = libssl_BIO_new_mem_buf(data, size)
        if not biobuf:
            raise ssl_error(space, "Can't allocate buffer")
        try:
            store = libssl_SSL_CTX_get_cert_store(self.ctx)
            loaded = 0
            while True:
                if ca_file_type == SSL_FILETYPE_ASN1:
                    cert = libssl_d2i_X509_bio(
                        biobuf, None)
                else:
                    cert = libssl_PEM_read_bio_X509(
                        biobuf, None, None, None)
                if not cert:
                    break
                try:
                    r = libssl_X509_STORE_add_cert(store, cert)
                finally:
                    libssl_X509_free(cert)
                if not r:
                    err = libssl_ERR_peek_last_error()
                    if (libssl_ERR_GET_LIB(err) == ERR_LIB_X509 and
                        libssl_ERR_GET_REASON(err) ==
                        X509_R_CERT_ALREADY_IN_HASH_TABLE):
                        # cert already in hash table, not an error
                        libssl_ERR_clear_error()
                    else:
                        break
                loaded += 1

            err = libssl_ERR_peek_last_error()
            if (ca_file_type == SSL_FILETYPE_ASN1 and
                loaded > 0 and
                libssl_ERR_GET_LIB(err) == ERR_LIB_ASN1 and
                libssl_ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG):
                # EOF ASN1 file, not an error
                libssl_ERR_clear_error()
            elif (ca_file_type == SSL_FILETYPE_PEM and
                  loaded > 0 and
                  libssl_ERR_GET_LIB(err) == ERR_LIB_PEM and
                  libssl_ERR_GET_REASON(err) == PEM_R_NO_START_LINE):
                # EOF PEM file, not an error
                libssl_ERR_clear_error()
            else:
                raise _ssl_seterror(space, None, 0)
        finally:
            libssl_BIO_free(biobuf)

    def cert_store_stats_w(self, space):
        store = libssl_SSL_CTX_get_cert_store(self.ctx)
        x509 = 0
        x509_ca = 0
        crl = 0
        for i in range(libssl_sk_X509_OBJECT_num(store[0].c_objs)):
            obj = libssl_sk_X509_OBJECT_value(store[0].c_objs, i)
            if intmask(obj.c_type) == X509_LU_X509:
                x509 += 1
                if libssl_X509_check_ca(
                        libssl_pypy_X509_OBJECT_data_x509(obj)):
                    x509_ca += 1
            elif intmask(obj.c_type) == X509_LU_CRL:
                crl += 1
            else:
                # Ignore X509_LU_FAIL, X509_LU_RETRY, X509_LU_PKEY.
                # As far as I can tell they are internal states and never
                # stored in a cert store
                pass
        w_result = space.newdict()
        space.setitem(w_result,
                      space.wrap('x509'), space.wrap(x509))
        space.setitem(w_result,
                      space.wrap('x509_ca'), space.wrap(x509_ca))
        space.setitem(w_result,
                      space.wrap('crl'), space.wrap(crl))
        return w_result

    @unwrap_spec(protos='bufferstr')
    def set_npn_protocols_w(self, space, protos):
        if not HAS_NPN:
            raise oefmt(space.w_NotImplementedError,
                        "The NPN extension requires OpenSSL 1.0.1 or later.")

        self.npn_protocols = SSLNpnProtocols(self.ctx, protos)

    @unwrap_spec(protos='bufferstr')
    def set_alpn_protocols_w(self, space, protos):
        if not HAS_ALPN:
            raise oefmt(space.w_NotImplementedError,
                        "The ALPN extension requires OpenSSL 1.0.2 or later.")

        self.alpn_protocols = SSLAlpnProtocols(self.ctx, protos)

    def get_ca_certs_w(self, space, w_binary_form=None):
        if w_binary_form and space.is_true(w_binary_form):
            binary_mode = True
        else:
            binary_mode = False
        rlist = []
        store = libssl_SSL_CTX_get_cert_store(self.ctx)
        for i in range(libssl_sk_X509_OBJECT_num(store[0].c_objs)):
            obj = libssl_sk_X509_OBJECT_value(store[0].c_objs, i)
            if intmask(obj.c_type) != X509_LU_X509:
                # not a x509 cert
                continue
            # CA for any purpose
            cert = libssl_pypy_X509_OBJECT_data_x509(obj)
            if not libssl_X509_check_ca(cert):
                continue
            if binary_mode:
                rlist.append(_certificate_to_der(space, cert))
            else:
                rlist.append(_decode_certificate(space, cert))
        return space.newlist(rlist)

    @unwrap_spec(name=str)
    def set_ecdh_curve_w(self, space, name):
        nid = libssl_OBJ_sn2nid(name)
        if nid == 0:
            raise oefmt(space.w_ValueError,
                        "unknown elliptic curve name '%s'", name)
        key = libssl_EC_KEY_new_by_curve_name(nid)
        if not key:
            raise _ssl_seterror(space, None, 0)
        try:
            libssl_SSL_CTX_set_tmp_ecdh(self.ctx, key)
        finally:
            libssl_EC_KEY_free(key)

    def set_servername_callback_w(self, space, w_callback):
        if space.is_none(w_callback):
            libssl_SSL_CTX_set_tlsext_servername_callback(
                self.ctx, lltype.nullptr(servername_cb.TO))
            self.servername_callback = None
            return
        if not space.is_true(space.callable(w_callback)):
            raise oefmt(space.w_TypeError, "not a callable object")
        callback_struct = ServernameCallback()
        callback_struct.space = space
        callback_struct.w_ctx = self
        callback_struct.w_set_hostname = w_callback
        self.servername_callback = callback_struct
        index = compute_unique_id(self)
        SERVERNAME_CALLBACKS.set(index, callback_struct)
        libssl_SSL_CTX_set_tlsext_servername_callback(
            self.ctx, _servername_callback)
        libssl_SSL_CTX_set_tlsext_servername_arg(self.ctx,
                                                 rffi.cast(rffi.VOIDP, index))

_SSLContext.typedef = TypeDef(
    "_ssl._SSLContext",
    __new__=interp2app(_SSLContext.descr_new),
    _wrap_socket=interp2app(_SSLContext.descr_wrap_socket),
    set_ciphers=interp2app(_SSLContext.descr_set_ciphers),
    cert_store_stats=interp2app(_SSLContext.cert_store_stats_w),
    load_cert_chain=interp2app(_SSLContext.load_cert_chain_w),
    load_dh_params=interp2app(_SSLContext.load_dh_params_w),
    load_verify_locations=interp2app(_SSLContext.load_verify_locations_w),
    session_stats = interp2app(_SSLContext.session_stats_w),
    set_default_verify_paths=interp2app(_SSLContext.descr_set_default_verify_paths),
    _set_npn_protocols=interp2app(_SSLContext.set_npn_protocols_w),
    _set_alpn_protocols=interp2app(_SSLContext.set_alpn_protocols_w),
    get_ca_certs=interp2app(_SSLContext.get_ca_certs_w),
    set_ecdh_curve=interp2app(_SSLContext.set_ecdh_curve_w),
    set_servername_callback=interp2app(_SSLContext.set_servername_callback_w),

    options=GetSetProperty(_SSLContext.descr_get_options,
                           _SSLContext.descr_set_options),
    verify_mode=GetSetProperty(_SSLContext.descr_get_verify_mode,
                               _SSLContext.descr_set_verify_mode),
    verify_flags=GetSetProperty(_SSLContext.descr_get_verify_flags,
                                _SSLContext.descr_set_verify_flags),
    check_hostname=GetSetProperty(_SSLContext.descr_get_check_hostname,
                                  _SSLContext.descr_set_check_hostname),
)


def _asn1obj2py(space, obj):
    nid = libssl_OBJ_obj2nid(obj)
    if nid == NID_undef:
        raise oefmt(space.w_ValueError, "Unknown object")
    with rffi.scoped_alloc_buffer(100) as buf:
        buflen = libssl_OBJ_obj2txt(buf.raw, 100, obj, 1)
        if buflen < 0:
            raise _ssl_seterror(space, None, 0)
        if buflen:
            w_buf = space.wrap(buf.str(buflen))
        else:
            w_buf = space.w_None
    w_sn = space.wrap(rffi.charp2str(libssl_OBJ_nid2sn(nid)))
    w_ln = space.wrap(rffi.charp2str(libssl_OBJ_nid2ln(nid)))
    return space.newtuple([space.wrap(nid), w_sn, w_ln, w_buf])


@unwrap_spec(txt=str, name=bool)
def txt2obj(space, txt, name=False):
    obj = libssl_OBJ_txt2obj(txt, not name)
    if not obj:
        raise oefmt(space.w_ValueError, "unknown object '%s'", txt)
    try:
        w_result = _asn1obj2py(space, obj)
    finally:
        libssl_ASN1_OBJECT_free(obj)
    return w_result


@unwrap_spec(nid=int)
def nid2obj(space, nid):
    if nid < NID_undef:
        raise oefmt(space.w_ValueError, "NID must be positive")
    obj = libssl_OBJ_nid2obj(nid)
    if not obj:
        raise oefmt(space.w_ValueError, "unknown NID %d", nid)
    try:
        w_result = _asn1obj2py(space, obj)
    finally:
        libssl_ASN1_OBJECT_free(obj)
    return w_result


def w_convert_path(space, path):
    if not path:
        return space.w_None
    else:
        return space.newbytes(rffi.charp2str(path))

def get_default_verify_paths(space):
    return space.newtuple([
        w_convert_path(space, libssl_X509_get_default_cert_file_env()),
        w_convert_path(space, libssl_X509_get_default_cert_file()),
        w_convert_path(space, libssl_X509_get_default_cert_dir_env()),
        w_convert_path(space, libssl_X509_get_default_cert_dir()),
        ])
Back to Top