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