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
|
/* Copyright (c) 1999-2000 Ng Pheng Siong. All rights reserved. */
/* $Id$ */
%{
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/dsa.h>
PyObject *dsa_sig_get_r(DSA_SIG *dsa_sig) {
const BIGNUM* pr;
DSA_SIG_get0(dsa_sig, &pr, NULL);
return bn_to_mpi(pr);
}
PyObject *dsa_sig_get_s(DSA_SIG *dsa_sig) {
const BIGNUM* qs;
DSA_SIG_get0(dsa_sig, NULL, &qs);
return bn_to_mpi(qs);
}
%}
%apply Pointer NONNULL { DSA * };
%rename(dsa_new) DSA_new;
extern DSA *DSA_new(void);
%rename(dsa_free) DSA_free;
extern void DSA_free(DSA *);
%rename(dsa_size) DSA_size;
extern int DSA_size(const DSA *); /* assert(dsa->q); */
%rename(dsa_gen_key) DSA_generate_key;
extern int DSA_generate_key(DSA *);
%warnfilter(454) _dsa_err;
%inline %{
static PyObject *_dsa_err;
void dsa_init(PyObject *dsa_err) {
Py_INCREF(dsa_err);
_dsa_err = dsa_err;
}
%}
%typemap(out) DSA * {
PyObject *self = NULL; /* bug in SWIG_NewPointerObj as of 3.0.5 */
if ($1 != NULL)
$result = SWIG_NewPointerObj($1, $1_descriptor, 0);
else {
$result = NULL;
}
}
%inline %{
DSA *dsa_generate_parameters(int bits, PyObject *pyfunc) {
DSA *dsa;
BN_GENCB *gencb;
int ret;
if ((gencb=BN_GENCB_new()) == NULL) {
m2_PyErr_Msg(_dh_err);
return NULL;
}
if ((dsa = DSA_new()) == NULL) {
m2_PyErr_Msg(_dsa_err);
BN_GENCB_free(gencb);
return NULL;
}
BN_GENCB_set(gencb, bn_gencb_callback, (void *) pyfunc);
Py_INCREF(pyfunc);
ret = DSA_generate_parameters_ex(dsa, bits, NULL, 0, NULL, NULL,
gencb);
Py_DECREF(pyfunc);
BN_GENCB_free(gencb);
if (ret)
return dsa;
m2_PyErr_Msg(_dsa_err);
DSA_free(dsa);
return NULL;
}
DSA *dsa_read_params(BIO *f, PyObject *pyfunc) {
DSA *ret;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
ret = PEM_read_bio_DSAparams(f, NULL, passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
if (ret == NULL) {
m2_PyErr_Msg(_dsa_err);
}
return ret;
}
DSA *dsa_read_key(BIO *f, PyObject *pyfunc) {
DSA *ret;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
ret = PEM_read_bio_DSAPrivateKey(f, NULL, passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
if (ret == NULL) {
m2_PyErr_Msg(_dsa_err);
}
return ret;
}
DSA *dsa_read_pub_key(BIO *f, PyObject *pyfunc) {
DSA *ret;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
ret = PEM_read_bio_DSA_PUBKEY(f, NULL, passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
if (ret == NULL) {
m2_PyErr_Msg(_dsa_err);
}
return ret;
}
%}
%typemap(out) DSA * ;
%inline %{
PyObject *dsa_get_p(DSA *dsa) {
const BIGNUM* p = NULL;
DSA_get0_pqg(dsa, &p, NULL, NULL);
if (!p) {
PyErr_SetString(_dsa_err, "'p' is unset");
return NULL;
}
return bn_to_mpi(p);
}
PyObject *dsa_get_q(DSA *dsa) {
const BIGNUM* q = NULL;
DSA_get0_pqg(dsa, NULL, &q, NULL);
if (!q) {
PyErr_SetString(_dsa_err, "'q' is unset");
return NULL;
}
return bn_to_mpi(q);
}
PyObject *dsa_get_g(DSA *dsa) {
const BIGNUM* g = NULL;
DSA_get0_pqg(dsa, NULL, NULL, &g);
if (!g) {
PyErr_SetString(_dsa_err, "'g' is unset");
return NULL;
}
return bn_to_mpi(g);
}
PyObject *dsa_get_pub(DSA *dsa) {
const BIGNUM* pub_key = NULL;
DSA_get0_key(dsa, &pub_key, NULL);
if (!pub_key) {
PyErr_SetString(_dsa_err, "'pub' is unset");
return NULL;
}
return bn_to_mpi(pub_key);
}
PyObject *dsa_get_priv(DSA *dsa) {
const BIGNUM* priv_key = NULL;
DSA_get0_key(dsa, NULL, &priv_key);
if (!priv_key) {
PyErr_SetString(_dsa_err, "'priv' is unset");
return NULL;
}
return bn_to_mpi(priv_key);
}
PyObject *dsa_set_pqg(DSA *dsa, PyObject *pval, PyObject* qval, PyObject* gval) {
BIGNUM* p, *q, *g;
if (!(p = m2_PyObject_AsBIGNUM(pval, _dsa_err))
|| !(q = m2_PyObject_AsBIGNUM(qval, _dsa_err))
|| !(g = m2_PyObject_AsBIGNUM(gval, _dsa_err)))
return NULL;
if (!DSA_set0_pqg(dsa, p, q, g)) {
PyErr_SetString(
_dsa_err,
"Cannot set prime number, subprime, or generator of subgroup for DSA.");
BN_free(p);
BN_free(q);
BN_free(g);
return NULL;
}
Py_RETURN_NONE;
}
PyObject *dsa_set_pub(DSA *dsa, PyObject *value) {
BIGNUM *bn;
const void *vbuf;
int vlen = 0;
if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
return NULL;
if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
m2_PyErr_Msg(_dsa_err);
return NULL;
}
if (!DSA_set0_key(dsa, bn, NULL)) {
BN_free(bn);
PyErr_SetString(_dsa_err, "Cannot set private and public key for DSA.");
}
Py_RETURN_NONE;
}
%}
%threadallow dsa_write_params_bio;
%inline %{
int dsa_write_params_bio(DSA* dsa, BIO* f) {
return PEM_write_bio_DSAparams(f, dsa);
}
%}
%inline %{
int dsa_write_key_bio(DSA* dsa, BIO* f, EVP_CIPHER *cipher, PyObject *pyfunc) {
int ret;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
ret = PEM_write_bio_DSAPrivateKey(f, dsa, cipher, NULL, 0,
passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
return ret;
}
%}
%inline %{
int dsa_write_key_bio_no_cipher(DSA* dsa, BIO* f, PyObject *pyfunc) {
int ret;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
ret = PEM_write_bio_DSAPrivateKey(f, dsa, NULL, NULL, 0,
passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
return ret;
}
%}
%threadallow dsa_write_pub_key_bio;
%inline %{
int dsa_write_pub_key_bio(DSA* dsa, BIO* f) {
return PEM_write_bio_DSA_PUBKEY(f, dsa);
}
%}
%inline %{
PyObject *dsa_sign(DSA *dsa, PyObject *value) {
const void *vbuf;
int vlen = 0;
PyObject *tuple;
DSA_SIG *sig;
if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
return NULL;
if (!(sig = DSA_do_sign(vbuf, vlen, dsa))) {
m2_PyErr_Msg(_dsa_err);
return NULL;
}
if (!(tuple = PyTuple_New(2))) {
DSA_SIG_free(sig);
PyErr_SetString(PyExc_RuntimeError, "PyTuple_New() fails");
return NULL;
}
PyTuple_SET_ITEM(tuple, 0, dsa_sig_get_r(sig));
PyTuple_SET_ITEM(tuple, 1, dsa_sig_get_s(sig));
DSA_SIG_free(sig);
return tuple;
}
int dsa_verify(DSA *dsa, PyObject *value, PyObject *r, PyObject *s) {
const void *vbuf, *rbuf, *sbuf;
int vlen = 0, rlen = 0, slen = 0;
DSA_SIG *sig;
BIGNUM* pr, *ps;
int ret;
if ((m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
|| (m2_PyObject_AsReadBufferInt(r, &rbuf, &rlen) == -1)
|| (m2_PyObject_AsReadBufferInt(s, &sbuf, &slen) == -1))
return -1;
if (!(sig = DSA_SIG_new())) {
m2_PyErr_Msg(_dsa_err);
return -1;
}
if (!(pr = BN_mpi2bn((unsigned char *)rbuf, rlen, NULL))) {
m2_PyErr_Msg(_dsa_err);
DSA_SIG_free(sig);
return -1;
}
if (!(ps = BN_mpi2bn((unsigned char *)sbuf, slen, NULL))) {
m2_PyErr_Msg(_dsa_err);
DSA_SIG_free(sig);
BN_free(pr);
return -1;
}
if (!DSA_SIG_set0(sig, pr, ps)) {
m2_PyErr_Msg(_dsa_err);
DSA_SIG_free(sig);
BN_free(pr);
BN_free(ps);
return -1;
}
ret = DSA_do_verify(vbuf, vlen, sig, dsa);
DSA_SIG_free(sig);
if (ret == -1)
m2_PyErr_Msg(_dsa_err);
return ret;
}
PyObject *dsa_sign_asn1(DSA *dsa, PyObject *value) {
const void *vbuf;
int vlen = 0;
void *sigbuf;
unsigned int siglen;
PyObject *ret;
if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
return NULL;
if (!(sigbuf = PyMem_Malloc(DSA_size(dsa)))) {
PyErr_SetString(PyExc_MemoryError, "dsa_sign_asn1");
return NULL;
}
if (!DSA_sign(0, vbuf, vlen, (unsigned char *)sigbuf, &siglen, dsa)) {
m2_PyErr_Msg(_dsa_err);
PyMem_Free(sigbuf);
return NULL;
}
ret = PyBytes_FromStringAndSize(sigbuf, siglen);
PyMem_Free(sigbuf);
return ret;
}
int dsa_verify_asn1(DSA *dsa, PyObject *value, PyObject *sig) {
const void *vbuf;
void *sbuf;
int vlen = 0, slen = 0, ret = 0;
if ((m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
|| (m2_PyObject_AsReadBufferInt(sig, (const void **)&sbuf, &slen)
== -1))
return -1;
if ((ret = DSA_verify(0, vbuf, vlen, sbuf, slen, dsa)) == -1)
m2_PyErr_Msg(_dsa_err);
return ret;
}
int dsa_check_key(DSA *dsa) {
const BIGNUM* pub_key, *priv_key;
DSA_get0_key(dsa, &pub_key, &priv_key);
return pub_key != NULL && priv_key != NULL;
}
int dsa_check_pub_key(DSA *dsa) {
const BIGNUM* pub_key;
DSA_get0_key(dsa, &pub_key, NULL);
return pub_key ? 1 : 0;
}
int dsa_keylen(DSA *dsa) {
const BIGNUM* p;
DSA_get0_pqg(dsa, &p, NULL, NULL);
return BN_num_bits(p);
}
int dsa_type_check(DSA *dsa) {
return 1;
}
%}
|