blob: 7a827a04700d5bdfa9b712e0483db696ccb7bd34 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * ssl.c
3 *
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05004 * Copyright (C) AB Strakt
5 * Copyright (C) Jean-Paul Calderone
6 * See LICENSE for details.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05007 *
8 * Main file of the SSL sub module.
9 * See the file RATIONALE for a short explanation of why this module was written.
10 *
11 * Reviewed 2001-07-23
12 */
13#include <Python.h>
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050014
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050015#ifndef MS_WINDOWS
16# include <sys/socket.h>
17# include <netinet/in.h>
18# if !(defined(__BEOS__) || defined(__CYGWIN__))
19# include <netinet/tcp.h>
20# endif
21#else
22# include <winsock.h>
23# include <wincrypt.h>
24#endif
25
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050026#define SSL_MODULE
27#include "ssl.h"
28
29static char ssl_doc[] = "\n\
30Main file of the SSL sub module.\n\
Jean-Paul Calderone5aa15c72008-03-04 22:20:17 -050031See the file RATIONALE for a short explanation of why this module was written.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050032";
33
Jean-Paul Calderone31ba5762010-11-01 17:30:41 -040034crypto_X509Obj* (*new_x509)(X509*, int);
35crypto_X509NameObj* (*new_x509name)(X509_NAME*, int);
36crypto_X509StoreObj* (*new_x509store)(X509_STORE*, int);
37
38
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -040039#ifndef PY3
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050040void **crypto_API;
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -040041#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050042
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040043int _pyOpenSSL_tstate_key;
44
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050045/* Exceptions defined by the SSL submodule */
46PyObject *ssl_Error, /* Base class */
47 *ssl_ZeroReturnError, /* Used with SSL_get_error */
48 *ssl_WantReadError, /* ... */
49 *ssl_WantWriteError, /* ... */
50 *ssl_WantX509LookupError, /* ... */
51 *ssl_SysCallError; /* Uses (errno,errstr) */
52
Jean-Paul Calderone2ca33022011-04-15 12:10:02 -040053static char ssl_SSLeay_version_doc[] = "\n\
54Return a string describing the version of OpenSSL in use.\n\
55\n\
56@param type: One of the SSLEAY_ constants defined in this module.\n\
57";
58
59static PyObject *
60ssl_SSLeay_version(PyObject *spam, PyObject *args) {
61 int t;
62 const char *version;
63
64 if (!PyArg_ParseTuple(args, "i:SSLeay_version", &t)) {
65 return NULL;
66 }
67
68 version = SSLeay_version(t);
69 return PyBytes_FromStringAndSize(version, strlen(version));
70}
71
72
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050073
74/* Methods in the OpenSSL.SSL module */
75static PyMethodDef ssl_methods[] = {
Jean-Paul Calderone2ca33022011-04-15 12:10:02 -040076 { "SSLeay_version", ssl_SSLeay_version, METH_VARARGS, ssl_SSLeay_version_doc },
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050077 { NULL, NULL }
78};
79
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -040080#ifdef PY3
81static struct PyModuleDef sslmodule = {
82 PyModuleDef_HEAD_INIT,
83 "SSL",
84 ssl_doc,
85 -1,
86 ssl_methods
87};
88#endif
89
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050090/*
91 * Initialize SSL sub module
92 *
93 * Arguments: None
94 * Returns: None
95 */
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -040096PyOpenSSL_MODINIT(SSL) {
97 PyObject *module;
98#ifndef PY3
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050099 static void *ssl_API[ssl_API_pointers];
100 PyObject *ssl_api_object;
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400101
102 import_crypto();
Jean-Paul Calderonee56627a2010-11-01 00:03:15 -0400103
104 new_x509 = crypto_X509_New;
105 new_x509name = crypto_X509Name_New;
106 new_x509store = crypto_X509Store_New;
Jean-Paul Calderone305626a2010-10-31 20:51:17 -0400107#else
Jean-Paul Calderoneff077d62010-10-31 21:09:45 -0400108# ifdef _WIN32
Jean-Paul Calderone305626a2010-10-31 20:51:17 -0400109 HMODULE crypto = GetModuleHandle("crypto.pyd");
110 if (crypto == NULL) {
111 PyErr_SetString(PyExc_RuntimeError, "Unable to get crypto module");
Jean-Paul Calderoned1ce64c2010-10-31 21:18:37 -0400112 PyOpenSSL_MODRETURN(NULL);
Jean-Paul Calderone305626a2010-10-31 20:51:17 -0400113 }
114
Jean-Paul Calderone040112f2010-10-31 23:26:13 -0400115 new_x509 = (crypto_X509Obj* (*)(X509*, int))GetProcAddress(crypto, "crypto_X509_New");
Jean-Paul Calderone5bcb3032010-10-31 23:30:29 -0400116 new_x509name = (crypto_X509NameObj* (*)(X509_NAME*, int))GetProcAddress(crypto, "crypto_X509Name_New");
117 new_x509store = (crypto_X509StoreObj* (*)(X509_STORE*, int))GetProcAddress(crypto, "crypto_X509Store_New");
Jean-Paul Calderone305626a2010-10-31 20:51:17 -0400118# else
Jean-Paul Calderone1e9312e2010-10-31 21:26:18 -0400119 new_x509 = crypto_X509_New;
Jean-Paul Calderone305626a2010-10-31 20:51:17 -0400120 new_x509name = crypto_X509Name_New;
Jean-Paul Calderone1e9312e2010-10-31 21:26:18 -0400121 new_x509store = crypto_X509Store_New;
Jean-Paul Calderone305626a2010-10-31 20:51:17 -0400122# endif
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400123#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500124
125 SSL_library_init();
126 ERR_load_SSL_strings();
127
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400128#ifdef PY3
129 module = PyModule_Create(&sslmodule);
130#else
131 module = Py_InitModule3("SSL", ssl_methods, ssl_doc);
132#endif
133 if (module == NULL) {
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -0400134 PyOpenSSL_MODRETURN(NULL);
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400135 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500136
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400137#ifndef PY3
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500138 /* Initialize the C API pointer array */
139 ssl_API[ssl_Context_New_NUM] = (void *)ssl_Context_New;
140 ssl_API[ssl_Connection_New_NUM] = (void *)ssl_Connection_New;
141 ssl_api_object = PyCObject_FromVoidPtr((void *)ssl_API, NULL);
142 if (ssl_api_object != NULL)
143 PyModule_AddObject(module, "_C_API", ssl_api_object);
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400144#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500145
146 /* Exceptions */
147/*
148 * ADD_EXCEPTION(dict,name,base) expands to a correct Exception declaration,
149 * inserting OpenSSL.SSL.name into dict, derviving the exception from base.
150 */
151#define ADD_EXCEPTION(_name, _base) \
152do { \
153 ssl_##_name = PyErr_NewException("OpenSSL.SSL."#_name, _base, NULL);\
154 if (ssl_##_name == NULL) \
155 goto error; \
156 if (PyModule_AddObject(module, #_name, ssl_##_name) != 0) \
157 goto error; \
158} while (0)
159
160 ssl_Error = PyErr_NewException("OpenSSL.SSL.Error", NULL, NULL);
161 if (ssl_Error == NULL)
162 goto error;
163 if (PyModule_AddObject(module, "Error", ssl_Error) != 0)
164 goto error;
165
166 ADD_EXCEPTION(ZeroReturnError, ssl_Error);
167 ADD_EXCEPTION(WantReadError, ssl_Error);
168 ADD_EXCEPTION(WantWriteError, ssl_Error);
169 ADD_EXCEPTION(WantX509LookupError, ssl_Error);
170 ADD_EXCEPTION(SysCallError, ssl_Error);
171#undef ADD_EXCEPTION
172
173 /* Method constants */
174 PyModule_AddIntConstant(module, "SSLv2_METHOD", ssl_SSLv2_METHOD);
175 PyModule_AddIntConstant(module, "SSLv3_METHOD", ssl_SSLv3_METHOD);
176 PyModule_AddIntConstant(module, "SSLv23_METHOD", ssl_SSLv23_METHOD);
177 PyModule_AddIntConstant(module, "TLSv1_METHOD", ssl_TLSv1_METHOD);
178
179 /* Verify constants */
180 PyModule_AddIntConstant(module, "VERIFY_NONE", SSL_VERIFY_NONE);
181 PyModule_AddIntConstant(module, "VERIFY_PEER", SSL_VERIFY_PEER);
182 PyModule_AddIntConstant(module, "VERIFY_FAIL_IF_NO_PEER_CERT",
183 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
184 PyModule_AddIntConstant(module, "VERIFY_CLIENT_ONCE",
185 SSL_VERIFY_CLIENT_ONCE);
186
187 /* File type constants */
188 PyModule_AddIntConstant(module, "FILETYPE_PEM", SSL_FILETYPE_PEM);
189 PyModule_AddIntConstant(module, "FILETYPE_ASN1", SSL_FILETYPE_ASN1);
190
191 /* SSL option constants */
192 PyModule_AddIntConstant(module, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE);
193 PyModule_AddIntConstant(module, "OP_EPHEMERAL_RSA", SSL_OP_EPHEMERAL_RSA);
194 PyModule_AddIntConstant(module, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
195 PyModule_AddIntConstant(module, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
196 PyModule_AddIntConstant(module, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
197
198 /* More SSL option constants */
199 PyModule_AddIntConstant(module, "OP_MICROSOFT_SESS_ID_BUG", SSL_OP_MICROSOFT_SESS_ID_BUG);
200 PyModule_AddIntConstant(module, "OP_NETSCAPE_CHALLENGE_BUG", SSL_OP_NETSCAPE_CHALLENGE_BUG);
201 PyModule_AddIntConstant(module, "OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG);
202 PyModule_AddIntConstant(module, "OP_SSLREF2_REUSE_CERT_TYPE_BUG", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
203 PyModule_AddIntConstant(module, "OP_MICROSOFT_BIG_SSLV3_BUFFER", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
204 PyModule_AddIntConstant(module, "OP_MSIE_SSLV2_RSA_PADDING", SSL_OP_MSIE_SSLV2_RSA_PADDING);
205 PyModule_AddIntConstant(module, "OP_SSLEAY_080_CLIENT_DH_BUG", SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
206 PyModule_AddIntConstant(module, "OP_TLS_D5_BUG", SSL_OP_TLS_D5_BUG);
207 PyModule_AddIntConstant(module, "OP_TLS_BLOCK_PADDING_BUG", SSL_OP_TLS_BLOCK_PADDING_BUG);
208 PyModule_AddIntConstant(module, "OP_DONT_INSERT_EMPTY_FRAGMENTS", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
209 PyModule_AddIntConstant(module, "OP_ALL", SSL_OP_ALL);
210 PyModule_AddIntConstant(module, "OP_CIPHER_SERVER_PREFERENCE", SSL_OP_CIPHER_SERVER_PREFERENCE);
211 PyModule_AddIntConstant(module, "OP_TLS_ROLLBACK_BUG", SSL_OP_TLS_ROLLBACK_BUG);
212 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_1", SSL_OP_PKCS1_CHECK_1);
213 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_2", SSL_OP_PKCS1_CHECK_2);
214 PyModule_AddIntConstant(module, "OP_NETSCAPE_CA_DN_BUG", SSL_OP_NETSCAPE_CA_DN_BUG);
215 PyModule_AddIntConstant(module, "OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
216
Jean-Paul Calderoneb43c3912008-12-28 22:30:56 -0500217 /* DTLS related options. The first two of these were introduced in
218 * 2005, the third in 2007. To accomodate systems which are still using
219 * older versions, make them optional. */
220#ifdef SSL_OP_NO_QUERY_MTU
Jean-Paul Calderone327d8f92008-12-28 21:55:56 -0500221 PyModule_AddIntConstant(module, "OP_NO_QUERY_MTU", SSL_OP_NO_QUERY_MTU);
Jean-Paul Calderoneb43c3912008-12-28 22:30:56 -0500222#endif
223#ifdef SSL_OP_COOKIE_EXCHANGE
Jean-Paul Calderone327d8f92008-12-28 21:55:56 -0500224 PyModule_AddIntConstant(module, "OP_COOKIE_EXCHANGE", SSL_OP_COOKIE_EXCHANGE);
Jean-Paul Calderoneb43c3912008-12-28 22:30:56 -0500225#endif
Jean-Paul Calderone327d8f92008-12-28 21:55:56 -0500226#ifdef SSL_OP_NO_TICKET
227 PyModule_AddIntConstant(module, "OP_NO_TICKET", SSL_OP_NO_TICKET);
228#endif
229
230 /* For SSL_set_shutdown */
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -0500231 PyModule_AddIntConstant(module, "SENT_SHUTDOWN", SSL_SENT_SHUTDOWN);
232 PyModule_AddIntConstant(module, "RECEIVED_SHUTDOWN", SSL_RECEIVED_SHUTDOWN);
233
Olivier Hervieua9aed932011-03-02 21:49:23 +0100234 /* For set_info_callback */
235 PyModule_AddIntConstant(module, "SSL_ST_CONNECT", SSL_ST_CONNECT);
236 PyModule_AddIntConstant(module, "SSL_ST_ACCEPT", SSL_ST_ACCEPT);
237 PyModule_AddIntConstant(module, "SSL_ST_MASK", SSL_ST_MASK);
238 PyModule_AddIntConstant(module, "SSL_ST_INIT", SSL_ST_INIT);
239 PyModule_AddIntConstant(module, "SSL_ST_BEFORE", SSL_ST_BEFORE);
240 PyModule_AddIntConstant(module, "SSL_ST_OK", SSL_ST_OK);
241 PyModule_AddIntConstant(module, "SSL_ST_RENEGOTIATE", SSL_ST_RENEGOTIATE);
242 PyModule_AddIntConstant(module, "SSL_CB_LOOP", SSL_CB_LOOP);
243 PyModule_AddIntConstant(module, "SSL_CB_EXIT", SSL_CB_EXIT);
244 PyModule_AddIntConstant(module, "SSL_CB_READ", SSL_CB_READ);
245 PyModule_AddIntConstant(module, "SSL_CB_WRITE", SSL_CB_WRITE);
246 PyModule_AddIntConstant(module, "SSL_CB_ALERT", SSL_CB_ALERT);
247 PyModule_AddIntConstant(module, "SSL_CB_READ_ALERT", SSL_CB_READ_ALERT);
248 PyModule_AddIntConstant(module, "SSL_CB_WRITE_ALERT", SSL_CB_WRITE_ALERT);
249 PyModule_AddIntConstant(module, "SSL_CB_ACCEPT_LOOP", SSL_CB_ACCEPT_LOOP);
250 PyModule_AddIntConstant(module, "SSL_CB_ACCEPT_EXIT", SSL_CB_ACCEPT_EXIT);
251 PyModule_AddIntConstant(module, "SSL_CB_CONNECT_LOOP", SSL_CB_CONNECT_LOOP);
252 PyModule_AddIntConstant(module, "SSL_CB_CONNECT_EXIT", SSL_CB_CONNECT_EXIT);
253 PyModule_AddIntConstant(module, "SSL_CB_HANDSHAKE_START", SSL_CB_HANDSHAKE_START);
254 PyModule_AddIntConstant(module, "SSL_CB_HANDSHAKE_DONE", SSL_CB_HANDSHAKE_DONE);
255
Jean-Paul Calderone2ca33022011-04-15 12:10:02 -0400256 /* Version information indicators, used with SSLeay_version */
257 PyModule_AddIntConstant(module, "SSLEAY_VERSION", SSLEAY_VERSION);
258 PyModule_AddIntConstant(module, "SSLEAY_CFLAGS", SSLEAY_CFLAGS);
259 PyModule_AddIntConstant(module, "SSLEAY_BUILT_ON", SSLEAY_BUILT_ON);
260 PyModule_AddIntConstant(module, "SSLEAY_PLATFORM", SSLEAY_PLATFORM);
261 PyModule_AddIntConstant(module, "SSLEAY_DIR", SSLEAY_DIR);
262
263 /* Straight up version number */
264 PyModule_AddIntConstant(module, "OPENSSL_VERSION_NUMBER", OPENSSL_VERSION_NUMBER);
265
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400266 if (!init_ssl_context(module))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500267 goto error;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400268 if (!init_ssl_connection(module))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500269 goto error;
270
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -0400271#ifdef WITH_THREAD
272 /*
273 * Initialize this module's threading support structures.
274 */
275 _pyOpenSSL_tstate_key = PyThread_create_key();
276#endif
277
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -0400278 PyOpenSSL_MODRETURN(module);
Jean-Paul Calderone83dbcfd2010-08-11 20:20:57 -0400279
280error:
Jean-Paul Calderoneb6d75252010-08-11 23:55:45 -0400281 PyOpenSSL_MODRETURN(NULL);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500282 ;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500283}