Esempio n. 1
0
def generate_fingerprint(public_key):
    with utils.tempdir() as tmpdir:
        try:
            pubfile = os.path.join(tmpdir, 'temp.pub')
            with open(pubfile, 'w') as f:
                f.write(public_key)
            return _generate_fingerprint(pubfile)
        except exception.ProcessExecutionError:
            raise exception.InvalidKeypair()
Esempio n. 2
0
def generate_x509_fingerprint(pem_key):
    try:
        (out, _err) = utils.execute('openssl', 'x509', '-inform', 'PEM',
                                    '-fingerprint', '-noout',
                                    process_input=pem_key)
        fingerprint = string.strip(out.rpartition('=')[2])
        return fingerprint.lower()
    except processutils.ProcessExecutionError as ex:
        raise exception.InvalidKeypair(
            reason=_('failed to generate X509 fingerprint. '
                     'Error message: %s') % ex)
Esempio n. 3
0
def generate_fingerprint(public_key):
    try:
        parts = public_key.split(' ')
        ssh_alg = parts[0]
        pub_data = parts[1].decode('base64')
        if ssh_alg == 'ssh-rsa':
            pkey = paramiko.RSAKey(data=pub_data)
        elif ssh_alg == 'ssh-dss':
            pkey = paramiko.DSSKey(data=pub_data)
        elif ssh_alg == 'ecdsa-sha2-nistp256':
            pkey = paramiko.ECDSAKey(data=pub_data, validate_point=False)
        else:
            raise exception.InvalidKeypair(
                reason=_('Unknown ssh key type %s') % ssh_alg)
        raw_fp = binascii.hexlify(pkey.get_fingerprint())
        return ':'.join(a + b for a, b in zip(raw_fp[::2], raw_fp[1::2]))
    except (IndexError, UnicodeDecodeError, binascii.Error,
            paramiko.ssh_exception.SSHException):
        raise exception.InvalidKeypair(
            reason=_('failed to generate fingerprint'))
Esempio n. 4
0
def generate_x509_fingerprint(pem_key):
    try:
        if isinstance(pem_key, six.text_type):
            pem_key = pem_key.encode('utf-8')
        (out, _err) = utils.execute('openssl', 'x509', '-inform', 'PEM',
                                    '-fingerprint', '-noout',
                                    process_input=pem_key)
        fingerprint = out.rpartition('=')[2].strip()
        return fingerprint.lower()
    except processutils.ProcessExecutionError as ex:
        raise exception.InvalidKeypair(
            reason=_('failed to generate X509 fingerprint. '
                     'Error message: %s') % ex)
Esempio n. 5
0
def generate_x509_fingerprint(pem_key: ty.Union[bytes, str]) -> str:
    try:
        if isinstance(pem_key, str):
            pem_key = pem_key.encode('utf-8')
        cert = x509.load_pem_x509_certificate(pem_key,
                                              backends.default_backend())
        raw_fp = binascii.hexlify(cert.fingerprint(
            hashes.SHA1())).decode('ascii')
        return ':'.join(a + b for a, b in zip(raw_fp[::2], raw_fp[1::2]))
    except (ValueError, TypeError, binascii.Error) as ex:
        raise exception.InvalidKeypair(
            reason=_('failed to generate X509 fingerprint. '
                     'Error message: %s') % ex)
Esempio n. 6
0
def generate_fingerprint(public_key):
    tmpdir = tempfile.mkdtemp()
    try:
        pubfile = os.path.join(tmpdir, 'temp.pub')
        with open(pubfile, 'w') as f:
            f.write(public_key)
        return _generate_fingerprint(pubfile)
    except exception.ProcessExecutionError:
        raise exception.InvalidKeypair()
    finally:
        try:
            shutil.rmtree(tmpdir)
        except IOError, e:
            LOG.debug(_('Could not remove tmpdir: %s'), str(e))
Esempio n. 7
0
def generate_fingerprint(public_key: str) -> str:
    try:
        pub_bytes = public_key.encode('utf-8')
        # Test that the given public_key string is a proper ssh key. The
        # returned object is unused since pyca/cryptography does not have a
        # fingerprint method.
        serialization.load_ssh_public_key(pub_bytes,
                                          backends.default_backend())
        pub_data = base64.b64decode(public_key.split(' ')[1])
        raw_fp = md5(pub_data, usedforsecurity=False).hexdigest()
        return ':'.join(a + b for a, b in zip(raw_fp[::2], raw_fp[1::2]))
    except Exception:
        raise exception.InvalidKeypair(
            reason=_('failed to generate fingerprint'))
Esempio n. 8
0
def generate_fingerprint(public_key: str) -> str:
    try:
        pub_bytes = public_key.encode('utf-8')
        # Test that the given public_key string is a proper ssh key. The
        # returned object is unused since pyca/cryptography does not have a
        # fingerprint method.
        serialization.load_ssh_public_key(
            pub_bytes, backends.default_backend())
        pub_data = base64.b64decode(public_key.split(' ')[1])
        digest = hashes.Hash(hashes.MD5(), backends.default_backend())
        digest.update(pub_data)
        md5hash = digest.finalize()
        raw_fp = binascii.hexlify(md5hash).decode('ascii')
        return ':'.join(a + b for a, b in zip(raw_fp[::2], raw_fp[1::2]))
    except Exception:
        raise exception.InvalidKeypair(
            reason=_('failed to generate fingerprint'))