add missing include files
[project/ustream-ssl.git] / ustream-openssl.c
1 /*
2  * ustream-ssl - library for SSL over ustream
3  *
4  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include <string.h>
20 #include <ctype.h>
21 #include <openssl/x509v3.h>
22 #include "ustream-ssl.h"
23 #include "ustream-internal.h"
24
25 __hidden struct ustream_ssl_ctx *
26 __ustream_ssl_context_new(bool server)
27 {
28         static bool _init = false;
29         const void *m;
30         SSL_CTX *c;
31
32         if (!_init) {
33                 SSL_load_error_strings();
34                 SSL_library_init();
35                 _init = true;
36         }
37
38 #ifdef CYASSL_OPENSSL_H_
39         if (server)
40                 m = SSLv23_server_method();
41         else
42                 m = SSLv23_client_method();
43 #else
44         if (server)
45                 m = TLSv1_server_method();
46         else
47                 m = TLSv1_client_method();
48 #endif
49
50         c = SSL_CTX_new((void *) m);
51         if (!c)
52                 return NULL;
53
54         SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
55
56         return (void *) c;
57 }
58
59 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
60 {
61         int ret;
62
63         ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
64         if (ret < 1)
65                 return -1;
66
67         return 0;
68 }
69
70 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
71 {
72         int ret;
73
74         ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
75         if (ret < 1)
76                 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
77
78         if (ret < 1)
79                 return -1;
80
81         return 0;
82 }
83
84 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
85 {
86         int ret;
87
88         ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
89         if (ret < 1)
90                 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
91
92         if (ret < 1)
93                 return -1;
94
95         return 0;
96 }
97
98 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
99 {
100         SSL_CTX_free((void *) ctx);
101 }
102
103 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
104 {
105         us->error = ret;
106         uloop_timeout_set(&us->error_timer, 0);
107 }
108
109 #ifndef CYASSL_OPENSSL_H_
110
111 static bool host_pattern_match(const unsigned char *pattern, const char *cn)
112 {
113         char c;
114
115         for (; (c = tolower(*pattern++)) != 0; cn++) {
116                 if (c != '*') {
117                         if (c != *cn)
118                                 return false;
119                         continue;
120                 }
121
122                 do {
123                         c = tolower(*pattern++);
124                 } while (c == '*');
125
126                 while (*cn) {
127                         if (c == tolower(*cn) &&
128                             host_pattern_match(pattern, cn))
129                                 return true;
130                         if (*cn == '.')
131                                 return false;
132                         cn++;
133                 }
134
135                 return !c;
136         }
137         return !*cn;
138 }
139
140 static bool host_pattern_match_asn1(ASN1_STRING *asn1, const char *cn)
141 {
142         unsigned char *pattern;
143         bool ret = false;
144
145         if (ASN1_STRING_to_UTF8(&pattern, asn1) < 0)
146                 return false;
147
148         if (!pattern)
149                 return false;
150
151         if (strlen((char *) pattern) == ASN1_STRING_length(asn1))
152                 ret = host_pattern_match(pattern, cn);
153
154         OPENSSL_free(pattern);
155
156         return ret;
157 }
158
159 static bool ustream_ssl_verify_cn_alt(struct ustream_ssl *us, X509 *cert)
160 {
161         GENERAL_NAMES *alt_names;
162         int i, n_alt;
163
164         alt_names = X509_get_ext_d2i (cert, NID_subject_alt_name, NULL, NULL);
165         if (!alt_names)
166                 return false;
167
168         n_alt = sk_GENERAL_NAME_num(alt_names);
169         for (i = 0; i < n_alt; i++) {
170                 const GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
171
172                 if (!name)
173                         continue;
174
175                 if (name->type != GEN_DNS)
176                         continue;
177
178                 if (host_pattern_match_asn1(name->d.dNSName, us->peer_cn))
179                         return true;
180         }
181
182         return false;
183 }
184
185 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
186 {
187         ASN1_STRING *astr;
188         X509_NAME *xname;
189         int i, last;
190
191         if (!us->peer_cn)
192                 return false;
193
194         if (ustream_ssl_verify_cn_alt(us, cert))
195                 return true;
196
197         xname = X509_get_subject_name(cert);
198
199         last = -1;
200         while (1) {
201                 i = X509_NAME_get_index_by_NID(xname, NID_commonName, last);
202                 if (i < 0)
203                         break;
204
205                 last = i;
206         }
207
208         if (last < 0)
209                 return false;
210
211         astr = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(xname, last));
212
213         return host_pattern_match_asn1(astr, us->peer_cn);
214 }
215
216
217 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
218 {
219         void *ssl = us->ssl;
220         X509 *cert;
221         int res;
222
223         cert = SSL_get_peer_certificate(ssl);
224         if (!cert)
225                 return;
226
227         res = SSL_get_verify_result(ssl);
228         if (res != X509_V_OK) {
229                 if (us->notify_verify_error)
230                         us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
231                 return;
232         }
233
234         us->valid_cert = true;
235         us->valid_cn = ustream_ssl_verify_cn(us, cert);
236 }
237
238 #endif
239
240 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
241 {
242         void *ssl = us->ssl;
243         int r;
244
245         if (us->server)
246                 r = SSL_accept(ssl);
247         else
248                 r = SSL_connect(ssl);
249
250         if (r == 1) {
251 #ifndef CYASSL_OPENSSL_H_
252                 ustream_ssl_verify_cert(us);
253 #endif
254                 return U_SSL_OK;
255         }
256
257         r = SSL_get_error(ssl, r);
258         if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
259                 return U_SSL_PENDING;
260
261         ustream_ssl_error(us, r);
262         return U_SSL_ERROR;
263 }
264
265 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
266 {
267         void *ssl = us->ssl;
268         int ret = SSL_write(ssl, buf, len);
269
270         if (ret < 0) {
271                 int err = SSL_get_error(ssl, ret);
272                 if (err == SSL_ERROR_WANT_WRITE)
273                         return 0;
274
275                 ustream_ssl_error(us, err);
276                 return -1;
277         }
278
279         return ret;
280 }
281
282 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
283 {
284         int ret = SSL_read(us->ssl, buf, len);
285
286         if (ret < 0) {
287                 ret = SSL_get_error(us->ssl, ret);
288                 if (ret == SSL_ERROR_WANT_READ)
289                         return U_SSL_PENDING;
290
291                 ustream_ssl_error(us, ret);
292                 return U_SSL_ERROR;
293         }
294
295         return ret;
296 }
297