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 signature_params_t
*scheme
= NULL
;
34 certificate_t
*cert
= NULL
;
35 private_key_t
*private = NULL
;
36 char *file
= NULL
, *keyid
= NULL
, *dn
= NULL
, *error
= NULL
;
37 identification_t
*id
= NULL
;
39 chunk_t encoding
= chunk_empty
;
40 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"))
65 else if (streq(arg
, "priv"))
71 error
= "invalid input type";
76 if (!enum_from_name(hash_algorithm_short_names
, arg
, &digest
))
78 error
= "invalid --digest type";
83 if (streq(arg
, "pss"))
87 else if (!streq(arg
, "pkcs1"))
89 error
= "invalid RSA padding";
100 san
->insert_last(san
, identification_create_from_string(arg
));
103 challenge_password
= chunk_create(arg
, strlen(arg
));
106 if (!get_form(arg
, &form
, CRED_CERTIFICATE
))
108 error
= "invalid output format";
118 error
= "invalid --req option";
126 error
= "--dn is required";
129 id
= identification_create_from_string(dn
);
130 if (id
->get_type(id
) != ID_DER_ASN1_DN
)
132 error
= "supplied --dn is not a distinguished name";
137 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
138 BUILD_FROM_FILE
, file
, BUILD_END
);
144 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
145 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
146 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
153 set_file_mode(stdin
, CERT_ASN1_DER
);
154 if (!chunk_from_fd(0, &chunk
))
156 fprintf(stderr
, "reading private key failed: %s\n", strerror(errno
));
160 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
161 BUILD_BLOB
, chunk
, BUILD_END
);
166 error
= "parsing private key failed";
169 scheme
= get_signature_scheme(private, digest
, pss
);
171 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_PKCS10_REQUEST
,
172 BUILD_SIGNING_KEY
, private,
174 BUILD_SUBJECT_ALTNAMES
, san
,
175 BUILD_CHALLENGE_PWD
, challenge_password
,
176 BUILD_SIGNATURE_SCHEME
, scheme
,
180 error
= "generating certificate request failed";
183 if (!cert
->get_encoding(cert
, form
, &encoding
))
185 error
= "encoding certificate request failed";
188 set_file_mode(stdout
, form
);
189 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
191 error
= "writing certificate request failed";
199 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
200 signature_params_destroy(scheme
);
205 fprintf(stderr
, "%s\n", error
);
211 san
->destroy_offset(san
, offsetof(identification_t
, destroy
));
212 return command_usage(error
);
216 * Register the command.
218 static void __attribute__ ((constructor
))reg()
220 command_register((command_t
) {
222 "create a PKCS#10 certificate request",
223 {"[--in file|--keyid hex] [--type rsa|ecdsa|bliss|priv] --dn distinguished-name",
224 "[--san subjectAltName]+ [--password challengePassword]",
225 "[--digest md5|sha1|sha224|sha256|sha384|sha512|sha3_224|sha3_256|sha3_384|sha3_512]",
226 "[--rsa-padding pkcs1|pss]",
227 "[--outform der|pem]"},
229 {"help", 'h', 0, "show usage information"},
230 {"in", 'i', 1, "private key input file, default: stdin"},
231 {"keyid", 'x', 1, "smartcard or TPM private key object handle"},
232 {"type", 't', 1, "type of input key, default: priv"},
233 {"dn", 'd', 1, "subject distinguished name"},
234 {"san", 'a', 1, "subjectAltName to include in cert request"},
235 {"password", 'p', 1, "challengePassword to include in cert request"},
236 {"digest", 'g', 1, "digest for signature creation, default: key-specific"},
237 {"rsa-padding", 'R', 1, "padding for RSA signatures, default: pkcs1"},
238 {"outform", 'f', 1, "encoding of generated request, default: der"},