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
445
446
447
448
449
450
451
452
|
/* Copyright (c) 1999-2000 Ng Pheng Siong. All rights reserved. */
/* $Id: _rsa.i 723 2010-02-13 06:53:13Z heikki $ */
%{
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/opensslv.h>
%}
%apply Pointer NONNULL { RSA * };
%apply Pointer NONNULL { PyObject *pyfunc };
%rename(rsa_new) RSA_new;
extern RSA *RSA_new(void);
%rename(rsa_free) RSA_free;
extern void RSA_free(RSA *);
%rename(rsa_size) RSA_size;
extern int RSA_size(const RSA *);
%rename(rsa_check_key) RSA_check_key;
extern int RSA_check_key(const RSA *);
%constant int no_padding = RSA_NO_PADDING;
%constant int pkcs1_padding = RSA_PKCS1_PADDING;
%constant int sslv23_padding = RSA_SSLV23_PADDING;
%constant int pkcs1_oaep_padding = RSA_PKCS1_OAEP_PADDING;
%constant int NID_sha1 = NID_sha1;
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
%constant int NID_sha224 = NID_sha224;
%constant int NID_sha256 = NID_sha256;
%constant int NID_sha384 = NID_sha384;
%constant int NID_sha512 = NID_sha512;
#endif
%constant int NID_md5 = NID_md5;
%constant int NID_ripemd160 = NID_ripemd160;
%inline %{
static PyObject *_rsa_err;
void rsa_init(PyObject *rsa_err) {
Py_INCREF(rsa_err);
_rsa_err = rsa_err;
}
%}
%inline %{
RSA *rsa_read_key(BIO *f, PyObject *pyfunc) {
RSA *rsa;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
rsa = PEM_read_bio_RSAPrivateKey(f, NULL, passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
return rsa;
}
%}
%inline %{
int rsa_write_key(RSA *rsa, BIO *f, EVP_CIPHER *cipher, PyObject *pyfunc) {
int ret;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
ret = PEM_write_bio_RSAPrivateKey(f, rsa, cipher, NULL, 0,
passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
return ret;
}
%}
%inline %{
int rsa_write_key_no_cipher(RSA *rsa, BIO *f, PyObject *pyfunc) {
int ret;
Py_INCREF(pyfunc);
Py_BEGIN_ALLOW_THREADS
ret = PEM_write_bio_RSAPrivateKey(f, rsa, NULL, NULL, 0,
passphrase_callback, (void *)pyfunc);
Py_END_ALLOW_THREADS
Py_DECREF(pyfunc);
return ret;
}
%}
%threadallow rsa_read_pub_key;
%inline %{
RSA *rsa_read_pub_key(BIO *f) {
return PEM_read_bio_RSA_PUBKEY(f, NULL, NULL, NULL);
}
%}
%threadallow rsa_write_pub_key;
%inline %{
int rsa_write_pub_key(RSA *rsa, BIO *f) {
return PEM_write_bio_RSA_PUBKEY(f, rsa);
}
PyObject *rsa_get_e(RSA *rsa) {
if (!rsa->e) {
PyErr_SetString(_rsa_err, "'e' is unset");
return NULL;
}
return bn_to_mpi(rsa->e);
}
PyObject *rsa_get_n(RSA *rsa) {
if (!rsa->n) {
PyErr_SetString(_rsa_err, "'n' is unset");
return NULL;
}
return bn_to_mpi(rsa->n);
}
PyObject *rsa_set_e(RSA *rsa, PyObject *value) {
BIGNUM *bn;
const void *vbuf;
int vlen;
if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
return NULL;
if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
if (rsa->e)
BN_free(rsa->e);
rsa->e = bn;
Py_INCREF(Py_None);
return Py_None;
}
PyObject *rsa_set_n(RSA *rsa, PyObject *value) {
BIGNUM *bn;
const void *vbuf;
int vlen;
if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
return NULL;
if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
if (rsa->n)
BN_free(rsa->n);
rsa->n = bn;
Py_INCREF(Py_None);
return Py_None;
}
PyObject *rsa_set_e_bin(RSA *rsa, PyObject *value) {
BIGNUM *bn;
const void *vbuf;
int vlen;
if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
return NULL;
if (!(bn = BN_bin2bn((unsigned char *)vbuf, vlen, NULL))) {
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
if (rsa->e)
BN_free(rsa->e);
rsa->e = bn;
Py_INCREF(Py_None);
return Py_None;
}
PyObject *rsa_set_n_bin(RSA *rsa, PyObject *value) {
BIGNUM *bn;
const void *vbuf;
int vlen;
if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
return NULL;
if (!(bn = BN_bin2bn((unsigned char *)vbuf, vlen, NULL))) {
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
if (rsa->n)
BN_free(rsa->n);
rsa->n = bn;
Py_INCREF(Py_None);
return Py_None;
}
PyObject *rsa_private_encrypt(RSA *rsa, PyObject *from, int padding) {
const void *fbuf;
void *tbuf;
int flen, tlen;
PyObject *ret;
if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
return NULL;
if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
PyErr_SetString(PyExc_MemoryError, "rsa_private_encrypt");
return NULL;
}
tlen = RSA_private_encrypt(flen, (unsigned char *)fbuf,
(unsigned char *)tbuf, rsa, padding);
if (tlen == -1) {
PyMem_Free(tbuf);
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
PyMem_Free(tbuf);
return ret;
}
PyObject *rsa_public_decrypt(RSA *rsa, PyObject *from, int padding) {
const void *fbuf;
void *tbuf;
int flen, tlen;
PyObject *ret;
if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
return NULL;
if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
PyErr_SetString(PyExc_MemoryError, "rsa_public_decrypt");
return NULL;
}
tlen = RSA_public_decrypt(flen, (unsigned char *)fbuf,
(unsigned char *)tbuf, rsa, padding);
if (tlen == -1) {
PyMem_Free(tbuf);
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
PyMem_Free(tbuf);
return ret;
}
PyObject *rsa_public_encrypt(RSA *rsa, PyObject *from, int padding) {
const void *fbuf;
void *tbuf;
int flen, tlen;
PyObject *ret;
if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
return NULL;
if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
PyErr_SetString(PyExc_MemoryError, "rsa_public_encrypt");
return NULL;
}
tlen = RSA_public_encrypt(flen, (unsigned char *)fbuf,
(unsigned char *)tbuf, rsa, padding);
if (tlen == -1) {
PyMem_Free(tbuf);
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
PyMem_Free(tbuf);
return ret;
}
PyObject *rsa_private_decrypt(RSA *rsa, PyObject *from, int padding) {
const void *fbuf;
void *tbuf;
int flen, tlen;
PyObject *ret;
if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
return NULL;
if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
PyErr_SetString(PyExc_MemoryError, "rsa_private_decrypt");
return NULL;
}
tlen = RSA_private_decrypt(flen, (unsigned char *)fbuf,
(unsigned char *)tbuf, rsa, padding);
if (tlen == -1) {
PyMem_Free(tbuf);
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
PyMem_Free(tbuf);
return ret;
}
#if OPENSSL_VERSION_NUMBER >= 0x0090708fL
PyObject *rsa_padding_add_pkcs1_pss(RSA *rsa, PyObject *digest, EVP_MD *hash, int salt_length) {
const void *dbuf;
unsigned char *tbuf;
int dlen, result, tlen;
PyObject *ret;
if (m2_PyObject_AsReadBufferInt(digest, &dbuf, &dlen) == -1)
return NULL;
tlen = RSA_size(rsa);
if (!(tbuf = OPENSSL_malloc(tlen))) {
PyErr_SetString(PyExc_MemoryError, "rsa_padding_add_pkcs1_pss");
return NULL;
}
result = RSA_padding_add_PKCS1_PSS(
rsa,
tbuf,
(unsigned char *)dbuf,
hash,
salt_length);
if (result == -1) {
OPENSSL_cleanse(tbuf, tlen);
OPENSSL_free(tbuf);
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
OPENSSL_cleanse(tbuf, tlen);
OPENSSL_free(tbuf);
return ret;
}
int rsa_verify_pkcs1_pss(RSA *rsa, PyObject *digest, PyObject *signature, EVP_MD *hash, int salt_length) {
const void *dbuf;
const void *sbuf;
int dlen, slen, ret;
if (m2_PyObject_AsReadBufferInt(digest, &dbuf, &dlen) == -1) {
return 0;
}
if (m2_PyObject_AsReadBufferInt(signature, &sbuf, &slen) == -1) {
return 0;
}
ret = RSA_verify_PKCS1_PSS(
rsa,
(unsigned char *)dbuf,
hash,
(unsigned char *)sbuf,
salt_length);
return ret;
}
#endif
PyObject *rsa_sign(RSA *rsa, PyObject *py_digest_string, int method_type) {
int digest_len = 0;
int buf_len = 0;
int ret = 0;
unsigned int real_buf_len = 0;
char *digest_string = NULL;
unsigned char * sign_buf = NULL;
PyObject *signature;
ret = m2_PyString_AsStringAndSizeInt(py_digest_string, &digest_string,
&digest_len);
if (ret == -1) {
/* PyString_AsStringAndSize raises the correct exceptions. */
return NULL;
}
buf_len = RSA_size(rsa);
sign_buf = (unsigned char *)PyMem_Malloc(buf_len);
ret = RSA_sign(method_type, (const unsigned char *)digest_string, digest_len,
sign_buf, &real_buf_len, rsa);
if (!ret) {
PyMem_Free(sign_buf);
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return NULL;
}
signature = PyString_FromStringAndSize((const char*) sign_buf, buf_len);
PyMem_Free(sign_buf);
return signature;
}
int rsa_verify(RSA *rsa, PyObject *py_verify_string, PyObject* py_sign_string, int method_type){
int ret = 0;
char * sign_string = NULL;
char * verify_string = NULL;
int verify_len = 0;
int sign_len = 0;
ret = m2_PyString_AsStringAndSizeInt(py_verify_string, &verify_string,
&verify_len);
if (ret == -1) {
/* PyString_AsStringAndSize raises the correct exceptions. */
return 0;
}
ret = m2_PyString_AsStringAndSizeInt(py_sign_string, &sign_string,
&sign_len);
if (ret == -1) {
return 0;
}
ret = RSA_verify(method_type, (unsigned char *) verify_string,
verify_len, (unsigned char *) sign_string,
sign_len, rsa);
if (!ret) {
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
}
return ret;
}
void genrsa_callback(int p, int n, void *arg) {
PyObject *argv, *ret, *cbfunc;
cbfunc = (PyObject *)arg;
argv = Py_BuildValue("(ii)", p, n);
ret = PyEval_CallObject(cbfunc, argv);
PyErr_Clear();
Py_DECREF(argv);
Py_XDECREF(ret);
}
RSA *rsa_generate_key(int bits, unsigned long e, PyObject *pyfunc) {
RSA *rsa;
Py_INCREF(pyfunc);
rsa = RSA_generate_key(bits, e, genrsa_callback, (void *)pyfunc);
Py_DECREF(pyfunc);
if (!rsa)
PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
return rsa;
}
int rsa_type_check(RSA *rsa) {
return 1;
}
int rsa_check_pub_key(RSA *rsa) {
return (rsa->e) && (rsa->n);
}
%}
%threadallow rsa_write_key_der;
%inline %{
int rsa_write_key_der(RSA *rsa, BIO *bio) {
return i2d_RSAPrivateKey_bio(bio, rsa);
}
%}
|