2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * Generate a private key
21 static int gen(int argc
, char *argv
[])
23 key_encoding_type_t form
= KEY_PRIV_ASN1_DER
;
24 key_type_t type
= KEY_RSA
;
31 switch (getopt_long(argc
, argv
, "", command_opts
, NULL
))
34 return command_usage(NULL
);
36 if (streq(optarg
, "rsa"))
40 else if (streq(optarg
, "ecdsa"))
46 return command_usage("invalid key type");
50 if (!get_form(optarg
, &form
, FALSE
))
52 return command_usage("invalid key output format");
59 return command_usage("invalid key size");
65 return command_usage("invalid --gen option");
69 /* default key sizes */
84 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
85 BUILD_KEY_SIZE
, size
, BUILD_END
);
88 fprintf(stderr
, "private key generation failed\n");
91 if (!key
->get_encoding(key
, form
, &encoding
))
93 fprintf(stderr
, "private key encoding failed\n");
98 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
100 fprintf(stderr
, "writing private key failed\n");
109 * Register the command.
111 static void __attribute__ ((constructor
))reg()
113 command_register((command_t
) {
114 gen
, 'g', "gen", "generate a new private key",
115 {"[--type rsa|ecdsa] [--size bits] [--outform der|pem|pgp]"},
117 {"help", 'h', 0, "show usage information"},
118 {"type", 't', 1, "type of key, default: rsa"},
119 {"size", 's', 1, "keylength in bits, default: rsa 2048, ecdsa 384"},
120 {"outform", 'f', 1, "encoding of generated private key"},