Ejemplo n.º 1
0
    def _generate_csr(self):
        csr = cryptography.x509.CertificateSigningRequestBuilder()
        try:
            csr = csr.subject_name(cryptography.x509.Name([
                cryptography.x509.NameAttribute(crypto_utils.cryptography_get_name_oid(entry[0]), to_text(entry[1])) for entry in self.subject
            ]))
        except ValueError as e:
            raise CertificateSigningRequestError(e)

        if self.subjectAltName:
            csr = csr.add_extension(cryptography.x509.SubjectAlternativeName([
                crypto_utils.cryptography_get_name(name) for name in self.subjectAltName
            ]), critical=self.subjectAltName_critical)

        if self.keyUsage:
            params = crypto_utils.cryptography_parse_key_usage_params(self.keyUsage)
            csr = csr.add_extension(cryptography.x509.KeyUsage(**params), critical=self.keyUsage_critical)

        if self.extendedKeyUsage:
            usages = [crypto_utils.cryptography_get_ext_keyusage(usage) for usage in self.extendedKeyUsage]
            csr = csr.add_extension(cryptography.x509.ExtendedKeyUsage(usages), critical=self.extendedKeyUsage_critical)

        if self.basicConstraints:
            params = {}
            ca, path_length = crypto_utils.cryptography_get_basic_constraints(self.basicConstraints)
            csr = csr.add_extension(cryptography.x509.BasicConstraints(ca, path_length), critical=self.basicConstraints_critical)

        if self.ocspMustStaple:
            try:
                # This only works with cryptography >= 2.1
                csr = csr.add_extension(cryptography.x509.TLSFeature([cryptography.x509.TLSFeatureType.status_request]), critical=self.ocspMustStaple_critical)
            except AttributeError as dummy:
                csr = csr.add_extension(
                    cryptography.x509.UnrecognizedExtension(CRYPTOGRAPHY_MUST_STAPLE_NAME, CRYPTOGRAPHY_MUST_STAPLE_VALUE),
                    critical=self.ocspMustStaple_critical
                )

        digest = None
        if self.digest == 'sha256':
            digest = cryptography.hazmat.primitives.hashes.SHA256()
        elif self.digest == 'sha384':
            digest = cryptography.hazmat.primitives.hashes.SHA384()
        elif self.digest == 'sha512':
            digest = cryptography.hazmat.primitives.hashes.SHA512()
        elif self.digest == 'sha1':
            digest = cryptography.hazmat.primitives.hashes.SHA1()
        elif self.digest == 'md5':
            digest = cryptography.hazmat.primitives.hashes.MD5()
        # FIXME
        else:
            raise CertificateSigningRequestError('Unsupported digest "{0}"'.format(self.digest))
        self.request = csr.sign(self.privatekey, digest, self.cryptography_backend)

        return self.request.public_bytes(cryptography.hazmat.primitives.serialization.Encoding.PEM)
Ejemplo n.º 2
0
 def _check_subject(csr):
     subject = [(crypto_utils.cryptography_get_name_oid(entry[0]),
                 entry[1]) for entry in self.subject]
     current_subject = [(sub.oid, sub.value) for sub in csr.subject]
     return set(subject) == set(current_subject)