2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2015-2016 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 <utils/debug.h>
23 #include <asn1/asn1.h>
24 #include <collections/linked_list.h>
25 #include <credentials/certificates/certificate.h>
26 #include <credentials/certificates/x509.h>
27 #include <credentials/certificates/pkcs10.h>
30 * Free cert policy with OID
32 static void destroy_cert_policy(x509_cert_policy_t
*policy
)
34 free(policy
->oid
.ptr
);
41 static void destroy_policy_mapping(x509_policy_mapping_t
*mapping
)
43 free(mapping
->issuer
.ptr
);
44 free(mapping
->subject
.ptr
);
49 * Free a CRL DistributionPoint
51 static void destroy_cdp(x509_cdp_t
*this)
53 DESTROY_IF(this->issuer
);
58 * Issue a certificate using a CA certificate and key
62 cred_encoding_type_t form
= CERT_ASN1_DER
;
63 hash_algorithm_t digest
= HASH_UNKNOWN
;
64 certificate_t
*cert_req
= NULL
, *cert
= NULL
, *ca
=NULL
;
65 private_key_t
*private = NULL
;
66 public_key_t
*public = NULL
;
67 credential_type_t type
= CRED_PUBLIC_KEY
;
68 key_type_t subtype
= KEY_ANY
;
70 char *file
= NULL
, *dn
= NULL
, *hex
= NULL
, *cacert
= NULL
, *cakey
= NULL
;
71 char *error
= NULL
, *keyid
= NULL
;
72 identification_t
*id
= NULL
;
73 linked_list_t
*san
, *cdps
, *ocsp
, *permitted
, *excluded
, *policies
, *mappings
;
74 linked_list_t
*addrblocks
;
75 int pathlen
= X509_NO_CONSTRAINT
, inhibit_any
= X509_NO_CONSTRAINT
;
76 int inhibit_mapping
= X509_NO_CONSTRAINT
, require_explicit
= X509_NO_CONSTRAINT
;
77 chunk_t serial
= chunk_empty
;
78 chunk_t encoding
= chunk_empty
;
79 time_t not_before
, not_after
, lifetime
= 1095 * 24 * 60 * 60;
80 char *datenb
= NULL
, *datena
= NULL
, *dateform
= NULL
;
81 x509_flag_t flags
= 0;
83 x509_cdp_t
*cdp
= NULL
;
84 x509_cert_policy_t
*policy
= NULL
;
85 traffic_selector_t
*ts
;
88 san
= linked_list_create();
89 cdps
= linked_list_create();
90 ocsp
= linked_list_create();
91 permitted
= linked_list_create();
92 excluded
= linked_list_create();
93 policies
= linked_list_create();
94 mappings
= linked_list_create();
95 addrblocks
= linked_list_create();
99 switch (command_getopt(&arg
))
104 if (streq(arg
, "pkcs10"))
108 else if (streq(arg
, "rsa"))
110 type
= CRED_PRIVATE_KEY
;
113 else if (streq(arg
, "ecdsa"))
115 type
= CRED_PRIVATE_KEY
;
118 else if (streq(arg
, "ed25519"))
120 type
= CRED_PRIVATE_KEY
;
121 subtype
= KEY_ED25519
;
123 else if (streq(arg
, "bliss"))
125 type
= CRED_PRIVATE_KEY
;
128 else if (streq(arg
, "priv"))
130 type
= CRED_PRIVATE_KEY
;
133 else if (!streq(arg
, "pub"))
135 error
= "invalid input type";
140 if (!enum_from_name(hash_algorithm_short_names
, arg
, &digest
))
142 error
= "invalid --digest type";
162 san
->insert_last(san
, identification_create_from_string(arg
));
165 lifetime
= atoi(arg
) * 24 * 60 * 60;
168 error
= "invalid --lifetime value";
194 error
= "invalid addressBlock";
197 addrblocks
->insert_last(addrblocks
, ts
);
200 permitted
->insert_last(permitted
,
201 identification_create_from_string(arg
));
204 excluded
->insert_last(excluded
,
205 identification_create_from_string(arg
));
211 oid
= asn1_oid_from_string(arg
);
214 error
= "--cert-policy OID invalid";
220 policies
->insert_last(policies
, policy
);
226 error
= "--cps-uri must follow a --cert-policy";
229 policy
->cps_uri
= arg
;
234 error
= "--user-notice must follow a --cert-policy";
237 policy
->unotice_text
= arg
;
241 char *pos
= strchr(arg
, ':');
242 x509_policy_mapping_t
*mapping
;
243 chunk_t subject_oid
, issuer_oid
;
248 issuer_oid
= asn1_oid_from_string(arg
);
249 subject_oid
= asn1_oid_from_string(pos
);
251 if (!pos
|| !issuer_oid
.len
|| !subject_oid
.len
)
253 error
= "--policy-map OIDs invalid";
257 .issuer
= issuer_oid
,
258 .subject
= subject_oid
,
260 mappings
->insert_last(mappings
, mapping
);
264 require_explicit
= atoi(arg
);
267 inhibit_mapping
= atoi(arg
);
270 inhibit_any
= atoi(arg
);
273 if (streq(arg
, "serverAuth"))
275 flags
|= X509_SERVER_AUTH
;
277 else if (streq(arg
, "clientAuth"))
279 flags
|= X509_CLIENT_AUTH
;
281 else if (streq(arg
, "ikeIntermediate"))
283 flags
|= X509_IKE_INTERMEDIATE
;
285 else if (streq(arg
, "crlSign"))
287 flags
|= X509_CRL_SIGN
;
289 else if (streq(arg
, "ocspSigning"))
291 flags
|= X509_OCSP_SIGNER
;
293 else if (streq(arg
, "msSmartcardLogon"))
295 flags
|= X509_MS_SMARTCARD_LOGON
;
299 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
301 error
= "invalid output format";
309 cdps
->insert_last(cdps
, cdp
);
312 if (!cdp
|| cdp
->issuer
)
314 error
= "--crlissuer must follow a --crl";
317 cdp
->issuer
= identification_create_from_string(arg
);
320 ocsp
->insert_last(ocsp
, arg
);
325 error
= "invalid --issue option";
333 error
= "--cacert is required";
336 if (!cakey
&& !keyid
)
338 error
= "--cakey or --keyid is required";
341 if (!calculate_lifetime(dateform
, datenb
, datena
, lifetime
,
342 ¬_before
, ¬_after
))
344 error
= "invalid --not-before/after datetime";
349 id
= identification_create_from_string(dn
);
350 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
352 error
= "supplied --dn is not a distinguished name";
357 DBG2(DBG_LIB
, "Reading ca certificate:");
358 ca
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
359 BUILD_FROM_FILE
, cacert
, BUILD_END
);
362 error
= "parsing CA certificate failed";
366 if (!(x509
->get_flags(x509
) & X509_CA
))
368 error
= "CA certificate misses CA basicConstraint";
371 public = ca
->get_public_key(ca
);
374 error
= "extracting CA certificate public key failed";
378 DBG2(DBG_LIB
, "Reading ca private key:");
381 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
,
382 public->get_type(public),
383 BUILD_FROM_FILE
, cakey
, BUILD_END
);
389 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
390 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
391 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
396 error
= "loading CA private key failed";
399 if (digest
== HASH_UNKNOWN
)
401 digest
= get_default_digest(private);
403 if (!private->belongs_to(private, public))
405 error
= "CA private key does not match CA certificate";
408 public->destroy(public);
412 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
416 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
420 error
= "no random number generator found";
423 if (!rng_allocate_bytes_not_zero(rng
, 8, &serial
, FALSE
))
425 error
= "failed to generate serial number";
429 serial
.ptr
[0] &= 0x7F;
435 enumerator_t
*enumerator
;
436 identification_t
*subjectAltName
;
439 DBG2(DBG_LIB
, "Reading certificate request");
442 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
444 BUILD_FROM_FILE
, file
, BUILD_END
);
450 set_file_mode(stdin
, CERT_ASN1_DER
);
451 if (!chunk_from_fd(0, &chunk
))
453 fprintf(stderr
, "%s: ", strerror(errno
));
454 error
= "reading certificate request failed";
457 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
459 BUILD_BLOB
, chunk
, BUILD_END
);
464 error
= "parsing certificate request failed";
468 /* If not set yet use subject from PKCS#10 certificate request as DN */
471 id
= cert_req
->get_subject(cert_req
);
475 /* Add subjectAltNames from PKCS#10 certificate request */
476 req
= (pkcs10_t
*)cert_req
;
477 enumerator
= req
->create_subjectAltName_enumerator(req
);
478 while (enumerator
->enumerate(enumerator
, &subjectAltName
))
480 san
->insert_last(san
, subjectAltName
->clone(subjectAltName
));
482 enumerator
->destroy(enumerator
);
484 /* Use public key from PKCS#10 certificate request */
485 public = cert_req
->get_public_key(cert_req
);
489 DBG2(DBG_LIB
, "Reading key:");
492 public = lib
->creds
->create(lib
->creds
, type
, subtype
,
493 BUILD_FROM_FILE
, file
, BUILD_END
);
499 if (!chunk_from_fd(0, &chunk
))
501 fprintf(stderr
, "%s: ", strerror(errno
));
502 error
= "reading key failed";
505 public = lib
->creds
->create(lib
->creds
, type
, subtype
,
506 BUILD_BLOB
, chunk
, BUILD_END
);
509 if (public && type
== CRED_PRIVATE_KEY
)
511 private_key_t
*priv
= (private_key_t
*)public;
512 public = priv
->get_public_key(priv
);
518 error
= "parsing public key failed";
524 id
= identification_create_from_encoding(ID_DER_ASN1_DN
,
525 chunk_from_chars(ASN1_SEQUENCE
, 0));
528 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
529 BUILD_SIGNING_KEY
, private, BUILD_SIGNING_CERT
, ca
,
530 BUILD_PUBLIC_KEY
, public, BUILD_SUBJECT
, id
,
531 BUILD_NOT_BEFORE_TIME
, not_before
, BUILD_DIGEST_ALG
, digest
,
532 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
533 BUILD_SUBJECT_ALTNAMES
, san
, BUILD_X509_FLAG
, flags
,
534 BUILD_PATHLEN
, pathlen
, BUILD_ADDRBLOCKS
, addrblocks
,
535 BUILD_CRL_DISTRIBUTION_POINTS
, cdps
,
536 BUILD_OCSP_ACCESS_LOCATIONS
, ocsp
,
537 BUILD_PERMITTED_NAME_CONSTRAINTS
, permitted
,
538 BUILD_EXCLUDED_NAME_CONSTRAINTS
, excluded
,
539 BUILD_CERTIFICATE_POLICIES
, policies
,
540 BUILD_POLICY_MAPPINGS
, mappings
,
541 BUILD_POLICY_REQUIRE_EXPLICIT
, require_explicit
,
542 BUILD_POLICY_INHIBIT_MAPPING
, inhibit_mapping
,
543 BUILD_POLICY_INHIBIT_ANY
, inhibit_any
,
547 error
= "generating certificate failed";
550 if (!cert
->get_encoding(cert
, form
, &encoding
))
552 error
= "encoding certificate failed";
555 set_file_mode(stdout
, form
);
556 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
558 error
= "writing certificate key failed";
564 DESTROY_IF(cert_req
);
569 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
570 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
571 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
572 addrblocks
->destroy_offset(addrblocks
, offsetof(traffic_selector_t
, destroy
));
573 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
574 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
575 cdps
->destroy_function(cdps
, (void*)destroy_cdp
);
582 fprintf(stderr
, "%s\n", error
);
588 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
589 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
590 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
591 addrblocks
->destroy_offset(addrblocks
, offsetof(traffic_selector_t
, destroy
));
592 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
593 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
594 cdps
->destroy_function(cdps
, (void*)destroy_cdp
);
596 return command_usage(error
);
600 * Register the command.
602 static void __attribute__ ((constructor
))reg()
604 command_register((command_t
) {
606 "issue a certificate using a CA certificate and key",
607 {"[--in file] [--type pub|pkcs10|priv|rsa|ecdsa|ed25519|bliss] --cakey file|--cakeyid hex",
608 " --cacert file [--dn subject-dn] [--san subjectAltName]+",
609 "[--lifetime days] [--serial hex] [--ca] [--pathlen len]",
610 "[--flag serverAuth|clientAuth|crlSign|ocspSigning|msSmartcardLogon]+",
611 "[--crl uri [--crlissuer i]]+ [--ocsp uri]+ [--nc-permitted name]",
612 "[--nc-excluded name] [--policy-mapping issuer-oid:subject-oid]",
613 "[--policy-explicit len] [--policy-inhibit len] [--policy-any len]",
614 "[--cert-policy oid [--cps-uri uri] [--user-notice text]]+",
615 "[--digest md5|sha1|sha224|sha256|sha384|sha512|sha3_224|sha3_256|sha3_384|sha3_512]",
616 "[--outform der|pem]"},
618 {"help", 'h', 0, "show usage information"},
619 {"in", 'i', 1, "key/request file to issue, default: stdin"},
620 {"type", 't', 1, "type of input, default: pub"},
621 {"cacert", 'c', 1, "CA certificate file"},
622 {"cakey", 'k', 1, "CA private key file"},
623 {"cakeyid", 'x', 1, "keyid on smartcard of CA private key"},
624 {"dn", 'd', 1, "distinguished name to include as subject"},
625 {"san", 'a', 1, "subjectAltName to include in certificate"},
626 {"lifetime", 'l', 1, "days the certificate is valid, default: 1095"},
627 {"not-before", 'F', 1, "date/time the validity of the cert starts"},
628 {"not-after", 'T', 1, "date/time the validity of the cert ends"},
629 {"dateform", 'D', 1, "strptime(3) input format, default: %d.%m.%y %T"},
630 {"serial", 's', 1, "serial number in hex, default: random"},
631 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
632 {"pathlen", 'p', 1, "set path length constraint"},
633 {"addrblock", 'B', 1, "RFC 3779 addrBlock to include"},
634 {"nc-permitted", 'n', 1, "add permitted NameConstraint"},
635 {"nc-excluded", 'N', 1, "add excluded NameConstraint"},
636 {"cert-policy", 'P', 1, "certificatePolicy OID to include"},
637 {"cps-uri", 'C', 1, "Certification Practice statement URI for certificatePolicy"},
638 {"user-notice", 'U', 1, "user notice for certificatePolicy"},
639 {"policy-mapping", 'M', 1, "policyMapping from issuer to subject OID"},
640 {"policy-explicit", 'E', 1, "requireExplicitPolicy constraint"},
641 {"policy-inhibit", 'H', 1, "inhibitPolicyMapping constraint"},
642 {"policy-any", 'A', 1, "inhibitAnyPolicy constraint"},
643 {"flag", 'e', 1, "include extendedKeyUsage flag"},
644 {"crl", 'u', 1, "CRL distribution point URI to include"},
645 {"crlissuer", 'I', 1, "CRL Issuer for CRL at distribution point"},
646 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
647 {"digest", 'g', 1, "digest for signature creation, default: key-specific"},
648 {"outform", 'f', 1, "encoding of generated cert, default: der"},