pki --pub and --keyid accept pkcs10 as input
[strongswan.git] / src / pki / commands / keyid.c
1 /*
2 * Copyright (C) 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 */
15
16 #include "pki.h"
17
18 #include <credentials/certificates/certificate.h>
19 #include <credentials/certificates/x509.h>
20
21 /**
22 * Calculate the keyid of a key/certificate
23 */
24 static int keyid(int argc, char *argv[])
25 {
26 credential_type_t type = CRED_PRIVATE_KEY;
27 int subtype = KEY_RSA;
28 certificate_t *cert;
29 private_key_t *private;
30 public_key_t *public;
31 char *file = NULL;
32 void *cred;
33 chunk_t id;
34
35 while (TRUE)
36 {
37 switch (getopt_long(argc, argv, "", command_opts, NULL))
38 {
39 case 'h':
40 return command_usage(NULL);
41 case 'v':
42 dbg_level = atoi(optarg);
43 continue;
44 case 't':
45 if (streq(optarg, "rsa-priv"))
46 {
47 type = CRED_PRIVATE_KEY;
48 subtype = KEY_RSA;
49 }
50 else if (streq(optarg, "ecdsa-priv"))
51 {
52 type = CRED_PRIVATE_KEY;
53 subtype = KEY_ECDSA;
54 }
55 else if (streq(optarg, "pub"))
56 {
57 type = CRED_PUBLIC_KEY;
58 subtype = KEY_ANY;
59 }
60 else if (streq(optarg, "pkcs10"))
61 {
62 type = CRED_CERTIFICATE;
63 subtype = CERT_PKCS10_REQUEST;
64 }
65 else if (streq(optarg, "x509"))
66 {
67 type = CRED_CERTIFICATE;
68 subtype = CERT_X509;
69 }
70 else
71 {
72 return command_usage( "invalid input type");
73 }
74 continue;
75 case 'i':
76 file = optarg;
77 continue;
78 case EOF:
79 break;
80 default:
81 return command_usage("invalid --keyid option");
82 }
83 break;
84 }
85 if (file)
86 {
87 cred = lib->creds->create(lib->creds, type, subtype,
88 BUILD_FROM_FILE, file, BUILD_END);
89 }
90 else
91 {
92 cred = lib->creds->create(lib->creds, type, subtype,
93 BUILD_FROM_FD, 0, BUILD_END);
94 }
95 if (!cred)
96 {
97 fprintf(stderr, "parsing input failed\n");
98 return 1;
99 }
100
101 if (type == CRED_PRIVATE_KEY)
102 {
103 private = cred;
104 if (private->get_fingerprint(private, KEY_ID_PUBKEY_SHA1, &id))
105 {
106 printf("subjectKeyIdentifier: %#B\n", &id);
107 }
108 if (private->get_fingerprint(private, KEY_ID_PUBKEY_INFO_SHA1, &id))
109 {
110 printf("subjectPublicKeyInfo hash: %#B\n", &id);
111 }
112 private->destroy(private);
113 }
114 else if (type == CRED_PUBLIC_KEY)
115 {
116 public = cred;
117 if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &id))
118 {
119 printf("subjectKeyIdentifier: %#B\n", &id);
120 }
121 if (public->get_fingerprint(public, KEY_ID_PUBKEY_INFO_SHA1, &id))
122 {
123 printf("subjectPublicKeyInfo hash: %#B\n", &id);
124 }
125 public->destroy(public);
126 }
127 else
128 {
129 cert = cred;
130 public = cert->get_public_key(cert);
131 if (!public)
132 {
133 fprintf(stderr, "extracting public key from certificate failed");
134 return 1;
135 }
136 if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &id))
137 {
138 printf("subjectKeyIdentifier: %#B\n", &id);
139 }
140 if (public->get_fingerprint(public, KEY_ID_PUBKEY_INFO_SHA1, &id))
141 {
142 printf("subjectPublicKeyInfo hash: %#B\n", &id);
143 }
144 public->destroy(public);
145 cert->destroy(cert);
146 }
147 return 0;
148 }
149
150 /**
151 * Register the command.
152 */
153 static void __attribute__ ((constructor))reg()
154 {
155 command_register((command_t)
156 { keyid, 'k', "keyid",
157 "calculate key identifiers of a key/certificate",
158 {"[--in file] [--type rsa-priv|ecdsa-priv|pub|pkcs10|x509]",
159 "[--debug 0|1|2|3|4]"},
160 {
161 {"help", 'h', 0, "show usage information"},
162 {"in", 'i', 1, "input file, default: stdin"},
163 {"type", 't', 1, "type of key, default: rsa-priv"},
164 {"debug", 'v', 1, "set debug level, default: 1"},
165 }
166 });
167 }
168