Fix all warnings when compiling with -Wall -W -pedantic.
[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         (void)level;
32         (void)priority;
33         va_list ap;
34
35         va_start(ap, format);
36         vfprintf(stderr, format, ap);
37         va_end(ap);
38
39         fputc('\n', stderr);
40 }
41
42 static void usage() {
43         fprintf(stderr, "Usage: %s [options] private_key_file public_key_file\n\n", program_name);
44         fprintf(stderr, "Valid options are:\n"
45                 "  --help  Display this help and exit.\n"
46                 "\n");
47         fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
48 }
49
50 static struct option const long_options[] = {
51         {"help", no_argument, NULL, 1},
52         {NULL, 0, NULL, 0}
53 };
54
55 int main(int argc, char *argv[]) {
56         program_name = argv[0];
57         int r;
58         int option_index = 0;
59
60         while((r = getopt_long(argc, argv, "", long_options, &option_index)) != EOF) {
61                 switch(r) {
62                 case 0:   /* long option */
63                         break;
64
65                 case '?': /* wrong options */
66                         usage();
67                         return 1;
68
69                 case 1: /* help */
70                         usage();
71                         return 0;
72
73                 default:
74                         break;
75                 }
76         }
77
78         argc -= optind - 1;
79         argv += optind - 1;
80
81         if(argc != 3) {
82                 fprintf(stderr, "Wrong number of arguments.\n");
83                 usage();
84                 return 1;
85         }
86
87         crypto_init();
88
89         ecdsa_t *key = ecdsa_generate();
90
91         if(!key) {
92                 return 1;
93         }
94
95         FILE *fp = fopen(argv[1], "w");
96
97         if(fp) {
98                 if(!ecdsa_write_pem_private_key(key, fp)) {
99                         fprintf(stderr, "Could not write ECDSA private key\n");
100                         return 1;
101                 }
102
103                 fclose(fp);
104         } else {
105                 fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[1], strerror(errno));
106                 return 1;
107         }
108
109         fp = fopen(argv[2], "w");
110
111         if(fp) {
112                 if(!ecdsa_write_pem_public_key(key, fp)) {
113                         fprintf(stderr, "Could not write ECDSA public key\n");
114                 }
115
116                 fclose(fp);
117         } else {
118                 fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[2], strerror(errno));
119                 return 1;
120         }
121
122         return 0;
123 }