Merge branch 'systemd'
[strongswan.git] / scripts / keyid2sql.c
1 /*
2 * Copyright (C) 2008 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdio.h>
17 #include <library.h>
18 #include <utils/debug.h>
19 #include <credentials/keys/private_key.h>
20 #include <credentials/keys/public_key.h>
21
22 /**
23 * print the keyids of a private or public key in sql format
24 */
25 int main(int argc, char *argv[])
26 {
27 public_key_t *public;
28 private_key_t *private;
29 chunk_t chunk;
30 char buf[8096];
31 int read, n;
32
33 library_init(NULL, "keyid2sql");
34 lib->plugins->load(lib->plugins, PLUGINS);
35 atexit(library_deinit);
36
37 read = fread(buf, 1, sizeof(buf), stdin);
38 if (read <= 0)
39 {
40 fprintf(stderr, "reading key failed.\n");
41 return -1;
42 }
43
44 chunk = chunk_create(buf, read);
45
46 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
47 BUILD_BLOB_PEM, chunk_clone(chunk),
48 BUILD_END);
49 if (private)
50 {
51 if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1, &chunk))
52 {
53 printf("%d, X'", ID_KEY_ID);
54 for (n = 0; n < chunk.len; n++)
55 {
56 printf("%.2x", chunk.ptr[n]);
57 }
58 printf("'\n");
59 }
60 private->destroy(private);
61 return 0;
62 }
63
64 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
65 BUILD_BLOB_PEM, chunk_clone(chunk),
66 BUILD_END);
67 if (!public)
68 {
69 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
70 BUILD_BLOB_PEM, chunk_clone(chunk),
71 BUILD_END);
72 }
73 if (public)
74 {
75 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
76 {
77 printf("%d, X'", ID_KEY_ID);
78 for (n = 0; n < chunk.len; n++)
79 {
80 printf("%.2x", chunk.ptr[n]);
81 }
82 printf("'\n");
83 }
84 public->destroy(public);
85 return 0;
86 }
87
88 fprintf(stderr, "unable to parse input key.\n");
89 return -1;
90 }
91