Merge pull request #2220 from reaperhulk/encode-cp

support encoding certificate policies in CertificateBuilder
diff --git a/src/_cffi_src/openssl/engine.py b/src/_cffi_src/openssl/engine.py
index 011f669..60c6f3e 100644
--- a/src/_cffi_src/openssl/engine.py
+++ b/src/_cffi_src/openssl/engine.py
@@ -44,6 +44,8 @@
 static const unsigned int ENGINE_METHOD_STORE;
 static const unsigned int ENGINE_METHOD_ALL;
 static const unsigned int ENGINE_METHOD_NONE;
+
+static const int ENGINE_R_CONFLICTING_ENGINE_ID;
 """
 
 FUNCTIONS = """
diff --git a/src/cryptography/hazmat/bindings/openssl/binding.py b/src/cryptography/hazmat/bindings/openssl/binding.py
index 47b1d6e..4fac11d 100644
--- a/src/cryptography/hazmat/bindings/openssl/binding.py
+++ b/src/cryptography/hazmat/bindings/openssl/binding.py
@@ -97,11 +97,6 @@
     @classmethod
     def _register_osrandom_engine(cls):
         _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()
         _openssl_assert(cls.lib, engine != cls.ffi.NULL)
@@ -113,7 +108,13 @@
             result = cls.lib.ENGINE_set_RAND(engine, cls._osrandom_method)
             _openssl_assert(cls.lib, result == 1)
             result = cls.lib.ENGINE_add(engine)
-            _openssl_assert(cls.lib, result == 1)
+            if result != 1:
+                errors = _consume_errors(cls.lib)
+                _openssl_assert(
+                    cls.lib,
+                    errors[0].reason == cls.lib.ENGINE_R_CONFLICTING_ENGINE_ID
+                )
+
         finally:
             result = cls.lib.ENGINE_free(engine)
             _openssl_assert(cls.lib, result == 1)
diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py
index 20171fa..76a9218 100644
--- a/tests/hazmat/bindings/test_openssl.py
+++ b/tests/hazmat/bindings/test_openssl.py
@@ -89,8 +89,8 @@
 
     def test_add_engine_more_than_once(self):
         b = Binding()
-        with pytest.raises(RuntimeError):
-            b._register_osrandom_engine()
+        b._register_osrandom_engine()
+        assert b.lib.ERR_get_error() == 0
 
     def test_ssl_ctx_options(self):
         # Test that we're properly handling 32-bit unsigned on all platforms.