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
23 cred_encoding_type_t form
= PRIVKEY_ASN1_DER
;
24 key_type_t type
= KEY_RSA
;
32 switch (command_getopt(&arg
))
35 return command_usage(NULL
);
37 if (streq(arg
, "rsa"))
41 else if (streq(arg
, "ecdsa"))
47 return command_usage("invalid key type");
51 if (!get_form(arg
, &form
, CRED_PRIVATE_KEY
))
53 return command_usage("invalid key output format");
60 return command_usage("invalid key size");
66 return command_usage("invalid --gen option");
70 /* default key sizes */
85 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
86 BUILD_KEY_SIZE
, size
, BUILD_END
);
89 fprintf(stderr
, "private key generation failed\n");
92 if (!key
->get_encoding(key
, form
, &encoding
))
94 fprintf(stderr
, "private key encoding failed\n");
99 if (fwrite(encoding
.ptr
, encoding
.len
, 1, stdout
) != 1)
101 fprintf(stderr
, "writing private key failed\n");
110 * Register the command.
112 static void __attribute__ ((constructor
))reg()
114 command_register((command_t
) {
115 gen
, 'g', "gen", "generate a new private key",
116 {"[--type rsa|ecdsa] [--size bits] [--outform der|pem|pgp]"},
118 {"help", 'h', 0, "show usage information"},
119 {"type", 't', 1, "type of key, default: rsa"},
120 {"size", 's', 1, "keylength in bits, default: rsa 2048, ecdsa 384"},
121 {"outform", 'f', 1, "encoding of generated private key"},