~exarkun/pyopenssl/session-methods

« back to all changes in this revision

Viewing changes to OpenSSL/ssl/session.c

  • Committer: Jean-Paul Calderone
  • Date: 2012-02-16 19:25:44 UTC
  • Revision ID: exarkun@twistedmatrix.com-20120216192544-5b81qbddgyxl271f
Add ASN1 serialization and deserialization.

Show diffs side-by-side

added added

removed removed

Lines of Context:
149
149
    return Py_None;
150
150
}
151
151
 
 
152
static char ssl_Session_from_asn1_doc[] = "\n\
 
153
Deserialize a Session object from an ASN.1 string.\n\
 
154
\n\
 
155
:returns: A :py:class:`Session` instance representing the session from the\n\
 
156
     serialized data.\n\
 
157
";
 
158
static PyObject *
 
159
ssl_Session_from_asn1(PyObject *cls, PyObject *args) {
 
160
    SSL_SESSION *native_session;
 
161
 
 
162
    char *asn1;
 
163
    const unsigned char **asn1p = (const unsigned char**)&asn1;
 
164
    int length;
 
165
 
 
166
    if (!PyArg_ParseTuple(args, "s#:from_asn1", asn1p, &length)) {
 
167
        return NULL;
 
168
    }
 
169
 
 
170
    native_session = d2i_SSL_SESSION(NULL, asn1p, length);
 
171
    if (native_session == NULL) {
 
172
        exception_from_error_queue(ssl_Error);
 
173
        return NULL;
 
174
    }
 
175
 
 
176
    /* XXX Not respecting subclassing here.
 
177
     */
 
178
    return (PyObject *)ssl_Session_from_SSL_SESSION(native_session);
 
179
}
 
180
 
 
181
static char ssl_Session_to_asn1_doc[] = "\n\
 
182
Serialize the Session object to an ASN.1 representation of it.\n\
 
183
\n\
 
184
:returns: A :py:obj:`str` containing the ASN.1 representation.\n\
 
185
";
 
186
static PyObject *
 
187
ssl_Session_to_asn1(ssl_SessionObj *self, PyObject *args) {
 
188
    unsigned char *buffer, *p;
 
189
    long           size;
 
190
    PyObject      *ret;
 
191
 
 
192
    if (!PyArg_ParseTuple(args, ":to_asn1")) {
 
193
        return NULL;
 
194
    }
 
195
 
 
196
    size = i2d_SSL_SESSION(self->session, NULL);
 
197
    if (size == 0) {
 
198
        PyErr_SetString(PyExc_ValueError, "Invalid session.");
 
199
        return NULL;
 
200
    }
 
201
 
 
202
    buffer = (unsigned char*)PyMem_Malloc(sizeof(unsigned char) * size);
 
203
    p = buffer;
 
204
    i2d_SSL_SESSION(self->session, &p);
 
205
    ret = PyBytes_FromStringAndSize((const char *)buffer, size);
 
206
    PyMem_Free(buffer);
 
207
    return ret;
 
208
}
 
209
 
 
210
 
152
211
/*
153
212
 * Member methods in the Session object
154
213
 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
163
222
  ADD_METHOD(set_time),
164
223
  ADD_METHOD(get_timeout),
165
224
  ADD_METHOD(set_timeout),
 
225
  ADD_METHOD(to_asn1),
 
226
  {"from_asn1", (PyCFunction)ssl_Session_from_asn1,
 
227
   METH_VARARGS | METH_CLASS, ssl_Session_from_asn1_doc},
166
228
    { NULL, NULL }
167
229
};
168
230
#undef ADD_METHOD