missed keyid2sql.c
[strongswan.git] / scripts / keyid2sql.c
1
2 #include <stdio.h>
3 #include <library.h>
4 #include <debug.h>
5 #include <utils/identification.h>
6 #include <credentials/keys/private_key.h>
7 #include <credentials/keys/public_key.h>
8
9
10 /**
11 * print the keyids of a private or public key in sql format
12 */
13 int main(int argc, char *argv[])
14 {
15 public_key_t *public;
16 private_key_t *private;
17 identification_t *keyid;
18 chunk_t chunk;
19 char buf[8096];
20 int read, n;
21
22 library_init(NULL);
23 lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, "gmp pubkey sha1");
24 atexit(library_deinit);
25
26 read = fread(buf, 1, sizeof(buf), stdin);
27 if (read <= 0)
28 {
29 fprintf(stderr, "reading key failed.\n");
30 return -1;
31 }
32
33 chunk = chunk_create(buf, read);
34
35 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
36 BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
37 BUILD_END);
38 if (private)
39 {
40 keyid = private->get_id(private, ID_PUBKEY_INFO_SHA1);
41 chunk = keyid->get_encoding(keyid);
42
43 printf("%d, X'", ID_PUBKEY_INFO_SHA1);
44 for (n = 0; n < chunk.len; n++)
45 {
46 printf("%.2x", chunk.ptr[n]);
47 }
48 printf("'\n");
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 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
65 chunk = keyid->get_encoding(keyid);
66
67 printf("%d, X'", ID_PUBKEY_INFO_SHA1);
68 for (n = 0; n < chunk.len; n++)
69 {
70 printf("%.2x", chunk.ptr[n]);
71 }
72 printf("'\n");
73 public->destroy(public);
74 return 0;
75 }
76
77 fprintf(stderr, "unable to parse input key.\n");
78 return -1;
79 }
80