当前位置: 首页>>代码示例>>Python>>正文


Python OpenSSL.crypto方法代码示例

本文整理汇总了Python中OpenSSL.crypto方法的典型用法代码示例。如果您正苦于以下问题:Python OpenSSL.crypto方法的具体用法?Python OpenSSL.crypto怎么用?Python OpenSSL.crypto使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OpenSSL的用法示例。


在下文中一共展示了OpenSSL.crypto方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_validate_cert

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def test_validate_cert(self, circuits_app, https_url, expected_results):
        """ Test the functions return type 'Certificate'
            When the function is successful in parsing the URL
            It should return a value we can parse into a certificate

            OpenSSL.crypto.load_certificate should be able to get a valid certificate

            When the function isint successful -- None"""
        function_params = {
            "https_url": https_url
        }
        # If we expected the result to be unsuccesful it should raise an error
        if expected_results['successful'] == False:
            with pytest.raises(Exception):
                call_utilities_extract_ssl_cert_from_url_function(circuits_app, function_params)

        else:

            results = call_utilities_extract_ssl_cert_from_url_function(circuits_app, function_params)
            if results['successful']:
                assert (isinstance(
                    OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, json.loads(results['certificate'])),
                    X509))  # Assert our successful results are of type X509
            else:
                assert (isinstance(results['certificate'], type(None))) 
开发者ID:ibmresilient,项目名称:resilient-community-apps,代码行数:27,代码来源:test_utilities_extract_ssl_cert_from_url.py

示例2: certificate_to_string

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def certificate_to_string(certificate: TS.X509) -> str:
    """
    Take an x509 certificate and encode it to a string suitable for adding to
    XML responses.

    :param certificate: A certificate,
        perhaps loaded from :func:`certificate_from_file`.
    """
    pem_bytes = OpenSSL.crypto.dump_certificate(
        OpenSSL.crypto.FILETYPE_PEM, certificate)
    return ''.join(pem_bytes.decode('ascii').strip().split('\n')[1:-1]) 
开发者ID:timheap,项目名称:flask-saml2,代码行数:13,代码来源:utils.py

示例3: certificate_from_string

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def certificate_from_string(
    certificate: str,
    format=OpenSSL.crypto.FILETYPE_PEM,
) -> TS.X509:
    """
    Load an X509 certificate from a string. This just strips off the header and
    footer text.

    :param str: A certificate string.
    :param format: The format of the certificate, from :doc:`OpenSSL:api/crypto`.
    """
    return OpenSSL.crypto.load_certificate(format, certificate) 
开发者ID:timheap,项目名称:flask-saml2,代码行数:14,代码来源:utils.py

示例4: certificate_from_file

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def certificate_from_file(
    filename: Union[str, pathlib.Path],
    format=OpenSSL.crypto.FILETYPE_PEM,
) -> TS.X509:
    """Load an X509 certificate from ``filename``.

    :param filename: The path to the certificate on disk.
    :param format: The format of the certificate, from :doc:`OpenSSL:api/crypto`.
    """
    with open(filename, 'r') as handle:
        return certificate_from_string(handle.read(), format) 
开发者ID:timheap,项目名称:flask-saml2,代码行数:13,代码来源:utils.py

示例5: private_key_from_string

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def private_key_from_string(
    private_key: str,
    format=OpenSSL.crypto.FILETYPE_PEM,
) -> TS.PKey:
    """Load a private key from a string.

    :param str: A private key string.
    :param format: The format of the private key, from :doc:`OpenSSL:api/crypto`.
    """
    return OpenSSL.crypto.load_privatekey(format, private_key) 
开发者ID:timheap,项目名称:flask-saml2,代码行数:12,代码来源:utils.py

示例6: private_key_from_file

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def private_key_from_file(
    filename: Union[str, pathlib.Path],
    format=OpenSSL.crypto.FILETYPE_PEM,
) -> TS.PKey:
    """Load a private key from ``filename``.

    :param filename: The path to the private key on disk.
    :param format: The format of the private key, from :doc:`OpenSSL:api/crypto`.
    """
    with open(filename, 'r') as handle:
        return private_key_from_string(handle.read(), format) 
开发者ID:timheap,项目名称:flask-saml2,代码行数:13,代码来源:utils.py

示例7: _parse_cert

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def _parse_cert(self, content: str) -> X509:
        return OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, content.encode()) 
开发者ID:andreroggeri,项目名称:pynubank,代码行数:4,代码来源:certificate_generator.py

示例8: _gen_cert

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def _gen_cert(self, key, cert):
        p12 = OpenSSL.crypto.PKCS12()
        p12.set_privatekey(key)
        p12.set_certificate(cert)

        return p12 
开发者ID:andreroggeri,项目名称:pynubank,代码行数:8,代码来源:certificate_generator.py

示例9: _generate_key

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def _generate_key(self):
        key = OpenSSL.crypto.PKey()
        key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)

        return key 
开发者ID:andreroggeri,项目名称:pynubank,代码行数:7,代码来源:certificate_generator.py

示例10: _get_public_key

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def _get_public_key(self, key) -> str:
        return OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM, key).decode() 
开发者ID:andreroggeri,项目名称:pynubank,代码行数:4,代码来源:certificate_generator.py

示例11: get_subj_alt_name

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def get_subj_alt_name(peer_cert):
        '''
        Copied from ndg.httpsclient.ssl_peer_verification.ServerSSLCertVerification
        Extract subjectAltName DNS name settings from certificate extensions
        @param peer_cert: peer certificate in SSL connection.  subjectAltName
        settings if any will be extracted from this
        @type peer_cert: OpenSSL.crypto.X509
        '''
        # Search through extensions
        dns_name = []
        general_names = SubjectAltName()
        for i in range(peer_cert.get_extension_count()):
            ext = peer_cert.get_extension(i)
            ext_name = ext.get_short_name()
            if ext_name == b"subjectAltName":
                # PyOpenSSL returns extension data in ASN.1 encoded form
                ext_dat = ext.get_data()
                decoded_dat = der_decoder.decode(ext_dat, asn1Spec=general_names)

                for name in decoded_dat:
                    if isinstance(name, SubjectAltName):
                        for entry in range(len(name)):
                            component = name.getComponentByPosition(entry)
                            n = bytes(component.getComponent())
                            if n.startswith(b"*"):
                                continue
                            dns_name.append(n)
        return dns_name 
开发者ID:miketwes,项目名称:XX-Net-mini,代码行数:30,代码来源:connect_creator.py

示例12: create_ca

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def create_ca():
        key = OpenSSL.crypto.PKey()
        key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
        ca = OpenSSL.crypto.X509()
        ca.set_version(2)
        ca.set_serial_number(0)
        subj = ca.get_subject()
        subj.countryName = 'CN'
        subj.stateOrProvinceName = 'Internet'
        subj.localityName = 'Cernet'
        subj.organizationName = CertUtil.ca_vendor
        # Log generated time.
        subj.organizationalUnitName = '%s Root - %d' % (CertUtil.ca_vendor, int(time.time()))
        subj.commonName = '%s XX-Net' % CertUtil.ca_vendor
        ca.gmtime_adj_notBefore(- 3600 * 24)
        ca.gmtime_adj_notAfter(CertUtil.ca_validity - 3600 * 24)
        ca.set_issuer(subj)
        ca.set_subject(subj)
        ca.set_pubkey(key)
        ca.add_extensions([
            OpenSSL.crypto.X509Extension(
                b'basicConstraints', False, b'CA:TRUE', subject=ca, issuer=ca)
            ])
        ca.sign(key, CertUtil.ca_digest)
        #xlog.debug("CA key:%s", key)
        xlog.info("create CA")
        return key, ca 
开发者ID:miketwes,项目名称:XX-Net-mini,代码行数:29,代码来源:cert_util.py

示例13: generate_ca_file

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def generate_ca_file():
        xlog.info("generate CA file:%s", CertUtil.ca_keyfile)
        key, ca = CertUtil.create_ca()
        with open(CertUtil.ca_certfile, 'wb') as fp:
            fp.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
        with open(CertUtil.ca_keyfile, 'wb') as fp:
            fp.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
            fp.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)) 
开发者ID:miketwes,项目名称:XX-Net-mini,代码行数:10,代码来源:cert_util.py

示例14: generate_cert_keyfile

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def generate_cert_keyfile():
        xlog.info("generate certs's key file:%s", CertUtil.cert_keyfile)
        pkey = OpenSSL.crypto.PKey()
        pkey.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
        with open(CertUtil.cert_keyfile, 'wb') as fp:
            fp.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, pkey))
            fp.write(OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM, pkey))
        CertUtil.cert_publickey = pkey 
开发者ID:miketwes,项目名称:XX-Net-mini,代码行数:10,代码来源:cert_util.py

示例15: _get_old_cert

# 需要导入模块: import OpenSSL [as 别名]
# 或者: from OpenSSL import crypto [as 别名]
def _get_old_cert(commonname):
        certfile = os.path.join(CertUtil.ca_certdir, utils.to_str(commonname) + '.crt')
        if os.path.exists(certfile):
            with open(certfile, 'rb') as fp:
                cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, fp.read())
            if datetime.datetime.strptime(utils.to_str(cert.get_notAfter()), '%Y%m%d%H%M%SZ') < datetime.datetime.utcnow() + datetime.timedelta(days=30):
                try:
                    os.remove(certfile)
                except OSError as e:
                    xlog.warning('CertUtil._get_old_cert failed: unable to remove outdated cert, %r', e)
                else:
                    return
                # well, have to use the old one
            return certfile 
开发者ID:miketwes,项目名称:XX-Net-mini,代码行数:16,代码来源:cert_util.py


注:本文中的OpenSSL.crypto方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。