another lease identity to be single-quoted
[strongswan.git] / scripts / key2keyid.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
8 */
9 int main(int argc, char *argv[])
10 {
11 public_key_t *public;
12 private_key_t *private;
13 chunk_t chunk;
14 char buf[8096];
15 int read;
16
17 library_init(NULL);
18 lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, "gmp pubkey sha1");
19 atexit(library_deinit);
20
21 read = fread(buf, 1, sizeof(buf), stdin);
22 if (read <= 0)
23 {
24 fprintf(stderr, "reading key failed.\n");
25 return -1;
26 }
27
28 chunk = chunk_create(buf, read);
29
30 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
31 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
32 BUILD_END);
33 if (private)
34 {
35 printf("parsed %d bits %N private key.\n",
36 private->get_keysize(private)*8,
37 key_type_names, private->get_type(private));
38 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_INFO_SHA1,
39 private->get_id(private, ID_PUBKEY_INFO_SHA1));
40 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_SHA1,
41 private->get_id(private, ID_PUBKEY_SHA1));
42 private->destroy(private);
43 return 0;
44 }
45
46 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
47 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
48 BUILD_END);
49 if (!public)
50 {
51 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
52 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
53 BUILD_END);
54 }
55 if (public)
56 {
57 printf("parsed %d bits %N public key.\n",
58 public->get_keysize(public)*8,
59 key_type_names, public->get_type(public));
60 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_INFO_SHA1,
61 public->get_id(public, ID_PUBKEY_INFO_SHA1));
62 printf("%N is:\t %D\n", id_type_names, ID_PUBKEY_SHA1,
63 public->get_id(public, ID_PUBKEY_SHA1));
64 public->destroy(public);
65 return 0;
66 }
67
68 fprintf(stderr, "unable to parse input key.\n");
69 return -1;
70 }
71