47c1c9
From e3e043ab363387033ddfdcaf3c15d8cf8dda17ed Mon Sep 17 00:00:00 2001
47c1c9
From: Christian Heimes <cheimes@redhat.com>
47c1c9
Date: Tue, 27 Oct 2020 16:42:15 +0100
47c1c9
Subject: [PATCH 1] Re-add deprecated and removed features
47c1c9
47c1c9
* encode_rfc6979_signature()
47c1c9
* decode_rfc6979_signature()
47c1c9
* Certificate.serial property
47c1c9
* MACContext
47c1c9
* osrandom engine is disabled
47c1c9
47c1c9
Signed-off-by: Christian Heimes <cheimes@redhat.com>
47c1c9
---
47c1c9
 .../hazmat/backends/openssl/cmac.py           |  3 +-
47c1c9
 .../hazmat/backends/openssl/hmac.py           |  3 +-
47c1c9
 .../hazmat/backends/openssl/x509.py           |  4 ++
47c1c9
 .../hazmat/primitives/asymmetric/utils.py     |  8 ++++
47c1c9
 src/cryptography/hazmat/primitives/cmac.py    |  3 +-
47c1c9
 src/cryptography/hazmat/primitives/hmac.py    |  3 +-
47c1c9
 src/cryptography/hazmat/primitives/mac.py     | 37 +++++++++++++++++++
47c1c9
 src/cryptography/x509/extensions.py           |  6 ++-
47c1c9
 tests/hazmat/backends/test_openssl.py         |  3 ++
47c1c9
 tests/hazmat/primitives/test_asym_utils.py    |  9 +++++
47c1c9
 tests/x509/test_x509.py                       |  1 +
47c1c9
 tests/x509/test_x509_ext.py                   |  5 +++
47c1c9
 12 files changed, 80 insertions(+), 5 deletions(-)
47c1c9
 create mode 100644 src/cryptography/hazmat/primitives/mac.py
47c1c9
47c1c9
diff --git a/src/cryptography/hazmat/backends/openssl/cmac.py b/src/cryptography/hazmat/backends/openssl/cmac.py
47c1c9
index 195fc230f..5281f634d 100644
47c1c9
--- a/src/cryptography/hazmat/backends/openssl/cmac.py
47c1c9
+++ b/src/cryptography/hazmat/backends/openssl/cmac.py
47c1c9
@@ -11,10 +11,11 @@ from cryptography.exceptions import (
47c1c9
     UnsupportedAlgorithm,
47c1c9
     _Reasons,
47c1c9
 )
47c1c9
-from cryptography.hazmat.primitives import constant_time
47c1c9
+from cryptography.hazmat.primitives import constant_time, mac
47c1c9
 from cryptography.hazmat.primitives.ciphers.modes import CBC
47c1c9
 
47c1c9
 
47c1c9
+@utils.register_interface(mac.MACContext)
47c1c9
 class _CMACContext(object):
47c1c9
     def __init__(self, backend, algorithm, ctx=None):
47c1c9
         if not backend.cmac_algorithm_supported(algorithm):
47c1c9
diff --git a/src/cryptography/hazmat/backends/openssl/hmac.py b/src/cryptography/hazmat/backends/openssl/hmac.py
47c1c9
index 5024223b2..11c850e10 100644
47c1c9
--- a/src/cryptography/hazmat/backends/openssl/hmac.py
47c1c9
+++ b/src/cryptography/hazmat/backends/openssl/hmac.py
47c1c9
@@ -11,9 +11,10 @@ from cryptography.exceptions import (
47c1c9
     UnsupportedAlgorithm,
47c1c9
     _Reasons,
47c1c9
 )
47c1c9
-from cryptography.hazmat.primitives import constant_time, hashes
47c1c9
+from cryptography.hazmat.primitives import constant_time, hashes, mac
47c1c9
 
47c1c9
 
47c1c9
+@utils.register_interface(mac.MACContext)
47c1c9
 @utils.register_interface(hashes.HashContext)
47c1c9
 class _HMACContext(object):
47c1c9
     def __init__(self, backend, key, algorithm, ctx=None):
47c1c9
diff --git a/src/cryptography/hazmat/backends/openssl/x509.py b/src/cryptography/hazmat/backends/openssl/x509.py
47c1c9
index 4d0dac764..c9074f59e 100644
47c1c9
--- a/src/cryptography/hazmat/backends/openssl/x509.py
47c1c9
+++ b/src/cryptography/hazmat/backends/openssl/x509.py
47c1c9
@@ -73,6 +73,10 @@ class _Certificate(object):
47c1c9
         self._backend.openssl_assert(asn1_int != self._backend._ffi.NULL)
47c1c9
         return _asn1_integer_to_int(self._backend, asn1_int)
47c1c9
 
47c1c9
+    @property
47c1c9
+    def serial(self):
47c1c9
+        return self.serial_number
47c1c9
+
47c1c9
     def public_key(self):
47c1c9
         pkey = self._backend._lib.X509_get_pubkey(self._x509)
47c1c9
         if pkey == self._backend._ffi.NULL:
47c1c9
diff --git a/src/cryptography/hazmat/primitives/asymmetric/utils.py b/src/cryptography/hazmat/primitives/asymmetric/utils.py
47c1c9
index 5f9b67786..886d7565b 100644
47c1c9
--- a/src/cryptography/hazmat/primitives/asymmetric/utils.py
47c1c9
+++ b/src/cryptography/hazmat/primitives/asymmetric/utils.py
47c1c9
@@ -39,3 +39,11 @@ class Prehashed(object):
47c1c9
         self._digest_size = algorithm.digest_size
47c1c9
 
47c1c9
     digest_size = utils.read_only_property("_digest_size")
47c1c9
+
47c1c9
+
47c1c9
+def decode_rfc6979_signature(signature):
47c1c9
+    return decode_dss_signature(signature)
47c1c9
+
47c1c9
+
47c1c9
+def encode_rfc6979_signature(r, s):
47c1c9
+    return encode_dss_signature(r, s)
47c1c9
diff --git a/src/cryptography/hazmat/primitives/cmac.py b/src/cryptography/hazmat/primitives/cmac.py
47c1c9
index bf962c906..7f37f13cc 100644
47c1c9
--- a/src/cryptography/hazmat/primitives/cmac.py
47c1c9
+++ b/src/cryptography/hazmat/primitives/cmac.py
47c1c9
@@ -12,9 +12,10 @@ from cryptography.exceptions import (
47c1c9
 )
47c1c9
 from cryptography.hazmat.backends import _get_backend
47c1c9
 from cryptography.hazmat.backends.interfaces import CMACBackend
47c1c9
-from cryptography.hazmat.primitives import ciphers
47c1c9
+from cryptography.hazmat.primitives import ciphers, mac
47c1c9
 
47c1c9
 
47c1c9
+@utils.register_interface(mac.MACContext)
47c1c9
 class CMAC(object):
47c1c9
     def __init__(self, algorithm, backend=None, ctx=None):
47c1c9
         backend = _get_backend(backend)
47c1c9
diff --git a/src/cryptography/hazmat/primitives/hmac.py b/src/cryptography/hazmat/primitives/hmac.py
47c1c9
index 8c421dc68..6f03a1071 100644
47c1c9
--- a/src/cryptography/hazmat/primitives/hmac.py
47c1c9
+++ b/src/cryptography/hazmat/primitives/hmac.py
47c1c9
@@ -12,9 +12,10 @@ from cryptography.exceptions import (
47c1c9
 )
47c1c9
 from cryptography.hazmat.backends import _get_backend
47c1c9
 from cryptography.hazmat.backends.interfaces import HMACBackend
47c1c9
-from cryptography.hazmat.primitives import hashes
47c1c9
+from cryptography.hazmat.primitives import hashes, mac
47c1c9
 
47c1c9
 
47c1c9
+@utils.register_interface(mac.MACContext)
47c1c9
 @utils.register_interface(hashes.HashContext)
47c1c9
 class HMAC(object):
47c1c9
     def __init__(self, key, algorithm, backend=None, ctx=None):
47c1c9
diff --git a/src/cryptography/hazmat/primitives/mac.py b/src/cryptography/hazmat/primitives/mac.py
47c1c9
new file mode 100644
47c1c9
index 000000000..4c95190ba
47c1c9
--- /dev/null
47c1c9
+++ b/src/cryptography/hazmat/primitives/mac.py
47c1c9
@@ -0,0 +1,37 @@
47c1c9
+# This file is dual licensed under the terms of the Apache License, Version
47c1c9
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
47c1c9
+# for complete details.
47c1c9
+
47c1c9
+from __future__ import absolute_import, division, print_function
47c1c9
+
47c1c9
+import abc
47c1c9
+
47c1c9
+import six
47c1c9
+
47c1c9
+
47c1c9
+@six.add_metaclass(abc.ABCMeta)
47c1c9
+class MACContext(object):
47c1c9
+    @abc.abstractmethod
47c1c9
+    def update(self, data):
47c1c9
+        """
47c1c9
+        Processes the provided bytes.
47c1c9
+        """
47c1c9
+
47c1c9
+    @abc.abstractmethod
47c1c9
+    def finalize(self):
47c1c9
+        """
47c1c9
+        Returns the message authentication code as bytes.
47c1c9
+        """
47c1c9
+
47c1c9
+    @abc.abstractmethod
47c1c9
+    def copy(self):
47c1c9
+        """
47c1c9
+        Return a MACContext that is a copy of the current context.
47c1c9
+        """
47c1c9
+
47c1c9
+    @abc.abstractmethod
47c1c9
+    def verify(self, signature):
47c1c9
+        """
47c1c9
+        Checks if the generated message authentication code matches the
47c1c9
+        signature.
47c1c9
+        """
47c1c9
diff --git a/src/cryptography/x509/extensions.py b/src/cryptography/x509/extensions.py
47c1c9
index 130ba69b8..ddbccdf3b 100644
47c1c9
--- a/src/cryptography/x509/extensions.py
47c1c9
+++ b/src/cryptography/x509/extensions.py
47c1c9
@@ -218,8 +218,12 @@ class AuthorityKeyIdentifier(object):
47c1c9
 
47c1c9
     @classmethod
47c1c9
     def from_issuer_subject_key_identifier(cls, ski):
47c1c9
+        if isinstance(ski, SubjectKeyIdentifier):
47c1c9
+            digest = ski.digest
47c1c9
+        else:
47c1c9
+            digest = ski.value.digest
47c1c9
         return cls(
47c1c9
-            key_identifier=ski.digest,
47c1c9
+            key_identifier=digest,
47c1c9
             authority_cert_issuer=None,
47c1c9
             authority_cert_serial_number=None,
47c1c9
         )
47c1c9
diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py
47c1c9
index 2f7e7bebf..73c17d84f 100644
47c1c9
--- a/tests/hazmat/backends/test_openssl.py
47c1c9
+++ b/tests/hazmat/backends/test_openssl.py
47c1c9
@@ -301,6 +301,9 @@ class TestOpenSSLRandomEngine(object):
47c1c9
         res = backend._lib.ENGINE_free(e)
47c1c9
         assert res == 1
47c1c9
 
47c1c9
+    def test_rhel8_no_osrandom(self):
47c1c9
+        pytest.fail("osrandom engine is not FIPS compliant, see RHBZ#1762667")
47c1c9
+
47c1c9
 
47c1c9
 @pytest.mark.skipif(
47c1c9
     backend._lib.CRYPTOGRAPHY_NEEDS_OSRANDOM_ENGINE,
47c1c9
diff --git a/tests/hazmat/primitives/test_asym_utils.py b/tests/hazmat/primitives/test_asym_utils.py
47c1c9
index 70bff012f..334b459b5 100644
47c1c9
--- a/tests/hazmat/primitives/test_asym_utils.py
47c1c9
+++ b/tests/hazmat/primitives/test_asym_utils.py
47c1c9
@@ -10,6 +10,8 @@ from cryptography.hazmat.primitives.asymmetric.utils import (
47c1c9
     Prehashed,
47c1c9
     decode_dss_signature,
47c1c9
     encode_dss_signature,
47c1c9
+    encode_rfc6979_signature,
47c1c9
+    decode_rfc6979_signature
47c1c9
 )
47c1c9
 
47c1c9
 
47c1c9
@@ -75,3 +77,10 @@ def test_decode_dss_invalid_asn1():
47c1c9
 def test_pass_invalid_prehashed_arg():
47c1c9
     with pytest.raises(TypeError):
47c1c9
         Prehashed(object())
47c1c9
+
47c1c9
+
47c1c9
+def test_deprecated_rfc6979_signature():
47c1c9
+    sig = encode_rfc6979_signature(1, 1)
47c1c9
+    assert sig == b"0\x06\x02\x01\x01\x02\x01\x01"
47c1c9
+    decoded = decode_rfc6979_signature(sig)
47c1c9
+    assert decoded == (1, 1)
47c1c9
diff --git a/tests/x509/test_x509.py b/tests/x509/test_x509.py
47c1c9
index 11c80816c..e5bdf17d4 100644
47c1c9
--- a/tests/x509/test_x509.py
47c1c9
+++ b/tests/x509/test_x509.py
47c1c9
@@ -685,6 +685,7 @@ class TestRSACertificate(object):
47c1c9
         )
47c1c9
         assert isinstance(cert, x509.Certificate)
47c1c9
         assert cert.serial_number == 11559813051657483483
47c1c9
+        assert cert.serial == cert.serial_number
47c1c9
         fingerprint = binascii.hexlify(cert.fingerprint(hashes.SHA1()))
47c1c9
         assert fingerprint == b"2b619ed04bfc9c3b08eb677d272192286a0947a8"
47c1c9
         assert isinstance(cert.signature_hash_algorithm, hashes.SHA1)
47c1c9
diff --git a/tests/x509/test_x509_ext.py b/tests/x509/test_x509_ext.py
47c1c9
index 2cd216fb6..ac2b2c03d 100644
47c1c9
--- a/tests/x509/test_x509_ext.py
47c1c9
+++ b/tests/x509/test_x509_ext.py
47c1c9
@@ -3442,6 +3442,11 @@ class TestAuthorityKeyIdentifierExtension(object):
47c1c9
         )
47c1c9
         assert ext.value == aki
47c1c9
 
47c1c9
+        aki = x509.AuthorityKeyIdentifier.from_issuer_subject_key_identifier(
47c1c9
+            ski_ext
47c1c9
+        )
47c1c9
+        assert ext.value == aki
47c1c9
+
47c1c9
 
47c1c9
 class TestNameConstraints(object):
47c1c9
     def test_ipaddress_wrong_type(self):
47c1c9
-- 
47c1c9
2.26.2
47c1c9