Wipe (some) secrets from memory after use
[tinc] / src / ed25519 / ecdsa.c
1 /*
2     ecdsa.c -- ECDSA key handling
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 "ed25519.h"
23
24 #define TINC_ECDSA_INTERNAL
25 typedef struct {
26         uint8_t private[64];
27         uint8_t public[32];
28 } ecdsa_t;
29
30 #include "../logger.h"
31 #include "../ecdsa.h"
32 #include "../utils.h"
33 #include "../xalloc.h"
34
35 // Get and set ECDSA keys
36 //
37 ecdsa_t *ecdsa_set_base64_public_key(const char *p) {
38         size_t len = strlen(p);
39
40         if(len != 43) {
41                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid size %lu for public key!", (unsigned long)len);
42                 return 0;
43         }
44
45         ecdsa_t *ecdsa = xzalloc(sizeof(*ecdsa));
46         len = b64decode_tinc(p, ecdsa->public, len);
47
48         if(len != 32) {
49                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid format of public key! len = %lu", (unsigned long)len);
50                 ecdsa_free(ecdsa);
51                 return 0;
52         }
53
54         return ecdsa;
55 }
56
57 char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa) {
58         char *base64 = xmalloc(44);
59         b64encode_tinc(ecdsa->public, base64, sizeof(ecdsa->public));
60
61         return base64;
62 }
63
64 // Read PEM ECDSA keys
65
66 static bool read_pem(FILE *fp, const char *type, void *vbuf, size_t size) {
67         const size_t buflen = size;
68         char line[1024];
69         bool data = false;
70         size_t typelen = strlen(type);
71         char *buf = vbuf;
72
73         while(fgets(line, sizeof(line), fp)) {
74                 if(!data) {
75                         if(strncmp(line, "-----BEGIN ", 11)) {
76                                 continue;
77                         }
78
79                         if(strncmp(line + 11, type, typelen)) {
80                                 continue;
81                         }
82
83                         data = true;
84                         continue;
85                 }
86
87                 if(!strncmp(line, "-----END ", 9)) {
88                         break;
89                 }
90
91                 size_t linelen = strcspn(line, "\r\n");
92                 size_t len = b64decode_tinc(line, line, linelen);
93
94                 if(!len || len > size) {
95                         logger(DEBUG_ALWAYS, LOG_ERR, "%s base64 data in PEM file\n", len ? "Too much" : "Invalid");
96                         errno = EINVAL;
97                         goto exit;
98                 }
99
100                 memcpy(buf, line, len);
101                 buf += len;
102                 size -= len;
103         }
104
105         if(size) {
106                 if(data) {
107                         errno = EINVAL;
108                         logger(DEBUG_ALWAYS, LOG_ERR, "Too little base64 data in PEM file\n");
109                 } else {
110                         errno = ENOENT;
111                 }
112         }
113
114 exit:
115         memzero(line, sizeof(line));
116
117         if(size) {
118                 memzero(vbuf, buflen);
119                 return false;
120         }
121
122         return true;
123 }
124
125 ecdsa_t *ecdsa_read_pem_public_key(FILE *fp) {
126         ecdsa_t *ecdsa = xzalloc(sizeof(*ecdsa));
127
128         if(read_pem(fp, "ED25519 PUBLIC KEY", ecdsa->public, sizeof(ecdsa->public))) {
129                 return ecdsa;
130         }
131
132         ecdsa_free(ecdsa);
133         return NULL;
134 }
135
136 ecdsa_t *ecdsa_read_pem_private_key(FILE *fp) {
137         ecdsa_t *ecdsa = xmalloc(sizeof(*ecdsa));
138
139         if(read_pem(fp, "ED25519 PRIVATE KEY", ecdsa->private, sizeof(*ecdsa))) {
140                 return ecdsa;
141         }
142
143         ecdsa_free(ecdsa);
144         return NULL;
145 }
146
147 size_t ecdsa_size(ecdsa_t *ecdsa) {
148         (void)ecdsa;
149         return 64;
150 }
151
152 // TODO: standardise output format?
153
154 bool ecdsa_sign(ecdsa_t *ecdsa, const void *in, size_t len, void *sig) {
155         ed25519_sign(sig, in, len, ecdsa->public, ecdsa->private);
156         return true;
157 }
158
159 bool ecdsa_verify(ecdsa_t *ecdsa, const void *in, size_t len, const void *sig) {
160         return ed25519_verify(sig, in, len, ecdsa->public);
161 }
162
163 bool ecdsa_active(ecdsa_t *ecdsa) {
164         return ecdsa;
165 }
166
167 void ecdsa_free(ecdsa_t *ecdsa) {
168         xzfree(ecdsa, sizeof(ecdsa_t));
169 }