Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
Expand file tree
/
Copy pathcert.c
More file actions
Latest commit
252 lines (220 loc) · 6.35 KB
/
Copy pathcert.c
File metadata and controls
252 lines (220 loc) · 6.35 KB
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
#include"Python.h"
#include"../_ssl.h"
#include"openssl/err.h"
#include"openssl/bio.h"
#include"openssl/pem.h"
#include"openssl/x509.h"
/*[clinic input]
module _ssl
class _ssl.Certificate "PySSLCertificate *" "PySSLCertificate_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=780fc647948cfffc]*/
#include"clinic/cert.c.h"
staticPyObject*
newCertificate(PyTypeObject*type, X509*cert, intupref)
{
PySSLCertificate*self;
assert(type!=NULL&&type->tp_alloc!=NULL);
assert(cert!=NULL);
self= (PySSLCertificate*) type->tp_alloc(type, 0);
if (self==NULL) {
returnNULL;
}
if (upref==1) {
X509_up_ref(cert);
}
self->cert=cert;
self->hash=-1;
return (PyObject*) self;
}
staticPyObject*
_PySSL_CertificateFromX509(_sslmodulestate*state, X509*cert, intupref)
{
returnnewCertificate(state->PySSLCertificate_Type, cert, upref);
}
staticPyObject*
_PySSL_CertificateFromX509Stack(_sslmodulestate*state, STACK_OF(X509) *stack, intupref)
{
intlen, i;
PyObject*result=NULL;
len=sk_X509_num(stack);
result=PyList_New(len);
if (result==NULL) {
returnNULL;
}
for (i=0; i<len; i++) {
X509*cert=sk_X509_value(stack, i);
PyObject*ocert=_PySSL_CertificateFromX509(state, cert, upref);
if (ocert==NULL) {
Py_DECREF(result);
returnNULL;
}
PyList_SetItem(result, i, ocert);
}
returnresult;
}
/*[clinic input]
_ssl.Certificate.public_bytes
format: int(c_default="PY_SSL_ENCODING_PEM") = Encoding.PEM
[clinic start generated code]*/
staticPyObject*
_ssl_Certificate_public_bytes_impl(PySSLCertificate*self, intformat)
/*[clinic end generated code: output=c01ddbb697429e12 input=4d38c45e874b0e64]*/
{
BIO*bio;
intretcode;
PyObject*result;
_sslmodulestate*state=get_state_cert(self);
bio=BIO_new(BIO_s_mem());
if (bio==NULL) {
PyErr_SetString(state->PySSLErrorObject,
"failed to allocate BIO");
returnNULL;
}
switch(format) {
casePY_SSL_ENCODING_PEM:
retcode=PEM_write_bio_X509(bio, self->cert);
break;
casePY_SSL_ENCODING_PEM_AUX:
retcode=PEM_write_bio_X509_AUX(bio, self->cert);
break;
casePY_SSL_ENCODING_DER:
retcode=i2d_X509_bio(bio, self->cert);
break;
default:
PyErr_SetString(PyExc_ValueError, "Unsupported format");
BIO_free(bio);
returnNULL;
}
if (retcode!=1) {
BIO_free(bio);
_setSSLError(state, NULL, 0, __FILE__, __LINE__);
returnNULL;
}
if (format==PY_SSL_ENCODING_DER) {
result=_PySSL_BytesFromBIO(state, bio);
} else {
result=_PySSL_UnicodeFromBIO(state, bio, "error");
}
BIO_free(bio);
returnresult;
}
/*[clinic input]
_ssl.Certificate.get_info
[clinic start generated code]*/
staticPyObject*
_ssl_Certificate_get_info_impl(PySSLCertificate*self)
/*[clinic end generated code: output=0f0deaac54f4408b input=ba2c1694b39d0778]*/
{
return_decode_certificate(get_state_cert(self), self->cert);
}
staticPyObject*
_x509name_print(_sslmodulestate*state, constX509_NAME*name,
intindent, unsigned longflags)
{
PyObject*res;
BIO*biobuf;
biobuf=BIO_new(BIO_s_mem());
if (biobuf==NULL) {
PyErr_SetString(PyExc_MemoryError, "failed to allocate BIO");
returnNULL;
}
if (X509_NAME_print_ex(biobuf, name, indent, flags) <= 0) {
_setSSLError(state, NULL, 0, __FILE__, __LINE__);
BIO_free(biobuf);
returnNULL;
}
res=_PySSL_UnicodeFromBIO(state, biobuf, "strict");
BIO_free(biobuf);
returnres;
}
/* ************************************************************************
* PySSLCertificate_Type
*/
#definePySSLCertificate_CAST(op) ((PySSLCertificate *)(op))
staticPyObject*
certificate_repr(PyObject*op)
{
PyObject*osubject, *result;
PySSLCertificate*self=PySSLCertificate_CAST(op);
/* subject string is ASCII encoded, UTF-8 chars are quoted */
osubject=_x509name_print(
get_state_cert(self),
X509_get_subject_name(self->cert),
0,
XN_FLAG_RFC2253
);
if (osubject==NULL)
returnNULL;
result=PyUnicode_FromFormat(
"<%s '%U'>",
Py_TYPE(self)->tp_name, osubject
);
Py_DECREF(osubject);
returnresult;
}
staticPy_hash_t
certificate_hash(PyObject*op)
{
PySSLCertificate*self=PySSLCertificate_CAST(op);
if (self->hash== (Py_hash_t)-1) {
unsigned longhash;
hash=X509_subject_name_hash(self->cert);
if ((Py_hash_t)hash== (Py_hash_t)-1) {
self->hash=-2;
} else {
self->hash= (Py_hash_t)hash;
}
}
returnself->hash;
}
staticPyObject*
certificate_richcompare(PyObject*lhs, PyObject*rhs, intop)
{
intcmp;
PySSLCertificate*self=PySSLCertificate_CAST(lhs);
_sslmodulestate*state=get_state_cert(self);
if (Py_TYPE(rhs) !=state->PySSLCertificate_Type) {
Py_RETURN_NOTIMPLEMENTED;
}
/* only support == and != */
if ((op!=Py_EQ) && (op!=Py_NE)) {
Py_RETURN_NOTIMPLEMENTED;
}
cmp=X509_cmp(self->cert, ((PySSLCertificate*)rhs)->cert);
if (((op==Py_EQ) && (cmp==0)) || ((op==Py_NE) && (cmp!=0))) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
staticvoid
certificate_dealloc(PyObject*op)
{
PySSLCertificate*self=PySSLCertificate_CAST(op);
PyTypeObject*tp=Py_TYPE(self);
X509_free(self->cert);
(void)Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
}
staticPyMethodDefcertificate_methods[] = {
/* methods */
_SSL_CERTIFICATE_PUBLIC_BYTES_METHODDEF
_SSL_CERTIFICATE_GET_INFO_METHODDEF
{NULL, NULL}
};
staticPyType_SlotPySSLCertificate_slots[] = {
{Py_tp_dealloc, certificate_dealloc},
{Py_tp_repr, certificate_repr},
{Py_tp_hash, certificate_hash},
{Py_tp_richcompare, certificate_richcompare},
{Py_tp_methods, certificate_methods},
{0, 0},
};
staticPyType_SpecPySSLCertificate_spec= {
"_ssl.Certificate",
sizeof(PySSLCertificate),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
PySSLCertificate_slots,
};