Python OpenSSL.crypto.load_pkcs7_data() Examples

The following are 25 code examples of OpenSSL.crypto.load_pkcs7_data(). 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 OpenSSL.crypto , or try the search function .
Example #1
Source File: cert.py    From apkutils with MIT License 7 votes vote down vote up
def _parse(self, buff, digestalgo):
        pkcs7 = crypto.load_pkcs7_data(crypto.FILETYPE_ASN1, buff)

        certs_stack = _ffi.NULL
        if pkcs7.type_is_signed():
            certs_stack = pkcs7._pkcs7.d.sign.cert
        elif pkcs7.type_is_signedAndEnveloped():
            certs_stack = pkcs7._pkcs7.d.signed_and_enveloped.cert

        pycerts = []

        for i in range(_lib.sk_X509_num(certs_stack)):
            tmp = _lib.X509_dup(_lib.sk_X509_value(certs_stack, i))
            pycert = X509._from_raw_x509_ptr(tmp)
            pycerts.append(pycert)

        if not pycerts:
            return None

        for cert in pycerts:
            name = str(cert.get_subject())[19:-2].replace('/', ', ')
            checksum = cert.digest(digestalgo).decode().replace(':', '')

            self.content.append((name, checksum)) 
Example #2
Source File: test_snakeoil_ca.py    From barbican with Apache License 2.0 6 votes vote down vote up
def test_create_ca(self):
        subca_dict = self._create_subca()
        self.assertEqual("sub ca1", subca_dict.get(cm.INFO_NAME))
        self.assertIsNotNone(subca_dict.get(cm.INFO_EXPIRATION))
        self.assertIsNotNone(subca_dict.get(cm.PLUGIN_CA_ID))
        ca_cert = subca_dict.get(cm.INFO_CA_SIGNING_CERT)
        self.assertIsNotNone(ca_cert)

        intermediates = subca_dict.get(cm.INFO_INTERMEDIATES)
        self.assertIsNotNone(intermediates)

        cacert = crypto.load_certificate(crypto.FILETYPE_PEM, ca_cert)
        subject = cacert.get_subject()
        self.assertEqual(
            "subordinate ca signing cert",
            subject.CN)

        pkcs7 = crypto.load_pkcs7_data(crypto.FILETYPE_PEM, intermediates)
        self.assertTrue(pkcs7.type_is_signed())

        # TODO(alee) Verify that ca cert is signed by parent CA 
Example #3
Source File: test_snakeoil_ca.py    From sgx-kms with Apache License 2.0 6 votes vote down vote up
def test_create_ca(self):
        subca_dict = self._create_subca()
        self.assertEqual("sub ca1", subca_dict.get(cm.INFO_NAME))
        self.assertIsNotNone(subca_dict.get(cm.INFO_EXPIRATION))
        self.assertIsNotNone(subca_dict.get(cm.PLUGIN_CA_ID))
        ca_cert = subca_dict.get(cm.INFO_CA_SIGNING_CERT)
        self.assertIsNotNone(ca_cert)

        intermediates = subca_dict.get(cm.INFO_INTERMEDIATES)
        self.assertIsNotNone(intermediates)

        cacert = crypto.load_certificate(crypto.FILETYPE_PEM, ca_cert)
        subject = cacert.get_subject()
        self.assertEqual(
            "subordinate ca signing cert",
            subject.CN)

        pkcs7 = crypto.load_pkcs7_data(crypto.FILETYPE_PEM, intermediates)
        self.assertTrue(pkcs7.type_is_signed())

        # TODO(alee) Verify that ca cert is signed by parent CA 
Example #4
Source File: test_certificate_orders.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def verify_valid_intermediates(self, secret_ref):
        secret_resp = self.secret_behaviors.get_secret(
            secret_ref,
            "application/pkix-cert")
        self.assertIsNotNone(secret_resp)
        self.assertIsNotNone(secret_resp.content)
        cert_chain = secret_resp.content
        crypto.load_pkcs7_data(crypto.FILETYPE_PEM, cert_chain) 
Example #5
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_attribute(self):
        """
        If an attribute other than one of the methods tested here is accessed
        on an instance of `PKCS7`, `AttributeError` is raised.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        with pytest.raises(AttributeError):
            pkcs7.foo 
Example #6
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_get_type_name(self):
        """
        `PKCS7.get_type_name` returns a `str` giving the
        type name.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        assert pkcs7.get_type_name() == b'pkcs7-signedData' 
Example #7
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_type_is_data(self):
        """
        `PKCS7.type_is_data` returns `False` if the PKCS7 object is not of
        the type data.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        assert not pkcs7.type_is_data() 
Example #8
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_type_is_signed_and_enveloped(self):
        """
        `PKCS7.type_is_signedAndEnveloped` returns `False`
        if the PKCS7 object is not of the type *signed and enveloped*.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        assert not pkcs7.type_is_signedAndEnveloped() 
Example #9
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_type_is_signed(self):
        """
        `PKCS7.type_is_signed` returns `True` if the PKCS7 object is of
        the type *signed*.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        assert pkcs7.type_is_signed() 
Example #10
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_load_pkcs7_type_invalid(self):
        """
        If the type passed to `load_pkcs7_data`, `ValueError` is raised.
        """
        with pytest.raises(ValueError):
            load_pkcs7_data(object(), b"foo") 
Example #11
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_load_pkcs7_data_invalid(self):
        """
        If the data passed to `load_pkcs7_data` is invalid, `Error` is raised.
        """
        with pytest.raises(Error):
            load_pkcs7_data(FILETYPE_PEM, b"foo") 
Example #12
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_load_pkcs7_data_asn1(self):
        """
        `load_pkcs7_data` accepts a bytes containing ASN1 data representing
        PKCS#7 and returns an instance of `PKCS7`.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_ASN1, pkcs7DataASN1)
        assert isinstance(pkcs7, PKCS7) 
Example #13
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_load_pkcs7_data_pem(self):
        """
        `load_pkcs7_data` accepts a PKCS#7 string and returns an instance of
        `PKCS`.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        assert isinstance(pkcs7, PKCS7) 
Example #14
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_load_pkcs7_data_pem(self):
        """
        :py:obj:`load_pkcs7_data` accepts a PKCS#7 string and returns an instance of
        :py:obj:`PKCS7Type`.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertTrue(isinstance(pkcs7, PKCS7Type)) 
Example #15
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_attribute(self):
        """
        If an attribute other than one of the methods tested here is accessed on
        an instance of :py:obj:`PKCS7Type`, :py:obj:`AttributeError` is raised.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertRaises(AttributeError, getattr, pkcs7, "foo") 
Example #16
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_get_type_name(self):
        """
        :py:obj:`PKCS7Type.get_type_name` returns a :py:obj:`str` giving the type name.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertEquals(pkcs7.get_type_name(), b('pkcs7-signedData')) 
Example #17
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_type_is_data_wrong_args(self):
        """
        :py:obj:`PKCS7Type.type_is_data` raises :py:obj:`TypeError` if called with any
        arguments.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertRaises(TypeError, pkcs7.type_is_data, None) 
Example #18
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_type_is_data(self):
        """
        :py:obj:`PKCS7Type.type_is_data` returns :py:obj:`False` if the PKCS7 object is not of
        the type data.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertFalse(pkcs7.type_is_data()) 
Example #19
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_type_is_signedAndEnveloped(self):
        """
        :py:obj:`PKCS7Type.type_is_signedAndEnveloped` returns :py:obj:`False` if the PKCS7
        object is not of the type *signed and enveloped*.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertFalse(pkcs7.type_is_signedAndEnveloped()) 
Example #20
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_type_is_signedAndEnveloped_wrong_args(self):
        """
        :py:obj:`PKCS7Type.type_is_signedAndEnveloped` raises :py:obj:`TypeError` if called
        with any arguments.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertRaises(TypeError, pkcs7.type_is_signedAndEnveloped, None) 
Example #21
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_type_is_enveloped(self):
        """
        :py:obj:`PKCS7Type.type_is_enveloped` returns :py:obj:`False` if the PKCS7 object is
        not of the type *enveloped*.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertFalse(pkcs7.type_is_enveloped()) 
Example #22
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_type_is_signed(self):
        """
        :py:obj:`PKCS7Type.type_is_signed` returns :py:obj:`True` if the PKCS7 object is of
        the type *signed*.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertTrue(pkcs7.type_is_signed()) 
Example #23
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_type_is_signed_wrong_args(self):
        """
        :py:obj:`PKCS7Type.type_is_signed` raises :py:obj:`TypeError` if called with any
        arguments.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data)
        self.assertRaises(TypeError, pkcs7.type_is_signed, None) 
Example #24
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_load_pkcs7_data_invalid(self):
        """
        If the data passed to :py:obj:`load_pkcs7_data` is invalid,
        :py:obj:`Error` is raised.
        """
        self.assertRaises(Error, load_pkcs7_data, FILETYPE_PEM, b"foo") 
Example #25
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_load_pkcs7_data_asn1(self):
        """
        :py:obj:`load_pkcs7_data` accepts a bytes containing ASN1 data
        representing PKCS#7 and returns an instance of :py:obj`PKCS7Type`.
        """
        pkcs7 = load_pkcs7_data(FILETYPE_ASN1, pkcs7DataASN1)
        self.assertTrue(isinstance(pkcs7, PKCS7Type))