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