openssl: Fix host_pattern_match
[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         SSL_CTX_set_quiet_shutdown(c, 1);
56
57         return (void *) c;
58 }
59
60 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
61 {
62         int ret;
63
64         ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
65         if (ret < 1)
66                 return -1;
67
68         return 0;
69 }
70
71 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
72 {
73         int ret;
74
75         ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
76         if (ret < 1)
77                 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
78
79         if (ret < 1)
80                 return -1;
81
82         return 0;
83 }
84
85 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
86 {
87         int ret;
88
89         ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
90         if (ret < 1)
91                 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
92
93         if (ret < 1)
94                 return -1;
95
96         return 0;
97 }
98
99 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
100 {
101         SSL_CTX_free((void *) ctx);
102 }
103
104 void __ustream_ssl_session_free(void *ssl)
105 {
106         SSL_shutdown(ssl);
107         SSL_free(ssl);
108 }
109
110 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
111 {
112         us->error = ret;
113         uloop_timeout_set(&us->error_timer, 0);
114 }
115
116 #ifndef CYASSL_OPENSSL_H_
117
118 static bool host_pattern_match(const unsigned char *pattern, const char *cn)
119 {
120         char c;
121
122         for (; (c = tolower(*pattern++)) != 0; cn++) {
123                 if (c != '*') {
124                         if (c != tolower(*cn))
125                                 return false;
126                         continue;
127                 }
128
129                 do {
130                         c = tolower(*pattern++);
131                 } while (c == '*');
132
133                 while (*cn) {
134                         if (c == tolower(*cn) &&
135                             host_pattern_match(pattern, cn))
136                                 return true;
137                         if (*cn == '.')
138                                 return false;
139                         cn++;
140                 }
141
142                 return !c;
143         }
144         return !*cn;
145 }
146
147 static bool host_pattern_match_asn1(ASN1_STRING *asn1, const char *cn)
148 {
149         unsigned char *pattern;
150         bool ret = false;
151
152         if (ASN1_STRING_to_UTF8(&pattern, asn1) < 0)
153                 return false;
154
155         if (!pattern)
156                 return false;
157
158         if (strlen((char *) pattern) == ASN1_STRING_length(asn1))
159                 ret = host_pattern_match(pattern, cn);
160
161         OPENSSL_free(pattern);
162
163         return ret;
164 }
165
166 static bool ustream_ssl_verify_cn_alt(struct ustream_ssl *us, X509 *cert)
167 {
168         GENERAL_NAMES *alt_names;
169         int i, n_alt;
170         bool ret = false;
171
172         alt_names = X509_get_ext_d2i (cert, NID_subject_alt_name, NULL, NULL);
173         if (!alt_names)
174                 return false;
175
176         n_alt = sk_GENERAL_NAME_num(alt_names);
177         for (i = 0; i < n_alt; i++) {
178                 const GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
179
180                 if (!name)
181                         continue;
182
183                 if (name->type != GEN_DNS)
184                         continue;
185
186                 if (host_pattern_match_asn1(name->d.dNSName, us->peer_cn)) {
187                         ret = true;
188                         break;
189                 }
190         }
191
192         sk_GENERAL_NAME_free(alt_names);
193         return ret;
194 }
195
196 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
197 {
198         ASN1_STRING *astr;
199         X509_NAME *xname;
200         int i, last;
201
202         if (!us->peer_cn)
203                 return false;
204
205         if (ustream_ssl_verify_cn_alt(us, cert))
206                 return true;
207
208         xname = X509_get_subject_name(cert);
209
210         last = -1;
211         while (1) {
212                 i = X509_NAME_get_index_by_NID(xname, NID_commonName, last);
213                 if (i < 0)
214                         break;
215
216                 last = i;
217         }
218
219         if (last < 0)
220                 return false;
221
222         astr = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(xname, last));
223
224         return host_pattern_match_asn1(astr, us->peer_cn);
225 }
226
227
228 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
229 {
230         void *ssl = us->ssl;
231         X509 *cert;
232         int res;
233
234         res = SSL_get_verify_result(ssl);
235         if (res != X509_V_OK) {
236                 if (us->notify_verify_error)
237                         us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
238                 return;
239         }
240
241         cert = SSL_get_peer_certificate(ssl);
242         if (!cert)
243                 return;
244
245         us->valid_cert = true;
246         us->valid_cn = ustream_ssl_verify_cn(us, cert);
247         X509_free(cert);
248 }
249
250 #endif
251
252 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
253 {
254         void *ssl = us->ssl;
255         int r;
256
257         if (us->server)
258                 r = SSL_accept(ssl);
259         else
260                 r = SSL_connect(ssl);
261
262         if (r == 1) {
263 #ifndef CYASSL_OPENSSL_H_
264                 ustream_ssl_verify_cert(us);
265 #endif
266                 return U_SSL_OK;
267         }
268
269         r = SSL_get_error(ssl, r);
270         if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
271                 return U_SSL_PENDING;
272
273         ustream_ssl_error(us, r);
274         return U_SSL_ERROR;
275 }
276
277 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
278 {
279         void *ssl = us->ssl;
280         int ret = SSL_write(ssl, buf, len);
281
282         if (ret < 0) {
283                 int err = SSL_get_error(ssl, ret);
284                 if (err == SSL_ERROR_WANT_WRITE)
285                         return 0;
286
287                 ustream_ssl_error(us, err);
288                 return -1;
289         }
290
291         return ret;
292 }
293
294 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
295 {
296         int ret = SSL_read(us->ssl, buf, len);
297
298         if (ret < 0) {
299                 ret = SSL_get_error(us->ssl, ret);
300                 if (ret == SSL_ERROR_WANT_READ)
301                         return U_SSL_PENDING;
302
303                 ustream_ssl_error(us, ret);
304                 return U_SSL_ERROR;
305         }
306
307         return ret;
308 }
309