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 <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
;
68 char *file
= NULL
, *dn
= NULL
, *hex
= NULL
, *cacert
= NULL
, *cakey
= NULL
;
69 char *error
= NULL
, *keyid
= NULL
;
70 identification_t
*id
= NULL
;
71 linked_list_t
*san
, *cdps
, *ocsp
, *permitted
, *excluded
, *policies
, *mappings
;
72 int pathlen
= X509_NO_CONSTRAINT
, inhibit_any
= X509_NO_CONSTRAINT
;
73 int inhibit_mapping
= X509_NO_CONSTRAINT
, require_explicit
= X509_NO_CONSTRAINT
;
74 chunk_t serial
= chunk_empty
;
75 chunk_t encoding
= chunk_empty
;
76 time_t not_before
, not_after
, lifetime
= 1095 * 24 * 60 * 60;
77 char *datenb
= NULL
, *datena
= NULL
, *dateform
= NULL
;
78 x509_flag_t flags
= 0;
80 x509_cdp_t
*cdp
= NULL
;
81 x509_cert_policy_t
*policy
= NULL
;
84 san
= linked_list_create();
85 cdps
= linked_list_create();
86 ocsp
= linked_list_create();
87 permitted
= linked_list_create();
88 excluded
= linked_list_create();
89 policies
= linked_list_create();
90 mappings
= linked_list_create();
94 switch (command_getopt(&arg
))
99 if (streq(arg
, "pkcs10"))
103 else if (!streq(arg
, "pub"))
105 error
= "invalid input type";
110 if (!enum_from_name(hash_algorithm_short_names
, arg
, &digest
))
112 error
= "invalid --digest type";
132 san
->insert_last(san
, identification_create_from_string(arg
));
135 lifetime
= atoi(arg
) * 24 * 60 * 60;
138 error
= "invalid --lifetime value";
161 permitted
->insert_last(permitted
,
162 identification_create_from_string(arg
));
165 excluded
->insert_last(excluded
,
166 identification_create_from_string(arg
));
172 oid
= asn1_oid_from_string(arg
);
175 error
= "--cert-policy OID invalid";
181 policies
->insert_last(policies
, policy
);
187 error
= "--cps-uri must follow a --cert-policy";
190 policy
->cps_uri
= arg
;
195 error
= "--user-notice must follow a --cert-policy";
198 policy
->unotice_text
= arg
;
202 char *pos
= strchr(arg
, ':');
203 x509_policy_mapping_t
*mapping
;
204 chunk_t subject_oid
, issuer_oid
;
209 issuer_oid
= asn1_oid_from_string(arg
);
210 subject_oid
= asn1_oid_from_string(pos
);
212 if (!pos
|| !issuer_oid
.len
|| !subject_oid
.len
)
214 error
= "--policy-map OIDs invalid";
218 .issuer
= issuer_oid
,
219 .subject
= subject_oid
,
221 mappings
->insert_last(mappings
, mapping
);
225 require_explicit
= atoi(arg
);
228 inhibit_mapping
= atoi(arg
);
231 inhibit_any
= atoi(arg
);
234 if (streq(arg
, "serverAuth"))
236 flags
|= X509_SERVER_AUTH
;
238 else if (streq(arg
, "clientAuth"))
240 flags
|= X509_CLIENT_AUTH
;
242 else if (streq(arg
, "ikeIntermediate"))
244 flags
|= X509_IKE_INTERMEDIATE
;
246 else if (streq(arg
, "crlSign"))
248 flags
|= X509_CRL_SIGN
;
250 else if (streq(arg
, "ocspSigning"))
252 flags
|= X509_OCSP_SIGNER
;
254 else if (streq(arg
, "msSmartcardLogon"))
256 flags
|= X509_MS_SMARTCARD_LOGON
;
260 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
262 error
= "invalid output format";
270 cdps
->insert_last(cdps
, cdp
);
273 if (!cdp
|| cdp
->issuer
)
275 error
= "--crlissuer must follow a --crl";
278 cdp
->issuer
= identification_create_from_string(arg
);
281 ocsp
->insert_last(ocsp
, arg
);
286 error
= "invalid --issue option";
294 error
= "--cacert is required";
297 if (!cakey
&& !keyid
)
299 error
= "--cakey or --keyid is required";
302 if (!calculate_lifetime(dateform
, datenb
, datena
, lifetime
,
303 ¬_before
, ¬_after
))
305 error
= "invalid --not-before/after datetime";
310 id
= identification_create_from_string(dn
);
311 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
313 error
= "supplied --dn is not a distinguished name";
318 DBG2(DBG_LIB
, "Reading ca certificate:");
319 ca
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
320 BUILD_FROM_FILE
, cacert
, BUILD_END
);
323 error
= "parsing CA certificate failed";
327 if (!(x509
->get_flags(x509
) & X509_CA
))
329 error
= "CA certificate misses CA basicConstraint";
332 public = ca
->get_public_key(ca
);
335 error
= "extracting CA certificate public key failed";
339 DBG2(DBG_LIB
, "Reading ca private key:");
342 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
,
343 public->get_type(public),
344 BUILD_FROM_FILE
, cakey
, BUILD_END
);
350 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
351 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
352 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
357 error
= "loading CA private key failed";
360 if (digest
== HASH_UNKNOWN
)
362 digest
= get_default_digest(private);
364 if (!private->belongs_to(private, public))
366 error
= "CA private key does not match CA certificate";
369 public->destroy(public);
373 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
377 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
381 error
= "no random number generator found";
384 if (!rng_allocate_bytes_not_zero(rng
, 8, &serial
, FALSE
))
386 error
= "failed to generate serial number";
390 serial
.ptr
[0] &= 0x7F;
396 enumerator_t
*enumerator
;
397 identification_t
*subjectAltName
;
400 DBG2(DBG_LIB
, "Reading certificate request");
403 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
405 BUILD_FROM_FILE
, file
, BUILD_END
);
411 set_file_mode(stdin
, CERT_ASN1_DER
);
412 if (!chunk_from_fd(0, &chunk
))
414 fprintf(stderr
, "%s: ", strerror(errno
));
415 error
= "reading certificate request failed";
418 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
420 BUILD_BLOB
, chunk
, BUILD_END
);
425 error
= "parsing certificate request failed";
429 /* If not set yet use subject from PKCS#10 certificate request as DN */
432 id
= cert_req
->get_subject(cert_req
);
436 /* Add subjectAltNames from PKCS#10 certificate request */
437 req
= (pkcs10_t
*)cert_req
;
438 enumerator
= req
->create_subjectAltName_enumerator(req
);
439 while (enumerator
->enumerate(enumerator
, &subjectAltName
))
441 san
->insert_last(san
, subjectAltName
->clone(subjectAltName
));
443 enumerator
->destroy(enumerator
);
445 /* Use public key from PKCS#10 certificate request */
446 public = cert_req
->get_public_key(cert_req
);
450 DBG2(DBG_LIB
, "Reading public key:");
453 public = lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
454 BUILD_FROM_FILE
, file
, BUILD_END
);
460 if (!chunk_from_fd(0, &chunk
))
462 fprintf(stderr
, "%s: ", strerror(errno
));
463 error
= "reading public key failed";
466 public = lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
467 BUILD_BLOB
, chunk
, BUILD_END
);
473 error
= "parsing public key failed";
479 id
= identification_create_from_encoding(ID_DER_ASN1_DN
,
480 chunk_from_chars(ASN1_SEQUENCE
, 0));
483 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
484 BUILD_SIGNING_KEY
, private, BUILD_SIGNING_CERT
, ca
,
485 BUILD_PUBLIC_KEY
, public, BUILD_SUBJECT
, id
,
486 BUILD_NOT_BEFORE_TIME
, not_before
, BUILD_DIGEST_ALG
, digest
,
487 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
488 BUILD_SUBJECT_ALTNAMES
, san
, BUILD_X509_FLAG
, flags
,
489 BUILD_PATHLEN
, pathlen
,
490 BUILD_CRL_DISTRIBUTION_POINTS
, cdps
,
491 BUILD_OCSP_ACCESS_LOCATIONS
, ocsp
,
492 BUILD_PERMITTED_NAME_CONSTRAINTS
, permitted
,
493 BUILD_EXCLUDED_NAME_CONSTRAINTS
, excluded
,
494 BUILD_CERTIFICATE_POLICIES
, policies
,
495 BUILD_POLICY_MAPPINGS
, mappings
,
496 BUILD_POLICY_REQUIRE_EXPLICIT
, require_explicit
,
497 BUILD_POLICY_INHIBIT_MAPPING
, inhibit_mapping
,
498 BUILD_POLICY_INHIBIT_ANY
, inhibit_any
,
502 error
= "generating certificate failed";
505 if (!cert
->get_encoding(cert
, form
, &encoding
))
507 error
= "encoding certificate failed";
510 set_file_mode(stdout
, form
);
511 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
513 error
= "writing certificate key failed";
519 DESTROY_IF(cert_req
);
524 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
525 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
526 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
527 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
528 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
529 cdps
->destroy_function(cdps
, (void*)destroy_cdp
);
536 fprintf(stderr
, "%s\n", error
);
542 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
543 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
544 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
545 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
546 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
547 cdps
->destroy_function(cdps
, (void*)destroy_cdp
);
549 return command_usage(error
);
553 * Register the command.
555 static void __attribute__ ((constructor
))reg()
557 command_register((command_t
) {
559 "issue a certificate using a CA certificate and key",
560 {"[--in file] [--type pub|pkcs10] --cakey file|--cakeyid hex",
561 " --cacert file [--dn subject-dn] [--san subjectAltName]+",
562 "[--lifetime days] [--serial hex] [--ca] [--pathlen len]",
563 "[--flag serverAuth|clientAuth|crlSign|ocspSigning|msSmartcardLogon]+",
564 "[--crl uri [--crlissuer i]]+ [--ocsp uri]+ [--nc-permitted name]",
565 "[--nc-excluded name] [--policy-mapping issuer-oid:subject-oid]",
566 "[--policy-explicit len] [--policy-inhibit len] [--policy-any len]",
567 "[--cert-policy oid [--cps-uri uri] [--user-notice text]]+",
568 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
570 {"help", 'h', 0, "show usage information"},
571 {"in", 'i', 1, "public key/request file to issue, default: stdin"},
572 {"type", 't', 1, "type of input, default: pub"},
573 {"cacert", 'c', 1, "CA certificate file"},
574 {"cakey", 'k', 1, "CA private key file"},
575 {"cakeyid", 'x', 1, "keyid on smartcard of CA private key"},
576 {"dn", 'd', 1, "distinguished name to include as subject"},
577 {"san", 'a', 1, "subjectAltName to include in certificate"},
578 {"lifetime", 'l', 1, "days the certificate is valid, default: 1095"},
579 {"not-before", 'F', 1, "date/time the validity of the cert starts"},
580 {"not-after", 'T', 1, "date/time the validity of the cert ends"},
581 {"dateform", 'D', 1, "strptime(3) input format, default: %d.%m.%y %T"},
582 {"serial", 's', 1, "serial number in hex, default: random"},
583 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
584 {"pathlen", 'p', 1, "set path length constraint"},
585 {"nc-permitted", 'n', 1, "add permitted NameConstraint"},
586 {"nc-excluded", 'N', 1, "add excluded NameConstraint"},
587 {"cert-policy", 'P', 1, "certificatePolicy OID to include"},
588 {"cps-uri", 'C', 1, "Certification Practice statement URI for certificatePolicy"},
589 {"user-notice", 'U', 1, "user notice for certificatePolicy"},
590 {"policy-mapping", 'M', 1, "policyMapping from issuer to subject OID"},
591 {"policy-explicit", 'E', 1, "requireExplicitPolicy constraint"},
592 {"policy-inhibit", 'H', 1, "inhibitPolicyMapping constraint"},
593 {"policy-any", 'A', 1, "inhibitAnyPolicy constraint"},
594 {"flag", 'e', 1, "include extendedKeyUsage flag"},
595 {"crl", 'u', 1, "CRL distribution point URI to include"},
596 {"crlissuer", 'I', 1, "CRL Issuer for CRL at distribution point"},
597 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
598 {"digest", 'g', 1, "digest for signature creation, default: key-specific"},
599 {"outform", 'f', 1, "encoding of generated cert, default: der"},