2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
21 #include <utils/debug.h>
22 #include <asn1/asn1.h>
23 #include <collections/linked_list.h>
24 #include <credentials/certificates/certificate.h>
25 #include <credentials/certificates/x509.h>
26 #include <credentials/certificates/pkcs10.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 * Free a CRL DistributionPoint
50 static void destroy_cdp(x509_cdp_t
*this)
52 DESTROY_IF(this->issuer
);
57 * Issue a certificate using a CA certificate and key
61 cred_encoding_type_t form
= CERT_ASN1_DER
;
62 hash_algorithm_t digest
= HASH_SHA1
;
63 certificate_t
*cert_req
= NULL
, *cert
= NULL
, *ca
=NULL
;
64 private_key_t
*private = NULL
;
65 public_key_t
*public = NULL
;
67 char *file
= NULL
, *dn
= NULL
, *hex
= NULL
, *cacert
= NULL
, *cakey
= NULL
;
68 char *error
= NULL
, *keyid
= NULL
;
69 identification_t
*id
= NULL
;
70 linked_list_t
*san
, *cdps
, *ocsp
, *permitted
, *excluded
, *policies
, *mappings
;
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
, lifetime
= 1095 * 24 * 60 * 60;
76 char *datenb
= NULL
, *datena
= NULL
, *dateform
= NULL
;
77 x509_flag_t flags
= 0;
79 x509_cdp_t
*cdp
= NULL
;
80 x509_cert_policy_t
*policy
= NULL
;
83 san
= linked_list_create();
84 cdps
= linked_list_create();
85 ocsp
= linked_list_create();
86 permitted
= linked_list_create();
87 excluded
= linked_list_create();
88 policies
= linked_list_create();
89 mappings
= linked_list_create();
93 switch (command_getopt(&arg
))
98 if (streq(arg
, "pkcs10"))
102 else if (!streq(arg
, "pub"))
104 error
= "invalid input type";
109 digest
= enum_from_name(hash_algorithm_short_names
, arg
);
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";
293 error
= "--cacert is required";
296 if (!cakey
&& !keyid
)
298 error
= "--cakey or --keyid is required";
301 if (!calculate_lifetime(dateform
, datenb
, datena
, lifetime
,
302 ¬_before
, ¬_after
))
304 error
= "invalid --not-before/after datetime";
309 id
= identification_create_from_string(dn
);
310 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
312 error
= "supplied --dn is not a distinguished name";
317 DBG2(DBG_LIB
, "Reading ca certificate:");
318 ca
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
319 BUILD_FROM_FILE
, cacert
, BUILD_END
);
322 error
= "parsing CA certificate failed";
326 if (!(x509
->get_flags(x509
) & X509_CA
))
328 error
= "CA certificate misses CA basicConstraint";
331 public = ca
->get_public_key(ca
);
334 error
= "extracting CA certificate public key failed";
338 DBG2(DBG_LIB
, "Reading ca private key:");
341 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
,
342 public->get_type(public),
343 BUILD_FROM_FILE
, cakey
, BUILD_END
);
349 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
350 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
351 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
356 error
= "loading CA private key failed";
359 if (!private->belongs_to(private, public))
361 error
= "CA private key does not match CA certificate";
364 public->destroy(public);
368 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
372 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
376 error
= "no random number generator found";
379 if (!rng_allocate_bytes_not_zero(rng
, 8, &serial
, FALSE
))
381 error
= "failed to generate serial number";
385 serial
.ptr
[0] &= 0x7F;
391 enumerator_t
*enumerator
;
392 identification_t
*subjectAltName
;
395 DBG2(DBG_LIB
, "Reading certificate request");
398 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
400 BUILD_FROM_FILE
, file
, BUILD_END
);
406 if (!chunk_from_fd(0, &chunk
))
408 fprintf(stderr
, "%s: ", strerror(errno
));
409 error
= "reading certificate request failed";
412 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
414 BUILD_BLOB
, chunk
, BUILD_END
);
419 error
= "parsing certificate request failed";
423 /* If not set yet use subject from PKCS#10 certificate request as DN */
426 id
= cert_req
->get_subject(cert_req
);
430 /* Add subjectAltNames from PKCS#10 certificate request */
431 req
= (pkcs10_t
*)cert_req
;
432 enumerator
= req
->create_subjectAltName_enumerator(req
);
433 while (enumerator
->enumerate(enumerator
, &subjectAltName
))
435 san
->insert_last(san
, subjectAltName
->clone(subjectAltName
));
437 enumerator
->destroy(enumerator
);
439 /* Use public key from PKCS#10 certificate request */
440 public = cert_req
->get_public_key(cert_req
);
444 DBG2(DBG_LIB
, "Reading public key:");
447 public = lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
448 BUILD_FROM_FILE
, file
, BUILD_END
);
454 if (!chunk_from_fd(0, &chunk
))
456 fprintf(stderr
, "%s: ", strerror(errno
));
457 error
= "reading public key failed";
460 public = lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
461 BUILD_BLOB
, chunk
, BUILD_END
);
467 error
= "parsing public key failed";
473 id
= identification_create_from_encoding(ID_DER_ASN1_DN
,
474 chunk_from_chars(ASN1_SEQUENCE
, 0));
477 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
478 BUILD_SIGNING_KEY
, private, BUILD_SIGNING_CERT
, ca
,
479 BUILD_PUBLIC_KEY
, public, BUILD_SUBJECT
, id
,
480 BUILD_NOT_BEFORE_TIME
, not_before
, BUILD_DIGEST_ALG
, digest
,
481 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
482 BUILD_SUBJECT_ALTNAMES
, san
, BUILD_X509_FLAG
, flags
,
483 BUILD_PATHLEN
, pathlen
,
484 BUILD_CRL_DISTRIBUTION_POINTS
, cdps
,
485 BUILD_OCSP_ACCESS_LOCATIONS
, ocsp
,
486 BUILD_PERMITTED_NAME_CONSTRAINTS
, permitted
,
487 BUILD_EXCLUDED_NAME_CONSTRAINTS
, excluded
,
488 BUILD_CERTIFICATE_POLICIES
, policies
,
489 BUILD_POLICY_MAPPINGS
, mappings
,
490 BUILD_POLICY_REQUIRE_EXPLICIT
, require_explicit
,
491 BUILD_POLICY_INHIBIT_MAPPING
, inhibit_mapping
,
492 BUILD_POLICY_INHIBIT_ANY
, inhibit_any
,
496 error
= "generating certificate failed";
499 if (!cert
->get_encoding(cert
, form
, &encoding
))
501 error
= "encoding certificate failed";
504 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
506 error
= "writing certificate key failed";
512 DESTROY_IF(cert_req
);
517 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
518 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
519 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
520 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
521 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
522 cdps
->destroy_function(cdps
, (void*)destroy_cdp
);
529 fprintf(stderr
, "%s\n", error
);
535 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
536 permitted
->destroy_offset(permitted
, offsetof(identification_t
, destroy
));
537 excluded
->destroy_offset(excluded
, offsetof(identification_t
, destroy
));
538 policies
->destroy_function(policies
, (void*)destroy_cert_policy
);
539 mappings
->destroy_function(mappings
, (void*)destroy_policy_mapping
);
540 cdps
->destroy_function(cdps
, (void*)destroy_cdp
);
542 return command_usage(error
);
546 * Register the command.
548 static void __attribute__ ((constructor
))reg()
550 command_register((command_t
) {
552 "issue a certificate using a CA certificate and key",
553 {"[--in file] [--type pub|pkcs10] --cakey file|--cakeyid hex",
554 " --cacert file [--dn subject-dn] [--san subjectAltName]+",
555 "[--lifetime days] [--serial hex] [--ca] [--pathlen len]",
556 "[--flag serverAuth|clientAuth|crlSign|ocspSigning|msSmartcardLogon]+",
557 "[--crl uri [--crlissuer i]]+ [--ocsp uri]+ [--nc-permitted name]",
558 "[--nc-excluded name] [--policy-mapping issuer-oid:subject-oid]",
559 "[--policy-explicit len] [--policy-inhibit len] [--policy-any len]",
560 "[--cert-policy oid [--cps-uri uri] [--user-notice text]]+",
561 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
563 {"help", 'h', 0, "show usage information"},
564 {"in", 'i', 1, "public key/request file to issue, default: stdin"},
565 {"type", 't', 1, "type of input, default: pub"},
566 {"cacert", 'c', 1, "CA certificate file"},
567 {"cakey", 'k', 1, "CA private key file"},
568 {"cakeyid", 'x', 1, "keyid on smartcard of CA private key"},
569 {"dn", 'd', 1, "distinguished name to include as subject"},
570 {"san", 'a', 1, "subjectAltName to include in certificate"},
571 {"lifetime", 'l', 1, "days the certificate is valid, default: 1095"},
572 {"not-before", 'F', 1, "date/time the validity of the cert starts"},
573 {"not-after", 'T', 1, "date/time the validity of the cert ends"},
574 {"dateform", 'D', 1, "strptime(3) input format, default: %d.%m.%y %T"},
575 {"serial", 's', 1, "serial number in hex, default: random"},
576 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
577 {"pathlen", 'p', 1, "set path length constraint"},
578 {"nc-permitted", 'n', 1, "add permitted NameConstraint"},
579 {"nc-excluded", 'N', 1, "add excluded NameConstraint"},
580 {"cert-policy", 'P', 1, "certificatePolicy OID to include"},
581 {"cps-uri", 'C', 1, "Certification Practice statement URI for certificatePolicy"},
582 {"user-notice", 'U', 1, "user notice for certificatePolicy"},
583 {"policy-mapping", 'M', 1, "policyMapping from issuer to subject OID"},
584 {"policy-explicit", 'E', 1, "requireExplicitPolicy constraint"},
585 {"policy-inhibit", 'H', 1, "inhibitPolicyMapping constraint"},
586 {"policy-any", 'A', 1, "inhibitAnyPolicy constraint"},
587 {"flag", 'e', 1, "include extendedKeyUsage flag"},
588 {"crl", 'u', 1, "CRL distribution point URI to include"},
589 {"crlissuer", 'I', 1, "CRL Issuer for CRL at distribution point"},
590 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
591 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
592 {"outform", 'f', 1, "encoding of generated cert, default: der"},