Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Convert asserts in bindings as well
  • Loading branch information
alex committed Sep 26, 2015
1 parent 1dbdd88 commit 5fed07c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
34 changes: 5 additions & 29 deletions src/cryptography/hazmat/backends/openssl/backend.py
Expand Up @@ -42,7 +42,9 @@
_Certificate, _CertificateSigningRequest, _DISTPOINT_TYPE_FULLNAME,
_DISTPOINT_TYPE_RELATIVENAME
)
from cryptography.hazmat.bindings.openssl.binding import Binding
from cryptography.hazmat.bindings.openssl.binding import (
_consume_errors, _openssl_assert, Binding
)
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa
from cryptography.hazmat.primitives.asymmetric.padding import (
Expand All @@ -58,14 +60,6 @@


_MemoryBIO = collections.namedtuple("_MemoryBIO", ["bio", "char_ptr"])
_OpenSSLError = collections.namedtuple("_OpenSSLError",
["code", "lib", "func", "reason"])


class UnhandledOpenSSLError(Exception):
def __init__(self, msg, errors):
super(UnhandledOpenSSLError, self).__init__(msg)
self.errors = errors


def _encode_asn1_int(backend, x):
Expand Down Expand Up @@ -541,14 +535,7 @@ def __init__(self):
self.activate_osrandom_engine()

def openssl_assert(self, ok):
if not ok:
errors = self._consume_errors()
raise UnhandledOpenSSLError(
"Unknown OpenSSL error. Please file an issue at https://github"
".com/pyca/cryptography/issues with information on how to "
"reproduce this.",
errors
)
return _openssl_assert(self._lib, ok)

def activate_builtin_random(self):
# Obtain a new structural reference.
Expand Down Expand Up @@ -759,18 +746,7 @@ def _err_string(self, code):
return self._ffi.string(err_buf, 256)[:]

def _consume_errors(self):
errors = []
while True:
code = self._lib.ERR_get_error()
if code == 0:
break

lib = self._lib.ERR_GET_LIB(code)
func = self._lib.ERR_GET_FUNC(code)
reason = self._lib.ERR_GET_REASON(code)

errors.append(_OpenSSLError(code, lib, func, reason))
return errors
return _consume_errors(self._lib)

def _unknown_error(self, error):
return InternalError(
Expand Down
51 changes: 44 additions & 7 deletions src/cryptography/hazmat/bindings/openssl/binding.py
Expand Up @@ -4,6 +4,7 @@

from __future__ import absolute_import, division, print_function

import collections
import os
import threading
import types
Expand All @@ -12,6 +13,42 @@
from cryptography.hazmat.bindings.openssl._conditional import CONDITIONAL_NAMES


_OpenSSLError = collections.namedtuple("_OpenSSLError",
["code", "lib", "func", "reason"])


class UnhandledOpenSSLError(Exception):
def __init__(self, msg, errors):
super(UnhandledOpenSSLError, self).__init__(msg)
self.errors = errors


def _consume_errors(lib):
errors = []
while True:
code = lib.ERR_get_error()
if code == 0:
break

lib = lib.ERR_GET_LIB(code)
func = lib.ERR_GET_FUNC(code)
reason = lib.ERR_GET_REASON(code)

errors.append(_OpenSSLError(code, lib, func, reason))
return errors


def _openssl_assert(lib, ok):
if not ok:
errors = _consume_errors(lib)
raise UnhandledOpenSSLError(
"Unknown OpenSSL error. Please file an issue at https://github.com"
"/pyca/cryptography/issues with information on how to reproduce "
"this.",
errors
)


@ffi.callback("int (*)(unsigned char *, int)", error=-1)
def _osrandom_rand_bytes(buf, size):
signed = ffi.cast("char *", buf)
Expand Down Expand Up @@ -64,27 +101,27 @@ def __init__(self):

@classmethod
def _register_osrandom_engine(cls):
assert cls.lib.ERR_peek_error() == 0
_openssl_assert(cls.lib, cls.lib.ERR_peek_error() == 0)
looked_up_engine = cls.lib.ENGINE_by_id(cls._osrandom_engine_id)
if looked_up_engine != ffi.NULL:
raise RuntimeError("osrandom engine already registered")

cls.lib.ERR_clear_error()

engine = cls.lib.ENGINE_new()
assert engine != cls.ffi.NULL
_openssl_assert(cls.lib, engine != cls.ffi.NULL)
try:
result = cls.lib.ENGINE_set_id(engine, cls._osrandom_engine_id)
assert result == 1
_openssl_assert(cls.lib, result == 1)
result = cls.lib.ENGINE_set_name(engine, cls._osrandom_engine_name)
assert result == 1
_openssl_assert(cls.lib, result == 1)
result = cls.lib.ENGINE_set_RAND(engine, cls._osrandom_method)
assert result == 1
_openssl_assert(cls.lib, result == 1)
result = cls.lib.ENGINE_add(engine)
assert result == 1
_openssl_assert(cls.lib, result == 1)
finally:
result = cls.lib.ENGINE_free(engine)
assert result == 1
_openssl_assert(cls.lib, result == 1)

@classmethod
def _ensure_ffi_initialized(cls):
Expand Down

0 comments on commit 5fed07c

Please sign in to comment.