summaryrefslogtreecommitdiff
path: root/fingerprint.c
blob: 9a9b6358397bf06da50ba961ad215819c34a35e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "includes.h"
RCSID("$Id: fingerprint.c,v 1.1 1999/11/17 06:29:08 damien Exp $");

#include "ssh.h"
#include "xmalloc.h"

#ifdef HAVE_OPENSSL
#include <openssl/md5.h>
#endif
#ifdef HAVE_SSL
#include <ssl/md5.h>
#endif

#define FPRINT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"

/* Generate key fingerprint in ascii format.
   Based on ideas and code from Bjoern Groenvall <bg@sics.se> */

char *
fingerprint(BIGNUM *e, BIGNUM *n)
{
	static char     retval[80];
	MD5_CTX         md;
	unsigned char   d[16];
	char           *buf;
	int             nlen, elen;

	nlen = BN_num_bytes(n);
	elen = BN_num_bytes(e);

	buf = xmalloc(nlen + elen);

	BN_bn2bin(n, buf);
	BN_bn2bin(e, buf + nlen);

	MD5_Init(&md);
	MD5_Update(&md, buf, nlen + elen);
	MD5_Final(d, &md);
	snprintf(retval, sizeof(retval), FPRINT,
		 d[0], d[1], d[2],  d[3],  d[4],  d[5],  d[6],  d[7],
		 d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
	memset(buf, 0, nlen + elen);
	xfree(buf);
	return retval;
}