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