Use Ed25519 keys.
[oweals/tinc.git] / 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 static void usage() {
31         fprintf(stderr, "Usage: %s [options] private_key_file public_key_file\n\n", program_name);
32         fprintf(stderr, "Valid options are:\n"
33                         "  --help  Display this help and exit.\n"
34                         "\n");
35         fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
36 }
37
38 static struct option const long_options[] = {
39         {"help", no_argument, NULL, 1},
40         {NULL, 0, NULL, 0}
41 };
42
43 int main(int argc, char *argv[]) {
44         program_name = argv[0];
45         int r;
46         int option_index = 0;
47
48         while((r = getopt_long(argc, argv, "", long_options, &option_index)) != EOF) {
49                 switch (r) {
50                         case 0:   /* long option */
51                                 break;
52
53                         case '?': /* wrong options */
54                                 usage();
55                                 return 1;
56
57                         case 1: /* help */
58                                 usage();
59                                 return 0;
60
61                         default:
62                                 break;
63                 }
64         }
65
66         argc -= optind - 1;
67         argv += optind - 1;
68
69         if(argc != 3) {
70                 fprintf(stderr, "Wrong number of arguments.\n");
71                 usage();
72                 return 1;
73         }
74
75         crypto_init();
76
77         ecdsa_t *key = ecdsa_generate();
78         if(!key)
79                 return 1;
80         
81         FILE *fp = fopen(argv[1], "w");
82         if(fp) {
83                 ecdsa_write_pem_private_key(key, fp);
84                 fclose(fp);
85         } else {
86                 fprintf(stderr, "Could not open '%s' for writing: %s\n", strerror(errno));
87                 return 1;
88         }
89
90         fp = fopen(argv[2], "w");
91         if(fp) {
92                 ecdsa_write_pem_public_key(key, fp);
93                 fclose(fp);
94         } else {
95                 fprintf(stderr, "Could not open '%s' for writing: %s\n", strerror(errno));
96                 return 1;
97         }
98
99         return 0;
100 }