splitted PKI tool to a file per command
[strongswan.git] / src / pki / commands / gen.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "pki.h"
17
18 /**
19 * Generate a private key
20 */
21 static int gen(int argc, char *argv[])
22 {
23 key_encoding_type_t form = KEY_PRIV_ASN1_DER;
24 key_type_t type = KEY_RSA;
25 u_int size = 0;
26 private_key_t *key;
27 chunk_t encoding;
28
29 while (TRUE)
30 {
31 switch (getopt_long(argc, argv, "", command_opts, NULL))
32 {
33 case 'h':
34 return command_usage(CMD_GEN, NULL);
35 case 't':
36 if (streq(optarg, "rsa"))
37 {
38 type = KEY_RSA;
39 }
40 else if (streq(optarg, "ecdsa"))
41 {
42 type = KEY_ECDSA;
43 }
44 else
45 {
46 return command_usage(CMD_GEN, "invalid key type");
47 }
48 continue;
49 case 'o':
50 if (!get_form(optarg, &form, FALSE))
51 {
52 return command_usage(CMD_GEN, "invalid key output format");
53 }
54 continue;
55 case 's':
56 size = atoi(optarg);
57 if (!size)
58 {
59 return command_usage(CMD_GEN, "invalid key size");
60 }
61 continue;
62 case EOF:
63 break;
64 default:
65 return command_usage(CMD_GEN, "invalid --gen option");
66 }
67 break;
68 }
69 /* default key sizes */
70 if (!size)
71 {
72 switch (type)
73 {
74 case KEY_RSA:
75 size = 2048;
76 break;
77 case KEY_ECDSA:
78 size = 384;
79 break;
80 default:
81 break;
82 }
83 }
84 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
85 BUILD_KEY_SIZE, size, BUILD_END);
86 if (!key)
87 {
88 fprintf(stderr, "private key generation failed\n");
89 return 1;
90 }
91 if (!key->get_encoding(key, form, &encoding))
92 {
93 fprintf(stderr, "private key encoding failed\n");
94 key->destroy(key);
95 return 1;
96 }
97 key->destroy(key);
98 if (fwrite(encoding.ptr, encoding.len, 1, stdout) != 1)
99 {
100 fprintf(stderr, "writing private key failed\n");
101 free(encoding.ptr);
102 return 1;
103 }
104 free(encoding.ptr);
105 return 0;
106 }
107
108 /**
109 * Register the command.
110 */
111 static void __attribute__ ((constructor))reg()
112 {
113 command_register(CMD_GEN, (command_t) {
114 gen, 'g', "gen", "generate a new private key",
115 {"[--type rsa|ecdsa] [--size bits] [--outform der|pem|pgp]"},
116 {
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"},
121 }
122 });
123 }
124