Use bits instead of bytes for a private/public key
[strongswan.git] / scripts / key2keyid.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
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;
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 printf("parsed %d bits %N private key.\n",
38 private->get_keysize(private),
39 key_type_names, private->get_type(private));
40 if (private->get_fingerprint(private, KEYID_PUBKEY_INFO_SHA1, &chunk))
41 {
42 printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
43 }
44 if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1, &chunk))
45 {
46 printf("subjectPublicKey keyid: %#B\n", &chunk);
47 }
48 if (private->get_fingerprint(private, KEYID_PGPV3, &chunk))
49 {
50 printf("PGP version 3 keyid: %#B\n", &chunk);
51 }
52 private->destroy(private);
53 return 0;
54 }
55
56 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
57 BUILD_BLOB_PEM, chunk_clone(chunk),
58 BUILD_END);
59 if (!public)
60 {
61 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
62 BUILD_BLOB_PEM, chunk_clone(chunk),
63 BUILD_END);
64 }
65 if (public)
66 {
67 printf("parsed %d bits %N public key.\n",
68 public->get_keysize(public),
69 key_type_names, public->get_type(public));
70 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &chunk))
71 {
72 printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
73 }
74 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
75 {
76 printf("subjectPublicKey keyid: %#B\n", &chunk);
77 }
78 if (public->get_fingerprint(public, KEYID_PGPV3, &chunk))
79 {
80 printf("PGP version 3 keyid: %#B\n", &chunk);
81 }
82 public->destroy(public);
83 return 0;
84 }
85
86 fprintf(stderr, "unable to parse input key.\n");
87 return -1;
88 }
89