2 ecdsa.c -- ECDSA key handling
3 Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
20 #include "../system.h"
24 #define TINC_ECDSA_INTERNAL
30 #include "../logger.h"
33 #include "../xalloc.h"
35 static ecdsa_t *ecdsa_new(void) ATTR_MALLOC ATTR_DEALLOCATOR(ecdsa_free);
36 static ecdsa_t *ecdsa_new(void) {
37 return xzalloc(sizeof(ecdsa_t));
40 // Get and set ECDSA keys
42 ecdsa_t *ecdsa_set_base64_public_key(const char *p) {
43 size_t len = strlen(p);
46 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid size %lu for public key!", (unsigned long)len);
50 ecdsa_t *ecdsa = ecdsa_new();
51 len = b64decode_tinc(p, ecdsa->public, len);
54 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid format of public key! len = %lu", (unsigned long)len);
62 char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa) {
63 char *base64 = xmalloc(44);
64 b64encode_tinc(ecdsa->public, base64, sizeof(ecdsa->public));
69 // Read PEM ECDSA keys
71 static bool read_pem(FILE *fp, const char *type, void *vbuf, size_t size) {
72 const size_t buflen = size;
75 size_t typelen = strlen(type);
78 while(fgets(line, sizeof(line), fp)) {
80 if(strncmp(line, "-----BEGIN ", 11)) {
84 if(strncmp(line + 11, type, typelen)) {
92 if(!strncmp(line, "-----END ", 9)) {
96 size_t linelen = strcspn(line, "\r\n");
97 size_t len = b64decode_tinc(line, line, linelen);
99 if(!len || len > size) {
100 logger(DEBUG_ALWAYS, LOG_ERR, "%s base64 data in PEM file\n", len ? "Too much" : "Invalid");
105 memcpy(buf, line, len);
113 logger(DEBUG_ALWAYS, LOG_ERR, "Too little base64 data in PEM file\n");
120 memzero(line, sizeof(line));
123 memzero(vbuf, buflen);
130 ecdsa_t *ecdsa_read_pem_public_key(FILE *fp) {
131 ecdsa_t *ecdsa = ecdsa_new();
133 if(read_pem(fp, "ED25519 PUBLIC KEY", ecdsa->public, sizeof(ecdsa->public))) {
141 ecdsa_t *ecdsa_read_pem_private_key(FILE *fp) {
142 ecdsa_t *ecdsa = ecdsa_new();
144 if(read_pem(fp, "ED25519 PRIVATE KEY", ecdsa->private, sizeof(*ecdsa))) {
152 size_t ecdsa_size(ecdsa_t *ecdsa) {
157 // TODO: standardise output format?
159 bool ecdsa_sign(ecdsa_t *ecdsa, const void *in, size_t len, void *sig) {
160 ed25519_sign(sig, in, len, ecdsa->public, ecdsa->private);
164 bool ecdsa_verify(ecdsa_t *ecdsa, const void *in, size_t len, const void *sig) {
165 return ed25519_verify(sig, in, len, ecdsa->public);
168 bool ecdsa_active(ecdsa_t *ecdsa) {
172 void ecdsa_free(ecdsa_t *ecdsa) {
173 xzfree(ecdsa, sizeof(ecdsa_t));