Skip to content

Commit 3e44725

Browse files
Add function get-public-key-fingerprint
1 parent 41af07f commit 3e44725

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

libguile-ssh/key-func.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,10 @@ Read public key from a file FILENAME. Return a SSH key.\
384384
#undef FUNC_NAME
385385

386386
static gssh_symbol_t hash_types[] = {
387-
{ "sha1", SSH_PUBLICKEY_HASH_SHA1 },
388-
{ "md5", SSH_PUBLICKEY_HASH_MD5 },
389-
{ NULL, -1 }
387+
{ "sha1", SSH_PUBLICKEY_HASH_SHA1 },
388+
{ "sha256", SSH_PUBLICKEY_HASH_SHA256 },
389+
{ "md5", SSH_PUBLICKEY_HASH_MD5 },
390+
{ NULL, -1 }
390391
};
391392

392393
SCM_DEFINE (guile_ssh_get_public_key_hash, "get-public-key-hash", 2, 0, 0,
@@ -434,6 +435,57 @@ Return a bytevector on success, #f on error.\
434435
}
435436
#undef FUNC_NAME
436437

438+
SCM_DEFINE (guile_ssh_get_public_key_fingerprint, "get-public-key-fingerprint", 2, 0, 0,
439+
(SCM key, SCM type),
440+
"\
441+
Get fingerprint of the public KEY as a formatted string.\n\
442+
Possible types are: 'sha1, 'md5\n\
443+
Return a fingerprint string on success, #f on error.\
444+
")
445+
#define FUNC_NAME s_guile_ssh_get_public_key_fingerprint
446+
{
447+
gssh_key_t *kd = gssh_key_from_scm (key);
448+
unsigned char *hash = NULL;
449+
size_t hash_len;
450+
char *fingerprint = NULL;
451+
int res;
452+
SCM ret;
453+
const gssh_symbol_t *hash_type = NULL;
454+
455+
SCM_ASSERT (scm_is_symbol (type), type, SCM_ARG2, FUNC_NAME);
456+
457+
scm_dynwind_begin (0);
458+
459+
hash_type = gssh_symbol_from_scm (hash_types, type);
460+
if (! hash_type)
461+
guile_ssh_error1 (FUNC_NAME, "Wrong type", type);
462+
463+
res = ssh_get_publickey_hash (kd->ssh_key, hash_type->value,
464+
&hash, &hash_len);
465+
scm_dynwind_free (hash);
466+
467+
if (res == SSH_OK)
468+
{
469+
fingerprint = ssh_get_fingerprint_hash (hash_type->value, hash, hash_len);
470+
if (fingerprint)
471+
{
472+
ret = scm_take_locale_string (fingerprint);
473+
}
474+
else
475+
{
476+
ret = SCM_BOOL_F;
477+
}
478+
}
479+
else
480+
{
481+
ret = SCM_BOOL_F;
482+
}
483+
484+
scm_dynwind_end ();
485+
return ret;
486+
}
487+
#undef FUNC_NAME
488+
437489

438490
/* Initialize Scheme procedures. */
439491
void

libguile-ssh/key-func.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern SCM guile_ssh_string_to_public_key (SCM arg1, SCM arg2);
2525
extern SCM guile_ssh_public_key_to_string (SCM arg1);
2626
extern SCM guile_ssh_private_key_from_file (SCM arg1, SCM arg2);
2727
extern SCM guile_ssh_public_key_from_file (SCM arg1, SCM arg2);
28+
extern SCM guile_ssh_get_public_key_fingerprint (SCM arg1, SCM arg2);
2829

2930
extern void init_key_func (void);
3031

modules/ssh/key.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
private-key->public-key
5858
private-key-from-file
5959
private-key-to-file
60+
get-public-key-fingerprint
6061
get-public-key-hash
6162
bytevector->hex-string))
6263

tests/key.scm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@
243243
(or (eq? (get-key-type key) 'ecdsa) ; libssh < 0.9
244244
(eq? (get-key-type key) 'ecdsa-p256)))))
245245

246+
247+
;;; Key fingerprints.
248+
249+
(test-assert-with-log "get-public-key-fingerprint: RSA SHA1"
250+
(let ((fingerprint (get-public-key-fingerprint *rsa-pub-key* 'sha1)))
251+
(and (string? fingerprint)
252+
(> (string-length fingerprint) 0))))
253+
254+
(test-assert-with-log "get-public-key-fingerprint: RSA SHA256"
255+
(let ((fingerprint (get-public-key-fingerprint *rsa-pub-key* 'sha256)))
256+
(and (string? fingerprint)
257+
(> (string-length fingerprint) 0))))
258+
259+
(test-assert-with-log "get-public-key-fingerprint: RSA MD5"
260+
(let ((fingerprint (get-public-key-fingerprint *rsa-pub-key* 'md5)))
261+
(and (string? fingerprint)
262+
(> (string-length fingerprint) 0))))
263+
246264

247265
;;; Check reading encrypted keys.
248266

0 commit comments

Comments
 (0)