8335f2595ba5eff7828b3bb6faf5eb3930b593c7
[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 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 char *file = NULL, *dn = NULL, *error = NULL;
35 identification_t *id = NULL;
36 linked_list_t *san;
37 chunk_t encoding = chunk_empty;
38 chunk_t challenge_password = chunk_empty;
39 char *arg;
40
41 san = linked_list_create();
42
43 while (TRUE)
44 {
45 switch (command_getopt(&arg))
46 {
47 case 'h':
48 goto usage;
49 case 't':
50 if (streq(arg, "rsa"))
51 {
52 type = KEY_RSA;
53 }
54 else if (streq(arg, "ecdsa"))
55 {
56 type = KEY_ECDSA;
57 }
58 else
59 {
60 error = "invalid input type";
61 goto usage;
62 }
63 continue;
64 case 'g':
65 digest = get_digest(arg);
66 if (digest == HASH_UNKNOWN)
67 {
68 error = "invalid --digest type";
69 goto usage;
70 }
71 continue;
72 case 'i':
73 file = arg;
74 continue;
75 case 'd':
76 dn = arg;
77 continue;
78 case 'a':
79 san->insert_last(san, identification_create_from_string(arg));
80 continue;
81 case 'p':
82 challenge_password = chunk_create(arg, strlen(arg));
83 continue;
84 case EOF:
85 break;
86 default:
87 error = "invalid --req option";
88 goto usage;
89 }
90 break;
91 }
92
93 if (!dn)
94 {
95 error = "--dn is required";
96 goto usage;
97 }
98 id = identification_create_from_string(dn);
99 if (id->get_type(id) != ID_DER_ASN1_DN)
100 {
101 error = "supplied --dn is not a distinguished name";
102 goto end;
103 }
104 if (file)
105 {
106 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
107 BUILD_FROM_FILE, file, BUILD_END);
108 }
109 else
110 {
111 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
112 BUILD_FROM_FD, 0, BUILD_END);
113 }
114 if (!private)
115 {
116 error = "parsing private key failed";
117 goto end;
118 }
119 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_PKCS10_REQUEST,
120 BUILD_SIGNING_KEY, private,
121 BUILD_SUBJECT, id,
122 BUILD_SUBJECT_ALTNAMES, san,
123 BUILD_PASSPHRASE, challenge_password,
124 BUILD_DIGEST_ALG, digest,
125 BUILD_END);
126 if (!cert)
127 {
128 error = "generating certificate request failed";
129 goto end;
130 }
131 encoding = cert->get_encoding(cert);
132 if (!encoding.ptr)
133 {
134 error = "encoding certificate request failed";
135 goto end;
136 }
137 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
138 {
139 error = "writing certificate request failed";
140 goto end;
141 }
142
143 end:
144 DESTROY_IF(id);
145 DESTROY_IF(cert);
146 DESTROY_IF(private);
147 san->destroy_offset(san, offsetof(identification_t, destroy));
148 free(encoding.ptr);
149
150 if (error)
151 {
152 fprintf(stderr, "%s\n", error);
153 return 1;
154 }
155 return 0;
156
157 usage:
158 san->destroy_offset(san, offsetof(identification_t, destroy));
159 return command_usage(error);
160 }
161
162 /**
163 * Register the command.
164 */
165 static void __attribute__ ((constructor))reg()
166 {
167 command_register((command_t) {
168 req, 'r', "req",
169 "create a PKCS#10 certificate request",
170 {"[--in file] [--type rsa|ecdsa]",
171 " --dn distinguished-name [--san subjectAltName]+",
172 "[--password challengePassword]",
173 "[--digest md5|sha1|sha224|sha256|sha384|sha512]"},
174 {
175 {"help", 'h', 0, "show usage information"},
176 {"in", 'i', 1, "private key input file, default: stdin"},
177 {"type", 't', 1, "type of input key, default: rsa"},
178 {"dn", 'd', 1, "subject distinguished name"},
179 {"san", 'a', 1, "subjectAltName to include in cert request"},
180 {"password",'p', 1, "challengePassword to include in cert request"},
181 {"digest", 'g', 1, "digest for signature creation, default: sha1"},
182 }
183 });
184 }