2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2009-2015 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
6 * HSR Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include <collections/linked_list.h>
25 #include <credentials/certificates/certificate.h>
28 * Create a self-signed PKCS#10 certificate requesst.
32 cred_encoding_type_t form
= CERT_ASN1_DER
;
33 key_type_t type
= KEY_RSA
;
34 hash_algorithm_t digest
= HASH_SHA256
;
35 certificate_t
*cert
= NULL
;
36 private_key_t
*private = NULL
;
37 char *file
= NULL
, *dn
= NULL
, *error
= NULL
;
38 identification_t
*id
= NULL
;
40 chunk_t encoding
= chunk_empty
;
41 chunk_t challenge_password
= chunk_empty
;
44 san
= linked_list_create();
48 switch (command_getopt(&arg
))
53 if (streq(arg
, "rsa"))
57 else if (streq(arg
, "ecdsa"))
61 else if (streq(arg
, "bliss"))
67 error
= "invalid input type";
72 if (!enum_from_name(hash_algorithm_short_names
, arg
, &digest
))
74 error
= "invalid --digest type";
85 san
->insert_last(san
, identification_create_from_string(arg
));
88 challenge_password
= chunk_create(arg
, strlen(arg
));
91 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
93 error
= "invalid output format";
100 error
= "invalid --req option";
108 error
= "--dn is required";
111 id
= identification_create_from_string(dn
);
112 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
114 error
= "supplied --dn is not a distinguished name";
119 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
120 BUILD_FROM_FILE
, file
, BUILD_END
);
126 set_file_mode(stdin
, CERT_ASN1_DER
);
127 if (!chunk_from_fd(0, &chunk
))
129 fprintf(stderr
, "reading private key failed: %s\n", strerror(errno
));
133 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
134 BUILD_BLOB
, chunk
, BUILD_END
);
139 error
= "parsing private key failed";
142 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_PKCS10_REQUEST
,
143 BUILD_SIGNING_KEY
, private,
145 BUILD_SUBJECT_ALTNAMES
, san
,
146 BUILD_CHALLENGE_PWD
, challenge_password
,
147 BUILD_DIGEST_ALG
, digest
,
151 error
= "generating certificate request failed";
154 if (!cert
->get_encoding(cert
, form
, &encoding
))
156 error
= "encoding certificate request failed";
159 set_file_mode(stdout
, form
);
160 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
162 error
= "writing certificate request failed";
170 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
175 fprintf(stderr
, "%s\n", error
);
181 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
182 return command_usage(error
);
186 * Register the command.
188 static void __attribute__ ((constructor
))reg()
190 command_register((command_t
) {
192 "create a PKCS#10 certificate request",
193 {" [--in file] [--type rsa|ecdsa|bliss] --dn distinguished-name",
194 "[--san subjectAltName]+ [--password challengePassword]",
195 "[--digest md5|sha1|sha224|sha256|sha384|sha512] [--outform der|pem]"},
197 {"help", 'h', 0, "show usage information"},
198 {"in", 'i', 1, "private key input file, default: stdin"},
199 {"type", 't', 1, "type of input key, default: rsa"},
200 {"dn", 'd', 1, "subject distinguished name"},
201 {"san", 'a', 1, "subjectAltName to include in cert request"},
202 {"password",'p', 1, "challengePassword to include in cert request"},
203 {"digest", 'g', 1, "digest for signature creation, default: sha256"},
204 {"outform", 'f', 1, "encoding of generated request, default: der"},