Python cryptography.x509.ReasonFlags() Examples

The following are 10 code examples of cryptography.x509.ReasonFlags(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cryptography.x509 , or try the search function .
Example #1
Source File: models.py    From django-ca with GNU General Public License v3.0 6 votes vote down vote up
def get_revocation(self):
        if self.revoked is False:
            raise ValueError('Certificate is not revoked.')

        revoked_cert = x509.RevokedCertificateBuilder().serial_number(
            self.x509.serial_number).revocation_date(self.revoked_date)

        reason = self.get_revocation_reason()
        if reason != x509.ReasonFlags.unspecified:
            # RFC 5270, 5.3.1: "reason code CRL entry extension SHOULD be absent instead of using the
            # unspecified (0) reasonCode value"
            revoked_cert = revoked_cert.add_extension(x509.CRLReason(reason), critical=False)

        compromised = self.get_compromised_time()
        if compromised:
            # RFC 5280, 5.3.2 says that this extension MUST be non-critical
            revoked_cert = revoked_cert.add_extension(x509.InvalidityDate(compromised), critical=False)

        return revoked_cert.build(default_backend()) 
Example #2
Source File: extensions.py    From django-ca with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, data=None):
        if data is None:
            data = {}

        if isinstance(data, x509.DistributionPoint):
            self.full_name = _gnl_or_empty(data.full_name)
            self.relative_name = data.relative_name
            self.crl_issuer = _gnl_or_empty(data.crl_issuer)
            self.reasons = data.reasons
        elif isinstance(data, dict):
            self.full_name = _gnl_or_empty(data.get('full_name'))
            self.relative_name = data.get('relative_name')
            self.crl_issuer = _gnl_or_empty(data.get('crl_issuer'))
            self.reasons = data.get('reasons')

            if self.full_name is not None and self.relative_name is not None:
                raise ValueError('full_name and relative_name cannot both have a value')

            if self.relative_name is not None:
                self.relative_name = x509_relative_name(self.relative_name)
            if self.reasons is not None:
                self.reasons = frozenset([x509.ReasonFlags[r] for r in self.reasons])
        else:
            raise ValueError('data must be x509.DistributionPoint or dict') 
Example #3
Source File: models.py    From django-ca with GNU General Public License v3.0 5 votes vote down vote up
def get_revocation_reason(self):
        """Get the revocation reason of this certificate."""
        if self.revoked is False:
            return

        return x509.ReasonFlags[self.revoked_reason] 
Example #4
Source File: models.py    From django-ca with GNU General Public License v3.0 5 votes vote down vote up
def revoke(self, reason='', compromised=None):
        if not reason:
            reason = ReasonFlags.unspecified

        pre_revoke_cert.send(sender=self.__class__, cert=self, reason=reason)

        self.revoked = True
        self.revoked_date = timezone.now()
        self.revoked_reason = reason.name
        self.compromised = compromised
        self.save()

        post_revoke_cert.send(sender=self.__class__, cert=self) 
Example #5
Source File: tests_models.py    From django-ca with GNU General Public License v3.0 5 votes vote down vote up
def test_get_revocation_reason(self):
        cert = self.certs['child-cert']
        self.assertIsNone(cert.get_revocation_reason())

        for reason in ReasonFlags:
            cert.revoke(reason)
            got = cert.get_revocation_reason()
            self.assertIsInstance(got, x509.ReasonFlags)
            self.assertEqual(got.name, reason.name) 
Example #6
Source File: ocsp.py    From teleport with Apache License 2.0 4 votes vote down vote up
def __init__(self, cert, issuer, algorithm, cert_status, this_update,
                 next_update, revocation_time, revocation_reason):
        if (
            not isinstance(cert, x509.Certificate) or
            not isinstance(issuer, x509.Certificate)
        ):
            raise TypeError("cert and issuer must be a Certificate")

        _verify_algorithm(algorithm)
        if not isinstance(this_update, datetime.datetime):
            raise TypeError("this_update must be a datetime object")
        if (
            next_update is not None and
            not isinstance(next_update, datetime.datetime)
        ):
            raise TypeError("next_update must be a datetime object or None")

        self._cert = cert
        self._issuer = issuer
        self._algorithm = algorithm
        self._this_update = this_update
        self._next_update = next_update

        if not isinstance(cert_status, OCSPCertStatus):
            raise TypeError(
                "cert_status must be an item from the OCSPCertStatus enum"
            )
        if cert_status is not OCSPCertStatus.REVOKED:
            if revocation_time is not None:
                raise ValueError(
                    "revocation_time can only be provided if the certificate "
                    "is revoked"
                )
            if revocation_reason is not None:
                raise ValueError(
                    "revocation_reason can only be provided if the certificate"
                    " is revoked"
                )
        else:
            if not isinstance(revocation_time, datetime.datetime):
                raise TypeError("revocation_time must be a datetime object")

            revocation_time = _convert_to_naive_utc_time(revocation_time)
            if revocation_time < _EARLIEST_UTC_TIME:
                raise ValueError('The revocation_time must be on or after'
                                 ' 1950 January 1.')

            if (
                revocation_reason is not None and
                not isinstance(revocation_reason, x509.ReasonFlags)
            ):
                raise TypeError(
                    "revocation_reason must be an item from the ReasonFlags "
                    "enum or None"
                )

        self._cert_status = cert_status
        self._revocation_time = revocation_time
        self._revocation_reason = revocation_reason 
Example #7
Source File: ocsp.py    From teleport with Apache License 2.0 4 votes vote down vote up
def __init__(self, cert, issuer, algorithm, cert_status, this_update,
                 next_update, revocation_time, revocation_reason):
        if (
            not isinstance(cert, x509.Certificate) or
            not isinstance(issuer, x509.Certificate)
        ):
            raise TypeError("cert and issuer must be a Certificate")

        _verify_algorithm(algorithm)
        if not isinstance(this_update, datetime.datetime):
            raise TypeError("this_update must be a datetime object")
        if (
            next_update is not None and
            not isinstance(next_update, datetime.datetime)
        ):
            raise TypeError("next_update must be a datetime object or None")

        self._cert = cert
        self._issuer = issuer
        self._algorithm = algorithm
        self._this_update = this_update
        self._next_update = next_update

        if not isinstance(cert_status, OCSPCertStatus):
            raise TypeError(
                "cert_status must be an item from the OCSPCertStatus enum"
            )
        if cert_status is not OCSPCertStatus.REVOKED:
            if revocation_time is not None:
                raise ValueError(
                    "revocation_time can only be provided if the certificate "
                    "is revoked"
                )
            if revocation_reason is not None:
                raise ValueError(
                    "revocation_reason can only be provided if the certificate"
                    " is revoked"
                )
        else:
            if not isinstance(revocation_time, datetime.datetime):
                raise TypeError("revocation_time must be a datetime object")

            revocation_time = _convert_to_naive_utc_time(revocation_time)
            if revocation_time < _EARLIEST_UTC_TIME:
                raise ValueError('The revocation_time must be on or after'
                                 ' 1950 January 1.')

            if (
                revocation_reason is not None and
                not isinstance(revocation_reason, x509.ReasonFlags)
            ):
                raise TypeError(
                    "revocation_reason must be an item from the ReasonFlags "
                    "enum or None"
                )

        self._cert_status = cert_status
        self._revocation_time = revocation_time
        self._revocation_reason = revocation_reason 
Example #8
Source File: ocsp.py    From learn_python3_spider with MIT License 4 votes vote down vote up
def __init__(self, cert, issuer, algorithm, cert_status, this_update,
                 next_update, revocation_time, revocation_reason):
        if (
            not isinstance(cert, x509.Certificate) or
            not isinstance(issuer, x509.Certificate)
        ):
            raise TypeError("cert and issuer must be a Certificate")

        _verify_algorithm(algorithm)
        if not isinstance(this_update, datetime.datetime):
            raise TypeError("this_update must be a datetime object")
        if (
            next_update is not None and
            not isinstance(next_update, datetime.datetime)
        ):
            raise TypeError("next_update must be a datetime object or None")

        self._cert = cert
        self._issuer = issuer
        self._algorithm = algorithm
        self._this_update = this_update
        self._next_update = next_update

        if not isinstance(cert_status, OCSPCertStatus):
            raise TypeError(
                "cert_status must be an item from the OCSPCertStatus enum"
            )
        if cert_status is not OCSPCertStatus.REVOKED:
            if revocation_time is not None:
                raise ValueError(
                    "revocation_time can only be provided if the certificate "
                    "is revoked"
                )
            if revocation_reason is not None:
                raise ValueError(
                    "revocation_reason can only be provided if the certificate"
                    " is revoked"
                )
        else:
            if not isinstance(revocation_time, datetime.datetime):
                raise TypeError("revocation_time must be a datetime object")

            revocation_time = _convert_to_naive_utc_time(revocation_time)
            if revocation_time < _EARLIEST_UTC_TIME:
                raise ValueError('The revocation_time must be on or after'
                                 ' 1950 January 1.')

            if (
                revocation_reason is not None and
                not isinstance(revocation_reason, x509.ReasonFlags)
            ):
                raise TypeError(
                    "revocation_reason must be an item from the ReasonFlags "
                    "enum or None"
                )

        self._cert_status = cert_status
        self._revocation_time = revocation_time
        self._revocation_reason = revocation_reason 
Example #9
Source File: ocsp.py    From quickstart-redhat-openshift with Apache License 2.0 4 votes vote down vote up
def __init__(self, cert, issuer, algorithm, cert_status, this_update,
                 next_update, revocation_time, revocation_reason):
        if (
            not isinstance(cert, x509.Certificate) or
            not isinstance(issuer, x509.Certificate)
        ):
            raise TypeError("cert and issuer must be a Certificate")

        _verify_algorithm(algorithm)
        if not isinstance(this_update, datetime.datetime):
            raise TypeError("this_update must be a datetime object")
        if (
            next_update is not None and
            not isinstance(next_update, datetime.datetime)
        ):
            raise TypeError("next_update must be a datetime object or None")

        self._cert = cert
        self._issuer = issuer
        self._algorithm = algorithm
        self._this_update = this_update
        self._next_update = next_update

        if not isinstance(cert_status, OCSPCertStatus):
            raise TypeError(
                "cert_status must be an item from the OCSPCertStatus enum"
            )
        if cert_status is not OCSPCertStatus.REVOKED:
            if revocation_time is not None:
                raise ValueError(
                    "revocation_time can only be provided if the certificate "
                    "is revoked"
                )
            if revocation_reason is not None:
                raise ValueError(
                    "revocation_reason can only be provided if the certificate"
                    " is revoked"
                )
        else:
            if not isinstance(revocation_time, datetime.datetime):
                raise TypeError("revocation_time must be a datetime object")

            revocation_time = _convert_to_naive_utc_time(revocation_time)
            if revocation_time < _EARLIEST_UTC_TIME:
                raise ValueError('The revocation_time must be on or after'
                                 ' 1950 January 1.')

            if (
                revocation_reason is not None and
                not isinstance(revocation_reason, x509.ReasonFlags)
            ):
                raise TypeError(
                    "revocation_reason must be an item from the ReasonFlags "
                    "enum or None"
                )

        self._cert_status = cert_status
        self._revocation_time = revocation_time
        self._revocation_reason = revocation_reason 
Example #10
Source File: ocsp.py    From Carnets with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def __init__(self, cert, issuer, algorithm, cert_status, this_update,
                 next_update, revocation_time, revocation_reason):
        if (
            not isinstance(cert, x509.Certificate) or
            not isinstance(issuer, x509.Certificate)
        ):
            raise TypeError("cert and issuer must be a Certificate")

        _verify_algorithm(algorithm)
        if not isinstance(this_update, datetime.datetime):
            raise TypeError("this_update must be a datetime object")
        if (
            next_update is not None and
            not isinstance(next_update, datetime.datetime)
        ):
            raise TypeError("next_update must be a datetime object or None")

        self._cert = cert
        self._issuer = issuer
        self._algorithm = algorithm
        self._this_update = this_update
        self._next_update = next_update

        if not isinstance(cert_status, OCSPCertStatus):
            raise TypeError(
                "cert_status must be an item from the OCSPCertStatus enum"
            )
        if cert_status is not OCSPCertStatus.REVOKED:
            if revocation_time is not None:
                raise ValueError(
                    "revocation_time can only be provided if the certificate "
                    "is revoked"
                )
            if revocation_reason is not None:
                raise ValueError(
                    "revocation_reason can only be provided if the certificate"
                    " is revoked"
                )
        else:
            if not isinstance(revocation_time, datetime.datetime):
                raise TypeError("revocation_time must be a datetime object")

            revocation_time = _convert_to_naive_utc_time(revocation_time)
            if revocation_time < _EARLIEST_UTC_TIME:
                raise ValueError('The revocation_time must be on or after'
                                 ' 1950 January 1.')

            if (
                revocation_reason is not None and
                not isinstance(revocation_reason, x509.ReasonFlags)
            ):
                raise TypeError(
                    "revocation_reason must be an item from the ReasonFlags "
                    "enum or None"
                )

        self._cert_status = cert_status
        self._revocation_time = revocation_time
        self._revocation_reason = revocation_reason