Python cryptography.x509 模块,Certificate() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cryptography.x509.Certificate()

项目:cursive    作者:openstack    | 项目源码 | 文件源码
def update(self, certificate):
        """Process the certificate to be verified.

        Raises an exception if the certificate is invalid. Stores it
        otherwise.

        :param certificate: the cryptography certificate to be verified
        :raises: SignatureVerificationError if the certificate is not of the
                 right type or if it is outside its valid date range.
        """
        if not isinstance(certificate, x509.Certificate):
            raise exception.SignatureVerificationError(
                "The certificate must be an x509.Certificate object."
            )

        if self._enforce_valid_dates:
            if not is_within_valid_dates(certificate):
                raise exception.SignatureVerificationError(
                    "The certificate is outside its valid date range."
                )

        self._signed_certificate = certificate
项目:cursive    作者:openstack    | 项目源码 | 文件源码
def test_verify_invalid_certificate(self, mock_utcnow, mock_get_cert):
        mock_utcnow.return_value = datetime.datetime(2017, 1, 1)
        certs = self.load_certificates(
            ['self_signed_cert.pem', 'self_signed_cert.der',
             'orphaned_cert.pem']
        )
        mock_get_cert.side_effect = certs
        cert_uuid = '3'
        trusted_cert_uuids = ['1', '2']
        self.assertRaisesRegex(
            exception.SignatureVerificationError,
            "Certificate chain building failed. Could not locate the "
            "signing certificate for the base certificate in the set of "
            "trusted certificates.",
            certificate_utils.verify_certificate,
            None,
            cert_uuid,
            trusted_cert_uuids
        )
项目:cursive    作者:openstack    | 项目源码 | 文件源码
def test_verify_valid_certificate_with_no_root(self, mock_utcnow,
                                                   mock_get_cert):
        mock_utcnow.return_value = datetime.datetime(2017, 1, 1)

        # Test verifying a valid certificate against an empty list of trusted
        # certificates.
        certs = self.load_certificates(['signed_cert.pem'])
        mock_get_cert.side_effect = certs
        cert_uuid = '3'
        trusted_cert_uuids = []
        self.assertRaisesRegex(
            exception.SignatureVerificationError,
            "Certificate chain building failed. Could not locate the "
            "signing certificate for the base certificate in the set of "
            "trusted certificates.",
            certificate_utils.verify_certificate,
            None,
            cert_uuid,
            trusted_cert_uuids
        )
项目:2FAssassin    作者:maxwellkoh    | 项目源码 | 文件源码
def from_cryptography(cls, crypto_cert):
        """
        Construct based on a ``cryptography`` *crypto_cert*.

        :param crypto_key: A ``cryptography`` X.509 certificate.
        :type crypto_key: ``cryptography.x509.Certificate``

        :rtype: PKey

        .. versionadded:: 17.1.0
        """
        if not isinstance(crypto_cert, x509.Certificate):
            raise TypeError("Must be a certificate")

        cert = cls()
        cert._x509 = crypto_cert._x509
        return cert
项目:2FAssassin    作者:maxwellkoh    | 项目源码 | 文件源码
def from_cryptography(cls, crypto_cert):
        """
        Construct based on a ``cryptography`` *crypto_cert*.

        :param crypto_key: A ``cryptography`` X.509 certificate.
        :type crypto_key: ``cryptography.x509.Certificate``

        :rtype: PKey

        .. versionadded:: 17.1.0
        """
        if not isinstance(crypto_cert, x509.Certificate):
            raise TypeError("Must be a certificate")

        cert = cls()
        cert._x509 = crypto_cert._x509
        return cert
项目:certproxy    作者:geneanet    | 项目源码 | 文件源码
def _assert_admin(self, cert):
        if cert is not None:
            if isinstance(cert, bytes):
                cert = load_certificate(cert_bytes=cert)

            if isinstance(cert, x509.Certificate):
                host = cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value
            else:
                raise TypeError('cert must be a raw certificate in PEM or DER format or an x509.Certificate instance.')

        else:
            logger.warning('Admin command received by unauthentified host.')
            raise HTTPResponse(
                status=401,
                body={'message': 'Authentication required'}
            )

        if host not in self.admin_hosts:
            logger.warning('Host %s unauthorized for admin commands.', host)
            raise HTTPResponse(
                status=403,
                body={'message': 'Host {} unauthorized for admin commands'.format(host)}
            )
项目:solaris-ips    作者:oracle    | 项目源码 | 文件源码
def __verify_x509_signature(self, c, key):
                """Verify the signature of a certificate or CRL 'c' against a
                provided public key 'key'."""

                verifier = key.verifier(
                    c.signature, padding.PKCS1v15(),
                    c.signature_hash_algorithm)

                if isinstance(c, x509.Certificate):
                        data = c.tbs_certificate_bytes
                elif isinstance(c, x509.CertificateRevocationList):
                        data = c.tbs_certlist_bytes
                else:
                        raise AssertionError("Invalid x509 object for "
                            "signature verification: {0}".format(type(c)))

                verifier.update(data)
                try:
                        verifier.verify()
                        return True
                except Exception:
                        return False
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def serial(self):
        warnings.warn(
            "Certificate serial is deprecated, use serial_number instead.",
            utils.DeprecatedIn14,
            stacklevel=2
        )
        return self.serial_number
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:cursive    作者:openstack    | 项目源码 | 文件源码
def __init__(self, certificate_tuples, enforce_valid_dates=True,
                 enforce_signing_extensions=True,
                 enforce_path_length=True):
        self._signing_certificates = []
        for certificate_tuple in certificate_tuples:
            certificate_uuid, certificate = certificate_tuple
            if not isinstance(certificate, x509.Certificate):
                LOG.error(
                    "A signing certificate must be an x509.Certificate object."
                )
                continue

            if enforce_valid_dates:
                if not is_within_valid_dates(certificate):
                    LOG.warning(
                        "Certificate '%s' is outside its valid date range and "
                        "cannot be used as a signing certificate.",
                        certificate_uuid)
                    continue

            if enforce_signing_extensions:
                if not can_sign_certificates(certificate, certificate_uuid):
                    LOG.warning(
                        "Certificate '%s' is not configured to act as a "
                        "signing certificate. It will not be used as a "
                        "signing certificate.",
                        certificate_uuid)
                    continue
            self._signing_certificates.append(certificate_tuple)

        self._signed_certificate = None
        self._enforce_valid_dates = enforce_valid_dates
        self._enforce_path_length = enforce_path_length
项目:cursive    作者:openstack    | 项目源码 | 文件源码
def test_context_verify_invalid_chain_length(self, mock_utcnow):
        mock_utcnow.return_value = datetime.datetime(2017, 11, 1)
        certs = self.load_certificates(
            ['grandparent_cert.pem', 'parent_cert.pem', 'child_cert.pem']
        )
        cert_tuples = [
            ('1', certs[0]),
            ('2', certs[1]),
            ('3', certs[2])
        ]
        cert = self.load_certificate('grandchild_cert.pem')

        context = certificate_utils.CertificateVerificationContext(
            cert_tuples
        )
        context.update(cert)
        self.assertRaisesRegex(
            exception.SignatureVerificationError,
            "Certificate validation failed. The signing certificate '1' is "
            "not configured to support certificate chains of sufficient "
            "length.",
            context.verify
        )

        context = certificate_utils.CertificateVerificationContext(
            cert_tuples,
            enforce_path_length=False
        )
        context.update(cert)
        context.verify()
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:2FAssassin    作者:maxwellkoh    | 项目源码 | 文件源码
def test_convert_to_cryptography_key(self):
        cert = load_certificate(FILETYPE_PEM, intermediate_cert_pem)
        crypto_cert = cert.to_cryptography()

        assert isinstance(crypto_cert, x509.Certificate)
        assert crypto_cert.version.value == cert.get_version()
项目:2FAssassin    作者:maxwellkoh    | 项目源码 | 文件源码
def to_cryptography(self):
        """
        Export as a ``cryptography`` certificate.

        :rtype: ``cryptography.x509.Certificate``

        .. versionadded:: 17.1.0
        """
        from cryptography.hazmat.backends.openssl.x509 import _Certificate
        backend = _get_backend()
        return _Certificate(backend, self._x509)
项目:2FAssassin    作者:maxwellkoh    | 项目源码 | 文件源码
def to_cryptography(self):
        """
        Export as a ``cryptography`` certificate.

        :rtype: ``cryptography.x509.Certificate``

        .. versionadded:: 17.1.0
        """
        from cryptography.hazmat.backends.openssl.x509 import _Certificate
        backend = _get_backend()
        return _Certificate(backend, self._x509)
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:certproxy    作者:geneanet    | 项目源码 | 文件源码
def _get_cert_config_if_allowed(self, domain, cert):
        if cert is not None:
            if isinstance(cert, bytes):
                cert = load_certificate(cert_bytes=cert)

            if isinstance(cert, x509.Certificate):
                host = cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value
            else:
                raise TypeError('cert must be a raw certificate in PEM or DER format or an x509.Certificate instance.')

        else:
            logger.warning('Request received for domain %s by unauthentified host.', domain)
            raise HTTPResponse(
                status=401,
                body={'message': 'Authentication required'}
            )

        certconfig = self.certificates_config.match(domain)

        if certconfig:
            logger.debug('Domain %s matches pattern %s', domain, certconfig.pattern)
            if host in self.admin_hosts or host in certconfig.allowed_hosts:
                return certconfig
            else:
                logger.warning('Host %s unauthorized for domain %s.', host, domain)
                raise HTTPResponse(
                    status=403,
                    body={'message': 'Host {} unauthorized for domain {}'.format(host, domain)}
                )
        else:
            logger.warning('No config matching domain %s found.', domain)
            raise HTTPResponse(
                status=404,
                body={'message': 'No configuration found for domain {}'.format(domain)}
            )
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:slack_scholar    作者:xLeitix    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:slack_scholar    作者:xLeitix    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:slack_scholar    作者:xLeitix    | 项目源码 | 文件源码
def serial(self):
        warnings.warn(
            "Certificate serial is deprecated, use serial_number instead.",
            utils.DeprecatedIn14,
            stacklevel=2
        )
        return self.serial_number
项目:slack_scholar    作者:xLeitix    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:aapns    作者:HDE    | 项目源码 | 文件源码
def gen_certificate(key: rsa.RSAPrivateKey,
                    common_name: str,
                    *,
                    issuer: Optional[str]=None,
                    sign_key: Optional[rsa.RSAPrivateKey]=None) -> x509.Certificate:
    now = datetime.datetime.utcnow()
    return x509.CertificateBuilder().subject_name(
        x509.Name([
            x509.NameAttribute(NameOID.COMMON_NAME, common_name),
        ])
    ).issuer_name(
        x509.Name([
            x509.NameAttribute(
                NameOID.COMMON_NAME,
                issuer or common_name
            )
        ])
    ).not_valid_before(
        now
    ).not_valid_after(
        now + datetime.timedelta(seconds=86400)
    ).serial_number(
        x509.random_serial_number()
    ).public_key(
        key.public_key()
    ).add_extension(
        x509.BasicConstraints(ca=True, path_length=0), critical=True
    ).sign(
        private_key=sign_key or key,
        algorithm=hashes.SHA256(),
        backend=BACKEND
    )
项目:RemoteTree    作者:deNULL    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)
项目:RemoteTree    作者:deNULL    | 项目源码 | 文件源码
def __eq__(self, other):
        if not isinstance(other, x509.Certificate):
            return NotImplemented

        res = self._backend._lib.X509_cmp(self._x509, other._x509)
        return res == 0
项目:RemoteTree    作者:deNULL    | 项目源码 | 文件源码
def serial(self):
        warnings.warn(
            "Certificate serial is deprecated, use serial_number instead.",
            utils.PersistentlyDeprecated,
            stacklevel=2
        )
        return self.serial_number
项目:RemoteTree    作者:deNULL    | 项目源码 | 文件源码
def public_key(self):
        pkey = self._backend._lib.X509_get_pubkey(self._x509)
        if pkey == self._backend._ffi.NULL:
            # Remove errors from the stack.
            self._backend._consume_errors()
            raise ValueError("Certificate public key is of an unknown type")

        pkey = self._backend._ffi.gc(pkey, self._backend._lib.EVP_PKEY_free)

        return self._backend._evp_pkey_to_public_key(pkey)
项目:quickstart-git2s3    作者:aws-quickstart    | 项目源码 | 文件源码
def __repr__(self):
        return "<Certificate(subject={0}, ...)>".format(self.subject)