Use a dedicated build part for challenge passwords, BUILD_PASSPHRASE gets obsolete
[strongswan.git] / src / pki / commands / req.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2009 Andreas Steffen
4 *
5 * HSR Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <time.h>
19
20 #include "pki.h"
21
22 #include <utils/linked_list.h>
23 #include <credentials/certificates/certificate.h>
24
25 /**
26 * Create a self-signed PKCS#10 certificate requesst.
27 */
28 static int req()
29 {
30 cred_encoding_type_t form = CERT_ASN1_DER;
31 key_type_t type = KEY_RSA;
32 hash_algorithm_t digest = HASH_SHA1;
33 certificate_t *cert = NULL;
34 private_key_t *private = NULL;
35 char *file = NULL, *dn = NULL, *error = NULL;
36 identification_t *id = NULL;
37 linked_list_t *san;
38 chunk_t encoding = chunk_empty;
39 chunk_t challenge_password = chunk_empty;
40 char *arg;
41
42 san = linked_list_create();
43
44 while (TRUE)
45 {
46 switch (command_getopt(&arg))
47 {
48 case 'h':
49 goto usage;
50 case 't':
51 if (streq(arg, "rsa"))
52 {
53 type = KEY_RSA;
54 }
55 else if (streq(arg, "ecdsa"))
56 {
57 type = KEY_ECDSA;
58 }
59 else
60 {
61 error = "invalid input type";
62 goto usage;
63 }
64 continue;
65 case 'g':
66 digest = get_digest(arg);
67 if (digest == HASH_UNKNOWN)
68 {
69 error = "invalid --digest type";
70 goto usage;
71 }
72 continue;
73 case 'i':
74 file = arg;
75 continue;
76 case 'd':
77 dn = arg;
78 continue;
79 case 'a':
80 san->insert_last(san, identification_create_from_string(arg));
81 continue;
82 case 'p':
83 challenge_password = chunk_create(arg, strlen(arg));
84 continue;
85 case 'f':
86 if (!get_form(arg, &form, CRED_CERTIFICATE))
87 {
88 return command_usage("invalid output format");
89 }
90 continue;
91 case EOF:
92 break;
93 default:
94 error = "invalid --req option";
95 goto usage;
96 }
97 break;
98 }
99
100 if (!dn)
101 {
102 error = "--dn is required";
103 goto usage;
104 }
105 id = identification_create_from_string(dn);
106 if (id->get_type(id) != ID_DER_ASN1_DN)
107 {
108 error = "supplied --dn is not a distinguished name";
109 goto end;
110 }
111 if (file)
112 {
113 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
114 BUILD_FROM_FILE, file, BUILD_END);
115 }
116 else
117 {
118 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
119 BUILD_FROM_FD, 0, BUILD_END);
120 }
121 if (!private)
122 {
123 error = "parsing private key failed";
124 goto end;
125 }
126 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_PKCS10_REQUEST,
127 BUILD_SIGNING_KEY, private,
128 BUILD_SUBJECT, id,
129 BUILD_SUBJECT_ALTNAMES, san,
130 BUILD_CHALLENGE_PWD, challenge_password,
131 BUILD_DIGEST_ALG, digest,
132 BUILD_END);
133 if (!cert)
134 {
135 error = "generating certificate request failed";
136 goto end;
137 }
138 if (!cert->get_encoding(cert, form, &encoding))
139 {
140 error = "encoding certificate request failed";
141 goto end;
142 }
143 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
144 {
145 error = "writing certificate request failed";
146 goto end;
147 }
148
149 end:
150 DESTROY_IF(id);
151 DESTROY_IF(cert);
152 DESTROY_IF(private);
153 san->destroy_offset(san, offsetof(identification_t, destroy));
154 free(encoding.ptr);
155
156 if (error)
157 {
158 fprintf(stderr, "%s\n", error);
159 return 1;
160 }
161 return 0;
162
163 usage:
164 san->destroy_offset(san, offsetof(identification_t, destroy));
165 return command_usage(error);
166 }
167
168 /**
169 * Register the command.
170 */
171 static void __attribute__ ((constructor))reg()
172 {
173 command_register((command_t) {
174 req, 'r', "req",
175 "create a PKCS#10 certificate request",
176 {"[--in file] [--type rsa|ecdsa]",
177 " --dn distinguished-name [--san subjectAltName]+",
178 "[--password challengePassword]",
179 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
180 {
181 {"help", 'h', 0, "show usage information"},
182 {"in", 'i', 1, "private key input file, default: stdin"},
183 {"type", 't', 1, "type of input key, default: rsa"},
184 {"dn", 'd', 1, "subject distinguished name"},
185 {"san", 'a', 1, "subjectAltName to include in cert request"},
186 {"password",'p', 1, "challengePassword to include in cert request"},
187 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
188 {"outform", 'f', 1, "encoding of generated request, default: der"},
189 }
190 });
191 }