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