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