2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2009-2017 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <collections/linked_list.h>
23 #include <credentials/certificates/certificate.h>
26 * Create a self-signed PKCS#10 certificate requesst.
30 cred_encoding_type_t form
= CERT_ASN1_DER
;
31 key_type_t type
= KEY_ANY
;
32 hash_algorithm_t digest
= HASH_UNKNOWN
;
33 certificate_t
*cert
= NULL
;
34 private_key_t
*private = NULL
;
35 char *file
= NULL
, *keyid
= NULL
, *dn
= NULL
, *error
= NULL
;
36 identification_t
*id
= NULL
;
38 chunk_t encoding
= chunk_empty
;
39 chunk_t challenge_password
= chunk_empty
;
42 san
= linked_list_create();
46 switch (command_getopt(&arg
))
51 if (streq(arg
, "rsa"))
55 else if (streq(arg
, "ecdsa"))
59 else if (streq(arg
, "bliss"))
63 else if (streq(arg
, "priv"))
69 error
= "invalid input type";
74 if (!enum_from_name(hash_algorithm_short_names
, arg
, &digest
))
76 error
= "invalid --digest type";
87 san
->insert_last(san
, identification_create_from_string(arg
));
90 challenge_password
= chunk_create(arg
, strlen(arg
));
93 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
95 error
= "invalid output format";
105 error
= "invalid --req option";
113 error
= "--dn is required";
116 id
= identification_create_from_string(dn
);
117 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
119 error
= "supplied --dn is not a distinguished name";
124 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
125 BUILD_FROM_FILE
, file
, BUILD_END
);
131 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
132 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
133 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
140 set_file_mode(stdin
, CERT_ASN1_DER
);
141 if (!chunk_from_fd(0, &chunk
))
143 fprintf(stderr
, "reading private key failed: %s\n", strerror(errno
));
147 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
148 BUILD_BLOB
, chunk
, BUILD_END
);
153 error
= "parsing private key failed";
156 if (digest
== HASH_UNKNOWN
)
158 digest
= get_default_digest(private);
160 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_PKCS10_REQUEST
,
161 BUILD_SIGNING_KEY
, private,
163 BUILD_SUBJECT_ALTNAMES
, san
,
164 BUILD_CHALLENGE_PWD
, challenge_password
,
165 BUILD_DIGEST_ALG
, digest
,
169 error
= "generating certificate request failed";
172 if (!cert
->get_encoding(cert
, form
, &encoding
))
174 error
= "encoding certificate request failed";
177 set_file_mode(stdout
, form
);
178 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
180 error
= "writing certificate request failed";
188 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
193 fprintf(stderr
, "%s\n", error
);
199 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
200 return command_usage(error
);
204 * Register the command.
206 static void __attribute__ ((constructor
))reg()
208 command_register((command_t
) {
210 "create a PKCS#10 certificate request",
211 {" [--in file|--keyid hex] [--type rsa|ecdsa|bliss|priv] --dn distinguished-name",
212 "[--san subjectAltName]+ [--password challengePassword]",
213 "[--digest md5|sha1|sha224|sha256|sha384|sha512|sha3_224|sha3_256|sha3_384|sha3_512]",
214 "[--outform der|pem]"},
216 {"help", 'h', 0, "show usage information"},
217 {"in", 'i', 1, "private key input file, default: stdin"},
218 {"keyid", 'x', 1, "smartcard or TPM private key object handle"},
219 {"type", 't', 1, "type of input key, default: priv"},
220 {"dn", 'd', 1, "subject distinguished name"},
221 {"san", 'a', 1, "subjectAltName to include in cert request"},
222 {"password",'p', 1, "challengePassword to include in cert request"},
223 {"digest", 'g', 1, "digest for signature creation, default: key-specific"},
224 {"outform", 'f', 1, "encoding of generated request, default: der"},