]> git.ipfire.org Git - thirdparty/strongswan.git/blame - scripts/keyid2sql.c
x509: Also encode extendedKeyUsage in cert requests if there are no SANs or certifica...
[thirdparty/strongswan.git] / scripts / keyid2sql.c
CommitLineData
f3af4969
TB
1/*
2 * Copyright (C) 2008 Andreas Steffen
19ef2aec
TB
3 *
4 * Copyright (C) secunet Security Networks AG
f3af4969
TB
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 */
f5a0d968
AS
16
17#include <stdio.h>
18#include <library.h>
f05b4272 19#include <utils/debug.h>
5c1e8ca7
AS
20#include <credentials/keys/private_key.h>
21#include <credentials/keys/public_key.h>
22
f5a0d968
AS
23/**
24 * print the keyids of a private or public key in sql format
25 */
26int main(int argc, char *argv[])
27{
28 public_key_t *public;
29 private_key_t *private;
f5a0d968
AS
30 chunk_t chunk;
31 char buf[8096];
32 int read, n;
7daf5226 33
34d3bfcf 34 library_init(NULL, "keyid2sql");
b18a5317 35 lib->plugins->load(lib->plugins, PLUGINS);
f5a0d968
AS
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 }
7daf5226 44
f5a0d968 45 chunk = chunk_create(buf, read);
7daf5226 46
f5a0d968 47 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
500aa260 48 BUILD_BLOB_PEM, chunk_clone(chunk),
f5a0d968
AS
49 BUILD_END);
50 if (private)
51 {
da9724e6 52 if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1, &chunk))
f5a0d968 53 {
94dde8a0
MW
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");
f5a0d968 60 }
f5a0d968
AS
61 private->destroy(private);
62 return 0;
63 }
7daf5226 64
f5a0d968 65 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
500aa260 66 BUILD_BLOB_PEM, chunk_clone(chunk),
f5a0d968
AS
67 BUILD_END);
68 if (!public)
69 {
70 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
500aa260 71 BUILD_BLOB_PEM, chunk_clone(chunk),
f5a0d968
AS
72 BUILD_END);
73 }
74 if (public)
75 {
da9724e6 76 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
f5a0d968 77 {
94dde8a0
MW
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");
f5a0d968 84 }
f5a0d968
AS
85 public->destroy(public);
86 return 0;
87 }
7daf5226 88
f5a0d968
AS
89 fprintf(stderr, "unable to parse input key.\n");
90 return -1;
91}
92