Esempio n. 1
0
    def test_deprecated(self, monkeypatch):
        mod = types.ModuleType("TestDeprecated/test_deprecated")
        monkeypatch.setitem(sys.modules, mod.__name__, mod)
        mod.X = deprecated(
            value=1,
            module_name=mod.__name__,
            message="deprecated message text",
            warning_class=DeprecationWarning
        )
        mod.Y = deprecated(
            value=2,
            module_name=mod.__name__,
            message="more deprecated text",
            warning_class=PendingDeprecationWarning,
        )
        mod = sys.modules[mod.__name__]
        mod.Z = 3

        with warnings.catch_warnings(record=True) as log:
            warnings.simplefilter("always", PendingDeprecationWarning)
            warnings.simplefilter("always", DeprecationWarning)
            assert mod.X == 1
            assert mod.Y == 2
            assert mod.Z == 3

        [msg1, msg2] = log
        assert msg1.category is DeprecationWarning
        assert msg1.message.args == ("deprecated message text",)

        assert msg2.category is PendingDeprecationWarning
        assert msg2.message.args == ("more deprecated text",)

        assert "Y" in dir(mod)
Esempio n. 2
0
    def test_deprecated(self, monkeypatch):
        mod = types.ModuleType("TestDeprecated/test_deprecated")
        monkeypatch.setitem(sys.modules, mod.__name__, mod)
        deprecated(
            name="X",
            value=1,
            module_name=mod.__name__,
            message="deprecated message text",
            warning_class=DeprecationWarning,
        )
        mod.Y = deprecated(
            value=2,
            module_name=mod.__name__,
            message="more deprecated text",
            warning_class=PendingDeprecationWarning,
        )
        mod = sys.modules[mod.__name__]
        mod.Z = 3

        with warnings.catch_warnings(record=True) as log:
            warnings.simplefilter("always", PendingDeprecationWarning)
            warnings.simplefilter("always", DeprecationWarning)
            assert mod.X == 1
            assert mod.Y == 2
            assert mod.Z == 3

        [msg1, msg2] = log
        assert msg1.category is DeprecationWarning
        assert msg1.message.args == ("deprecated message text", )

        assert msg2.category is PendingDeprecationWarning
        assert msg2.message.args == ("more deprecated text", )

        assert "Y" in dir(mod)
Esempio n. 3
0
    def test_deleting_deprecated_members(self, monkeypatch):
        mod = types.ModuleType("TestDeprecated/test_deprecated")
        monkeypatch.setitem(sys.modules, mod.__name__, mod)
        deprecated(
            name="X",
            value=1,
            module_name=mod.__name__,
            message="deprecated message text",
            warning_class=DeprecationWarning,
        )
        mod.Y = deprecated(
            value=2,
            module_name=mod.__name__,
            message="more deprecated text",
            warning_class=PendingDeprecationWarning,
        )
        mod = sys.modules[mod.__name__]
        mod.Z = 3

        with warnings.catch_warnings(record=True) as log:
            warnings.simplefilter("always", PendingDeprecationWarning)
            warnings.simplefilter("always", DeprecationWarning)
            del mod.X
            del mod.Y
            del mod.Z

        [msg1, msg2] = log
        assert msg1.category is DeprecationWarning
        assert msg1.message.args == ("deprecated message text", )

        assert msg2.category is PendingDeprecationWarning
        assert msg2.message.args == ("more deprecated text", )

        assert "X" not in dir(mod)
        assert "Y" not in dir(mod)
        assert "Z" not in dir(mod)

        with pytest.raises(AttributeError):
            del mod.X
Esempio n. 4
0
    EllipticCurveSignatureAlgorithm)
from cryptography.hazmat.primitives.interfaces.ciphers import (
    BlockCipherAlgorithm, CipherAlgorithm, Mode, ModeWithAuthenticationTag,
    ModeWithInitializationVector, ModeWithNonce)

__all__ = [
    "BlockCipherAlgorithm", "CipherAlgorithm", "EllipticCurve",
    "EllipticCurvePrivateKey", "EllipticCurvePrivateKeyWithNumbers",
    "EllipticCurvePublicKey", "EllipticCurvePublicKeyWithNumbers",
    "EllipticCurveSignatureAlgorithm", "Mode", "ModeWithAuthenticationTag",
    "ModeWithInitializationVector", "ModeWithNonce"
]

DSAParameters = utils.deprecated(
    dsa.DSAParameters, __name__,
    ("The DSAParameters interface has moved to the "
     "cryptography.hazmat.primitives.asymmetric.dsa.module"),
    utils.DeprecatedIn08)

DSAParametersWithNumbers = utils.deprecated(
    dsa.DSAParametersWithNumbers, __name__,
    ("The DSAParametersWithNumbers interface has moved to the "
     "cryptography.hazmat.primitives.asymmetric.dsa.module"),
    utils.DeprecatedIn08)

DSAPrivateKey = utils.deprecated(
    dsa.DSAPrivateKey, __name__,
    ("The DSAPrivateKey interface has moved to the "
     "cryptography.hazmat.primitives.asymmetric.dsa.module"),
    utils.DeprecatedIn08)
Esempio n. 5
0
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import absolute_import, division, print_function

from cryptography import utils
from cryptography.hazmat.primitives.mac import MACContext as _MACContext

MACContext = utils.deprecated(
    _MACContext, __name__,
    "MACContext was moved to cryptography.hazmat.primitives.mac.MACContext "
    "in version 1.9.", utils.DeprecatedIn19)
Esempio n. 6
0
        """
        Returns an EllipticCurvePrivateNumbers.
        """

    @abc.abstractmethod
    def private_bytes(self, encoding, format, encryption_algorithm):
        """
        Returns the key serialized as bytes.
        """


EllipticCurvePrivateKeyWithNumbers = utils.deprecated(
    EllipticCurvePrivateKeyWithSerialization,
    __name__,
    (
        "The EllipticCurvePrivateKeyWithNumbers interface has been renamed to "
        "EllipticCurvePrivateKeyWithSerialization"
    ),
    utils.DeprecatedIn08
)


@six.add_metaclass(abc.ABCMeta)
class EllipticCurvePublicKey(object):
    @abc.abstractmethod
    def verifier(self, signature, signature_algorithm):
        """
        Returns an AsymmetricVerificationContext used for signing data.
        """

    @abc.abstractproperty
Esempio n. 7
0
    @abc.abstractmethod
    def private_numbers(self):
        """
        Returns a DSAPrivateNumbers.
        """

    @abc.abstractmethod
    def private_bytes(self, encoding, format, encryption_algorithm):
        """
        Returns the key serialized as bytes.
        """


DSAPrivateKeyWithNumbers = utils.deprecated(
    DSAPrivateKeyWithSerialization,
    __name__,
    ("The DSAPrivateKeyWithNumbers interface has been renamed to " "DSAPrivateKeyWithSerialization"),
    utils.DeprecatedIn08,
)


@six.add_metaclass(abc.ABCMeta)
class DSAPublicKey(object):
    @abc.abstractproperty
    def key_size(self):
        """
        The bit length of the prime modulus.
        """

    @abc.abstractmethod
    def parameters(self):
        """
Esempio n. 8
0
    block_size = 64
    key_sizes = frozenset(range(32, 449, 8))

    def __init__(self, key: bytes):
        self.key = _verify_key_size(self, key)

    @property
    def key_size(self) -> int:
        return len(self.key) * 8


_BlowfishInternal = Blowfish
utils.deprecated(
    Blowfish,
    __name__,
    "Blowfish has been deprecated",
    utils.DeprecatedIn37,
    name="Blowfish",
)


class CAST5(CipherAlgorithm, BlockCipherAlgorithm):
    name = "CAST5"
    block_size = 64
    key_sizes = frozenset(range(40, 129, 8))

    def __init__(self, key: bytes):
        self.key = _verify_key_size(self, key)

    @property
    def key_size(self) -> int:
Esempio n. 9
0
class AlreadyUpdated(Exception):
    pass


class NotYetFinalized(Exception):
    pass


class InvalidTag(Exception):
    pass


class InvalidSignature(Exception):
    pass


class InternalError(Exception):
    def __init__(self, msg, err_code):
        super(InternalError, self).__init__(msg)
        self.err_code = err_code


class InvalidKey(Exception):
    pass


InvalidToken = utils.deprecated(
    twofactor.InvalidToken, __name__,
    ("The InvalidToken exception has moved to the "
     "cryptography.hazmat.primitives.twofactor module"), utils.DeprecatedIn09)
Esempio n. 10
0
    INHIBIT_ANY_POLICY = ObjectIdentifier("2.5.29.54")
    AUTHORITY_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.1")
    SUBJECT_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.11")
    OCSP_NO_CHECK = ObjectIdentifier("1.3.6.1.5.5.7.48.1.5")
    CRL_NUMBER = ObjectIdentifier("2.5.29.20")


class CRLEntryExtensionOID(object):
    CERTIFICATE_ISSUER = ObjectIdentifier("2.5.29.29")
    CRL_REASON = ObjectIdentifier("2.5.29.21")
    INVALIDITY_DATE = ObjectIdentifier("2.5.29.24")


CRLExtensionOID = utils.deprecated(
    CRLEntryExtensionOID,
    __name__,
    "CRLExtensionOID has been renamed to CRLEntryExtensionOID",
    utils.DeprecatedIn12
)


class NameOID(object):
    COMMON_NAME = ObjectIdentifier("2.5.4.3")
    COUNTRY_NAME = ObjectIdentifier("2.5.4.6")
    LOCALITY_NAME = ObjectIdentifier("2.5.4.7")
    STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8")
    ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10")
    ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11")
    SERIAL_NUMBER = ObjectIdentifier("2.5.4.5")
    SURNAME = ObjectIdentifier("2.5.4.4")
    GIVEN_NAME = ObjectIdentifier("2.5.4.42")
    TITLE = ObjectIdentifier("2.5.4.12")
Esempio n. 11
0
OID_SERVER_AUTH = ExtendedKeyUsageOID.SERVER_AUTH
OID_TIME_STAMPING = ExtendedKeyUsageOID.TIME_STAMPING

OID_ANY_POLICY = CertificatePoliciesOID.ANY_POLICY
OID_CPS_QUALIFIER = CertificatePoliciesOID.CPS_QUALIFIER
OID_CPS_USER_NOTICE = CertificatePoliciesOID.CPS_USER_NOTICE

OID_CERTIFICATE_ISSUER = CRLEntryExtensionOID.CERTIFICATE_ISSUER
OID_CRL_REASON = CRLEntryExtensionOID.CRL_REASON
OID_INVALIDITY_DATE = CRLEntryExtensionOID.INVALIDITY_DATE

OID_CA_ISSUERS = AuthorityInformationAccessOID.CA_ISSUERS
OID_OCSP = AuthorityInformationAccessOID.OCSP

UnsupportedExtension = utils.deprecated(
    UnsupportedExtension, __name__,
    "UnsupportedExtension is no longer necessary, it is never raised",
    utils.DeprecatedIn19)

__all__ = [
    "certificate_transparency",
    "load_pem_x509_certificate",
    "load_der_x509_certificate",
    "load_pem_x509_csr",
    "load_der_x509_csr",
    "load_pem_x509_crl",
    "load_der_x509_crl",
    "random_serial_number",
    "InvalidVersion",
    "DuplicateExtension",
    "UnsupportedExtension",
    "ExtensionNotFound",
    pass


class InvalidTag(Exception):
    pass


class InvalidSignature(Exception):
    pass


class InternalError(Exception):
    def __init__(self, msg, err_code):
        super(InternalError, self).__init__(msg)
        self.err_code = err_code


class InvalidKey(Exception):
    pass


InvalidToken = utils.deprecated(
    twofactor.InvalidToken,
    __name__,
    (
        "The InvalidToken exception has moved to the "
        "cryptography.hazmat.primitives.twofactor module"
    ),
    utils.DeprecatedIn09
)
Esempio n. 13
0
class SHA512(object):
    name = "sha512"
    digest_size = 64
    block_size = 128


@utils.register_interface(HashAlgorithm)
class RIPEMD160(object):
    name = "ripemd160"
    digest_size = 20
    block_size = 64


RIPEMD160 = utils.deprecated(
    RIPEMD160,
    __name__,
    "The RIPEMD160 hash was deprecated in version 1.9.",
    utils.DeprecatedIn19
)


@utils.register_interface(HashAlgorithm)
class Whirlpool(object):
    name = "whirlpool"
    digest_size = 64
    block_size = 64


Whirlpool = utils.deprecated(
    Whirlpool,
    __name__,
    "The Whirlpool hash was deprecated in version 1.9.",
Esempio n. 14
0
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from cryptography import utils

# This module exists to test `cryptography.utils.deprecated`

DEPRECATED = utils.deprecated(3, __name__, "Test Deprecated Object",
                              DeprecationWarning)

NOT_DEPRECATED = 12
Esempio n. 15
0
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import absolute_import, division, print_function

from cryptography import utils
from cryptography.hazmat.primitives.mac import MACContext as _MACContext


MACContext = utils.deprecated(
    _MACContext,
    __name__,
    "MACContext was moved to cryptography.hazmat.primitives.mac.MACContext "
    "in version 1.9.",
    utils.DeprecatedIn19
)
Esempio n. 16
0
from cryptography import utils
from cryptography.hazmat.primitives import ciphers, hashes
from cryptography.hazmat.primitives.asymmetric import (
    AsymmetricSignatureContext, AsymmetricVerificationContext, dsa, ec,
    padding, rsa
)
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
from cryptography.hazmat.primitives.padding import PaddingContext


BlockCipherAlgorithm = utils.deprecated(
    ciphers.BlockCipherAlgorithm,
    __name__,
    (
        "The BlockCipherAlgorithm interface has moved to the "
        "cryptography.hazmat.primitives.ciphers module"
    ),
    utils.DeprecatedIn08
)


CipherAlgorithm = utils.deprecated(
    ciphers.CipherAlgorithm,
    __name__,
    (
        "The CipherAlgorithm interface has moved to the "
        "cryptography.hazmat.primitives.ciphers module"
    ),
    utils.DeprecatedIn08
)
Esempio n. 17
0
OID_TIME_STAMPING = ExtendedKeyUsageOID.TIME_STAMPING

OID_ANY_POLICY = CertificatePoliciesOID.ANY_POLICY
OID_CPS_QUALIFIER = CertificatePoliciesOID.CPS_QUALIFIER
OID_CPS_USER_NOTICE = CertificatePoliciesOID.CPS_USER_NOTICE

OID_CERTIFICATE_ISSUER = CRLEntryExtensionOID.CERTIFICATE_ISSUER
OID_CRL_REASON = CRLEntryExtensionOID.CRL_REASON
OID_INVALIDITY_DATE = CRLEntryExtensionOID.INVALIDITY_DATE

OID_CA_ISSUERS = AuthorityInformationAccessOID.CA_ISSUERS
OID_OCSP = AuthorityInformationAccessOID.OCSP

UnsupportedExtension = utils.deprecated(
    UnsupportedExtension,
    __name__,
    "UnsupportedExtension is no longer necessary, it is never raised",
    utils.DeprecatedIn19
)

__all__ = [
    "certificate_transparency",
    "load_pem_x509_certificate",
    "load_der_x509_certificate",
    "load_pem_x509_csr",
    "load_der_x509_csr",
    "load_pem_x509_crl",
    "load_der_x509_crl",
    "random_serial_number",
    "InvalidVersion",
    "DuplicateExtension",
    "UnsupportedExtension",
Esempio n. 18
0
@utils.register_interface(HashAlgorithm)
class SHA512(object):
    name = "sha512"
    digest_size = 64
    block_size = 128


@utils.register_interface(HashAlgorithm)
class RIPEMD160(object):
    name = "ripemd160"
    digest_size = 20
    block_size = 64


RIPEMD160 = utils.deprecated(
    RIPEMD160, __name__, "The RIPEMD160 hash was deprecated in version 1.9.",
    utils.DeprecatedIn19)


@utils.register_interface(HashAlgorithm)
class Whirlpool(object):
    name = "whirlpool"
    digest_size = 64
    block_size = 64


Whirlpool = utils.deprecated(
    Whirlpool, __name__, "The Whirlpool hash was deprecated in version 1.9.",
    utils.DeprecatedIn19)

Esempio n. 19
0
    INHIBIT_ANY_POLICY = ObjectIdentifier("2.5.29.54")
    AUTHORITY_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.1")
    SUBJECT_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.11")
    OCSP_NO_CHECK = ObjectIdentifier("1.3.6.1.5.5.7.48.1.5")
    CRL_NUMBER = ObjectIdentifier("2.5.29.20")


class CRLEntryExtensionOID(object):
    CERTIFICATE_ISSUER = ObjectIdentifier("2.5.29.29")
    CRL_REASON = ObjectIdentifier("2.5.29.21")
    INVALIDITY_DATE = ObjectIdentifier("2.5.29.24")


CRLExtensionOID = utils.deprecated(
    CRLEntryExtensionOID,
    __name__,
    "CRLExtensionOID has been renamed to CRLEntryExtensionOID",
    utils.DeprecatedIn12
)


class NameOID(object):
    COMMON_NAME = ObjectIdentifier("2.5.4.3")
    COUNTRY_NAME = ObjectIdentifier("2.5.4.6")
    LOCALITY_NAME = ObjectIdentifier("2.5.4.7")
    STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8")
    ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10")
    ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11")
    SERIAL_NUMBER = ObjectIdentifier("2.5.4.5")
    SURNAME = ObjectIdentifier("2.5.4.4")
    GIVEN_NAME = ObjectIdentifier("2.5.4.42")
    TITLE = ObjectIdentifier("2.5.4.12")
Esempio n. 20
0
        """
        Returns an RSAPrivateNumbers.
        """

    @abc.abstractmethod
    def private_bytes(self, encoding, format, encryption_algorithm):
        """
        Returns the key serialized as bytes.
        """


RSAPrivateKeyWithNumbers = utils.deprecated(
    RSAPrivateKeyWithSerialization,
    __name__,
    (
        "The RSAPrivateKeyWithNumbers interface has been renamed to "
        "RSAPrivateKeyWithSerialization"
    ),
    utils.DeprecatedIn08
)


@six.add_metaclass(abc.ABCMeta)
class RSAPublicKey(object):
    @abc.abstractmethod
    def verifier(self, signature, padding, algorithm):
        """
        Returns an AsymmetricVerificationContext used for verifying signatures.
        """

    @abc.abstractmethod
Esempio n. 21
0
class RSAPrivateKeyWithSerialization(RSAPrivateKey):
    @abc.abstractmethod
    def private_numbers(self):
        """
        Returns an RSAPrivateNumbers.
        """

    @abc.abstractmethod
    def private_bytes(self, encoding, format, encryption_algorithm):
        """
        Returns the key serialized as bytes.
        """


RSAPrivateKeyWithNumbers = utils.deprecated(
    RSAPrivateKeyWithSerialization, __name__,
    ("The RSAPrivateKeyWithNumbers interface has been renamed to "
     "RSAPrivateKeyWithSerialization"), utils.DeprecatedIn08)


@six.add_metaclass(abc.ABCMeta)
class RSAPublicKey(object):
    @abc.abstractmethod
    def verifier(self, signature, padding, algorithm):
        """
        Returns an AsymmetricVerificationContext used for verifying signatures.
        """

    @abc.abstractmethod
    def encrypt(self, plaintext, padding):
        """
        Encrypts the given plaintext.
Esempio n. 22
0
import abc

import six

from cryptography import utils
from cryptography.hazmat.primitives import ciphers, hashes
from cryptography.hazmat.primitives.asymmetric import (
    AsymmetricSignatureContext, AsymmetricVerificationContext, dsa, ec,
    padding, rsa)
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
from cryptography.hazmat.primitives.padding import PaddingContext

BlockCipherAlgorithm = utils.deprecated(
    ciphers.BlockCipherAlgorithm, __name__,
    ("The BlockCipherAlgorithm interface has moved to the "
     "cryptography.hazmat.primitives.ciphers module"), utils.DeprecatedIn08)

CipherAlgorithm = utils.deprecated(
    ciphers.CipherAlgorithm, __name__,
    ("The CipherAlgorithm interface has moved to the "
     "cryptography.hazmat.primitives.ciphers module"), utils.DeprecatedIn08)

Mode = utils.deprecated(
    modes.Mode, __name__,
    ("The Mode interface has moved to the "
     "cryptography.hazmat.primitives.ciphers.modes module"),
    utils.DeprecatedIn08)

ModeWithAuthenticationTag = utils.deprecated(
    modes.ModeWithAuthenticationTag, __name__,