From 83274d581eed40117ff555f247858af2d3bfbb8e Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 20 Feb 2014 17:59:03 -0600 Subject: add private _int_to_bn method to openssl backend --- cryptography/hazmat/backends/openssl/backend.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py index 90d608fa..be584868 100644 --- a/cryptography/hazmat/backends/openssl/backend.py +++ b/cryptography/hazmat/backends/openssl/backend.py @@ -274,6 +274,20 @@ class Backend(object): self._lib.OPENSSL_free(hex_cdata) return int(hex_str, 16) + def _int_to_bn(self, num): + """ + Converts a python integer to a BIGNUM. The returned BIGNUM will not + be garbage collected (to support adding them to structs that take + ownership of the object). Be sure to register it for GC if it will + be discarded after use. + """ + hex_num = hex(num).rstrip("L").lstrip("0x") or "0" + bn_ptr = self._ffi.new("BIGNUM **") + res = self._lib.BN_hex2bn(bn_ptr, hex_num) + assert res != 0 + assert bn_ptr[0] != self._ffi.NULL + return bn_ptr[0] + def generate_rsa_private_key(self, public_exponent, key_size): if public_exponent < 3: raise ValueError("public_exponent must be >= 3") @@ -288,13 +302,9 @@ class Backend(object): assert ctx != self._ffi.NULL ctx = self._ffi.gc(ctx, self._lib.RSA_free) - bn = self._lib.BN_new() - assert bn != self._ffi.NULL + bn = self._int_to_bn(public_exponent) bn = self._ffi.gc(bn, self._lib.BN_free) - res = self._lib.BN_set_word(bn, public_exponent) - assert res == 1 - res = self._lib.RSA_generate_key_ex( ctx, key_size, bn, self._ffi.NULL ) -- cgit v1.2.3 From b73dd29234e854d25c8fc09bdf5bb57143fc0250 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 20 Feb 2014 18:01:03 -0600 Subject: bytes and strings...yes. --- cryptography/hazmat/backends/openssl/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py index be584868..de6f841c 100644 --- a/cryptography/hazmat/backends/openssl/backend.py +++ b/cryptography/hazmat/backends/openssl/backend.py @@ -281,7 +281,7 @@ class Backend(object): ownership of the object). Be sure to register it for GC if it will be discarded after use. """ - hex_num = hex(num).rstrip("L").lstrip("0x") or "0" + hex_num = hex(num).rstrip("L").lstrip("0x").encode("ascii") or b"0" bn_ptr = self._ffi.new("BIGNUM **") res = self._lib.BN_hex2bn(bn_ptr, hex_num) assert res != 0 -- cgit v1.2.3