loading default modules depending on configure options
[strongswan.git] / scripts / key2keyid.c
1
2 #include <stdio.h>
3 #include <library.h>
4 #include <debug.h>
5
6 static void dbg_stderr(int level, char *fmt, ...)
7 {
8
9 }
10
11 /**
12 * print the keyids of a private or public key
13 */
14 int main(int argc, char *argv[])
15 {
16 public_key_t *public;
17 private_key_t *private;
18 chunk_t chunk;
19 char buf[8096];
20 int read;
21
22 //dbg = dbg_stderr;
23
24 library_init(NULL);
25 lib->plugins->load(lib->plugins, "/usr/local/libexec/ipsec/plugins", "gmp pubkey sha1");
26 atexit(library_deinit);
27
28 read = fread(buf, 1, sizeof(buf), stdin);
29 if (read <= 0)
30 {
31 fprintf(stderr, "reading key failed.\n");
32 return -1;
33 }
34
35 chunk = chunk_create(buf, read);
36
37 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
38 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
39 BUILD_END);
40 if (private)
41 {
42 printf("parsed %d bits %N private key.\n",
43 private->get_keysize(private)*8,
44 key_type_names, private->get_type(private));
45 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_INFO_SHA1,
46 private->get_id(private, ID_PUBKEY_INFO_SHA1));
47 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_SHA1,
48 private->get_id(private, ID_PUBKEY_SHA1));
49 private->destroy(private);
50 return 0;
51 }
52
53 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
54 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
55 BUILD_END);
56 if (!public)
57 {
58 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
59 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
60 BUILD_END);
61 }
62 if (public)
63 {
64 printf("parsed %d bits %N public key.\n",
65 public->get_keysize(public)*8,
66 key_type_names, public->get_type(public));
67 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_INFO_SHA1,
68 public->get_id(public, ID_PUBKEY_INFO_SHA1));
69 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_SHA1,
70 public->get_id(public, ID_PUBKEY_SHA1));
71 public->destroy(public);
72 return 0;
73 }
74
75 fprintf(stderr, "unable to parse input key.\n");
76 return -1;
77 }
78