Reformat all code using astyle.
[tinc] / src / sptps_keypair.c
1 /*
2     sptps_test.c -- Simple Peer-to-Peer Security test program
3     Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>,
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include <getopt.h>
23
24 #include "crypto.h"
25 #include "ecdsagen.h"
26 #include "utils.h"
27
28 static char *program_name;
29
30 void logger(int level, int priority, const char *format, ...) {
31         va_list ap;
32         va_start(ap, format);
33         vfprintf(stderr, format, ap);
34         va_end(ap);
35         fputc('\n', stderr);
36 }
37
38 static void usage() {
39         fprintf(stderr, "Usage: %s [options] private_key_file public_key_file\n\n", program_name);
40         fprintf(stderr, "Valid options are:\n"
41                 "  --help  Display this help and exit.\n"
42                 "\n");
43         fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
44 }
45
46 static struct option const long_options[] = {
47         {"help", no_argument, NULL, 1},
48         {NULL, 0, NULL, 0}
49 };
50
51 int main(int argc, char *argv[]) {
52         program_name = argv[0];
53         int r;
54         int option_index = 0;
55
56         while((r = getopt_long(argc, argv, "", long_options, &option_index)) != EOF) {
57                 switch(r) {
58                 case 0:   /* long option */
59                         break;
60
61                 case '?': /* wrong options */
62                         usage();
63                         return 1;
64
65                 case 1: /* help */
66                         usage();
67                         return 0;
68
69                 default:
70                         break;
71                 }
72         }
73
74         argc -= optind - 1;
75         argv += optind - 1;
76
77         if(argc != 3) {
78                 fprintf(stderr, "Wrong number of arguments.\n");
79                 usage();
80                 return 1;
81         }
82
83         crypto_init();
84
85         ecdsa_t *key = ecdsa_generate();
86
87         if(!key) {
88                 return 1;
89         }
90
91         FILE *fp = fopen(argv[1], "w");
92
93         if(fp) {
94                 if(!ecdsa_write_pem_private_key(key, fp)) {
95                         fprintf(stderr, "Could not write ECDSA private key\n");
96                         return 1;
97                 }
98
99                 fclose(fp);
100         } else {
101                 fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[1], strerror(errno));
102                 return 1;
103         }
104
105         fp = fopen(argv[2], "w");
106
107         if(fp) {
108                 if(!ecdsa_write_pem_public_key(key, fp)) {
109                         fprintf(stderr, "Could not write ECDSA public key\n");
110                 }
111
112                 fclose(fp);
113         } else {
114                 fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[2], strerror(errno));
115                 return 1;
116         }
117
118         return 0;
119 }