fixed another typo
[strongswan.git] / src / pki / commands / self.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include <time.h>
17
18 #include "pki.h"
19
20 #include <utils/linked_list.h>
21 #include <utils/optionsfrom.h>
22 #include <credentials/certificates/certificate.h>
23 #include <credentials/certificates/x509.h>
24
25 /**
26 * Create a self signed certificate.
27 */
28 static int self(int argc, char *argv[])
29 {
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;
38 int lifetime = 1080;
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;
43 options_t *options;
44
45 options = options_create();
46 san = linked_list_create();
47 ocsp = linked_list_create();
48
49 while (TRUE)
50 {
51 switch (getopt_long(argc, argv, "", command_opts, NULL))
52 {
53 case 'h':
54 goto usage;
55 case '+':
56 if (!options->from(options, optarg, &argc, &argv, optind))
57 {
58 error = "invalid options file";
59 goto usage;
60 }
61 continue;
62 case 't':
63 if (streq(optarg, "rsa"))
64 {
65 type = KEY_RSA;
66 }
67 else if (streq(optarg, "ecdsa"))
68 {
69 type = KEY_ECDSA;
70 }
71 else
72 {
73 error = "invalid input type";
74 goto usage;
75 }
76 continue;
77 case 'g':
78 digest = get_digest(optarg);
79 if (digest == HASH_UNKNOWN)
80 {
81 error = "invalid --digest type";
82 goto usage;
83 }
84 continue;
85 case 'i':
86 file = optarg;
87 continue;
88 case 'd':
89 dn = optarg;
90 continue;
91 case 'a':
92 san->insert_last(san, identification_create_from_string(optarg));
93 continue;
94 case 'l':
95 lifetime = atoi(optarg);
96 if (!lifetime)
97 {
98 error = "invalid --lifetime value";
99 goto usage;
100 }
101 continue;
102 case 's':
103 hex = optarg;
104 continue;
105 case 'b':
106 flags |= X509_CA;
107 continue;
108 case 'o':
109 ocsp->insert_last(ocsp, optarg);
110 continue;
111 case EOF:
112 break;
113 default:
114 error = "invalid --self option";
115 goto usage;
116 }
117 break;
118 }
119
120 if (!dn)
121 {
122 error = "--dn is required";
123 goto usage;
124 }
125 id = identification_create_from_string(dn);
126 if (id->get_type(id) != ID_DER_ASN1_DN)
127 {
128 error = "supplied --dn is not a distinguished name";
129 goto end;
130 }
131 if (file)
132 {
133 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
134 BUILD_FROM_FILE, file, BUILD_END);
135 }
136 else
137 {
138 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
139 BUILD_FROM_FD, 0, BUILD_END);
140 }
141 if (!private)
142 {
143 error = "parsing private key failed";
144 goto end;
145 }
146 public = private->get_public_key(private);
147 if (!public)
148 {
149 error = "extracting public key failed";
150 goto end;
151 }
152 if (hex)
153 {
154 serial = chunk_from_hex(chunk_create(hex, strlen(hex)), NULL);
155 }
156 else
157 {
158 rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
159
160 if (!rng)
161 {
162 error = "no random number generator found";
163 goto end;
164 }
165 rng->allocate_bytes(rng, 8, &serial);
166 rng->destroy(rng);
167 }
168 not_before = time(NULL);
169 not_after = not_before + lifetime * 24 * 60 * 60;
170 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
171 BUILD_SIGNING_KEY, private, BUILD_PUBLIC_KEY, public,
172 BUILD_SUBJECT, id, BUILD_NOT_BEFORE_TIME, not_before,
173 BUILD_NOT_AFTER_TIME, not_after, BUILD_SERIAL, serial,
174 BUILD_DIGEST_ALG, digest, BUILD_X509_FLAG, flags,
175 BUILD_SUBJECT_ALTNAMES, san,
176 BUILD_OCSP_ACCESS_LOCATIONS, ocsp, BUILD_END);
177 if (!cert)
178 {
179 error = "generating certificate failed";
180 goto end;
181 }
182 encoding = cert->get_encoding(cert);
183 if (!encoding.ptr)
184 {
185 error = "encoding certificate failed";
186 goto end;
187 }
188 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
189 {
190 error = "writing certificate key failed";
191 goto end;
192 }
193
194 end:
195 DESTROY_IF(id);
196 DESTROY_IF(cert);
197 DESTROY_IF(public);
198 DESTROY_IF(private);
199 san->destroy_offset(san, offsetof(identification_t, destroy));
200 ocsp->destroy(ocsp);
201 options->destroy(options);
202 free(encoding.ptr);
203 free(serial.ptr);
204
205 if (error)
206 {
207 fprintf(stderr, "%s\n", error);
208 return 1;
209 }
210 return 0;
211
212 usage:
213 san->destroy_offset(san, offsetof(identification_t, destroy));
214 ocsp->destroy(ocsp);
215 options->destroy(options);
216 return command_usage(error);
217 }
218
219 /**
220 * Register the command.
221 */
222 static void __attribute__ ((constructor))reg()
223 {
224 command_register((command_t) {
225 self, 's', "self",
226 "create a self signed certificate",
227 {"[--in file] [--type rsa|ecdsa]",
228 " --dn distinguished-name [--san subjectAltName]+",
229 "[--lifetime days] [--serial hex] [--ca] [--ocsp uri]+",
230 "[--digest md5|sha1|sha224|sha256|sha384|sha512]",
231 "[--options file]"},
232 {
233 {"help", 'h', 0, "show usage information"},
234 {"in", 'i', 1, "private key input file, default: stdin"},
235 {"type", 't', 1, "type of input key, default: rsa"},
236 {"dn", 'd', 1, "subject and issuer distinguished name"},
237 {"san", 'a', 1, "subjectAltName to include in certificate"},
238 {"lifetime",'l', 1, "days the certificate is valid, default: 1080"},
239 {"serial", 's', 1, "serial number in hex, default: random"},
240 {"ca", 'b', 0, "include CA basicConstraint, default: no"},
241 {"ocsp", 'o', 1, "OCSP AuthorityInfoAccess URI to include"},
242 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
243 {"options", '+', 1, "read command line options from file"},
244 }
245 });
246 }