blob: f9932a4b15c5f572acfa2a79124701ec41a8be53 [file] [log] [blame]
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -05001/*
2 * session.c
3 *
4 * Copyright (C) Jean-Paul Calderone
5 * Copyright (C) Alejandro Alvarez Ayllon
6 * See LICENSE for details.
7 *
8 * SSL Session object data structures and functions.
9 *
10 */
11#include <Python.h>
12#define SSL_MODULE
13#include "ssl.h"
14
15static char ssl_Session_doc[] = "\n\
16Session() -> Session instance\n\
17\n\
18";
19
20/*
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -050021 * Initialize an already-constructed Session instance with an OpenSSL session
22 * structure (or NULL). A reference to the OpenSSL session structure is stolen.
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050023 */
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -050024static ssl_SessionObj*
25ssl_Session_init(ssl_SessionObj *self, SSL_SESSION *native_session) {
26 self->session = native_session;
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050027 return self;
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050028}
29
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050030/*
31 * Create a Session object
32 */
33static PyObject*
34ssl_Session_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
35 ssl_SessionObj *self;
36
37 if (!PyArg_ParseTuple(args, ":Session")) {
38 return NULL;
39 }
40
41 self = PyObject_New(ssl_SessionObj, &ssl_Session_Type);
42 if (self == NULL) {
43 return NULL;
44 }
45
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -050046 return (PyObject *)ssl_Session_init(self, NULL);
47}
48
49/*
50 * Create a Session object from an existing SSL_SESSION*. A reference to the
51 * SSL_SESSION* is stolen.
52 */
53ssl_SessionObj*
54ssl_Session_from_SSL_SESSION(SSL_SESSION *native_session) {
55 ssl_SessionObj *self;
56
57 self = PyObject_New(ssl_SessionObj, &ssl_Session_Type);
58 if (self == NULL) {
59 return NULL;
60 }
61
62 return ssl_Session_init(self, native_session);
63}
64
65/*
66 * Discard the reference to the OpenSSL session structure, if there is one, so
67 * that it can be freed if it is no longer in use. Also release the memory for
68 * the Python object.
69 */
70static void
71ssl_Session_dealloc(ssl_SessionObj *self) {
72 if (self->session != NULL) {
73 SSL_SESSION_free(self->session);
74 self->session = NULL;
75 }
Jean-Paul Calderone9c98bf92012-02-14 17:18:58 -050076 Py_TYPE(self)->tp_free((PyObject*)self);
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050077}
78
79/*
80 * Member methods in the Session object
81 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
82 * { 'name', (PyCFunction)ssl_Session_name, METH_VARARGS }
83 * for convenience
84 * ADD_ALIAS(name,real) creates an "alias" of the ssl_Session_real
85 * function with the name 'name'
86 */
87#define ADD_METHOD(name) { #name, (PyCFunction)ssl_Session_##name, METH_VARARGS, ssl_Session_##name##_doc }
88static PyMethodDef ssl_Session_methods[] = {
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050089 { NULL, NULL }
90};
91#undef ADD_METHOD
92
93/*
94 * The Python Session type definition.
95 */
96PyTypeObject ssl_Session_Type = {
97 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
98 "OpenSSL.SSL.Session",
99 sizeof(ssl_SessionObj),
100 0,
Jean-Paul Calderone64eaffc2012-02-13 11:53:49 -0500101 (destructor)ssl_Session_dealloc, /* tp_dealloc */
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -0500102 NULL, /* print */
103 NULL, /* tp_getattr */
104 NULL, /* setattr */
105 NULL, /* compare */
106 NULL, /* repr */
107 NULL, /* as_number */
108 NULL, /* as_sequence */
109 NULL, /* as_mapping */
110 NULL, /* hash */
111 NULL, /* call */
112 NULL, /* str */
113 NULL, /* getattro */
114 NULL, /* setattro */
115 NULL, /* as_buffer */
116 Py_TPFLAGS_DEFAULT, // Py_TPFLAGS_HAVE_GC, /* tp_flags */
117 ssl_Session_doc, /* tp_doc */
118 NULL, // (traverseproc)ssl_Session_traverse, /* tp_traverse */
119 NULL, // (inquiry)ssl_Session_clear, /* tp_clear */
120 NULL, /* tp_richcompare */
121 0, /* tp_weaklistoffset */
122 NULL, /* tp_iter */
123 NULL, /* tp_iternext */
124 ssl_Session_methods, /* tp_methods */
125 NULL, /* tp_members */
126 NULL, /* tp_getset */
127 NULL, /* tp_base */
128 NULL, /* tp_dict */
129 NULL, /* tp_descr_get */
130 NULL, /* tp_descr_set */
131 0, /* tp_dictoffset */
132 NULL, /* tp_init */
133 NULL, /* tp_alloc */
134 ssl_Session_new, /* tp_new */
135};
136
137/*
138 * Initialize the Session part of the SSL sub module
139 *
140 * Arguments: dict - The OpenSSL.SSL module
141 * Returns: 1 for success, 0 otherwise
142 */
143int
144init_ssl_session(PyObject *module) {
145
146 if (PyType_Ready(&ssl_Session_Type) < 0) {
147 return 0;
148 }
149
150 /* PyModule_AddObject steals a reference.
151 */
152 Py_INCREF((PyObject *)&ssl_Session_Type);
153 if (PyModule_AddObject(module, "Session", (PyObject *)&ssl_Session_Type) < 0) {
154 return 0;
155 }
156
157 return 1;
158}
159