blob: 94d4844641f639d25e726251d54f3dedfd4c5d4f [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * ssl.c
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
Jean-Paul Calderone8b63d452008-03-21 18:31:12 -04005 * Copyright (C) Jean-Paul Calderone 2008, All rights reserved
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006 *
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 Calderone12ea9a02008-02-22 12:24:39 -050013
Jean-Paul Calderone12ea9a02008-02-22 12:24:39 -050014#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 Calderone897bc252008-02-18 20:50:23 -050025#define SSL_MODULE
26#include "ssl.h"
27
28static char ssl_doc[] = "\n\
29Main file of the SSL sub module.\n\
Jean-Paul Calderone5aa15c72008-03-04 22:20:17 -050030See the file RATIONALE for a short explanation of why this module was written.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050031";
32
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050033void **crypto_API;
34
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040035int _pyOpenSSL_tstate_key;
36
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050037/* Exceptions defined by the SSL submodule */
38PyObject *ssl_Error, /* Base class */
39 *ssl_ZeroReturnError, /* Used with SSL_get_error */
40 *ssl_WantReadError, /* ... */
41 *ssl_WantWriteError, /* ... */
42 *ssl_WantX509LookupError, /* ... */
43 *ssl_SysCallError; /* Uses (errno,errstr) */
44
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050045
46/* Methods in the OpenSSL.SSL module */
47static PyMethodDef ssl_methods[] = {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050048 { NULL, NULL }
49};
50
51/*
52 * Initialize SSL sub module
53 *
54 * Arguments: None
55 * Returns: None
56 */
57void
58initSSL(void)
59{
60 static void *ssl_API[ssl_API_pointers];
61 PyObject *ssl_api_object;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -040062 PyObject *module;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050063
64 SSL_library_init();
65 ERR_load_SSL_strings();
66
67 import_crypto();
68
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -040069 if ((module = Py_InitModule3("SSL", ssl_methods, ssl_doc)) == NULL) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050070 return;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -040071 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050072
73 /* Initialize the C API pointer array */
74 ssl_API[ssl_Context_New_NUM] = (void *)ssl_Context_New;
75 ssl_API[ssl_Connection_New_NUM] = (void *)ssl_Connection_New;
76 ssl_api_object = PyCObject_FromVoidPtr((void *)ssl_API, NULL);
77 if (ssl_api_object != NULL)
78 PyModule_AddObject(module, "_C_API", ssl_api_object);
79
80 /* Exceptions */
81/*
82 * ADD_EXCEPTION(dict,name,base) expands to a correct Exception declaration,
83 * inserting OpenSSL.SSL.name into dict, derviving the exception from base.
84 */
85#define ADD_EXCEPTION(_name, _base) \
86do { \
87 ssl_##_name = PyErr_NewException("OpenSSL.SSL."#_name, _base, NULL);\
88 if (ssl_##_name == NULL) \
89 goto error; \
90 if (PyModule_AddObject(module, #_name, ssl_##_name) != 0) \
91 goto error; \
92} while (0)
93
94 ssl_Error = PyErr_NewException("OpenSSL.SSL.Error", NULL, NULL);
95 if (ssl_Error == NULL)
96 goto error;
97 if (PyModule_AddObject(module, "Error", ssl_Error) != 0)
98 goto error;
99
100 ADD_EXCEPTION(ZeroReturnError, ssl_Error);
101 ADD_EXCEPTION(WantReadError, ssl_Error);
102 ADD_EXCEPTION(WantWriteError, ssl_Error);
103 ADD_EXCEPTION(WantX509LookupError, ssl_Error);
104 ADD_EXCEPTION(SysCallError, ssl_Error);
105#undef ADD_EXCEPTION
106
107 /* Method constants */
108 PyModule_AddIntConstant(module, "SSLv2_METHOD", ssl_SSLv2_METHOD);
109 PyModule_AddIntConstant(module, "SSLv3_METHOD", ssl_SSLv3_METHOD);
110 PyModule_AddIntConstant(module, "SSLv23_METHOD", ssl_SSLv23_METHOD);
111 PyModule_AddIntConstant(module, "TLSv1_METHOD", ssl_TLSv1_METHOD);
112
113 /* Verify constants */
114 PyModule_AddIntConstant(module, "VERIFY_NONE", SSL_VERIFY_NONE);
115 PyModule_AddIntConstant(module, "VERIFY_PEER", SSL_VERIFY_PEER);
116 PyModule_AddIntConstant(module, "VERIFY_FAIL_IF_NO_PEER_CERT",
117 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
118 PyModule_AddIntConstant(module, "VERIFY_CLIENT_ONCE",
119 SSL_VERIFY_CLIENT_ONCE);
120
121 /* File type constants */
122 PyModule_AddIntConstant(module, "FILETYPE_PEM", SSL_FILETYPE_PEM);
123 PyModule_AddIntConstant(module, "FILETYPE_ASN1", SSL_FILETYPE_ASN1);
124
125 /* SSL option constants */
126 PyModule_AddIntConstant(module, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE);
127 PyModule_AddIntConstant(module, "OP_EPHEMERAL_RSA", SSL_OP_EPHEMERAL_RSA);
128 PyModule_AddIntConstant(module, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
129 PyModule_AddIntConstant(module, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
130 PyModule_AddIntConstant(module, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
131
132 /* More SSL option constants */
133 PyModule_AddIntConstant(module, "OP_MICROSOFT_SESS_ID_BUG", SSL_OP_MICROSOFT_SESS_ID_BUG);
134 PyModule_AddIntConstant(module, "OP_NETSCAPE_CHALLENGE_BUG", SSL_OP_NETSCAPE_CHALLENGE_BUG);
135 PyModule_AddIntConstant(module, "OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG);
136 PyModule_AddIntConstant(module, "OP_SSLREF2_REUSE_CERT_TYPE_BUG", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
137 PyModule_AddIntConstant(module, "OP_MICROSOFT_BIG_SSLV3_BUFFER", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
138 PyModule_AddIntConstant(module, "OP_MSIE_SSLV2_RSA_PADDING", SSL_OP_MSIE_SSLV2_RSA_PADDING);
139 PyModule_AddIntConstant(module, "OP_SSLEAY_080_CLIENT_DH_BUG", SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
140 PyModule_AddIntConstant(module, "OP_TLS_D5_BUG", SSL_OP_TLS_D5_BUG);
141 PyModule_AddIntConstant(module, "OP_TLS_BLOCK_PADDING_BUG", SSL_OP_TLS_BLOCK_PADDING_BUG);
142 PyModule_AddIntConstant(module, "OP_DONT_INSERT_EMPTY_FRAGMENTS", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
143 PyModule_AddIntConstant(module, "OP_ALL", SSL_OP_ALL);
144 PyModule_AddIntConstant(module, "OP_CIPHER_SERVER_PREFERENCE", SSL_OP_CIPHER_SERVER_PREFERENCE);
145 PyModule_AddIntConstant(module, "OP_TLS_ROLLBACK_BUG", SSL_OP_TLS_ROLLBACK_BUG);
146 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_1", SSL_OP_PKCS1_CHECK_1);
147 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_2", SSL_OP_PKCS1_CHECK_2);
148 PyModule_AddIntConstant(module, "OP_NETSCAPE_CA_DN_BUG", SSL_OP_NETSCAPE_CA_DN_BUG);
149 PyModule_AddIntConstant(module, "OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
150
Jean-Paul Calderoneb43c3912008-12-28 22:30:56 -0500151 /* DTLS related options. The first two of these were introduced in
152 * 2005, the third in 2007. To accomodate systems which are still using
153 * older versions, make them optional. */
154#ifdef SSL_OP_NO_QUERY_MTU
Jean-Paul Calderone327d8f92008-12-28 21:55:56 -0500155 PyModule_AddIntConstant(module, "OP_NO_QUERY_MTU", SSL_OP_NO_QUERY_MTU);
Jean-Paul Calderoneb43c3912008-12-28 22:30:56 -0500156#endif
157#ifdef SSL_OP_COOKIE_EXCHANGE
Jean-Paul Calderone327d8f92008-12-28 21:55:56 -0500158 PyModule_AddIntConstant(module, "OP_COOKIE_EXCHANGE", SSL_OP_COOKIE_EXCHANGE);
Jean-Paul Calderoneb43c3912008-12-28 22:30:56 -0500159#endif
Jean-Paul Calderone327d8f92008-12-28 21:55:56 -0500160#ifdef SSL_OP_NO_TICKET
161 PyModule_AddIntConstant(module, "OP_NO_TICKET", SSL_OP_NO_TICKET);
162#endif
163
164 /* For SSL_set_shutdown */
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -0500165 PyModule_AddIntConstant(module, "SENT_SHUTDOWN", SSL_SENT_SHUTDOWN);
166 PyModule_AddIntConstant(module, "RECEIVED_SHUTDOWN", SSL_RECEIVED_SHUTDOWN);
167
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400168 if (!init_ssl_context(module))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500169 goto error;
Jean-Paul Calderone1bd11fa2009-05-27 17:09:15 -0400170 if (!init_ssl_connection(module))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500171 goto error;
172
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -0400173#ifdef WITH_THREAD
174 /*
175 * Initialize this module's threading support structures.
176 */
177 _pyOpenSSL_tstate_key = PyThread_create_key();
178#endif
179
180 error:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500181 ;
182}