Do not query for CKA_ALWAYS_AUTHENTICATE if PKCS#11 Cryptoki version < 2.20
[strongswan.git] / src / pki / commands / self.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 <time.h>
17
18 #include "pki.h"
19
20 #include <utils/linked_list.h>
21 #include <credentials/certificates/certificate.h>
22 #include <credentials/certificates/x509.h>
23
24 /**
25 * Create a self signed certificate.
26 */
27 static int self()
28 {
29 cred_encoding_type_t form = CERT_ASN1_DER;
30 key_type_t type = KEY_RSA;
31 hash_algorithm_t digest = HASH_SHA1;
32 certificate_t *cert = NULL;
33 private_key_t *private = NULL;
34 public_key_t *public = NULL;
35 char *file = NULL, *dn = NULL, *hex = NULL, *error = NULL, *keyid = NULL;
36 identification_t *id = NULL;
37 linked_list_t *san, *ocsp;
38 int lifetime = 1095;
39 int pathlen = X509_NO_PATH_LEN_CONSTRAINT;
40 chunk_t serial = chunk_empty;
41 chunk_t encoding = chunk_empty;
42 time_t not_before, not_after;
43 x509_flag_t flags = 0;
44 char *arg;
45
46 san = linked_list_create();
47 ocsp = linked_list_create();
48
49 while (TRUE)
50 {
51 switch (command_getopt(&arg))
52 {
53 case 'h':
54 goto usage;
55 case 't':
56 if (streq(arg, "rsa"))
57 {
58 type = KEY_RSA;
59 }
60 else if (streq(arg, "ecdsa"))
61 {
62 type = KEY_ECDSA;
63 }
64 else
65 {
66 error = "invalid input type";
67 goto usage;
68 }
69 continue;
70 case 'g':
71 digest = get_digest(arg);
72 if (digest == HASH_UNKNOWN)
73 {
74 error = "invalid --digest type";
75 goto usage;
76 }
77 continue;
78 case 'i':
79 file = arg;
80 continue;
81 case 'x':
82 keyid = arg;
83 continue;
84 case 'd':
85 dn = arg;
86 continue;
87 case 'a':
88 san->insert_last(san, identification_create_from_string(arg));
89 continue;
90 case 'l':
91 lifetime = atoi(arg);
92 if (!lifetime)
93 {
94 error = "invalid --lifetime value";
95 goto usage;
96 }
97 continue;
98 case 's':
99 hex = arg;
100 continue;
101 case 'b':
102 flags |= X509_CA;
103 continue;
104 case 'p':
105 pathlen = atoi(arg);
106 continue;
107 case 'e':
108 if (streq(arg, "serverAuth"))
109 {
110 flags |= X509_SERVER_AUTH;
111 }
112 else if (streq(arg, "clientAuth"))
113 {
114 flags |= X509_CLIENT_AUTH;
115 }
116 else if (streq(arg, "ocspSigning"))
117 {
118 flags |= X509_OCSP_SIGNER;
119 }
120 continue;
121 case 'f':
122 if (!get_form(arg, &form, CRED_CERTIFICATE))
123 {
124 return command_usage("invalid output format");
125 }
126 continue;
127 case 'o':
128 ocsp->insert_last(ocsp, arg);
129 continue;
130 case EOF:
131 break;
132 default:
133 error = "invalid --self option";
134 goto usage;
135 }
136 break;
137 }
138
139 if (!dn)
140 {
141 error = "--dn is required";
142 goto usage;
143 }
144 id = identification_create_from_string(dn);
145 if (id->get_type(id) != ID_DER_ASN1_DN)
146 {
147 error = "supplied --dn is not a distinguished name";
148 goto end;
149 }
150 if (file)
151 {
152 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
153 BUILD_FROM_FILE, file, BUILD_END);
154 }
155 else if (keyid)
156 {
157 chunk_t chunk;
158
159 chunk = chunk_from_hex(chunk_create(keyid, strlen(keyid)), NULL);
160 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_ANY,
161 BUILD_PKCS11_KEYID, chunk, BUILD_END);
162 free(chunk.ptr);
163 }
164 else
165 {
166 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
167 BUILD_FROM_FD, 0, BUILD_END);
168 }
169 if (!private)
170 {
171 error = "loading private key failed";
172 goto end;
173 }
174 public = private->get_public_key(private);
175 if (!public)
176 {
177 error = "extracting public key failed";
178 goto end;
179 }
180 if (hex)
181 {
182 serial = chunk_from_hex(chunk_create(hex, strlen(hex)), NULL);
183 }
184 else
185 {
186 rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
187
188 if (!rng)
189 {
190 error = "no random number generator found";
191 goto end;
192 }
193 rng->allocate_bytes(rng, 8, &serial);
194 while (*serial.ptr == 0x00)
195 {
196 /* we don't accept a serial number with leading zeroes */
197 rng->get_bytes(rng, 1, serial.ptr);
198 }
199 rng->destroy(rng);
200 }
201 not_before = time(NULL);
202 not_after = not_before + lifetime * 24 * 60 * 60;
203 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
204 BUILD_SIGNING_KEY, private, BUILD_PUBLIC_KEY, public,
205 BUILD_SUBJECT, id, BUILD_NOT_BEFORE_TIME, not_before,
206 BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
207 BUILD_DIGEST_ALG, digest, BUILD_X509_FLAG, flags,
208 BUILD_PATHLEN, pathlen, BUILD_SUBJECT_ALTNAMES, san,
209 BUILD_OCSP_ACCESS_LOCATIONS, ocsp, BUILD_END);
210 if (!cert)
211 {
212 error = "generating certificate failed";
213 goto end;
214 }
215 if (!cert->get_encoding(cert, form, &encoding))
216 {
217 error = "encoding certificate failed";
218 goto end;
219 }
220 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
221 {
222 error = "writing certificate key failed";
223 goto end;
224 }
225
226 end:
227 DESTROY_IF(id);
228 DESTROY_IF(cert);
229 DESTROY_IF(public);
230 DESTROY_IF(private);
231 san->destroy_offset(san, offsetof(identification_t, destroy));
232 ocsp->destroy(ocsp);
233 free(encoding.ptr);
234 free(serial.ptr);
235
236 if (error)
237 {
238 fprintf(stderr, "%s\n", error);
239 return 1;
240 }
241 return 0;
242
243 usage:
244 san->destroy_offset(san, offsetof(identification_t, destroy));
245 ocsp->destroy(ocsp);
246 return command_usage(error);
247 }
248
249 /**
250 * Register the command.
251 */
252 static void __attribute__ ((constructor))reg()
253 {
254 command_register((command_t) {
255 self, 's', "self",
256 "create a self signed certificate",
257 {"[--in file | --keyid hex] [--type rsa|ecdsa]",
258 " --dn distinguished-name [--san subjectAltName]+",
259 "[--lifetime days] [--serial hex] [--ca] [--ocsp uri]+",
260 "[--flag serverAuth|clientAuth|ocspSigning]+",
261 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
262 {
263 {"help", 'h', 0, "show usage information"},
264 {"in", 'i', 1, "private key input file, default: stdin"},
265 {"keyid", 'x', 1, "keyid on smartcard of private key"},
266 {"type", 't', 1, "type of input key, default: rsa"},
267 {"dn", 'd', 1, "subject and issuer distinguished name"},
268 {"san", 'a', 1, "subjectAltName to include in certificate"},
269 {"lifetime",'l', 1, "days the certificate is valid, default: 1095"},
270 {"serial", 's', 1, "serial number in hex, default: random"},
271 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
272 {"pathlen", 'p', 1, "set path length constraint"},
273 {"flag", 'e', 1, "include extendedKeyUsage flag"},
274 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
275 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
276 {"outform", 'f', 1, "encoding of generated cert, default: der"},
277 }
278 });
279 }