Ejemplo n.º 1
0
    def dump(self, check_mode=False):
        result = {
            'changed': self.changed,
            'filename': self.path,
            'privatekey': self.privatekey_path,
            'last_update': None,
            'next_update': None,
            'digest': None,
            'issuer_ordered': None,
            'issuer': None,
            'revoked_certificates': [],
        }
        if self.backup_file:
            result['backup_file'] = self.backup_file

        if check_mode:
            result['last_update'] = self.last_update.strftime(TIMESTAMP_FORMAT)
            result['next_update'] = self.next_update.strftime(TIMESTAMP_FORMAT)
            # result['digest'] = crypto_utils.cryptography_oid_to_name(self.crl.signature_algorithm_oid)
            result['digest'] = self.module.params['digest']
            result['issuer_ordered'] = self.issuer
            result['issuer'] = {}
            for k, v in self.issuer:
                result['issuer'][k] = v
            result['revoked_certificates'] = []
            for entry in self.revoked_certificates:
                result['revoked_certificates'].append(self._dump_revoked(entry))
        elif self.crl:
            result['last_update'] = self.crl.last_update.strftime(TIMESTAMP_FORMAT)
            result['next_update'] = self.crl.next_update.strftime(TIMESTAMP_FORMAT)
            try:
                result['digest'] = crypto_utils.cryptography_oid_to_name(self.crl.signature_algorithm_oid)
            except AttributeError:
                # Older cryptography versions don't have signature_algorithm_oid yet
                dotted = crypto_utils._obj2txt(
                    self.crl._backend._lib,
                    self.crl._backend._ffi,
                    self.crl._x509_crl.sig_alg.algorithm
                )
                oid = x509.oid.ObjectIdentifier(dotted)
                result['digest'] = crypto_utils.cryptography_oid_to_name(oid)
            issuer = []
            for attribute in self.crl.issuer:
                issuer.append([crypto_utils.cryptography_oid_to_name(attribute.oid), attribute.value])
            result['issuer_ordered'] = issuer
            result['issuer'] = {}
            for k, v in issuer:
                result['issuer'][k] = v
            result['revoked_certificates'] = []
            for cert in self.crl:
                entry = crypto_utils.cryptography_decode_revoked_certificate(cert)
                result['revoked_certificates'].append(self._dump_revoked(entry))

        if self.return_content:
            result['crl'] = self.crl_content

        return result
Ejemplo n.º 2
0
    def _generate_crl(self):
        backend = default_backend()
        crl = CertificateRevocationListBuilder()

        try:
            crl = crl.issuer_name(
                Name([
                    NameAttribute(
                        crypto_utils.cryptography_name_to_oid(entry[0]),
                        to_text(entry[1])) for entry in self.issuer
                ]))
        except ValueError as e:
            raise CRLError(e)

        crl = crl.last_update(self.last_update)
        crl = crl.next_update(self.next_update)

        if self.update and self.crl:
            new_entries = set([
                self._compress_entry(entry)
                for entry in self.revoked_certificates
            ])
            for entry in self.crl:
                decoded_entry = self._compress_entry(
                    crypto_utils.cryptography_decode_revoked_certificate(
                        entry))
                if decoded_entry not in new_entries:
                    crl = crl.add_revoked_certificate(entry)
        for entry in self.revoked_certificates:
            revoked_cert = RevokedCertificateBuilder()
            revoked_cert = revoked_cert.serial_number(entry['serial_number'])
            revoked_cert = revoked_cert.revocation_date(
                entry['revocation_date'])
            if entry['issuer'] is not None:
                revoked_cert = revoked_cert.add_extension(
                    x509.CertificateIssuer([
                        crypto_utils.cryptography_get_name(name)
                        for name in self.entry['issuer']
                    ]), entry['issuer_critical'])
            if entry['reason'] is not None:
                revoked_cert = revoked_cert.add_extension(
                    x509.CRLReason(entry['reason']), entry['reason_critical'])
            if entry['invalidity_date'] is not None:
                revoked_cert = revoked_cert.add_extension(
                    x509.InvalidityDate(entry['invalidity_date']),
                    entry['invalidity_date_critical'])
            crl = crl.add_revoked_certificate(revoked_cert.build(backend))

        self.crl = crl.sign(self.privatekey, self.digest, backend=backend)
        return self.crl.public_bytes(Encoding.PEM)
Ejemplo n.º 3
0
    def check(self, perms_required=True):
        """Ensure the resource is in its desired state."""

        state_and_perms = super(CRL, self).check(self.module, perms_required)

        if not state_and_perms:
            return False

        if self.crl is None:
            return False

        if self.last_update != self.crl.last_update and not self.ignore_timestamps:
            return False
        if self.next_update != self.crl.next_update and not self.ignore_timestamps:
            return False
        if self.digest.name != self.crl.signature_hash_algorithm.name:
            return False

        want_issuer = [(crypto_utils.cryptography_name_to_oid(entry[0]),
                        entry[1]) for entry in self.issuer]
        if want_issuer != [(sub.oid, sub.value) for sub in self.crl.issuer]:
            return False

        old_entries = [
            self._compress_entry(
                crypto_utils.cryptography_decode_revoked_certificate(cert))
            for cert in self.crl
        ]
        new_entries = [
            self._compress_entry(cert) for cert in self.revoked_certificates
        ]
        if self.update:
            # We don't simply use a set so that duplicate entries are treated correctly
            for entry in new_entries:
                try:
                    old_entries.remove(entry)
                except ValueError:
                    return False
        else:
            if old_entries != new_entries:
                return False

        return True
Ejemplo n.º 4
0
    def get_info(self):
        result = {
            'changed': False,
            'last_update': None,
            'next_update': None,
            'digest': None,
            'issuer_ordered': None,
            'issuer': None,
            'revoked_certificates': [],
        }

        result['last_update'] = self.crl.last_update.strftime(TIMESTAMP_FORMAT)
        result['next_update'] = self.crl.next_update.strftime(TIMESTAMP_FORMAT)
        try:
            result['digest'] = crypto_utils.cryptography_oid_to_name(
                self.crl.signature_algorithm_oid)
        except AttributeError:
            # Older cryptography versions don't have signature_algorithm_oid yet
            dotted = crypto_utils._obj2txt(
                self.crl._backend._lib, self.crl._backend._ffi,
                self.crl._x509_crl.sig_alg.algorithm)
            oid = x509.oid.ObjectIdentifier(dotted)
            result['digest'] = crypto_utils.cryptography_oid_to_name(oid)
        issuer = []
        for attribute in self.crl.issuer:
            issuer.append([
                crypto_utils.cryptography_oid_to_name(attribute.oid),
                attribute.value
            ])
        result['issuer_ordered'] = issuer
        result['issuer'] = {}
        for k, v in issuer:
            result['issuer'][k] = v
        result['revoked_certificates'] = []
        for cert in self.crl:
            entry = crypto_utils.cryptography_decode_revoked_certificate(cert)
            result['revoked_certificates'].append(self._dump_revoked(entry))

        return result