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
;
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();
50 switch (getopt_long(argc
, argv
, "", command_opts
, NULL
))
55 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
57 error
= "invalid options file";
62 if (streq(optarg
, "rsa"))
66 else if (streq(optarg
, "ecdsa"))
72 error
= "invalid input type";
77 digest
= get_digest(optarg
);
78 if (digest
== HASH_UNKNOWN
)
80 error
= "invalid --digest type";
91 san
->insert_last(san
, identification_create_from_string(optarg
));
94 lifetime
= atoi(optarg
);
97 error
= "invalid --lifetime value";
110 error
= "invalid --self option";
118 error
= "--dn is required";
121 id
= identification_create_from_string(dn
);
122 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
124 error
= "supplied --dn is not a distinguished name";
129 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
130 BUILD_FROM_FILE
, file
, BUILD_END
);
134 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
135 BUILD_FROM_FD
, 0, BUILD_END
);
139 error
= "parsing private key failed";
142 public = private->get_public_key(private);
145 error
= "extracting public key failed";
150 serial
= chunk_from_hex(chunk_create(hex
, strlen(hex
)), NULL
);
154 rng_t
*rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
158 error
= "no random number generator found";
161 rng
->allocate_bytes(rng
, 8, &serial
);
164 not_before
= time(NULL
);
165 not_after
= not_before
+ lifetime
* 24 * 60 * 60;
166 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
167 BUILD_SIGNING_KEY
, private, BUILD_PUBLIC_KEY
, public,
168 BUILD_SUBJECT
, id
, BUILD_NOT_BEFORE_TIME
, not_before
,
169 BUILD_NOT_AFTER_TIME
, not_after
, BUILD_SERIAL
, serial
,
170 BUILD_DIGEST_ALG
, digest
, BUILD_X509_FLAG
, flags
,
171 BUILD_SUBJECT_ALTNAMES
, san
, BUILD_END
);
174 error
= "generating certificate failed";
177 encoding
= cert
->get_encoding(cert
);
180 error
= "encoding certificate failed";
183 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
185 error
= "writing certificate key failed";
194 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
195 options
->destroy(options
);
201 fprintf(stderr
, "%s\n", error
);
207 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
208 options
->destroy(options
);
209 return command_usage(CMD_SELF
, error
);
213 * Register the command.
215 static void __attribute__ ((constructor
))reg()
217 command_register(CMD_SELF
, (command_t
) {
219 "create a self signed certificate",
220 {"[--in file] [--type rsa|ecdsa]",
221 " --dn distinguished-name [--san subjectAltName]+",
222 "[--lifetime days] [--serial hex] [--ca]",
223 "[--digest md5|sha1|sha224|sha256|sha384|sha512]",
226 {"help", 'h', 0, "show usage information"},
227 {"in", 'i', 1, "private key input file, default: stdin"},
228 {"type", 't', 1, "type of input key, default: rsa"},
229 {"dn", 'd', 1, "subject and issuer distinguished name"},
230 {"san", 'a', 1, "subjectAltName to include in certificate"},
231 {"lifetime",'l', 1, "days the certificate is valid, default: 1080"},
232 {"serial", 's', 1, "serial number in hex, default: random"},
233 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
234 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
235 {"options", '+', 1, "read command line options from file"},