Ticket #43240: m2crypto-fixes.diff
File m2crypto-fixes.diff, 50.4 KB (added by jul_bsd@…, 11 years ago) |
---|
-
SWIG/_evp.i
169 169 int iter, 170 170 int keylen) { 171 171 unsigned char key[EVP_MAX_KEY_LENGTH]; 172 unsigned char *saltbuf;173 char *passbuf;174 172 PyObject *ret; 175 int passlen, saltlen;173 Py_buffer passbuf, saltbuf; 176 174 177 if (m2_PyObject_AsReadBufferInt(pass, (const void **)&passbuf, 178 &passlen) == -1) 175 if (m2_PyObject_GetBufferInt(pass, &passbuf, PyBUF_SIMPLE) == -1) 176 return NULL; 177 if (m2_PyObject_GetBufferInt(salt, &saltbuf, PyBUF_SIMPLE) == -1) { 178 m2_PyBuffer_Release(pass, &passbuf); 179 179 return NULL; 180 if (m2_PyObject_AsReadBufferInt(salt, (const void **)&saltbuf, 181 &saltlen) == -1) 182 return NULL; 180 } 183 181 184 PKCS5_PBKDF2_HMAC_SHA1(passbuf, passlen, saltbuf, saltlen, iter, 185 keylen, key); 182 PKCS5_PBKDF2_HMAC_SHA1((char *) passbuf.buf, passbuf.len, 183 (unsigned char *) saltbuf.buf, saltbuf.len, 184 iter, keylen, key); 186 185 ret = PyString_FromStringAndSize((char*)key, keylen); 187 186 OPENSSL_cleanse(key, keylen); 187 m2_PyBuffer_Release(pass, &passbuf); 188 m2_PyBuffer_Release(salt, &saltbuf); 188 189 return ret; 189 190 } 190 191 … … 202 203 } 203 204 204 205 int digest_update(EVP_MD_CTX *ctx, PyObject *blob) { 205 const void *buf;206 Py_ssize_t len;206 Py_buffer buf; 207 int ret; 207 208 208 if ( PyObject_AsReadBuffer(blob, &buf, &len) == -1)209 209 if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1) 210 return -1; 210 211 211 return EVP_DigestUpdate(ctx, buf, len); 212 ret = EVP_DigestUpdate(ctx, buf.buf, buf.len); 213 m2_PyBuffer_Release(blob, &buf); 214 return ret; 212 215 } 213 216 214 217 PyObject *digest_final(EVP_MD_CTX *ctx) { … … 247 250 } 248 251 249 252 PyObject *hmac_init(HMAC_CTX *ctx, PyObject *key, const EVP_MD *md) { 250 const void *kbuf; 251 int klen; 253 Py_buffer kbuf; 252 254 253 if (m2_PyObject_ AsReadBufferInt(key, &kbuf, &klen) == -1)254 255 if (m2_PyObject_GetBufferInt(key, &kbuf, PyBUF_SIMPLE) == -1) 256 return NULL; 255 257 256 HMAC_Init(ctx, kbuf, klen, md); 258 HMAC_Init(ctx, kbuf.buf, kbuf.len, md); 259 m2_PyBuffer_Release(key, &kbuf); 257 260 Py_INCREF(Py_None); 258 261 return Py_None; 259 262 } 260 263 261 264 PyObject *hmac_update(HMAC_CTX *ctx, PyObject *blob) { 262 const void *buf; 263 Py_ssize_t len; 265 Py_buffer buf; 264 266 265 if ( PyObject_AsReadBuffer(blob, &buf, &len) == -1)266 267 if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1) 268 return NULL; 267 269 268 HMAC_Update(ctx, buf, len); 270 HMAC_Update(ctx, buf.buf, buf.len); 271 m2_PyBuffer_Release(blob, &buf); 269 272 Py_INCREF(Py_None); 270 273 return Py_None; 271 274 } … … 286 289 } 287 290 288 291 PyObject *hmac(PyObject *key, PyObject *data, const EVP_MD *md) { 289 const void *kbuf, *dbuf;290 292 void *blob; 291 int klen;292 293 unsigned int blen; 293 Py_ssize_t dlen;294 294 PyObject *ret; 295 Py_buffer kbuf, dbuf; 295 296 296 if ((m2_PyObject_AsReadBufferInt(key, &kbuf, &klen) == -1) 297 || (PyObject_AsReadBuffer(data, &dbuf, &dlen) == -1)) 297 if (m2_PyObject_GetBufferInt(key, &kbuf, PyBUF_SIMPLE) == -1) 298 298 return NULL; 299 if (m2_PyObject_GetBuffer(data, &dbuf, PyBUF_SIMPLE) == -1) { 300 m2_PyBuffer_Release(key, &kbuf); 301 return NULL; 302 } 299 303 300 304 if (!(blob = PyMem_Malloc(EVP_MAX_MD_SIZE))) { 301 305 PyErr_SetString(PyExc_MemoryError, "hmac"); 306 m2_PyBuffer_Release(key, &kbuf); 307 m2_PyBuffer_Release(data, &dbuf); 302 308 return NULL; 303 309 } 304 HMAC(md, kbuf , klen, dbuf, dlen, blob, &blen);310 HMAC(md, kbuf.buf, kbuf.len, dbuf.buf, dbuf.len, blob, &blen); 305 311 blob = PyMem_Realloc(blob, blen); 306 312 ret = PyString_FromStringAndSize(blob, blen); 307 313 PyMem_Free(blob); 314 m2_PyBuffer_Release(key, &kbuf); 315 m2_PyBuffer_Release(data, &dbuf); 308 316 return ret; 309 317 } 310 318 … … 329 337 PyObject *iv, /* Not used */ 330 338 int iter) { 331 339 unsigned char key[EVP_MAX_KEY_LENGTH]; 332 const void *dbuf, *sbuf; 333 int dlen, klen; 334 Py_ssize_t slen; 340 int klen; 335 341 PyObject *ret; 342 Py_buffer dbuf, sbuf; 336 343 337 if ((m2_PyObject_AsReadBufferInt(data, &dbuf, &dlen) == -1) 338 || (PyObject_AsReadBuffer(salt, &sbuf, &slen) == -1)) 344 if (m2_PyObject_GetBufferInt(data, &dbuf, PyBUF_SIMPLE) == -1) 339 345 return NULL; 346 if (m2_PyObject_GetBuffer(salt, &sbuf, PyBUF_SIMPLE) == -1) { 347 m2_PyBuffer_Release(data, &dbuf); 348 return NULL; 349 } 340 350 341 assert((s len == 8) || (slen == 0));342 klen = EVP_BytesToKey(cipher, md, (unsigned char *)sbuf ,343 (unsigned char *)dbuf , dlen, iter,351 assert((sbuf.len == 8) || (sbuf.len == 0)); 352 klen = EVP_BytesToKey(cipher, md, (unsigned char *)sbuf.buf, 353 (unsigned char *)dbuf.buf, dbuf.len, iter, 344 354 key, NULL); /* Since we are not returning IV no need to derive it */ 345 355 ret = PyString_FromStringAndSize((char*)key, klen); 356 m2_PyBuffer_Release(data, &dbuf); 357 m2_PyBuffer_Release(salt, &sbuf); 346 358 return ret; 347 359 } 348 360 349 361 PyObject *cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, 350 362 PyObject *key, PyObject *iv, int mode) { 351 const void *kbuf, *ibuf; 352 Py_ssize_t klen, ilen; 363 Py_buffer kbuf, ibuf; 353 364 354 if ((PyObject_AsReadBuffer(key, &kbuf, &klen) == -1) 355 || (PyObject_AsReadBuffer(iv, &ibuf, &ilen) == -1)) 365 if (m2_PyObject_GetBuffer(key, &kbuf, PyBUF_SIMPLE) == -1) 356 366 return NULL; 367 if (m2_PyObject_GetBuffer(iv, &ibuf, PyBUF_SIMPLE) == -1) { 368 m2_PyBuffer_Release(key, &kbuf); 369 return NULL; 370 } 357 371 358 if (!EVP_CipherInit(ctx, cipher, (unsigned char *)kbuf ,359 (unsigned char *)ibuf , mode)) {372 if (!EVP_CipherInit(ctx, cipher, (unsigned char *)kbuf.buf, 373 (unsigned char *)ibuf.buf, mode)) { 360 374 PyErr_SetString(_evp_err, ERR_reason_error_string(ERR_get_error())); 375 m2_PyBuffer_Release(key, &kbuf); 376 m2_PyBuffer_Release(iv, &ibuf); 361 377 return NULL; 362 378 } 363 379 Py_INCREF(Py_None); 380 m2_PyBuffer_Release(key, &kbuf); 381 m2_PyBuffer_Release(iv, &ibuf); 364 382 return Py_None; 365 383 } 366 384 367 385 PyObject *cipher_update(EVP_CIPHER_CTX *ctx, PyObject *blob) { 368 const void *buf; 369 int len, olen; 386 int olen; 370 387 void *obuf; 371 388 PyObject *ret; 389 Py_buffer buf; 372 390 373 if (m2_PyObject_ AsReadBufferInt(blob, &buf, &len) == -1)391 if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1) 374 392 return NULL; 375 393 376 if (!(obuf = PyMem_Malloc( len + EVP_CIPHER_CTX_block_size(ctx) - 1))) {394 if (!(obuf = PyMem_Malloc(buf.len + EVP_CIPHER_CTX_block_size(ctx) - 1))) { 377 395 PyErr_SetString(PyExc_MemoryError, "cipher_update"); 396 m2_PyBuffer_Release(blob, &buf); 378 397 return NULL; 379 398 } 380 if (!EVP_CipherUpdate(ctx, obuf, &olen, (unsigned char *)buf ,len)) {399 if (!EVP_CipherUpdate(ctx, obuf, &olen, (unsigned char *)buf.buf, buf.len)) { 381 400 PyMem_Free(obuf); 382 401 PyErr_SetString(_evp_err, ERR_reason_error_string(ERR_get_error())); 402 m2_PyBuffer_Release(blob, &buf); 383 403 return NULL; 384 404 } 385 405 ret = PyString_FromStringAndSize(obuf, olen); 386 406 PyMem_Free(obuf); 407 m2_PyBuffer_Release(blob, &buf); 387 408 return ret; 388 409 } 389 410 … … 407 428 } 408 429 409 430 PyObject *sign_update(EVP_MD_CTX *ctx, PyObject *blob) { 410 const void *buf; 411 Py_ssize_t len; 431 Py_buffer buf; 412 432 413 if ( PyObject_AsReadBuffer(blob, &buf, &len) == -1)433 if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1) 414 434 return NULL; 415 435 416 if (!EVP_SignUpdate(ctx, buf ,len)) {436 if (!EVP_SignUpdate(ctx, buf.buf, buf.len)) { 417 437 PyErr_SetString(_evp_err, ERR_reason_error_string(ERR_get_error())); 438 m2_PyBuffer_Release(blob, &buf); 418 439 return NULL; 419 440 } 420 441 Py_INCREF(Py_None); 442 m2_PyBuffer_Release(blob, &buf); 421 443 return Py_None; 422 444 } 423 445 … … 445 467 } 446 468 447 469 int verify_update(EVP_MD_CTX *ctx, PyObject *blob) { 448 const void *buf;449 Py_ssize_t len;470 Py_buffer buf; 471 int ret; 450 472 451 if ( PyObject_AsReadBuffer(blob, &buf, &len) == -1)473 if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1) 452 474 return -1; 453 454 return EVP_VerifyUpdate(ctx, buf, len); 475 ret = EVP_VerifyUpdate(ctx, buf.buf, buf.len); 476 m2_PyBuffer_Release(blob, &buf); 477 return ret; 455 478 } 456 479 457 480 458 481 int verify_final(EVP_MD_CTX *ctx, PyObject *blob, EVP_PKEY *pkey) { 459 unsigned char *kbuf;460 int len;482 Py_buffer kbuf; 483 int ret; 461 484 462 if (m2_PyObject_ AsReadBufferInt(blob, (const void **)&kbuf, &len) == -1)485 if (m2_PyObject_GetBufferInt(blob, &kbuf, PyBUF_SIMPLE) == -1) 463 486 return -1; 464 487 465 return EVP_VerifyFinal(ctx, kbuf, len, pkey); 488 ret = EVP_VerifyFinal(ctx, (unsigned char *) kbuf.buf, kbuf.len, pkey); 489 m2_PyBuffer_Release(blob, &kbuf); 490 return ret; 466 491 } 467 492 %} 468 493 -
SWIG/_dh.i
83 83 } 84 84 85 85 PyObject *dh_compute_key(DH *dh, PyObject *pubkey) { 86 const void *pkbuf;87 int pklen,klen;86 Py_buffer pkbuf; 87 int klen; 88 88 void *key; 89 89 BIGNUM *pk; 90 90 PyObject *ret; 91 91 92 if (m2_PyObject_ AsReadBufferInt(pubkey, &pkbuf, &pklen) == -1)93 92 if (m2_PyObject_GetBufferInt(pubkey, &pkbuf, PyBUF_SIMPLE) == -1) 93 return NULL; 94 94 95 if (!(pk = BN_mpi2bn((unsigned char *)pkbuf , pklen, NULL))) {95 if (!(pk = BN_mpi2bn((unsigned char *)pkbuf.buf, pkbuf.len, NULL))) { 96 96 PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error())); 97 m2_PyBuffer_Release(pubkey, &pkbuf); 97 98 return NULL; 98 99 } 99 100 if (!(key = PyMem_Malloc(DH_size(dh)))) { 100 101 BN_free(pk); 101 102 PyErr_SetString(PyExc_MemoryError, "dh_compute_key"); 103 m2_PyBuffer_Release(pubkey, &pkbuf); 102 104 return NULL; 103 105 } 104 106 if ((klen = DH_compute_key((unsigned char *)key, pk, dh)) == -1) { 105 107 BN_free(pk); 106 108 PyMem_Free(key); 107 109 PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error())); 110 m2_PyBuffer_Release(pubkey, &pkbuf); 108 111 return NULL; 109 112 } 110 113 ret = PyString_FromStringAndSize((const char *)key, klen); 111 114 BN_free(pk); 112 115 PyMem_Free(key); 116 m2_PyBuffer_Release(pubkey, &pkbuf); 113 117 return ret; 114 118 } 115 119 … … 147 151 148 152 PyObject *dh_set_p(DH *dh, PyObject *value) { 149 153 BIGNUM *bn; 150 const void *vbuf; 151 int vlen; 154 Py_buffer vbuf; 152 155 153 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)154 156 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 157 return NULL; 155 158 156 if (!(bn = BN_mpi2bn((unsigned char *)vbuf , vlen, NULL))) {159 if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) { 157 160 PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error())); 161 m2_PyBuffer_Release(value, &vbuf); 158 162 return NULL; 159 163 } 160 164 if (dh->p) 161 165 BN_free(dh->p); 162 166 dh->p = bn; 167 m2_PyBuffer_Release(value, &vbuf); 163 168 Py_INCREF(Py_None); 164 169 return Py_None; 165 170 } 166 171 167 172 PyObject *dh_set_g(DH *dh, PyObject *value) { 168 173 BIGNUM *bn; 169 const void *vbuf; 170 int vlen; 174 Py_buffer vbuf; 171 175 172 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)173 176 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 177 return NULL; 174 178 175 if (!(bn = BN_mpi2bn((unsigned char *)vbuf , vlen, NULL))) {179 if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) { 176 180 PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error())); 181 m2_PyBuffer_Release(value, &vbuf); 177 182 return NULL; 178 183 } 179 184 if (dh->g) 180 185 BN_free(dh->g); 181 186 dh->g = bn; 182 187 Py_INCREF(Py_None); 188 m2_PyBuffer_Release(value, &vbuf); 183 189 return Py_None; 184 190 } 185 191 %} -
SWIG/_ssl.i
315 315 } 316 316 317 317 int ssl_ctx_set_session_id_context(SSL_CTX *ctx, PyObject *sid_ctx) { 318 const void *buf;319 int len;318 Py_buffer buf; 319 int ret; 320 320 321 if (m2_PyObject_AsReadBufferInt(sid_ctx, &buf, &len) == -1) 322 return -1; 323 324 return SSL_CTX_set_session_id_context(ctx, buf, len); 321 if (m2_PyObject_GetBufferInt(sid_ctx, &buf, PyBUF_SIMPLE) == -1) 322 return -1; 323 ret = SSL_CTX_set_session_id_context(ctx, buf.buf, buf.len); 324 m2_PyBuffer_Release(sid_ctx, &buf); 325 return ret; 325 326 } 326 327 327 328 void ssl_ctx_set_info_callback(SSL_CTX *ctx, PyObject *pyfunc) { … … 384 385 } 385 386 386 387 int ssl_set_session_id_context(SSL *ssl, PyObject *sid_ctx) { 387 const void *buf;388 int len;388 Py_buffer buf; 389 int ret; 389 390 390 if (m2_PyObject_ AsReadBufferInt(sid_ctx, &buf, &len) == -1)391 391 if (m2_PyObject_GetBufferInt(sid_ctx, &buf, PyBUF_SIMPLE) == -1) 392 return -1; 392 393 393 return SSL_set_session_id_context(ssl, buf, len); 394 ret = SSL_set_session_id_context(ssl, buf.buf, buf.len); 395 m2_PyBuffer_Release(sid_ctx, &buf); 396 return ret; 394 397 } 395 398 396 399 int ssl_set_fd(SSL *ssl, int fd) { … … 583 586 } 584 587 585 588 int ssl_write(SSL *ssl, PyObject *blob) { 586 const void *buf;587 int len, r, err, ret;589 int r, err, ret; 590 Py_buffer buf; 588 591 589 590 if (m2_PyObject_AsReadBufferInt(blob, &buf, &len) == -1) { 592 if (m2_PyObject_GetBufferInt(blob, &buf, PyBUF_SIMPLE) == -1) 591 593 return -1; 592 }593 594 594 595 595 Py_BEGIN_ALLOW_THREADS 596 r = SSL_write(ssl, buf ,len);596 r = SSL_write(ssl, buf.buf, buf.len); 597 597 Py_END_ALLOW_THREADS 598 598 599 599 … … 623 623 ret = -1; 624 624 } 625 625 626 626 m2_PyBuffer_Release(blob, &buf); 627 627 return ret; 628 628 } 629 629 630 630 int ssl_write_nbio(SSL *ssl, PyObject *blob) { 631 const void *buf;632 int len, r, err, ret;631 int r, err, ret; 632 Py_buffer buf; 633 633 634 635 if (m2_PyObject_AsReadBufferInt(blob, &buf, &len) == -1) { 634 if (m2_PyObject_GetBufferInt(blob, &buf, PyBUF_SIMPLE) == -1) 636 635 return -1; 637 }638 639 636 640 637 Py_BEGIN_ALLOW_THREADS 641 r = SSL_write(ssl, buf ,len);638 r = SSL_write(ssl, buf.buf, buf.len); 642 639 Py_END_ALLOW_THREADS 643 640 644 641 … … 667 664 ret = -1; 668 665 } 669 666 670 667 m2_PyBuffer_Release(blob, &buf); 671 668 return ret; 672 669 } 673 670 -
SWIG/_lib.h
1 1 /* Copyright (c) 1999 Ng Pheng Siong. All rights reserved. */ 2 2 /* $Id$ */ 3 3 4 #if PY_VERSION_HEX < 0x02060000 5 typedef struct { /* Only a subset */ 6 void *buf; 7 Py_ssize_t len; 8 } Py_buffer; 9 10 #define PyBUF_SIMPLE 0 11 #endif /* PY_VERSION_HEX < 0x02060000 */ 12 4 13 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) 5 14 typedef int Py_ssize_t; 6 15 #define PY_SSIZE_T_MAX INT_MAX … … 8 17 #endif 9 18 10 19 typedef struct _blob { 11 12 20 unsigned char *data; 21 int len; 13 22 } Blob; 14 23 15 24 Blob *blob_new(int len, const char *errmsg); 16 25 Blob *blob_copy(Blob *from, const char *errmsg); 17 26 void blob_free(Blob *blob); 18 27 28 /* Always use these together, to correctly handle non-memoryview objects. */ 29 static int m2_PyObject_GetBufferInt(PyObject *obj, Py_buffer *view, int flags); 30 static int m2_PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags); 31 static void m2_PyBuffer_Release(PyObject *obj, Py_buffer *view); 32 19 33 static int m2_PyObject_AsReadBufferInt(PyObject *obj, const void **buffer, 20 34 int *buffer_len); 21 35 static int m2_PyString_AsStringAndSizeInt(PyObject *obj, char **s, int *len); … … 24 38 int passphrase_callback(char *buf, int num, int v, void *userdata); 25 39 26 40 void lib_init(void); 27 -
SWIG/_lib.i
47 47 /* Python helpers. */ 48 48 49 49 %} 50 %ignore PyBuffer_Release; 51 %ignore PyObject_CheckBuffer; 52 %ignore PyObject_GetBuffer; 53 %ignore m2_PyBuffer_Release; 54 %ignore m2_PyObject_GetBuffer; 55 %ignore m2_PyObject_GetBufferInt; 50 56 %ignore m2_PyObject_AsReadBufferInt; 51 57 %ignore m2_PyString_AsStringAndSizeInt; 52 58 %{ 59 60 61 #if PY_VERSION_HEX < 0x02060000 62 static int PyObject_CheckBuffer(PyObject *obj) { 63 (void)obj; 64 return 0; 65 } 66 67 static int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) { 68 (void)obj; 69 (void)view; 70 (void)flags; 71 return -1; 72 } 73 74 static void PyBuffer_Release(Py_buffer *view) { 75 (void)view; 76 } 77 #endif /* PY_VERSION_HEX < 0x02060000 */ 78 79 80 static void m2_PyBuffer_Release(PyObject *obj, Py_buffer *view) { 81 if (PyObject_CheckBuffer(obj)) 82 PyBuffer_Release(view); 83 /* else do nothing, view->buf comes from PyObject_AsReadBuffer */ 84 } 85 86 static int m2_PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) { 87 int ret; 88 89 if (PyObject_CheckBuffer(obj)) 90 ret = PyObject_GetBuffer(obj, view, flags); 91 else { 92 const void *buf; 93 94 ret = PyObject_AsReadBuffer(obj, &buf, &view->len); 95 96 if (ret == 0) 97 view->buf = (void *)buf; 98 } 99 return ret; 100 } 101 102 103 static int m2_PyObject_GetBufferInt(PyObject *obj, Py_buffer *view, int flags) { 104 int ret; 105 106 ret = m2_PyObject_GetBuffer(obj, view, flags); 107 if (ret) 108 return ret; 109 if (view->len > INT_MAX) { 110 PyErr_SetString(PyExc_ValueError, "object too large"); 111 m2_PyBuffer_Release(obj, view); 112 return -1; 113 } 114 return 0; 115 } 116 53 117 static int 54 118 m2_PyObject_AsReadBufferInt(PyObject *obj, const void **buffer, 55 119 int *buffer_len) … … 73 137 { 74 138 int ret; 75 139 Py_ssize_t len2; 76 77 140 ret = PyString_AsStringAndSize(obj, s, &len2); 78 141 if (ret) 79 142 return ret; … … 318 381 } 319 382 320 383 BIGNUM *mpi_to_bn(PyObject *value) { 321 const void *vbuf;322 int vlen;384 Py_buffer vbuf; 385 BIGNUM *ret; 323 386 324 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)325 387 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 388 return NULL; 326 389 327 return BN_mpi2bn(vbuf, vlen, NULL); 390 ret = BN_mpi2bn(vbuf.buf, vbuf.len, NULL); 391 m2_PyBuffer_Release(value, &vbuf); 392 return ret; 328 393 } 329 394 330 395 PyObject *bn_to_bin(BIGNUM *bn) { … … 344 409 } 345 410 346 411 BIGNUM *bin_to_bn(PyObject *value) { 347 const void *vbuf;348 int vlen;412 Py_buffer vbuf; 413 BIGNUM *ret; 349 414 350 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)351 415 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 416 return NULL; 352 417 353 return BN_bin2bn(vbuf, vlen, NULL); 418 ret = BN_bin2bn(vbuf.buf, vbuf.len, NULL); 419 m2_PyBuffer_Release(value, &vbuf); 420 return ret; 354 421 } 355 422 356 423 PyObject *bn_to_hex(BIGNUM *bn) { … … 372 439 } 373 440 374 441 BIGNUM *hex_to_bn(PyObject *value) { 375 const void *vbuf; 376 Py_ssize_t vlen; 442 Py_buffer vbuf; 377 443 BIGNUM *bn; 378 444 379 if ( PyObject_AsReadBuffer(value, &vbuf, &vlen) == -1)380 445 if (m2_PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) == -1) 446 return NULL; 381 447 382 448 if ((bn=BN_new())==NULL) { 383 449 PyErr_SetString(PyExc_MemoryError, "hex_to_bn"); 450 m2_PyBuffer_Release(value, &vbuf); 384 451 return NULL; 385 452 } 386 if (BN_hex2bn(&bn, (const char *)vbuf ) <= 0) {453 if (BN_hex2bn(&bn, (const char *)vbuf.buf) <= 0) { 387 454 PyErr_SetString(PyExc_RuntimeError, 388 455 ERR_error_string(ERR_get_error(), NULL)); 389 456 BN_free(bn); 457 m2_PyBuffer_Release(value, &vbuf); 390 458 return NULL; 391 459 } 460 m2_PyBuffer_Release(value, &vbuf); 392 461 return bn; 393 462 } 394 463 395 464 BIGNUM *dec_to_bn(PyObject *value) { 396 const void *vbuf; 397 Py_ssize_t vlen; 465 Py_buffer vbuf; 398 466 BIGNUM *bn; 399 467 400 if ( PyObject_AsReadBuffer(value, &vbuf, &vlen) == -1)401 468 if (m2_PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) == -1) 469 return NULL; 402 470 403 471 if ((bn=BN_new())==NULL) { 404 472 PyErr_SetString(PyExc_MemoryError, "dec_to_bn"); 473 m2_PyBuffer_Release(value, &vbuf); 405 474 return NULL; 406 475 } 407 if ((BN_dec2bn(&bn, (const char *)vbuf ) <= 0)) {476 if ((BN_dec2bn(&bn, (const char *)vbuf.buf) <= 0)) { 408 477 PyErr_SetString(PyExc_RuntimeError, 409 478 ERR_error_string(ERR_get_error(), NULL)); 410 479 BN_free(bn); 480 m2_PyBuffer_Release(value, &vbuf); 411 481 return NULL; 412 482 } 483 m2_PyBuffer_Release(value, &vbuf); 413 484 return bn; 414 485 } 415 486 %} -
SWIG/_dsa.i
99 99 100 100 PyObject *dsa_set_p(DSA *dsa, PyObject *value) { 101 101 BIGNUM *bn; 102 const void *vbuf; 103 int vlen; 102 Py_buffer vbuf; 104 103 105 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)106 104 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 105 return NULL; 107 106 108 if (!(bn = BN_mpi2bn((unsigned char *)vbuf , vlen, NULL))) {107 if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) { 109 108 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 109 m2_PyBuffer_Release(value, &vbuf); 110 110 return NULL; 111 111 } 112 112 if (dsa->p) 113 113 BN_free(dsa->p); 114 114 dsa->p = bn; 115 m2_PyBuffer_Release(value, &vbuf); 115 116 Py_INCREF(Py_None); 116 117 return Py_None; 117 118 } 118 119 119 120 PyObject *dsa_set_q(DSA *dsa, PyObject *value) { 120 121 BIGNUM *bn; 121 const void *vbuf; 122 int vlen; 122 Py_buffer vbuf; 123 123 124 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)125 124 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 125 return NULL; 126 126 127 if (!(bn = BN_mpi2bn((unsigned char *)vbuf , vlen, NULL))) {127 if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) { 128 128 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 129 m2_PyBuffer_Release(value, &vbuf); 129 130 return NULL; 130 131 } 131 132 if (dsa->q) 132 133 BN_free(dsa->q); 133 134 dsa->q = bn; 135 m2_PyBuffer_Release(value, &vbuf); 134 136 Py_INCREF(Py_None); 135 137 return Py_None; 136 138 } 137 139 138 140 PyObject *dsa_set_g(DSA *dsa, PyObject *value) { 139 141 BIGNUM *bn; 140 const void *vbuf; 141 int vlen; 142 Py_buffer vbuf; 142 143 143 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)144 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 144 145 return NULL; 145 146 146 if (!(bn = BN_mpi2bn((unsigned char *)vbuf , vlen, NULL))) {147 if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) { 147 148 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 149 m2_PyBuffer_Release(value, &vbuf); 148 150 return NULL; 149 151 } 150 152 if (dsa->g) 151 153 BN_free(dsa->g); 152 154 dsa->g = bn; 155 m2_PyBuffer_Release(value, &vbuf); 153 156 Py_INCREF(Py_None); 154 157 return Py_None; 155 158 } … … 236 239 } 237 240 238 241 PyObject *dsa_sign(DSA *dsa, PyObject *value) { 239 const void *vbuf; 240 int vlen; 242 Py_buffer vbuf; 241 243 PyObject *tuple; 242 244 DSA_SIG *sig; 243 245 244 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)246 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 245 247 return NULL; 246 248 247 if (!(sig = DSA_do_sign(vbuf , vlen, dsa))) {249 if (!(sig = DSA_do_sign(vbuf.buf, vbuf.len, dsa))) { 248 250 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 251 m2_PyBuffer_Release(value, &vbuf); 249 252 return NULL; 250 253 } 251 254 if (!(tuple = PyTuple_New(2))) { 252 255 DSA_SIG_free(sig); 253 256 PyErr_SetString(PyExc_RuntimeError, "PyTuple_New() fails"); 257 m2_PyBuffer_Release(value, &vbuf); 254 258 return NULL; 255 259 } 256 260 PyTuple_SET_ITEM(tuple, 0, dsa_sig_get_r(sig)); 257 261 PyTuple_SET_ITEM(tuple, 1, dsa_sig_get_s(sig)); 258 262 DSA_SIG_free(sig); 263 m2_PyBuffer_Release(value, &vbuf); 259 264 return tuple; 260 265 } 261 266 262 267 int dsa_verify(DSA *dsa, PyObject *value, PyObject *r, PyObject *s) { 263 const void *vbuf, *rbuf, *sbuf; 264 int vlen, rlen, slen; 268 Py_buffer vbuf, rbuf, sbuf; 265 269 DSA_SIG *sig; 266 270 int ret; 267 271 268 if ((m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1) 269 || (m2_PyObject_AsReadBufferInt(r, &rbuf, &rlen) == -1) 270 || (m2_PyObject_AsReadBufferInt(s, &sbuf, &slen) == -1)) 272 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 271 273 return -1; 274 if (m2_PyObject_GetBufferInt(r, &rbuf, PyBUF_SIMPLE) == -1) { 275 m2_PyBuffer_Release(value, &vbuf); 276 return -1; 277 } 278 if (m2_PyObject_GetBufferInt(s, &sbuf, PyBUF_SIMPLE) == -1) { 279 m2_PyBuffer_Release(value, &vbuf); 280 m2_PyBuffer_Release(r, &rbuf); 281 return -1; 282 } 272 283 273 284 if (!(sig = DSA_SIG_new())) { 274 285 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 286 m2_PyBuffer_Release(value, &vbuf); 287 m2_PyBuffer_Release(r, &rbuf); 288 m2_PyBuffer_Release(s, &sbuf); 275 289 return -1; 276 290 } 277 if (!(sig->r = BN_mpi2bn((unsigned char *)rbuf , rlen, NULL))) {291 if (!(sig->r = BN_mpi2bn((unsigned char *)rbuf.buf, rbuf.len, NULL))) { 278 292 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 279 293 DSA_SIG_free(sig); 294 m2_PyBuffer_Release(value, &vbuf); 295 m2_PyBuffer_Release(r, &rbuf); 296 m2_PyBuffer_Release(s, &sbuf); 280 297 return -1; 281 298 } 282 if (!(sig->s = BN_mpi2bn((unsigned char *)sbuf , slen, NULL))) {299 if (!(sig->s = BN_mpi2bn((unsigned char *)sbuf.buf, sbuf.len, NULL))) { 283 300 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 284 301 DSA_SIG_free(sig); 302 m2_PyBuffer_Release(value, &vbuf); 303 m2_PyBuffer_Release(r, &rbuf); 304 m2_PyBuffer_Release(s, &sbuf); 285 305 return -1; 286 306 } 287 ret = DSA_do_verify(vbuf , vlen, sig, dsa);307 ret = DSA_do_verify(vbuf.buf, vbuf.len, sig, dsa); 288 308 DSA_SIG_free(sig); 289 309 if (ret == -1) 290 310 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 311 m2_PyBuffer_Release(value, &vbuf); 312 m2_PyBuffer_Release(r, &rbuf); 313 m2_PyBuffer_Release(s, &sbuf); 291 314 return ret; 292 315 } 293 316 294 317 PyObject *dsa_sign_asn1(DSA *dsa, PyObject *value) { 295 const void *vbuf; 296 int vlen; 318 Py_buffer vbuf; 297 319 void *sigbuf; 298 320 unsigned int siglen; 299 321 PyObject *ret; 300 322 301 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)323 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 302 324 return NULL; 303 325 304 326 if (!(sigbuf = PyMem_Malloc(DSA_size(dsa)))) { 305 327 PyErr_SetString(PyExc_MemoryError, "dsa_sign_asn1"); 328 m2_PyBuffer_Release(value, &vbuf); 306 329 return NULL; 307 330 } 308 if (!DSA_sign(0, vbuf, vlen, (unsigned char *)sigbuf, &siglen, dsa)) { 331 if (!DSA_sign(0, vbuf.buf, vbuf.len, 332 (unsigned char *)sigbuf, &siglen, dsa)) { 309 333 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 310 334 PyMem_Free(sigbuf); 335 m2_PyBuffer_Release(value, &vbuf); 311 336 return NULL; 312 337 } 313 338 ret = PyString_FromStringAndSize(sigbuf, siglen); 314 339 PyMem_Free(sigbuf); 340 m2_PyBuffer_Release(value, &vbuf); 315 341 return ret; 316 342 } 317 343 318 344 int dsa_verify_asn1(DSA *dsa, PyObject *value, PyObject *sig) { 319 const void *vbuf; 320 void *sbuf; 321 int vlen, slen, ret; 345 int ret; 346 Py_buffer vbuf, sbuf; 322 347 323 if ((m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1) 324 || (m2_PyObject_AsReadBufferInt(sig, (const void **)&sbuf, &slen) 325 == -1)) 326 return -1; 348 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 349 return -1; 350 if (m2_PyObject_GetBufferInt(sig, &sbuf, PyBUF_SIMPLE) == -1) { 351 m2_PyBuffer_Release(value, &vbuf); 352 return -1; 353 } 327 354 328 if ((ret = DSA_verify(0, vbuf, vlen, sbuf, slen, dsa)) == -1) 355 if ((ret = DSA_verify(0, (const void *) vbuf.buf, vbuf.len, 356 (void *) sbuf.buf, sbuf.len, dsa)) == -1) 329 357 PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error())); 358 m2_PyBuffer_Release(value, &vbuf); 359 m2_PyBuffer_Release(sig, &sbuf); 330 360 return ret; 331 361 } 332 362 -
SWIG/_aes.i
42 42 // otherwise: encrypt (Python code will supply the value 1.) 43 43 */ 44 44 PyObject *AES_set_key(AES_KEY *key, PyObject *value, int bits, int op) { 45 const void *vbuf; 46 Py_ssize_t vlen; 45 Py_buffer vbuf; 47 46 48 if ( PyObject_AsReadBuffer(value, &vbuf, &vlen) == -1)49 47 if (m2_PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) == -1) 48 return NULL; 50 49 51 50 if (op == 0) 52 AES_set_encrypt_key(vbuf , bits, key);51 AES_set_encrypt_key(vbuf.buf, bits, key); 53 52 else 54 AES_set_decrypt_key(vbuf, bits, key); 53 AES_set_decrypt_key(vbuf.buf, bits, key); 54 m2_PyBuffer_Release(value, &vbuf); 55 55 Py_INCREF(Py_None); 56 56 return Py_None; 57 57 } … … 61 61 // otherwise: encrypt (Python code will supply the value 1.) 62 62 */ 63 63 PyObject *AES_crypt(const AES_KEY *key, PyObject *in, int outlen, int op) { 64 const void *buf;65 Py_ssize_t len;66 64 unsigned char *out; 65 Py_buffer buf; 67 66 68 if ( PyObject_AsReadBuffer(in, &buf, &len) == -1)69 67 if (m2_PyObject_GetBuffer(in, &buf, PyBUF_SIMPLE) == -1) 68 return NULL; 70 69 71 70 if (!(out=(unsigned char *)PyMem_Malloc(outlen))) { 72 71 PyErr_SetString(PyExc_MemoryError, "AES_crypt"); 72 m2_PyBuffer_Release(in, &buf); 73 73 return NULL; 74 74 } 75 75 76 if (op == 0) 76 AES_encrypt((const unsigned char *) in, out, key);77 AES_encrypt((const unsigned char *)buf.buf, out, key); 77 78 else 78 AES_decrypt((const unsigned char *)in, out, key); 79 AES_decrypt((const unsigned char *)buf.buf, out, key); 80 m2_PyBuffer_Release(in, &buf); 79 81 return PyString_FromStringAndSize((char*)out, outlen); 80 82 } 81 83 -
SWIG/_bio.i
121 121 } 122 122 123 123 int bio_write(BIO *bio, PyObject *from) { 124 const void *fbuf;125 int flen,ret;124 Py_buffer fbuf; 125 int ret; 126 126 127 if (m2_PyObject_ AsReadBufferInt(from, &fbuf, &flen) == -1)128 127 if (m2_PyObject_GetBufferInt(from, &fbuf, PyBUF_SIMPLE) == -1) 128 return -1; 129 129 130 130 Py_BEGIN_ALLOW_THREADS 131 ret = BIO_write(bio, fbuf , flen);131 ret = BIO_write(bio, fbuf.buf, fbuf.len); 132 132 Py_END_ALLOW_THREADS 133 133 if (ret < 0) { 134 134 if (ERR_peek_error()) { 135 135 PyErr_SetString(_bio_err, ERR_reason_error_string(ERR_get_error())); 136 136 } 137 137 } 138 m2_PyBuffer_Release(from, &fbuf); 138 139 return ret; 139 140 } 140 141 … … 175 176 } 176 177 177 178 PyObject *bio_set_cipher(BIO *b, EVP_CIPHER *c, PyObject *key, PyObject *iv, int op) { 178 const void *kbuf, *ibuf; 179 Py_ssize_t klen, ilen; 179 Py_buffer kbuf, ibuf; 180 180 181 if ((PyObject_AsReadBuffer(key, &kbuf, &klen) == -1) 182 || (PyObject_AsReadBuffer(iv, &ibuf, &ilen) == -1)) 183 return NULL; 181 if (m2_PyObject_GetBuffer(key, &kbuf, PyBUF_SIMPLE) == -1) 182 return NULL; 184 183 184 if (m2_PyObject_GetBuffer(iv, &ibuf, PyBUF_SIMPLE) == -1) { 185 m2_PyBuffer_Release(key, &kbuf); 186 return NULL; 187 } 188 185 189 BIO_set_cipher(b, (const EVP_CIPHER *)c, 186 (unsigned char *)kbuf, (unsigned char *)ibuf, op); 190 (unsigned char *)kbuf.buf, (unsigned char *)ibuf.buf, op); 191 m2_PyBuffer_Release(iv, &ibuf); 192 m2_PyBuffer_Release(key, &kbuf); 187 193 Py_INCREF(Py_None); 188 194 return Py_None; 189 195 } -
SWIG/_util.i
17 17 18 18 PyObject *util_hex_to_string(PyObject *blob) { 19 19 PyObject *obj; 20 const void *buf;21 20 char *ret; 22 Py_ ssize_t len;21 Py_buffer buf; 23 22 24 if ( PyObject_AsReadBuffer(blob, &buf, &len) == -1)25 23 if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1) 24 return NULL; 26 25 27 ret = hex_to_string((unsigned char *)buf ,len);26 ret = hex_to_string((unsigned char *)buf.buf, buf.len); 28 27 if (!ret) { 29 28 PyErr_SetString(_util_err, ERR_reason_error_string(ERR_get_error())); 29 m2_PyBuffer_Release(blob, &buf); 30 30 return NULL; 31 31 } 32 32 obj = PyString_FromString(ret); 33 33 OPENSSL_free(ret); 34 m2_PyBuffer_Release(blob, &buf); 34 35 return obj; 35 36 } 36 37 37 38 PyObject *util_string_to_hex(PyObject *blob) { 38 39 PyObject *obj; 39 const void *buf;40 40 unsigned char *ret; 41 Py_ssize_t len0;42 41 long len; 42 Py_buffer buf; 43 43 44 if ( PyObject_AsReadBuffer(blob, &buf, &len0) == -1)45 44 if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1) 45 return NULL; 46 46 47 len = len0;48 ret = string_to_hex((char *)buf , &len);47 len = buf.len; 48 ret = string_to_hex((char *)buf.buf, &len); 49 49 if (ret == NULL) { 50 50 PyErr_SetString(_util_err, ERR_reason_error_string(ERR_get_error())); 51 m2_PyBuffer_Release(blob, &buf); 51 52 return NULL; 52 53 } 53 54 obj = PyString_FromStringAndSize((char*)ret, len); 54 55 OPENSSL_free(ret); 56 m2_PyBuffer_Release(blob, &buf); 55 57 return obj; 56 58 } 57 59 %} -
SWIG/_rand.i
26 26 } 27 27 28 28 PyObject *rand_seed(PyObject *seed) { 29 const void *buf; 30 int len; 29 Py_buffer buf; 31 30 32 if (m2_PyObject_ AsReadBufferInt(seed, &buf, &len) == -1)31 if (m2_PyObject_GetBufferInt(seed, &buf, PyBUF_SIMPLE) == -1) 33 32 return NULL; 34 33 35 RAND_seed(buf, len); 34 RAND_seed(buf.buf, buf.len); 35 m2_PyBuffer_Release(seed, &buf); 36 36 Py_INCREF(Py_None); 37 37 return Py_None; 38 38 } 39 39 40 40 PyObject *rand_add(PyObject *blob, double entropy) { 41 const void *buf; 42 int len; 41 Py_buffer buf; 43 42 44 if (m2_PyObject_ AsReadBufferInt(blob, &buf, &len) == -1)43 if (m2_PyObject_GetBufferInt(blob, &buf, PyBUF_SIMPLE) == -1) 45 44 return NULL; 46 45 47 RAND_add(buf, len, entropy); 46 RAND_add(buf.buf, buf.len, entropy); 47 m2_PyBuffer_Release(blob, &buf); 48 48 Py_INCREF(Py_None); 49 49 return Py_None; 50 50 } -
SWIG/_rsa.i
120 120 121 121 PyObject *rsa_set_e(RSA *rsa, PyObject *value) { 122 122 BIGNUM *bn; 123 const void *vbuf; 124 int vlen; 123 Py_buffer pybuf; 125 124 126 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)127 125 if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1) 126 return NULL; 128 127 129 if (!(bn = BN_mpi2bn((unsigned char *) vbuf, vlen, NULL))) {128 if (!(bn = BN_mpi2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) { 130 129 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 130 m2_PyBuffer_Release(value, &pybuf); 131 131 return NULL; 132 132 } 133 133 if (rsa->e) 134 134 BN_free(rsa->e); 135 m2_PyBuffer_Release(value, &pybuf); 135 136 rsa->e = bn; 136 137 Py_INCREF(Py_None); 137 138 return Py_None; … … 139 140 140 141 PyObject *rsa_set_n(RSA *rsa, PyObject *value) { 141 142 BIGNUM *bn; 142 const void *vbuf; 143 int vlen; 143 Py_buffer pybuf; 144 144 145 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)146 145 if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1) 146 return NULL; 147 147 148 if (!(bn = BN_mpi2bn((unsigned char *) vbuf, vlen, NULL))) {148 if (!(bn = BN_mpi2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) { 149 149 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 150 m2_PyBuffer_Release(value, &pybuf); 150 151 return NULL; 151 152 } 152 153 if (rsa->n) 153 154 BN_free(rsa->n); 155 m2_PyBuffer_Release(value, &pybuf); 154 156 rsa->n = bn; 155 157 Py_INCREF(Py_None); 156 158 return Py_None; … … 158 160 159 161 PyObject *rsa_set_e_bin(RSA *rsa, PyObject *value) { 160 162 BIGNUM *bn; 161 const void *vbuf; 162 int vlen; 163 Py_buffer pybuf; 163 164 164 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)165 165 if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1) 166 return NULL; 166 167 167 if (!(bn = BN_bin2bn((unsigned char *) vbuf, vlen, NULL))) {168 if (!(bn = BN_bin2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) { 168 169 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 170 m2_PyBuffer_Release(value, &pybuf); 169 171 return NULL; 170 172 } 171 173 if (rsa->e) 172 174 BN_free(rsa->e); 175 m2_PyBuffer_Release(value, &pybuf); 173 176 rsa->e = bn; 174 177 Py_INCREF(Py_None); 175 178 return Py_None; … … 177 180 178 181 PyObject *rsa_set_n_bin(RSA *rsa, PyObject *value) { 179 182 BIGNUM *bn; 180 const void *vbuf; 181 int vlen; 183 Py_buffer pybuf; 182 184 183 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)184 185 if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1) 186 return NULL; 185 187 186 if (!(bn = BN_bin2bn((unsigned char *) vbuf, vlen, NULL))) {188 if (!(bn = BN_bin2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) { 187 189 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 190 m2_PyBuffer_Release(value, &pybuf); 188 191 return NULL; 189 192 } 190 193 if (rsa->n) 191 194 BN_free(rsa->n); 195 m2_PyBuffer_Release(value, &pybuf); 192 196 rsa->n = bn; 193 197 Py_INCREF(Py_None); 194 198 return Py_None; 195 199 } 196 200 197 201 PyObject *rsa_private_encrypt(RSA *rsa, PyObject *from, int padding) { 198 const void *fbuf;199 202 void *tbuf; 200 int flen,tlen;203 int tlen; 201 204 PyObject *ret; 205 Py_buffer pybuf; 202 206 203 if (m2_PyObject_ AsReadBufferInt(from, &fbuf, &flen) == -1)204 207 if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1) 208 return NULL; 205 209 206 210 if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) { 207 211 PyErr_SetString(PyExc_MemoryError, "rsa_private_encrypt"); 212 m2_PyBuffer_Release(from, &pybuf); 208 213 return NULL; 209 214 } 210 tlen = RSA_private_encrypt( flen, (unsigned char *)fbuf,211 (unsigned char *)tbuf, rsa, padding);215 tlen = RSA_private_encrypt(pybuf.len, (unsigned char *)pybuf.buf, 216 (unsigned char *)tbuf, rsa, padding); 212 217 if (tlen == -1) { 213 218 PyMem_Free(tbuf); 219 m2_PyBuffer_Release(from, &pybuf); 214 220 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 215 221 return NULL; 216 222 } 217 223 ret = PyString_FromStringAndSize((const char *)tbuf, tlen); 218 224 PyMem_Free(tbuf); 225 m2_PyBuffer_Release(from, &pybuf); 219 226 return ret; 220 227 } 221 228 222 229 PyObject *rsa_public_decrypt(RSA *rsa, PyObject *from, int padding) { 223 const void *fbuf;224 230 void *tbuf; 225 int flen,tlen;231 int tlen; 226 232 PyObject *ret; 233 Py_buffer pybuf; 227 234 228 if (m2_PyObject_ AsReadBufferInt(from, &fbuf, &flen) == -1)235 if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1) 229 236 return NULL; 230 237 231 238 if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) { 232 239 PyErr_SetString(PyExc_MemoryError, "rsa_public_decrypt"); 240 m2_PyBuffer_Release(from, &pybuf); 233 241 return NULL; 234 242 } 235 tlen = RSA_public_decrypt( flen, (unsigned char *)fbuf,236 (unsigned char *)tbuf, rsa, padding);243 tlen = RSA_public_decrypt(pybuf.len, (unsigned char *)pybuf.buf, 244 (unsigned char *)tbuf, rsa, padding); 237 245 if (tlen == -1) { 238 246 PyMem_Free(tbuf); 247 m2_PyBuffer_Release(from, &pybuf); 239 248 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 240 249 return NULL; 241 250 } 242 251 ret = PyString_FromStringAndSize((const char *)tbuf, tlen); 243 252 PyMem_Free(tbuf); 253 m2_PyBuffer_Release(from, &pybuf); 244 254 return ret; 245 255 } 246 256 247 257 PyObject *rsa_public_encrypt(RSA *rsa, PyObject *from, int padding) { 248 const void *fbuf;249 258 void *tbuf; 250 int flen,tlen;259 int tlen; 251 260 PyObject *ret; 261 Py_buffer pybuf; 252 262 253 if (m2_PyObject_ AsReadBufferInt(from, &fbuf, &flen) == -1)263 if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1) 254 264 return NULL; 255 265 256 266 if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) { 257 267 PyErr_SetString(PyExc_MemoryError, "rsa_public_encrypt"); 268 m2_PyBuffer_Release(from, &pybuf); 258 269 return NULL; 259 270 } 260 tlen = RSA_public_encrypt( flen, (unsigned char *)fbuf,271 tlen = RSA_public_encrypt(pybuf.len, (unsigned char *)pybuf.buf, 261 272 (unsigned char *)tbuf, rsa, padding); 262 273 if (tlen == -1) { 263 274 PyMem_Free(tbuf); 275 m2_PyBuffer_Release(from, &pybuf); 264 276 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 265 277 return NULL; 266 278 } 267 279 ret = PyString_FromStringAndSize((const char *)tbuf, tlen); 268 280 PyMem_Free(tbuf); 281 m2_PyBuffer_Release(from, &pybuf); 269 282 return ret; 270 283 } 271 284 272 285 PyObject *rsa_private_decrypt(RSA *rsa, PyObject *from, int padding) { 273 const void *fbuf;274 286 void *tbuf; 275 int flen,tlen;287 int tlen; 276 288 PyObject *ret; 289 Py_buffer pybuf; 277 290 278 if (m2_PyObject_ AsReadBufferInt(from, &fbuf, &flen) == -1)291 if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1) 279 292 return NULL; 280 293 281 294 if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) { 282 295 PyErr_SetString(PyExc_MemoryError, "rsa_private_decrypt"); 296 m2_PyBuffer_Release(from, &pybuf); 283 297 return NULL; 284 298 } 285 tlen = RSA_private_decrypt(flen, (unsigned char *)fbuf, 286 (unsigned char *)tbuf, rsa, padding); 299 tlen = RSA_private_decrypt(pybuf.len, (unsigned char *)pybuf.buf, 300 (unsigned char *)tbuf, rsa, padding); 301 287 302 if (tlen == -1) { 288 303 PyMem_Free(tbuf); 304 m2_PyBuffer_Release(from, &pybuf); 289 305 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 290 306 return NULL; 291 307 } 292 308 ret = PyString_FromStringAndSize((const char *)tbuf, tlen); 293 309 PyMem_Free(tbuf); 310 m2_PyBuffer_Release(from, &pybuf); 294 311 return ret; 295 312 } 296 313 297 314 #if OPENSSL_VERSION_NUMBER >= 0x0090708fL 298 315 PyObject *rsa_padding_add_pkcs1_pss(RSA *rsa, PyObject *digest, EVP_MD *hash, int salt_length) { 299 const void *dbuf;300 316 unsigned char *tbuf; 301 int dlen,result, tlen;317 int result, tlen; 302 318 PyObject *ret; 319 Py_buffer dbuf; 303 320 304 if (m2_PyObject_ AsReadBufferInt(digest, &dbuf, &dlen) == -1)321 if (m2_PyObject_GetBufferInt(digest, &dbuf, PyBUF_SIMPLE) == -1) 305 322 return NULL; 306 323 307 324 tlen = RSA_size(rsa); 308 325 309 326 if (!(tbuf = OPENSSL_malloc(tlen))) { 310 327 PyErr_SetString(PyExc_MemoryError, "rsa_padding_add_pkcs1_pss"); 328 m2_PyBuffer_Release(digest, &dbuf); 311 329 return NULL; 312 330 } 313 331 result = RSA_padding_add_PKCS1_PSS( 314 332 rsa, 315 333 tbuf, 316 (unsigned char *)dbuf ,334 (unsigned char *)dbuf.buf, 317 335 hash, 318 336 salt_length); 319 337 320 338 if (result == -1) { 321 339 OPENSSL_cleanse(tbuf, tlen); 322 340 OPENSSL_free(tbuf); 341 m2_PyBuffer_Release(digest, &dbuf); 323 342 PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error())); 324 343 return NULL; 325 344 } 326 345 ret = PyString_FromStringAndSize((const char *)tbuf, tlen); 327 346 OPENSSL_cleanse(tbuf, tlen); 328 347 OPENSSL_free(tbuf); 348 m2_PyBuffer_Release(digest, &dbuf); 329 349 return ret; 330 350 } 331 351 332 352 int rsa_verify_pkcs1_pss(RSA *rsa, PyObject *digest, PyObject *signature, EVP_MD *hash, int salt_length) { 333 const void *dbuf; 334 const void *sbuf; 335 int dlen, slen, ret; 353 int ret; 354 Py_buffer dbuf, sbuf; 336 355 337 if (m2_PyObject_ AsReadBufferInt(digest, &dbuf, &dlen) == -1) {356 if (m2_PyObject_GetBufferInt(digest, &dbuf, PyBUF_SIMPLE) == -1) { 338 357 return 0; 339 358 } 340 341 if (m2_PyObject_AsReadBufferInt(signature, &sbuf, &slen) == -1) {359 if (m2_PyObject_GetBufferInt(signature, &sbuf, PyBUF_SIMPLE) == -1) { 360 m2_PyBuffer_Release(digest, &dbuf); 342 361 return 0; 343 362 } 344 363 345 364 ret = RSA_verify_PKCS1_PSS( 346 365 rsa, 347 (unsigned char *)dbuf ,366 (unsigned char *)dbuf.buf, 348 367 hash, 349 (unsigned char *)sbuf ,368 (unsigned char *)sbuf.buf, 350 369 salt_length); 351 370 371 m2_PyBuffer_Release(digest, &dbuf); 372 m2_PyBuffer_Release(signature, &sbuf); 352 373 return ret; 353 374 } 354 375 #endif -
SWIG/_ec.i
255 255 } 256 256 257 257 PyObject *ecdsa_sign(EC_KEY *key, PyObject *value) { 258 const void *vbuf;259 int vlen;260 258 PyObject *tuple; 261 259 ECDSA_SIG *sig; 260 Py_buffer vbuf; 262 261 263 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)264 262 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 263 return NULL; 265 264 266 if (!(sig = ECDSA_do_sign(vbuf , vlen, key))) {265 if (!(sig = ECDSA_do_sign(vbuf.buf, vbuf.len, key))) { 267 266 PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error())); 267 m2_PyBuffer_Release(value, &vbuf); 268 268 return NULL; 269 269 } 270 270 if (!(tuple = PyTuple_New(2))) { 271 271 ECDSA_SIG_free(sig); 272 272 PyErr_SetString(PyExc_RuntimeError, "PyTuple_New() fails"); 273 m2_PyBuffer_Release(value, &vbuf); 273 274 return NULL; 274 275 } 275 276 PyTuple_SET_ITEM(tuple, 0, ecdsa_sig_get_r(sig)); 276 277 PyTuple_SET_ITEM(tuple, 1, ecdsa_sig_get_s(sig)); 277 278 ECDSA_SIG_free(sig); 279 m2_PyBuffer_Release(value, &vbuf); 278 280 return tuple; 279 281 } 280 282 281 283 int ecdsa_verify(EC_KEY *key, PyObject *value, PyObject *r, PyObject *s) { 282 const void *vbuf, *rbuf, *sbuf; 283 int vlen, rlen, slen; 284 Py_buffer vbuf, rbuf, sbuf; 284 285 ECDSA_SIG *sig; 285 286 int ret; 286 287 287 if ((m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1) 288 || (m2_PyObject_AsReadBufferInt(r, &rbuf, &rlen) == -1) 289 || (m2_PyObject_AsReadBufferInt(s, &sbuf, &slen) == -1)) 290 return -1; 288 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 289 return -1; 290 if (m2_PyObject_GetBufferInt(r, &rbuf, PyBUF_SIMPLE) == -1) { 291 m2_PyBuffer_Release(value, &vbuf); 292 return -1; 293 } 294 if (m2_PyObject_GetBufferInt(s, &sbuf, PyBUF_SIMPLE) == -1) { 295 m2_PyBuffer_Release(value, &vbuf); 296 m2_PyBuffer_Release(r, &rbuf); 297 return -1; 298 } 291 299 292 300 if (!(sig = ECDSA_SIG_new())) { 293 301 PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error())); 302 m2_PyBuffer_Release(value, &vbuf); 303 m2_PyBuffer_Release(r, &rbuf); 304 m2_PyBuffer_Release(s, &sbuf); 294 305 return -1; 295 306 } 296 if (!BN_mpi2bn((unsigned char *)rbuf , rlen, sig->r)) {307 if (!BN_mpi2bn((unsigned char *)rbuf.buf, rbuf.len, sig->r)) { 297 308 PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error())); 298 309 ECDSA_SIG_free(sig); 310 m2_PyBuffer_Release(value, &vbuf); 311 m2_PyBuffer_Release(r, &rbuf); 312 m2_PyBuffer_Release(s, &sbuf); 299 313 return -1; 300 314 } 301 if (!BN_mpi2bn((unsigned char *)sbuf , slen, sig->s)) {315 if (!BN_mpi2bn((unsigned char *)sbuf.buf, sbuf.len, sig->s)) { 302 316 PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error())); 303 317 ECDSA_SIG_free(sig); 318 m2_PyBuffer_Release(value, &vbuf); 319 m2_PyBuffer_Release(r, &rbuf); 320 m2_PyBuffer_Release(s, &sbuf); 304 321 return -1; 305 322 } 306 ret = ECDSA_do_verify(vbuf , vlen, sig, key);323 ret = ECDSA_do_verify(vbuf.buf, vbuf.len, sig, key); 307 324 ECDSA_SIG_free(sig); 325 m2_PyBuffer_Release(value, &vbuf); 326 m2_PyBuffer_Release(r, &rbuf); 327 m2_PyBuffer_Release(s, &sbuf); 308 328 if (ret == -1) 309 329 PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error())); 310 330 return ret; … … 312 332 313 333 314 334 PyObject *ecdsa_sign_asn1(EC_KEY *key, PyObject *value) { 315 const void *vbuf; 316 int vlen; 335 Py_buffer vbuf; 317 336 void *sigbuf; 318 337 unsigned int siglen; 319 338 PyObject *ret; 320 339 321 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)340 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 322 341 return NULL; 323 342 324 343 if (!(sigbuf = PyMem_Malloc(ECDSA_size(key)))) { 325 344 PyErr_SetString(PyExc_MemoryError, "ecdsa_sign_asn1"); 345 m2_PyBuffer_Release(value, &vbuf); 326 346 return NULL; 327 347 } 328 if (!ECDSA_sign(0, vbuf, vlen, (unsigned char *)sigbuf, &siglen, key)) { 348 if (!ECDSA_sign(0, vbuf.buf, vbuf.len, 349 (unsigned char *)sigbuf, &siglen, key)) { 329 350 PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error())); 330 351 PyMem_Free(sigbuf); 352 m2_PyBuffer_Release(value, &vbuf); 331 353 return NULL; 332 354 } 333 355 ret = PyString_FromStringAndSize(sigbuf, siglen); 334 356 PyMem_Free(sigbuf); 357 m2_PyBuffer_Release(value, &vbuf); 335 358 return ret; 336 359 } 337 360 338 361 339 362 int ecdsa_verify_asn1(EC_KEY *key, PyObject *value, PyObject *sig) { 340 const void *vbuf; 341 void *sbuf; 342 int vlen, slen, ret; 363 Py_buffer vbuf, sbuf; 364 int ret; 343 365 344 if ((m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1) 345 || (m2_PyObject_AsReadBufferInt(sig, (const void **)&sbuf, &slen) 346 == -1)) 347 return -1; 366 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 367 return -1; 368 if (m2_PyObject_GetBufferInt(sig, &sbuf, PyBUF_SIMPLE) == -1) { 369 m2_PyBuffer_Release(value, &vbuf); 370 return -1; 371 } 348 372 349 if ((ret = ECDSA_verify(0, vbuf, vlen, sbuf, slen, key)) == -1) 373 if ((ret = ECDSA_verify(0, (const void *) vbuf.buf, vbuf.len, 374 sbuf.buf, sbuf.len, key)) == -1) 350 375 PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error())); 376 m2_PyBuffer_Release(value, &vbuf); 377 m2_PyBuffer_Release(sig, &sbuf); 351 378 return ret; 352 379 } 353 380 … … 391 418 EC_KEY *keypair; 392 419 393 420 if (PyObject_AsReadBuffer(pubkey, &keypairbuf, &keypairbuflen) == -1) 394 { 395 return NULL; 396 } 421 return NULL; 397 422 398 423 tempBuf = (const unsigned char *)keypairbuf; 399 424 if ((keypair = d2i_EC_PUBKEY( NULL, &tempBuf, keypairbuflen)) == 0) -
SWIG/_rc4.i
22 22 } 23 23 24 24 PyObject *rc4_set_key(RC4_KEY *key, PyObject *value) { 25 const void *vbuf; 26 int vlen; 25 Py_buffer vbuf; 27 26 28 if (m2_PyObject_ AsReadBufferInt(value, &vbuf, &vlen) == -1)27 if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1) 29 28 return NULL; 30 29 31 RC4_set_key(key, vlen, vbuf); 30 RC4_set_key(key, vbuf.len, vbuf.buf); 31 m2_PyBuffer_Release(value, &vbuf); 32 32 Py_INCREF(Py_None); 33 33 return Py_None; 34 34 } 35 35 36 36 PyObject *rc4_update(RC4_KEY *key, PyObject *in) { 37 37 PyObject *ret; 38 const void *buf;39 Py_ssize_t len;40 38 void *out; 39 Py_buffer buf; 41 40 42 if ( PyObject_AsReadBuffer(in, &buf, &len) == -1)41 if (m2_PyObject_GetBuffer(in, &buf, PyBUF_SIMPLE) == -1) 43 42 return NULL; 44 43 45 if (!(out = PyMem_Malloc( len))) {44 if (!(out = PyMem_Malloc(buf.len))) { 46 45 PyErr_SetString(PyExc_MemoryError, "expected a string object"); 46 m2_PyBuffer_Release(in, &buf); 47 47 return NULL; 48 48 } 49 RC4(key, len,buf, out);50 ret = PyString_FromStringAndSize(out, len);49 RC4(key, buf.len, buf.buf, out); 50 ret = PyString_FromStringAndSize(out, buf.len); 51 51 PyMem_Free(out); 52 m2_PyBuffer_Release(in, &buf); 52 53 return ret; 53 54 } 54 55