pki: Optionally generate RSA/PSS signatures
[strongswan.git] / src / pki / commands / self.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2015-2017 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
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 */
16
17 #include <time.h>
18 #include <errno.h>
19
20 #include "pki.h"
21
22 #include <collections/linked_list.h>
23 #include <credentials/certificates/certificate.h>
24 #include <credentials/certificates/x509.h>
25 #include <selectors/traffic_selector.h>
26 #include <asn1/asn1.h>
27
28 /**
29 * Free cert policy with OID
30 */
31 static void destroy_cert_policy(x509_cert_policy_t *policy)
32 {
33 free(policy->oid.ptr);
34 free(policy);
35 }
36
37 /**
38 * Free policy mapping
39 */
40 static void destroy_policy_mapping(x509_policy_mapping_t *mapping)
41 {
42 free(mapping->issuer.ptr);
43 free(mapping->subject.ptr);
44 free(mapping);
45 }
46
47 /**
48 * Create a self signed certificate.
49 */
50 static int self()
51 {
52 cred_encoding_type_t form = CERT_ASN1_DER;
53 key_type_t type = KEY_ANY;
54 hash_algorithm_t digest = HASH_UNKNOWN;
55 signature_params_t *scheme = NULL;
56 certificate_t *cert = NULL;
57 private_key_t *private = NULL;
58 public_key_t *public = NULL;
59 char *file = NULL, *dn = NULL, *hex = NULL, *error = NULL, *keyid = NULL;
60 identification_t *id = NULL;
61 linked_list_t *san, *ocsp, *permitted, *excluded, *policies, *mappings;
62 linked_list_t *addrblocks;
63 int pathlen = X509_NO_CONSTRAINT, inhibit_any = X509_NO_CONSTRAINT;
64 int inhibit_mapping = X509_NO_CONSTRAINT;
65 int require_explicit = X509_NO_CONSTRAINT;
66 chunk_t serial = chunk_empty;
67 chunk_t encoding = chunk_empty;
68 time_t not_before, not_after, lifetime = 1095 * 24 * 60 * 60;
69 char *datenb = NULL, *datena = NULL, *dateform = NULL;
70 x509_flag_t flags = 0;
71 x509_cert_policy_t *policy = NULL;
72 traffic_selector_t *ts;
73 char *arg;
74 bool pss = FALSE;
75
76 san = linked_list_create();
77 ocsp = linked_list_create();
78 permitted = linked_list_create();
79 excluded = linked_list_create();
80 policies = linked_list_create();
81 mappings = linked_list_create();
82 addrblocks = linked_list_create();
83
84 while (TRUE)
85 {
86 switch (command_getopt(&arg))
87 {
88 case 'h':
89 goto usage;
90 case 't':
91 if (streq(arg, "rsa"))
92 {
93 type = KEY_RSA;
94 }
95 else if (streq(arg, "ecdsa"))
96 {
97 type = KEY_ECDSA;
98 }
99 else if (streq(arg, "ed25519"))
100 {
101 type = KEY_ED25519;
102 }
103 else if (streq(arg, "bliss"))
104 {
105 type = KEY_BLISS;
106 }
107 else if (streq(arg, "priv"))
108 {
109 type = KEY_ANY;
110 }
111 else
112 {
113 error = "invalid input type";
114 goto usage;
115 }
116 continue;
117 case 'g':
118 if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
119 {
120 error = "invalid --digest type";
121 goto usage;
122 }
123 continue;
124 case 'R':
125 if (streq(arg, "pss"))
126 {
127 pss = TRUE;
128 }
129 else if (!streq(arg, "pkcs1"))
130 {
131 error = "invalid RSA padding";
132 goto usage;
133 }
134 continue;
135 case 'i':
136 file = arg;
137 continue;
138 case 'x':
139 keyid = arg;
140 continue;
141 case 'd':
142 dn = arg;
143 continue;
144 case 'a':
145 san->insert_last(san, identification_create_from_string(arg));
146 continue;
147 case 'l':
148 lifetime = atoi(arg) * 24 * 60 * 60;
149 if (!lifetime)
150 {
151 error = "invalid --lifetime value";
152 goto usage;
153 }
154 continue;
155 case 'D':
156 dateform = arg;
157 continue;
158 case 'F':
159 datenb = arg;
160 continue;
161 case 'T':
162 datena = arg;
163 continue;
164 case 's':
165 hex = arg;
166 continue;
167 case 'b':
168 flags |= X509_CA;
169 continue;
170 case 'p':
171 pathlen = atoi(arg);
172 continue;
173 case 'B':
174 ts = parse_ts(arg);
175 if (!ts)
176 {
177 error = "invalid addressBlock";
178 goto usage;
179 }
180 addrblocks->insert_last(addrblocks, ts);
181 continue;
182 case 'n':
183 permitted->insert_last(permitted,
184 identification_create_from_string(arg));
185 continue;
186 case 'N':
187 excluded->insert_last(excluded,
188 identification_create_from_string(arg));
189 continue;
190 case 'P':
191 {
192 chunk_t oid;
193
194 oid = asn1_oid_from_string(arg);
195 if (!oid.len)
196 {
197 error = "--cert-policy OID invalid";
198 goto usage;
199 }
200 INIT(policy,
201 .oid = oid,
202 );
203 policies->insert_last(policies, policy);
204 continue;
205 }
206 case 'C':
207 if (!policy)
208 {
209 error = "--cps-uri must follow a --cert-policy";
210 goto usage;
211 }
212 policy->cps_uri = arg;
213 continue;
214 case 'U':
215 if (!policy)
216 {
217 error = "--user-notice must follow a --cert-policy";
218 goto usage;
219 }
220 policy->unotice_text = arg;
221 continue;
222 case 'M':
223 {
224 char *pos = strchr(arg, ':');
225 x509_policy_mapping_t *mapping;
226 chunk_t subject_oid, issuer_oid;
227
228 if (pos)
229 {
230 *pos++ = '\0';
231 issuer_oid = asn1_oid_from_string(arg);
232 subject_oid = asn1_oid_from_string(pos);
233 }
234 if (!pos || !issuer_oid.len || !subject_oid.len)
235 {
236 error = "--policy-map OIDs invalid";
237 goto usage;
238 }
239 INIT(mapping,
240 .issuer = issuer_oid,
241 .subject = subject_oid,
242 );
243 mappings->insert_last(mappings, mapping);
244 continue;
245 }
246 case 'E':
247 require_explicit = atoi(arg);
248 continue;
249 case 'H':
250 inhibit_mapping = atoi(arg);
251 continue;
252 case 'A':
253 inhibit_any = atoi(arg);
254 continue;
255 case 'e':
256 if (streq(arg, "serverAuth"))
257 {
258 flags |= X509_SERVER_AUTH;
259 }
260 else if (streq(arg, "clientAuth"))
261 {
262 flags |= X509_CLIENT_AUTH;
263 }
264 else if (streq(arg, "ikeIntermediate"))
265 {
266 flags |= X509_IKE_INTERMEDIATE;
267 }
268 else if (streq(arg, "crlSign"))
269 {
270 flags |= X509_CRL_SIGN;
271 }
272 else if (streq(arg, "ocspSigning"))
273 {
274 flags |= X509_OCSP_SIGNER;
275 }
276 else if (streq(arg, "msSmartcardLogon"))
277 {
278 flags |= X509_MS_SMARTCARD_LOGON;
279 }
280 continue;
281 case 'f':
282 if (!get_form(arg, &form, CRED_CERTIFICATE))
283 {
284 error = "invalid output format";
285 goto usage;
286 }
287 continue;
288 case 'o':
289 ocsp->insert_last(ocsp, arg);
290 continue;
291 case EOF:
292 break;
293 default:
294 error = "invalid --self option";
295 goto usage;
296 }
297 break;
298 }
299
300 if (!dn)
301 {
302 error = "--dn is required";
303 goto usage;
304 }
305 if (!calculate_lifetime(dateform, datenb, datena, lifetime,
306 &not_before, &not_after))
307 {
308 error = "invalid --not-before/after datetime";
309 goto usage;
310 }
311 id = identification_create_from_string(dn);
312 if (id->get_type(id) != ID_DER_ASN1_DN)
313 {
314 error = "supplied --dn is not a distinguished name";
315 goto end;
316 }
317 if (file)
318 {
319 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
320 BUILD_FROM_FILE, file, BUILD_END);
321 }
322 else if (keyid)
323 {
324 chunk_t chunk;
325
326 chunk = chunk_from_hex(chunk_create(keyid, strlen(keyid)), NULL);
327 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_ANY,
328 BUILD_PKCS11_KEYID, chunk, BUILD_END);
329 free(chunk.ptr);
330 }
331 else
332 {
333 chunk_t chunk;
334
335 set_file_mode(stdin, CERT_ASN1_DER);
336 if (!chunk_from_fd(0, &chunk))
337 {
338 fprintf(stderr, "%s: ", strerror(errno));
339 error = "reading private key failed";
340 goto end;
341 }
342 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
343 BUILD_BLOB, chunk, BUILD_END);
344 free(chunk.ptr);
345 }
346 if (!private)
347 {
348 error = "loading private key failed";
349 goto end;
350 }
351 public = private->get_public_key(private);
352 if (!public)
353 {
354 error = "extracting public key failed";
355 goto end;
356 }
357 if (hex)
358 {
359 serial = chunk_from_hex(chunk_create(hex, strlen(hex)), NULL);
360 }
361 else
362 {
363 rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
364
365 if (!rng)
366 {
367 error = "no random number generator found";
368 goto end;
369 }
370 if (!rng_allocate_bytes_not_zero(rng, 8, &serial, FALSE))
371 {
372 error = "failed to generate serial number";
373 rng->destroy(rng);
374 goto end;
375 }
376 serial.ptr[0] &= 0x7F;
377 rng->destroy(rng);
378 }
379 scheme = get_signature_scheme(private, digest, pss);
380
381 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
382 BUILD_SIGNING_KEY, private, BUILD_PUBLIC_KEY, public,
383 BUILD_SUBJECT, id, BUILD_NOT_BEFORE_TIME, not_before,
384 BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
385 BUILD_SIGNATURE_SCHEME, scheme, BUILD_X509_FLAG, flags,
386 BUILD_PATHLEN, pathlen, BUILD_SUBJECT_ALTNAMES, san,
387 BUILD_ADDRBLOCKS, addrblocks,
388 BUILD_OCSP_ACCESS_LOCATIONS, ocsp,
389 BUILD_PERMITTED_NAME_CONSTRAINTS, permitted,
390 BUILD_EXCLUDED_NAME_CONSTRAINTS, excluded,
391 BUILD_CERTIFICATE_POLICIES, policies,
392 BUILD_POLICY_MAPPINGS, mappings,
393 BUILD_POLICY_REQUIRE_EXPLICIT, require_explicit,
394 BUILD_POLICY_INHIBIT_MAPPING, inhibit_mapping,
395 BUILD_POLICY_INHIBIT_ANY, inhibit_any,
396 BUILD_END);
397 if (!cert)
398 {
399 error = "generating certificate failed";
400 goto end;
401 }
402 if (!cert->get_encoding(cert, form, &encoding))
403 {
404 error = "encoding certificate failed";
405 goto end;
406 }
407 set_file_mode(stdout, form);
408 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
409 {
410 error = "writing certificate key failed";
411 goto end;
412 }
413
414 end:
415 DESTROY_IF(id);
416 DESTROY_IF(cert);
417 DESTROY_IF(public);
418 DESTROY_IF(private);
419 san->destroy_offset(san, offsetof(identification_t, destroy));
420 permitted->destroy_offset(permitted, offsetof(identification_t, destroy));
421 excluded->destroy_offset(excluded, offsetof(identification_t, destroy));
422 addrblocks->destroy_offset(addrblocks, offsetof(traffic_selector_t, destroy));
423 policies->destroy_function(policies, (void*)destroy_cert_policy);
424 mappings->destroy_function(mappings, (void*)destroy_policy_mapping);
425 ocsp->destroy(ocsp);
426 signature_params_destroy(scheme);
427 free(encoding.ptr);
428 free(serial.ptr);
429
430 if (error)
431 {
432 fprintf(stderr, "%s\n", error);
433 return 1;
434 }
435 return 0;
436
437 usage:
438 san->destroy_offset(san, offsetof(identification_t, destroy));
439 permitted->destroy_offset(permitted, offsetof(identification_t, destroy));
440 excluded->destroy_offset(excluded, offsetof(identification_t, destroy));
441 addrblocks->destroy_offset(addrblocks, offsetof(traffic_selector_t, destroy));
442 policies->destroy_function(policies, (void*)destroy_cert_policy);
443 mappings->destroy_function(mappings, (void*)destroy_policy_mapping);
444 ocsp->destroy(ocsp);
445 return command_usage(error);
446 }
447
448 /**
449 * Register the command.
450 */
451 static void __attribute__ ((constructor))reg()
452 {
453 command_register((command_t) {
454 self, 's', "self",
455 "create a self signed certificate",
456 {"[--in file|--keyid hex] [--type rsa|ecdsa|ed25519|bliss|priv]",
457 " --dn distinguished-name [--san subjectAltName]+",
458 "[--lifetime days] [--serial hex] [--ca] [--ocsp uri]+",
459 "[--flag serverAuth|clientAuth|crlSign|ocspSigning|msSmartcardLogon]+",
460 "[--nc-permitted name] [--nc-excluded name]",
461 "[--policy-map issuer-oid:subject-oid]",
462 "[--policy-explicit len] [--policy-inhibit len] [--policy-any len]",
463 "[--cert-policy oid [--cps-uri uri] [--user-notice text]]+",
464 "[--digest md5|sha1|sha224|sha256|sha384|sha512|sha3_224|sha3_256|sha3_384|sha3_512]",
465 "[--rsa-padding pkcs1|pss]",
466 "[--outform der|pem]"},
467 {
468 {"help", 'h', 0, "show usage information"},
469 {"in", 'i', 1, "private key input file, default: stdin"},
470 {"keyid", 'x', 1, "smartcard or TPM private key object handle"},
471 {"type", 't', 1, "type of input key, default: priv"},
472 {"dn", 'd', 1, "subject and issuer distinguished name"},
473 {"san", 'a', 1, "subjectAltName to include in certificate"},
474 {"lifetime", 'l', 1, "days the certificate is valid, default: 1095"},
475 {"not-before", 'F', 1, "date/time the validity of the cert starts"},
476 {"not-after", 'T', 1, "date/time the validity of the cert ends"},
477 {"dateform", 'D', 1, "strptime(3) input format, default: %d.%m.%y %T"},
478 {"serial", 's', 1, "serial number in hex, default: random"},
479 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
480 {"pathlen", 'p', 1, "set path length constraint"},
481 {"addrblock", 'B', 1, "RFC 3779 addrBlock to include"},
482 {"nc-permitted", 'n', 1, "add permitted NameConstraint"},
483 {"nc-excluded", 'N', 1, "add excluded NameConstraint"},
484 {"cert-policy", 'P', 1, "certificatePolicy OID to include"},
485 {"cps-uri", 'C', 1, "Certification Practice statement URI for certificatePolicy"},
486 {"user-notice", 'U', 1, "user notice for certificatePolicy"},
487 {"policy-mapping", 'M', 1, "policyMapping from issuer to subject OID"},
488 {"policy-explicit", 'E', 1, "requireExplicitPolicy constraint"},
489 {"policy-inhibit", 'H', 1, "inhibitPolicyMapping constraint"},
490 {"policy-any", 'A', 1, "inhibitAnyPolicy constraint"},
491 {"flag", 'e', 1, "include extendedKeyUsage flag"},
492 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
493 {"digest", 'g', 1, "digest for signature creation, default: key-specific"},
494 {"rsa-padding", 'R', 1, "padding for RSA signatures, default: pkcs1"},
495 {"outform", 'f', 1, "encoding of generated cert, default: der"},
496 }
497 });
498 }