]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/pki/commands/req.c
pki: Switch to binary mode on Windows when reading/writing DER to FDs
[people/ms/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 #include <errno.h>
20
21 #include "pki.h"
22
23 #include <collections/linked_list.h>
24 #include <credentials/certificates/certificate.h>
25
26 /**
27 * Create a self-signed PKCS#10 certificate requesst.
28 */
29 static int req()
30 {
31 cred_encoding_type_t form = CERT_ASN1_DER;
32 key_type_t type = KEY_RSA;
33 hash_algorithm_t digest = HASH_SHA1;
34 certificate_t *cert = NULL;
35 private_key_t *private = NULL;
36 char *file = NULL, *dn = NULL, *error = NULL;
37 identification_t *id = NULL;
38 linked_list_t *san;
39 chunk_t encoding = chunk_empty;
40 chunk_t challenge_password = chunk_empty;
41 char *arg;
42
43 san = linked_list_create();
44
45 while (TRUE)
46 {
47 switch (command_getopt(&arg))
48 {
49 case 'h':
50 goto usage;
51 case 't':
52 if (streq(arg, "rsa"))
53 {
54 type = KEY_RSA;
55 }
56 else if (streq(arg, "ecdsa"))
57 {
58 type = KEY_ECDSA;
59 }
60 else
61 {
62 error = "invalid input type";
63 goto usage;
64 }
65 continue;
66 case 'g':
67 if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
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 error = "invalid output format";
89 goto usage;
90 }
91 continue;
92 case EOF:
93 break;
94 default:
95 error = "invalid --req option";
96 goto usage;
97 }
98 break;
99 }
100
101 if (!dn)
102 {
103 error = "--dn is required";
104 goto usage;
105 }
106 id = identification_create_from_string(dn);
107 if (id->get_type(id) != ID_DER_ASN1_DN)
108 {
109 error = "supplied --dn is not a distinguished name";
110 goto end;
111 }
112 if (file)
113 {
114 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
115 BUILD_FROM_FILE, file, BUILD_END);
116 }
117 else
118 {
119 chunk_t chunk;
120
121 set_file_mode(stdin, CERT_ASN1_DER);
122 if (!chunk_from_fd(0, &chunk))
123 {
124 fprintf(stderr, "reading private key failed: %s\n", strerror(errno));
125 error = "";
126 goto end;
127 }
128 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
129 BUILD_BLOB, chunk, BUILD_END);
130 free(chunk.ptr);
131 }
132 if (!private)
133 {
134 error = "parsing private key failed";
135 goto end;
136 }
137 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_PKCS10_REQUEST,
138 BUILD_SIGNING_KEY, private,
139 BUILD_SUBJECT, id,
140 BUILD_SUBJECT_ALTNAMES, san,
141 BUILD_CHALLENGE_PWD, challenge_password,
142 BUILD_DIGEST_ALG, digest,
143 BUILD_END);
144 if (!cert)
145 {
146 error = "generating certificate request failed";
147 goto end;
148 }
149 if (!cert->get_encoding(cert, form, &encoding))
150 {
151 error = "encoding certificate request failed";
152 goto end;
153 }
154 set_file_mode(stdout, form);
155 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
156 {
157 error = "writing certificate request failed";
158 goto end;
159 }
160
161 end:
162 DESTROY_IF(id);
163 DESTROY_IF(cert);
164 DESTROY_IF(private);
165 san->destroy_offset(san, offsetof(identification_t, destroy));
166 free(encoding.ptr);
167
168 if (error)
169 {
170 fprintf(stderr, "%s\n", error);
171 return 1;
172 }
173 return 0;
174
175 usage:
176 san->destroy_offset(san, offsetof(identification_t, destroy));
177 return command_usage(error);
178 }
179
180 /**
181 * Register the command.
182 */
183 static void __attribute__ ((constructor))reg()
184 {
185 command_register((command_t) {
186 req, 'r', "req",
187 "create a PKCS#10 certificate request",
188 {" [--in file] [--type rsa|ecdsa] --dn distinguished-name",
189 "[--san subjectAltName]+ [--password challengePassword]",
190 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
191 {
192 {"help", 'h', 0, "show usage information"},
193 {"in", 'i', 1, "private key input file, default: stdin"},
194 {"type", 't', 1, "type of input key, default: rsa"},
195 {"dn", 'd', 1, "subject distinguished name"},
196 {"san", 'a', 1, "subjectAltName to include in cert request"},
197 {"password",'p', 1, "challengePassword to include in cert request"},
198 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
199 {"outform", 'f', 1, "encoding of generated request, default: der"},
200 }
201 });
202 }