]> git.ipfire.org Git - people/ms/strongswan.git/blame - scripts/key2keyid.c
use ./configured plugins in keyid scripts
[people/ms/strongswan.git] / scripts / key2keyid.c
CommitLineData
6e884098
MW
1
2#include <stdio.h>
3#include <library.h>
4#include <debug.h>
af1feed9
AS
5#include <credentials/keys/private_key.h>
6#include <credentials/keys/public_key.h>
6e884098 7
6e884098
MW
8/**
9 * print the keyids of a private or public key
10 */
11int 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
6e884098 19 library_init(NULL);
0df451bc 20 lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, PLUGINS);
6e884098
MW
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,
500aa260 33 BUILD_BLOB_PEM, chunk_clone(chunk),
6e884098
MW
34 BUILD_END);
35 if (private)
36 {
37 printf("parsed %d bits %N private key.\n",
38 private->get_keysize(private)*8,
39 key_type_names, private->get_type(private));
94dde8a0
MW
40 if (private->get_fingerprint(private, KEY_ID_PUBKEY_INFO_SHA1, &chunk))
41 {
42 printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
43 }
44 if (private->get_fingerprint(private, KEY_ID_PUBKEY_SHA1, &chunk))
45 {
46 printf("subjectPublicKey keyid: %#B\n", &chunk);
47 }
48 if (private->get_fingerprint(private, KEY_ID_PGPV3, &chunk))
49 {
50 printf("PGP verison 3 keyid: %#B\n", &chunk);
51 }
6e884098
MW
52 private->destroy(private);
53 return 0;
54 }
55
56 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
500aa260 57 BUILD_BLOB_PEM, chunk_clone(chunk),
6e884098
MW
58 BUILD_END);
59 if (!public)
60 {
61 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
500aa260 62 BUILD_BLOB_PEM, chunk_clone(chunk),
6e884098
MW
63 BUILD_END);
64 }
65 if (public)
66 {
67 printf("parsed %d bits %N public key.\n",
68 public->get_keysize(public)*8,
69 key_type_names, public->get_type(public));
94dde8a0
MW
70 if (public->get_fingerprint(public, KEY_ID_PUBKEY_INFO_SHA1, &chunk))
71 {
72 printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
73 }
74 if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &chunk))
75 {
76 printf("subjectPublicKey keyid: %#B\n", &chunk);
77 }
78 if (public->get_fingerprint(public, KEY_ID_PGPV3, &chunk))
79 {
80 printf("PGP verison 3 keyid: %#B\n", &chunk);
81 }
6e884098
MW
82 public->destroy(public);
83 return 0;
84 }
85
86 fprintf(stderr, "unable to parse input key.\n");
87 return -1;
88}
89