def import_ssl(keyring, certdata, cn=None, email=None):
    """
    certdata should be pem-formated certificate data to be added to the
    keyring. The return value will be the sha1 fingerprint of the certificate
    added.
    """

    # Check that this realy is a pem-formated certificate,
    # and get the fingerprint and subject of the certificate
    out, err, ret = run_command([
        "openssl", "x509", "-noout", "-inform", "pem", "-sha1",
        "-fingerprint", "-subject"
    ], input=certdata)

    fingerprint = None
    subject = None
    for line in out.split("\n"):
        data = line.split("=", 1)

        if data[0] == "SHA1 Fingerprint":
            fingerprint = data[1].replace(':', '')

        if data[0] == "subject":
            subject = data[1].split('/')

    if fingerprint is None or subject is None:
        raise ValueError("OpensSSL failed to parse ssl certificate.")

    # SSLSocket breaks badly on multiple certifiates with the same subject
    # in the keyring, so ensure that it unique to this slave/user.
    if ((cn and not "CN={cn}".format(cn=cn) in subject) or
            (email and not "emailAddress={email}".format(email=email) in subject)):
        raise ValueError("Incorrect subject of ssl certificate (cn=\"%s\", emailAddress=\"%s\", subject=\"%s\")" %
                         (cn, email, subject))

    # Add the valid pem-formated certificate to the keyring.
    keyring = open(keyring, 'a')
    fcntl.lockf(keyring, fcntl.LOCK_EX)
    keyring.write(certdata)
    keyring.close()

    return fingerprint
dataset/ETHPy150Open Debian/debile/debile/master/keyrings.py/import_ssl/original