added keyid2sql helper script
[strongswan.git] / scripts / keyid2sql.c
1
2 #include <stdio.h>
3 #include <library.h>
4 #include <debug.h>
5
6 /**
7 * print the keyids of a private or public key in sql format
8 */
9 int main(int argc, char *argv[])
10 {
11 public_key_t *public;
12 private_key_t *private;
13 identification_t *keyid;
14 chunk_t chunk;
15 char buf[8096];
16 int read, n;
17
18 library_init(NULL);
19 lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, "gmp pubkey sha1");
20 atexit(library_deinit);
21
22 read = fread(buf, 1, sizeof(buf), stdin);
23 if (read <= 0)
24 {
25 fprintf(stderr, "reading key failed.\n");
26 return -1;
27 }
28
29 chunk = chunk_create(buf, read);
30
31 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
32 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
33 BUILD_END);
34 if (private)
35 {
36 keyid = private->get_id(private, ID_PUBKEY_INFO_SHA1);
37 chunk = keyid->get_encoding(keyid);
38
39 printf("%d, X'", ID_PUBKEY_INFO_SHA1);
40 for (n = 0; n < chunk.len; n++)
41 {
42 printf("%.2x", chunk.ptr[n]);
43 }
44 printf("'\n");
45 private->destroy(private);
46 return 0;
47 }
48
49 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
50 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
51 BUILD_END);
52 if (!public)
53 {
54 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
55 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
56 BUILD_END);
57 }
58 if (public)
59 {
60 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
61 chunk = keyid->get_encoding(keyid);
62
63 printf("%d, X'", ID_PUBKEY_INFO_SHA1);
64 for (n = 0; n < chunk.len; n++)
65 {
66 printf("%.2x", chunk.ptr[n]);
67 }
68 printf("'\n");
69 public->destroy(public);
70 return 0;
71 }
72
73 fprintf(stderr, "unable to parse input key.\n");
74 return -1;
75 }
76