summaryrefslogtreecommitdiff
path: root/scripts/keyid2sql.c
blob: 588bd7ac03daa5c99c77df172476de51ffd8de4b (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

#include <stdio.h>
#include <library.h>
#include <debug.h>
#include <utils/identification.h>
#include <credentials/keys/private_key.h>
#include <credentials/keys/public_key.h>


/**
 * print the keyids of a private or public key in sql format
 */
int main(int argc, char *argv[])
{
	public_key_t *public;
	private_key_t *private;
	identification_t *keyid;
	chunk_t chunk;
	char buf[8096];
	int read, n;
	
	library_init(NULL);
	lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, "gmp pubkey sha1");
	atexit(library_deinit);

	read = fread(buf, 1, sizeof(buf), stdin);
	if (read <= 0)
	{
		fprintf(stderr, "reading key failed.\n");
		return -1;
	}
	
	chunk = chunk_create(buf, read);
	
	private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
								 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
								 BUILD_END);
	if (private)
	{
		keyid = private->get_id(private, ID_PUBKEY_INFO_SHA1);
		chunk = keyid->get_encoding(keyid);

		printf("%d, X'", ID_PUBKEY_INFO_SHA1);
		for (n = 0; n < chunk.len; n++)
		{
			printf("%.2x", chunk.ptr[n]);
		}
		printf("'\n");
		private->destroy(private);
		return 0;
	}
	
	public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
								BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
								BUILD_END);
	if (!public)
	{
		public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
									BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
									BUILD_END);
	}
	if (public)
	{
		keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
		chunk = keyid->get_encoding(keyid);

		printf("%d, X'", ID_PUBKEY_INFO_SHA1);
		for (n = 0; n < chunk.len; n++)
		{
			printf("%.2x", chunk.ptr[n]);
		}
		printf("'\n");
		public->destroy(public);
		return 0;
	}
	
	fprintf(stderr, "unable to parse input key.\n");
	return -1;
}