2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2015-2017 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 <selectors/traffic_selector.h>
26 #include <asn1/asn1.h>
29 * Free cert policy with OID
31 static void destroy_cert_policy(x509_cert_policy_t
*policy
)
33 free(policy
->oid
.ptr
);
40 static void destroy_policy_mapping(x509_policy_mapping_t
*mapping
)
42 free(mapping
->issuer
.ptr
);
43 free(mapping
->subject
.ptr
);
48 * Create a self signed certificate.
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
;
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();
86 switch (command_getopt(&arg
))
91 if (streq(arg
, "rsa"))
95 else if (streq(arg
, "ecdsa"))
99 else if (streq(arg
, "ed25519"))
103 else if (streq(arg
, "bliss"))
107 else if (streq(arg
, "priv"))
113 error
= "invalid input type";
118 if (!enum_from_name(hash_algorithm_short_names
, arg
, &digest
))
120 error
= "invalid --digest type";
125 if (streq(arg
, "pss"))
129 else if (!streq(arg
, "pkcs1"))
131 error
= "invalid RSA padding";
145 san
->insert_last(san
, identification_create_from_string(arg
));
148 lifetime
= atoi(arg
) * 24 * 60 * 60;
151 error
= "invalid --lifetime value";
177 error
= "invalid addressBlock";
180 addrblocks
->insert_last(addrblocks
, ts
);
183 permitted
->insert_last(permitted
,
184 identification_create_from_string(arg
));
187 excluded
->insert_last(excluded
,
188 identification_create_from_string(arg
));
194 oid
= asn1_oid_from_string(arg
);
197 error
= "--cert-policy OID invalid";
203 policies
->insert_last(policies
, policy
);
209 error
= "--cps-uri must follow a --cert-policy";
212 policy
->cps_uri
= arg
;
217 error
= "--user-notice must follow a --cert-policy";
220 policy
->unotice_text
= arg
;
224 char *pos
= strchr(arg
, ':');
225 x509_policy_mapping_t
*mapping
;
226 chunk_t subject_oid
, issuer_oid
;
231 issuer_oid
= asn1_oid_from_string(arg
);
232 subject_oid
= asn1_oid_from_string(pos
);
234 if (!pos
|| !issuer_oid
.len
|| !subject_oid
.len
)
236 error
= "--policy-map OIDs invalid";
240 .issuer
= issuer_oid
,
241 .subject
= subject_oid
,
243 mappings
->insert_last(mappings
, mapping
);
247 require_explicit
= atoi(arg
);
250 inhibit_mapping
= atoi(arg
);
253 inhibit_any
= atoi(arg
);
256 if (streq(arg
, "serverAuth"))
258 flags
|= X509_SERVER_AUTH
;
260 else if (streq(arg
, "clientAuth"))
262 flags
|= X509_CLIENT_AUTH
;
264 else if (streq(arg
, "ikeIntermediate"))
266 flags
|= X509_IKE_INTERMEDIATE
;
268 else if (streq(arg
, "crlSign"))
270 flags
|= X509_CRL_SIGN
;
272 else if (streq(arg
, "ocspSigning"))
274 flags
|= X509_OCSP_SIGNER
;
276 else if (streq(arg
, "msSmartcardLogon"))
278 flags
|= X509_MS_SMARTCARD_LOGON
;
282 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
284 error
= "invalid output format";
289 ocsp
->insert_last(ocsp
, arg
);
294 error
= "invalid --self option";
302 error
= "--dn is required";
305 if (!calculate_lifetime(dateform
, datenb
, datena
, lifetime
,
306 ¬_before
, ¬_after
))
308 error
= "invalid --not-before/after datetime";
311 id
= identification_create_from_string(dn
);
312 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
314 error
= "supplied --dn is not a distinguished name";
319 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
320 BUILD_FROM_FILE
, file
, BUILD_END
);
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
);
335 set_file_mode(stdin
, CERT_ASN1_DER
);
336 if (!chunk_from_fd(0, &chunk
))
338 fprintf(stderr
, "%s: ", strerror(errno
));
339 error
= "reading private key failed";
342 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
343 BUILD_BLOB
, chunk
, BUILD_END
);
348 error
= "loading private key failed";
351 public = private->get_public_key(private);
354 error
= "extracting public key failed";
359 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
363 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
367 error
= "no random number generator found";
370 if (!rng_allocate_bytes_not_zero(rng
, 8, &serial
, FALSE
))
372 error
= "failed to generate serial number";
376 serial
.ptr
[0] &= 0x7F;
379 scheme
= get_signature_scheme(private, digest
, pss
);
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
,
399 error
= "generating certificate failed";
402 if (!cert
->get_encoding(cert
, form
, &encoding
))
404 error
= "encoding certificate failed";
407 set_file_mode(stdout
, form
);
408 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
410 error
= "writing certificate key failed";
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
);
426 signature_params_destroy(scheme
);
432 fprintf(stderr
, "%s\n", error
);
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
);
445 return command_usage(error
);
449 * Register the command.
451 static void __attribute__ ((constructor
))reg()
453 command_register((command_t
) {
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]"},
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"},