tool to calculate KEYIDs from keys
[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 * print the keyids of a private or public key
12 */
13 int main(int argc, char *argv[])
14 {
15 public_key_t *public;
16 private_key_t *private;
17 chunk_t chunk;
18 char buf[8096];
19 int read;
20
21 dbg = dbg_stderr;
22
23 library_init(NULL);
24 lib->plugins->load(lib->plugins, "/usr/local/libexec/ipsec/plugins", "libstrongswan-");
25 atexit(library_deinit);
26
27 read = fread(buf, 1, sizeof(buf), stdin);
28 if (read <= 0)
29 {
30 fprintf(stderr, "reading key failed.\n");
31 return -1;
32 }
33
34 chunk = chunk_create(buf, read);
35
36 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
37 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
38 BUILD_END);
39 if (private)
40 {
41 printf("parsed %d bits %N private key.\n",
42 private->get_keysize(private)*8,
43 key_type_names, private->get_type(private));
44 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_INFO_SHA1,
45 private->get_id(private, ID_PUBKEY_INFO_SHA1));
46 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_SHA1,
47 private->get_id(private, ID_PUBKEY_SHA1));
48 private->destroy(private);
49 return 0;
50 }
51
52 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
53 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
54 BUILD_END);
55 if (!public)
56 {
57 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
58 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
59 BUILD_END);
60 }
61 if (public)
62 {
63 printf("parsed %d bits %N public key.\n",
64 public->get_keysize(public)*8,
65 key_type_names, public->get_type(public));
66 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_INFO_SHA1,
67 public->get_id(public, ID_PUBKEY_INFO_SHA1));
68 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_SHA1,
69 public->get_id(public, ID_PUBKEY_SHA1));
70 public->destroy(public);
71 return 0;
72 }
73
74 fprintf(stderr, "unable to parse input key.\n");
75 return -1;
76 }
77