diff options
author | Daniel Wozniak <dan@woz.io> | 2018-09-07 04:58:12 -0700 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2018-10-02 13:35:00 +0200 |
commit | 2b32a803e4be518df18b9136482b6573172bbc27 (patch) | |
tree | 6f304c664d91a8dc88f59ac791d173a3171b00cd | |
parent | 82d68fc1a3239513f84e4d7dcfa8cc2c1fd408da (diff) | |
download | m2crypto-2b32a803e4be518df18b9136482b6573172bbc27.tar.gz |
Fix asn1_integer_set metho used by x509 serial
- Avoid overflow errors on py3 if the integer is larger then the
greatest c long value.
- Fix fallback asn1_integer_set by using python 3 specific C api methods
-rw-r--r-- | SWIG/_asn1.i | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/SWIG/_asn1.i b/SWIG/_asn1.i index 0c36519..6f73929 100644 --- a/SWIG/_asn1.i +++ b/SWIG/_asn1.i @@ -149,8 +149,12 @@ int asn1_integer_set(ASN1_INTEGER *asn1, PyObject *value) { * /usr/include/python2.7/longobject.h. */ #if PY_MAJOR_VERSION >= 3 - if (PyLong_Check(value)) - return ASN1_INTEGER_set(asn1, PyLong_AsLong(value)); + if (PyLong_Check(value)) { + int overflow = 0; + long val = PyLong_AsLongAndOverflow(value, &overflow); + if (overflow == 0) + return ASN1_INTEGER_set(asn1, val); + } #else if (PyInt_Check(value)) return ASN1_INTEGER_set(asn1, PyInt_AS_LONG(value)); @@ -161,7 +165,11 @@ int asn1_integer_set(ASN1_INTEGER *asn1, PyObject *value) { return 0; } +#if PY_MAJOR_VERSION >= 3 + fmt = PyUnicode_FromString("%x"); +#else fmt = PyString_FromString("%x"); +#endif // PY_MAJOR_VERSION >= 3 if (!fmt) return 0; @@ -176,7 +184,11 @@ int asn1_integer_set(ASN1_INTEGER *asn1, PyObject *value) { Py_INCREF(value); PyTuple_SET_ITEM(args, 0, value); +#if PY_MAJOR_VERSION >= 3 + hex = PyUnicode_Format(fmt, args); +#else hex = PyString_Format(fmt, args); +#endif // PY_MAJOR_VERSION >= 3 if (!hex){ PyErr_SetString(PyExc_RuntimeError, "PyString_Format() failed"); @@ -188,7 +200,11 @@ int asn1_integer_set(ASN1_INTEGER *asn1, PyObject *value) { Py_DECREF(fmt); Py_DECREF(args); +#if PY_MAJOR_VERSION >= 3 + if (BN_hex2bn(&bn, PyUnicode_AsUTF8(hex)) <= 0){ +#else if (BN_hex2bn(&bn, PyString_AsString(hex)) <= 0){ +#endif // PY_MAJOR_VERSION >= 3 m2_PyErr_Msg(PyExc_RuntimeError); Py_DECREF(hex); return 0; |