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
20 #include <utils/linked_list.h>
21 #include <credentials/certificates/certificate.h>
22 #include <credentials/certificates/x509.h>
25 * Create a self signed certificate.
29 cred_encoding_type_t form
= CERT_ASN1_DER
;
30 key_type_t type
= KEY_RSA
;
31 hash_algorithm_t digest
= HASH_SHA1
;
32 certificate_t
*cert
= NULL
;
33 private_key_t
*private = NULL
;
34 public_key_t
*public = NULL
;
35 char *file
= NULL
, *dn
= NULL
, *hex
= NULL
, *error
= NULL
, *keyid
= NULL
;
36 identification_t
*id
= NULL
;
37 linked_list_t
*san
, *ocsp
;
39 int pathlen
= X509_NO_PATH_LEN_CONSTRAINT
;
40 chunk_t serial
= chunk_empty
;
41 chunk_t encoding
= chunk_empty
;
42 time_t not_before
, not_after
;
43 x509_flag_t flags
= 0;
46 san
= linked_list_create();
47 ocsp
= linked_list_create();
51 switch (command_getopt(&arg
))
56 if (streq(arg
, "rsa"))
60 else if (streq(arg
, "ecdsa"))
66 error
= "invalid input type";
71 digest
= get_digest(arg
);
72 if (digest
== HASH_UNKNOWN
)
74 error
= "invalid --digest type";
88 san
->insert_last(san
, identification_create_from_string(arg
));
94 error
= "invalid --lifetime value";
108 if (streq(arg
, "serverAuth"))
110 flags
|= X509_SERVER_AUTH
;
112 else if (streq(arg
, "clientAuth"))
114 flags
|= X509_CLIENT_AUTH
;
116 else if (streq(arg
, "crlSign"))
118 flags
|= X509_CRL_SIGN
;
120 else if (streq(arg
, "ocspSigning"))
122 flags
|= X509_OCSP_SIGNER
;
126 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
128 return command_usage("invalid output format");
132 ocsp
->insert_last(ocsp
, arg
);
137 error
= "invalid --self option";
145 error
= "--dn is required";
148 id
= identification_create_from_string(dn
);
149 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
151 error
= "supplied --dn is not a distinguished name";
156 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
157 BUILD_FROM_FILE
, file
, BUILD_END
);
163 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
164 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
165 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
170 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
171 BUILD_FROM_FD
, 0, BUILD_END
);
175 error
= "loading private key failed";
178 public = private->get_public_key(private);
181 error
= "extracting public key failed";
186 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
190 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
194 error
= "no random number generator found";
197 rng
->allocate_bytes(rng
, 8, &serial
);
198 while (*serial
.ptr
== 0x00)
200 /* we don't accept a serial number with leading zeroes */
201 rng
->get_bytes(rng
, 1, serial
.ptr
);
205 not_before
= time(NULL
);
206 not_after
= not_before
+ lifetime
* 24 * 60 * 60;
207 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
208 BUILD_SIGNING_KEY
, private, BUILD_PUBLIC_KEY
, public,
209 BUILD_SUBJECT
, id
, BUILD_NOT_BEFORE_TIME
, not_before
,
210 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
211 BUILD_DIGEST_ALG
, digest
, BUILD_X509_FLAG
, flags
,
212 BUILD_PATHLEN
, pathlen
, BUILD_SUBJECT_ALTNAMES
, san
,
213 BUILD_OCSP_ACCESS_LOCATIONS
, ocsp
, BUILD_END
);
216 error
= "generating certificate failed";
219 if (!cert
->get_encoding(cert
, form
, &encoding
))
221 error
= "encoding certificate failed";
224 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
226 error
= "writing certificate key failed";
235 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
242 fprintf(stderr
, "%s\n", error
);
248 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
250 return command_usage(error
);
254 * Register the command.
256 static void __attribute__ ((constructor
))reg()
258 command_register((command_t
) {
260 "create a self signed certificate",
261 {"[--in file | --keyid hex] [--type rsa|ecdsa]",
262 " --dn distinguished-name [--san subjectAltName]+",
263 "[--lifetime days] [--serial hex] [--ca] [--ocsp uri]+",
264 "[--flag serverAuth|clientAuth|crlSign|ocspSigning]+",
265 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
267 {"help", 'h', 0, "show usage information"},
268 {"in", 'i', 1, "private key input file, default: stdin"},
269 {"keyid", 'x', 1, "keyid on smartcard of private key"},
270 {"type", 't', 1, "type of input key, default: rsa"},
271 {"dn", 'd', 1, "subject and issuer distinguished name"},
272 {"san", 'a', 1, "subjectAltName to include in certificate"},
273 {"lifetime",'l', 1, "days the certificate is valid, default: 1095"},
274 {"serial", 's', 1, "serial number in hex, default: random"},
275 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
276 {"pathlen", 'p', 1, "set path length constraint"},
277 {"flag", 'e', 1, "include extendedKeyUsage flag"},
278 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
279 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
280 {"outform", 'f', 1, "encoding of generated cert, default: der"},