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/linked_list.h>
22 #include <credentials/certificates/certificate.h>
23 #include <credentials/certificates/x509.h>
24 #include <credentials/certificates/pkcs10.h>
27 * Issue a certificate using a CA certificate and key
31 hash_algorithm_t digest
= HASH_SHA1
;
32 certificate_t
*cert_req
= NULL
, *cert
= NULL
, *ca
=NULL
;
33 private_key_t
*private = NULL
;
34 public_key_t
*public = NULL
;
36 char *file
= NULL
, *dn
= NULL
, *hex
= NULL
, *cacert
= NULL
, *cakey
= NULL
;
38 identification_t
*id
= NULL
;
39 linked_list_t
*san
, *cdps
, *ocsp
;
41 int pathlen
= X509_NO_PATH_LEN_CONSTRAINT
;
42 chunk_t serial
= chunk_empty
;
43 chunk_t encoding
= chunk_empty
;
44 time_t not_before
, not_after
;
45 x509_flag_t flags
= 0;
49 san
= linked_list_create();
50 cdps
= linked_list_create();
51 ocsp
= linked_list_create();
55 switch (command_getopt(&arg
))
60 if (streq(arg
, "pkcs10"))
64 else if (!streq(arg
, "pub"))
66 error
= "invalid input type";
71 digest
= get_digest(arg
);
72 if (digest
== HASH_UNKNOWN
)
74 error
= "invalid --digest type";
91 san
->insert_last(san
, identification_create_from_string(arg
));
97 error
= "invalid --lifetime value";
111 if (streq(arg
, "serverAuth"))
113 flags
|= X509_SERVER_AUTH
;
115 else if (streq(arg
, "ocspSigning"))
117 flags
|= X509_OCSP_SIGNER
;
121 cdps
->insert_last(cdps
, arg
);
124 ocsp
->insert_last(ocsp
, arg
);
129 error
= "invalid --issue option";
137 error
= "--dn is required";
142 error
= "--cacert is required";
147 error
= "--cakey is required";
152 id
= identification_create_from_string(dn
);
153 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
155 error
= "supplied --dn is not a distinguished name";
160 DBG2("Reading ca certificate:");
161 ca
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
162 BUILD_FROM_FILE
, cacert
, BUILD_END
);
165 error
= "parsing CA certificate failed";
169 if (!(x509
->get_flags(x509
) & X509_CA
))
171 error
= "CA certificate misses CA basicConstraint";
174 public = ca
->get_public_key(ca
);
177 error
= "extracting CA certificate public key failed";
181 DBG2("Reading ca private key:");
182 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
,
183 public->get_type(public),
184 BUILD_FROM_FILE
, cakey
, BUILD_END
);
187 error
= "parsing CA private key failed";
190 if (!private->belongs_to(private, public))
192 error
= "CA private key does not match CA certificate";
195 public->destroy(public);
199 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
203 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
207 error
= "no random number generator found";
210 rng
->allocate_bytes(rng
, 8, &serial
);
216 enumerator_t
*enumerator
;
217 identification_t
*subjectAltName
;
220 DBG2("Reading certificate request");
223 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
225 BUILD_FROM_FILE
, file
, BUILD_END
);
229 cert_req
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
231 BUILD_FROM_FD
, 0, BUILD_END
);
235 error
= "parsing certificate request failed";
239 /* If not set yet use subject from PKCS#10 certificate request as DN */
242 id
= cert_req
->get_subject(cert_req
);
246 /* Add subjectAltNames from PKCS#10 certificate request */
247 req
= (pkcs10_t
*)cert_req
;
248 enumerator
= req
->create_subjectAltName_enumerator(req
);
249 while (enumerator
->enumerate(enumerator
, &subjectAltName
))
251 san
->insert_last(san
, subjectAltName
->clone(subjectAltName
));
253 enumerator
->destroy(enumerator
);
255 /* Use public key from PKCS#10 certificate request */
256 public = cert_req
->get_public_key(cert_req
);
260 DBG2("Reading public key:");
263 public = lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
264 BUILD_FROM_FILE
, file
, BUILD_END
);
268 public = lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
269 BUILD_FROM_FD
, 0, BUILD_END
);
274 error
= "parsing public key failed";
278 not_before
= time(NULL
);
279 not_after
= not_before
+ lifetime
* 24 * 60 * 60;
281 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
282 BUILD_SIGNING_KEY
, private, BUILD_SIGNING_CERT
, ca
,
283 BUILD_PUBLIC_KEY
, public, BUILD_SUBJECT
, id
,
284 BUILD_NOT_BEFORE_TIME
, not_before
, BUILD_DIGEST_ALG
, digest
,
285 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
286 BUILD_SUBJECT_ALTNAMES
, san
, BUILD_X509_FLAG
, flags
,
287 BUILD_PATHLEN
, pathlen
,
288 BUILD_CRL_DISTRIBUTION_POINTS
, cdps
,
289 BUILD_OCSP_ACCESS_LOCATIONS
, ocsp
, BUILD_END
);
292 error
= "generating certificate failed";
295 encoding
= cert
->get_encoding(cert
);
298 error
= "encoding certificate failed";
301 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
303 error
= "writing certificate key failed";
309 DESTROY_IF(cert_req
);
314 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
322 fprintf(stderr
, "%s\n", error
);
328 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
331 return command_usage(error
);
335 * Register the command.
337 static void __attribute__ ((constructor
))reg()
339 command_register((command_t
) {
341 "issue a certificate using a CA certificate and key",
342 {"[--in file] [--type pub|pkcs10]",
343 " --cacert file --cakey file --dn subject-dn [--san subjectAltName]+",
344 "[--lifetime days] [--serial hex] [--crl uri]+ [--ocsp uri]+",
345 "[--ca] [--pathlen len] [--flag serverAuth|ocspSigning]+",
346 "[--digest md5|sha1|sha224|sha256|sha384|sha512]"},
348 {"help", 'h', 0, "show usage information"},
349 {"in", 'i', 1, "public key/request file to issue, default: stdin"},
350 {"type", 't', 1, "type of input, default: pub"},
351 {"cacert", 'c', 1, "CA certificate file"},
352 {"cakey", 'k', 1, "CA private key file"},
353 {"dn", 'd', 1, "distinguished name to include as subject"},
354 {"san", 'a', 1, "subjectAltName to include in certificate"},
355 {"lifetime",'l', 1, "days the certificate is valid, default: 1080"},
356 {"serial", 's', 1, "serial number in hex, default: random"},
357 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
358 {"pathlen", 'p', 1, "set path length constraint"},
359 {"flag", 'f', 1, "include extendedKeyUsage flag"},
360 {"crl", 'u', 1, "CRL distribution point URI to include"},
361 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
362 {"digest", 'g', 1, "digest for signature creation, default: sha1"},