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