Slightly renamed different policyConstraints to distinguish them better
[strongswan.git] / src / pki / commands / issue.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 <debug.h>
21 #include <asn1/asn1.h>
22 #include <utils/linked_list.h>
23 #include <credentials/certificates/certificate.h>
24 #include <credentials/certificates/x509.h>
25 #include <credentials/certificates/pkcs10.h>
26
27 /**
28 * Free cert policy with OID
29 */
30 static void destroy_cert_policy(x509_cert_policy_t *policy)
31 {
32 free(policy->oid.ptr);
33 free(policy);
34 }
35
36 /**
37 * Free policy mapping
38 */
39 static void destroy_policy_mapping(x509_policy_mapping_t *mapping)
40 {
41 free(mapping->issuer.ptr);
42 free(mapping->subject.ptr);
43 free(mapping);
44 }
45
46 /**
47 * Free a CRL DistributionPoint
48 */
49 static void destroy_cdp(x509_cdp_t *this)
50 {
51 DESTROY_IF(this->issuer);
52 free(this);
53 }
54
55 /**
56 * Issue a certificate using a CA certificate and key
57 */
58 static int issue()
59 {
60 cred_encoding_type_t form = CERT_ASN1_DER;
61 hash_algorithm_t digest = HASH_SHA1;
62 certificate_t *cert_req = NULL, *cert = NULL, *ca =NULL;
63 private_key_t *private = NULL;
64 public_key_t *public = NULL;
65 bool pkcs10 = FALSE;
66 char *file = NULL, *dn = NULL, *hex = NULL, *cacert = NULL, *cakey = NULL;
67 char *error = NULL, *keyid = NULL;
68 identification_t *id = NULL;
69 linked_list_t *san, *cdps, *ocsp, *permitted, *excluded, *policies, *mappings;
70 int lifetime = 1095;
71 int pathlen = X509_NO_CONSTRAINT, inhibit_any = X509_NO_CONSTRAINT;
72 int inhibit_mapping = X509_NO_CONSTRAINT, require_explicit = X509_NO_CONSTRAINT;
73 chunk_t serial = chunk_empty;
74 chunk_t encoding = chunk_empty;
75 time_t not_before, not_after;
76 x509_flag_t flags = 0;
77 x509_t *x509;
78 x509_cdp_t *cdp = NULL;
79 x509_cert_policy_t *policy = NULL;
80 char *arg;
81
82 san = linked_list_create();
83 cdps = linked_list_create();
84 ocsp = linked_list_create();
85 permitted = linked_list_create();
86 excluded = linked_list_create();
87 policies = linked_list_create();
88 mappings = linked_list_create();
89
90 while (TRUE)
91 {
92 switch (command_getopt(&arg))
93 {
94 case 'h':
95 goto usage;
96 case 't':
97 if (streq(arg, "pkcs10"))
98 {
99 pkcs10 = TRUE;
100 }
101 else if (!streq(arg, "pub"))
102 {
103 error = "invalid input type";
104 goto usage;
105 }
106 continue;
107 case 'g':
108 digest = get_digest(arg);
109 if (digest == HASH_UNKNOWN)
110 {
111 error = "invalid --digest type";
112 goto usage;
113 }
114 continue;
115 case 'i':
116 file = arg;
117 continue;
118 case 'c':
119 cacert = arg;
120 continue;
121 case 'k':
122 cakey = arg;
123 continue;
124 case 'x':
125 keyid = arg;
126 continue;
127 case 'd':
128 dn = arg;
129 continue;
130 case 'a':
131 san->insert_last(san, identification_create_from_string(arg));
132 continue;
133 case 'l':
134 lifetime = atoi(arg);
135 if (!lifetime)
136 {
137 error = "invalid --lifetime value";
138 goto usage;
139 }
140 continue;
141 case 's':
142 hex = arg;
143 continue;
144 case 'b':
145 flags |= X509_CA;
146 continue;
147 case 'p':
148 pathlen = atoi(arg);
149 continue;
150 case 'n':
151 permitted->insert_last(permitted,
152 identification_create_from_string(arg));
153 continue;
154 case 'N':
155 excluded->insert_last(excluded,
156 identification_create_from_string(arg));
157 continue;
158 case 'P':
159 {
160 chunk_t oid;
161
162 oid = asn1_oid_from_string(arg);
163 if (!oid.len)
164 {
165 error = "--cert-policy OID invalid";
166 goto usage;
167 }
168 INIT(policy,
169 .oid = oid,
170 );
171 policies->insert_last(policies, policy);
172 continue;
173 }
174 case 'C':
175 if (!policy)
176 {
177 error = "--cps-uri must follow a --cert-policy";
178 goto usage;
179 }
180 policy->cps_uri = arg;
181 continue;
182 case 'U':
183 if (!policy)
184 {
185 error = "--user-notice must follow a --cert-policy";
186 goto usage;
187 }
188 policy->unotice_text = arg;
189 continue;
190 case 'M':
191 {
192 char *pos = strchr(arg, ':');
193 x509_policy_mapping_t *mapping;
194 chunk_t subject_oid, issuer_oid;
195
196 if (pos)
197 {
198 *pos++ = '\0';
199 issuer_oid = asn1_oid_from_string(arg);
200 subject_oid = asn1_oid_from_string(pos);
201 }
202 if (!pos || !issuer_oid.len || !subject_oid.len)
203 {
204 error = "--policy-map OIDs invalid";
205 goto usage;
206 }
207 INIT(mapping,
208 .issuer = issuer_oid,
209 .subject = subject_oid,
210 );
211 mappings->insert_last(mappings, mapping);
212 continue;
213 }
214 case 'E':
215 require_explicit = atoi(arg);
216 continue;
217 case 'H':
218 inhibit_mapping = atoi(arg);
219 continue;
220 case 'A':
221 inhibit_any = atoi(arg);
222 continue;
223 case 'e':
224 if (streq(arg, "serverAuth"))
225 {
226 flags |= X509_SERVER_AUTH;
227 }
228 else if (streq(arg, "clientAuth"))
229 {
230 flags |= X509_CLIENT_AUTH;
231 }
232 else if (streq(arg, "crlSign"))
233 {
234 flags |= X509_CRL_SIGN;
235 }
236 else if (streq(arg, "ocspSigning"))
237 {
238 flags |= X509_OCSP_SIGNER;
239 }
240 continue;
241 case 'f':
242 if (!get_form(arg, &form, CRED_CERTIFICATE))
243 {
244 error = "invalid output format";
245 goto usage;
246 }
247 continue;
248 case 'u':
249 INIT(cdp,
250 .uri = arg,
251 );
252 cdps->insert_last(cdps, cdp);
253 continue;
254 case 'I':
255 if (!cdp || cdp->issuer)
256 {
257 error = "--crlissuer must follow a --crl";
258 goto usage;
259 }
260 cdp->issuer = identification_create_from_string(arg);
261 continue;
262 case 'o':
263 ocsp->insert_last(ocsp, arg);
264 continue;
265 case EOF:
266 break;
267 default:
268 error = "invalid --issue option";
269 goto usage;
270 }
271 break;
272 }
273
274 if (!pkcs10 && !dn)
275 {
276 error = "--dn is required";
277 goto usage;
278 }
279 if (!cacert)
280 {
281 error = "--cacert is required";
282 goto usage;
283 }
284 if (!cakey && !keyid)
285 {
286 error = "--cakey or --keyid is required";
287 goto usage;
288 }
289 if (dn)
290 {
291 id = identification_create_from_string(dn);
292 if (id->get_type(id) != ID_DER_ASN1_DN)
293 {
294 error = "supplied --dn is not a distinguished name";
295 goto end;
296 }
297 }
298
299 DBG2(DBG_LIB, "Reading ca certificate:");
300 ca = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
301 BUILD_FROM_FILE, cacert, BUILD_END);
302 if (!ca)
303 {
304 error = "parsing CA certificate failed";
305 goto end;
306 }
307 x509 = (x509_t*)ca;
308 if (!(x509->get_flags(x509) & X509_CA))
309 {
310 error = "CA certificate misses CA basicConstraint";
311 goto end;
312 }
313 public = ca->get_public_key(ca);
314 if (!public)
315 {
316 error = "extracting CA certificate public key failed";
317 goto end;
318 }
319
320 DBG2(DBG_LIB, "Reading ca private key:");
321 if (cakey)
322 {
323 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
324 public->get_type(public),
325 BUILD_FROM_FILE, cakey, BUILD_END);
326 }
327 else
328 {
329 chunk_t chunk;
330
331 chunk = chunk_from_hex(chunk_create(keyid, strlen(keyid)), NULL);
332 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_ANY,
333 BUILD_PKCS11_KEYID, chunk, BUILD_END);
334 free(chunk.ptr);
335 }
336 if (!private)
337 {
338 error = "loading CA private key failed";
339 goto end;
340 }
341 if (!private->belongs_to(private, public))
342 {
343 error = "CA private key does not match CA certificate";
344 goto end;
345 }
346 public->destroy(public);
347
348 if (hex)
349 {
350 serial = chunk_from_hex(chunk_create(hex, strlen(hex)), NULL);
351 }
352 else
353 {
354 rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
355
356 if (!rng)
357 {
358 error = "no random number generator found";
359 goto end;
360 }
361 rng->allocate_bytes(rng, 8, &serial);
362 while (*serial.ptr == 0x00)
363 {
364 /* we don't accept a serial number with leading zeroes */
365 rng->get_bytes(rng, 1, serial.ptr);
366 }
367 rng->destroy(rng);
368 }
369
370 if (pkcs10)
371 {
372 enumerator_t *enumerator;
373 identification_t *subjectAltName;
374 pkcs10_t *req;
375
376 DBG2(DBG_LIB, "Reading certificate request");
377 if (file)
378 {
379 cert_req = lib->creds->create(lib->creds, CRED_CERTIFICATE,
380 CERT_PKCS10_REQUEST,
381 BUILD_FROM_FILE, file, BUILD_END);
382 }
383 else
384 {
385 cert_req = lib->creds->create(lib->creds, CRED_CERTIFICATE,
386 CERT_PKCS10_REQUEST,
387 BUILD_FROM_FD, 0, BUILD_END);
388 }
389 if (!cert_req)
390 {
391 error = "parsing certificate request failed";
392 goto end;
393 }
394
395 /* If not set yet use subject from PKCS#10 certificate request as DN */
396 if (!id)
397 {
398 id = cert_req->get_subject(cert_req);
399 id = id->clone(id);
400 }
401
402 /* Add subjectAltNames from PKCS#10 certificate request */
403 req = (pkcs10_t*)cert_req;
404 enumerator = req->create_subjectAltName_enumerator(req);
405 while (enumerator->enumerate(enumerator, &subjectAltName))
406 {
407 san->insert_last(san, subjectAltName->clone(subjectAltName));
408 }
409 enumerator->destroy(enumerator);
410
411 /* Use public key from PKCS#10 certificate request */
412 public = cert_req->get_public_key(cert_req);
413 }
414 else
415 {
416 DBG2(DBG_LIB, "Reading public key:");
417 if (file)
418 {
419 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
420 BUILD_FROM_FILE, file, BUILD_END);
421 }
422 else
423 {
424 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
425 BUILD_FROM_FD, 0, BUILD_END);
426 }
427 }
428 if (!public)
429 {
430 error = "parsing public key failed";
431 goto end;
432 }
433
434 not_before = time(NULL);
435 not_after = not_before + lifetime * 24 * 60 * 60;
436
437 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
438 BUILD_SIGNING_KEY, private, BUILD_SIGNING_CERT, ca,
439 BUILD_PUBLIC_KEY, public, BUILD_SUBJECT, id,
440 BUILD_NOT_BEFORE_TIME, not_before, BUILD_DIGEST_ALG, digest,
441 BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
442 BUILD_SUBJECT_ALTNAMES, san, BUILD_X509_FLAG, flags,
443 BUILD_PATHLEN, pathlen,
444 BUILD_CRL_DISTRIBUTION_POINTS, cdps,
445 BUILD_OCSP_ACCESS_LOCATIONS, ocsp,
446 BUILD_PERMITTED_NAME_CONSTRAINTS, permitted,
447 BUILD_EXCLUDED_NAME_CONSTRAINTS, excluded,
448 BUILD_CERTIFICATE_POLICIES, policies,
449 BUILD_POLICY_MAPPINGS, mappings,
450 BUILD_POLICY_REQUIRE_EXPLICIT, require_explicit,
451 BUILD_POLICY_INHIBIT_MAPPING, inhibit_mapping,
452 BUILD_POLICY_INHIBIT_ANY, inhibit_any,
453 BUILD_END);
454 if (!cert)
455 {
456 error = "generating certificate failed";
457 goto end;
458 }
459 if (!cert->get_encoding(cert, form, &encoding))
460 {
461 error = "encoding certificate failed";
462 goto end;
463 }
464 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
465 {
466 error = "writing certificate key failed";
467 goto end;
468 }
469
470 end:
471 DESTROY_IF(id);
472 DESTROY_IF(cert_req);
473 DESTROY_IF(cert);
474 DESTROY_IF(ca);
475 DESTROY_IF(public);
476 DESTROY_IF(private);
477 san->destroy_offset(san, offsetof(identification_t, destroy));
478 permitted->destroy_offset(permitted, offsetof(identification_t, destroy));
479 excluded->destroy_offset(excluded, offsetof(identification_t, destroy));
480 policies->destroy_function(policies, (void*)destroy_cert_policy);
481 mappings->destroy_function(mappings, (void*)destroy_policy_mapping);
482 cdps->destroy_function(cdps, (void*)destroy_cdp);
483 ocsp->destroy(ocsp);
484 free(encoding.ptr);
485 free(serial.ptr);
486
487 if (error)
488 {
489 fprintf(stderr, "%s\n", error);
490 return 1;
491 }
492 return 0;
493
494 usage:
495 san->destroy_offset(san, offsetof(identification_t, destroy));
496 permitted->destroy_offset(permitted, offsetof(identification_t, destroy));
497 excluded->destroy_offset(excluded, offsetof(identification_t, destroy));
498 policies->destroy_function(policies, (void*)destroy_cert_policy);
499 mappings->destroy_function(mappings, (void*)destroy_policy_mapping);
500 cdps->destroy_function(cdps, (void*)destroy_cdp);
501 ocsp->destroy(ocsp);
502 return command_usage(error);
503 }
504
505 /**
506 * Register the command.
507 */
508 static void __attribute__ ((constructor))reg()
509 {
510 command_register((command_t) {
511 issue, 'i', "issue",
512 "issue a certificate using a CA certificate and key",
513 {"[--in file] [--type pub|pkcs10] --cakey file | --cakeyid hex",
514 " --cacert file --dn subject-dn [--san subjectAltName]+",
515 "[--lifetime days] [--serial hex] [--crl uri [--crlissuer i] ]+ [--ocsp uri]+",
516 "[--ca] [--pathlen len] [--flag serverAuth|clientAuth|crlSign|ocspSigning]+",
517 "[--nc-permitted name] [--nc-excluded name]",
518 "[--cert-policy oid [--cps-uri uri] [--user-notice text] ]+",
519 "[--policy-map issuer-oid:subject-oid]",
520 "[--policy-explicit len] [--policy-inhibit len] [--policy-any len]",
521 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
522 {
523 {"help", 'h', 0, "show usage information"},
524 {"in", 'i', 1, "public key/request file to issue, default: stdin"},
525 {"type", 't', 1, "type of input, default: pub"},
526 {"cacert", 'c', 1, "CA certificate file"},
527 {"cakey", 'k', 1, "CA private key file"},
528 {"cakeyid", 'x', 1, "keyid on smartcard of CA private key"},
529 {"dn", 'd', 1, "distinguished name to include as subject"},
530 {"san", 'a', 1, "subjectAltName to include in certificate"},
531 {"lifetime", 'l', 1, "days the certificate is valid, default: 1095"},
532 {"serial", 's', 1, "serial number in hex, default: random"},
533 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
534 {"pathlen", 'p', 1, "set path length constraint"},
535 {"nc-permitted", 'n', 1, "add permitted NameConstraint"},
536 {"nc-excluded", 'N', 1, "add excluded NameConstraint"},
537 {"cert-policy", 'P', 1, "certificatePolicy OID to include"},
538 {"cps-uri", 'C', 1, "Certification Practice statement URI for certificatePolicy"},
539 {"user-notice", 'U', 1, "user notice for certificatePolicy"},
540 {"policy-mapping", 'M', 1, "policyMapping from issuer to subject OID"},
541 {"policy-explicit", 'E', 1, "requireExplicitPolicy constraint"},
542 {"policy-inhibit", 'H', 1, "inhibitPolicyMapping constraint"},
543 {"policy-any", 'A', 1, "inhibitAnyPolicy constraint"},
544 {"flag", 'e', 1, "include extendedKeyUsage flag"},
545 {"crl", 'u', 1, "CRL distribution point URI to include"},
546 {"crlissuer", 'I', 1, "CRL Issuer for CRL at distribution point"},
547 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
548 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
549 {"outform", 'f', 1, "encoding of generated cert, default: der"},
550 }
551 });
552 }
553