Ticket #43240: m2crypto-fixes.diff

File m2crypto-fixes.diff, 50.4 KB (added by jul_bsd@…, 11 years ago)
  • SWIG/_evp.i

     
    169169                                 int iter,
    170170                                 int keylen) {
    171171    unsigned char key[EVP_MAX_KEY_LENGTH];
    172     unsigned char *saltbuf;
    173     char *passbuf;
    174172    PyObject *ret;
    175     int passlen, saltlen;
     173    Py_buffer passbuf, saltbuf;
    176174
    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);
    179179        return NULL;
    180     if (m2_PyObject_AsReadBufferInt(salt, (const void **)&saltbuf,
    181                                     &saltlen) == -1)
    182         return NULL;
     180    }
    183181
    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);
    186185    ret = PyString_FromStringAndSize((char*)key, keylen);
    187186    OPENSSL_cleanse(key, keylen);
     187    m2_PyBuffer_Release(pass, &passbuf);
     188    m2_PyBuffer_Release(salt, &saltbuf);
    188189    return ret;
    189190}
    190191
     
    202203}
    203204
    204205int digest_update(EVP_MD_CTX *ctx, PyObject *blob) {
    205     const void *buf;
    206     Py_ssize_t len;
     206    Py_buffer buf;
     207    int ret;
    207208
    208     if (PyObject_AsReadBuffer(blob, &buf, &len) == -1)
    209         return -1;
     209    if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1)
     210      return -1;
    210211
    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;
    212215}
    213216
    214217PyObject *digest_final(EVP_MD_CTX *ctx) {
     
    247250}
    248251
    249252PyObject *hmac_init(HMAC_CTX *ctx, PyObject *key, const EVP_MD *md) {
    250     const void *kbuf;
    251     int klen;
     253    Py_buffer kbuf;
    252254
    253     if (m2_PyObject_AsReadBufferInt(key, &kbuf, &klen) == -1)
    254         return NULL;
     255    if (m2_PyObject_GetBufferInt(key, &kbuf, PyBUF_SIMPLE) == -1)
     256      return NULL;
    255257
    256     HMAC_Init(ctx, kbuf, klen, md);
     258    HMAC_Init(ctx, kbuf.buf, kbuf.len, md);
     259    m2_PyBuffer_Release(key, &kbuf);
    257260    Py_INCREF(Py_None);
    258261    return Py_None;
    259262}
    260263
    261264PyObject *hmac_update(HMAC_CTX *ctx, PyObject *blob) {
    262     const void *buf;
    263     Py_ssize_t len;
     265    Py_buffer buf;
    264266
    265     if (PyObject_AsReadBuffer(blob, &buf, &len) == -1)
    266         return NULL;
     267    if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1)
     268      return NULL;
    267269
    268     HMAC_Update(ctx, buf, len);
     270    HMAC_Update(ctx, buf.buf, buf.len);
     271    m2_PyBuffer_Release(blob, &buf);
    269272    Py_INCREF(Py_None);
    270273    return Py_None;
    271274}
     
    286289}
    287290
    288291PyObject *hmac(PyObject *key, PyObject *data, const EVP_MD *md) {
    289     const void *kbuf, *dbuf;
    290292    void *blob;
    291     int klen;
    292293    unsigned int blen;
    293     Py_ssize_t dlen;
    294294    PyObject *ret;
     295    Py_buffer kbuf, dbuf;
    295296
    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)
    298298        return NULL;
     299    if (m2_PyObject_GetBuffer(data, &dbuf, PyBUF_SIMPLE) == -1) {
     300        m2_PyBuffer_Release(key, &kbuf);
     301        return NULL;
     302    }
    299303
    300304    if (!(blob = PyMem_Malloc(EVP_MAX_MD_SIZE))) {
    301305        PyErr_SetString(PyExc_MemoryError, "hmac");
     306        m2_PyBuffer_Release(key, &kbuf);
     307        m2_PyBuffer_Release(data, &dbuf);
    302308        return NULL;
    303309    }
    304     HMAC(md, kbuf, klen, dbuf, dlen, blob, &blen);
     310    HMAC(md, kbuf.buf, kbuf.len, dbuf.buf, dbuf.len, blob, &blen);
    305311    blob = PyMem_Realloc(blob, blen);
    306312    ret = PyString_FromStringAndSize(blob, blen);
    307313    PyMem_Free(blob);
     314    m2_PyBuffer_Release(key, &kbuf);
     315    m2_PyBuffer_Release(data, &dbuf);
    308316    return ret;
    309317}
    310318
     
    329337                        PyObject *iv, /* Not used */
    330338                        int iter) {
    331339    unsigned char key[EVP_MAX_KEY_LENGTH];
    332     const void *dbuf, *sbuf;
    333     int dlen, klen;
    334     Py_ssize_t slen;
     340    int klen;
    335341    PyObject *ret;
     342    Py_buffer dbuf, sbuf;
    336343
    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)
    339345        return NULL;
     346    if (m2_PyObject_GetBuffer(salt, &sbuf, PyBUF_SIMPLE) == -1) {
     347        m2_PyBuffer_Release(data, &dbuf);
     348        return NULL;
     349    }
    340350
    341     assert((slen == 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,
    344354        key, NULL); /* Since we are not returning IV no need to derive it */
    345355    ret = PyString_FromStringAndSize((char*)key, klen);
     356    m2_PyBuffer_Release(data, &dbuf);
     357    m2_PyBuffer_Release(salt, &sbuf);
    346358    return ret;
    347359}
    348360
    349361PyObject *cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
    350362                        PyObject *key, PyObject *iv, int mode) {
    351     const void *kbuf, *ibuf;
    352     Py_ssize_t klen, ilen;
     363    Py_buffer kbuf, ibuf;
    353364
    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)
    356366        return NULL;
     367    if (m2_PyObject_GetBuffer(iv, &ibuf, PyBUF_SIMPLE) == -1) {
     368        m2_PyBuffer_Release(key, &kbuf);
     369        return NULL;
     370    }
    357371
    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)) {
    360374        PyErr_SetString(_evp_err, ERR_reason_error_string(ERR_get_error()));
     375        m2_PyBuffer_Release(key, &kbuf);
     376        m2_PyBuffer_Release(iv, &ibuf);
    361377        return NULL;
    362378    }
    363379    Py_INCREF(Py_None);
     380    m2_PyBuffer_Release(key, &kbuf);
     381    m2_PyBuffer_Release(iv, &ibuf);
    364382    return Py_None;
    365383}
    366384
    367385PyObject *cipher_update(EVP_CIPHER_CTX *ctx, PyObject *blob) {
    368     const void *buf;
    369     int len, olen;
     386    int olen;
    370387    void *obuf;
    371388    PyObject *ret;
     389    Py_buffer buf;
    372390
    373     if (m2_PyObject_AsReadBufferInt(blob, &buf, &len) == -1)
     391    if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1)
    374392        return NULL;
    375393
    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))) {
    377395        PyErr_SetString(PyExc_MemoryError, "cipher_update");
     396        m2_PyBuffer_Release(blob, &buf);
    378397        return NULL;
    379398    }
    380     if (!EVP_CipherUpdate(ctx, obuf, &olen, (unsigned char *)buf, len)) {
     399    if (!EVP_CipherUpdate(ctx, obuf, &olen, (unsigned char *)buf.buf, buf.len)) {
    381400        PyMem_Free(obuf);
    382401        PyErr_SetString(_evp_err, ERR_reason_error_string(ERR_get_error()));
     402        m2_PyBuffer_Release(blob, &buf);
    383403        return NULL;
    384404    }
    385405    ret = PyString_FromStringAndSize(obuf, olen);
    386406    PyMem_Free(obuf);
     407    m2_PyBuffer_Release(blob, &buf);
    387408    return ret;
    388409}
    389410
     
    407428}
    408429
    409430PyObject *sign_update(EVP_MD_CTX *ctx, PyObject *blob) {
    410     const void *buf;
    411     Py_ssize_t len;
     431    Py_buffer buf;
    412432
    413     if (PyObject_AsReadBuffer(blob, &buf, &len) == -1)
     433    if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1)
    414434        return NULL;
    415435
    416     if (!EVP_SignUpdate(ctx, buf, len)) {
     436    if (!EVP_SignUpdate(ctx, buf.buf, buf.len)) {
    417437        PyErr_SetString(_evp_err, ERR_reason_error_string(ERR_get_error()));
     438        m2_PyBuffer_Release(blob, &buf);
    418439        return NULL;
    419440    }
    420441    Py_INCREF(Py_None);
     442    m2_PyBuffer_Release(blob, &buf);
    421443    return Py_None;
    422444}
    423445
     
    445467}
    446468
    447469int verify_update(EVP_MD_CTX *ctx, PyObject *blob) {
    448     const void *buf;
    449     Py_ssize_t len;
     470    Py_buffer buf;
     471    int ret;
    450472
    451     if (PyObject_AsReadBuffer(blob, &buf, &len) == -1)
     473    if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1)
    452474        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;
    455478}
    456479
    457480
    458481int 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;
    461484
    462     if (m2_PyObject_AsReadBufferInt(blob, (const void **)&kbuf, &len) == -1)
     485    if (m2_PyObject_GetBufferInt(blob, &kbuf, PyBUF_SIMPLE) == -1)
    463486        return -1;
    464487
    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;
    466491}
    467492%}
    468493
  • SWIG/_dh.i

     
    8383}
    8484
    8585PyObject *dh_compute_key(DH *dh, PyObject *pubkey) {
    86     const void *pkbuf;
    87     int pklen, klen;
     86    Py_buffer pkbuf;
     87    int klen;
    8888    void *key;
    8989    BIGNUM *pk;
    9090    PyObject *ret;
    9191
    92     if (m2_PyObject_AsReadBufferInt(pubkey, &pkbuf, &pklen) == -1)
    93         return NULL;
     92    if (m2_PyObject_GetBufferInt(pubkey, &pkbuf, PyBUF_SIMPLE) == -1)
     93      return NULL;
    9494
    95     if (!(pk = BN_mpi2bn((unsigned char *)pkbuf, pklen, NULL))) {
     95    if (!(pk = BN_mpi2bn((unsigned char *)pkbuf.buf, pkbuf.len, NULL))) {
    9696        PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error()));
     97        m2_PyBuffer_Release(pubkey, &pkbuf);
    9798        return NULL;
    9899    }
    99100    if (!(key = PyMem_Malloc(DH_size(dh)))) {
    100101        BN_free(pk);
    101102        PyErr_SetString(PyExc_MemoryError, "dh_compute_key");
     103        m2_PyBuffer_Release(pubkey, &pkbuf);
    102104        return NULL;
    103105    }
    104106    if ((klen = DH_compute_key((unsigned char *)key, pk, dh)) == -1) {
    105107        BN_free(pk);
    106108        PyMem_Free(key);
    107109        PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error()));
     110        m2_PyBuffer_Release(pubkey, &pkbuf);
    108111        return NULL;
    109112    }
    110113    ret = PyString_FromStringAndSize((const char *)key, klen);
    111114    BN_free(pk);
    112115    PyMem_Free(key);
     116    m2_PyBuffer_Release(pubkey, &pkbuf);
    113117    return ret;
    114118}
    115119       
     
    147151
    148152PyObject *dh_set_p(DH *dh, PyObject *value) {
    149153    BIGNUM *bn;
    150     const void *vbuf;
    151     int vlen;
     154    Py_buffer vbuf;
    152155
    153     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    154         return NULL;
     156    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
     157      return NULL;
    155158
    156     if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
     159    if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) {
    157160        PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error()));
     161        m2_PyBuffer_Release(value, &vbuf);
    158162        return NULL;
    159163    }
    160164    if (dh->p)
    161165        BN_free(dh->p);
    162166    dh->p = bn;
     167    m2_PyBuffer_Release(value, &vbuf);
    163168    Py_INCREF(Py_None);
    164169    return Py_None;
    165170}
    166171
    167172PyObject *dh_set_g(DH *dh, PyObject *value) {
    168173    BIGNUM *bn;
    169     const void *vbuf;
    170     int vlen;
     174    Py_buffer vbuf;
    171175
    172     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    173         return NULL;
     176    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
     177      return NULL;
    174178
    175     if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
     179    if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) {
    176180        PyErr_SetString(_dh_err, ERR_reason_error_string(ERR_get_error()));
     181        m2_PyBuffer_Release(value, &vbuf);
    177182        return NULL;
    178183    }
    179184    if (dh->g)
    180185        BN_free(dh->g);
    181186    dh->g = bn;
    182187    Py_INCREF(Py_None);
     188    m2_PyBuffer_Release(value, &vbuf);
    183189    return Py_None;
    184190}
    185191%}
  • SWIG/_ssl.i

     
    315315}
    316316
    317317int 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;
    320320
    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;
    325326}
    326327
    327328void ssl_ctx_set_info_callback(SSL_CTX *ctx, PyObject *pyfunc) {
     
    384385}
    385386
    386387int ssl_set_session_id_context(SSL *ssl, PyObject *sid_ctx) {
    387     const void *buf;
    388     int len;
     388    Py_buffer buf;
     389    int ret;
    389390
    390     if (m2_PyObject_AsReadBufferInt(sid_ctx, &buf, &len) == -1)
    391         return -1;
     391    if (m2_PyObject_GetBufferInt(sid_ctx, &buf, PyBUF_SIMPLE) == -1)
     392      return -1;
    392393
    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;
    394397}
    395398
    396399int ssl_set_fd(SSL *ssl, int fd) {
     
    583586}
    584587
    585588int 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;
    588591
    589 
    590     if (m2_PyObject_AsReadBufferInt(blob, &buf, &len) == -1) {
     592    if (m2_PyObject_GetBufferInt(blob, &buf, PyBUF_SIMPLE) == -1)
    591593        return -1;
    592     }
    593594
    594    
    595595    Py_BEGIN_ALLOW_THREADS
    596     r = SSL_write(ssl, buf, len);
     596    r = SSL_write(ssl, buf.buf, buf.len);
    597597    Py_END_ALLOW_THREADS
    598598
    599599
     
    623623            ret = -1;
    624624    }
    625625   
    626    
     626    m2_PyBuffer_Release(blob, &buf);
    627627    return ret;
    628628}
    629629
    630630int 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;
    633633
    634 
    635     if (m2_PyObject_AsReadBufferInt(blob, &buf, &len) == -1) {
     634    if (m2_PyObject_GetBufferInt(blob, &buf, PyBUF_SIMPLE) == -1)
    636635        return -1;
    637     }
    638 
    639636   
    640637    Py_BEGIN_ALLOW_THREADS
    641     r = SSL_write(ssl, buf, len);
     638    r = SSL_write(ssl, buf.buf, buf.len);
    642639    Py_END_ALLOW_THREADS
    643640   
    644641   
     
    667664            ret = -1;
    668665    }
    669666   
    670    
     667    m2_PyBuffer_Release(blob, &buf);
    671668    return ret;
    672669}
    673670
  • SWIG/_lib.h

     
    11/* Copyright (c) 1999 Ng Pheng Siong. All rights reserved. */
    22/* $Id$ */
    33
     4#if PY_VERSION_HEX < 0x02060000
     5typedef 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
    413#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
    514typedef int Py_ssize_t;
    615#define PY_SSIZE_T_MAX INT_MAX
     
    817#endif
    918
    1019typedef struct _blob {
    11         unsigned char *data;
    12         int len;
     20  unsigned char *data;
     21  int len;
    1322} Blob;
    1423
    1524Blob *blob_new(int len, const char *errmsg);
    1625Blob *blob_copy(Blob *from, const char *errmsg);
    1726void blob_free(Blob *blob);
    1827
     28/* Always use these together, to correctly handle non-memoryview objects. */
     29static int m2_PyObject_GetBufferInt(PyObject *obj, Py_buffer *view, int flags);
     30static int m2_PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags);
     31static void m2_PyBuffer_Release(PyObject *obj, Py_buffer *view);
     32
    1933static int m2_PyObject_AsReadBufferInt(PyObject *obj, const void **buffer,
    2034                                       int *buffer_len);
    2135static int m2_PyString_AsStringAndSizeInt(PyObject *obj, char **s, int *len);
     
    2438int passphrase_callback(char *buf, int num, int v, void *userdata);
    2539
    2640void lib_init(void);
    27 
  • SWIG/_lib.i

     
    4747/* Python helpers. */
    4848
    4949%}
     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;
    5056%ignore m2_PyObject_AsReadBufferInt;
    5157%ignore m2_PyString_AsStringAndSizeInt;
    5258%{
     59
     60
     61#if PY_VERSION_HEX < 0x02060000
     62static int PyObject_CheckBuffer(PyObject *obj) {
     63    (void)obj;
     64    return 0;
     65}
     66
     67static int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {
     68    (void)obj;
     69    (void)view;
     70    (void)flags;
     71    return -1;
     72}
     73
     74static void PyBuffer_Release(Py_buffer *view) {
     75    (void)view;
     76}
     77#endif /* PY_VERSION_HEX < 0x02060000 */
     78
     79
     80static 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
     86static 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
     103static 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
    53117static int
    54118m2_PyObject_AsReadBufferInt(PyObject *obj, const void **buffer,
    55119                int *buffer_len)
     
    73137{
    74138    int ret;
    75139    Py_ssize_t len2;
    76 
    77140    ret = PyString_AsStringAndSize(obj, s, &len2);
    78141    if (ret)
    79142       return ret;
     
    318381}
    319382
    320383BIGNUM *mpi_to_bn(PyObject *value) {
    321     const void *vbuf;
    322     int vlen;
     384    Py_buffer vbuf;
     385    BIGNUM *ret;
    323386
    324     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    325         return NULL;
     387    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
     388      return NULL;
    326389
    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;
    328393}
    329394
    330395PyObject *bn_to_bin(BIGNUM *bn) {
     
    344409}
    345410
    346411BIGNUM *bin_to_bn(PyObject *value) {
    347     const void *vbuf;
    348     int vlen;
     412    Py_buffer vbuf;
     413    BIGNUM *ret;
    349414
    350     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    351         return NULL;
     415    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
     416      return NULL;
    352417
    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;
    354421}
    355422
    356423PyObject *bn_to_hex(BIGNUM *bn) {
     
    372439}
    373440
    374441BIGNUM *hex_to_bn(PyObject *value) {
    375     const void *vbuf;
    376     Py_ssize_t vlen;
     442    Py_buffer vbuf;
    377443    BIGNUM *bn;
    378444
    379     if (PyObject_AsReadBuffer(value, &vbuf, &vlen) == -1)
    380         return NULL;
     445    if (m2_PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) == -1)
     446      return NULL;
    381447
    382448    if ((bn=BN_new())==NULL) {
    383449        PyErr_SetString(PyExc_MemoryError, "hex_to_bn");
     450        m2_PyBuffer_Release(value, &vbuf);
    384451        return NULL;
    385452    }
    386     if (BN_hex2bn(&bn, (const char *)vbuf) <= 0) {
     453    if (BN_hex2bn(&bn, (const char *)vbuf.buf) <= 0) {
    387454        PyErr_SetString(PyExc_RuntimeError,
    388455              ERR_error_string(ERR_get_error(), NULL));
    389456        BN_free(bn);
     457        m2_PyBuffer_Release(value, &vbuf);
    390458        return NULL;
    391459    }
     460    m2_PyBuffer_Release(value, &vbuf);
    392461    return bn;
    393462}
    394463
    395464BIGNUM *dec_to_bn(PyObject *value) {
    396     const void *vbuf;
    397     Py_ssize_t vlen;
     465    Py_buffer vbuf;
    398466    BIGNUM *bn;
    399467
    400     if (PyObject_AsReadBuffer(value, &vbuf, &vlen) == -1)
    401         return NULL;
     468    if (m2_PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) == -1)
     469      return NULL;
    402470
    403471    if ((bn=BN_new())==NULL) {
    404472      PyErr_SetString(PyExc_MemoryError, "dec_to_bn");
     473      m2_PyBuffer_Release(value, &vbuf);
    405474      return NULL;
    406475    }
    407     if ((BN_dec2bn(&bn, (const char *)vbuf) <= 0)) {
     476    if ((BN_dec2bn(&bn, (const char *)vbuf.buf) <= 0)) {
    408477      PyErr_SetString(PyExc_RuntimeError,
    409478            ERR_error_string(ERR_get_error(), NULL));
    410479      BN_free(bn);
     480      m2_PyBuffer_Release(value, &vbuf);
    411481      return NULL;
    412482    }
     483    m2_PyBuffer_Release(value, &vbuf);
    413484    return bn;
    414485}
    415486%}
  • SWIG/_dsa.i

     
    9999
    100100PyObject *dsa_set_p(DSA *dsa, PyObject *value) {
    101101    BIGNUM *bn;
    102     const void *vbuf;
    103     int vlen;
     102    Py_buffer vbuf;
    104103
    105     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    106         return NULL;
     104    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
     105      return NULL;
    107106
    108     if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
     107    if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) {
    109108        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
     109        m2_PyBuffer_Release(value, &vbuf);
    110110        return NULL;
    111111    }
    112112    if (dsa->p)
    113113        BN_free(dsa->p);
    114114    dsa->p = bn;
     115    m2_PyBuffer_Release(value, &vbuf);
    115116    Py_INCREF(Py_None);
    116117    return Py_None;
    117118}
    118119
    119120PyObject *dsa_set_q(DSA *dsa, PyObject *value) {
    120121    BIGNUM *bn;
    121     const void *vbuf;
    122     int vlen;
     122    Py_buffer vbuf;
    123123
    124     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    125         return NULL;
     124    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
     125      return NULL;
    126126
    127     if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
     127    if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) {
    128128        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
     129        m2_PyBuffer_Release(value, &vbuf);
    129130        return NULL;
    130131    }
    131132    if (dsa->q)
    132133        BN_free(dsa->q);
    133134    dsa->q = bn;
     135    m2_PyBuffer_Release(value, &vbuf);
    134136    Py_INCREF(Py_None);
    135137    return Py_None;
    136138}
    137139
    138140PyObject *dsa_set_g(DSA *dsa, PyObject *value) {
    139141    BIGNUM *bn;
    140     const void *vbuf;
    141     int vlen;
     142    Py_buffer vbuf;
    142143
    143     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
     144    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
    144145        return NULL;
    145146
    146     if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
     147    if (!(bn = BN_mpi2bn((unsigned char *)vbuf.buf, vbuf.len, NULL))) {
    147148        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
     149        m2_PyBuffer_Release(value, &vbuf);
    148150        return NULL;
    149151    }
    150152    if (dsa->g)
    151153        BN_free(dsa->g);
    152154    dsa->g = bn;
     155    m2_PyBuffer_Release(value, &vbuf);
    153156    Py_INCREF(Py_None);
    154157    return Py_None;
    155158}
     
    236239}
    237240
    238241PyObject *dsa_sign(DSA *dsa, PyObject *value) {
    239     const void *vbuf;
    240     int vlen;
     242    Py_buffer vbuf;
    241243    PyObject *tuple;
    242244    DSA_SIG *sig;
    243245
    244     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
     246    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
    245247        return NULL;
    246248
    247     if (!(sig = DSA_do_sign(vbuf, vlen, dsa))) {
     249    if (!(sig = DSA_do_sign(vbuf.buf, vbuf.len, dsa))) {
    248250        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
     251        m2_PyBuffer_Release(value, &vbuf);
    249252        return NULL;
    250253    }
    251254    if (!(tuple = PyTuple_New(2))) {
    252255        DSA_SIG_free(sig);
    253256        PyErr_SetString(PyExc_RuntimeError, "PyTuple_New() fails");
     257        m2_PyBuffer_Release(value, &vbuf);
    254258        return NULL;
    255259    }
    256260    PyTuple_SET_ITEM(tuple, 0, dsa_sig_get_r(sig));
    257261    PyTuple_SET_ITEM(tuple, 1, dsa_sig_get_s(sig));
    258262    DSA_SIG_free(sig);
     263    m2_PyBuffer_Release(value, &vbuf);
    259264    return tuple;
    260265}
    261266
    262267int 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;
    265269    DSA_SIG *sig;
    266270    int ret;
    267271
    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)
    271273        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    }
    272283
    273284    if (!(sig = DSA_SIG_new())) {
    274285        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);
    275289        return -1;
    276290    }
    277     if (!(sig->r = BN_mpi2bn((unsigned char *)rbuf, rlen, NULL))) {
     291    if (!(sig->r = BN_mpi2bn((unsigned char *)rbuf.buf, rbuf.len, NULL))) {
    278292        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
    279293        DSA_SIG_free(sig);
     294        m2_PyBuffer_Release(value, &vbuf);
     295        m2_PyBuffer_Release(r, &rbuf);
     296        m2_PyBuffer_Release(s, &sbuf);
    280297        return -1;
    281298    }
    282     if (!(sig->s = BN_mpi2bn((unsigned char *)sbuf, slen, NULL))) {
     299    if (!(sig->s = BN_mpi2bn((unsigned char *)sbuf.buf, sbuf.len, NULL))) {
    283300        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
    284301        DSA_SIG_free(sig);
     302        m2_PyBuffer_Release(value, &vbuf);
     303        m2_PyBuffer_Release(r, &rbuf);
     304        m2_PyBuffer_Release(s, &sbuf);
    285305        return -1;
    286306    }
    287     ret = DSA_do_verify(vbuf, vlen, sig, dsa);
     307    ret = DSA_do_verify(vbuf.buf, vbuf.len, sig, dsa);
    288308    DSA_SIG_free(sig);
    289309    if (ret == -1)
    290310        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);
    291314    return ret;
    292315}
    293316
    294317PyObject *dsa_sign_asn1(DSA *dsa, PyObject *value) {
    295     const void *vbuf;
    296     int vlen;
     318    Py_buffer vbuf;
    297319    void *sigbuf;
    298320    unsigned int siglen;
    299321    PyObject *ret;
    300322
    301     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
     323    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
    302324        return NULL;
    303325
    304326    if (!(sigbuf = PyMem_Malloc(DSA_size(dsa)))) {
    305327        PyErr_SetString(PyExc_MemoryError, "dsa_sign_asn1");
     328        m2_PyBuffer_Release(value, &vbuf);
    306329        return NULL;
    307330    }
    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)) {
    309333        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
    310334        PyMem_Free(sigbuf);
     335        m2_PyBuffer_Release(value, &vbuf);
    311336        return NULL;
    312337    }
    313338    ret = PyString_FromStringAndSize(sigbuf, siglen);
    314339    PyMem_Free(sigbuf);
     340    m2_PyBuffer_Release(value, &vbuf);
    315341    return ret;
    316342}
    317343
    318344int 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;
    322347
    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    }
    327354
    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)
    329357        PyErr_SetString(_dsa_err, ERR_reason_error_string(ERR_get_error()));
     358    m2_PyBuffer_Release(value, &vbuf);
     359    m2_PyBuffer_Release(sig, &sbuf);
    330360    return ret;
    331361}
    332362
  • SWIG/_aes.i

     
    4242// otherwise: encrypt (Python code will supply the value 1.)
    4343*/
    4444PyObject *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;
    4746
    48     if (PyObject_AsReadBuffer(value, &vbuf, &vlen) == -1)
    49         return NULL;
     47    if (m2_PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) == -1)
     48      return NULL;
    5049
    5150    if (op == 0)
    52         AES_set_encrypt_key(vbuf, bits, key);
     51        AES_set_encrypt_key(vbuf.buf, bits, key);
    5352    else
    54         AES_set_decrypt_key(vbuf, bits, key);
     53        AES_set_decrypt_key(vbuf.buf, bits, key);
     54    m2_PyBuffer_Release(value, &vbuf);
    5555    Py_INCREF(Py_None);
    5656    return Py_None;
    5757}
     
    6161// otherwise: encrypt (Python code will supply the value 1.)
    6262*/
    6363PyObject *AES_crypt(const AES_KEY *key, PyObject *in, int outlen, int op) {
    64     const void *buf;
    65     Py_ssize_t len;
    6664    unsigned char *out;
     65    Py_buffer buf;
    6766
    68     if (PyObject_AsReadBuffer(in, &buf, &len) == -1)
    69         return NULL;
     67    if (m2_PyObject_GetBuffer(in, &buf, PyBUF_SIMPLE) == -1)
     68      return NULL;
    7069
    7170    if (!(out=(unsigned char *)PyMem_Malloc(outlen))) {
    7271        PyErr_SetString(PyExc_MemoryError, "AES_crypt");
     72        m2_PyBuffer_Release(in, &buf);
    7373        return NULL;
    7474    }
     75
    7576    if (op == 0)
    76         AES_encrypt((const unsigned char *)in, out, key);
     77        AES_encrypt((const unsigned char *)buf.buf, out, key);
    7778    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);
    7981    return PyString_FromStringAndSize((char*)out, outlen);
    8082}
    8183
  • SWIG/_bio.i

     
    121121}
    122122
    123123int bio_write(BIO *bio, PyObject *from) {
    124     const void *fbuf;
    125     int flen, ret;
     124    Py_buffer fbuf;
     125    int ret;
    126126
    127     if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
    128         return -1;
     127    if (m2_PyObject_GetBufferInt(from, &fbuf, PyBUF_SIMPLE) == -1)
     128      return -1;
    129129
    130130    Py_BEGIN_ALLOW_THREADS
    131     ret = BIO_write(bio, fbuf, flen);
     131    ret = BIO_write(bio, fbuf.buf, fbuf.len);
    132132    Py_END_ALLOW_THREADS
    133133    if (ret < 0) {
    134134        if (ERR_peek_error()) {
    135135            PyErr_SetString(_bio_err, ERR_reason_error_string(ERR_get_error()));
    136136        }
    137137    }
     138    m2_PyBuffer_Release(from, &fbuf);
    138139    return ret;
    139140}
    140141
     
    175176}
    176177
    177178PyObject *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;
    180180
    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;
    184183
     184    if (m2_PyObject_GetBuffer(iv, &ibuf, PyBUF_SIMPLE) == -1) {
     185      m2_PyBuffer_Release(key, &kbuf);
     186      return NULL;
     187    }
     188
    185189    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);
    187193    Py_INCREF(Py_None);
    188194    return Py_None;
    189195}
  • SWIG/_util.i

     
    1717   
    1818PyObject *util_hex_to_string(PyObject *blob) {
    1919    PyObject *obj;
    20     const void *buf;
    2120    char *ret;
    22     Py_ssize_t len;
     21    Py_buffer buf;
    2322
    24     if (PyObject_AsReadBuffer(blob, &buf, &len) == -1)
    25         return NULL;
     23    if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1)
     24      return NULL;
    2625
    27     ret = hex_to_string((unsigned char *)buf, len);
     26    ret = hex_to_string((unsigned char *)buf.buf, buf.len);
    2827    if (!ret) {
    2928        PyErr_SetString(_util_err, ERR_reason_error_string(ERR_get_error()));
     29        m2_PyBuffer_Release(blob, &buf);
    3030        return NULL;
    3131    }
    3232    obj = PyString_FromString(ret);
    3333    OPENSSL_free(ret);
     34    m2_PyBuffer_Release(blob, &buf);
    3435    return obj;
    3536}
    3637
    3738PyObject *util_string_to_hex(PyObject *blob) {
    3839    PyObject *obj;
    39     const void *buf;
    4040    unsigned char *ret;
    41     Py_ssize_t len0;
    4241    long len;
     42    Py_buffer buf;
    4343
    44     if (PyObject_AsReadBuffer(blob, &buf, &len0) == -1)
    45         return NULL;
     44    if (m2_PyObject_GetBuffer(blob, &buf, PyBUF_SIMPLE) == -1)
     45      return NULL;
    4646
    47     len = len0;
    48     ret = string_to_hex((char *)buf, &len);
     47    len = buf.len;
     48    ret = string_to_hex((char *)buf.buf, &len);
    4949    if (ret == NULL) {
    5050        PyErr_SetString(_util_err, ERR_reason_error_string(ERR_get_error()));
     51        m2_PyBuffer_Release(blob, &buf);
    5152        return NULL;
    5253    }
    5354    obj = PyString_FromStringAndSize((char*)ret, len);
    5455    OPENSSL_free(ret);
     56    m2_PyBuffer_Release(blob, &buf);
    5557    return obj;
    5658}
    5759%}
  • SWIG/_rand.i

     
    2626}
    2727
    2828PyObject *rand_seed(PyObject *seed) {
    29     const void *buf;
    30     int len;
     29    Py_buffer buf;
    3130
    32     if (m2_PyObject_AsReadBufferInt(seed, &buf, &len) == -1)
     31    if (m2_PyObject_GetBufferInt(seed, &buf, PyBUF_SIMPLE) == -1)
    3332        return NULL;
    3433
    35     RAND_seed(buf, len);
     34    RAND_seed(buf.buf, buf.len);
     35    m2_PyBuffer_Release(seed, &buf);
    3636    Py_INCREF(Py_None);
    3737    return Py_None;
    3838}
    3939
    4040PyObject *rand_add(PyObject *blob, double entropy) {
    41     const void *buf;
    42     int len;
     41    Py_buffer buf;
    4342
    44     if (m2_PyObject_AsReadBufferInt(blob, &buf, &len) == -1)
     43    if (m2_PyObject_GetBufferInt(blob, &buf, PyBUF_SIMPLE) == -1)
    4544        return NULL;
    4645
    47     RAND_add(buf, len, entropy);
     46    RAND_add(buf.buf, buf.len, entropy);
     47    m2_PyBuffer_Release(blob, &buf);
    4848    Py_INCREF(Py_None);
    4949    return Py_None;
    5050}
  • SWIG/_rsa.i

     
    120120
    121121PyObject *rsa_set_e(RSA *rsa, PyObject *value) {
    122122    BIGNUM *bn;
    123     const void *vbuf;
    124     int vlen;
     123    Py_buffer pybuf;
    125124
    126     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    127         return NULL;
     125    if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1)
     126      return NULL;
    128127
    129     if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
     128    if (!(bn = BN_mpi2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) {
    130129        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
     130        m2_PyBuffer_Release(value, &pybuf);
    131131        return NULL;
    132132    }
    133133    if (rsa->e)
    134134        BN_free(rsa->e);
     135    m2_PyBuffer_Release(value, &pybuf);
    135136    rsa->e = bn;
    136137    Py_INCREF(Py_None);
    137138    return Py_None;
     
    139140
    140141PyObject *rsa_set_n(RSA *rsa, PyObject *value) {
    141142    BIGNUM *bn;
    142     const void *vbuf;
    143     int vlen;
     143    Py_buffer pybuf;
    144144
    145     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    146         return NULL;
     145    if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1)
     146      return NULL;
    147147
    148     if (!(bn = BN_mpi2bn((unsigned char *)vbuf, vlen, NULL))) {
     148    if (!(bn = BN_mpi2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) {
    149149        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
     150        m2_PyBuffer_Release(value, &pybuf);
    150151        return NULL;
    151152    }
    152153    if (rsa->n)
    153154        BN_free(rsa->n);
     155    m2_PyBuffer_Release(value, &pybuf);
    154156    rsa->n = bn;
    155157    Py_INCREF(Py_None);
    156158    return Py_None;
     
    158160
    159161PyObject *rsa_set_e_bin(RSA *rsa, PyObject *value) {
    160162    BIGNUM *bn;
    161     const void *vbuf;
    162     int vlen;
     163    Py_buffer pybuf;
    163164
    164     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    165         return NULL;
     165    if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1)
     166      return NULL;
    166167
    167     if (!(bn = BN_bin2bn((unsigned char *)vbuf, vlen, NULL))) {
     168    if (!(bn = BN_bin2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) {
    168169        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
     170        m2_PyBuffer_Release(value, &pybuf);
    169171        return NULL;
    170172    }
    171173    if (rsa->e)
    172174        BN_free(rsa->e);
     175    m2_PyBuffer_Release(value, &pybuf);
    173176    rsa->e = bn;
    174177    Py_INCREF(Py_None);
    175178    return Py_None;
     
    177180
    178181PyObject *rsa_set_n_bin(RSA *rsa, PyObject *value) {
    179182    BIGNUM *bn;
    180     const void *vbuf;
    181     int vlen;
     183    Py_buffer pybuf;
    182184
    183     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    184         return NULL;
     185    if (m2_PyObject_GetBufferInt(value, &pybuf, PyBUF_SIMPLE) == -1)
     186      return NULL;
    185187
    186     if (!(bn = BN_bin2bn((unsigned char *)vbuf, vlen, NULL))) {
     188    if (!(bn = BN_bin2bn((unsigned char *)pybuf.buf, pybuf.len, NULL))) {
    187189        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
     190        m2_PyBuffer_Release(value, &pybuf);
    188191        return NULL;
    189192    }
    190193    if (rsa->n)
    191194        BN_free(rsa->n);
     195    m2_PyBuffer_Release(value, &pybuf);
    192196    rsa->n = bn;
    193197    Py_INCREF(Py_None);
    194198    return Py_None;
    195199}
    196200
    197201PyObject *rsa_private_encrypt(RSA *rsa, PyObject *from, int padding) {
    198     const void *fbuf;
    199202    void *tbuf;
    200     int flen, tlen;
     203    int tlen;
    201204    PyObject *ret;
     205    Py_buffer pybuf;
    202206
    203     if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
    204         return NULL;
     207    if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1)
     208      return NULL;
    205209
    206210    if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
    207211        PyErr_SetString(PyExc_MemoryError, "rsa_private_encrypt");
     212        m2_PyBuffer_Release(from, &pybuf);
    208213        return NULL;
    209214    }
    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);
    212217    if (tlen == -1) {
    213218        PyMem_Free(tbuf);
     219        m2_PyBuffer_Release(from, &pybuf);
    214220        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
    215221        return NULL;
    216222    }
    217223    ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
    218224    PyMem_Free(tbuf);
     225    m2_PyBuffer_Release(from, &pybuf);
    219226    return ret;
    220227}
    221228
    222229PyObject *rsa_public_decrypt(RSA *rsa, PyObject *from, int padding) {
    223     const void *fbuf;
    224230    void *tbuf;
    225     int flen, tlen;
     231    int tlen;
    226232    PyObject *ret;
     233    Py_buffer pybuf;
    227234
    228     if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
     235    if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1)
    229236        return NULL;
    230237
    231238    if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
    232239        PyErr_SetString(PyExc_MemoryError, "rsa_public_decrypt");
     240        m2_PyBuffer_Release(from, &pybuf);
    233241        return NULL;
    234242    }
    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);
    237245    if (tlen == -1) {
    238246        PyMem_Free(tbuf);
     247        m2_PyBuffer_Release(from, &pybuf);
    239248        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
    240249        return NULL;
    241250    }
    242251    ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
    243252    PyMem_Free(tbuf);
     253    m2_PyBuffer_Release(from, &pybuf);
    244254    return ret;
    245255}
    246256
    247257PyObject *rsa_public_encrypt(RSA *rsa, PyObject *from, int padding) {
    248     const void *fbuf;
    249258    void *tbuf;
    250     int flen, tlen;
     259    int tlen;
    251260    PyObject *ret;
     261    Py_buffer pybuf;
    252262
    253     if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
     263    if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1)
    254264        return NULL;
    255265
    256266    if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
    257267        PyErr_SetString(PyExc_MemoryError, "rsa_public_encrypt");
     268        m2_PyBuffer_Release(from, &pybuf);
    258269        return NULL;
    259270    }
    260     tlen = RSA_public_encrypt(flen, (unsigned char *)fbuf,
     271    tlen = RSA_public_encrypt(pybuf.len, (unsigned char *)pybuf.buf,
    261272        (unsigned char *)tbuf, rsa, padding);
    262273    if (tlen == -1) {
    263274        PyMem_Free(tbuf);
     275        m2_PyBuffer_Release(from, &pybuf);
    264276        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
    265277        return NULL;
    266278    }
    267279    ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
    268280    PyMem_Free(tbuf);
     281    m2_PyBuffer_Release(from, &pybuf);
    269282    return ret;
    270283}
    271284
    272285PyObject *rsa_private_decrypt(RSA *rsa, PyObject *from, int padding) {
    273     const void *fbuf;
    274286    void *tbuf;
    275     int flen, tlen;
     287    int tlen;
    276288    PyObject *ret;
     289    Py_buffer pybuf;
    277290
    278     if (m2_PyObject_AsReadBufferInt(from, &fbuf, &flen) == -1)
     291    if (m2_PyObject_GetBufferInt(from, &pybuf, PyBUF_SIMPLE) == -1)
    279292        return NULL;
    280293
    281294    if (!(tbuf = PyMem_Malloc(BN_num_bytes(rsa->n)))) {
    282295        PyErr_SetString(PyExc_MemoryError, "rsa_private_decrypt");
     296        m2_PyBuffer_Release(from, &pybuf);
    283297        return NULL;
    284298    }
    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
    287302    if (tlen == -1) {
    288303        PyMem_Free(tbuf);
     304        m2_PyBuffer_Release(from, &pybuf);
    289305        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
    290306        return NULL;
    291307    }
    292308    ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
    293309    PyMem_Free(tbuf);
     310    m2_PyBuffer_Release(from, &pybuf);
    294311    return ret;
    295312}
    296313
    297314#if OPENSSL_VERSION_NUMBER >= 0x0090708fL
    298315PyObject *rsa_padding_add_pkcs1_pss(RSA *rsa, PyObject *digest, EVP_MD *hash, int salt_length) {
    299     const void *dbuf;
    300316    unsigned char *tbuf;
    301     int dlen, result, tlen;
     317    int result, tlen;
    302318    PyObject *ret;
     319    Py_buffer dbuf;
    303320
    304     if (m2_PyObject_AsReadBufferInt(digest, &dbuf, &dlen) == -1)
     321    if (m2_PyObject_GetBufferInt(digest, &dbuf, PyBUF_SIMPLE) == -1)
    305322        return NULL;
    306323
    307324    tlen = RSA_size(rsa);
    308325
    309326    if (!(tbuf = OPENSSL_malloc(tlen))) {
    310327        PyErr_SetString(PyExc_MemoryError, "rsa_padding_add_pkcs1_pss");
     328        m2_PyBuffer_Release(digest, &dbuf);
    311329        return NULL;
    312330    }
    313331    result = RSA_padding_add_PKCS1_PSS(
    314332        rsa,
    315333        tbuf,
    316         (unsigned char *)dbuf,
     334        (unsigned char *)dbuf.buf,
    317335        hash,
    318336        salt_length);
    319337
    320338    if (result == -1) {
    321339        OPENSSL_cleanse(tbuf, tlen);
    322340        OPENSSL_free(tbuf);
     341        m2_PyBuffer_Release(digest, &dbuf);
    323342        PyErr_SetString(_rsa_err, ERR_reason_error_string(ERR_get_error()));
    324343        return NULL;
    325344    }
    326345    ret = PyString_FromStringAndSize((const char *)tbuf, tlen);
    327346    OPENSSL_cleanse(tbuf, tlen);
    328347    OPENSSL_free(tbuf);
     348    m2_PyBuffer_Release(digest, &dbuf);
    329349    return ret;
    330350}
    331351
    332352int 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;
    336355
    337     if (m2_PyObject_AsReadBufferInt(digest, &dbuf, &dlen) == -1) {
     356    if (m2_PyObject_GetBufferInt(digest, &dbuf, PyBUF_SIMPLE) == -1) {
    338357        return 0;
    339358    }
    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);
    342361        return 0;
    343362    }
    344363
    345364    ret = RSA_verify_PKCS1_PSS(
    346365        rsa,
    347         (unsigned char *)dbuf,
     366        (unsigned char *)dbuf.buf,
    348367        hash,
    349         (unsigned char *)sbuf,
     368        (unsigned char *)sbuf.buf,
    350369        salt_length);
    351370
     371    m2_PyBuffer_Release(digest, &dbuf);
     372    m2_PyBuffer_Release(signature, &sbuf);
    352373    return ret;
    353374}
    354375#endif
  • SWIG/_ec.i

     
    255255}
    256256
    257257PyObject *ecdsa_sign(EC_KEY *key, PyObject *value) {
    258     const void *vbuf;
    259     int vlen;
    260258    PyObject *tuple;
    261259    ECDSA_SIG *sig;
     260    Py_buffer vbuf;
    262261
    263     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
    264         return NULL;
     262    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
     263      return NULL;
    265264
    266     if (!(sig = ECDSA_do_sign(vbuf, vlen, key))) {
     265    if (!(sig = ECDSA_do_sign(vbuf.buf, vbuf.len, key))) {
    267266        PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error()));
     267        m2_PyBuffer_Release(value, &vbuf);
    268268        return NULL;
    269269    }
    270270    if (!(tuple = PyTuple_New(2))) {
    271271        ECDSA_SIG_free(sig);
    272272        PyErr_SetString(PyExc_RuntimeError, "PyTuple_New() fails");
     273        m2_PyBuffer_Release(value, &vbuf);
    273274        return NULL;
    274275    }
    275276    PyTuple_SET_ITEM(tuple, 0, ecdsa_sig_get_r(sig));
    276277    PyTuple_SET_ITEM(tuple, 1, ecdsa_sig_get_s(sig));
    277278    ECDSA_SIG_free(sig);
     279    m2_PyBuffer_Release(value, &vbuf);
    278280    return tuple;
    279281}
    280282
    281283int 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;
    284285    ECDSA_SIG *sig;
    285286    int ret;
    286287
    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    }
    291299
    292300    if (!(sig = ECDSA_SIG_new())) {
    293301        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);
    294305        return -1;
    295306    }
    296     if (!BN_mpi2bn((unsigned char *)rbuf, rlen, sig->r)) {
     307    if (!BN_mpi2bn((unsigned char *)rbuf.buf, rbuf.len, sig->r)) {
    297308        PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error()));
    298309        ECDSA_SIG_free(sig);
     310        m2_PyBuffer_Release(value, &vbuf);
     311        m2_PyBuffer_Release(r, &rbuf);
     312        m2_PyBuffer_Release(s, &sbuf);
    299313        return -1;
    300314    }
    301     if (!BN_mpi2bn((unsigned char *)sbuf, slen, sig->s)) {
     315    if (!BN_mpi2bn((unsigned char *)sbuf.buf, sbuf.len, sig->s)) {
    302316        PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error()));
    303317        ECDSA_SIG_free(sig);
     318        m2_PyBuffer_Release(value, &vbuf);
     319        m2_PyBuffer_Release(r, &rbuf);
     320        m2_PyBuffer_Release(s, &sbuf);
    304321        return -1;
    305322    }
    306     ret = ECDSA_do_verify(vbuf, vlen, sig, key);
     323    ret = ECDSA_do_verify(vbuf.buf, vbuf.len, sig, key);
    307324    ECDSA_SIG_free(sig);
     325    m2_PyBuffer_Release(value, &vbuf);
     326    m2_PyBuffer_Release(r, &rbuf);
     327    m2_PyBuffer_Release(s, &sbuf);
    308328    if (ret == -1)
    309329        PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error()));
    310330    return ret;
     
    312332
    313333
    314334PyObject *ecdsa_sign_asn1(EC_KEY *key, PyObject *value) {
    315     const void *vbuf;
    316     int vlen;
     335    Py_buffer vbuf;
    317336    void *sigbuf;
    318337    unsigned int siglen;
    319338    PyObject *ret;
    320339
    321     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
     340    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
    322341        return NULL;
    323342
    324343    if (!(sigbuf = PyMem_Malloc(ECDSA_size(key)))) {
    325344        PyErr_SetString(PyExc_MemoryError, "ecdsa_sign_asn1");
     345        m2_PyBuffer_Release(value, &vbuf);
    326346        return NULL;
    327347    }
    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)) {
    329350        PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error()));
    330351        PyMem_Free(sigbuf);
     352        m2_PyBuffer_Release(value, &vbuf);
    331353        return NULL;
    332354    }
    333355    ret = PyString_FromStringAndSize(sigbuf, siglen);
    334356    PyMem_Free(sigbuf);
     357    m2_PyBuffer_Release(value, &vbuf);
    335358    return ret;
    336359}
    337360
    338361
    339362int 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;
    343365
    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    }
    348372
    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)
    350375        PyErr_SetString(_ec_err, ERR_reason_error_string(ERR_get_error()));
     376    m2_PyBuffer_Release(value, &vbuf);
     377    m2_PyBuffer_Release(sig, &sbuf);
    351378    return ret;
    352379}
    353380
     
    391418    EC_KEY *keypair;
    392419
    393420    if (PyObject_AsReadBuffer(pubkey, &keypairbuf, &keypairbuflen) == -1)
    394     {
    395         return NULL;
    396     }
     421      return NULL;
    397422
    398423    tempBuf = (const unsigned char *)keypairbuf;
    399424    if ((keypair = d2i_EC_PUBKEY( NULL, &tempBuf, keypairbuflen)) == 0)
  • SWIG/_rc4.i

     
    2222}
    2323
    2424PyObject *rc4_set_key(RC4_KEY *key, PyObject *value) {
    25     const void *vbuf;
    26     int vlen;
     25    Py_buffer vbuf;
    2726
    28     if (m2_PyObject_AsReadBufferInt(value, &vbuf, &vlen) == -1)
     27    if (m2_PyObject_GetBufferInt(value, &vbuf, PyBUF_SIMPLE) == -1)
    2928        return NULL;
    3029
    31     RC4_set_key(key, vlen, vbuf);
     30    RC4_set_key(key, vbuf.len, vbuf.buf);
     31    m2_PyBuffer_Release(value, &vbuf);
    3232    Py_INCREF(Py_None);
    3333    return Py_None;
    3434}
    3535
    3636PyObject *rc4_update(RC4_KEY *key, PyObject *in) {
    3737    PyObject *ret;
    38     const void *buf;
    39     Py_ssize_t len;
    4038    void *out;
     39    Py_buffer buf;
    4140
    42     if (PyObject_AsReadBuffer(in, &buf, &len) == -1)
     41    if (m2_PyObject_GetBuffer(in, &buf, PyBUF_SIMPLE) == -1)
    4342        return NULL;
    4443
    45     if (!(out = PyMem_Malloc(len))) {
     44    if (!(out = PyMem_Malloc(buf.len))) {
    4645        PyErr_SetString(PyExc_MemoryError, "expected a string object");
     46        m2_PyBuffer_Release(in, &buf);
    4747        return NULL;
    4848    }
    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);
    5151    PyMem_Free(out);
     52    m2_PyBuffer_Release(in, &buf);
    5253    return ret;
    5354}
    5455