| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#define PY_SSIZE_T_CLEAN |
| 18 |
#include "Python.h" |
| 19 |
|
| 20 |
#ifdef WITH_THREAD |
| 21 |
#include "pythread.h" |
| 22 |
|
| 23 |
|
| 24 |
#define PySSL_BEGIN_ALLOW_THREADS_S(save) \ |
| 25 |
do { if (_ssl_locks_count>0) { (save) = PyEval_SaveThread(); } } while (0) |
| 26 |
#define PySSL_END_ALLOW_THREADS_S(save) \ |
| 27 |
do { if (_ssl_locks_count>0) { PyEval_RestoreThread(save); } } while (0) |
| 28 |
#define PySSL_BEGIN_ALLOW_THREADS { \ |
| 29 |
PyThreadState *_save = NULL; \ |
| 30 |
PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 31 |
#define PySSL_BLOCK_THREADS PySSL_END_ALLOW_THREADS_S(_save); |
| 32 |
#define PySSL_UNBLOCK_THREADS PySSL_BEGIN_ALLOW_THREADS_S(_save); |
| 33 |
#define PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS_S(_save); } |
| 34 |
|
| 35 |
#else |
| 36 |
|
| 37 |
#define PySSL_BEGIN_ALLOW_THREADS_S(save) |
| 38 |
#define PySSL_END_ALLOW_THREADS_S(save) |
| 39 |
#define PySSL_BEGIN_ALLOW_THREADS |
| 40 |
#define PySSL_BLOCK_THREADS |
| 41 |
#define PySSL_UNBLOCK_THREADS |
| 42 |
#define PySSL_END_ALLOW_THREADS |
| 43 |
|
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
#include "socketmodule.h" |
| 48 |
|
| 49 |
#if defined(HAVE_POLL_H) |
| 50 |
#include <poll.h> |
| 51 |
#elif defined(HAVE_SYS_POLL_H) |
| 52 |
#include <sys/poll.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef MS_WINDOWS |
| 56 |
|
| 57 |
#include <arpa/inet.h> |
| 58 |
#endif |
| 59 |
|
| 60 |
|
| 61 |
#ifdef __GNUC__ |
| 62 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 63 |
#endif |
| 64 |
#ifdef __clang__ |
| 65 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 66 |
#endif |
| 67 |
|
| 68 |
|
| 69 |
#include "openssl/rsa.h" |
| 70 |
#include "openssl/crypto.h" |
| 71 |
#include "openssl/x509.h" |
| 72 |
#include "openssl/x509v3.h" |
| 73 |
#include "openssl/pem.h" |
| 74 |
#include "openssl/ssl.h" |
| 75 |
#include "openssl/err.h" |
| 76 |
#include "openssl/rand.h" |
| 77 |
#include "openssl/dh.h" |
| 78 |
|
| 79 |
|
| 80 |
static PyObject *PySSLErrorObject; |
| 81 |
static PyObject *PySSLZeroReturnErrorObject; |
| 82 |
static PyObject *PySSLWantReadErrorObject; |
| 83 |
static PyObject *PySSLWantWriteErrorObject; |
| 84 |
static PyObject *PySSLSyscallErrorObject; |
| 85 |
static PyObject *PySSLEOFErrorObject; |
| 86 |
|
| 87 |
|
| 88 |
static PyObject *err_codes_to_names; |
| 89 |
static PyObject *err_names_to_codes; |
| 90 |
static PyObject *lib_codes_to_names; |
| 91 |
|
| 92 |
struct py_ssl_error_code { |
| 93 |
const char *mnemonic; |
| 94 |
int library, reason; |
| 95 |
}; |
| 96 |
struct py_ssl_library_code { |
| 97 |
const char *library; |
| 98 |
int code; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
#include "_ssl_data.h" |
| 103 |
|
| 104 |
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 105 |
# define OPENSSL_VERSION_1_1 1 |
| 106 |
# define PY_OPENSSL_1_1_API 1 |
| 107 |
#endif |
| 108 |
|
| 109 |
|
| 110 |
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x2070000fL |
| 111 |
# define PY_OPENSSL_1_1_API 1 |
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 118 |
# define HAVE_TLSv1_2 1 |
| 119 |
#else |
| 120 |
# define HAVE_TLSv1_2 0 |
| 121 |
#endif |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 127 |
# define HAVE_SNI 1 |
| 128 |
#else |
| 129 |
# define HAVE_SNI 0 |
| 130 |
#endif |
| 131 |
|
| 132 |
|
| 133 |
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 134 |
# define HAVE_ALPN 1 |
| 135 |
#else |
| 136 |
# define HAVE_ALPN 0 |
| 137 |
#endif |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#ifdef OPENSSL_NO_NEXTPROTONEG |
| 146 |
# define HAVE_NPN 0 |
| 147 |
#elif (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER) |
| 148 |
# define HAVE_NPN 0 |
| 149 |
#elif defined(TLSEXT_TYPE_next_proto_neg) |
| 150 |
# define HAVE_NPN 1 |
| 151 |
#else |
| 152 |
# define HAVE_NPN 0 |
| 153 |
#endif |
| 154 |
|
| 155 |
#ifndef INVALID_SOCKET |
| 156 |
#define INVALID_SOCKET (-1) |
| 157 |
#endif |
| 158 |
|
| 159 |
|
| 160 |
#if !defined(OPENSSL_VERSION_1_1) && defined(WITH_THREAD) |
| 161 |
#define HAVE_OPENSSL_CRYPTO_LOCK |
| 162 |
#endif |
| 163 |
|
| 164 |
#if defined(OPENSSL_VERSION_1_1) && !defined(OPENSSL_NO_SSL2) |
| 165 |
#define OPENSSL_NO_SSL2 |
| 166 |
#endif |
| 167 |
|
| 168 |
#ifndef PY_OPENSSL_1_1_API |
| 169 |
|
| 170 |
|
| 171 |
#define TLS_method SSLv23_method |
| 172 |
|
| 173 |
static int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne) |
| 174 |
{ |
| 175 |
return ne->set; |
| 176 |
} |
| 177 |
|
| 178 |
#ifndef OPENSSL_NO_COMP |
| 179 |
static int COMP_get_type(const COMP_METHOD *meth) |
| 180 |
{ |
| 181 |
return meth->type; |
| 182 |
} |
| 183 |
#endif |
| 184 |
|
| 185 |
static pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) |
| 186 |
{ |
| 187 |
return ctx->default_passwd_callback; |
| 188 |
} |
| 189 |
|
| 190 |
static void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) |
| 191 |
{ |
| 192 |
return ctx->default_passwd_callback_userdata; |
| 193 |
} |
| 194 |
|
| 195 |
static int X509_OBJECT_get_type(X509_OBJECT *x) |
| 196 |
{ |
| 197 |
return x->type; |
| 198 |
} |
| 199 |
|
| 200 |
static X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) |
| 201 |
{ |
| 202 |
return x->data.x509; |
| 203 |
} |
| 204 |
|
| 205 |
static STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *store) { |
| 206 |
return store->objs; |
| 207 |
} |
| 208 |
|
| 209 |
static X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *store) |
| 210 |
{ |
| 211 |
return store->param; |
| 212 |
} |
| 213 |
#endif |
| 214 |
|
| 215 |
|
| 216 |
enum py_ssl_error { |
| 217 |
|
| 218 |
PY_SSL_ERROR_NONE, |
| 219 |
PY_SSL_ERROR_SSL, |
| 220 |
PY_SSL_ERROR_WANT_READ, |
| 221 |
PY_SSL_ERROR_WANT_WRITE, |
| 222 |
PY_SSL_ERROR_WANT_X509_LOOKUP, |
| 223 |
PY_SSL_ERROR_SYSCALL, |
| 224 |
PY_SSL_ERROR_ZERO_RETURN, |
| 225 |
PY_SSL_ERROR_WANT_CONNECT, |
| 226 |
|
| 227 |
PY_SSL_ERROR_EOF, |
| 228 |
PY_SSL_ERROR_NO_SOCKET, |
| 229 |
PY_SSL_ERROR_INVALID_ERROR_CODE |
| 230 |
}; |
| 231 |
|
| 232 |
enum py_ssl_server_or_client { |
| 233 |
PY_SSL_CLIENT, |
| 234 |
PY_SSL_SERVER |
| 235 |
}; |
| 236 |
|
| 237 |
enum py_ssl_cert_requirements { |
| 238 |
PY_SSL_CERT_NONE, |
| 239 |
PY_SSL_CERT_OPTIONAL, |
| 240 |
PY_SSL_CERT_REQUIRED |
| 241 |
}; |
| 242 |
|
| 243 |
enum py_ssl_version { |
| 244 |
PY_SSL_VERSION_SSL2, |
| 245 |
PY_SSL_VERSION_SSL3=1, |
| 246 |
PY_SSL_VERSION_TLS, |
| 247 |
#if HAVE_TLSv1_2 |
| 248 |
PY_SSL_VERSION_TLS1, |
| 249 |
PY_SSL_VERSION_TLS1_1, |
| 250 |
PY_SSL_VERSION_TLS1_2 |
| 251 |
#else |
| 252 |
PY_SSL_VERSION_TLS1 |
| 253 |
#endif |
| 254 |
}; |
| 255 |
|
| 256 |
#ifdef WITH_THREAD |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
static unsigned int _ssl_locks_count = 0; |
| 262 |
|
| 263 |
#endif |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
#define X509_NAME_MAXLEN 256 |
| 268 |
|
| 269 |
|
| 270 |
#if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 271 |
# define HAVE_OPENSSL_RAND 1 |
| 272 |
#else |
| 273 |
# undef HAVE_OPENSSL_RAND |
| 274 |
#endif |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
#if OPENSSL_VERSION_NUMBER >= 0x009080dfL && OPENSSL_VERSION_NUMBER != 0x00909000L |
| 280 |
# define HAVE_SSL_CTX_CLEAR_OPTIONS |
| 281 |
#else |
| 282 |
# undef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 283 |
#endif |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
#define PySSL_CB_MAXLEN 128 |
| 288 |
|
| 289 |
|
| 290 |
#if OPENSSL_VERSION_NUMBER >= 0x0090500fL |
| 291 |
# define HAVE_OPENSSL_FINISHED 1 |
| 292 |
#else |
| 293 |
# define HAVE_OPENSSL_FINISHED 0 |
| 294 |
#endif |
| 295 |
|
| 296 |
|
| 297 |
#if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_ECDH) |
| 298 |
# define OPENSSL_NO_ECDH |
| 299 |
#endif |
| 300 |
|
| 301 |
|
| 302 |
#if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_COMP) |
| 303 |
# define OPENSSL_NO_COMP |
| 304 |
#endif |
| 305 |
|
| 306 |
|
| 307 |
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL |
| 308 |
# define HAVE_OPENSSL_VERIFY_PARAM |
| 309 |
#endif |
| 310 |
|
| 311 |
|
| 312 |
typedef struct { |
| 313 |
PyObject_HEAD |
| 314 |
SSL_CTX *ctx; |
| 315 |
#if HAVE_NPN |
| 316 |
unsigned char *npn_protocols; |
| 317 |
int npn_protocols_len; |
| 318 |
#endif |
| 319 |
#if HAVE_ALPN |
| 320 |
unsigned char *alpn_protocols; |
| 321 |
int alpn_protocols_len; |
| 322 |
#endif |
| 323 |
#ifndef OPENSSL_NO_TLSEXT |
| 324 |
PyObject *set_hostname; |
| 325 |
#endif |
| 326 |
int check_hostname; |
| 327 |
} PySSLContext; |
| 328 |
|
| 329 |
typedef struct { |
| 330 |
PyObject_HEAD |
| 331 |
PySocketSockObject *Socket; |
| 332 |
PyObject *ssl_sock; |
| 333 |
SSL *ssl; |
| 334 |
PySSLContext *ctx; |
| 335 |
X509 *peer_cert; |
| 336 |
char shutdown_seen_zero; |
| 337 |
char handshake_done; |
| 338 |
enum py_ssl_server_or_client socket_type; |
| 339 |
} PySSLSocket; |
| 340 |
|
| 341 |
static PyTypeObject PySSLContext_Type; |
| 342 |
static PyTypeObject PySSLSocket_Type; |
| 343 |
|
| 344 |
static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args); |
| 345 |
static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args); |
| 346 |
static int check_socket_and_wait_for_timeout(PySocketSockObject *s, |
| 347 |
int writing); |
| 348 |
static PyObject *PySSL_peercert(PySSLSocket *self, PyObject *args); |
| 349 |
static PyObject *PySSL_cipher(PySSLSocket *self); |
| 350 |
|
| 351 |
#define PySSLContext_Check(v) (Py_TYPE(v) == &PySSLContext_Type) |
| 352 |
#define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type) |
| 353 |
|
| 354 |
typedef enum { |
| 355 |
SOCKET_IS_NONBLOCKING, |
| 356 |
SOCKET_IS_BLOCKING, |
| 357 |
SOCKET_HAS_TIMED_OUT, |
| 358 |
SOCKET_HAS_BEEN_CLOSED, |
| 359 |
SOCKET_TOO_LARGE_FOR_SELECT, |
| 360 |
SOCKET_OPERATION_OK |
| 361 |
} timeout_state; |
| 362 |
|
| 363 |
|
| 364 |
#define STRINGIFY1(x) #x |
| 365 |
#define STRINGIFY2(x) STRINGIFY1(x) |
| 366 |
#define ERRSTR1(x,y,z) (x ":" y ": " z) |
| 367 |
#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x) |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
PyDoc_STRVAR(SSLError_doc, |
| 375 |
"An error occurred in the SSL implementation."); |
| 376 |
|
| 377 |
PyDoc_STRVAR(SSLZeroReturnError_doc, |
| 378 |
"SSL/TLS session closed cleanly."); |
| 379 |
|
| 380 |
PyDoc_STRVAR(SSLWantReadError_doc, |
| 381 |
"Non-blocking SSL socket needs to read more data\n" |
| 382 |
"before the requested operation can be completed."); |
| 383 |
|
| 384 |
PyDoc_STRVAR(SSLWantWriteError_doc, |
| 385 |
"Non-blocking SSL socket needs to write more data\n" |
| 386 |
"before the requested operation can be completed."); |
| 387 |
|
| 388 |
PyDoc_STRVAR(SSLSyscallError_doc, |
| 389 |
"System error when attempting SSL operation."); |
| 390 |
|
| 391 |
PyDoc_STRVAR(SSLEOFError_doc, |
| 392 |
"SSL/TLS connection terminated abruptly."); |
| 393 |
|
| 394 |
|
| 395 |
static PyObject * |
| 396 |
SSLError_str(PyEnvironmentErrorObject *self) |
| 397 |
{ |
| 398 |
if (self->strerror != NULL) { |
| 399 |
Py_INCREF(self->strerror); |
| 400 |
return self->strerror; |
| 401 |
} |
| 402 |
else |
| 403 |
return PyObject_Str(self->args); |
| 404 |
} |
| 405 |
|
| 406 |
static void |
| 407 |
fill_and_set_sslerror(PyObject *type, int ssl_errno, const char *errstr, |
| 408 |
int lineno, unsigned long errcode) |
| 409 |
{ |
| 410 |
PyObject *err_value = NULL, *reason_obj = NULL, *lib_obj = NULL; |
| 411 |
PyObject *init_value, *msg, *key; |
| 412 |
|
| 413 |
if (errcode != 0) { |
| 414 |
int lib, reason; |
| 415 |
|
| 416 |
lib = ERR_GET_LIB(errcode); |
| 417 |
reason = ERR_GET_REASON(errcode); |
| 418 |
key = Py_BuildValue("ii", lib, reason); |
| 419 |
if (key == NULL) |
| 420 |
goto fail; |
| 421 |
reason_obj = PyDict_GetItem(err_codes_to_names, key); |
| 422 |
Py_DECREF(key); |
| 423 |
if (reason_obj == NULL) { |
| 424 |
|
| 425 |
PyErr_Clear(); |
| 426 |
} |
| 427 |
key = PyLong_FromLong(lib); |
| 428 |
if (key == NULL) |
| 429 |
goto fail; |
| 430 |
lib_obj = PyDict_GetItem(lib_codes_to_names, key); |
| 431 |
Py_DECREF(key); |
| 432 |
if (lib_obj == NULL) { |
| 433 |
PyErr_Clear(); |
| 434 |
} |
| 435 |
if (errstr == NULL) |
| 436 |
errstr = ERR_reason_error_string(errcode); |
| 437 |
} |
| 438 |
if (errstr == NULL) |
| 439 |
errstr = "unknown error"; |
| 440 |
|
| 441 |
if (reason_obj && lib_obj) |
| 442 |
msg = PyUnicode_FromFormat("[%S: %S] %s (_ssl.c:%d)", |
| 443 |
lib_obj, reason_obj, errstr, lineno); |
| 444 |
else if (lib_obj) |
| 445 |
msg = PyUnicode_FromFormat("[%S] %s (_ssl.c:%d)", |
| 446 |
lib_obj, errstr, lineno); |
| 447 |
else |
| 448 |
msg = PyUnicode_FromFormat("%s (_ssl.c:%d)", errstr, lineno); |
| 449 |
if (msg == NULL) |
| 450 |
goto fail; |
| 451 |
|
| 452 |
init_value = Py_BuildValue("iN", ssl_errno, msg); |
| 453 |
if (init_value == NULL) |
| 454 |
goto fail; |
| 455 |
|
| 456 |
err_value = PyObject_CallObject(type, init_value); |
| 457 |
Py_DECREF(init_value); |
| 458 |
if (err_value == NULL) |
| 459 |
goto fail; |
| 460 |
|
| 461 |
if (reason_obj == NULL) |
| 462 |
reason_obj = Py_None; |
| 463 |
if (PyObject_SetAttrString(err_value, "reason", reason_obj)) |
| 464 |
goto fail; |
| 465 |
if (lib_obj == NULL) |
| 466 |
lib_obj = Py_None; |
| 467 |
if (PyObject_SetAttrString(err_value, "library", lib_obj)) |
| 468 |
goto fail; |
| 469 |
PyErr_SetObject(type, err_value); |
| 470 |
fail: |
| 471 |
Py_XDECREF(err_value); |
| 472 |
} |
| 473 |
|
| 474 |
static PyObject * |
| 475 |
PySSL_SetError(PySSLSocket *obj, int ret, char *filename, int lineno) |
| 476 |
{ |
| 477 |
PyObject *type = PySSLErrorObject; |
| 478 |
char *errstr = NULL; |
| 479 |
int err; |
| 480 |
enum py_ssl_error p = PY_SSL_ERROR_NONE; |
| 481 |
unsigned long e = 0; |
| 482 |
|
| 483 |
assert(ret <= 0); |
| 484 |
e = ERR_peek_last_error(); |
| 485 |
|
| 486 |
if (obj->ssl != NULL) { |
| 487 |
err = SSL_get_error(obj->ssl, ret); |
| 488 |
|
| 489 |
switch (err) { |
| 490 |
case SSL_ERROR_ZERO_RETURN: |
| 491 |
errstr = "TLS/SSL connection has been closed (EOF)"; |
| 492 |
type = PySSLZeroReturnErrorObject; |
| 493 |
p = PY_SSL_ERROR_ZERO_RETURN; |
| 494 |
break; |
| 495 |
case SSL_ERROR_WANT_READ: |
| 496 |
errstr = "The operation did not complete (read)"; |
| 497 |
type = PySSLWantReadErrorObject; |
| 498 |
p = PY_SSL_ERROR_WANT_READ; |
| 499 |
break; |
| 500 |
case SSL_ERROR_WANT_WRITE: |
| 501 |
p = PY_SSL_ERROR_WANT_WRITE; |
| 502 |
type = PySSLWantWriteErrorObject; |
| 503 |
errstr = "The operation did not complete (write)"; |
| 504 |
break; |
| 505 |
case SSL_ERROR_WANT_X509_LOOKUP: |
| 506 |
p = PY_SSL_ERROR_WANT_X509_LOOKUP; |
| 507 |
errstr = "The operation did not complete (X509 lookup)"; |
| 508 |
break; |
| 509 |
case SSL_ERROR_WANT_CONNECT: |
| 510 |
p = PY_SSL_ERROR_WANT_CONNECT; |
| 511 |
errstr = "The operation did not complete (connect)"; |
| 512 |
break; |
| 513 |
case SSL_ERROR_SYSCALL: |
| 514 |
{ |
| 515 |
if (e == 0) { |
| 516 |
PySocketSockObject *s = obj->Socket; |
| 517 |
if (ret == 0) { |
| 518 |
p = PY_SSL_ERROR_EOF; |
| 519 |
type = PySSLEOFErrorObject; |
| 520 |
errstr = "EOF occurred in violation of protocol"; |
| 521 |
} else if (ret == -1) { |
| 522 |
|
| 523 |
Py_INCREF(s); |
| 524 |
ERR_clear_error(); |
| 525 |
s->errorhandler(); |
| 526 |
Py_DECREF(s); |
| 527 |
return NULL; |
| 528 |
} else { |
| 529 |
p = PY_SSL_ERROR_SYSCALL; |
| 530 |
type = PySSLSyscallErrorObject; |
| 531 |
errstr = "Some I/O error occurred"; |
| 532 |
} |
| 533 |
} else { |
| 534 |
p = PY_SSL_ERROR_SYSCALL; |
| 535 |
} |
| 536 |
break; |
| 537 |
} |
| 538 |
case SSL_ERROR_SSL: |
| 539 |
{ |
| 540 |
p = PY_SSL_ERROR_SSL; |
| 541 |
if (e == 0) |
| 542 |
|
| 543 |
errstr = "A failure in the SSL library occurred"; |
| 544 |
break; |
| 545 |
} |
| 546 |
default: |
| 547 |
p = PY_SSL_ERROR_INVALID_ERROR_CODE; |
| 548 |
errstr = "Invalid error code"; |
| 549 |
} |
| 550 |
} |
| 551 |
fill_and_set_sslerror(type, p, errstr, lineno, e); |
| 552 |
ERR_clear_error(); |
| 553 |
return NULL; |
| 554 |
} |
| 555 |
|
| 556 |
static PyObject * |
| 557 |
_setSSLError (char *errstr, int errcode, char *filename, int lineno) { |
| 558 |
|
| 559 |
if (errstr == NULL) |
| 560 |
errcode = ERR_peek_last_error(); |
| 561 |
else |
| 562 |
errcode = 0; |
| 563 |
fill_and_set_sslerror(PySSLErrorObject, errcode, errstr, lineno, errcode); |
| 564 |
ERR_clear_error(); |
| 565 |
return NULL; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static PySSLSocket * |
| 573 |
newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, |
| 574 |
enum py_ssl_server_or_client socket_type, |
| 575 |
char *server_hostname, PyObject *ssl_sock) |
| 576 |
{ |
| 577 |
PySSLSocket *self; |
| 578 |
SSL_CTX *ctx = sslctx->ctx; |
| 579 |
long mode; |
| 580 |
|
| 581 |
self = PyObject_New(PySSLSocket, &PySSLSocket_Type); |
| 582 |
if (self == NULL) |
| 583 |
return NULL; |
| 584 |
|
| 585 |
self->peer_cert = NULL; |
| 586 |
self->ssl = NULL; |
| 587 |
self->Socket = NULL; |
| 588 |
self->ssl_sock = NULL; |
| 589 |
self->ctx = sslctx; |
| 590 |
self->shutdown_seen_zero = 0; |
| 591 |
self->handshake_done = 0; |
| 592 |
Py_INCREF(sslctx); |
| 593 |
|
| 594 |
|
| 595 |
(void) ERR_get_state(); |
| 596 |
ERR_clear_error(); |
| 597 |
|
| 598 |
PySSL_BEGIN_ALLOW_THREADS |
| 599 |
self->ssl = SSL_new(ctx); |
| 600 |
PySSL_END_ALLOW_THREADS |
| 601 |
SSL_set_app_data(self->ssl,self); |
| 602 |
SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); |
| 603 |
mode = SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER; |
| 604 |
#ifdef SSL_MODE_AUTO_RETRY |
| 605 |
mode |= SSL_MODE_AUTO_RETRY; |
| 606 |
#endif |
| 607 |
SSL_set_mode(self->ssl, mode); |
| 608 |
|
| 609 |
#if HAVE_SNI |
| 610 |
if (server_hostname != NULL) { |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
int send_sni = 1; |
| 616 |
#if OPENSSL_VERSION_NUMBER >= 0x10200000L |
| 617 |
ASN1_OCTET_STRING *ip = a2i_IPADDRESS(server_hostname); |
| 618 |
if (ip == NULL) { |
| 619 |
send_sni = 1; |
| 620 |
ERR_clear_error(); |
| 621 |
} else { |
| 622 |
send_sni = 0; |
| 623 |
ASN1_OCTET_STRING_free(ip); |
| 624 |
} |
| 625 |
#elif defined(HAVE_INET_PTON) |
| 626 |
#ifdef ENABLE_IPV6 |
| 627 |
#define PySSL_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 628 |
char packed[PySSL_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; |
| 629 |
#else |
| 630 |
char packed[sizeof(struct in_addr)]; |
| 631 |
#endif |
| 632 |
if (inet_pton(AF_INET, server_hostname, packed)) { |
| 633 |
send_sni = 0; |
| 634 |
#ifdef ENABLE_IPV6 |
| 635 |
} else if(inet_pton(AF_INET6, server_hostname, packed)) { |
| 636 |
send_sni = 0; |
| 637 |
#endif |
| 638 |
} else { |
| 639 |
send_sni = 1; |
| 640 |
} |
| 641 |
#endif |
| 642 |
if (send_sni) { |
| 643 |
SSL_set_tlsext_host_name(self->ssl, server_hostname); |
| 644 |
} |
| 645 |
} |
| 646 |
#endif |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
if (sock->sock_timeout >= 0.0) { |
| 652 |
BIO_set_nbio(SSL_get_rbio(self->ssl), 1); |
| 653 |
BIO_set_nbio(SSL_get_wbio(self->ssl), 1); |
| 654 |
} |
| 655 |
|
| 656 |
PySSL_BEGIN_ALLOW_THREADS |
| 657 |
if (socket_type == PY_SSL_CLIENT) |
| 658 |
SSL_set_connect_state(self->ssl); |
| 659 |
else |
| 660 |
SSL_set_accept_state(self->ssl); |
| 661 |
PySSL_END_ALLOW_THREADS |
| 662 |
|
| 663 |
self->socket_type = socket_type; |
| 664 |
self->Socket = sock; |
| 665 |
Py_INCREF(self->Socket); |
| 666 |
if (ssl_sock != Py_None) { |
| 667 |
self->ssl_sock = PyWeakref_NewRef(ssl_sock, NULL); |
| 668 |
if (self->ssl_sock == NULL) { |
| 669 |
Py_DECREF(self); |
| 670 |
return NULL; |
| 671 |
} |
| 672 |
} |
| 673 |
return self; |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self) |
| 680 |
{ |
| 681 |
int ret; |
| 682 |
int err; |
| 683 |
int sockstate, nonblocking; |
| 684 |
PySocketSockObject *sock = self->Socket; |
| 685 |
|
| 686 |
Py_INCREF(sock); |
| 687 |
|
| 688 |
|
| 689 |
nonblocking = (sock->sock_timeout >= 0.0); |
| 690 |
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 691 |
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
do { |
| 696 |
PySSL_BEGIN_ALLOW_THREADS |
| 697 |
ret = SSL_do_handshake(self->ssl); |
| 698 |
err = SSL_get_error(self->ssl, ret); |
| 699 |
PySSL_END_ALLOW_THREADS |
| 700 |
if (PyErr_CheckSignals()) |
| 701 |
goto error; |
| 702 |
if (err == SSL_ERROR_WANT_READ) { |
| 703 |
sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 704 |
} else if (err == SSL_ERROR_WANT_WRITE) { |
| 705 |
sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 706 |
} else { |
| 707 |
sockstate = SOCKET_OPERATION_OK; |
| 708 |
} |
| 709 |
if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 710 |
PyErr_SetString(PySSLErrorObject, |
| 711 |
ERRSTR("The handshake operation timed out")); |
| 712 |
goto error; |
| 713 |
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 714 |
PyErr_SetString(PySSLErrorObject, |
| 715 |
ERRSTR("Underlying socket has been closed.")); |
| 716 |
goto error; |
| 717 |
} else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 718 |
PyErr_SetString(PySSLErrorObject, |
| 719 |
ERRSTR("Underlying socket too large for select().")); |
| 720 |
goto error; |
| 721 |
} else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 722 |
break; |
| 723 |
} |
| 724 |
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
| 725 |
Py_DECREF(sock); |
| 726 |
if (ret < 1) |
| 727 |
return PySSL_SetError(self, ret, __FILE__, __LINE__); |
| 728 |
|
| 729 |
if (self->peer_cert) |
| 730 |
X509_free (self->peer_cert); |
| 731 |
PySSL_BEGIN_ALLOW_THREADS |
| 732 |
self->peer_cert = SSL_get_peer_certificate(self->ssl); |
| 733 |
PySSL_END_ALLOW_THREADS |
| 734 |
self->handshake_done = 1; |
| 735 |
|
| 736 |
Py_INCREF(Py_None); |
| 737 |
return Py_None; |
| 738 |
|
| 739 |
error: |
| 740 |
Py_DECREF(sock); |
| 741 |
return NULL; |
| 742 |
} |
| 743 |
|
| 744 |
static PyObject * |
| 745 |
_asn1obj2py(const ASN1_OBJECT *name, int no_name) |
| 746 |
{ |
| 747 |
char buf[X509_NAME_MAXLEN]; |
| 748 |
char *namebuf = buf; |
| 749 |
int buflen; |
| 750 |
PyObject *name_obj = NULL; |
| 751 |
|
| 752 |
buflen = OBJ_obj2txt(namebuf, X509_NAME_MAXLEN, name, no_name); |
| 753 |
if (buflen < 0) { |
| 754 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 755 |
return NULL; |
| 756 |
} |
| 757 |
|
| 758 |
if (buflen > X509_NAME_MAXLEN - 1) { |
| 759 |
|
| 760 |
buflen = OBJ_obj2txt(NULL, 0, name, no_name); |
| 761 |
|
| 762 |
namebuf = PyMem_Malloc(buflen + 1); |
| 763 |
if (namebuf == NULL) { |
| 764 |
PyErr_NoMemory(); |
| 765 |
return NULL; |
| 766 |
} |
| 767 |
buflen = OBJ_obj2txt(namebuf, buflen + 1, name, no_name); |
| 768 |
if (buflen < 0) { |
| 769 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 770 |
goto done; |
| 771 |
} |
| 772 |
} |
| 773 |
if (!buflen && no_name) { |
| 774 |
Py_INCREF(Py_None); |
| 775 |
name_obj = Py_None; |
| 776 |
} |
| 777 |
else { |
| 778 |
name_obj = PyString_FromStringAndSize(namebuf, buflen); |
| 779 |
} |
| 780 |
|
| 781 |
done: |
| 782 |
if (buf != namebuf) { |
| 783 |
PyMem_Free(namebuf); |
| 784 |
} |
| 785 |
return name_obj; |
| 786 |
} |
| 787 |
|
| 788 |
static PyObject * |
| 789 |
_create_tuple_for_attribute(ASN1_OBJECT *name, ASN1_STRING *value) |
| 790 |
{ |
| 791 |
Py_ssize_t buflen; |
| 792 |
unsigned char *valuebuf = NULL; |
| 793 |
PyObject *attr, *value_obj; |
| 794 |
|
| 795 |
buflen = ASN1_STRING_to_UTF8(&valuebuf, value); |
| 796 |
if (buflen < 0) { |
| 797 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 798 |
return NULL; |
| 799 |
} |
| 800 |
value_obj = PyUnicode_DecodeUTF8((char *) valuebuf, |
| 801 |
buflen, "strict"); |
| 802 |
|
| 803 |
attr = Py_BuildValue("NN", _asn1obj2py(name, 0), value_obj); |
| 804 |
OPENSSL_free(valuebuf); |
| 805 |
return attr; |
| 806 |
} |
| 807 |
|
| 808 |
static PyObject * |
| 809 |
_create_tuple_for_X509_NAME (X509_NAME *xname) |
| 810 |
{ |
| 811 |
PyObject *dn = NULL; |
| 812 |
PyObject *rdn = NULL; |
| 813 |
PyObject *rdnt; |
| 814 |
PyObject *attr = NULL; |
| 815 |
int entry_count = X509_NAME_entry_count(xname); |
| 816 |
X509_NAME_ENTRY *entry; |
| 817 |
ASN1_OBJECT *name; |
| 818 |
ASN1_STRING *value; |
| 819 |
int index_counter; |
| 820 |
int rdn_level = -1; |
| 821 |
int retcode; |
| 822 |
|
| 823 |
dn = PyList_New(0); |
| 824 |
if (dn == NULL) |
| 825 |
return NULL; |
| 826 |
|
| 827 |
rdn = PyList_New(0); |
| 828 |
if (rdn == NULL) |
| 829 |
goto fail0; |
| 830 |
|
| 831 |
for (index_counter = 0; |
| 832 |
index_counter < entry_count; |
| 833 |
index_counter++) |
| 834 |
{ |
| 835 |
entry = X509_NAME_get_entry(xname, index_counter); |
| 836 |
|
| 837 |
|
| 838 |
if (rdn_level >= 0) { |
| 839 |
if (rdn_level != X509_NAME_ENTRY_set(entry)) { |
| 840 |
|
| 841 |
|
| 842 |
rdnt = PyList_AsTuple(rdn); |
| 843 |
Py_DECREF(rdn); |
| 844 |
if (rdnt == NULL) |
| 845 |
goto fail0; |
| 846 |
retcode = PyList_Append(dn, rdnt); |
| 847 |
Py_DECREF(rdnt); |
| 848 |
if (retcode < 0) |
| 849 |
goto fail0; |
| 850 |
|
| 851 |
rdn = PyList_New(0); |
| 852 |
if (rdn == NULL) |
| 853 |
goto fail0; |
| 854 |
} |
| 855 |
} |
| 856 |
rdn_level = X509_NAME_ENTRY_set(entry); |
| 857 |
|
| 858 |
|
| 859 |
name = X509_NAME_ENTRY_get_object(entry); |
| 860 |
value = X509_NAME_ENTRY_get_data(entry); |
| 861 |
attr = _create_tuple_for_attribute(name, value); |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
if (attr == NULL) |
| 869 |
goto fail1; |
| 870 |
retcode = PyList_Append(rdn, attr); |
| 871 |
Py_DECREF(attr); |
| 872 |
if (retcode < 0) |
| 873 |
goto fail1; |
| 874 |
} |
| 875 |
|
| 876 |
if (rdn != NULL) { |
| 877 |
if (PyList_GET_SIZE(rdn) > 0) { |
| 878 |
rdnt = PyList_AsTuple(rdn); |
| 879 |
Py_DECREF(rdn); |
| 880 |
if (rdnt == NULL) |
| 881 |
goto fail0; |
| 882 |
retcode = PyList_Append(dn, rdnt); |
| 883 |
Py_DECREF(rdnt); |
| 884 |
if (retcode < 0) |
| 885 |
goto fail0; |
| 886 |
} |
| 887 |
else { |
| 888 |
Py_DECREF(rdn); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
rdnt = PyList_AsTuple(dn); |
| 894 |
Py_DECREF(dn); |
| 895 |
if (rdnt == NULL) |
| 896 |
return NULL; |
| 897 |
return rdnt; |
| 898 |
|
| 899 |
fail1: |
| 900 |
Py_XDECREF(rdn); |
| 901 |
|
| 902 |
fail0: |
| 903 |
Py_XDECREF(dn); |
| 904 |
return NULL; |
| 905 |
} |
| 906 |
|
| 907 |
static PyObject * |
| 908 |
_get_peer_alt_names (X509 *certificate) { |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
int i, j; |
| 917 |
PyObject *peer_alt_names = Py_None; |
| 918 |
PyObject *v = NULL, *t; |
| 919 |
X509_EXTENSION *ext = NULL; |
| 920 |
GENERAL_NAMES *names = NULL; |
| 921 |
GENERAL_NAME *name; |
| 922 |
const X509V3_EXT_METHOD *method; |
| 923 |
BIO *biobuf = NULL; |
| 924 |
char buf[2048]; |
| 925 |
char *vptr; |
| 926 |
int len; |
| 927 |
|
| 928 |
#if OPENSSL_VERSION_NUMBER >= 0x009060dfL |
| 929 |
const unsigned char *p; |
| 930 |
#else |
| 931 |
unsigned char *p; |
| 932 |
#endif |
| 933 |
|
| 934 |
if (certificate == NULL) |
| 935 |
return peer_alt_names; |
| 936 |
|
| 937 |
|
| 938 |
biobuf = BIO_new(BIO_s_mem()); |
| 939 |
|
| 940 |
i = -1; |
| 941 |
while ((i = X509_get_ext_by_NID( |
| 942 |
certificate, NID_subject_alt_name, i)) >= 0) { |
| 943 |
|
| 944 |
if (peer_alt_names == Py_None) { |
| 945 |
peer_alt_names = PyList_New(0); |
| 946 |
if (peer_alt_names == NULL) |
| 947 |
goto fail; |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
ext = X509_get_ext(certificate, i); |
| 952 |
if(!(method = X509V3_EXT_get(ext))) { |
| 953 |
PyErr_SetString |
| 954 |
(PySSLErrorObject, |
| 955 |
ERRSTR("No method for internalizing subjectAltName!")); |
| 956 |
goto fail; |
| 957 |
} |
| 958 |
|
| 959 |
p = X509_EXTENSION_get_data(ext)->data; |
| 960 |
if (method->it) |
| 961 |
names = (GENERAL_NAMES*) |
| 962 |
(ASN1_item_d2i(NULL, |
| 963 |
&p, |
| 964 |
X509_EXTENSION_get_data(ext)->length, |
| 965 |
ASN1_ITEM_ptr(method->it))); |
| 966 |
else |
| 967 |
names = (GENERAL_NAMES*) |
| 968 |
(method->d2i(NULL, |
| 969 |
&p, |
| 970 |
X509_EXTENSION_get_data(ext)->length)); |
| 971 |
|
| 972 |
for(j = 0; j < sk_GENERAL_NAME_num(names); j++) { |
| 973 |
|
| 974 |
int gntype; |
| 975 |
ASN1_STRING *as = NULL; |
| 976 |
|
| 977 |
name = sk_GENERAL_NAME_value(names, j); |
| 978 |
gntype = name->type; |
| 979 |
switch (gntype) { |
| 980 |
case GEN_DIRNAME: |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
t = PyTuple_New(2); |
| 985 |
if (t == NULL) { |
| 986 |
goto fail; |
| 987 |
} |
| 988 |
|
| 989 |
v = PyString_FromString("DirName"); |
| 990 |
if (v == NULL) { |
| 991 |
Py_DECREF(t); |
| 992 |
goto fail; |
| 993 |
} |
| 994 |
PyTuple_SET_ITEM(t, 0, v); |
| 995 |
|
| 996 |
v = _create_tuple_for_X509_NAME (name->d.dirn); |
| 997 |
if (v == NULL) { |
| 998 |
Py_DECREF(t); |
| 999 |
goto fail; |
| 1000 |
} |
| 1001 |
PyTuple_SET_ITEM(t, 1, v); |
| 1002 |
break; |
| 1003 |
|
| 1004 |
case GEN_EMAIL: |
| 1005 |
case GEN_DNS: |
| 1006 |
case GEN_URI: |
| 1007 |
|
| 1008 |
|
| 1009 |
t = PyTuple_New(2); |
| 1010 |
if (t == NULL) |
| 1011 |
goto fail; |
| 1012 |
switch (gntype) { |
| 1013 |
case GEN_EMAIL: |
| 1014 |
v = PyString_FromString("email"); |
| 1015 |
as = name->d.rfc822Name; |
| 1016 |
break; |
| 1017 |
case GEN_DNS: |
| 1018 |
v = PyString_FromString("DNS"); |
| 1019 |
as = name->d.dNSName; |
| 1020 |
break; |
| 1021 |
case GEN_URI: |
| 1022 |
v = PyString_FromString("URI"); |
| 1023 |
as = name->d.uniformResourceIdentifier; |
| 1024 |
break; |
| 1025 |
} |
| 1026 |
if (v == NULL) { |
| 1027 |
Py_DECREF(t); |
| 1028 |
goto fail; |
| 1029 |
} |
| 1030 |
PyTuple_SET_ITEM(t, 0, v); |
| 1031 |
v = PyString_FromStringAndSize((char *)ASN1_STRING_data(as), |
| 1032 |
ASN1_STRING_length(as)); |
| 1033 |
if (v == NULL) { |
| 1034 |
Py_DECREF(t); |
| 1035 |
goto fail; |
| 1036 |
} |
| 1037 |
PyTuple_SET_ITEM(t, 1, v); |
| 1038 |
break; |
| 1039 |
|
| 1040 |
case GEN_RID: |
| 1041 |
t = PyTuple_New(2); |
| 1042 |
if (t == NULL) |
| 1043 |
goto fail; |
| 1044 |
|
| 1045 |
v = PyUnicode_FromString("Registered ID"); |
| 1046 |
if (v == NULL) { |
| 1047 |
Py_DECREF(t); |
| 1048 |
goto fail; |
| 1049 |
} |
| 1050 |
PyTuple_SET_ITEM(t, 0, v); |
| 1051 |
|
| 1052 |
len = i2t_ASN1_OBJECT(buf, sizeof(buf)-1, name->d.rid); |
| 1053 |
if (len < 0) { |
| 1054 |
Py_DECREF(t); |
| 1055 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1056 |
goto fail; |
| 1057 |
} else if (len >= (int)sizeof(buf)) { |
| 1058 |
v = PyUnicode_FromString("<INVALID>"); |
| 1059 |
} else { |
| 1060 |
v = PyUnicode_FromStringAndSize(buf, len); |
| 1061 |
} |
| 1062 |
if (v == NULL) { |
| 1063 |
Py_DECREF(t); |
| 1064 |
goto fail; |
| 1065 |
} |
| 1066 |
PyTuple_SET_ITEM(t, 1, v); |
| 1067 |
break; |
| 1068 |
|
| 1069 |
default: |
| 1070 |
|
| 1071 |
switch (gntype) { |
| 1072 |
|
| 1073 |
case GEN_OTHERNAME: |
| 1074 |
case GEN_X400: |
| 1075 |
case GEN_EDIPARTY: |
| 1076 |
case GEN_IPADD: |
| 1077 |
case GEN_RID: |
| 1078 |
break; |
| 1079 |
default: |
| 1080 |
if (PyErr_Warn(PyExc_RuntimeWarning, |
| 1081 |
"Unknown general name type") == -1) { |
| 1082 |
goto fail; |
| 1083 |
} |
| 1084 |
break; |
| 1085 |
} |
| 1086 |
(void) BIO_reset(biobuf); |
| 1087 |
GENERAL_NAME_print(biobuf, name); |
| 1088 |
len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1089 |
if (len < 0) { |
| 1090 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1091 |
goto fail; |
| 1092 |
} |
| 1093 |
vptr = strchr(buf, ':'); |
| 1094 |
if (vptr == NULL) { |
| 1095 |
PyErr_Format(PyExc_ValueError, |
| 1096 |
"Invalid value %.200s", |
| 1097 |
buf); |
| 1098 |
goto fail; |
| 1099 |
} |
| 1100 |
t = PyTuple_New(2); |
| 1101 |
if (t == NULL) |
| 1102 |
goto fail; |
| 1103 |
v = PyString_FromStringAndSize(buf, (vptr - buf)); |
| 1104 |
if (v == NULL) { |
| 1105 |
Py_DECREF(t); |
| 1106 |
goto fail; |
| 1107 |
} |
| 1108 |
PyTuple_SET_ITEM(t, 0, v); |
| 1109 |
v = PyString_FromStringAndSize((vptr + 1), (len - (vptr - buf + 1))); |
| 1110 |
if (v == NULL) { |
| 1111 |
Py_DECREF(t); |
| 1112 |
goto fail; |
| 1113 |
} |
| 1114 |
PyTuple_SET_ITEM(t, 1, v); |
| 1115 |
break; |
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
if (PyList_Append(peer_alt_names, t) < 0) { |
| 1121 |
Py_DECREF(t); |
| 1122 |
goto fail; |
| 1123 |
} |
| 1124 |
Py_DECREF(t); |
| 1125 |
} |
| 1126 |
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 1127 |
} |
| 1128 |
BIO_free(biobuf); |
| 1129 |
if (peer_alt_names != Py_None) { |
| 1130 |
v = PyList_AsTuple(peer_alt_names); |
| 1131 |
Py_DECREF(peer_alt_names); |
| 1132 |
return v; |
| 1133 |
} else { |
| 1134 |
return peer_alt_names; |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
fail: |
| 1139 |
if (biobuf != NULL) |
| 1140 |
BIO_free(biobuf); |
| 1141 |
|
| 1142 |
if (peer_alt_names != Py_None) { |
| 1143 |
Py_XDECREF(peer_alt_names); |
| 1144 |
} |
| 1145 |
|
| 1146 |
return NULL; |
| 1147 |
} |
| 1148 |
|
| 1149 |
static PyObject * |
| 1150 |
_get_aia_uri(X509 *certificate, int nid) { |
| 1151 |
PyObject *lst = NULL, *ostr = NULL; |
| 1152 |
int i, result; |
| 1153 |
AUTHORITY_INFO_ACCESS *info; |
| 1154 |
|
| 1155 |
info = X509_get_ext_d2i(certificate, NID_info_access, NULL, NULL); |
| 1156 |
if (info == NULL) |
| 1157 |
return Py_None; |
| 1158 |
if (sk_ACCESS_DESCRIPTION_num(info) == 0) { |
| 1159 |
AUTHORITY_INFO_ACCESS_free(info); |
| 1160 |
return Py_None; |
| 1161 |
} |
| 1162 |
|
| 1163 |
if ((lst = PyList_New(0)) == NULL) { |
| 1164 |
goto fail; |
| 1165 |
} |
| 1166 |
|
| 1167 |
for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 1168 |
ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 1169 |
ASN1_IA5STRING *uri; |
| 1170 |
|
| 1171 |
if ((OBJ_obj2nid(ad->method) != nid) || |
| 1172 |
(ad->location->type != GEN_URI)) { |
| 1173 |
continue; |
| 1174 |
} |
| 1175 |
uri = ad->location->d.uniformResourceIdentifier; |
| 1176 |
ostr = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1177 |
uri->length); |
| 1178 |
if (ostr == NULL) { |
| 1179 |
goto fail; |
| 1180 |
} |
| 1181 |
result = PyList_Append(lst, ostr); |
| 1182 |
Py_DECREF(ostr); |
| 1183 |
if (result < 0) { |
| 1184 |
goto fail; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
AUTHORITY_INFO_ACCESS_free(info); |
| 1188 |
|
| 1189 |
|
| 1190 |
if (PyList_Size(lst) == 0) { |
| 1191 |
Py_DECREF(lst); |
| 1192 |
return Py_None; |
| 1193 |
} else { |
| 1194 |
PyObject *tup; |
| 1195 |
tup = PyList_AsTuple(lst); |
| 1196 |
Py_DECREF(lst); |
| 1197 |
return tup; |
| 1198 |
} |
| 1199 |
|
| 1200 |
fail: |
| 1201 |
AUTHORITY_INFO_ACCESS_free(info); |
| 1202 |
Py_XDECREF(lst); |
| 1203 |
return NULL; |
| 1204 |
} |
| 1205 |
|
| 1206 |
static PyObject * |
| 1207 |
_get_crl_dp(X509 *certificate) { |
| 1208 |
STACK_OF(DIST_POINT) *dps; |
| 1209 |
int i, j; |
| 1210 |
PyObject *lst, *res = NULL; |
| 1211 |
|
| 1212 |
dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL); |
| 1213 |
|
| 1214 |
if (dps == NULL) |
| 1215 |
return Py_None; |
| 1216 |
|
| 1217 |
lst = PyList_New(0); |
| 1218 |
if (lst == NULL) |
| 1219 |
goto done; |
| 1220 |
|
| 1221 |
for (i=0; i < sk_DIST_POINT_num(dps); i++) { |
| 1222 |
DIST_POINT *dp; |
| 1223 |
STACK_OF(GENERAL_NAME) *gns; |
| 1224 |
|
| 1225 |
dp = sk_DIST_POINT_value(dps, i); |
| 1226 |
if (dp->distpoint == NULL) { |
| 1227 |
|
| 1228 |
continue; |
| 1229 |
} |
| 1230 |
gns = dp->distpoint->name.fullname; |
| 1231 |
|
| 1232 |
for (j=0; j < sk_GENERAL_NAME_num(gns); j++) { |
| 1233 |
GENERAL_NAME *gn; |
| 1234 |
ASN1_IA5STRING *uri; |
| 1235 |
PyObject *ouri; |
| 1236 |
int err; |
| 1237 |
|
| 1238 |
gn = sk_GENERAL_NAME_value(gns, j); |
| 1239 |
if (gn->type != GEN_URI) { |
| 1240 |
continue; |
| 1241 |
} |
| 1242 |
uri = gn->d.uniformResourceIdentifier; |
| 1243 |
ouri = PyUnicode_FromStringAndSize((char *)uri->data, |
| 1244 |
uri->length); |
| 1245 |
if (ouri == NULL) |
| 1246 |
goto done; |
| 1247 |
|
| 1248 |
err = PyList_Append(lst, ouri); |
| 1249 |
Py_DECREF(ouri); |
| 1250 |
if (err < 0) |
| 1251 |
goto done; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None; |
| 1257 |
|
| 1258 |
done: |
| 1259 |
Py_XDECREF(lst); |
| 1260 |
CRL_DIST_POINTS_free(dps); |
| 1261 |
return res; |
| 1262 |
} |
| 1263 |
|
| 1264 |
static PyObject * |
| 1265 |
_decode_certificate(X509 *certificate) { |
| 1266 |
|
| 1267 |
PyObject *retval = NULL; |
| 1268 |
BIO *biobuf = NULL; |
| 1269 |
PyObject *peer; |
| 1270 |
PyObject *peer_alt_names = NULL; |
| 1271 |
PyObject *issuer; |
| 1272 |
PyObject *version; |
| 1273 |
PyObject *sn_obj; |
| 1274 |
PyObject *obj; |
| 1275 |
ASN1_INTEGER *serialNumber; |
| 1276 |
char buf[2048]; |
| 1277 |
int len, result; |
| 1278 |
ASN1_TIME *notBefore, *notAfter; |
| 1279 |
PyObject *pnotBefore, *pnotAfter; |
| 1280 |
|
| 1281 |
retval = PyDict_New(); |
| 1282 |
if (retval == NULL) |
| 1283 |
return NULL; |
| 1284 |
|
| 1285 |
peer = _create_tuple_for_X509_NAME( |
| 1286 |
X509_get_subject_name(certificate)); |
| 1287 |
if (peer == NULL) |
| 1288 |
goto fail0; |
| 1289 |
if (PyDict_SetItemString(retval, (const char *) "subject", peer) < 0) { |
| 1290 |
Py_DECREF(peer); |
| 1291 |
goto fail0; |
| 1292 |
} |
| 1293 |
Py_DECREF(peer); |
| 1294 |
|
| 1295 |
issuer = _create_tuple_for_X509_NAME( |
| 1296 |
X509_get_issuer_name(certificate)); |
| 1297 |
if (issuer == NULL) |
| 1298 |
goto fail0; |
| 1299 |
if (PyDict_SetItemString(retval, (const char *)"issuer", issuer) < 0) { |
| 1300 |
Py_DECREF(issuer); |
| 1301 |
goto fail0; |
| 1302 |
} |
| 1303 |
Py_DECREF(issuer); |
| 1304 |
|
| 1305 |
version = PyLong_FromLong(X509_get_version(certificate) + 1); |
| 1306 |
if (version == NULL) |
| 1307 |
goto fail0; |
| 1308 |
if (PyDict_SetItemString(retval, "version", version) < 0) { |
| 1309 |
Py_DECREF(version); |
| 1310 |
goto fail0; |
| 1311 |
} |
| 1312 |
Py_DECREF(version); |
| 1313 |
|
| 1314 |
|
| 1315 |
biobuf = BIO_new(BIO_s_mem()); |
| 1316 |
|
| 1317 |
(void) BIO_reset(biobuf); |
| 1318 |
serialNumber = X509_get_serialNumber(certificate); |
| 1319 |
|
| 1320 |
i2a_ASN1_INTEGER(biobuf, serialNumber); |
| 1321 |
len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1322 |
if (len < 0) { |
| 1323 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1324 |
goto fail1; |
| 1325 |
} |
| 1326 |
sn_obj = PyUnicode_FromStringAndSize(buf, len); |
| 1327 |
if (sn_obj == NULL) |
| 1328 |
goto fail1; |
| 1329 |
if (PyDict_SetItemString(retval, "serialNumber", sn_obj) < 0) { |
| 1330 |
Py_DECREF(sn_obj); |
| 1331 |
goto fail1; |
| 1332 |
} |
| 1333 |
Py_DECREF(sn_obj); |
| 1334 |
|
| 1335 |
(void) BIO_reset(biobuf); |
| 1336 |
notBefore = X509_get_notBefore(certificate); |
| 1337 |
ASN1_TIME_print(biobuf, notBefore); |
| 1338 |
len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1339 |
if (len < 0) { |
| 1340 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1341 |
goto fail1; |
| 1342 |
} |
| 1343 |
pnotBefore = PyUnicode_FromStringAndSize(buf, len); |
| 1344 |
if (pnotBefore == NULL) |
| 1345 |
goto fail1; |
| 1346 |
if (PyDict_SetItemString(retval, "notBefore", pnotBefore) < 0) { |
| 1347 |
Py_DECREF(pnotBefore); |
| 1348 |
goto fail1; |
| 1349 |
} |
| 1350 |
Py_DECREF(pnotBefore); |
| 1351 |
|
| 1352 |
(void) BIO_reset(biobuf); |
| 1353 |
notAfter = X509_get_notAfter(certificate); |
| 1354 |
ASN1_TIME_print(biobuf, notAfter); |
| 1355 |
len = BIO_gets(biobuf, buf, sizeof(buf)-1); |
| 1356 |
if (len < 0) { |
| 1357 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1358 |
goto fail1; |
| 1359 |
} |
| 1360 |
pnotAfter = PyString_FromStringAndSize(buf, len); |
| 1361 |
if (pnotAfter == NULL) |
| 1362 |
goto fail1; |
| 1363 |
if (PyDict_SetItemString(retval, "notAfter", pnotAfter) < 0) { |
| 1364 |
Py_DECREF(pnotAfter); |
| 1365 |
goto fail1; |
| 1366 |
} |
| 1367 |
Py_DECREF(pnotAfter); |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
peer_alt_names = _get_peer_alt_names(certificate); |
| 1372 |
if (peer_alt_names == NULL) |
| 1373 |
goto fail1; |
| 1374 |
else if (peer_alt_names != Py_None) { |
| 1375 |
if (PyDict_SetItemString(retval, "subjectAltName", |
| 1376 |
peer_alt_names) < 0) { |
| 1377 |
Py_DECREF(peer_alt_names); |
| 1378 |
goto fail1; |
| 1379 |
} |
| 1380 |
Py_DECREF(peer_alt_names); |
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
obj = _get_aia_uri(certificate, NID_ad_OCSP); |
| 1385 |
if (obj == NULL) { |
| 1386 |
goto fail1; |
| 1387 |
} else if (obj != Py_None) { |
| 1388 |
result = PyDict_SetItemString(retval, "OCSP", obj); |
| 1389 |
Py_DECREF(obj); |
| 1390 |
if (result < 0) { |
| 1391 |
goto fail1; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
obj = _get_aia_uri(certificate, NID_ad_ca_issuers); |
| 1396 |
if (obj == NULL) { |
| 1397 |
goto fail1; |
| 1398 |
} else if (obj != Py_None) { |
| 1399 |
result = PyDict_SetItemString(retval, "caIssuers", obj); |
| 1400 |
Py_DECREF(obj); |
| 1401 |
if (result < 0) { |
| 1402 |
goto fail1; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
obj = _get_crl_dp(certificate); |
| 1408 |
if (obj == NULL) { |
| 1409 |
goto fail1; |
| 1410 |
} else if (obj != Py_None) { |
| 1411 |
result = PyDict_SetItemString(retval, "crlDistributionPoints", obj); |
| 1412 |
Py_DECREF(obj); |
| 1413 |
if (result < 0) { |
| 1414 |
goto fail1; |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
BIO_free(biobuf); |
| 1419 |
return retval; |
| 1420 |
|
| 1421 |
fail1: |
| 1422 |
if (biobuf != NULL) |
| 1423 |
BIO_free(biobuf); |
| 1424 |
fail0: |
| 1425 |
Py_XDECREF(retval); |
| 1426 |
return NULL; |
| 1427 |
} |
| 1428 |
|
| 1429 |
static PyObject * |
| 1430 |
_certificate_to_der(X509 *certificate) |
| 1431 |
{ |
| 1432 |
unsigned char *bytes_buf = NULL; |
| 1433 |
int len; |
| 1434 |
PyObject *retval; |
| 1435 |
|
| 1436 |
bytes_buf = NULL; |
| 1437 |
len = i2d_X509(certificate, &bytes_buf); |
| 1438 |
if (len < 0) { |
| 1439 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 1440 |
return NULL; |
| 1441 |
} |
| 1442 |
|
| 1443 |
retval = PyBytes_FromStringAndSize((const char *) bytes_buf, len); |
| 1444 |
OPENSSL_free(bytes_buf); |
| 1445 |
return retval; |
| 1446 |
} |
| 1447 |
|
| 1448 |
static PyObject * |
| 1449 |
PySSL_test_decode_certificate (PyObject *mod, PyObject *args) { |
| 1450 |
|
| 1451 |
PyObject *retval = NULL; |
| 1452 |
char *filename = NULL; |
| 1453 |
X509 *x=NULL; |
| 1454 |
BIO *cert; |
| 1455 |
|
| 1456 |
if (!PyArg_ParseTuple(args, "s:test_decode_certificate", &filename)) |
| 1457 |
return NULL; |
| 1458 |
|
| 1459 |
if ((cert=BIO_new(BIO_s_file())) == NULL) { |
| 1460 |
PyErr_SetString(PySSLErrorObject, |
| 1461 |
"Can't malloc memory to read file"); |
| 1462 |
goto fail0; |
| 1463 |
} |
| 1464 |
|
| 1465 |
if (BIO_read_filename(cert,filename) <= 0) { |
| 1466 |
PyErr_SetString(PySSLErrorObject, |
| 1467 |
"Can't open file"); |
| 1468 |
goto fail0; |
| 1469 |
} |
| 1470 |
|
| 1471 |
x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); |
| 1472 |
if (x == NULL) { |
| 1473 |
PyErr_SetString(PySSLErrorObject, |
| 1474 |
"Error decoding PEM-encoded file"); |
| 1475 |
goto fail0; |
| 1476 |
} |
| 1477 |
|
| 1478 |
retval = _decode_certificate(x); |
| 1479 |
X509_free(x); |
| 1480 |
|
| 1481 |
fail0: |
| 1482 |
|
| 1483 |
if (cert != NULL) BIO_free(cert); |
| 1484 |
return retval; |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
static PyObject * |
| 1489 |
PySSL_peercert(PySSLSocket *self, PyObject *args) |
| 1490 |
{ |
| 1491 |
int verification; |
| 1492 |
PyObject *binary_mode = Py_None; |
| 1493 |
int b; |
| 1494 |
|
| 1495 |
if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode)) |
| 1496 |
return NULL; |
| 1497 |
|
| 1498 |
if (!self->handshake_done) { |
| 1499 |
PyErr_SetString(PyExc_ValueError, |
| 1500 |
"handshake not done yet"); |
| 1501 |
return NULL; |
| 1502 |
} |
| 1503 |
if (!self->peer_cert) |
| 1504 |
Py_RETURN_NONE; |
| 1505 |
|
| 1506 |
b = PyObject_IsTrue(binary_mode); |
| 1507 |
if (b < 0) |
| 1508 |
return NULL; |
| 1509 |
if (b) { |
| 1510 |
|
| 1511 |
return _certificate_to_der(self->peer_cert); |
| 1512 |
} else { |
| 1513 |
verification = SSL_CTX_get_verify_mode(SSL_get_SSL_CTX(self->ssl)); |
| 1514 |
if ((verification & SSL_VERIFY_PEER) == 0) |
| 1515 |
return PyDict_New(); |
| 1516 |
else |
| 1517 |
return _decode_certificate(self->peer_cert); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
PyDoc_STRVAR(PySSL_peercert_doc, |
| 1522 |
"peer_certificate([der=False]) -> certificate\n\ |
| 1523 |
\n\ |
| 1524 |
Returns the certificate for the peer. If no certificate was provided,\n\ |
| 1525 |
returns None. If a certificate was provided, but not validated, returns\n\ |
| 1526 |
an empty dictionary. Otherwise returns a dict containing information\n\ |
| 1527 |
about the peer certificate.\n\ |
| 1528 |
\n\ |
| 1529 |
If the optional argument is True, returns a DER-encoded copy of the\n\ |
| 1530 |
peer certificate, or None if no certificate was provided. This will\n\ |
| 1531 |
return the certificate even if it wasn't validated."); |
| 1532 |
|
| 1533 |
static PyObject *PySSL_cipher (PySSLSocket *self) { |
| 1534 |
|
| 1535 |
PyObject *retval, *v; |
| 1536 |
const SSL_CIPHER *current; |
| 1537 |
char *cipher_name; |
| 1538 |
char *cipher_protocol; |
| 1539 |
|
| 1540 |
if (self->ssl == NULL) |
| 1541 |
Py_RETURN_NONE; |
| 1542 |
current = SSL_get_current_cipher(self->ssl); |
| 1543 |
if (current == NULL) |
| 1544 |
Py_RETURN_NONE; |
| 1545 |
|
| 1546 |
retval = PyTuple_New(3); |
| 1547 |
if (retval == NULL) |
| 1548 |
return NULL; |
| 1549 |
|
| 1550 |
cipher_name = (char *) SSL_CIPHER_get_name(current); |
| 1551 |
if (cipher_name == NULL) { |
| 1552 |
Py_INCREF(Py_None); |
| 1553 |
PyTuple_SET_ITEM(retval, 0, Py_None); |
| 1554 |
} else { |
| 1555 |
v = PyString_FromString(cipher_name); |
| 1556 |
if (v == NULL) |
| 1557 |
goto fail0; |
| 1558 |
PyTuple_SET_ITEM(retval, 0, v); |
| 1559 |
} |
| 1560 |
cipher_protocol = (char *) SSL_CIPHER_get_version(current); |
| 1561 |
if (cipher_protocol == NULL) { |
| 1562 |
Py_INCREF(Py_None); |
| 1563 |
PyTuple_SET_ITEM(retval, 1, Py_None); |
| 1564 |
} else { |
| 1565 |
v = PyString_FromString(cipher_protocol); |
| 1566 |
if (v == NULL) |
| 1567 |
goto fail0; |
| 1568 |
PyTuple_SET_ITEM(retval, 1, v); |
| 1569 |
} |
| 1570 |
v = PyInt_FromLong(SSL_CIPHER_get_bits(current, NULL)); |
| 1571 |
if (v == NULL) |
| 1572 |
goto fail0; |
| 1573 |
PyTuple_SET_ITEM(retval, 2, v); |
| 1574 |
return retval; |
| 1575 |
|
| 1576 |
fail0: |
| 1577 |
Py_DECREF(retval); |
| 1578 |
return NULL; |
| 1579 |
} |
| 1580 |
|
| 1581 |
static PyObject *PySSL_version(PySSLSocket *self) |
| 1582 |
{ |
| 1583 |
const char *version; |
| 1584 |
|
| 1585 |
if (self->ssl == NULL) |
| 1586 |
Py_RETURN_NONE; |
| 1587 |
version = SSL_get_version(self->ssl); |
| 1588 |
if (!strcmp(version, "unknown")) |
| 1589 |
Py_RETURN_NONE; |
| 1590 |
return PyUnicode_FromString(version); |
| 1591 |
} |
| 1592 |
|
| 1593 |
#if HAVE_NPN |
| 1594 |
static PyObject *PySSL_selected_npn_protocol(PySSLSocket *self) { |
| 1595 |
const unsigned char *out; |
| 1596 |
unsigned int outlen; |
| 1597 |
|
| 1598 |
SSL_get0_next_proto_negotiated(self->ssl, |
| 1599 |
&out, &outlen); |
| 1600 |
|
| 1601 |
if (out == NULL) |
| 1602 |
Py_RETURN_NONE; |
| 1603 |
return PyString_FromStringAndSize((char *)out, outlen); |
| 1604 |
} |
| 1605 |
#endif |
| 1606 |
|
| 1607 |
#if HAVE_ALPN |
| 1608 |
static PyObject *PySSL_selected_alpn_protocol(PySSLSocket *self) { |
| 1609 |
const unsigned char *out; |
| 1610 |
unsigned int outlen; |
| 1611 |
|
| 1612 |
SSL_get0_alpn_selected(self->ssl, &out, &outlen); |
| 1613 |
|
| 1614 |
if (out == NULL) |
| 1615 |
Py_RETURN_NONE; |
| 1616 |
return PyString_FromStringAndSize((char *)out, outlen); |
| 1617 |
} |
| 1618 |
#endif |
| 1619 |
|
| 1620 |
static PyObject *PySSL_compression(PySSLSocket *self) { |
| 1621 |
#ifdef OPENSSL_NO_COMP |
| 1622 |
Py_RETURN_NONE; |
| 1623 |
#else |
| 1624 |
const COMP_METHOD *comp_method; |
| 1625 |
const char *short_name; |
| 1626 |
|
| 1627 |
if (self->ssl == NULL) |
| 1628 |
Py_RETURN_NONE; |
| 1629 |
comp_method = SSL_get_current_compression(self->ssl); |
| 1630 |
if (comp_method == NULL || COMP_get_type(comp_method) == NID_undef) |
| 1631 |
Py_RETURN_NONE; |
| 1632 |
short_name = OBJ_nid2sn(COMP_get_type(comp_method)); |
| 1633 |
if (short_name == NULL) |
| 1634 |
Py_RETURN_NONE; |
| 1635 |
return PyBytes_FromString(short_name); |
| 1636 |
#endif |
| 1637 |
} |
| 1638 |
|
| 1639 |
static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { |
| 1640 |
Py_INCREF(self->ctx); |
| 1641 |
return self->ctx; |
| 1642 |
} |
| 1643 |
|
| 1644 |
static int PySSL_set_context(PySSLSocket *self, PyObject *value, |
| 1645 |
void *closure) { |
| 1646 |
|
| 1647 |
if (PyObject_TypeCheck(value, &PySSLContext_Type)) { |
| 1648 |
#if !HAVE_SNI |
| 1649 |
PyErr_SetString(PyExc_NotImplementedError, "setting a socket's " |
| 1650 |
"context is not supported by your OpenSSL library"); |
| 1651 |
return -1; |
| 1652 |
#else |
| 1653 |
Py_INCREF(value); |
| 1654 |
Py_SETREF(self->ctx, (PySSLContext *)value); |
| 1655 |
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); |
| 1656 |
#endif |
| 1657 |
} else { |
| 1658 |
PyErr_SetString(PyExc_TypeError, "The value must be a SSLContext"); |
| 1659 |
return -1; |
| 1660 |
} |
| 1661 |
|
| 1662 |
return 0; |
| 1663 |
} |
| 1664 |
|
| 1665 |
PyDoc_STRVAR(PySSL_set_context_doc, |
| 1666 |
"_setter_context(ctx)\n\ |
| 1667 |
\ |
| 1668 |
This changes the context associated with the SSLSocket. This is typically\n\ |
| 1669 |
used from within a callback function set by the set_servername_callback\n\ |
| 1670 |
on the SSLContext to change the certificate information associated with the\n\ |
| 1671 |
SSLSocket before the cryptographic exchange handshake messages\n"); |
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
static void PySSL_dealloc(PySSLSocket *self) |
| 1676 |
{ |
| 1677 |
if (self->peer_cert) |
| 1678 |
X509_free (self->peer_cert); |
| 1679 |
if (self->ssl) |
| 1680 |
SSL_free(self->ssl); |
| 1681 |
Py_XDECREF(self->Socket); |
| 1682 |
Py_XDECREF(self->ssl_sock); |
| 1683 |
Py_XDECREF(self->ctx); |
| 1684 |
PyObject_Del(self); |
| 1685 |
} |
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
static int |
| 1693 |
check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) |
| 1694 |
{ |
| 1695 |
fd_set fds; |
| 1696 |
struct timeval tv; |
| 1697 |
int rc; |
| 1698 |
|
| 1699 |
|
| 1700 |
if (s->sock_timeout < 0.0) |
| 1701 |
return SOCKET_IS_BLOCKING; |
| 1702 |
else if (s->sock_timeout == 0.0) |
| 1703 |
return SOCKET_IS_NONBLOCKING; |
| 1704 |
|
| 1705 |
|
| 1706 |
if (s->sock_fd < 0) |
| 1707 |
return SOCKET_HAS_BEEN_CLOSED; |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
#ifdef HAVE_POLL |
| 1712 |
{ |
| 1713 |
struct pollfd pollfd; |
| 1714 |
int timeout; |
| 1715 |
|
| 1716 |
pollfd.fd = s->sock_fd; |
| 1717 |
pollfd.events = writing ? POLLOUT : POLLIN; |
| 1718 |
|
| 1719 |
|
| 1720 |
timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 1721 |
PySSL_BEGIN_ALLOW_THREADS |
| 1722 |
rc = poll(&pollfd, 1, timeout); |
| 1723 |
PySSL_END_ALLOW_THREADS |
| 1724 |
|
| 1725 |
goto normal_return; |
| 1726 |
} |
| 1727 |
#endif |
| 1728 |
|
| 1729 |
|
| 1730 |
if (!_PyIsSelectable_fd(s->sock_fd)) |
| 1731 |
return SOCKET_TOO_LARGE_FOR_SELECT; |
| 1732 |
|
| 1733 |
|
| 1734 |
tv.tv_sec = (int)s->sock_timeout; |
| 1735 |
tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1736 |
FD_ZERO(&fds); |
| 1737 |
FD_SET(s->sock_fd, &fds); |
| 1738 |
|
| 1739 |
|
| 1740 |
PySSL_BEGIN_ALLOW_THREADS |
| 1741 |
if (writing) |
| 1742 |
rc = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 1743 |
else |
| 1744 |
rc = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 1745 |
PySSL_END_ALLOW_THREADS |
| 1746 |
|
| 1747 |
#ifdef HAVE_POLL |
| 1748 |
normal_return: |
| 1749 |
#endif |
| 1750 |
|
| 1751 |
|
| 1752 |
return rc == 0 ? SOCKET_HAS_TIMED_OUT : SOCKET_OPERATION_OK; |
| 1753 |
} |
| 1754 |
|
| 1755 |
static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args) |
| 1756 |
{ |
| 1757 |
Py_buffer buf; |
| 1758 |
int len; |
| 1759 |
int sockstate; |
| 1760 |
int err; |
| 1761 |
int nonblocking; |
| 1762 |
PySocketSockObject *sock = self->Socket; |
| 1763 |
|
| 1764 |
Py_INCREF(sock); |
| 1765 |
|
| 1766 |
if (!PyArg_ParseTuple(args, "s*:write", &buf)) { |
| 1767 |
Py_DECREF(sock); |
| 1768 |
return NULL; |
| 1769 |
} |
| 1770 |
|
| 1771 |
if (buf.len > INT_MAX) { |
| 1772 |
PyErr_Format(PyExc_OverflowError, |
| 1773 |
"string longer than %d bytes", INT_MAX); |
| 1774 |
goto error; |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
nonblocking = (sock->sock_timeout >= 0.0); |
| 1779 |
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1780 |
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1781 |
|
| 1782 |
sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 1783 |
if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1784 |
PyErr_SetString(PySSLErrorObject, |
| 1785 |
"The write operation timed out"); |
| 1786 |
goto error; |
| 1787 |
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1788 |
PyErr_SetString(PySSLErrorObject, |
| 1789 |
"Underlying socket has been closed."); |
| 1790 |
goto error; |
| 1791 |
} else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 1792 |
PyErr_SetString(PySSLErrorObject, |
| 1793 |
"Underlying socket too large for select()."); |
| 1794 |
goto error; |
| 1795 |
} |
| 1796 |
do { |
| 1797 |
PySSL_BEGIN_ALLOW_THREADS |
| 1798 |
len = SSL_write(self->ssl, buf.buf, (int)buf.len); |
| 1799 |
err = SSL_get_error(self->ssl, len); |
| 1800 |
PySSL_END_ALLOW_THREADS |
| 1801 |
if (PyErr_CheckSignals()) { |
| 1802 |
goto error; |
| 1803 |
} |
| 1804 |
if (err == SSL_ERROR_WANT_READ) { |
| 1805 |
sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 1806 |
} else if (err == SSL_ERROR_WANT_WRITE) { |
| 1807 |
sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 1808 |
} else { |
| 1809 |
sockstate = SOCKET_OPERATION_OK; |
| 1810 |
} |
| 1811 |
if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1812 |
PyErr_SetString(PySSLErrorObject, |
| 1813 |
"The write operation timed out"); |
| 1814 |
goto error; |
| 1815 |
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { |
| 1816 |
PyErr_SetString(PySSLErrorObject, |
| 1817 |
"Underlying socket has been closed."); |
| 1818 |
goto error; |
| 1819 |
} else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1820 |
break; |
| 1821 |
} |
| 1822 |
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
| 1823 |
|
| 1824 |
Py_DECREF(sock); |
| 1825 |
PyBuffer_Release(&buf); |
| 1826 |
if (len > 0) |
| 1827 |
return PyInt_FromLong(len); |
| 1828 |
else |
| 1829 |
return PySSL_SetError(self, len, __FILE__, __LINE__); |
| 1830 |
|
| 1831 |
error: |
| 1832 |
Py_DECREF(sock); |
| 1833 |
PyBuffer_Release(&buf); |
| 1834 |
return NULL; |
| 1835 |
} |
| 1836 |
|
| 1837 |
PyDoc_STRVAR(PySSL_SSLwrite_doc, |
| 1838 |
"write(s) -> len\n\ |
| 1839 |
\n\ |
| 1840 |
Writes the string s into the SSL object. Returns the number\n\ |
| 1841 |
of bytes written."); |
| 1842 |
|
| 1843 |
static PyObject *PySSL_SSLpending(PySSLSocket *self) |
| 1844 |
{ |
| 1845 |
int count = 0; |
| 1846 |
|
| 1847 |
PySSL_BEGIN_ALLOW_THREADS |
| 1848 |
count = SSL_pending(self->ssl); |
| 1849 |
PySSL_END_ALLOW_THREADS |
| 1850 |
if (count < 0) |
| 1851 |
return PySSL_SetError(self, count, __FILE__, __LINE__); |
| 1852 |
else |
| 1853 |
return PyInt_FromLong(count); |
| 1854 |
} |
| 1855 |
|
| 1856 |
PyDoc_STRVAR(PySSL_SSLpending_doc, |
| 1857 |
"pending() -> count\n\ |
| 1858 |
\n\ |
| 1859 |
Returns the number of already decrypted bytes available for read,\n\ |
| 1860 |
pending on the connection.\n"); |
| 1861 |
|
| 1862 |
static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args) |
| 1863 |
{ |
| 1864 |
PyObject *dest = NULL; |
| 1865 |
Py_buffer buf; |
| 1866 |
char *mem; |
| 1867 |
int len, count; |
| 1868 |
int buf_passed = 0; |
| 1869 |
int sockstate; |
| 1870 |
int err; |
| 1871 |
int nonblocking; |
| 1872 |
PySocketSockObject *sock = self->Socket; |
| 1873 |
|
| 1874 |
Py_INCREF(sock); |
| 1875 |
|
| 1876 |
buf.obj = NULL; |
| 1877 |
buf.buf = NULL; |
| 1878 |
if (!PyArg_ParseTuple(args, "i|w*:read", &len, &buf)) |
| 1879 |
goto error; |
| 1880 |
|
| 1881 |
if ((buf.buf == NULL) && (buf.obj == NULL)) { |
| 1882 |
if (len < 0) { |
| 1883 |
PyErr_SetString(PyExc_ValueError, "size should not be negative"); |
| 1884 |
goto error; |
| 1885 |
} |
| 1886 |
dest = PyBytes_FromStringAndSize(NULL, len); |
| 1887 |
if (dest == NULL) |
| 1888 |
goto error; |
| 1889 |
if (len == 0) { |
| 1890 |
Py_XDECREF(sock); |
| 1891 |
return dest; |
| 1892 |
} |
| 1893 |
mem = PyBytes_AS_STRING(dest); |
| 1894 |
} |
| 1895 |
else { |
| 1896 |
buf_passed = 1; |
| 1897 |
mem = buf.buf; |
| 1898 |
if (len <= 0 || len > buf.len) { |
| 1899 |
len = (int) buf.len; |
| 1900 |
if (buf.len != len) { |
| 1901 |
PyErr_SetString(PyExc_OverflowError, |
| 1902 |
"maximum length can't fit in a C 'int'"); |
| 1903 |
goto error; |
| 1904 |
} |
| 1905 |
if (len == 0) { |
| 1906 |
count = 0; |
| 1907 |
goto done; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
} |
| 1911 |
|
| 1912 |
|
| 1913 |
nonblocking = (sock->sock_timeout >= 0.0); |
| 1914 |
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1915 |
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1916 |
|
| 1917 |
do { |
| 1918 |
PySSL_BEGIN_ALLOW_THREADS |
| 1919 |
count = SSL_read(self->ssl, mem, len); |
| 1920 |
err = SSL_get_error(self->ssl, count); |
| 1921 |
PySSL_END_ALLOW_THREADS |
| 1922 |
if (PyErr_CheckSignals()) |
| 1923 |
goto error; |
| 1924 |
if (err == SSL_ERROR_WANT_READ) { |
| 1925 |
sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 1926 |
} else if (err == SSL_ERROR_WANT_WRITE) { |
| 1927 |
sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 1928 |
} else if ((err == SSL_ERROR_ZERO_RETURN) && |
| 1929 |
(SSL_get_shutdown(self->ssl) == |
| 1930 |
SSL_RECEIVED_SHUTDOWN)) |
| 1931 |
{ |
| 1932 |
count = 0; |
| 1933 |
goto done; |
| 1934 |
} else { |
| 1935 |
sockstate = SOCKET_OPERATION_OK; |
| 1936 |
} |
| 1937 |
if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 1938 |
PyErr_SetString(PySSLErrorObject, |
| 1939 |
"The read operation timed out"); |
| 1940 |
goto error; |
| 1941 |
} else if (sockstate == SOCKET_IS_NONBLOCKING) { |
| 1942 |
break; |
| 1943 |
} |
| 1944 |
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); |
| 1945 |
if (count <= 0) { |
| 1946 |
PySSL_SetError(self, count, __FILE__, __LINE__); |
| 1947 |
goto error; |
| 1948 |
} |
| 1949 |
|
| 1950 |
done: |
| 1951 |
Py_DECREF(sock); |
| 1952 |
if (!buf_passed) { |
| 1953 |
_PyBytes_Resize(&dest, count); |
| 1954 |
return dest; |
| 1955 |
} |
| 1956 |
else { |
| 1957 |
PyBuffer_Release(&buf); |
| 1958 |
return PyLong_FromLong(count); |
| 1959 |
} |
| 1960 |
|
| 1961 |
error: |
| 1962 |
Py_DECREF(sock); |
| 1963 |
if (!buf_passed) |
| 1964 |
Py_XDECREF(dest); |
| 1965 |
else |
| 1966 |
PyBuffer_Release(&buf); |
| 1967 |
return NULL; |
| 1968 |
} |
| 1969 |
|
| 1970 |
PyDoc_STRVAR(PySSL_SSLread_doc, |
| 1971 |
"read([len]) -> string\n\ |
| 1972 |
\n\ |
| 1973 |
Read up to len bytes from the SSL socket."); |
| 1974 |
|
| 1975 |
static PyObject *PySSL_SSLshutdown(PySSLSocket *self) |
| 1976 |
{ |
| 1977 |
int err, ssl_err, sockstate, nonblocking; |
| 1978 |
int zeros = 0; |
| 1979 |
PySocketSockObject *sock = self->Socket; |
| 1980 |
|
| 1981 |
|
| 1982 |
if (sock->sock_fd < 0) { |
| 1983 |
_setSSLError("Underlying socket connection gone", |
| 1984 |
PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); |
| 1985 |
return NULL; |
| 1986 |
} |
| 1987 |
Py_INCREF(sock); |
| 1988 |
|
| 1989 |
|
| 1990 |
nonblocking = (sock->sock_timeout >= 0.0); |
| 1991 |
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); |
| 1992 |
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); |
| 1993 |
|
| 1994 |
while (1) { |
| 1995 |
PySSL_BEGIN_ALLOW_THREADS |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
if (self->shutdown_seen_zero) |
| 2005 |
SSL_set_read_ahead(self->ssl, 0); |
| 2006 |
err = SSL_shutdown(self->ssl); |
| 2007 |
PySSL_END_ALLOW_THREADS |
| 2008 |
|
| 2009 |
if (err > 0) |
| 2010 |
break; |
| 2011 |
if (err == 0) { |
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
if (++zeros > 1) |
| 2016 |
break; |
| 2017 |
|
| 2018 |
self->shutdown_seen_zero = 1; |
| 2019 |
continue; |
| 2020 |
} |
| 2021 |
|
| 2022 |
|
| 2023 |
ssl_err = SSL_get_error(self->ssl, err); |
| 2024 |
if (ssl_err == SSL_ERROR_WANT_READ) |
| 2025 |
sockstate = check_socket_and_wait_for_timeout(sock, 0); |
| 2026 |
else if (ssl_err == SSL_ERROR_WANT_WRITE) |
| 2027 |
sockstate = check_socket_and_wait_for_timeout(sock, 1); |
| 2028 |
else |
| 2029 |
break; |
| 2030 |
if (sockstate == SOCKET_HAS_TIMED_OUT) { |
| 2031 |
if (ssl_err == SSL_ERROR_WANT_READ) |
| 2032 |
PyErr_SetString(PySSLErrorObject, |
| 2033 |
"The read operation timed out"); |
| 2034 |
else |
| 2035 |
PyErr_SetString(PySSLErrorObject, |
| 2036 |
"The write operation timed out"); |
| 2037 |
goto error; |
| 2038 |
} |
| 2039 |
else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { |
| 2040 |
PyErr_SetString(PySSLErrorObject, |
| 2041 |
"Underlying socket too large for select()."); |
| 2042 |
goto error; |
| 2043 |
} |
| 2044 |
else if (sockstate != SOCKET_OPERATION_OK) |
| 2045 |
|
| 2046 |
break; |
| 2047 |
} |
| 2048 |
|
| 2049 |
if (err < 0) { |
| 2050 |
Py_DECREF(sock); |
| 2051 |
return PySSL_SetError(self, err, __FILE__, __LINE__); |
| 2052 |
} |
| 2053 |
else |
| 2054 |
|
| 2055 |
return (PyObject *) sock; |
| 2056 |
|
| 2057 |
error: |
| 2058 |
Py_DECREF(sock); |
| 2059 |
return NULL; |
| 2060 |
} |
| 2061 |
|
| 2062 |
PyDoc_STRVAR(PySSL_SSLshutdown_doc, |
| 2063 |
"shutdown(s) -> socket\n\ |
| 2064 |
\n\ |
| 2065 |
Does the SSL shutdown handshake with the remote end, and returns\n\ |
| 2066 |
the underlying socket object."); |
| 2067 |
|
| 2068 |
#if HAVE_OPENSSL_FINISHED |
| 2069 |
static PyObject * |
| 2070 |
PySSL_tls_unique_cb(PySSLSocket *self) |
| 2071 |
{ |
| 2072 |
PyObject *retval = NULL; |
| 2073 |
char buf[PySSL_CB_MAXLEN]; |
| 2074 |
size_t len; |
| 2075 |
|
| 2076 |
if (SSL_session_reused(self->ssl) ^ !self->socket_type) { |
| 2077 |
|
| 2078 |
len = SSL_get_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2079 |
} |
| 2080 |
else { |
| 2081 |
|
| 2082 |
len = SSL_get_peer_finished(self->ssl, buf, PySSL_CB_MAXLEN); |
| 2083 |
} |
| 2084 |
|
| 2085 |
|
| 2086 |
if (len == 0) |
| 2087 |
Py_RETURN_NONE; |
| 2088 |
|
| 2089 |
retval = PyBytes_FromStringAndSize(buf, len); |
| 2090 |
|
| 2091 |
return retval; |
| 2092 |
} |
| 2093 |
|
| 2094 |
PyDoc_STRVAR(PySSL_tls_unique_cb_doc, |
| 2095 |
"tls_unique_cb() -> bytes\n\ |
| 2096 |
\n\ |
| 2097 |
Returns the 'tls-unique' channel binding data, as defined by RFC 5929.\n\ |
| 2098 |
\n\ |
| 2099 |
If the TLS handshake is not yet complete, None is returned"); |
| 2100 |
|
| 2101 |
#endif |
| 2102 |
|
| 2103 |
static PyGetSetDef ssl_getsetlist[] = { |
| 2104 |
{"context", (getter) PySSL_get_context, |
| 2105 |
(setter) PySSL_set_context, PySSL_set_context_doc}, |
| 2106 |
{NULL}, |
| 2107 |
}; |
| 2108 |
|
| 2109 |
static PyMethodDef PySSLMethods[] = { |
| 2110 |
{"do_handshake", (PyCFunction)PySSL_SSLdo_handshake, METH_NOARGS}, |
| 2111 |
{"write", (PyCFunction)PySSL_SSLwrite, METH_VARARGS, |
| 2112 |
PySSL_SSLwrite_doc}, |
| 2113 |
{"read", (PyCFunction)PySSL_SSLread, METH_VARARGS, |
| 2114 |
PySSL_SSLread_doc}, |
| 2115 |
{"pending", (PyCFunction)PySSL_SSLpending, METH_NOARGS, |
| 2116 |
PySSL_SSLpending_doc}, |
| 2117 |
{"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS, |
| 2118 |
PySSL_peercert_doc}, |
| 2119 |
{"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS}, |
| 2120 |
{"version", (PyCFunction)PySSL_version, METH_NOARGS}, |
| 2121 |
#if HAVE_NPN |
| 2122 |
{"selected_npn_protocol", (PyCFunction)PySSL_selected_npn_protocol, METH_NOARGS}, |
| 2123 |
#endif |
| 2124 |
#if HAVE_ALPN |
| 2125 |
{"selected_alpn_protocol", (PyCFunction)PySSL_selected_alpn_protocol, METH_NOARGS}, |
| 2126 |
#endif |
| 2127 |
{"compression", (PyCFunction)PySSL_compression, METH_NOARGS}, |
| 2128 |
{"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS, |
| 2129 |
PySSL_SSLshutdown_doc}, |
| 2130 |
#if HAVE_OPENSSL_FINISHED |
| 2131 |
{"tls_unique_cb", (PyCFunction)PySSL_tls_unique_cb, METH_NOARGS, |
| 2132 |
PySSL_tls_unique_cb_doc}, |
| 2133 |
#endif |
| 2134 |
{NULL, NULL} |
| 2135 |
}; |
| 2136 |
|
| 2137 |
static PyTypeObject PySSLSocket_Type = { |
| 2138 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2139 |
"_ssl._SSLSocket", |
| 2140 |
sizeof(PySSLSocket), |
| 2141 |
0, |
| 2142 |
|
| 2143 |
(destructor)PySSL_dealloc, |
| 2144 |
0, |
| 2145 |
0, |
| 2146 |
0, |
| 2147 |
0, |
| 2148 |
0, |
| 2149 |
0, |
| 2150 |
0, |
| 2151 |
0, |
| 2152 |
0, |
| 2153 |
0, |
| 2154 |
0, |
| 2155 |
0, |
| 2156 |
0, |
| 2157 |
0, |
| 2158 |
Py_TPFLAGS_DEFAULT, |
| 2159 |
0, |
| 2160 |
0, |
| 2161 |
0, |
| 2162 |
0, |
| 2163 |
0, |
| 2164 |
0, |
| 2165 |
0, |
| 2166 |
PySSLMethods, |
| 2167 |
0, |
| 2168 |
ssl_getsetlist, |
| 2169 |
}; |
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
static PyObject * |
| 2177 |
context_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2178 |
{ |
| 2179 |
char *kwlist[] = {"protocol", NULL}; |
| 2180 |
PySSLContext *self; |
| 2181 |
int proto_version = PY_SSL_VERSION_TLS; |
| 2182 |
long options; |
| 2183 |
SSL_CTX *ctx = NULL; |
| 2184 |
int result; |
| 2185 |
|
| 2186 |
if (!PyArg_ParseTupleAndKeywords( |
| 2187 |
args, kwds, "i:_SSLContext", kwlist, |
| 2188 |
&proto_version)) |
| 2189 |
return NULL; |
| 2190 |
|
| 2191 |
PySSL_BEGIN_ALLOW_THREADS |
| 2192 |
if (proto_version == PY_SSL_VERSION_TLS1) |
| 2193 |
ctx = SSL_CTX_new(TLSv1_method()); |
| 2194 |
#if HAVE_TLSv1_2 |
| 2195 |
else if (proto_version == PY_SSL_VERSION_TLS1_1) |
| 2196 |
ctx = SSL_CTX_new(TLSv1_1_method()); |
| 2197 |
else if (proto_version == PY_SSL_VERSION_TLS1_2) |
| 2198 |
ctx = SSL_CTX_new(TLSv1_2_method()); |
| 2199 |
#endif |
| 2200 |
#ifndef OPENSSL_NO_SSL3 |
| 2201 |
else if (proto_version == PY_SSL_VERSION_SSL3) |
| 2202 |
ctx = SSL_CTX_new(SSLv3_method()); |
| 2203 |
#endif |
| 2204 |
#ifndef OPENSSL_NO_SSL2 |
| 2205 |
else if (proto_version == PY_SSL_VERSION_SSL2) |
| 2206 |
ctx = SSL_CTX_new(SSLv2_method()); |
| 2207 |
#endif |
| 2208 |
else if (proto_version == PY_SSL_VERSION_TLS) |
| 2209 |
ctx = SSL_CTX_new(TLS_method()); |
| 2210 |
else |
| 2211 |
proto_version = -1; |
| 2212 |
PySSL_END_ALLOW_THREADS |
| 2213 |
|
| 2214 |
if (proto_version == -1) { |
| 2215 |
PyErr_SetString(PyExc_ValueError, |
| 2216 |
"invalid protocol version"); |
| 2217 |
return NULL; |
| 2218 |
} |
| 2219 |
if (ctx == NULL) { |
| 2220 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2221 |
return NULL; |
| 2222 |
} |
| 2223 |
|
| 2224 |
assert(type != NULL && type->tp_alloc != NULL); |
| 2225 |
self = (PySSLContext *) type->tp_alloc(type, 0); |
| 2226 |
if (self == NULL) { |
| 2227 |
SSL_CTX_free(ctx); |
| 2228 |
return NULL; |
| 2229 |
} |
| 2230 |
self->ctx = ctx; |
| 2231 |
#if HAVE_NPN |
| 2232 |
self->npn_protocols = NULL; |
| 2233 |
#endif |
| 2234 |
#if HAVE_ALPN |
| 2235 |
self->alpn_protocols = NULL; |
| 2236 |
#endif |
| 2237 |
#ifndef OPENSSL_NO_TLSEXT |
| 2238 |
self->set_hostname = NULL; |
| 2239 |
#endif |
| 2240 |
|
| 2241 |
self->check_hostname = 0; |
| 2242 |
|
| 2243 |
SSL_CTX_set_verify(self->ctx, SSL_VERIFY_NONE, NULL); |
| 2244 |
options = SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; |
| 2245 |
if (proto_version != PY_SSL_VERSION_SSL2) |
| 2246 |
options |= SSL_OP_NO_SSLv2; |
| 2247 |
if (proto_version != PY_SSL_VERSION_SSL3) |
| 2248 |
options |= SSL_OP_NO_SSLv3; |
| 2249 |
|
| 2250 |
|
| 2251 |
#ifdef SSL_OP_NO_COMPRESSION |
| 2252 |
options |= SSL_OP_NO_COMPRESSION; |
| 2253 |
#endif |
| 2254 |
#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
| 2255 |
options |= SSL_OP_CIPHER_SERVER_PREFERENCE; |
| 2256 |
#endif |
| 2257 |
#ifdef SSL_OP_SINGLE_DH_USE |
| 2258 |
options |= SSL_OP_SINGLE_DH_USE; |
| 2259 |
#endif |
| 2260 |
#ifdef SSL_OP_SINGLE_ECDH_USE |
| 2261 |
options |= SSL_OP_SINGLE_ECDH_USE; |
| 2262 |
#endif |
| 2263 |
SSL_CTX_set_options(self->ctx, options); |
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
if (proto_version != PY_SSL_VERSION_SSL2) { |
| 2268 |
result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL:!MD5"); |
| 2269 |
} else { |
| 2270 |
|
| 2271 |
result = SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL:!eNULL"); |
| 2272 |
} |
| 2273 |
if (result == 0) { |
| 2274 |
Py_DECREF(self); |
| 2275 |
ERR_clear_error(); |
| 2276 |
PyErr_SetString(PySSLErrorObject, |
| 2277 |
"No cipher can be selected."); |
| 2278 |
return NULL; |
| 2279 |
} |
| 2280 |
|
| 2281 |
#if !defined(OPENSSL_NO_ECDH) && !defined(OPENSSL_VERSION_1_1) |
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
#if defined(SSL_CTX_set_ecdh_auto) |
| 2287 |
SSL_CTX_set_ecdh_auto(self->ctx, 1); |
| 2288 |
#else |
| 2289 |
{ |
| 2290 |
EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 2291 |
SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 2292 |
EC_KEY_free(key); |
| 2293 |
} |
| 2294 |
#endif |
| 2295 |
#endif |
| 2296 |
|
| 2297 |
#define SID_CTX "Python" |
| 2298 |
SSL_CTX_set_session_id_context(self->ctx, (const unsigned char *) SID_CTX, |
| 2299 |
sizeof(SID_CTX)); |
| 2300 |
#undef SID_CTX |
| 2301 |
|
| 2302 |
#ifdef X509_V_FLAG_TRUSTED_FIRST |
| 2303 |
{ |
| 2304 |
|
| 2305 |
|
| 2306 |
X509_STORE *store = SSL_CTX_get_cert_store(self->ctx); |
| 2307 |
X509_STORE_set_flags(store, X509_V_FLAG_TRUSTED_FIRST); |
| 2308 |
} |
| 2309 |
#endif |
| 2310 |
|
| 2311 |
return (PyObject *)self; |
| 2312 |
} |
| 2313 |
|
| 2314 |
static int |
| 2315 |
context_traverse(PySSLContext *self, visitproc visit, void *arg) |
| 2316 |
{ |
| 2317 |
#ifndef OPENSSL_NO_TLSEXT |
| 2318 |
Py_VISIT(self->set_hostname); |
| 2319 |
#endif |
| 2320 |
return 0; |
| 2321 |
} |
| 2322 |
|
| 2323 |
static int |
| 2324 |
context_clear(PySSLContext *self) |
| 2325 |
{ |
| 2326 |
#ifndef OPENSSL_NO_TLSEXT |
| 2327 |
Py_CLEAR(self->set_hostname); |
| 2328 |
#endif |
| 2329 |
return 0; |
| 2330 |
} |
| 2331 |
|
| 2332 |
static void |
| 2333 |
context_dealloc(PySSLContext *self) |
| 2334 |
{ |
| 2335 |
|
| 2336 |
PyObject_GC_UnTrack(self); |
| 2337 |
context_clear(self); |
| 2338 |
SSL_CTX_free(self->ctx); |
| 2339 |
#if HAVE_NPN |
| 2340 |
PyMem_FREE(self->npn_protocols); |
| 2341 |
#endif |
| 2342 |
#if HAVE_ALPN |
| 2343 |
PyMem_FREE(self->alpn_protocols); |
| 2344 |
#endif |
| 2345 |
Py_TYPE(self)->tp_free(self); |
| 2346 |
} |
| 2347 |
|
| 2348 |
static PyObject * |
| 2349 |
set_ciphers(PySSLContext *self, PyObject *args) |
| 2350 |
{ |
| 2351 |
int ret; |
| 2352 |
const char *cipherlist; |
| 2353 |
|
| 2354 |
if (!PyArg_ParseTuple(args, "s:set_ciphers", &cipherlist)) |
| 2355 |
return NULL; |
| 2356 |
ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); |
| 2357 |
if (ret == 0) { |
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
ERR_clear_error(); |
| 2362 |
PyErr_SetString(PySSLErrorObject, |
| 2363 |
"No cipher can be selected."); |
| 2364 |
return NULL; |
| 2365 |
} |
| 2366 |
Py_RETURN_NONE; |
| 2367 |
} |
| 2368 |
|
| 2369 |
#if HAVE_NPN || HAVE_ALPN |
| 2370 |
static int |
| 2371 |
do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen, |
| 2372 |
const unsigned char *server_protocols, unsigned int server_protocols_len, |
| 2373 |
const unsigned char *client_protocols, unsigned int client_protocols_len) |
| 2374 |
{ |
| 2375 |
int ret; |
| 2376 |
if (client_protocols == NULL) { |
| 2377 |
client_protocols = (unsigned char *)""; |
| 2378 |
client_protocols_len = 0; |
| 2379 |
} |
| 2380 |
if (server_protocols == NULL) { |
| 2381 |
server_protocols = (unsigned char *)""; |
| 2382 |
server_protocols_len = 0; |
| 2383 |
} |
| 2384 |
|
| 2385 |
ret = SSL_select_next_proto(out, outlen, |
| 2386 |
server_protocols, server_protocols_len, |
| 2387 |
client_protocols, client_protocols_len); |
| 2388 |
if (alpn && ret != OPENSSL_NPN_NEGOTIATED) |
| 2389 |
return SSL_TLSEXT_ERR_NOACK; |
| 2390 |
|
| 2391 |
return SSL_TLSEXT_ERR_OK; |
| 2392 |
} |
| 2393 |
#endif |
| 2394 |
|
| 2395 |
#if HAVE_NPN |
| 2396 |
|
| 2397 |
static int |
| 2398 |
_advertiseNPN_cb(SSL *s, |
| 2399 |
const unsigned char **data, unsigned int *len, |
| 2400 |
void *args) |
| 2401 |
{ |
| 2402 |
PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 2403 |
|
| 2404 |
if (ssl_ctx->npn_protocols == NULL) { |
| 2405 |
*data = (unsigned char *)""; |
| 2406 |
*len = 0; |
| 2407 |
} else { |
| 2408 |
*data = ssl_ctx->npn_protocols; |
| 2409 |
*len = ssl_ctx->npn_protocols_len; |
| 2410 |
} |
| 2411 |
|
| 2412 |
return SSL_TLSEXT_ERR_OK; |
| 2413 |
} |
| 2414 |
|
| 2415 |
static int |
| 2416 |
_selectNPN_cb(SSL *s, |
| 2417 |
unsigned char **out, unsigned char *outlen, |
| 2418 |
const unsigned char *server, unsigned int server_len, |
| 2419 |
void *args) |
| 2420 |
{ |
| 2421 |
PySSLContext *ctx = (PySSLContext *)args; |
| 2422 |
return do_protocol_selection(0, out, outlen, server, server_len, |
| 2423 |
ctx->npn_protocols, ctx->npn_protocols_len); |
| 2424 |
} |
| 2425 |
#endif |
| 2426 |
|
| 2427 |
static PyObject * |
| 2428 |
_set_npn_protocols(PySSLContext *self, PyObject *args) |
| 2429 |
{ |
| 2430 |
#if HAVE_NPN |
| 2431 |
Py_buffer protos; |
| 2432 |
|
| 2433 |
if (!PyArg_ParseTuple(args, "s*:set_npn_protocols", &protos)) |
| 2434 |
return NULL; |
| 2435 |
|
| 2436 |
if (self->npn_protocols != NULL) { |
| 2437 |
PyMem_Free(self->npn_protocols); |
| 2438 |
} |
| 2439 |
|
| 2440 |
self->npn_protocols = PyMem_Malloc(protos.len); |
| 2441 |
if (self->npn_protocols == NULL) { |
| 2442 |
PyBuffer_Release(&protos); |
| 2443 |
return PyErr_NoMemory(); |
| 2444 |
} |
| 2445 |
memcpy(self->npn_protocols, protos.buf, protos.len); |
| 2446 |
self->npn_protocols_len = (int) protos.len; |
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
SSL_CTX_set_next_protos_advertised_cb(self->ctx, |
| 2451 |
_advertiseNPN_cb, |
| 2452 |
self); |
| 2453 |
SSL_CTX_set_next_proto_select_cb(self->ctx, |
| 2454 |
_selectNPN_cb, |
| 2455 |
self); |
| 2456 |
|
| 2457 |
PyBuffer_Release(&protos); |
| 2458 |
Py_RETURN_NONE; |
| 2459 |
#else |
| 2460 |
PyErr_SetString(PyExc_NotImplementedError, |
| 2461 |
"The NPN extension requires OpenSSL 1.0.1 or later."); |
| 2462 |
return NULL; |
| 2463 |
#endif |
| 2464 |
} |
| 2465 |
|
| 2466 |
#if HAVE_ALPN |
| 2467 |
static int |
| 2468 |
_selectALPN_cb(SSL *s, |
| 2469 |
const unsigned char **out, unsigned char *outlen, |
| 2470 |
const unsigned char *client_protocols, unsigned int client_protocols_len, |
| 2471 |
void *args) |
| 2472 |
{ |
| 2473 |
PySSLContext *ctx = (PySSLContext *)args; |
| 2474 |
return do_protocol_selection(1, (unsigned char **)out, outlen, |
| 2475 |
ctx->alpn_protocols, ctx->alpn_protocols_len, |
| 2476 |
client_protocols, client_protocols_len); |
| 2477 |
} |
| 2478 |
#endif |
| 2479 |
|
| 2480 |
static PyObject * |
| 2481 |
_set_alpn_protocols(PySSLContext *self, PyObject *args) |
| 2482 |
{ |
| 2483 |
#if HAVE_ALPN |
| 2484 |
Py_buffer protos; |
| 2485 |
|
| 2486 |
if (!PyArg_ParseTuple(args, "s*:set_npn_protocols", &protos)) |
| 2487 |
return NULL; |
| 2488 |
|
| 2489 |
PyMem_FREE(self->alpn_protocols); |
| 2490 |
self->alpn_protocols = PyMem_Malloc(protos.len); |
| 2491 |
if (!self->alpn_protocols) |
| 2492 |
return PyErr_NoMemory(); |
| 2493 |
memcpy(self->alpn_protocols, protos.buf, protos.len); |
| 2494 |
self->alpn_protocols_len = protos.len; |
| 2495 |
PyBuffer_Release(&protos); |
| 2496 |
|
| 2497 |
if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len)) |
| 2498 |
return PyErr_NoMemory(); |
| 2499 |
SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self); |
| 2500 |
|
| 2501 |
PyBuffer_Release(&protos); |
| 2502 |
Py_RETURN_NONE; |
| 2503 |
#else |
| 2504 |
PyErr_SetString(PyExc_NotImplementedError, |
| 2505 |
"The ALPN extension requires OpenSSL 1.0.2 or later."); |
| 2506 |
return NULL; |
| 2507 |
#endif |
| 2508 |
} |
| 2509 |
|
| 2510 |
static PyObject * |
| 2511 |
get_verify_mode(PySSLContext *self, void *c) |
| 2512 |
{ |
| 2513 |
switch (SSL_CTX_get_verify_mode(self->ctx)) { |
| 2514 |
case SSL_VERIFY_NONE: |
| 2515 |
return PyLong_FromLong(PY_SSL_CERT_NONE); |
| 2516 |
case SSL_VERIFY_PEER: |
| 2517 |
return PyLong_FromLong(PY_SSL_CERT_OPTIONAL); |
| 2518 |
case SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT: |
| 2519 |
return PyLong_FromLong(PY_SSL_CERT_REQUIRED); |
| 2520 |
} |
| 2521 |
PyErr_SetString(PySSLErrorObject, |
| 2522 |
"invalid return value from SSL_CTX_get_verify_mode"); |
| 2523 |
return NULL; |
| 2524 |
} |
| 2525 |
|
| 2526 |
static int |
| 2527 |
set_verify_mode(PySSLContext *self, PyObject *arg, void *c) |
| 2528 |
{ |
| 2529 |
int n, mode; |
| 2530 |
if (!PyArg_Parse(arg, "i", &n)) |
| 2531 |
return -1; |
| 2532 |
if (n == PY_SSL_CERT_NONE) |
| 2533 |
mode = SSL_VERIFY_NONE; |
| 2534 |
else if (n == PY_SSL_CERT_OPTIONAL) |
| 2535 |
mode = SSL_VERIFY_PEER; |
| 2536 |
else if (n == PY_SSL_CERT_REQUIRED) |
| 2537 |
mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2538 |
else { |
| 2539 |
PyErr_SetString(PyExc_ValueError, |
| 2540 |
"invalid value for verify_mode"); |
| 2541 |
return -1; |
| 2542 |
} |
| 2543 |
if (mode == SSL_VERIFY_NONE && self->check_hostname) { |
| 2544 |
PyErr_SetString(PyExc_ValueError, |
| 2545 |
"Cannot set verify_mode to CERT_NONE when " |
| 2546 |
"check_hostname is enabled."); |
| 2547 |
return -1; |
| 2548 |
} |
| 2549 |
SSL_CTX_set_verify(self->ctx, mode, NULL); |
| 2550 |
return 0; |
| 2551 |
} |
| 2552 |
|
| 2553 |
#ifdef HAVE_OPENSSL_VERIFY_PARAM |
| 2554 |
static PyObject * |
| 2555 |
get_verify_flags(PySSLContext *self, void *c) |
| 2556 |
{ |
| 2557 |
X509_STORE *store; |
| 2558 |
X509_VERIFY_PARAM *param; |
| 2559 |
unsigned long flags; |
| 2560 |
|
| 2561 |
store = SSL_CTX_get_cert_store(self->ctx); |
| 2562 |
param = X509_STORE_get0_param(store); |
| 2563 |
flags = X509_VERIFY_PARAM_get_flags(param); |
| 2564 |
return PyLong_FromUnsignedLong(flags); |
| 2565 |
} |
| 2566 |
|
| 2567 |
static int |
| 2568 |
set_verify_flags(PySSLContext *self, PyObject *arg, void *c) |
| 2569 |
{ |
| 2570 |
X509_STORE *store; |
| 2571 |
X509_VERIFY_PARAM *param; |
| 2572 |
unsigned long new_flags, flags, set, clear; |
| 2573 |
|
| 2574 |
if (!PyArg_Parse(arg, "k", &new_flags)) |
| 2575 |
return -1; |
| 2576 |
store = SSL_CTX_get_cert_store(self->ctx); |
| 2577 |
param = X509_STORE_get0_param(store); |
| 2578 |
flags = X509_VERIFY_PARAM_get_flags(param); |
| 2579 |
clear = flags & ~new_flags; |
| 2580 |
set = ~flags & new_flags; |
| 2581 |
if (clear) { |
| 2582 |
if (!X509_VERIFY_PARAM_clear_flags(param, clear)) { |
| 2583 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2584 |
return -1; |
| 2585 |
} |
| 2586 |
} |
| 2587 |
if (set) { |
| 2588 |
if (!X509_VERIFY_PARAM_set_flags(param, set)) { |
| 2589 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2590 |
return -1; |
| 2591 |
} |
| 2592 |
} |
| 2593 |
return 0; |
| 2594 |
} |
| 2595 |
#endif |
| 2596 |
|
| 2597 |
static PyObject * |
| 2598 |
get_options(PySSLContext *self, void *c) |
| 2599 |
{ |
| 2600 |
return PyLong_FromLong(SSL_CTX_get_options(self->ctx)); |
| 2601 |
} |
| 2602 |
|
| 2603 |
static int |
| 2604 |
set_options(PySSLContext *self, PyObject *arg, void *c) |
| 2605 |
{ |
| 2606 |
long new_opts, opts, set, clear; |
| 2607 |
if (!PyArg_Parse(arg, "l", &new_opts)) |
| 2608 |
return -1; |
| 2609 |
opts = SSL_CTX_get_options(self->ctx); |
| 2610 |
clear = opts & ~new_opts; |
| 2611 |
set = ~opts & new_opts; |
| 2612 |
if (clear) { |
| 2613 |
#ifdef HAVE_SSL_CTX_CLEAR_OPTIONS |
| 2614 |
SSL_CTX_clear_options(self->ctx, clear); |
| 2615 |
#else |
| 2616 |
PyErr_SetString(PyExc_ValueError, |
| 2617 |
"can't clear options before OpenSSL 0.9.8m"); |
| 2618 |
return -1; |
| 2619 |
#endif |
| 2620 |
} |
| 2621 |
if (set) |
| 2622 |
SSL_CTX_set_options(self->ctx, set); |
| 2623 |
return 0; |
| 2624 |
} |
| 2625 |
|
| 2626 |
static PyObject * |
| 2627 |
get_check_hostname(PySSLContext *self, void *c) |
| 2628 |
{ |
| 2629 |
return PyBool_FromLong(self->check_hostname); |
| 2630 |
} |
| 2631 |
|
| 2632 |
static int |
| 2633 |
set_check_hostname(PySSLContext *self, PyObject *arg, void *c) |
| 2634 |
{ |
| 2635 |
PyObject *py_check_hostname; |
| 2636 |
int check_hostname; |
| 2637 |
if (!PyArg_Parse(arg, "O", &py_check_hostname)) |
| 2638 |
return -1; |
| 2639 |
|
| 2640 |
check_hostname = PyObject_IsTrue(py_check_hostname); |
| 2641 |
if (check_hostname < 0) |
| 2642 |
return -1; |
| 2643 |
if (check_hostname && |
| 2644 |
SSL_CTX_get_verify_mode(self->ctx) == SSL_VERIFY_NONE) { |
| 2645 |
PyErr_SetString(PyExc_ValueError, |
| 2646 |
"check_hostname needs a SSL context with either " |
| 2647 |
"CERT_OPTIONAL or CERT_REQUIRED"); |
| 2648 |
return -1; |
| 2649 |
} |
| 2650 |
self->check_hostname = check_hostname; |
| 2651 |
return 0; |
| 2652 |
} |
| 2653 |
|
| 2654 |
|
| 2655 |
typedef struct { |
| 2656 |
PyThreadState *thread_state; |
| 2657 |
PyObject *callable; |
| 2658 |
char *password; |
| 2659 |
int size; |
| 2660 |
int error; |
| 2661 |
} _PySSLPasswordInfo; |
| 2662 |
|
| 2663 |
static int |
| 2664 |
_pwinfo_set(_PySSLPasswordInfo *pw_info, PyObject* password, |
| 2665 |
const char *bad_type_error) |
| 2666 |
{ |
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
PyObject *password_bytes = NULL; |
| 2672 |
const char *data = NULL; |
| 2673 |
Py_ssize_t size; |
| 2674 |
|
| 2675 |
if (PyUnicode_Check(password)) { |
| 2676 |
password_bytes = PyUnicode_AsEncodedString(password, NULL, NULL); |
| 2677 |
if (!password_bytes) { |
| 2678 |
goto error; |
| 2679 |
} |
| 2680 |
data = PyBytes_AS_STRING(password_bytes); |
| 2681 |
size = PyBytes_GET_SIZE(password_bytes); |
| 2682 |
} else if (PyBytes_Check(password)) { |
| 2683 |
data = PyBytes_AS_STRING(password); |
| 2684 |
size = PyBytes_GET_SIZE(password); |
| 2685 |
} else if (PyByteArray_Check(password)) { |
| 2686 |
data = PyByteArray_AS_STRING(password); |
| 2687 |
size = PyByteArray_GET_SIZE(password); |
| 2688 |
} else { |
| 2689 |
PyErr_SetString(PyExc_TypeError, bad_type_error); |
| 2690 |
goto error; |
| 2691 |
} |
| 2692 |
|
| 2693 |
if (size > (Py_ssize_t)INT_MAX) { |
| 2694 |
PyErr_Format(PyExc_ValueError, |
| 2695 |
"password cannot be longer than %d bytes", INT_MAX); |
| 2696 |
goto error; |
| 2697 |
} |
| 2698 |
|
| 2699 |
PyMem_Free(pw_info->password); |
| 2700 |
pw_info->password = PyMem_Malloc(size); |
| 2701 |
if (!pw_info->password) { |
| 2702 |
PyErr_SetString(PyExc_MemoryError, |
| 2703 |
"unable to allocate password buffer"); |
| 2704 |
goto error; |
| 2705 |
} |
| 2706 |
memcpy(pw_info->password, data, size); |
| 2707 |
pw_info->size = (int)size; |
| 2708 |
|
| 2709 |
Py_XDECREF(password_bytes); |
| 2710 |
return 1; |
| 2711 |
|
| 2712 |
error: |
| 2713 |
Py_XDECREF(password_bytes); |
| 2714 |
return 0; |
| 2715 |
} |
| 2716 |
|
| 2717 |
static int |
| 2718 |
_password_callback(char *buf, int size, int rwflag, void *userdata) |
| 2719 |
{ |
| 2720 |
_PySSLPasswordInfo *pw_info = (_PySSLPasswordInfo*) userdata; |
| 2721 |
PyObject *fn_ret = NULL; |
| 2722 |
|
| 2723 |
PySSL_END_ALLOW_THREADS_S(pw_info->thread_state); |
| 2724 |
|
| 2725 |
if (pw_info->callable) { |
| 2726 |
fn_ret = PyObject_CallFunctionObjArgs(pw_info->callable, NULL); |
| 2727 |
if (!fn_ret) { |
| 2728 |
|
| 2729 |
|
| 2730 |
goto error; |
| 2731 |
} |
| 2732 |
|
| 2733 |
if (!_pwinfo_set(pw_info, fn_ret, |
| 2734 |
"password callback must return a string")) { |
| 2735 |
goto error; |
| 2736 |
} |
| 2737 |
Py_CLEAR(fn_ret); |
| 2738 |
} |
| 2739 |
|
| 2740 |
if (pw_info->size > size) { |
| 2741 |
PyErr_Format(PyExc_ValueError, |
| 2742 |
"password cannot be longer than %d bytes", size); |
| 2743 |
goto error; |
| 2744 |
} |
| 2745 |
|
| 2746 |
PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 2747 |
memcpy(buf, pw_info->password, pw_info->size); |
| 2748 |
return pw_info->size; |
| 2749 |
|
| 2750 |
error: |
| 2751 |
Py_XDECREF(fn_ret); |
| 2752 |
PySSL_BEGIN_ALLOW_THREADS_S(pw_info->thread_state); |
| 2753 |
pw_info->error = 1; |
| 2754 |
return -1; |
| 2755 |
} |
| 2756 |
|
| 2757 |
static PyObject * |
| 2758 |
load_cert_chain(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 2759 |
{ |
| 2760 |
char *kwlist[] = {"certfile", "keyfile", "password", NULL}; |
| 2761 |
PyObject *keyfile = NULL, *keyfile_bytes = NULL, *password = NULL; |
| 2762 |
char *certfile_bytes = NULL; |
| 2763 |
pem_password_cb *orig_passwd_cb = SSL_CTX_get_default_passwd_cb(self->ctx); |
| 2764 |
void *orig_passwd_userdata = SSL_CTX_get_default_passwd_cb_userdata(self->ctx); |
| 2765 |
_PySSLPasswordInfo pw_info = { NULL, NULL, NULL, 0, 0 }; |
| 2766 |
int r; |
| 2767 |
|
| 2768 |
errno = 0; |
| 2769 |
ERR_clear_error(); |
| 2770 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2771 |
"et|OO:load_cert_chain", kwlist, |
| 2772 |
Py_FileSystemDefaultEncoding, &certfile_bytes, |
| 2773 |
&keyfile, &password)) |
| 2774 |
return NULL; |
| 2775 |
|
| 2776 |
if (keyfile && keyfile != Py_None) { |
| 2777 |
if (PyString_Check(keyfile)) { |
| 2778 |
Py_INCREF(keyfile); |
| 2779 |
keyfile_bytes = keyfile; |
| 2780 |
} else { |
| 2781 |
PyObject *u = PyUnicode_FromObject(keyfile); |
| 2782 |
if (!u) |
| 2783 |
goto error; |
| 2784 |
keyfile_bytes = PyUnicode_AsEncodedString( |
| 2785 |
u, Py_FileSystemDefaultEncoding, NULL); |
| 2786 |
Py_DECREF(u); |
| 2787 |
if (!keyfile_bytes) |
| 2788 |
goto error; |
| 2789 |
} |
| 2790 |
} |
| 2791 |
|
| 2792 |
if (password && password != Py_None) { |
| 2793 |
if (PyCallable_Check(password)) { |
| 2794 |
pw_info.callable = password; |
| 2795 |
} else if (!_pwinfo_set(&pw_info, password, |
| 2796 |
"password should be a string or callable")) { |
| 2797 |
goto error; |
| 2798 |
} |
| 2799 |
SSL_CTX_set_default_passwd_cb(self->ctx, _password_callback); |
| 2800 |
SSL_CTX_set_default_passwd_cb_userdata(self->ctx, &pw_info); |
| 2801 |
} |
| 2802 |
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
| 2803 |
r = SSL_CTX_use_certificate_chain_file(self->ctx, certfile_bytes); |
| 2804 |
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 2805 |
if (r != 1) { |
| 2806 |
if (pw_info.error) { |
| 2807 |
ERR_clear_error(); |
| 2808 |
|
| 2809 |
} |
| 2810 |
else if (errno != 0) { |
| 2811 |
ERR_clear_error(); |
| 2812 |
PyErr_SetFromErrno(PyExc_IOError); |
| 2813 |
} |
| 2814 |
else { |
| 2815 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2816 |
} |
| 2817 |
goto error; |
| 2818 |
} |
| 2819 |
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
| 2820 |
r = SSL_CTX_use_PrivateKey_file(self->ctx, |
| 2821 |
keyfile_bytes ? PyBytes_AS_STRING(keyfile_bytes) : certfile_bytes, |
| 2822 |
SSL_FILETYPE_PEM); |
| 2823 |
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 2824 |
if (r != 1) { |
| 2825 |
if (pw_info.error) { |
| 2826 |
ERR_clear_error(); |
| 2827 |
|
| 2828 |
} |
| 2829 |
else if (errno != 0) { |
| 2830 |
ERR_clear_error(); |
| 2831 |
PyErr_SetFromErrno(PyExc_IOError); |
| 2832 |
} |
| 2833 |
else { |
| 2834 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2835 |
} |
| 2836 |
goto error; |
| 2837 |
} |
| 2838 |
PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); |
| 2839 |
r = SSL_CTX_check_private_key(self->ctx); |
| 2840 |
PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); |
| 2841 |
if (r != 1) { |
| 2842 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2843 |
goto error; |
| 2844 |
} |
| 2845 |
SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 2846 |
SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
| 2847 |
Py_XDECREF(keyfile_bytes); |
| 2848 |
PyMem_Free(pw_info.password); |
| 2849 |
PyMem_Free(certfile_bytes); |
| 2850 |
Py_RETURN_NONE; |
| 2851 |
|
| 2852 |
error: |
| 2853 |
SSL_CTX_set_default_passwd_cb(self->ctx, orig_passwd_cb); |
| 2854 |
SSL_CTX_set_default_passwd_cb_userdata(self->ctx, orig_passwd_userdata); |
| 2855 |
Py_XDECREF(keyfile_bytes); |
| 2856 |
PyMem_Free(pw_info.password); |
| 2857 |
PyMem_Free(certfile_bytes); |
| 2858 |
return NULL; |
| 2859 |
} |
| 2860 |
|
| 2861 |
|
| 2862 |
|
| 2863 |
static int |
| 2864 |
_add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len, |
| 2865 |
int filetype) |
| 2866 |
{ |
| 2867 |
BIO *biobuf = NULL; |
| 2868 |
X509_STORE *store; |
| 2869 |
int retval = 0, err, loaded = 0; |
| 2870 |
|
| 2871 |
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM); |
| 2872 |
|
| 2873 |
if (len <= 0) { |
| 2874 |
PyErr_SetString(PyExc_ValueError, |
| 2875 |
"Empty certificate data"); |
| 2876 |
return -1; |
| 2877 |
} else if (len > INT_MAX) { |
| 2878 |
PyErr_SetString(PyExc_OverflowError, |
| 2879 |
"Certificate data is too long."); |
| 2880 |
return -1; |
| 2881 |
} |
| 2882 |
|
| 2883 |
biobuf = BIO_new_mem_buf(data, (int)len); |
| 2884 |
if (biobuf == NULL) { |
| 2885 |
_setSSLError("Can't allocate buffer", 0, __FILE__, __LINE__); |
| 2886 |
return -1; |
| 2887 |
} |
| 2888 |
|
| 2889 |
store = SSL_CTX_get_cert_store(self->ctx); |
| 2890 |
assert(store != NULL); |
| 2891 |
|
| 2892 |
while (1) { |
| 2893 |
X509 *cert = NULL; |
| 2894 |
int r; |
| 2895 |
|
| 2896 |
if (filetype == SSL_FILETYPE_ASN1) { |
| 2897 |
cert = d2i_X509_bio(biobuf, NULL); |
| 2898 |
} else { |
| 2899 |
cert = PEM_read_bio_X509(biobuf, NULL, |
| 2900 |
SSL_CTX_get_default_passwd_cb(self->ctx), |
| 2901 |
SSL_CTX_get_default_passwd_cb_userdata(self->ctx) |
| 2902 |
); |
| 2903 |
} |
| 2904 |
if (cert == NULL) { |
| 2905 |
break; |
| 2906 |
} |
| 2907 |
r = X509_STORE_add_cert(store, cert); |
| 2908 |
X509_free(cert); |
| 2909 |
if (!r) { |
| 2910 |
err = ERR_peek_last_error(); |
| 2911 |
if ((ERR_GET_LIB(err) == ERR_LIB_X509) && |
| 2912 |
(ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 2913 |
|
| 2914 |
ERR_clear_error(); |
| 2915 |
} else { |
| 2916 |
break; |
| 2917 |
} |
| 2918 |
} |
| 2919 |
loaded++; |
| 2920 |
} |
| 2921 |
|
| 2922 |
err = ERR_peek_last_error(); |
| 2923 |
if ((filetype == SSL_FILETYPE_ASN1) && |
| 2924 |
(loaded > 0) && |
| 2925 |
(ERR_GET_LIB(err) == ERR_LIB_ASN1) && |
| 2926 |
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) { |
| 2927 |
|
| 2928 |
ERR_clear_error(); |
| 2929 |
retval = 0; |
| 2930 |
} else if ((filetype == SSL_FILETYPE_PEM) && |
| 2931 |
(loaded > 0) && |
| 2932 |
(ERR_GET_LIB(err) == ERR_LIB_PEM) && |
| 2933 |
(ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 2934 |
|
| 2935 |
ERR_clear_error(); |
| 2936 |
retval = 0; |
| 2937 |
} else { |
| 2938 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 2939 |
retval = -1; |
| 2940 |
} |
| 2941 |
|
| 2942 |
BIO_free(biobuf); |
| 2943 |
return retval; |
| 2944 |
} |
| 2945 |
|
| 2946 |
|
| 2947 |
static PyObject * |
| 2948 |
load_verify_locations(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 2949 |
{ |
| 2950 |
char *kwlist[] = {"cafile", "capath", "cadata", NULL}; |
| 2951 |
PyObject *cadata = NULL, *cafile = NULL, *capath = NULL; |
| 2952 |
PyObject *cafile_bytes = NULL, *capath_bytes = NULL; |
| 2953 |
const char *cafile_buf = NULL, *capath_buf = NULL; |
| 2954 |
int r = 0, ok = 1; |
| 2955 |
|
| 2956 |
errno = 0; |
| 2957 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2958 |
"|OOO:load_verify_locations", kwlist, |
| 2959 |
&cafile, &capath, &cadata)) |
| 2960 |
return NULL; |
| 2961 |
|
| 2962 |
if (cafile == Py_None) |
| 2963 |
cafile = NULL; |
| 2964 |
if (capath == Py_None) |
| 2965 |
capath = NULL; |
| 2966 |
if (cadata == Py_None) |
| 2967 |
cadata = NULL; |
| 2968 |
|
| 2969 |
if (cafile == NULL && capath == NULL && cadata == NULL) { |
| 2970 |
PyErr_SetString(PyExc_TypeError, |
| 2971 |
"cafile, capath and cadata cannot be all omitted"); |
| 2972 |
goto error; |
| 2973 |
} |
| 2974 |
|
| 2975 |
if (cafile) { |
| 2976 |
if (PyString_Check(cafile)) { |
| 2977 |
Py_INCREF(cafile); |
| 2978 |
cafile_bytes = cafile; |
| 2979 |
} else { |
| 2980 |
PyObject *u = PyUnicode_FromObject(cafile); |
| 2981 |
if (!u) |
| 2982 |
goto error; |
| 2983 |
cafile_bytes = PyUnicode_AsEncodedString( |
| 2984 |
u, Py_FileSystemDefaultEncoding, NULL); |
| 2985 |
Py_DECREF(u); |
| 2986 |
if (!cafile_bytes) |
| 2987 |
goto error; |
| 2988 |
} |
| 2989 |
} |
| 2990 |
if (capath) { |
| 2991 |
if (PyString_Check(capath)) { |
| 2992 |
Py_INCREF(capath); |
| 2993 |
capath_bytes = capath; |
| 2994 |
} else { |
| 2995 |
PyObject *u = PyUnicode_FromObject(capath); |
| 2996 |
if (!u) |
| 2997 |
goto error; |
| 2998 |
capath_bytes = PyUnicode_AsEncodedString( |
| 2999 |
u, Py_FileSystemDefaultEncoding, NULL); |
| 3000 |
Py_DECREF(u); |
| 3001 |
if (!capath_bytes) |
| 3002 |
goto error; |
| 3003 |
} |
| 3004 |
} |
| 3005 |
|
| 3006 |
|
| 3007 |
if (cadata) { |
| 3008 |
Py_buffer buf; |
| 3009 |
PyObject *cadata_ascii = NULL; |
| 3010 |
|
| 3011 |
if (!PyUnicode_Check(cadata) && PyObject_GetBuffer(cadata, &buf, PyBUF_SIMPLE) == 0) { |
| 3012 |
if (!PyBuffer_IsContiguous(&buf, 'C') || buf.ndim > 1) { |
| 3013 |
PyBuffer_Release(&buf); |
| 3014 |
PyErr_SetString(PyExc_TypeError, |
| 3015 |
"cadata should be a contiguous buffer with " |
| 3016 |
"a single dimension"); |
| 3017 |
goto error; |
| 3018 |
} |
| 3019 |
r = _add_ca_certs(self, buf.buf, buf.len, SSL_FILETYPE_ASN1); |
| 3020 |
PyBuffer_Release(&buf); |
| 3021 |
if (r == -1) { |
| 3022 |
goto error; |
| 3023 |
} |
| 3024 |
} else { |
| 3025 |
PyErr_Clear(); |
| 3026 |
cadata_ascii = PyUnicode_AsASCIIString(cadata); |
| 3027 |
if (cadata_ascii == NULL) { |
| 3028 |
PyErr_SetString(PyExc_TypeError, |
| 3029 |
"cadata should be an ASCII string or a " |
| 3030 |
"bytes-like object"); |
| 3031 |
goto error; |
| 3032 |
} |
| 3033 |
r = _add_ca_certs(self, |
| 3034 |
PyBytes_AS_STRING(cadata_ascii), |
| 3035 |
PyBytes_GET_SIZE(cadata_ascii), |
| 3036 |
SSL_FILETYPE_PEM); |
| 3037 |
Py_DECREF(cadata_ascii); |
| 3038 |
if (r == -1) { |
| 3039 |
goto error; |
| 3040 |
} |
| 3041 |
} |
| 3042 |
} |
| 3043 |
|
| 3044 |
|
| 3045 |
if (cafile_bytes || capath_bytes) { |
| 3046 |
if (cafile) |
| 3047 |
cafile_buf = PyBytes_AS_STRING(cafile_bytes); |
| 3048 |
if (capath) |
| 3049 |
capath_buf = PyBytes_AS_STRING(capath_bytes); |
| 3050 |
PySSL_BEGIN_ALLOW_THREADS |
| 3051 |
r = SSL_CTX_load_verify_locations( |
| 3052 |
self->ctx, |
| 3053 |
cafile_buf, |
| 3054 |
capath_buf); |
| 3055 |
PySSL_END_ALLOW_THREADS |
| 3056 |
if (r != 1) { |
| 3057 |
ok = 0; |
| 3058 |
if (errno != 0) { |
| 3059 |
ERR_clear_error(); |
| 3060 |
PyErr_SetFromErrno(PyExc_IOError); |
| 3061 |
} |
| 3062 |
else { |
| 3063 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3064 |
} |
| 3065 |
goto error; |
| 3066 |
} |
| 3067 |
} |
| 3068 |
goto end; |
| 3069 |
|
| 3070 |
error: |
| 3071 |
ok = 0; |
| 3072 |
end: |
| 3073 |
Py_XDECREF(cafile_bytes); |
| 3074 |
Py_XDECREF(capath_bytes); |
| 3075 |
if (ok) { |
| 3076 |
Py_RETURN_NONE; |
| 3077 |
} else { |
| 3078 |
return NULL; |
| 3079 |
} |
| 3080 |
} |
| 3081 |
|
| 3082 |
static PyObject * |
| 3083 |
load_dh_params(PySSLContext *self, PyObject *filepath) |
| 3084 |
{ |
| 3085 |
BIO *bio; |
| 3086 |
DH *dh; |
| 3087 |
PyObject *filepath_bytes = NULL; |
| 3088 |
|
| 3089 |
if (PyString_Check(filepath)) { |
| 3090 |
Py_INCREF(filepath); |
| 3091 |
filepath_bytes = filepath; |
| 3092 |
} else { |
| 3093 |
PyObject *u = PyUnicode_FromObject(filepath); |
| 3094 |
if (!u) |
| 3095 |
return NULL; |
| 3096 |
filepath_bytes = PyUnicode_AsEncodedString( |
| 3097 |
u, Py_FileSystemDefaultEncoding, NULL); |
| 3098 |
Py_DECREF(u); |
| 3099 |
if (!filepath_bytes) |
| 3100 |
return NULL; |
| 3101 |
} |
| 3102 |
|
| 3103 |
bio = BIO_new_file(PyBytes_AS_STRING(filepath_bytes), "r"); |
| 3104 |
if (bio == NULL) { |
| 3105 |
Py_DECREF(filepath_bytes); |
| 3106 |
ERR_clear_error(); |
| 3107 |
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, filepath); |
| 3108 |
return NULL; |
| 3109 |
} |
| 3110 |
errno = 0; |
| 3111 |
PySSL_BEGIN_ALLOW_THREADS |
| 3112 |
dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 3113 |
BIO_free(bio); |
| 3114 |
Py_DECREF(filepath_bytes); |
| 3115 |
PySSL_END_ALLOW_THREADS |
| 3116 |
if (dh == NULL) { |
| 3117 |
if (errno != 0) { |
| 3118 |
ERR_clear_error(); |
| 3119 |
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); |
| 3120 |
} |
| 3121 |
else { |
| 3122 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3123 |
} |
| 3124 |
return NULL; |
| 3125 |
} |
| 3126 |
if (SSL_CTX_set_tmp_dh(self->ctx, dh) == 0) |
| 3127 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3128 |
DH_free(dh); |
| 3129 |
Py_RETURN_NONE; |
| 3130 |
} |
| 3131 |
|
| 3132 |
static PyObject * |
| 3133 |
context_wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 3134 |
{ |
| 3135 |
char *kwlist[] = {"sock", "server_side", "server_hostname", "ssl_sock", NULL}; |
| 3136 |
PySocketSockObject *sock; |
| 3137 |
int server_side = 0; |
| 3138 |
char *hostname = NULL; |
| 3139 |
PyObject *hostname_obj, *ssl_sock = Py_None, *res; |
| 3140 |
|
| 3141 |
|
| 3142 |
|
| 3143 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!i|O!O:_wrap_socket", kwlist, |
| 3144 |
PySocketModule.Sock_Type, |
| 3145 |
&sock, &server_side, |
| 3146 |
Py_TYPE(Py_None), &hostname_obj, |
| 3147 |
&ssl_sock)) { |
| 3148 |
PyErr_Clear(); |
| 3149 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!iet|O:_wrap_socket", kwlist, |
| 3150 |
PySocketModule.Sock_Type, |
| 3151 |
&sock, &server_side, |
| 3152 |
"idna", &hostname, &ssl_sock)) |
| 3153 |
return NULL; |
| 3154 |
} |
| 3155 |
|
| 3156 |
res = (PyObject *) newPySSLSocket(self, sock, server_side, |
| 3157 |
hostname, ssl_sock); |
| 3158 |
if (hostname != NULL) |
| 3159 |
PyMem_Free(hostname); |
| 3160 |
return res; |
| 3161 |
} |
| 3162 |
|
| 3163 |
static PyObject * |
| 3164 |
session_stats(PySSLContext *self, PyObject *unused) |
| 3165 |
{ |
| 3166 |
int r; |
| 3167 |
PyObject *value, *stats = PyDict_New(); |
| 3168 |
if (!stats) |
| 3169 |
return NULL; |
| 3170 |
|
| 3171 |
#define ADD_STATS(SSL_NAME, KEY_NAME) \ |
| 3172 |
value = PyLong_FromLong(SSL_CTX_sess_ ## SSL_NAME (self->ctx)); \ |
| 3173 |
if (value == NULL) \ |
| 3174 |
goto error; \ |
| 3175 |
r = PyDict_SetItemString(stats, KEY_NAME, value); \ |
| 3176 |
Py_DECREF(value); \ |
| 3177 |
if (r < 0) \ |
| 3178 |
goto error; |
| 3179 |
|
| 3180 |
ADD_STATS(number, "number"); |
| 3181 |
ADD_STATS(connect, "connect"); |
| 3182 |
ADD_STATS(connect_good, "connect_good"); |
| 3183 |
ADD_STATS(connect_renegotiate, "connect_renegotiate"); |
| 3184 |
ADD_STATS(accept, "accept"); |
| 3185 |
ADD_STATS(accept_good, "accept_good"); |
| 3186 |
ADD_STATS(accept_renegotiate, "accept_renegotiate"); |
| 3187 |
ADD_STATS(accept, "accept"); |
| 3188 |
ADD_STATS(hits, "hits"); |
| 3189 |
ADD_STATS(misses, "misses"); |
| 3190 |
ADD_STATS(timeouts, "timeouts"); |
| 3191 |
ADD_STATS(cache_full, "cache_full"); |
| 3192 |
|
| 3193 |
#undef ADD_STATS |
| 3194 |
|
| 3195 |
return stats; |
| 3196 |
|
| 3197 |
error: |
| 3198 |
Py_DECREF(stats); |
| 3199 |
return NULL; |
| 3200 |
} |
| 3201 |
|
| 3202 |
static PyObject * |
| 3203 |
set_default_verify_paths(PySSLContext *self, PyObject *unused) |
| 3204 |
{ |
| 3205 |
if (!SSL_CTX_set_default_verify_paths(self->ctx)) { |
| 3206 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3207 |
return NULL; |
| 3208 |
} |
| 3209 |
Py_RETURN_NONE; |
| 3210 |
} |
| 3211 |
|
| 3212 |
#ifndef OPENSSL_NO_ECDH |
| 3213 |
static PyObject * |
| 3214 |
set_ecdh_curve(PySSLContext *self, PyObject *name) |
| 3215 |
{ |
| 3216 |
char *name_bytes; |
| 3217 |
int nid; |
| 3218 |
EC_KEY *key; |
| 3219 |
|
| 3220 |
name_bytes = PyBytes_AsString(name); |
| 3221 |
if (!name_bytes) { |
| 3222 |
return NULL; |
| 3223 |
} |
| 3224 |
nid = OBJ_sn2nid(name_bytes); |
| 3225 |
if (nid == 0) { |
| 3226 |
PyObject *r = PyObject_Repr(name); |
| 3227 |
if (!r) |
| 3228 |
return NULL; |
| 3229 |
PyErr_Format(PyExc_ValueError, |
| 3230 |
"unknown elliptic curve name %s", PyString_AS_STRING(r)); |
| 3231 |
Py_DECREF(r); |
| 3232 |
return NULL; |
| 3233 |
} |
| 3234 |
key = EC_KEY_new_by_curve_name(nid); |
| 3235 |
if (key == NULL) { |
| 3236 |
_setSSLError(NULL, 0, __FILE__, __LINE__); |
| 3237 |
return NULL; |
| 3238 |
} |
| 3239 |
SSL_CTX_set_tmp_ecdh(self->ctx, key); |
| 3240 |
EC_KEY_free(key); |
| 3241 |
Py_RETURN_NONE; |
| 3242 |
} |
| 3243 |
#endif |
| 3244 |
|
| 3245 |
#if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
| 3246 |
static int |
| 3247 |
_servername_callback(SSL *s, int *al, void *args) |
| 3248 |
{ |
| 3249 |
int ret; |
| 3250 |
PySSLContext *ssl_ctx = (PySSLContext *) args; |
| 3251 |
PySSLSocket *ssl; |
| 3252 |
PyObject *servername_o; |
| 3253 |
PyObject *servername_idna; |
| 3254 |
PyObject *result; |
| 3255 |
|
| 3256 |
PyObject *ssl_socket; |
| 3257 |
const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); |
| 3258 |
#ifdef WITH_THREAD |
| 3259 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 3260 |
#endif |
| 3261 |
|
| 3262 |
if (ssl_ctx->set_hostname == NULL) { |
| 3263 |
|
| 3264 |
|
| 3265 |
#ifdef WITH_THREAD |
| 3266 |
PyGILState_Release(gstate); |
| 3267 |
#endif |
| 3268 |
return SSL_TLSEXT_ERR_OK; |
| 3269 |
} |
| 3270 |
|
| 3271 |
ssl = SSL_get_app_data(s); |
| 3272 |
assert(PySSLSocket_Check(ssl)); |
| 3273 |
if (ssl->ssl_sock == NULL) { |
| 3274 |
ssl_socket = Py_None; |
| 3275 |
} else { |
| 3276 |
ssl_socket = PyWeakref_GetObject(ssl->ssl_sock); |
| 3277 |
Py_INCREF(ssl_socket); |
| 3278 |
} |
| 3279 |
if (ssl_socket == Py_None) { |
| 3280 |
goto error; |
| 3281 |
} |
| 3282 |
|
| 3283 |
if (servername == NULL) { |
| 3284 |
result = PyObject_CallFunctionObjArgs(ssl_ctx->set_hostname, ssl_socket, |
| 3285 |
Py_None, ssl_ctx, NULL); |
| 3286 |
} |
| 3287 |
else { |
| 3288 |
servername_o = PyBytes_FromString(servername); |
| 3289 |
if (servername_o == NULL) { |
| 3290 |
PyErr_WriteUnraisable((PyObject *) ssl_ctx); |
| 3291 |
goto error; |
| 3292 |
} |
| 3293 |
servername_idna = PyUnicode_FromEncodedObject(servername_o, "idna", NULL); |
| 3294 |
if (servername_idna == NULL) { |
| 3295 |
PyErr_WriteUnraisable(servername_o); |
| 3296 |
Py_DECREF(servername_o); |
| 3297 |
goto error; |
| 3298 |
} |
| 3299 |
Py_DECREF(servername_o); |
| 3300 |
result = PyObject_CallFunctionObjArgs(ssl_ctx->set_hostname, ssl_socket, |
| 3301 |
servername_idna, ssl_ctx, NULL); |
| 3302 |
Py_DECREF(servername_idna); |
| 3303 |
} |
| 3304 |
Py_DECREF(ssl_socket); |
| 3305 |
|
| 3306 |
if (result == NULL) { |
| 3307 |
PyErr_WriteUnraisable(ssl_ctx->set_hostname); |
| 3308 |
*al = SSL_AD_HANDSHAKE_FAILURE; |
| 3309 |
ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 3310 |
} |
| 3311 |
else { |
| 3312 |
if (result != Py_None) { |
| 3313 |
*al = (int) PyLong_AsLong(result); |
| 3314 |
if (PyErr_Occurred()) { |
| 3315 |
PyErr_WriteUnraisable(result); |
| 3316 |
*al = SSL_AD_INTERNAL_ERROR; |
| 3317 |
} |
| 3318 |
ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 3319 |
} |
| 3320 |
else { |
| 3321 |
ret = SSL_TLSEXT_ERR_OK; |
| 3322 |
} |
| 3323 |
Py_DECREF(result); |
| 3324 |
} |
| 3325 |
|
| 3326 |
#ifdef WITH_THREAD |
| 3327 |
PyGILState_Release(gstate); |
| 3328 |
#endif |
| 3329 |
return ret; |
| 3330 |
|
| 3331 |
error: |
| 3332 |
Py_DECREF(ssl_socket); |
| 3333 |
*al = SSL_AD_INTERNAL_ERROR; |
| 3334 |
ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 3335 |
#ifdef WITH_THREAD |
| 3336 |
PyGILState_Release(gstate); |
| 3337 |
#endif |
| 3338 |
return ret; |
| 3339 |
} |
| 3340 |
#endif |
| 3341 |
|
| 3342 |
PyDoc_STRVAR(PySSL_set_servername_callback_doc, |
| 3343 |
"set_servername_callback(method)\n\ |
| 3344 |
\n\ |
| 3345 |
This sets a callback that will be called when a server name is provided by\n\ |
| 3346 |
the SSL/TLS client in the SNI extension.\n\ |
| 3347 |
\n\ |
| 3348 |
If the argument is None then the callback is disabled. The method is called\n\ |
| 3349 |
with the SSLSocket, the server name as a string, and the SSLContext object.\n\ |
| 3350 |
See RFC 6066 for details of the SNI extension."); |
| 3351 |
|
| 3352 |
static PyObject * |
| 3353 |
set_servername_callback(PySSLContext *self, PyObject *args) |
| 3354 |
{ |
| 3355 |
#if HAVE_SNI && !defined(OPENSSL_NO_TLSEXT) |
| 3356 |
PyObject *cb; |
| 3357 |
|
| 3358 |
if (!PyArg_ParseTuple(args, "O", &cb)) |
| 3359 |
return NULL; |
| 3360 |
|
| 3361 |
Py_CLEAR(self->set_hostname); |
| 3362 |
if (cb == Py_None) { |
| 3363 |
SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 3364 |
} |
| 3365 |
else { |
| 3366 |
if (!PyCallable_Check(cb)) { |
| 3367 |
SSL_CTX_set_tlsext_servername_callback(self->ctx, NULL); |
| 3368 |
PyErr_SetString(PyExc_TypeError, |
| 3369 |
"not a callable object"); |
| 3370 |
return NULL; |
| 3371 |
} |
| 3372 |
Py_INCREF(cb); |
| 3373 |
self->set_hostname = cb; |
| 3374 |
SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); |
| 3375 |
SSL_CTX_set_tlsext_servername_arg(self->ctx, self); |
| 3376 |
} |
| 3377 |
Py_RETURN_NONE; |
| 3378 |
#else |
| 3379 |
PyErr_SetString(PyExc_NotImplementedError, |
| 3380 |
"The TLS extension servername callback, " |
| 3381 |
"SSL_CTX_set_tlsext_servername_callback, " |
| 3382 |
"is not in the current OpenSSL library."); |
| 3383 |
return NULL; |
| 3384 |
#endif |
| 3385 |
} |
| 3386 |
|
| 3387 |
PyDoc_STRVAR(PySSL_get_stats_doc, |
| 3388 |
"cert_store_stats() -> {'crl': int, 'x509_ca': int, 'x509': int}\n\ |
| 3389 |
\n\ |
| 3390 |
Returns quantities of loaded X.509 certificates. X.509 certificates with a\n\ |
| 3391 |
CA extension and certificate revocation lists inside the context's cert\n\ |
| 3392 |
store.\n\ |
| 3393 |
NOTE: Certificates in a capath directory aren't loaded unless they have\n\ |
| 3394 |
been used at least once."); |
| 3395 |
|
| 3396 |
static PyObject * |
| 3397 |
cert_store_stats(PySSLContext *self) |
| 3398 |
{ |
| 3399 |
X509_STORE *store; |
| 3400 |
STACK_OF(X509_OBJECT) *objs; |
| 3401 |
X509_OBJECT *obj; |
| 3402 |
int x509 = 0, crl = 0, ca = 0, i; |
| 3403 |
|
| 3404 |
store = SSL_CTX_get_cert_store(self->ctx); |
| 3405 |
objs = X509_STORE_get0_objects(store); |
| 3406 |
for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 3407 |
obj = sk_X509_OBJECT_value(objs, i); |
| 3408 |
switch (X509_OBJECT_get_type(obj)) { |
| 3409 |
case X509_LU_X509: |
| 3410 |
x509++; |
| 3411 |
if (X509_check_ca(X509_OBJECT_get0_X509(obj))) { |
| 3412 |
ca++; |
| 3413 |
} |
| 3414 |
break; |
| 3415 |
case X509_LU_CRL: |
| 3416 |
crl++; |
| 3417 |
break; |
| 3418 |
default: |
| 3419 |
|
| 3420 |
|
| 3421 |
|
| 3422 |
break; |
| 3423 |
} |
| 3424 |
} |
| 3425 |
return Py_BuildValue("{sisisi}", "x509", x509, "crl", crl, |
| 3426 |
"x509_ca", ca); |
| 3427 |
} |
| 3428 |
|
| 3429 |
PyDoc_STRVAR(PySSL_get_ca_certs_doc, |
| 3430 |
"get_ca_certs(binary_form=False) -> list of loaded certificate\n\ |
| 3431 |
\n\ |
| 3432 |
Returns a list of dicts with information of loaded CA certs. If the\n\ |
| 3433 |
optional argument is True, returns a DER-encoded copy of the CA certificate.\n\ |
| 3434 |
NOTE: Certificates in a capath directory aren't loaded unless they have\n\ |
| 3435 |
been used at least once."); |
| 3436 |
|
| 3437 |
static PyObject * |
| 3438 |
get_ca_certs(PySSLContext *self, PyObject *args, PyObject *kwds) |
| 3439 |
{ |
| 3440 |
char *kwlist[] = {"binary_form", NULL}; |
| 3441 |
X509_STORE *store; |
| 3442 |
PyObject *ci = NULL, *rlist = NULL, *py_binary_mode = Py_False; |
| 3443 |
STACK_OF(X509_OBJECT) *objs; |
| 3444 |
int i; |
| 3445 |
int binary_mode = 0; |
| 3446 |
|
| 3447 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:get_ca_certs", |
| 3448 |
kwlist, &py_binary_mode)) { |
| 3449 |
return NULL; |
| 3450 |
} |
| 3451 |
binary_mode = PyObject_IsTrue(py_binary_mode); |
| 3452 |
if (binary_mode < 0) { |
| 3453 |
return NULL; |
| 3454 |
} |
| 3455 |
|
| 3456 |
if ((rlist = PyList_New(0)) == NULL) { |
| 3457 |
return NULL; |
| 3458 |
} |
| 3459 |
|
| 3460 |
store = SSL_CTX_get_cert_store(self->ctx); |
| 3461 |
objs = X509_STORE_get0_objects(store); |
| 3462 |
for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 3463 |
X509_OBJECT *obj; |
| 3464 |
X509 *cert; |
| 3465 |
|
| 3466 |
obj = sk_X509_OBJECT_value(objs, i); |
| 3467 |
if (X509_OBJECT_get_type(obj) != X509_LU_X509) { |
| 3468 |
|
| 3469 |
continue; |
| 3470 |
} |
| 3471 |
|
| 3472 |
cert = X509_OBJECT_get0_X509(obj); |
| 3473 |
if (!X509_check_ca(cert)) { |
| 3474 |
continue; |
| 3475 |
} |
| 3476 |
if (binary_mode) { |
| 3477 |
ci = _certificate_to_der(cert); |
| 3478 |
} else { |
| 3479 |
ci = _decode_certificate(cert); |
| 3480 |
} |
| 3481 |
if (ci == NULL) { |
| 3482 |
goto error; |
| 3483 |
} |
| 3484 |
if (PyList_Append(rlist, ci) == -1) { |
| 3485 |
goto error; |
| 3486 |
} |
| 3487 |
Py_CLEAR(ci); |
| 3488 |
} |
| 3489 |
return rlist; |
| 3490 |
|
| 3491 |
error: |
| 3492 |
Py_XDECREF(ci); |
| 3493 |
Py_XDECREF(rlist); |
| 3494 |
return NULL; |
| 3495 |
} |
| 3496 |
|
| 3497 |
|
| 3498 |
static PyGetSetDef context_getsetlist[] = { |
| 3499 |
{"check_hostname", (getter) get_check_hostname, |
| 3500 |
(setter) set_check_hostname, NULL}, |
| 3501 |
{"options", (getter) get_options, |
| 3502 |
(setter) set_options, NULL}, |
| 3503 |
#ifdef HAVE_OPENSSL_VERIFY_PARAM |
| 3504 |
{"verify_flags", (getter) get_verify_flags, |
| 3505 |
(setter) set_verify_flags, NULL}, |
| 3506 |
#endif |
| 3507 |
{"verify_mode", (getter) get_verify_mode, |
| 3508 |
(setter) set_verify_mode, NULL}, |
| 3509 |
{NULL}, |
| 3510 |
}; |
| 3511 |
|
| 3512 |
static struct PyMethodDef context_methods[] = { |
| 3513 |
{"_wrap_socket", (PyCFunction) context_wrap_socket, |
| 3514 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 3515 |
{"set_ciphers", (PyCFunction) set_ciphers, |
| 3516 |
METH_VARARGS, NULL}, |
| 3517 |
{"_set_alpn_protocols", (PyCFunction) _set_alpn_protocols, |
| 3518 |
METH_VARARGS, NULL}, |
| 3519 |
{"_set_npn_protocols", (PyCFunction) _set_npn_protocols, |
| 3520 |
METH_VARARGS, NULL}, |
| 3521 |
{"load_cert_chain", (PyCFunction) load_cert_chain, |
| 3522 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 3523 |
{"load_dh_params", (PyCFunction) load_dh_params, |
| 3524 |
METH_O, NULL}, |
| 3525 |
{"load_verify_locations", (PyCFunction) load_verify_locations, |
| 3526 |
METH_VARARGS | METH_KEYWORDS, NULL}, |
| 3527 |
{"session_stats", (PyCFunction) session_stats, |
| 3528 |
METH_NOARGS, NULL}, |
| 3529 |
{"set_default_verify_paths", (PyCFunction) set_default_verify_paths, |
| 3530 |
METH_NOARGS, NULL}, |
| 3531 |
#ifndef OPENSSL_NO_ECDH |
| 3532 |
{"set_ecdh_curve", (PyCFunction) set_ecdh_curve, |
| 3533 |
METH_O, NULL}, |
| 3534 |
#endif |
| 3535 |
{"set_servername_callback", (PyCFunction) set_servername_callback, |
| 3536 |
METH_VARARGS, PySSL_set_servername_callback_doc}, |
| 3537 |
{"cert_store_stats", (PyCFunction) cert_store_stats, |
| 3538 |
METH_NOARGS, PySSL_get_stats_doc}, |
| 3539 |
{"get_ca_certs", (PyCFunction) get_ca_certs, |
| 3540 |
METH_VARARGS | METH_KEYWORDS, PySSL_get_ca_certs_doc}, |
| 3541 |
{NULL, NULL} |
| 3542 |
}; |
| 3543 |
|
| 3544 |
static PyTypeObject PySSLContext_Type = { |
| 3545 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3546 |
"_ssl._SSLContext", |
| 3547 |
sizeof(PySSLContext), |
| 3548 |
0, |
| 3549 |
(destructor)context_dealloc, |
| 3550 |
0, |
| 3551 |
0, |
| 3552 |
0, |
| 3553 |
0, |
| 3554 |
0, |
| 3555 |
0, |
| 3556 |
0, |
| 3557 |
0, |
| 3558 |
0, |
| 3559 |
0, |
| 3560 |
0, |
| 3561 |
0, |
| 3562 |
0, |
| 3563 |
0, |
| 3564 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 3565 |
0, |
| 3566 |
(traverseproc) context_traverse, |
| 3567 |
(inquiry) context_clear, |
| 3568 |
0, |
| 3569 |
0, |
| 3570 |
0, |
| 3571 |
0, |
| 3572 |
context_methods, |
| 3573 |
0, |
| 3574 |
context_getsetlist, |
| 3575 |
0, |
| 3576 |
0, |
| 3577 |
0, |
| 3578 |
0, |
| 3579 |
0, |
| 3580 |
0, |
| 3581 |
0, |
| 3582 |
context_new, |
| 3583 |
}; |
| 3584 |
|
| 3585 |
|
| 3586 |
|
| 3587 |
#ifdef HAVE_OPENSSL_RAND |
| 3588 |
|
| 3589 |
|
| 3590 |
static PyObject * |
| 3591 |
PySSL_RAND_add(PyObject *self, PyObject *args) |
| 3592 |
{ |
| 3593 |
char *buf; |
| 3594 |
Py_ssize_t len, written; |
| 3595 |
double entropy; |
| 3596 |
|
| 3597 |
if (!PyArg_ParseTuple(args, "s#d:RAND_add", &buf, &len, &entropy)) |
| 3598 |
return NULL; |
| 3599 |
do { |
| 3600 |
if (len >= INT_MAX) { |
| 3601 |
written = INT_MAX; |
| 3602 |
} else { |
| 3603 |
written = len; |
| 3604 |
} |
| 3605 |
RAND_add(buf, (int)written, entropy); |
| 3606 |
buf += written; |
| 3607 |
len -= written; |
| 3608 |
} while (len); |
| 3609 |
Py_INCREF(Py_None); |
| 3610 |
return Py_None; |
| 3611 |
} |
| 3612 |
|
| 3613 |
PyDoc_STRVAR(PySSL_RAND_add_doc, |
| 3614 |
"RAND_add(string, entropy)\n\ |
| 3615 |
\n\ |
| 3616 |
Mix string into the OpenSSL PRNG state. entropy (a float) is a lower\n\ |
| 3617 |
bound on the entropy contained in string. See RFC 1750."); |
| 3618 |
|
| 3619 |
static PyObject * |
| 3620 |
PySSL_RAND_status(PyObject *self) |
| 3621 |
{ |
| 3622 |
return PyLong_FromLong(RAND_status()); |
| 3623 |
} |
| 3624 |
|
| 3625 |
PyDoc_STRVAR(PySSL_RAND_status_doc, |
| 3626 |
"RAND_status() -> 0 or 1\n\ |
| 3627 |
\n\ |
| 3628 |
Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\ |
| 3629 |
It is necessary to seed the PRNG with RAND_add() on some platforms before\n\ |
| 3630 |
using the ssl() function."); |
| 3631 |
|
| 3632 |
#endif |
| 3633 |
|
| 3634 |
|
| 3635 |
#ifndef OPENSSL_NO_EGD |
| 3636 |
|
| 3637 |
static PyObject * |
| 3638 |
PySSL_RAND_egd(PyObject *self, PyObject *arg) |
| 3639 |
{ |
| 3640 |
int bytes; |
| 3641 |
|
| 3642 |
if (!PyString_Check(arg)) |
| 3643 |
return PyErr_Format(PyExc_TypeError, |
| 3644 |
"RAND_egd() expected string, found %s", |
| 3645 |
Py_TYPE(arg)->tp_name); |
| 3646 |
bytes = RAND_egd(PyString_AS_STRING(arg)); |
| 3647 |
if (bytes == -1) { |
| 3648 |
PyErr_SetString(PySSLErrorObject, |
| 3649 |
"EGD connection failed or EGD did not return " |
| 3650 |
"enough data to seed the PRNG"); |
| 3651 |
return NULL; |
| 3652 |
} |
| 3653 |
return PyInt_FromLong(bytes); |
| 3654 |
} |
| 3655 |
|
| 3656 |
PyDoc_STRVAR(PySSL_RAND_egd_doc, |
| 3657 |
"RAND_egd(path) -> bytes\n\ |
| 3658 |
\n\ |
| 3659 |
Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\ |
| 3660 |
Returns number of bytes read. Raises SSLError if connection to EGD\n\ |
| 3661 |
fails or if it does not provide enough data to seed PRNG."); |
| 3662 |
|
| 3663 |
#endif |
| 3664 |
|
| 3665 |
|
| 3666 |
PyDoc_STRVAR(PySSL_get_default_verify_paths_doc, |
| 3667 |
"get_default_verify_paths() -> tuple\n\ |
| 3668 |
\n\ |
| 3669 |
Return search paths and environment vars that are used by SSLContext's\n\ |
| 3670 |
set_default_verify_paths() to load default CAs. The values are\n\ |
| 3671 |
'cert_file_env', 'cert_file', 'cert_dir_env', 'cert_dir'."); |
| 3672 |
|
| 3673 |
static PyObject * |
| 3674 |
PySSL_get_default_verify_paths(PyObject *self) |
| 3675 |
{ |
| 3676 |
PyObject *ofile_env = NULL; |
| 3677 |
PyObject *ofile = NULL; |
| 3678 |
PyObject *odir_env = NULL; |
| 3679 |
PyObject *odir = NULL; |
| 3680 |
|
| 3681 |
#define CONVERT(info, target) { \ |
| 3682 |
const char *tmp = (info); \ |
| 3683 |
target = NULL; \ |
| 3684 |
if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ |
| 3685 |
else { target = PyBytes_FromString(tmp); } \ |
| 3686 |
if (!target) goto error; \ |
| 3687 |
} |
| 3688 |
|
| 3689 |
CONVERT(X509_get_default_cert_file_env(), ofile_env); |
| 3690 |
CONVERT(X509_get_default_cert_file(), ofile); |
| 3691 |
CONVERT(X509_get_default_cert_dir_env(), odir_env); |
| 3692 |
CONVERT(X509_get_default_cert_dir(), odir); |
| 3693 |
#undef CONVERT |
| 3694 |
|
| 3695 |
return Py_BuildValue("NNNN", ofile_env, ofile, odir_env, odir); |
| 3696 |
|
| 3697 |
error: |
| 3698 |
Py_XDECREF(ofile_env); |
| 3699 |
Py_XDECREF(ofile); |
| 3700 |
Py_XDECREF(odir_env); |
| 3701 |
Py_XDECREF(odir); |
| 3702 |
return NULL; |
| 3703 |
} |
| 3704 |
|
| 3705 |
static PyObject* |
| 3706 |
asn1obj2py(ASN1_OBJECT *obj) |
| 3707 |
{ |
| 3708 |
int nid; |
| 3709 |
const char *ln, *sn; |
| 3710 |
|
| 3711 |
nid = OBJ_obj2nid(obj); |
| 3712 |
if (nid == NID_undef) { |
| 3713 |
PyErr_Format(PyExc_ValueError, "Unknown object"); |
| 3714 |
return NULL; |
| 3715 |
} |
| 3716 |
sn = OBJ_nid2sn(nid); |
| 3717 |
ln = OBJ_nid2ln(nid); |
| 3718 |
return Py_BuildValue("issN", nid, sn, ln, _asn1obj2py(obj, 1)); |
| 3719 |
} |
| 3720 |
|
| 3721 |
PyDoc_STRVAR(PySSL_txt2obj_doc, |
| 3722 |
"txt2obj(txt, name=False) -> (nid, shortname, longname, oid)\n\ |
| 3723 |
\n\ |
| 3724 |
Lookup NID, short name, long name and OID of an ASN1_OBJECT. By default\n\ |
| 3725 |
objects are looked up by OID. With name=True short and long name are also\n\ |
| 3726 |
matched."); |
| 3727 |
|
| 3728 |
static PyObject* |
| 3729 |
PySSL_txt2obj(PyObject *self, PyObject *args, PyObject *kwds) |
| 3730 |
{ |
| 3731 |
char *kwlist[] = {"txt", "name", NULL}; |
| 3732 |
PyObject *result = NULL; |
| 3733 |
char *txt; |
| 3734 |
PyObject *pyname = Py_None; |
| 3735 |
int name = 0; |
| 3736 |
ASN1_OBJECT *obj; |
| 3737 |
|
| 3738 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|O:txt2obj", |
| 3739 |
kwlist, &txt, &pyname)) { |
| 3740 |
return NULL; |
| 3741 |
} |
| 3742 |
name = PyObject_IsTrue(pyname); |
| 3743 |
if (name < 0) |
| 3744 |
return NULL; |
| 3745 |
obj = OBJ_txt2obj(txt, name ? 0 : 1); |
| 3746 |
if (obj == NULL) { |
| 3747 |
PyErr_Format(PyExc_ValueError, "unknown object '%.100s'", txt); |
| 3748 |
return NULL; |
| 3749 |
} |
| 3750 |
result = asn1obj2py(obj); |
| 3751 |
ASN1_OBJECT_free(obj); |
| 3752 |
return result; |
| 3753 |
} |
| 3754 |
|
| 3755 |
PyDoc_STRVAR(PySSL_nid2obj_doc, |
| 3756 |
"nid2obj(nid) -> (nid, shortname, longname, oid)\n\ |
| 3757 |
\n\ |
| 3758 |
Lookup NID, short name, long name and OID of an ASN1_OBJECT by NID."); |
| 3759 |
|
| 3760 |
static PyObject* |
| 3761 |
PySSL_nid2obj(PyObject *self, PyObject *args) |
| 3762 |
{ |
| 3763 |
PyObject *result = NULL; |
| 3764 |
int nid; |
| 3765 |
ASN1_OBJECT *obj; |
| 3766 |
|
| 3767 |
if (!PyArg_ParseTuple(args, "i:nid2obj", &nid)) { |
| 3768 |
return NULL; |
| 3769 |
} |
| 3770 |
if (nid < NID_undef) { |
| 3771 |
PyErr_SetString(PyExc_ValueError, "NID must be positive."); |
| 3772 |
return NULL; |
| 3773 |
} |
| 3774 |
obj = OBJ_nid2obj(nid); |
| 3775 |
if (obj == NULL) { |
| 3776 |
PyErr_Format(PyExc_ValueError, "unknown NID %i", nid); |
| 3777 |
return NULL; |
| 3778 |
} |
| 3779 |
result = asn1obj2py(obj); |
| 3780 |
ASN1_OBJECT_free(obj); |
| 3781 |
return result; |
| 3782 |
} |
| 3783 |
|
| 3784 |
#ifdef _MSC_VER |
| 3785 |
|
| 3786 |
static PyObject* |
| 3787 |
certEncodingType(DWORD encodingType) |
| 3788 |
{ |
| 3789 |
static PyObject *x509_asn = NULL; |
| 3790 |
static PyObject *pkcs_7_asn = NULL; |
| 3791 |
|
| 3792 |
if (x509_asn == NULL) { |
| 3793 |
x509_asn = PyString_InternFromString("x509_asn"); |
| 3794 |
if (x509_asn == NULL) |
| 3795 |
return NULL; |
| 3796 |
} |
| 3797 |
if (pkcs_7_asn == NULL) { |
| 3798 |
pkcs_7_asn = PyString_InternFromString("pkcs_7_asn"); |
| 3799 |
if (pkcs_7_asn == NULL) |
| 3800 |
return NULL; |
| 3801 |
} |
| 3802 |
switch(encodingType) { |
| 3803 |
case X509_ASN_ENCODING: |
| 3804 |
Py_INCREF(x509_asn); |
| 3805 |
return x509_asn; |
| 3806 |
case PKCS_7_ASN_ENCODING: |
| 3807 |
Py_INCREF(pkcs_7_asn); |
| 3808 |
return pkcs_7_asn; |
| 3809 |
default: |
| 3810 |
return PyInt_FromLong(encodingType); |
| 3811 |
} |
| 3812 |
} |
| 3813 |
|
| 3814 |
static PyObject* |
| 3815 |
parseKeyUsage(PCCERT_CONTEXT pCertCtx, DWORD flags) |
| 3816 |
{ |
| 3817 |
CERT_ENHKEY_USAGE *usage; |
| 3818 |
DWORD size, error, i; |
| 3819 |
PyObject *retval; |
| 3820 |
|
| 3821 |
if (!CertGetEnhancedKeyUsage(pCertCtx, flags, NULL, &size)) { |
| 3822 |
error = GetLastError(); |
| 3823 |
if (error == CRYPT_E_NOT_FOUND) { |
| 3824 |
Py_RETURN_TRUE; |
| 3825 |
} |
| 3826 |
return PyErr_SetFromWindowsErr(error); |
| 3827 |
} |
| 3828 |
|
| 3829 |
usage = (CERT_ENHKEY_USAGE*)PyMem_Malloc(size); |
| 3830 |
if (usage == NULL) { |
| 3831 |
return PyErr_NoMemory(); |
| 3832 |
} |
| 3833 |
|
| 3834 |
|
| 3835 |
if (!CertGetEnhancedKeyUsage(pCertCtx, flags, usage, &size)) { |
| 3836 |
PyMem_Free(usage); |
| 3837 |
error = GetLastError(); |
| 3838 |
if (error == CRYPT_E_NOT_FOUND) { |
| 3839 |
Py_RETURN_TRUE; |
| 3840 |
} |
| 3841 |
return PyErr_SetFromWindowsErr(error); |
| 3842 |
} |
| 3843 |
retval = PySet_New(NULL); |
| 3844 |
if (retval == NULL) { |
| 3845 |
goto error; |
| 3846 |
} |
| 3847 |
for (i = 0; i < usage->cUsageIdentifier; ++i) { |
| 3848 |
if (usage->rgpszUsageIdentifier[i]) { |
| 3849 |
PyObject *oid; |
| 3850 |
int err; |
| 3851 |
oid = PyString_FromString(usage->rgpszUsageIdentifier[i]); |
| 3852 |
if (oid == NULL) { |
| 3853 |
Py_CLEAR(retval); |
| 3854 |
goto error; |
| 3855 |
} |
| 3856 |
err = PySet_Add(retval, oid); |
| 3857 |
Py_DECREF(oid); |
| 3858 |
if (err == -1) { |
| 3859 |
Py_CLEAR(retval); |
| 3860 |
goto error; |
| 3861 |
} |
| 3862 |
} |
| 3863 |
} |
| 3864 |
error: |
| 3865 |
PyMem_Free(usage); |
| 3866 |
return retval; |
| 3867 |
} |
| 3868 |
|
| 3869 |
PyDoc_STRVAR(PySSL_enum_certificates_doc, |
| 3870 |
"enum_certificates(store_name) -> []\n\ |
| 3871 |
\n\ |
| 3872 |
Retrieve certificates from Windows' cert store. store_name may be one of\n\ |
| 3873 |
'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too.\n\ |
| 3874 |
The function returns a list of (bytes, encoding_type, trust) tuples. The\n\ |
| 3875 |
encoding_type flag can be interpreted with X509_ASN_ENCODING or\n\ |
| 3876 |
PKCS_7_ASN_ENCODING. The trust setting is either a set of OIDs or the\n\ |
| 3877 |
boolean True."); |
| 3878 |
|
| 3879 |
static PyObject * |
| 3880 |
PySSL_enum_certificates(PyObject *self, PyObject *args, PyObject *kwds) |
| 3881 |
{ |
| 3882 |
char *kwlist[] = {"store_name", NULL}; |
| 3883 |
char *store_name; |
| 3884 |
HCERTSTORE hStore = NULL; |
| 3885 |
PCCERT_CONTEXT pCertCtx = NULL; |
| 3886 |
PyObject *keyusage = NULL, *cert = NULL, *enc = NULL, *tup = NULL; |
| 3887 |
PyObject *result = NULL; |
| 3888 |
|
| 3889 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:enum_certificates", |
| 3890 |
kwlist, &store_name)) { |
| 3891 |
return NULL; |
| 3892 |
} |
| 3893 |
result = PyList_New(0); |
| 3894 |
if (result == NULL) { |
| 3895 |
return NULL; |
| 3896 |
} |
| 3897 |
hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, |
| 3898 |
CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 3899 |
store_name); |
| 3900 |
if (hStore == NULL) { |
| 3901 |
Py_DECREF(result); |
| 3902 |
return PyErr_SetFromWindowsErr(GetLastError()); |
| 3903 |
} |
| 3904 |
|
| 3905 |
while (pCertCtx = CertEnumCertificatesInStore(hStore, pCertCtx)) { |
| 3906 |
cert = PyBytes_FromStringAndSize((const char*)pCertCtx->pbCertEncoded, |
| 3907 |
pCertCtx->cbCertEncoded); |
| 3908 |
if (!cert) { |
| 3909 |
Py_CLEAR(result); |
| 3910 |
break; |
| 3911 |
} |
| 3912 |
if ((enc = certEncodingType(pCertCtx->dwCertEncodingType)) == NULL) { |
| 3913 |
Py_CLEAR(result); |
| 3914 |
break; |
| 3915 |
} |
| 3916 |
keyusage = parseKeyUsage(pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG); |
| 3917 |
if (keyusage == Py_True) { |
| 3918 |
Py_DECREF(keyusage); |
| 3919 |
keyusage = parseKeyUsage(pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG); |
| 3920 |
} |
| 3921 |
if (keyusage == NULL) { |
| 3922 |
Py_CLEAR(result); |
| 3923 |
break; |
| 3924 |
} |
| 3925 |
if ((tup = PyTuple_New(3)) == NULL) { |
| 3926 |
Py_CLEAR(result); |
| 3927 |
break; |
| 3928 |
} |
| 3929 |
PyTuple_SET_ITEM(tup, 0, cert); |
| 3930 |
cert = NULL; |
| 3931 |
PyTuple_SET_ITEM(tup, 1, enc); |
| 3932 |
enc = NULL; |
| 3933 |
PyTuple_SET_ITEM(tup, 2, keyusage); |
| 3934 |
keyusage = NULL; |
| 3935 |
if (PyList_Append(result, tup) < 0) { |
| 3936 |
Py_CLEAR(result); |
| 3937 |
break; |
| 3938 |
} |
| 3939 |
Py_CLEAR(tup); |
| 3940 |
} |
| 3941 |
if (pCertCtx) { |
| 3942 |
|
| 3943 |
CertFreeCertificateContext(pCertCtx); |
| 3944 |
} |
| 3945 |
|
| 3946 |
|
| 3947 |
Py_XDECREF(cert); |
| 3948 |
Py_XDECREF(enc); |
| 3949 |
Py_XDECREF(keyusage); |
| 3950 |
Py_XDECREF(tup); |
| 3951 |
|
| 3952 |
if (!CertCloseStore(hStore, 0)) { |
| 3953 |
|
| 3954 |
Py_XDECREF(result); |
| 3955 |
return PyErr_SetFromWindowsErr(GetLastError()); |
| 3956 |
} |
| 3957 |
return result; |
| 3958 |
} |
| 3959 |
|
| 3960 |
PyDoc_STRVAR(PySSL_enum_crls_doc, |
| 3961 |
"enum_crls(store_name) -> []\n\ |
| 3962 |
\n\ |
| 3963 |
Retrieve CRLs from Windows' cert store. store_name may be one of\n\ |
| 3964 |
'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too.\n\ |
| 3965 |
The function returns a list of (bytes, encoding_type) tuples. The\n\ |
| 3966 |
encoding_type flag can be interpreted with X509_ASN_ENCODING or\n\ |
| 3967 |
PKCS_7_ASN_ENCODING."); |
| 3968 |
|
| 3969 |
static PyObject * |
| 3970 |
PySSL_enum_crls(PyObject *self, PyObject *args, PyObject *kwds) |
| 3971 |
{ |
| 3972 |
char *kwlist[] = {"store_name", NULL}; |
| 3973 |
char *store_name; |
| 3974 |
HCERTSTORE hStore = NULL; |
| 3975 |
PCCRL_CONTEXT pCrlCtx = NULL; |
| 3976 |
PyObject *crl = NULL, *enc = NULL, *tup = NULL; |
| 3977 |
PyObject *result = NULL; |
| 3978 |
|
| 3979 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:enum_crls", |
| 3980 |
kwlist, &store_name)) { |
| 3981 |
return NULL; |
| 3982 |
} |
| 3983 |
result = PyList_New(0); |
| 3984 |
if (result == NULL) { |
| 3985 |
return NULL; |
| 3986 |
} |
| 3987 |
hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, (HCRYPTPROV)NULL, |
| 3988 |
CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, |
| 3989 |
store_name); |
| 3990 |
if (hStore == NULL) { |
| 3991 |
Py_DECREF(result); |
| 3992 |
return PyErr_SetFromWindowsErr(GetLastError()); |
| 3993 |
} |
| 3994 |
|
| 3995 |
while (pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx)) { |
| 3996 |
crl = PyBytes_FromStringAndSize((const char*)pCrlCtx->pbCrlEncoded, |
| 3997 |
pCrlCtx->cbCrlEncoded); |
| 3998 |
if (!crl) { |
| 3999 |
Py_CLEAR(result); |
| 4000 |
break; |
| 4001 |
} |
| 4002 |
if ((enc = certEncodingType(pCrlCtx->dwCertEncodingType)) == NULL) { |
| 4003 |
Py_CLEAR(result); |
| 4004 |
break; |
| 4005 |
} |
| 4006 |
if ((tup = PyTuple_New(2)) == NULL) { |
| 4007 |
Py_CLEAR(result); |
| 4008 |
break; |
| 4009 |
} |
| 4010 |
PyTuple_SET_ITEM(tup, 0, crl); |
| 4011 |
crl = NULL; |
| 4012 |
PyTuple_SET_ITEM(tup, 1, enc); |
| 4013 |
enc = NULL; |
| 4014 |
|
| 4015 |
if (PyList_Append(result, tup) < 0) { |
| 4016 |
Py_CLEAR(result); |
| 4017 |
break; |
| 4018 |
} |
| 4019 |
Py_CLEAR(tup); |
| 4020 |
} |
| 4021 |
if (pCrlCtx) { |
| 4022 |
|
| 4023 |
CertFreeCRLContext(pCrlCtx); |
| 4024 |
} |
| 4025 |
|
| 4026 |
|
| 4027 |
Py_XDECREF(crl); |
| 4028 |
Py_XDECREF(enc); |
| 4029 |
Py_XDECREF(tup); |
| 4030 |
|
| 4031 |
if (!CertCloseStore(hStore, 0)) { |
| 4032 |
|
| 4033 |
Py_XDECREF(result); |
| 4034 |
return PyErr_SetFromWindowsErr(GetLastError()); |
| 4035 |
} |
| 4036 |
return result; |
| 4037 |
} |
| 4038 |
|
| 4039 |
#endif |
| 4040 |
|
| 4041 |
|
| 4042 |
|
| 4043 |
static PyMethodDef PySSL_methods[] = { |
| 4044 |
{"_test_decode_cert", PySSL_test_decode_certificate, |
| 4045 |
METH_VARARGS}, |
| 4046 |
#ifdef HAVE_OPENSSL_RAND |
| 4047 |
{"RAND_add", PySSL_RAND_add, METH_VARARGS, |
| 4048 |
PySSL_RAND_add_doc}, |
| 4049 |
{"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS, |
| 4050 |
PySSL_RAND_status_doc}, |
| 4051 |
#endif |
| 4052 |
#ifndef OPENSSL_NO_EGD |
| 4053 |
{"RAND_egd", PySSL_RAND_egd, METH_VARARGS, |
| 4054 |
PySSL_RAND_egd_doc}, |
| 4055 |
#endif |
| 4056 |
{"get_default_verify_paths", (PyCFunction)PySSL_get_default_verify_paths, |
| 4057 |
METH_NOARGS, PySSL_get_default_verify_paths_doc}, |
| 4058 |
#ifdef _MSC_VER |
| 4059 |
{"enum_certificates", (PyCFunction)PySSL_enum_certificates, |
| 4060 |
METH_VARARGS | METH_KEYWORDS, PySSL_enum_certificates_doc}, |
| 4061 |
{"enum_crls", (PyCFunction)PySSL_enum_crls, |
| 4062 |
METH_VARARGS | METH_KEYWORDS, PySSL_enum_crls_doc}, |
| 4063 |
#endif |
| 4064 |
{"txt2obj", (PyCFunction)PySSL_txt2obj, |
| 4065 |
METH_VARARGS | METH_KEYWORDS, PySSL_txt2obj_doc}, |
| 4066 |
{"nid2obj", (PyCFunction)PySSL_nid2obj, |
| 4067 |
METH_VARARGS, PySSL_nid2obj_doc}, |
| 4068 |
{NULL, NULL} |
| 4069 |
}; |
| 4070 |
|
| 4071 |
|
| 4072 |
#ifdef HAVE_OPENSSL_CRYPTO_LOCK |
| 4073 |
|
| 4074 |
|
| 4075 |
|
| 4076 |
|
| 4077 |
|
| 4078 |
|
| 4079 |
static PyThread_type_lock *_ssl_locks = NULL; |
| 4080 |
|
| 4081 |
#if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 4082 |
|
| 4083 |
static void |
| 4084 |
_ssl_threadid_callback(CRYPTO_THREADID *id) |
| 4085 |
{ |
| 4086 |
CRYPTO_THREADID_set_numeric(id, |
| 4087 |
(unsigned long)PyThread_get_thread_ident()); |
| 4088 |
} |
| 4089 |
#else |
| 4090 |
|
| 4091 |
static unsigned long |
| 4092 |
_ssl_thread_id_function (void) { |
| 4093 |
return PyThread_get_thread_ident(); |
| 4094 |
} |
| 4095 |
#endif |
| 4096 |
|
| 4097 |
static void _ssl_thread_locking_function |
| 4098 |
(int mode, int n, const char *file, int line) { |
| 4099 |
|
| 4100 |
|
| 4101 |
|
| 4102 |
|
| 4103 |
|
| 4104 |
|
| 4105 |
|
| 4106 |
|
| 4107 |
|
| 4108 |
|
| 4109 |
|
| 4110 |
|
| 4111 |
|
| 4112 |
|
| 4113 |
if ((_ssl_locks == NULL) || |
| 4114 |
(n < 0) || ((unsigned)n >= _ssl_locks_count)) |
| 4115 |
return; |
| 4116 |
|
| 4117 |
if (mode & CRYPTO_LOCK) { |
| 4118 |
PyThread_acquire_lock(_ssl_locks[n], 1); |
| 4119 |
} else { |
| 4120 |
PyThread_release_lock(_ssl_locks[n]); |
| 4121 |
} |
| 4122 |
} |
| 4123 |
|
| 4124 |
static int _setup_ssl_threads(void) { |
| 4125 |
|
| 4126 |
unsigned int i; |
| 4127 |
|
| 4128 |
if (_ssl_locks == NULL) { |
| 4129 |
_ssl_locks_count = CRYPTO_num_locks(); |
| 4130 |
_ssl_locks = PyMem_New(PyThread_type_lock, _ssl_locks_count); |
| 4131 |
if (_ssl_locks == NULL) { |
| 4132 |
PyErr_NoMemory(); |
| 4133 |
return 0; |
| 4134 |
} |
| 4135 |
memset(_ssl_locks, 0, |
| 4136 |
sizeof(PyThread_type_lock) * _ssl_locks_count); |
| 4137 |
for (i = 0; i < _ssl_locks_count; i++) { |
| 4138 |
_ssl_locks[i] = PyThread_allocate_lock(); |
| 4139 |
if (_ssl_locks[i] == NULL) { |
| 4140 |
unsigned int j; |
| 4141 |
for (j = 0; j < i; j++) { |
| 4142 |
PyThread_free_lock(_ssl_locks[j]); |
| 4143 |
} |
| 4144 |
PyMem_Free(_ssl_locks); |
| 4145 |
return 0; |
| 4146 |
} |
| 4147 |
} |
| 4148 |
CRYPTO_set_locking_callback(_ssl_thread_locking_function); |
| 4149 |
#if OPENSSL_VERSION_NUMBER >= 0x10000000 |
| 4150 |
CRYPTO_THREADID_set_callback(_ssl_threadid_callback); |
| 4151 |
#else |
| 4152 |
CRYPTO_set_id_callback(_ssl_thread_id_function); |
| 4153 |
#endif |
| 4154 |
} |
| 4155 |
return 1; |
| 4156 |
} |
| 4157 |
|
| 4158 |
#endif |
| 4159 |
|
| 4160 |
PyDoc_STRVAR(module_doc, |
| 4161 |
"Implementation module for SSL socket operations. See the socket module\n\ |
| 4162 |
for documentation."); |
| 4163 |
|
| 4164 |
|
| 4165 |
|
| 4166 |
|
| 4167 |
static void |
| 4168 |
parse_openssl_version(unsigned long libver, |
| 4169 |
unsigned int *major, unsigned int *minor, |
| 4170 |
unsigned int *fix, unsigned int *patch, |
| 4171 |
unsigned int *status) |
| 4172 |
{ |
| 4173 |
*status = libver & 0xF; |
| 4174 |
libver >>= 4; |
| 4175 |
*patch = libver & 0xFF; |
| 4176 |
libver >>= 8; |
| 4177 |
*fix = libver & 0xFF; |
| 4178 |
libver >>= 8; |
| 4179 |
*minor = libver & 0xFF; |
| 4180 |
libver >>= 8; |
| 4181 |
*major = libver & 0xFF; |
| 4182 |
} |
| 4183 |
|
| 4184 |
PyMODINIT_FUNC |
| 4185 |
init_ssl(void) |
| 4186 |
{ |
| 4187 |
PyObject *m, *d, *r; |
| 4188 |
unsigned long libver; |
| 4189 |
unsigned int major, minor, fix, patch, status; |
| 4190 |
struct py_ssl_error_code *errcode; |
| 4191 |
struct py_ssl_library_code *libcode; |
| 4192 |
|
| 4193 |
if (PyType_Ready(&PySSLContext_Type) < 0) |
| 4194 |
return; |
| 4195 |
if (PyType_Ready(&PySSLSocket_Type) < 0) |
| 4196 |
return; |
| 4197 |
|
| 4198 |
m = Py_InitModule3("_ssl", PySSL_methods, module_doc); |
| 4199 |
if (m == NULL) |
| 4200 |
return; |
| 4201 |
d = PyModule_GetDict(m); |
| 4202 |
|
| 4203 |
|
| 4204 |
if (PySocketModule_ImportModuleAndAPI()) |
| 4205 |
return; |
| 4206 |
|
| 4207 |
#ifndef OPENSSL_VERSION_1_1 |
| 4208 |
|
| 4209 |
OPENSSL_add_all_algorithms_noconf(); |
| 4210 |
|
| 4211 |
SSL_load_error_strings(); |
| 4212 |
SSL_library_init(); |
| 4213 |
#endif |
| 4214 |
|
| 4215 |
#ifdef WITH_THREAD |
| 4216 |
#ifdef HAVE_OPENSSL_CRYPTO_LOCK |
| 4217 |
|
| 4218 |
if (!_setup_ssl_threads()) { |
| 4219 |
return; |
| 4220 |
} |
| 4221 |
#elif OPENSSL_VERSION_1_1 && defined(OPENSSL_THREADS) |
| 4222 |
|
| 4223 |
_ssl_locks_count++; |
| 4224 |
#endif |
| 4225 |
#endif |
| 4226 |
|
| 4227 |
|
| 4228 |
PySSLErrorObject = PyErr_NewExceptionWithDoc( |
| 4229 |
"ssl.SSLError", SSLError_doc, |
| 4230 |
PySocketModule.error, NULL); |
| 4231 |
if (PySSLErrorObject == NULL) |
| 4232 |
return; |
| 4233 |
((PyTypeObject *)PySSLErrorObject)->tp_str = (reprfunc)SSLError_str; |
| 4234 |
|
| 4235 |
PySSLZeroReturnErrorObject = PyErr_NewExceptionWithDoc( |
| 4236 |
"ssl.SSLZeroReturnError", SSLZeroReturnError_doc, |
| 4237 |
PySSLErrorObject, NULL); |
| 4238 |
PySSLWantReadErrorObject = PyErr_NewExceptionWithDoc( |
| 4239 |
"ssl.SSLWantReadError", SSLWantReadError_doc, |
| 4240 |
PySSLErrorObject, NULL); |
| 4241 |
PySSLWantWriteErrorObject = PyErr_NewExceptionWithDoc( |
| 4242 |
"ssl.SSLWantWriteError", SSLWantWriteError_doc, |
| 4243 |
PySSLErrorObject, NULL); |
| 4244 |
PySSLSyscallErrorObject = PyErr_NewExceptionWithDoc( |
| 4245 |
"ssl.SSLSyscallError", SSLSyscallError_doc, |
| 4246 |
PySSLErrorObject, NULL); |
| 4247 |
PySSLEOFErrorObject = PyErr_NewExceptionWithDoc( |
| 4248 |
"ssl.SSLEOFError", SSLEOFError_doc, |
| 4249 |
PySSLErrorObject, NULL); |
| 4250 |
if (PySSLZeroReturnErrorObject == NULL |
| 4251 |
|| PySSLWantReadErrorObject == NULL |
| 4252 |
|| PySSLWantWriteErrorObject == NULL |
| 4253 |
|| PySSLSyscallErrorObject == NULL |
| 4254 |
|| PySSLEOFErrorObject == NULL) |
| 4255 |
return; |
| 4256 |
|
| 4257 |
((PyTypeObject *)PySSLZeroReturnErrorObject)->tp_str = (reprfunc)SSLError_str; |
| 4258 |
((PyTypeObject *)PySSLWantReadErrorObject)->tp_str = (reprfunc)SSLError_str; |
| 4259 |
((PyTypeObject *)PySSLWantWriteErrorObject)->tp_str = (reprfunc)SSLError_str; |
| 4260 |
((PyTypeObject *)PySSLSyscallErrorObject)->tp_str = (reprfunc)SSLError_str; |
| 4261 |
((PyTypeObject *)PySSLEOFErrorObject)->tp_str = (reprfunc)SSLError_str; |
| 4262 |
|
| 4263 |
if (PyDict_SetItemString(d, "SSLError", PySSLErrorObject) != 0 |
| 4264 |
|| PyDict_SetItemString(d, "SSLZeroReturnError", PySSLZeroReturnErrorObject) != 0 |
| 4265 |
|| PyDict_SetItemString(d, "SSLWantReadError", PySSLWantReadErrorObject) != 0 |
| 4266 |
|| PyDict_SetItemString(d, "SSLWantWriteError", PySSLWantWriteErrorObject) != 0 |
| 4267 |
|| PyDict_SetItemString(d, "SSLSyscallError", PySSLSyscallErrorObject) != 0 |
| 4268 |
|| PyDict_SetItemString(d, "SSLEOFError", PySSLEOFErrorObject) != 0) |
| 4269 |
return; |
| 4270 |
if (PyDict_SetItemString(d, "_SSLContext", |
| 4271 |
(PyObject *)&PySSLContext_Type) != 0) |
| 4272 |
return; |
| 4273 |
if (PyDict_SetItemString(d, "_SSLSocket", |
| 4274 |
(PyObject *)&PySSLSocket_Type) != 0) |
| 4275 |
return; |
| 4276 |
PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN", |
| 4277 |
PY_SSL_ERROR_ZERO_RETURN); |
| 4278 |
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ", |
| 4279 |
PY_SSL_ERROR_WANT_READ); |
| 4280 |
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_WRITE", |
| 4281 |
PY_SSL_ERROR_WANT_WRITE); |
| 4282 |
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_X509_LOOKUP", |
| 4283 |
PY_SSL_ERROR_WANT_X509_LOOKUP); |
| 4284 |
PyModule_AddIntConstant(m, "SSL_ERROR_SYSCALL", |
| 4285 |
PY_SSL_ERROR_SYSCALL); |
| 4286 |
PyModule_AddIntConstant(m, "SSL_ERROR_SSL", |
| 4287 |
PY_SSL_ERROR_SSL); |
| 4288 |
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_CONNECT", |
| 4289 |
PY_SSL_ERROR_WANT_CONNECT); |
| 4290 |
|
| 4291 |
PyModule_AddIntConstant(m, "SSL_ERROR_EOF", |
| 4292 |
PY_SSL_ERROR_EOF); |
| 4293 |
PyModule_AddIntConstant(m, "SSL_ERROR_INVALID_ERROR_CODE", |
| 4294 |
PY_SSL_ERROR_INVALID_ERROR_CODE); |
| 4295 |
|
| 4296 |
PyModule_AddIntConstant(m, "CERT_NONE", |
| 4297 |
PY_SSL_CERT_NONE); |
| 4298 |
PyModule_AddIntConstant(m, "CERT_OPTIONAL", |
| 4299 |
PY_SSL_CERT_OPTIONAL); |
| 4300 |
PyModule_AddIntConstant(m, "CERT_REQUIRED", |
| 4301 |
PY_SSL_CERT_REQUIRED); |
| 4302 |
|
| 4303 |
PyModule_AddIntConstant(m, "VERIFY_DEFAULT", |
| 4304 |
0); |
| 4305 |
PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_LEAF", |
| 4306 |
X509_V_FLAG_CRL_CHECK); |
| 4307 |
PyModule_AddIntConstant(m, "VERIFY_CRL_CHECK_CHAIN", |
| 4308 |
X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4309 |
PyModule_AddIntConstant(m, "VERIFY_X509_STRICT", |
| 4310 |
X509_V_FLAG_X509_STRICT); |
| 4311 |
#ifdef X509_V_FLAG_TRUSTED_FIRST |
| 4312 |
PyModule_AddIntConstant(m, "VERIFY_X509_TRUSTED_FIRST", |
| 4313 |
X509_V_FLAG_TRUSTED_FIRST); |
| 4314 |
#endif |
| 4315 |
|
| 4316 |
|
| 4317 |
|
| 4318 |
|
| 4319 |
|
| 4320 |
#define ADD_AD_CONSTANT(s) \ |
| 4321 |
PyModule_AddIntConstant(m, "ALERT_DESCRIPTION_"#s, \ |
| 4322 |
SSL_AD_##s) |
| 4323 |
|
| 4324 |
ADD_AD_CONSTANT(CLOSE_NOTIFY); |
| 4325 |
ADD_AD_CONSTANT(UNEXPECTED_MESSAGE); |
| 4326 |
ADD_AD_CONSTANT(BAD_RECORD_MAC); |
| 4327 |
ADD_AD_CONSTANT(RECORD_OVERFLOW); |
| 4328 |
ADD_AD_CONSTANT(DECOMPRESSION_FAILURE); |
| 4329 |
ADD_AD_CONSTANT(HANDSHAKE_FAILURE); |
| 4330 |
ADD_AD_CONSTANT(BAD_CERTIFICATE); |
| 4331 |
ADD_AD_CONSTANT(UNSUPPORTED_CERTIFICATE); |
| 4332 |
ADD_AD_CONSTANT(CERTIFICATE_REVOKED); |
| 4333 |
ADD_AD_CONSTANT(CERTIFICATE_EXPIRED); |
| 4334 |
ADD_AD_CONSTANT(CERTIFICATE_UNKNOWN); |
| 4335 |
ADD_AD_CONSTANT(ILLEGAL_PARAMETER); |
| 4336 |
ADD_AD_CONSTANT(UNKNOWN_CA); |
| 4337 |
ADD_AD_CONSTANT(ACCESS_DENIED); |
| 4338 |
ADD_AD_CONSTANT(DECODE_ERROR); |
| 4339 |
ADD_AD_CONSTANT(DECRYPT_ERROR); |
| 4340 |
ADD_AD_CONSTANT(PROTOCOL_VERSION); |
| 4341 |
ADD_AD_CONSTANT(INSUFFICIENT_SECURITY); |
| 4342 |
ADD_AD_CONSTANT(INTERNAL_ERROR); |
| 4343 |
ADD_AD_CONSTANT(USER_CANCELLED); |
| 4344 |
ADD_AD_CONSTANT(NO_RENEGOTIATION); |
| 4345 |
|
| 4346 |
#ifdef SSL_AD_UNSUPPORTED_EXTENSION |
| 4347 |
ADD_AD_CONSTANT(UNSUPPORTED_EXTENSION); |
| 4348 |
#endif |
| 4349 |
#ifdef SSL_AD_CERTIFICATE_UNOBTAINABLE |
| 4350 |
ADD_AD_CONSTANT(CERTIFICATE_UNOBTAINABLE); |
| 4351 |
#endif |
| 4352 |
#ifdef SSL_AD_UNRECOGNIZED_NAME |
| 4353 |
ADD_AD_CONSTANT(UNRECOGNIZED_NAME); |
| 4354 |
#endif |
| 4355 |
#ifdef SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
| 4356 |
ADD_AD_CONSTANT(BAD_CERTIFICATE_STATUS_RESPONSE); |
| 4357 |
#endif |
| 4358 |
#ifdef SSL_AD_BAD_CERTIFICATE_HASH_VALUE |
| 4359 |
ADD_AD_CONSTANT(BAD_CERTIFICATE_HASH_VALUE); |
| 4360 |
#endif |
| 4361 |
#ifdef SSL_AD_UNKNOWN_PSK_IDENTITY |
| 4362 |
ADD_AD_CONSTANT(UNKNOWN_PSK_IDENTITY); |
| 4363 |
#endif |
| 4364 |
|
| 4365 |
#undef ADD_AD_CONSTANT |
| 4366 |
|
| 4367 |
|
| 4368 |
#ifndef OPENSSL_NO_SSL2 |
| 4369 |
PyModule_AddIntConstant(m, "PROTOCOL_SSLv2", |
| 4370 |
PY_SSL_VERSION_SSL2); |
| 4371 |
#endif |
| 4372 |
#ifndef OPENSSL_NO_SSL3 |
| 4373 |
PyModule_AddIntConstant(m, "PROTOCOL_SSLv3", |
| 4374 |
PY_SSL_VERSION_SSL3); |
| 4375 |
#endif |
| 4376 |
PyModule_AddIntConstant(m, "PROTOCOL_SSLv23", |
| 4377 |
PY_SSL_VERSION_TLS); |
| 4378 |
PyModule_AddIntConstant(m, "PROTOCOL_TLS", |
| 4379 |
PY_SSL_VERSION_TLS); |
| 4380 |
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1", |
| 4381 |
PY_SSL_VERSION_TLS1); |
| 4382 |
#if HAVE_TLSv1_2 |
| 4383 |
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1", |
| 4384 |
PY_SSL_VERSION_TLS1_1); |
| 4385 |
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2", |
| 4386 |
PY_SSL_VERSION_TLS1_2); |
| 4387 |
#endif |
| 4388 |
|
| 4389 |
|
| 4390 |
PyModule_AddIntConstant(m, "OP_ALL", |
| 4391 |
SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
| 4392 |
PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 4393 |
PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 4394 |
PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
| 4395 |
#if HAVE_TLSv1_2 |
| 4396 |
PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1); |
| 4397 |
PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2); |
| 4398 |
#endif |
| 4399 |
#ifdef SSL_OP_NO_TLSv1_3 |
| 4400 |
PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3); |
| 4401 |
#else |
| 4402 |
PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0); |
| 4403 |
#endif |
| 4404 |
PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE", |
| 4405 |
SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 4406 |
PyModule_AddIntConstant(m, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
| 4407 |
#ifdef SSL_OP_SINGLE_ECDH_USE |
| 4408 |
PyModule_AddIntConstant(m, "OP_SINGLE_ECDH_USE", SSL_OP_SINGLE_ECDH_USE); |
| 4409 |
#endif |
| 4410 |
#ifdef SSL_OP_NO_COMPRESSION |
| 4411 |
PyModule_AddIntConstant(m, "OP_NO_COMPRESSION", |
| 4412 |
SSL_OP_NO_COMPRESSION); |
| 4413 |
#endif |
| 4414 |
#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 4415 |
PyModule_AddIntConstant(m, "OP_ENABLE_MIDDLEBOX_COMPAT", |
| 4416 |
SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 4417 |
#endif |
| 4418 |
|
| 4419 |
#if HAVE_SNI |
| 4420 |
r = Py_True; |
| 4421 |
#else |
| 4422 |
r = Py_False; |
| 4423 |
#endif |
| 4424 |
Py_INCREF(r); |
| 4425 |
PyModule_AddObject(m, "HAS_SNI", r); |
| 4426 |
|
| 4427 |
#if HAVE_OPENSSL_FINISHED |
| 4428 |
r = Py_True; |
| 4429 |
#else |
| 4430 |
r = Py_False; |
| 4431 |
#endif |
| 4432 |
Py_INCREF(r); |
| 4433 |
PyModule_AddObject(m, "HAS_TLS_UNIQUE", r); |
| 4434 |
|
| 4435 |
#ifdef OPENSSL_NO_ECDH |
| 4436 |
r = Py_False; |
| 4437 |
#else |
| 4438 |
r = Py_True; |
| 4439 |
#endif |
| 4440 |
Py_INCREF(r); |
| 4441 |
PyModule_AddObject(m, "HAS_ECDH", r); |
| 4442 |
|
| 4443 |
#if HAVE_NPN |
| 4444 |
r = Py_True; |
| 4445 |
#else |
| 4446 |
r = Py_False; |
| 4447 |
#endif |
| 4448 |
Py_INCREF(r); |
| 4449 |
PyModule_AddObject(m, "HAS_NPN", r); |
| 4450 |
|
| 4451 |
#if HAVE_ALPN |
| 4452 |
r = Py_True; |
| 4453 |
#else |
| 4454 |
r = Py_False; |
| 4455 |
#endif |
| 4456 |
Py_INCREF(r); |
| 4457 |
PyModule_AddObject(m, "HAS_ALPN", r); |
| 4458 |
|
| 4459 |
#if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3) |
| 4460 |
r = Py_True; |
| 4461 |
#else |
| 4462 |
r = Py_False; |
| 4463 |
#endif |
| 4464 |
Py_INCREF(r); |
| 4465 |
PyModule_AddObject(m, "HAS_TLSv1_3", r); |
| 4466 |
|
| 4467 |
|
| 4468 |
err_codes_to_names = PyDict_New(); |
| 4469 |
err_names_to_codes = PyDict_New(); |
| 4470 |
if (err_codes_to_names == NULL || err_names_to_codes == NULL) |
| 4471 |
return; |
| 4472 |
errcode = error_codes; |
| 4473 |
while (errcode->mnemonic != NULL) { |
| 4474 |
PyObject *mnemo, *key; |
| 4475 |
mnemo = PyUnicode_FromString(errcode->mnemonic); |
| 4476 |
key = Py_BuildValue("ii", errcode->library, errcode->reason); |
| 4477 |
if (mnemo == NULL || key == NULL) |
| 4478 |
return; |
| 4479 |
if (PyDict_SetItem(err_codes_to_names, key, mnemo)) |
| 4480 |
return; |
| 4481 |
if (PyDict_SetItem(err_names_to_codes, mnemo, key)) |
| 4482 |
return; |
| 4483 |
Py_DECREF(key); |
| 4484 |
Py_DECREF(mnemo); |
| 4485 |
errcode++; |
| 4486 |
} |
| 4487 |
if (PyModule_AddObject(m, "err_codes_to_names", err_codes_to_names)) |
| 4488 |
return; |
| 4489 |
if (PyModule_AddObject(m, "err_names_to_codes", err_names_to_codes)) |
| 4490 |
return; |
| 4491 |
|
| 4492 |
lib_codes_to_names = PyDict_New(); |
| 4493 |
if (lib_codes_to_names == NULL) |
| 4494 |
return; |
| 4495 |
libcode = library_codes; |
| 4496 |
while (libcode->library != NULL) { |
| 4497 |
PyObject *mnemo, *key; |
| 4498 |
key = PyLong_FromLong(libcode->code); |
| 4499 |
mnemo = PyUnicode_FromString(libcode->library); |
| 4500 |
if (key == NULL || mnemo == NULL) |
| 4501 |
return; |
| 4502 |
if (PyDict_SetItem(lib_codes_to_names, key, mnemo)) |
| 4503 |
return; |
| 4504 |
Py_DECREF(key); |
| 4505 |
Py_DECREF(mnemo); |
| 4506 |
libcode++; |
| 4507 |
} |
| 4508 |
if (PyModule_AddObject(m, "lib_codes_to_names", lib_codes_to_names)) |
| 4509 |
return; |
| 4510 |
|
| 4511 |
|
| 4512 |
|
| 4513 |
|
| 4514 |
|
| 4515 |
libver = SSLeay(); |
| 4516 |
r = PyLong_FromUnsignedLong(libver); |
| 4517 |
if (r == NULL) |
| 4518 |
return; |
| 4519 |
if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r)) |
| 4520 |
return; |
| 4521 |
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 4522 |
r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 4523 |
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r)) |
| 4524 |
return; |
| 4525 |
r = PyString_FromString(SSLeay_version(SSLEAY_VERSION)); |
| 4526 |
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r)) |
| 4527 |
return; |
| 4528 |
|
| 4529 |
libver = OPENSSL_VERSION_NUMBER; |
| 4530 |
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status); |
| 4531 |
r = Py_BuildValue("IIIII", major, minor, fix, patch, status); |
| 4532 |
if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r)) |
| 4533 |
return; |
| 4534 |
} |