]> git.ipfire.org Git - people/ms/strongswan.git/blame - scripts/key2keyid.c
Merge branch 'utils-split'
[people/ms/strongswan.git] / scripts / key2keyid.c
CommitLineData
f3af4969
TB
1/*
2 * Copyright (C) 2008-2009 Martin Willi
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 */
6e884098
MW
15
16#include <stdio.h>
17#include <library.h>
f05b4272 18#include <utils/debug.h>
af1feed9
AS
19#include <credentials/keys/private_key.h>
20#include <credentials/keys/public_key.h>
6e884098 21
6e884098
MW
22/**
23 * print the keyids of a private or public key
24 */
25int 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;
7daf5226 32
34d3bfcf 33 library_init(NULL, "key2keyid");
b18a5317 34 lib->plugins->load(lib->plugins, PLUGINS);
6e884098
MW
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 }
7daf5226 43
6e884098 44 chunk = chunk_create(buf, read);
7daf5226 45
6e884098 46 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
500aa260 47 BUILD_BLOB_PEM, chunk_clone(chunk),
6e884098
MW
48 BUILD_END);
49 if (private)
50 {
51 printf("parsed %d bits %N private key.\n",
a944d209 52 private->get_keysize(private),
6e884098 53 key_type_names, private->get_type(private));
da9724e6 54 if (private->get_fingerprint(private, KEYID_PUBKEY_INFO_SHA1, &chunk))
94dde8a0
MW
55 {
56 printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
57 }
da9724e6 58 if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1, &chunk))
94dde8a0
MW
59 {
60 printf("subjectPublicKey keyid: %#B\n", &chunk);
61 }
da9724e6 62 if (private->get_fingerprint(private, KEYID_PGPV3, &chunk))
94dde8a0 63 {
0465d2c0 64 printf("PGP version 3 keyid: %#B\n", &chunk);
94dde8a0 65 }
6e884098
MW
66 private->destroy(private);
67 return 0;
68 }
7daf5226 69
6e884098 70 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
500aa260 71 BUILD_BLOB_PEM, chunk_clone(chunk),
6e884098
MW
72 BUILD_END);
73 if (!public)
74 {
75 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
500aa260 76 BUILD_BLOB_PEM, chunk_clone(chunk),
6e884098
MW
77 BUILD_END);
78 }
79 if (public)
80 {
81 printf("parsed %d bits %N public key.\n",
a944d209 82 public->get_keysize(public),
6e884098 83 key_type_names, public->get_type(public));
da9724e6 84 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &chunk))
94dde8a0
MW
85 {
86 printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
87 }
da9724e6 88 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
94dde8a0
MW
89 {
90 printf("subjectPublicKey keyid: %#B\n", &chunk);
91 }
da9724e6 92 if (public->get_fingerprint(public, KEYID_PGPV3, &chunk))
94dde8a0 93 {
0465d2c0 94 printf("PGP version 3 keyid: %#B\n", &chunk);
94dde8a0 95 }
6e884098
MW
96 public->destroy(public);
97 return 0;
98 }
7daf5226 99
6e884098
MW
100 fprintf(stderr, "unable to parse input key.\n");
101 return -1;
102}
103