blob: 1f8cbcc0055eab03bb6fff08e75d05aa15fa5595 [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
45static char ssl_Context_doc[] = "\n\
46The factory function inserted in the module dictionary to create Context\n\
47objects\n\
48\n\
49Arguments: spam - Always NULL\n\
50 args - The Python argument tuple, should be:\n\
51 method - The SSL method to use\n\
52Returns: The Context object\n\
53";
54
55static PyObject *
56ssl_Context(PyObject *spam, PyObject *args)
57{
58 int method;
59
60 if (!PyArg_ParseTuple(args, "i:Context", &method))
61 return NULL;
62
63 return (PyObject *)ssl_Context_New(method);
64}
65
66static char ssl_Connection_doc[] = "\n\
67The factory function inserted in the module dictionary to create Connection\n\
68objects\n\
69\n\
70Arguments: spam - Always NULL\n\
71 args - The Python argument tuple, should be:\n\
72 ctx - An SSL Context to use for this connection\n\
73 sock - The socket to use for transport layer\n\
74Returns: The Connection object\n\
75";
76
77static PyObject *
78ssl_Connection(PyObject *spam, PyObject *args)
79{
80 ssl_ContextObj *ctx;
81 PyObject *sock;
82
83 if (!PyArg_ParseTuple(args, "O!O:Connection", &ssl_Context_Type, &ctx, &sock))
84 return NULL;
85
86 return (PyObject *)ssl_Connection_New(ctx, sock);
87}
88
89
90/* Methods in the OpenSSL.SSL module */
91static PyMethodDef ssl_methods[] = {
92 { "Context", ssl_Context, METH_VARARGS, ssl_Context_doc },
93 { "Connection", ssl_Connection, METH_VARARGS, ssl_Connection_doc },
94 { NULL, NULL }
95};
96
97/*
98 * Initialize SSL sub module
99 *
100 * Arguments: None
101 * Returns: None
102 */
103void
104initSSL(void)
105{
106 static void *ssl_API[ssl_API_pointers];
107 PyObject *ssl_api_object;
108 PyObject *module, *dict;
109
110 SSL_library_init();
111 ERR_load_SSL_strings();
112
113 import_crypto();
114
115 if ((module = Py_InitModule3("SSL", ssl_methods, ssl_doc)) == NULL)
116 return;
117
118 /* Initialize the C API pointer array */
119 ssl_API[ssl_Context_New_NUM] = (void *)ssl_Context_New;
120 ssl_API[ssl_Connection_New_NUM] = (void *)ssl_Connection_New;
121 ssl_api_object = PyCObject_FromVoidPtr((void *)ssl_API, NULL);
122 if (ssl_api_object != NULL)
123 PyModule_AddObject(module, "_C_API", ssl_api_object);
124
125 /* Exceptions */
126/*
127 * ADD_EXCEPTION(dict,name,base) expands to a correct Exception declaration,
128 * inserting OpenSSL.SSL.name into dict, derviving the exception from base.
129 */
130#define ADD_EXCEPTION(_name, _base) \
131do { \
132 ssl_##_name = PyErr_NewException("OpenSSL.SSL."#_name, _base, NULL);\
133 if (ssl_##_name == NULL) \
134 goto error; \
135 if (PyModule_AddObject(module, #_name, ssl_##_name) != 0) \
136 goto error; \
137} while (0)
138
139 ssl_Error = PyErr_NewException("OpenSSL.SSL.Error", NULL, NULL);
140 if (ssl_Error == NULL)
141 goto error;
142 if (PyModule_AddObject(module, "Error", ssl_Error) != 0)
143 goto error;
144
145 ADD_EXCEPTION(ZeroReturnError, ssl_Error);
146 ADD_EXCEPTION(WantReadError, ssl_Error);
147 ADD_EXCEPTION(WantWriteError, ssl_Error);
148 ADD_EXCEPTION(WantX509LookupError, ssl_Error);
149 ADD_EXCEPTION(SysCallError, ssl_Error);
150#undef ADD_EXCEPTION
151
152 /* Method constants */
153 PyModule_AddIntConstant(module, "SSLv2_METHOD", ssl_SSLv2_METHOD);
154 PyModule_AddIntConstant(module, "SSLv3_METHOD", ssl_SSLv3_METHOD);
155 PyModule_AddIntConstant(module, "SSLv23_METHOD", ssl_SSLv23_METHOD);
156 PyModule_AddIntConstant(module, "TLSv1_METHOD", ssl_TLSv1_METHOD);
157
158 /* Verify constants */
159 PyModule_AddIntConstant(module, "VERIFY_NONE", SSL_VERIFY_NONE);
160 PyModule_AddIntConstant(module, "VERIFY_PEER", SSL_VERIFY_PEER);
161 PyModule_AddIntConstant(module, "VERIFY_FAIL_IF_NO_PEER_CERT",
162 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
163 PyModule_AddIntConstant(module, "VERIFY_CLIENT_ONCE",
164 SSL_VERIFY_CLIENT_ONCE);
165
166 /* File type constants */
167 PyModule_AddIntConstant(module, "FILETYPE_PEM", SSL_FILETYPE_PEM);
168 PyModule_AddIntConstant(module, "FILETYPE_ASN1", SSL_FILETYPE_ASN1);
169
170 /* SSL option constants */
171 PyModule_AddIntConstant(module, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE);
172 PyModule_AddIntConstant(module, "OP_EPHEMERAL_RSA", SSL_OP_EPHEMERAL_RSA);
173 PyModule_AddIntConstant(module, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
174 PyModule_AddIntConstant(module, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
175 PyModule_AddIntConstant(module, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
176
177 /* More SSL option constants */
178 PyModule_AddIntConstant(module, "OP_MICROSOFT_SESS_ID_BUG", SSL_OP_MICROSOFT_SESS_ID_BUG);
179 PyModule_AddIntConstant(module, "OP_NETSCAPE_CHALLENGE_BUG", SSL_OP_NETSCAPE_CHALLENGE_BUG);
180 PyModule_AddIntConstant(module, "OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG);
181 PyModule_AddIntConstant(module, "OP_SSLREF2_REUSE_CERT_TYPE_BUG", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
182 PyModule_AddIntConstant(module, "OP_MICROSOFT_BIG_SSLV3_BUFFER", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
183 PyModule_AddIntConstant(module, "OP_MSIE_SSLV2_RSA_PADDING", SSL_OP_MSIE_SSLV2_RSA_PADDING);
184 PyModule_AddIntConstant(module, "OP_SSLEAY_080_CLIENT_DH_BUG", SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
185 PyModule_AddIntConstant(module, "OP_TLS_D5_BUG", SSL_OP_TLS_D5_BUG);
186 PyModule_AddIntConstant(module, "OP_TLS_BLOCK_PADDING_BUG", SSL_OP_TLS_BLOCK_PADDING_BUG);
187 PyModule_AddIntConstant(module, "OP_DONT_INSERT_EMPTY_FRAGMENTS", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
188 PyModule_AddIntConstant(module, "OP_ALL", SSL_OP_ALL);
189 PyModule_AddIntConstant(module, "OP_CIPHER_SERVER_PREFERENCE", SSL_OP_CIPHER_SERVER_PREFERENCE);
190 PyModule_AddIntConstant(module, "OP_TLS_ROLLBACK_BUG", SSL_OP_TLS_ROLLBACK_BUG);
191 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_1", SSL_OP_PKCS1_CHECK_1);
192 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_2", SSL_OP_PKCS1_CHECK_2);
193 PyModule_AddIntConstant(module, "OP_NETSCAPE_CA_DN_BUG", SSL_OP_NETSCAPE_CA_DN_BUG);
194 PyModule_AddIntConstant(module, "OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
195
Jean-Paul Calderone72b8f0f2008-02-21 23:57:40 -0500196 /* For SSL_set_shutdown */
197 PyModule_AddIntConstant(module, "SENT_SHUTDOWN", SSL_SENT_SHUTDOWN);
198 PyModule_AddIntConstant(module, "RECEIVED_SHUTDOWN", SSL_RECEIVED_SHUTDOWN);
199
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500200 dict = PyModule_GetDict(module);
201 if (!init_ssl_context(dict))
202 goto error;
203 if (!init_ssl_connection(dict))
204 goto error;
205
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -0400206#ifdef WITH_THREAD
207 /*
208 * Initialize this module's threading support structures.
209 */
210 _pyOpenSSL_tstate_key = PyThread_create_key();
211#endif
212
213 error:
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500214 ;
215}