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 <utils/optionsfrom.h>
22 #include <credentials/certificates/certificate.h>
23 #include <credentials/certificates/x509.h>
26 * Create a self signed certificate.
28 static int self(int argc
, char *argv
[])
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
;
36 identification_t
*id
= NULL
;
37 linked_list_t
*san
, *ocsp
;
39 chunk_t serial
= chunk_empty
;
40 chunk_t encoding
= chunk_empty
;
41 time_t not_before
, not_after
;
42 x509_flag_t flags
= 0;
45 options
= options_create();
46 san
= linked_list_create();
47 ocsp
= linked_list_create();
51 switch (getopt_long(argc
, argv
, "", command_opts
, NULL
))
56 dbg_level
= atoi(optarg
);
59 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
61 error
= "invalid options file";
66 if (streq(optarg
, "rsa"))
70 else if (streq(optarg
, "ecdsa"))
76 error
= "invalid input type";
81 digest
= get_digest(optarg
);
82 if (digest
== HASH_UNKNOWN
)
84 error
= "invalid --digest type";
95 san
->insert_last(san
, identification_create_from_string(optarg
));
98 lifetime
= atoi(optarg
);
101 error
= "invalid --lifetime value";
112 ocsp
->insert_last(ocsp
, optarg
);
117 error
= "invalid --self option";
125 error
= "--dn is required";
128 id
= identification_create_from_string(dn
);
129 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
131 error
= "supplied --dn is not a distinguished name";
136 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
137 BUILD_FROM_FILE
, file
, BUILD_END
);
141 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
142 BUILD_FROM_FD
, 0, BUILD_END
);
146 error
= "parsing private key failed";
149 public = private->get_public_key(private);
152 error
= "extracting public key failed";
157 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
161 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
165 error
= "no random number generator found";
168 rng
->allocate_bytes(rng
, 8, &serial
);
171 not_before
= time(NULL
);
172 not_after
= not_before
+ lifetime
* 24 * 60 * 60;
173 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
174 BUILD_SIGNING_KEY
, private, BUILD_PUBLIC_KEY
, public,
175 BUILD_SUBJECT
, id
, BUILD_NOT_BEFORE_TIME
, not_before
,
176 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
177 BUILD_DIGEST_ALG
, digest
, BUILD_X509_FLAG
, flags
,
178 BUILD_SUBJECT_ALTNAMES
, san
,
179 BUILD_OCSP_ACCESS_LOCATIONS
, ocsp
, BUILD_END
);
182 error
= "generating certificate failed";
185 encoding
= cert
->get_encoding(cert
);
188 error
= "encoding certificate failed";
191 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
193 error
= "writing certificate key failed";
202 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
204 options
->destroy(options
);
210 fprintf(stderr
, "%s\n", error
);
216 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
218 options
->destroy(options
);
219 return command_usage(error
);
223 * Register the command.
225 static void __attribute__ ((constructor
))reg()
227 command_register((command_t
) {
229 "create a self signed certificate",
230 {"[--in file] [--type rsa|ecdsa]",
231 " --dn distinguished-name [--san subjectAltName]+",
232 "[--lifetime days] [--serial hex] [--ca] [--ocsp uri]+",
233 "[--digest md5|sha1|sha224|sha256|sha384|sha512]",
236 {"help", 'h', 0, "show usage information"},
237 {"in", 'i', 1, "private key input file, default: stdin"},
238 {"type", 't', 1, "type of input key, default: rsa"},
239 {"dn", 'd', 1, "subject and issuer distinguished name"},
240 {"san", 'a', 1, "subjectAltName to include in certificate"},
241 {"lifetime",'l', 1, "days the certificate is valid, default: 1080"},
242 {"serial", 's', 1, "serial number in hex, default: random"},
243 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
244 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
245 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
246 {"debug", 'v', 1, "set debug level, default: 1"},
247 {"options", '+', 1, "read command line options from file"},