2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2015 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
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>.
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
22 #include <collections/linked_list.h>
23 #include <credentials/certificates/certificate.h>
24 #include <credentials/certificates/x509.h>
25 #include <asn1/asn1.h>
28 * Free cert policy with OID
30 static void destroy_cert_policy(x509_cert_policy_t
*policy
)
32 free(policy
->oid
.ptr
);
39 static void destroy_policy_mapping(x509_policy_mapping_t
*mapping
)
41 free(mapping
->issuer
.ptr
);
42 free(mapping
->subject
.ptr
);
47 * Create a self signed certificate.
51 cred_encoding_type_t form
= CERT_ASN1_DER
;
52 key_type_t type
= KEY_RSA
;
53 hash_algorithm_t digest
= HASH_UNKNOWN
;
54 certificate_t
*cert
= NULL
;
55 private_key_t
*private = NULL
;
56 public_key_t
*public = NULL
;
57 char *file
= NULL
, *dn
= NULL
, *hex
= NULL
, *error
= NULL
, *keyid
= NULL
;
58 identification_t
*id
= NULL
;
59 linked_list_t
*san
, *ocsp
, *permitted
, *excluded
, *policies
, *mappings
;
60 int pathlen
= X509_NO_CONSTRAINT
, inhibit_any
= X509_NO_CONSTRAINT
;
61 int inhibit_mapping
= X509_NO_CONSTRAINT
;
62 int require_explicit
= X509_NO_CONSTRAINT
;
63 chunk_t serial
= chunk_empty
;
64 chunk_t encoding
= chunk_empty
;
65 time_t not_before
, not_after
, lifetime
= 1095 * 24 * 60 * 60;
66 char *datenb
= NULL
, *datena
= NULL
, *dateform
= NULL
;
67 x509_flag_t flags
= 0;
68 x509_cert_policy_t
*policy
= NULL
;
71 san
= linked_list_create();
72 ocsp
= linked_list_create();
73 permitted
= linked_list_create();
74 excluded
= linked_list_create();
75 policies
= linked_list_create();
76 mappings
= linked_list_create();
80 switch (command_getopt(&arg
))
85 if (streq(arg
, "rsa"))
89 else if (streq(arg
, "ecdsa"))
93 else if (streq(arg
, "bliss"))
99 error
= "invalid input type";
104 if (!enum_from_name(hash_algorithm_short_names
, arg
, &digest
))
106 error
= "invalid --digest type";
120 san
->insert_last(san
, identification_create_from_string(arg
));
123 lifetime
= atoi(arg
) * 24 * 60 * 60;
126 error
= "invalid --lifetime value";
149 permitted
->insert_last(permitted
,
150 identification_create_from_string(arg
));
153 excluded
->insert_last(excluded
,
154 identification_create_from_string(arg
));
160 oid
= asn1_oid_from_string(arg
);
163 error
= "--cert-policy OID invalid";
169 policies
->insert_last(policies
, policy
);
175 error
= "--cps-uri must follow a --cert-policy";
178 policy
->cps_uri
= arg
;
183 error
= "--user-notice must follow a --cert-policy";
186 policy
->unotice_text
= arg
;
190 char *pos
= strchr(arg
, ':');
191 x509_policy_mapping_t
*mapping
;
192 chunk_t subject_oid
, issuer_oid
;
197 issuer_oid
= asn1_oid_from_string(arg
);
198 subject_oid
= asn1_oid_from_string(pos
);
200 if (!pos
|| !issuer_oid
.len
|| !subject_oid
.len
)
202 error
= "--policy-map OIDs invalid";
206 .issuer
= issuer_oid
,
207 .subject
= subject_oid
,
209 mappings
->insert_last(mappings
, mapping
);
213 require_explicit
= atoi(arg
);
216 inhibit_mapping
= atoi(arg
);
219 inhibit_any
= atoi(arg
);
222 if (streq(arg
, "serverAuth"))
224 flags
|= X509_SERVER_AUTH
;
226 else if (streq(arg
, "clientAuth"))
228 flags
|= X509_CLIENT_AUTH
;
230 else if (streq(arg
, "ikeIntermediate"))
232 flags
|= X509_IKE_INTERMEDIATE
;
234 else if (streq(arg
, "crlSign"))
236 flags
|= X509_CRL_SIGN
;
238 else if (streq(arg
, "ocspSigning"))
240 flags
|= X509_OCSP_SIGNER
;
242 else if (streq(arg
, "msSmartcardLogon"))
244 flags
|= X509_MS_SMARTCARD_LOGON
;
248 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
250 error
= "invalid output format";
255 ocsp
->insert_last(ocsp
, arg
);
260 error
= "invalid --self option";
268 error
= "--dn is required";
271 if (!calculate_lifetime(dateform
, datenb
, datena
, lifetime
,
272 ¬_before
, ¬_after
))
274 error
= "invalid --not-before/after datetime";
277 id
= identification_create_from_string(dn
);
278 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
280 error
= "supplied --dn is not a distinguished name";
285 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
286 BUILD_FROM_FILE
, file
, BUILD_END
);
292 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
293 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
294 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
301 set_file_mode(stdin
, CERT_ASN1_DER
);
302 if (!chunk_from_fd(0, &chunk
))
304 fprintf(stderr
, "%s: ", strerror(errno
));
305 error
= "reading private key failed";
308 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
309 BUILD_BLOB
, chunk
, BUILD_END
);
314 error
= "loading private key failed";
317 if (digest
== HASH_UNKNOWN
)
319 digest
= get_default_digest(private);
321 public = private->get_public_key(private);
324 error
= "extracting public key failed";
329 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
333 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
337 error
= "no random number generator found";
340 if (!rng_allocate_bytes_not_zero(rng
, 8, &serial
, FALSE
))
342 error
= "failed to generate serial number";
346 serial
.ptr
[0] &= 0x7F;
349 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
350 BUILD_SIGNING_KEY
, private, BUILD_PUBLIC_KEY
, public,
351 BUILD_SUBJECT
, id
, BUILD_NOT_BEFORE_TIME
, not_before
,
352 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
353 BUILD_DIGEST_ALG
, digest
, BUILD_X509_FLAG
, flags
,
354 BUILD_PATHLEN
, pathlen
, BUILD_SUBJECT_ALTNAMES
, san
,
355 BUILD_OCSP_ACCESS_LOCATIONS
, ocsp
,
356 BUILD_PERMITTED_NAME_CONSTRAINTS
, permitted
,
357 BUILD_EXCLUDED_NAME_CONSTRAINTS
, excluded
,
358 BUILD_CERTIFICATE_POLICIES
, policies
,
359 BUILD_POLICY_MAPPINGS
, mappings
,
360 BUILD_POLICY_REQUIRE_EXPLICIT
, require_explicit
,
361 BUILD_POLICY_INHIBIT_MAPPING
, inhibit_mapping
,
362 BUILD_POLICY_INHIBIT_ANY
, inhibit_any
,
366 error
= "generating certificate failed";
369 if (!cert
->get_encoding(cert
, form
, &encoding
))
371 error
= "encoding certificate failed";
374 set_file_mode(stdout
, form
);
375 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
377 error
= "writing certificate key failed";
386 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
387 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
388 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
389 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
390 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
397 fprintf(stderr
, "%s\n", error
);
403 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
404 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
405 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
406 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
407 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
409 return command_usage(error
);
413 * Register the command.
415 static void __attribute__ ((constructor
))reg()
417 command_register((command_t
) {
419 "create a self signed certificate",
420 {" [--in file|--keyid hex] [--type rsa|ecdsa|bliss]",
421 " --dn distinguished-name [--san subjectAltName]+",
422 "[--lifetime days] [--serial hex] [--ca] [--ocsp uri]+",
423 "[--flag serverAuth|clientAuth|crlSign|ocspSigning|msSmartcardLogon]+",
424 "[--nc-permitted name] [--nc-excluded name]",
425 "[--policy-map issuer-oid:subject-oid]",
426 "[--policy-explicit len] [--policy-inhibit len] [--policy-any len]",
427 "[--cert-policy oid [--cps-uri uri] [--user-notice text]]+",
428 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
430 {"help", 'h', 0, "show usage information"},
431 {"in", 'i', 1, "private key input file, default: stdin"},
432 {"keyid", 'x', 1, "keyid on smartcard of private key"},
433 {"type", 't', 1, "type of input key, default: rsa"},
434 {"dn", 'd', 1, "subject and issuer distinguished name"},
435 {"san", 'a', 1, "subjectAltName to include in certificate"},
436 {"lifetime", 'l', 1, "days the certificate is valid, default: 1095"},
437 {"not-before", 'F', 1, "date/time the validity of the cert starts"},
438 {"not-after", 'T', 1, "date/time the validity of the cert ends"},
439 {"dateform", 'D', 1, "strptime(3) input format, default: %d.%m.%y %T"},
440 {"serial", 's', 1, "serial number in hex, default: random"},
441 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
442 {"pathlen", 'p', 1, "set path length constraint"},
443 {"nc-permitted", 'n', 1, "add permitted NameConstraint"},
444 {"nc-excluded", 'N', 1, "add excluded NameConstraint"},
445 {"cert-policy", 'P', 1, "certificatePolicy OID to include"},
446 {"cps-uri", 'C', 1, "Certification Practice statement URI for certificatePolicy"},
447 {"user-notice", 'U', 1, "user notice for certificatePolicy"},
448 {"policy-mapping", 'M', 1, "policyMapping from issuer to subject OID"},
449 {"policy-explicit", 'E', 1, "requireExplicitPolicy constraint"},
450 {"policy-inhibit", 'H', 1, "inhibitPolicyMapping constraint"},
451 {"policy-any", 'A', 1, "inhibitAnyPolicy constraint"},
452 {"flag", 'e', 1, "include extendedKeyUsage flag"},
453 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
454 {"digest", 'g', 1, "digest for signature creation, default: key-specific"},
455 {"outform", 'f', 1, "encoding of generated cert, default: der"},