Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * ssl.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 5 | * Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | * |
| 7 | * Main file of the SSL sub module. |
| 8 | * See the file RATIONALE for a short explanation of why this module was written. |
| 9 | * |
| 10 | * Reviewed 2001-07-23 |
| 11 | */ |
| 12 | #include <Python.h> |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 13 | |
Jean-Paul Calderone | 12ea9a0 | 2008-02-22 12:24:39 -0500 | [diff] [blame] | 14 | #ifndef MS_WINDOWS |
| 15 | # include <sys/socket.h> |
| 16 | # include <netinet/in.h> |
| 17 | # if !(defined(__BEOS__) || defined(__CYGWIN__)) |
| 18 | # include <netinet/tcp.h> |
| 19 | # endif |
| 20 | #else |
| 21 | # include <winsock.h> |
| 22 | # include <wincrypt.h> |
| 23 | #endif |
| 24 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 25 | #define SSL_MODULE |
| 26 | #include "ssl.h" |
| 27 | |
| 28 | static char ssl_doc[] = "\n\ |
| 29 | Main file of the SSL sub module.\n\ |
Jean-Paul Calderone | 5aa15c7 | 2008-03-04 22:20:17 -0500 | [diff] [blame] | 30 | See the file RATIONALE for a short explanation of why this module was written.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 31 | "; |
| 32 | |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 33 | #ifndef PY3 |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 34 | void **crypto_API; |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 35 | #endif |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 36 | |
Jean-Paul Calderone | 00db9da | 2008-09-21 17:42:34 -0400 | [diff] [blame] | 37 | int _pyOpenSSL_tstate_key; |
| 38 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 39 | /* Exceptions defined by the SSL submodule */ |
| 40 | PyObject *ssl_Error, /* Base class */ |
| 41 | *ssl_ZeroReturnError, /* Used with SSL_get_error */ |
| 42 | *ssl_WantReadError, /* ... */ |
| 43 | *ssl_WantWriteError, /* ... */ |
| 44 | *ssl_WantX509LookupError, /* ... */ |
| 45 | *ssl_SysCallError; /* Uses (errno,errstr) */ |
| 46 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 47 | |
| 48 | /* Methods in the OpenSSL.SSL module */ |
| 49 | static PyMethodDef ssl_methods[] = { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 50 | { NULL, NULL } |
| 51 | }; |
| 52 | |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 53 | #ifdef PY3 |
| 54 | static struct PyModuleDef sslmodule = { |
| 55 | PyModuleDef_HEAD_INIT, |
| 56 | "SSL", |
| 57 | ssl_doc, |
| 58 | -1, |
| 59 | ssl_methods |
| 60 | }; |
| 61 | #endif |
| 62 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 63 | /* |
| 64 | * Initialize SSL sub module |
| 65 | * |
| 66 | * Arguments: None |
| 67 | * Returns: None |
| 68 | */ |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 69 | PyOpenSSL_MODINIT(SSL) { |
| 70 | PyObject *module; |
| 71 | #ifndef PY3 |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 72 | static void *ssl_API[ssl_API_pointers]; |
| 73 | PyObject *ssl_api_object; |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 74 | |
| 75 | import_crypto(); |
| 76 | #endif |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 77 | |
| 78 | SSL_library_init(); |
| 79 | ERR_load_SSL_strings(); |
| 80 | |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 81 | #ifdef PY3 |
| 82 | module = PyModule_Create(&sslmodule); |
| 83 | #else |
| 84 | module = Py_InitModule3("SSL", ssl_methods, ssl_doc); |
| 85 | #endif |
| 86 | if (module == NULL) { |
| 87 | return NULL; |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 88 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 89 | |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 90 | #ifndef PY3 |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 91 | /* Initialize the C API pointer array */ |
| 92 | ssl_API[ssl_Context_New_NUM] = (void *)ssl_Context_New; |
| 93 | ssl_API[ssl_Connection_New_NUM] = (void *)ssl_Connection_New; |
| 94 | ssl_api_object = PyCObject_FromVoidPtr((void *)ssl_API, NULL); |
| 95 | if (ssl_api_object != NULL) |
| 96 | PyModule_AddObject(module, "_C_API", ssl_api_object); |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 97 | #endif |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 98 | |
| 99 | /* Exceptions */ |
| 100 | /* |
| 101 | * ADD_EXCEPTION(dict,name,base) expands to a correct Exception declaration, |
| 102 | * inserting OpenSSL.SSL.name into dict, derviving the exception from base. |
| 103 | */ |
| 104 | #define ADD_EXCEPTION(_name, _base) \ |
| 105 | do { \ |
| 106 | ssl_##_name = PyErr_NewException("OpenSSL.SSL."#_name, _base, NULL);\ |
| 107 | if (ssl_##_name == NULL) \ |
| 108 | goto error; \ |
| 109 | if (PyModule_AddObject(module, #_name, ssl_##_name) != 0) \ |
| 110 | goto error; \ |
| 111 | } while (0) |
| 112 | |
| 113 | ssl_Error = PyErr_NewException("OpenSSL.SSL.Error", NULL, NULL); |
| 114 | if (ssl_Error == NULL) |
| 115 | goto error; |
| 116 | if (PyModule_AddObject(module, "Error", ssl_Error) != 0) |
| 117 | goto error; |
| 118 | |
| 119 | ADD_EXCEPTION(ZeroReturnError, ssl_Error); |
| 120 | ADD_EXCEPTION(WantReadError, ssl_Error); |
| 121 | ADD_EXCEPTION(WantWriteError, ssl_Error); |
| 122 | ADD_EXCEPTION(WantX509LookupError, ssl_Error); |
| 123 | ADD_EXCEPTION(SysCallError, ssl_Error); |
| 124 | #undef ADD_EXCEPTION |
| 125 | |
| 126 | /* Method constants */ |
| 127 | PyModule_AddIntConstant(module, "SSLv2_METHOD", ssl_SSLv2_METHOD); |
| 128 | PyModule_AddIntConstant(module, "SSLv3_METHOD", ssl_SSLv3_METHOD); |
| 129 | PyModule_AddIntConstant(module, "SSLv23_METHOD", ssl_SSLv23_METHOD); |
| 130 | PyModule_AddIntConstant(module, "TLSv1_METHOD", ssl_TLSv1_METHOD); |
| 131 | |
| 132 | /* Verify constants */ |
| 133 | PyModule_AddIntConstant(module, "VERIFY_NONE", SSL_VERIFY_NONE); |
| 134 | PyModule_AddIntConstant(module, "VERIFY_PEER", SSL_VERIFY_PEER); |
| 135 | PyModule_AddIntConstant(module, "VERIFY_FAIL_IF_NO_PEER_CERT", |
| 136 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); |
| 137 | PyModule_AddIntConstant(module, "VERIFY_CLIENT_ONCE", |
| 138 | SSL_VERIFY_CLIENT_ONCE); |
| 139 | |
| 140 | /* File type constants */ |
| 141 | PyModule_AddIntConstant(module, "FILETYPE_PEM", SSL_FILETYPE_PEM); |
| 142 | PyModule_AddIntConstant(module, "FILETYPE_ASN1", SSL_FILETYPE_ASN1); |
| 143 | |
| 144 | /* SSL option constants */ |
| 145 | PyModule_AddIntConstant(module, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE); |
| 146 | PyModule_AddIntConstant(module, "OP_EPHEMERAL_RSA", SSL_OP_EPHEMERAL_RSA); |
| 147 | PyModule_AddIntConstant(module, "OP_NO_SSLv2", SSL_OP_NO_SSLv2); |
| 148 | PyModule_AddIntConstant(module, "OP_NO_SSLv3", SSL_OP_NO_SSLv3); |
| 149 | PyModule_AddIntConstant(module, "OP_NO_TLSv1", SSL_OP_NO_TLSv1); |
| 150 | |
| 151 | /* More SSL option constants */ |
| 152 | PyModule_AddIntConstant(module, "OP_MICROSOFT_SESS_ID_BUG", SSL_OP_MICROSOFT_SESS_ID_BUG); |
| 153 | PyModule_AddIntConstant(module, "OP_NETSCAPE_CHALLENGE_BUG", SSL_OP_NETSCAPE_CHALLENGE_BUG); |
| 154 | PyModule_AddIntConstant(module, "OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG); |
| 155 | PyModule_AddIntConstant(module, "OP_SSLREF2_REUSE_CERT_TYPE_BUG", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG); |
| 156 | PyModule_AddIntConstant(module, "OP_MICROSOFT_BIG_SSLV3_BUFFER", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER); |
| 157 | PyModule_AddIntConstant(module, "OP_MSIE_SSLV2_RSA_PADDING", SSL_OP_MSIE_SSLV2_RSA_PADDING); |
| 158 | PyModule_AddIntConstant(module, "OP_SSLEAY_080_CLIENT_DH_BUG", SSL_OP_SSLEAY_080_CLIENT_DH_BUG); |
| 159 | PyModule_AddIntConstant(module, "OP_TLS_D5_BUG", SSL_OP_TLS_D5_BUG); |
| 160 | PyModule_AddIntConstant(module, "OP_TLS_BLOCK_PADDING_BUG", SSL_OP_TLS_BLOCK_PADDING_BUG); |
| 161 | PyModule_AddIntConstant(module, "OP_DONT_INSERT_EMPTY_FRAGMENTS", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
| 162 | PyModule_AddIntConstant(module, "OP_ALL", SSL_OP_ALL); |
| 163 | PyModule_AddIntConstant(module, "OP_CIPHER_SERVER_PREFERENCE", SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 164 | PyModule_AddIntConstant(module, "OP_TLS_ROLLBACK_BUG", SSL_OP_TLS_ROLLBACK_BUG); |
| 165 | PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_1", SSL_OP_PKCS1_CHECK_1); |
| 166 | PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_2", SSL_OP_PKCS1_CHECK_2); |
| 167 | PyModule_AddIntConstant(module, "OP_NETSCAPE_CA_DN_BUG", SSL_OP_NETSCAPE_CA_DN_BUG); |
| 168 | PyModule_AddIntConstant(module, "OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); |
| 169 | |
Jean-Paul Calderone | b43c391 | 2008-12-28 22:30:56 -0500 | [diff] [blame] | 170 | /* DTLS related options. The first two of these were introduced in |
| 171 | * 2005, the third in 2007. To accomodate systems which are still using |
| 172 | * older versions, make them optional. */ |
| 173 | #ifdef SSL_OP_NO_QUERY_MTU |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 174 | PyModule_AddIntConstant(module, "OP_NO_QUERY_MTU", SSL_OP_NO_QUERY_MTU); |
Jean-Paul Calderone | b43c391 | 2008-12-28 22:30:56 -0500 | [diff] [blame] | 175 | #endif |
| 176 | #ifdef SSL_OP_COOKIE_EXCHANGE |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 177 | PyModule_AddIntConstant(module, "OP_COOKIE_EXCHANGE", SSL_OP_COOKIE_EXCHANGE); |
Jean-Paul Calderone | b43c391 | 2008-12-28 22:30:56 -0500 | [diff] [blame] | 178 | #endif |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 179 | #ifdef SSL_OP_NO_TICKET |
| 180 | PyModule_AddIntConstant(module, "OP_NO_TICKET", SSL_OP_NO_TICKET); |
| 181 | #endif |
| 182 | |
| 183 | /* For SSL_set_shutdown */ |
Jean-Paul Calderone | 72b8f0f | 2008-02-21 23:57:40 -0500 | [diff] [blame] | 184 | PyModule_AddIntConstant(module, "SENT_SHUTDOWN", SSL_SENT_SHUTDOWN); |
| 185 | PyModule_AddIntConstant(module, "RECEIVED_SHUTDOWN", SSL_RECEIVED_SHUTDOWN); |
| 186 | |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 187 | if (!init_ssl_context(module)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 188 | goto error; |
Jean-Paul Calderone | 1bd11fa | 2009-05-27 17:09:15 -0400 | [diff] [blame] | 189 | if (!init_ssl_connection(module)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 190 | goto error; |
| 191 | |
Jean-Paul Calderone | 00db9da | 2008-09-21 17:42:34 -0400 | [diff] [blame] | 192 | #ifdef WITH_THREAD |
| 193 | /* |
| 194 | * Initialize this module's threading support structures. |
| 195 | */ |
| 196 | _pyOpenSSL_tstate_key = PyThread_create_key(); |
| 197 | #endif |
| 198 | |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 199 | #ifdef PY3 |
| 200 | return module; |
| 201 | #endif |
| 202 | |
| 203 | error: |
| 204 | #ifdef PY3 |
| 205 | return NULL; |
| 206 | #else |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 207 | ; |
Jean-Paul Calderone | 83dbcfd | 2010-08-11 20:20:57 -0400 | [diff] [blame] | 208 | #endif |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 209 | } |