2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
18 #include <credentials/certificates/certificate.h>
19 #include <credentials/certificates/x509.h>
22 * Calculate the keyid of a key/certificate
26 credential_type_t type
= CRED_PRIVATE_KEY
;
27 int subtype
= KEY_RSA
;
29 private_key_t
*private;
38 switch (command_getopt(&arg
))
41 return command_usage(NULL
);
43 if (streq(arg
, "rsa-priv"))
45 type
= CRED_PRIVATE_KEY
;
48 else if (streq(arg
, "ecdsa-priv"))
50 type
= CRED_PRIVATE_KEY
;
53 else if (streq(arg
, "pub"))
55 type
= CRED_PUBLIC_KEY
;
58 else if (streq(arg
, "pkcs10"))
60 type
= CRED_CERTIFICATE
;
61 subtype
= CERT_PKCS10_REQUEST
;
63 else if (streq(arg
, "x509"))
65 type
= CRED_CERTIFICATE
;
70 return command_usage( "invalid input type");
79 return command_usage("invalid --keyid option");
85 cred
= lib
->creds
->create(lib
->creds
, type
, subtype
,
86 BUILD_FROM_FILE
, file
, BUILD_END
);
90 cred
= lib
->creds
->create(lib
->creds
, type
, subtype
,
91 BUILD_FROM_FD
, 0, BUILD_END
);
95 fprintf(stderr
, "parsing input failed\n");
99 if (type
== CRED_PRIVATE_KEY
)
102 if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1
, &id
))
104 printf("subjectKeyIdentifier: %#B\n", &id
);
106 if (private->get_fingerprint(private, KEYID_PUBKEY_INFO_SHA1
, &id
))
108 printf("subjectPublicKeyInfo hash: %#B\n", &id
);
110 private->destroy(private);
112 else if (type
== CRED_PUBLIC_KEY
)
115 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &id
))
117 printf("subjectKeyIdentifier: %#B\n", &id
);
119 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1
, &id
))
121 printf("subjectPublicKeyInfo hash: %#B\n", &id
);
123 public->destroy(public);
128 public = cert
->get_public_key(cert
);
131 fprintf(stderr
, "extracting public key from certificate failed");
134 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &id
))
136 printf("subjectKeyIdentifier: %#B\n", &id
);
138 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1
, &id
))
140 printf("subjectPublicKeyInfo hash: %#B\n", &id
);
142 public->destroy(public);
149 * Register the command.
151 static void __attribute__ ((constructor
))reg()
153 command_register((command_t
)
154 { keyid
, 'k', "keyid",
155 "calculate key identifiers of a key/certificate",
156 {"[--in file] [--type rsa-priv|ecdsa-priv|pub|pkcs10|x509]"},
158 {"help", 'h', 0, "show usage information"},
159 {"in", 'i', 1, "input file, default: stdin"},
160 {"type", 't', 1, "type of key, default: rsa-priv"},