Initial import to Tizen
[profile/ivi/python-pyOpenSSL.git] / OpenSSL / crypto / x509store.c
1 /*
2  * x509store.c
3  *
4  * Copyright (C) AB Strakt
5  * See LICENSE for details.
6  *
7  * X.509 Store handling, mostly thin wrapping.
8  * See the file RATIONALE for a short explanation of why this module was written.
9  */
10 #include <Python.h>
11 #define crypto_MODULE
12 #include "crypto.h"
13
14 static char crypto_X509Store_add_cert_doc[] = "\n\
15 Add a certificate\n\
16 \n\
17 @param cert: The certificate to add\n\
18 @return: None\n\
19 ";
20
21 static PyObject *
22 crypto_X509Store_add_cert(crypto_X509StoreObj *self, PyObject *args)
23 {
24     crypto_X509Obj *cert;
25
26     if (!PyArg_ParseTuple(args, "O!:add_cert", &crypto_X509_Type, &cert))
27         return NULL;
28
29     if (!X509_STORE_add_cert(self->x509_store, cert->x509))
30     {
31         exception_from_error_queue(crypto_Error);
32         return NULL;
33     }
34
35     Py_INCREF(Py_None);
36     return Py_None;
37 }
38
39
40 /*
41  * ADD_METHOD(name) expands to a correct PyMethodDef declaration
42  *   {  'name', (PyCFunction)crypto_X509Store_name, METH_VARARGS }
43  * for convenience
44  */
45 #define ADD_METHOD(name)        \
46     { #name, (PyCFunction)crypto_X509Store_##name, METH_VARARGS, crypto_X509Store_##name##_doc }
47 static PyMethodDef crypto_X509Store_methods[] =
48 {
49     ADD_METHOD(add_cert),
50     { NULL, NULL }
51 };
52 #undef ADD_METHOD
53
54
55 /*
56  * Constructor for X509Store, never called by Python code directly
57  *
58  * Arguments: name    - A "real" X509_STORE object
59  *            dealloc - Boolean value to specify whether the destructor should
60  *                      free the "real" X509_STORE object
61  * Returns:   The newly created X509Store object
62  */
63 crypto_X509StoreObj *
64 crypto_X509Store_New(X509_STORE *store, int dealloc)
65 {
66     crypto_X509StoreObj *self;
67
68     self = PyObject_New(crypto_X509StoreObj, &crypto_X509Store_Type);
69
70     if (self == NULL)
71         return NULL;
72
73     self->x509_store = store;
74     self->dealloc = dealloc;
75
76     return self;
77 }
78
79 /*
80  * Deallocate the memory used by the X509Store object
81  *
82  * Arguments: self - The X509Store object
83  * Returns:   None
84  */
85 static void
86 crypto_X509Store_dealloc(crypto_X509StoreObj *self)
87 {
88     /* Sometimes we don't have to dealloc this */
89     if (self->dealloc)
90         X509_STORE_free(self->x509_store);
91
92     PyObject_Del(self);
93 }
94
95
96 PyTypeObject crypto_X509Store_Type = {
97     PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
98     "X509Store",
99     sizeof(crypto_X509StoreObj),
100     0,
101     (destructor)crypto_X509Store_dealloc,
102     NULL, /* print */
103     NULL, /* 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,
117     NULL, /* doc */
118     NULL, /* traverse */
119     NULL, /* clear */
120     NULL, /* tp_richcompare */
121     0, /* tp_weaklistoffset */
122     NULL, /* tp_iter */
123     NULL, /* tp_iternext */
124     crypto_X509Store_methods, /* tp_methods */
125 };
126
127
128 /*
129  * Initialize the X509Store part of the crypto module
130  *
131  * Arguments: module - The crypto module
132  * Returns:   None
133  */
134 int
135 init_crypto_x509store(PyObject *module)
136 {
137     if (PyType_Ready(&crypto_X509Store_Type) < 0) {
138         return 0;
139     }
140
141     if (PyModule_AddObject(module, "X509StoreType", (PyObject *)&crypto_X509Store_Type) != 0) {
142         return 0;
143     }
144
145     return 1;
146 }