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