bca89fc5623719e77d69b93a77206124be84dfcb
[tinc] / src / openssl / 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 <openssl/pem.h>
23 #include <openssl/err.h>
24
25 #define __TINC_ECDSA_INTERNAL__
26 typedef EC_KEY ecdsa_t;
27
28 #include "../logger.h"
29 #include "../ecdsa.h"
30 #include "../utils.h"
31 #include "../xalloc.h"
32
33 // Get and set ECDSA keys
34 //
35 ecdsa_t *ecdsa_set_base64_public_key(const char *p) {
36         ecdsa_t *ecdsa = EC_KEY_new_by_curve_name(NID_secp521r1);
37         if(!ecdsa) {
38                 logger(DEBUG_ALWAYS, LOG_DEBUG, "EC_KEY_new_by_curve_name failed: %s", ERR_error_string(ERR_get_error(), NULL));
39                 return NULL;
40         }
41
42         int len = strlen(p);
43         unsigned char pubkey[len / 4 * 3 + 3];
44         const unsigned char *ppubkey = pubkey;
45         len = b64decode(p, (char *)pubkey, len);
46
47         if(!o2i_ECPublicKey(&ecdsa, &ppubkey, len)) {
48                 logger(DEBUG_ALWAYS, LOG_DEBUG, "o2i_ECPublicKey failed: %s", ERR_error_string(ERR_get_error(), NULL));
49                 EC_KEY_free(ecdsa);
50                 return NULL;
51         }
52
53         return ecdsa;
54 }
55
56 char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa) {
57         unsigned char *pubkey = NULL;
58         int len = i2o_ECPublicKey(ecdsa, &pubkey);
59
60         char *base64 = xmalloc(len * 4 / 3 + 5);
61         b64encode((char *)pubkey, base64, len);
62
63         free(pubkey);
64
65         return base64;
66 }
67
68 // Read PEM ECDSA keys
69
70 ecdsa_t *ecdsa_read_pem_public_key(FILE *fp) {
71         ecdsa_t *ecdsa = PEM_read_EC_PUBKEY(fp, NULL, NULL, NULL);
72
73         if(!ecdsa)
74                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA public key: %s", ERR_error_string(ERR_get_error(), NULL));
75
76         return ecdsa;
77 }
78
79 ecdsa_t *ecdsa_read_pem_private_key(FILE *fp) {
80         ecdsa_t *ecdsa = PEM_read_ECPrivateKey(fp, NULL, NULL, NULL);
81
82         if(!ecdsa)
83                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read ECDSA private key: %s", ERR_error_string(ERR_get_error(), NULL));
84
85         return ecdsa;
86 }
87
88 size_t ecdsa_size(ecdsa_t *ecdsa) {
89         return ECDSA_size(ecdsa);
90 }
91
92 // TODO: standardise output format?
93
94 bool ecdsa_sign(ecdsa_t *ecdsa, const void *in, size_t len, void *sig) {
95         unsigned int siglen = ECDSA_size(ecdsa);
96
97         unsigned char hash[SHA512_DIGEST_LENGTH];
98         SHA512(in, len, hash);
99
100         memset(sig, 0, siglen);
101
102         if(!ECDSA_sign(0, hash, sizeof hash, sig, &siglen, ecdsa)) {
103                 logger(DEBUG_ALWAYS, LOG_DEBUG, "ECDSA_sign() failed: %s", ERR_error_string(ERR_get_error(), NULL));
104                 return false;
105         }
106
107         return true;
108 }
109
110 bool ecdsa_verify(ecdsa_t *ecdsa, const void *in, size_t len, const void *sig) {
111         unsigned int siglen = ECDSA_size(ecdsa);
112
113         unsigned char hash[SHA512_DIGEST_LENGTH];
114         SHA512(in, len, hash);
115
116         if(!ECDSA_verify(0, hash, sizeof hash, sig, siglen, ecdsa)) {
117                 logger(DEBUG_ALWAYS, LOG_DEBUG, "ECDSA_verify() failed: %s", ERR_error_string(ERR_get_error(), NULL));
118                 return false;
119         }
120
121         return true;
122 }
123
124 bool ecdsa_active(ecdsa_t *ecdsa) {
125         return ecdsa;
126 }
127
128 void ecdsa_free(ecdsa_t *ecdsa) {
129         if(ecdsa)
130                 EC_KEY_free(ecdsa);
131 }