summaryrefslogtreecommitdiff
path: root/OpenSSL/crypto/revoked.c
blob: 93f99460577c1f6ba416257b2ee23ff076f9ed6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <Python.h>
#define crypto_MODULE
#include "crypto.h"

#ifdef _WIN32
#define strcasecmp(string1, string2) _stricmp(string1, string2)
#endif

/* http://www.openssl.org/docs/apps/x509v3_config.html#CRL_distribution_points_ */
/* which differs from crl_reasons of crypto/x509v3/v3_enum.c that matches */
/* OCSP_crl_reason_str.  We use the latter, just like the command line program.  */
static const char *crl_reasons[] = {
    "unspecified",
    "keyCompromise",
    "CACompromise",
    "affiliationChanged",
    "superseded",
    "cessationOfOperation",
    "certificateHold",
    NULL,
    "removeFromCRL",
};

#define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))

static char crypto_Revoked_all_reasons_doc[] = "\n\
Return a list of all the supported reason strings.\n\
\n\
@return: A list of reason strings.\n\
";
static PyObject *
crypto_Revoked_all_reasons(crypto_RevokedObj *self, PyObject *args) {
    PyObject *list, *str;
    int j;

    list = PyList_New(0);
    for (j = 0; j < NUM_REASONS; j++) {
        if(crl_reasons[j]) {
            str = PyBytes_FromString(crl_reasons[j]);
            PyList_Append(list, str);
            Py_DECREF(str);
        }
    }
    return list;
}

static PyObject *
X509_EXTENSION_value_to_PyString(X509_EXTENSION *ex) {
    BIO *bio = NULL;
    PyObject *str = NULL;
    int str_len;
    char *tmp_str;

    /* Create a openssl BIO buffer */
    bio = BIO_new(BIO_s_mem());
    if (bio == NULL) {
        goto err;
    }

    /* These are not the droids you are looking for. */
    if (!X509V3_EXT_print(bio, ex, 0, 0)) {
        if (M_ASN1_OCTET_STRING_print(bio, ex->value) == 0) {
            goto err;
        }
    }

    /* Convert to a Python string. */
    str_len = BIO_get_mem_data(bio, &tmp_str);
    str = PyBytes_FromStringAndSize(tmp_str, str_len);

    /* Cleanup */
    BIO_free(bio);
    return str;

 err:
    if (bio) {
        BIO_free(bio);
    }
    if (str) {
        Py_DECREF(str);
    }
    return NULL;
}

static void
delete_reason(STACK_OF(X509_EXTENSION) *sk) {
    X509_EXTENSION * ext;
    int j;

    for (j = 0; j < sk_X509_EXTENSION_num(sk); j++) {
         ext = sk_X509_EXTENSION_value(sk, j);
         if (OBJ_obj2nid(ext->object) == NID_crl_reason) {
             X509_EXTENSION_free(ext);
             (void) sk_X509_EXTENSION_delete(sk, j);
             break;
         }
    }
}

static int
reason_str_to_code(const char * reason_str) {
    int reason_code = -1, j;
    char *spaceless_reason, * sp;

    /*  Remove spaces so that the responses of
     *  get_reason() work in set_reason()  */
    if ((spaceless_reason = strdup(reason_str)) == NULL) {
        return -1;
    }

    while ((sp = strchr(spaceless_reason, ' '))) {
       memmove(sp, sp+1, strlen(sp));
    }

    for (j = 0; j < NUM_REASONS; j++) {
        if(crl_reasons[j] && !strcasecmp(spaceless_reason, crl_reasons[j])) {
            reason_code = j;
            break;
        }
    }
    free(spaceless_reason);
    return reason_code;
}


static char crypto_Revoked_set_reason_doc[] = "\n\
Set the reason of a Revoked object.\n\
\n\
@param reason: The reason string.\n\
@type reason: L{str}\n\
@return: None\n\
";
static PyObject *
crypto_Revoked_set_reason(crypto_RevokedObj *self, PyObject *args, PyObject *keywds) {
    static char *kwlist[] = {"reason", NULL};
    const char *reason_str = NULL;
    int reason_code;
    ASN1_ENUMERATED *rtmp = NULL;

    if (!PyArg_ParseTupleAndKeywords(
            args, keywds, "O&:set_reason", kwlist,
            crypto_byte_converter, &reason_str)) {
        return NULL;
    }

    if(reason_str == NULL) {
        delete_reason(self->revoked->extensions);
        goto done;
    }

    reason_code = reason_str_to_code(reason_str);
    if (reason_code == -1) {
        PyErr_SetString(PyExc_ValueError, "bad reason string");
        return NULL;
    }

    rtmp = ASN1_ENUMERATED_new();
    if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code)) {
        goto err;
    }
    delete_reason(self->revoked->extensions);
    if (!X509_REVOKED_add1_ext_i2d(self->revoked, NID_crl_reason, rtmp, 0, 0)) {
        goto err;
    }

 done:
    Py_INCREF(Py_None);
    return Py_None;

 err:
    exception_from_error_queue(crypto_Error);
    return NULL;
}


static char crypto_Revoked_get_reason_doc[] = "\n\
Return the reason of a Revoked object.\n\
\n\
@return: The reason as a string\n\
";
static PyObject *
crypto_Revoked_get_reason(crypto_RevokedObj *self, PyObject *args) {
    X509_EXTENSION * ext;
    int j;
    STACK_OF(X509_EXTENSION) *sk = NULL;

    if (!PyArg_ParseTuple(args, ":get_reason")) {
        return NULL;
    }

    sk = self->revoked->extensions;
    for (j = 0; j < sk_X509_EXTENSION_num(sk); j++) {
         ext = sk_X509_EXTENSION_value(sk, j);
         if (OBJ_obj2nid(ext->object) == NID_crl_reason) {
             return X509_EXTENSION_value_to_PyString(ext);
         }
    }

    Py_INCREF(Py_None);
    return Py_None;
}


static char crypto_Revoked_get_rev_date_doc[] = "\n\
Retrieve the revocation date\n\
\n\
@return: A string giving the timestamp, in the format:\n\
\n\
                 YYYYMMDDhhmmssZ\n\
                 YYYYMMDDhhmmss+hhmm\n\
                 YYYYMMDDhhmmss-hhmm\n\
";

static PyObject*
crypto_Revoked_get_rev_date(crypto_RevokedObj *self, PyObject *args) {
    /* returns a borrowed reference.  */
    return _get_asn1_time(
        ":get_rev_date", self->revoked->revocationDate, args);
}

static char crypto_Revoked_set_rev_date_doc[] = "\n\
Set the revocation timestamp\n\
\n\
@param when: A string giving the timestamp, in the format:\n\
\n\
                 YYYYMMDDhhmmssZ\n\
                 YYYYMMDDhhmmss+hhmm\n\
                 YYYYMMDDhhmmss-hhmm\n\
\n\
@return: None\n\
";

static PyObject*
crypto_Revoked_set_rev_date(crypto_RevokedObj *self, PyObject *args) {
    return _set_asn1_time(
        BYTESTRING_FMT ":set_rev_date", self->revoked->revocationDate, args);
}

/* The integer is converted to an upper-case hex string
 * without a '0x' prefix. */
static PyObject *
ASN1_INTEGER_to_PyString(ASN1_INTEGER *asn1_int) {
    BIO *bio = NULL;
    PyObject *str = NULL;
    int str_len;
    char *tmp_str;

    /* Create a openssl BIO buffer */
    bio = BIO_new(BIO_s_mem());
    if (bio == NULL) {
        goto err;
    }

    /* Write the integer to the BIO as a hex string. */
    if (i2a_ASN1_INTEGER(bio, asn1_int) < 0) {
        goto err;
    }

    /* Convert to a Python string. */
    str_len = BIO_get_mem_data(bio, &tmp_str);
    str = PyBytes_FromStringAndSize(tmp_str, str_len);

    /* Cleanup */
    BIO_free(bio);
    return str;

 err:
    if (bio) {
        BIO_free(bio);
    }
    if (str) {
        Py_DECREF(str);
    }
    return NULL;
}


static char crypto_Revoked_get_serial_doc[] = "\n\
Return the serial number of a Revoked structure\n\
\n\
@return: The serial number as a string\n\
";
static PyObject *
crypto_Revoked_get_serial(crypto_RevokedObj *self, PyObject *args) {
    if (!PyArg_ParseTuple(args, ":get_serial")) {
        return NULL;
    }

    if (self->revoked->serialNumber == NULL) {
        /* never happens */
        Py_INCREF(Py_None);
        return Py_None;
    } else {
        return ASN1_INTEGER_to_PyString(self->revoked->serialNumber);
    }
}

static char crypto_Revoked_set_serial_doc[] = "\n\
Set the serial number of a revoked Revoked structure\n\
\n\
@param hex_str: The new serial number.\n\
@type hex_str: L{str}\n\
@return: None\n\
";
static PyObject *
crypto_Revoked_set_serial(crypto_RevokedObj *self, PyObject *args, PyObject *keywds) {
    static char *kwlist[] = {"hex_str", NULL};
    const char *hex_str = NULL;
    BIGNUM *serial = NULL;
    ASN1_INTEGER *tmpser = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, keywds, BYTESTRING_FMT ":set_serial",
                                     kwlist, &hex_str)) {
        return NULL;
    }

    if (!BN_hex2bn(&serial, hex_str) ) {
        PyErr_SetString(PyExc_ValueError, "bad hex string");
        return NULL;
    }

    tmpser = BN_to_ASN1_INTEGER(serial, NULL);
    BN_free(serial);
    serial = NULL;
    X509_REVOKED_set_serialNumber(self->revoked, tmpser);
    ASN1_INTEGER_free(tmpser);

    Py_INCREF(Py_None);
    return Py_None;
}


crypto_RevokedObj *
crypto_Revoked_New(X509_REVOKED *revoked) {
    crypto_RevokedObj *self;

    self = PyObject_New(crypto_RevokedObj, &crypto_Revoked_Type);
    if (self == NULL) {
        return NULL;
    }
    self->revoked = revoked;
    return self;
}

/*
 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
 *   {  'name', (PyCFunction)crypto_Revoked_name, METH_VARARGS, crypto_Revoked_name_doc }
 * for convenience
 */
#define ADD_METHOD(name)        \
    { #name, (PyCFunction)crypto_Revoked_##name, METH_VARARGS, crypto_Revoked_##name##_doc }
#define ADD_KW_METHOD(name)        \
    { #name, (PyCFunction)crypto_Revoked_##name, METH_VARARGS | METH_KEYWORDS, crypto_Revoked_##name##_doc }
static PyMethodDef crypto_Revoked_methods[] = {
    ADD_METHOD(all_reasons),
    ADD_METHOD(get_reason),
    ADD_KW_METHOD(set_reason),
    ADD_METHOD(get_rev_date),
    ADD_METHOD(set_rev_date),
    ADD_METHOD(get_serial),
    ADD_KW_METHOD(set_serial),
    { NULL, NULL }
};
#undef ADD_METHOD


static void
crypto_Revoked_dealloc(crypto_RevokedObj *self) {
    X509_REVOKED_free(self->revoked);
    self->revoked = NULL;

    PyObject_Del(self);
}

static char crypto_Revoked_doc[] = "\n\
Revoked() -> Revoked instance\n\
\n\
Create a new empty Revoked object.\n\
\n\
@returns: The Revoked object\n\
";

static PyObject* crypto_Revoked_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
    if (!PyArg_ParseTuple(args, ":Revoked")) {
        return NULL;
    }

    return (PyObject *)crypto_Revoked_New(X509_REVOKED_new());
}

PyTypeObject crypto_Revoked_Type = {
    PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
    "Revoked",
    sizeof(crypto_RevokedObj),
    0,
    (destructor)crypto_Revoked_dealloc,
    NULL, /* print */
    NULL, /* getattr */
    NULL, /* setattr */
    NULL, /* compare */
    NULL, /* repr */
    NULL, /* as_number */
    NULL, /* as_sequence */
    NULL, /* as_mapping */
    NULL, /* hash */
    NULL, /* call */
    NULL, /* str */
    NULL, /* getattro */
    NULL, /* setattro */
    NULL, /* as_buffer */
    Py_TPFLAGS_DEFAULT,
    crypto_Revoked_doc, /* doc */
    NULL, /* traverse */
    NULL, /* clear */
    NULL, /* tp_richcompare */
    0, /* tp_weaklistoffset */
    NULL, /* tp_iter */
    NULL, /* tp_iternext */
    crypto_Revoked_methods, /* tp_methods */
    NULL, /* tp_members */
    NULL, /* tp_getset */
    NULL, /* tp_base */
    NULL, /* tp_dict */
    NULL, /* tp_descr_get */
    NULL, /* tp_descr_set */
    0, /* tp_dictoffset */
    NULL, /* tp_init */
    NULL, /* tp_alloc */
    crypto_Revoked_new, /* tp_new */
};

int init_crypto_revoked(PyObject *module) {
    if(PyType_Ready(&crypto_Revoked_Type) < 0) {
        return 0;
    }

    /* PyModule_AddObject steals a reference.
     */
    Py_INCREF((PyObject *)&crypto_Revoked_Type);
    if (PyModule_AddObject(module, "Revoked", (PyObject *)&crypto_Revoked_Type) != 0) {
        return 0;
    }
    return 1;
}