How to use the asn1crypto.pem function in asn1crypto

To help you get started, we’ve selected a few asn1crypto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wbond / asn1crypto / tests / test_pem.py View on Github external
def test_armor_wrong_type2(self):
        with self.assertRaisesRegexp(TypeError, 'der_bytes must be a byte string'):
            pem.armor('CERTIFICATE', '')
github scalyr / scalyr-agent-2 / scalyr_agent / third_party_tls / certvalidator / crl_client.py View on Github external
:param timeout:
        The number of seconds after which an HTTP request should timeout

    :return:
        An asn1crypto.crl.CertificateList object
    """

    if sys.version_info < (3,):
        url = util.iri_to_uri(url)
    request = Request(url)
    request.add_header(b'Accept', b'application/pkix-crl')
    request.add_header(b'User-Agent', user_agent.encode('iso-8859-1'))
    response = urlopen(request, None, timeout)
    data = response.read()
    if pem.detect(data):
        _, _, data = pem.unarmor(data)
    return crl.CertificateList.load(data)
github balena / python-smime / smime / cert.py View on Github external
def to_pem(self):
        """Get the PEM-encoding of the certificate."""
        return pem.armor(self.PEM_MARKERS[0], self.to_der())
github opendxl / opendxl-client-python / dxlclient / _cli / _crypto.py View on Github external
def validate_cert_pem(pem_text, message_on_exception=None):
    """
    Validate that the supplied `pem_text` string contains a PEM-encoded
    certificate

    :param str pem_text: text to validate as a PEM-encoded certificate
    :param str message_on_exception: extra text to add into an exception if
        a validation failure occurs
    :raise Exception: if the `pem_text` does not represent a valid PEM-encoded
        certificate
    """
    try:
        pem_bytes = pem_text if isinstance(pem_text, bytes) \
            else pem_text.encode()
        object_name, _, der_bytes = pem.unarmor(pem_bytes)
        if object_name != "CERTIFICATE":
            raise Exception(
                "Expected CERTIFICATE type for PEM, Received: {}".format(
                    object_name))
        x509.Certificate.load(der_bytes)
    except Exception as ex:
        logger.error("%s. Reason: %s",
                     message_on_exception or
                     "Failed to validate certificate PEM",
                     ex)
        logger.debug("Certificate PEM: %s", pem_text)
        raise
github wbond / certvalidator / certvalidator / path.py View on Github external
An asn1crypto.x509.Certificate object

        :return:
            The current ValidationPath object, for chaining
        """

        if not isinstance(cert, x509.Certificate):
            if not isinstance(cert, byte_cls):
                raise TypeError(pretty_message(
                    '''
                    cert must be a byte string or an
                    asn1crypto.x509.Certificate object, not %s
                    ''',
                    type_name(cert)
                ))
            if pem.detect(cert):
                _, _, cert = pem.unarmor(cert)
            cert = x509.Certificate.load(cert)

        if cert.issuer_serial in self._cert_hashes:
            raise DuplicateCertificateError()

        self._cert_hashes.add(cert.issuer_serial)
        self._certs.append(cert)

        return self
github wbond / certvalidator / certvalidator / path.py View on Github external
:return:
            The current ValidationPath object, for chaining
        """

        if not isinstance(cert, x509.Certificate):
            if not isinstance(cert, byte_cls):
                raise TypeError(pretty_message(
                    '''
                    cert must be a byte string or an
                    asn1crypto.x509.Certificate object, not %s
                    ''',
                    type_name(cert)
                ))
            if pem.detect(cert):
                _, _, cert = pem.unarmor(cert)
            cert = x509.Certificate.load(cert)

        if cert.issuer_serial in self._cert_hashes:
            raise DuplicateCertificateError()

        self._cert_hashes.add(cert.issuer_serial)
        self._certs.append(cert)

        return self