implement certificate validation (including CN verification)
[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 static bool host_pattern_match(const unsigned char *pattern, const char *cn)
109 {
110 char c;
111
112 for (; (c = tolower(*pattern++)) != 0; cn++) {
113 if (c != '*') {
114 if (c != *cn)
115 return false;
116 continue;
117 }
118
119 do {
120 c = tolower(*pattern++);
121 } while (c == '*');
122
123 while (*cn) {
124 if (c == tolower(*cn) &&
125 host_pattern_match(pattern, cn))
126 return true;
127 if (*cn == '.')
128 return false;
129 cn++;
130 }
131
132 return !c;
133 }
134 return !*cn;
135 }
136
137 static bool host_pattern_match_asn1(ASN1_STRING *asn1, const char *cn)
138 {
139 unsigned char *pattern;
140 bool ret = false;
141
142 if (ASN1_STRING_to_UTF8(&pattern, asn1) < 0)
143 return false;
144
145 if (!pattern)
146 return false;
147
148 if (strlen((char *) pattern) == ASN1_STRING_length(asn1))
149 ret = host_pattern_match(pattern, cn);
150
151 OPENSSL_free(pattern);
152
153 return ret;
154 }
155
156 static bool ustream_ssl_verify_cn_alt(struct ustream_ssl *us, X509 *cert)
157 {
158 GENERAL_NAMES *alt_names;
159 int i, n_alt;
160
161 alt_names = X509_get_ext_d2i (cert, NID_subject_alt_name, NULL, NULL);
162 if (!alt_names)
163 return false;
164
165 n_alt = sk_GENERAL_NAME_num(alt_names);
166 for (i = 0; i < n_alt; i++) {
167 const GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
168
169 if (!name)
170 continue;
171
172 if (name->type != GEN_DNS)
173 continue;
174
175 if (host_pattern_match_asn1(name->d.dNSName, us->peer_cn))
176 return true;
177 }
178
179 return false;
180 }
181
182 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
183 {
184 ASN1_STRING *astr;
185 X509_NAME *xname;
186 int i, last;
187
188 if (!us->peer_cn)
189 return false;
190
191 if (ustream_ssl_verify_cn_alt(us, cert))
192 return true;
193
194 xname = X509_get_subject_name(cert);
195
196 last = -1;
197 while (1) {
198 i = X509_NAME_get_index_by_NID(xname, NID_commonName, last);
199 if (i < 0)
200 break;
201
202 last = i;
203 }
204
205 if (last < 0)
206 return false;
207
208 astr = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(xname, last));
209
210 return host_pattern_match_asn1(astr, us->peer_cn);
211 }
212
213
214 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
215 {
216 void *ssl = us->ssl;
217 X509 *cert;
218 int res;
219
220 cert = SSL_get_peer_certificate(ssl);
221 if (!cert)
222 return;
223
224 res = SSL_get_verify_result(ssl);
225 if (res != X509_V_OK) {
226 if (us->notify_verify_error)
227 us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
228 return;
229 }
230
231 us->valid_cert = true;
232 us->valid_cn = ustream_ssl_verify_cn(us, cert);
233 }
234
235 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
236 {
237 void *ssl = us->ssl;
238 int r;
239
240 if (us->server)
241 r = SSL_accept(ssl);
242 else
243 r = SSL_connect(ssl);
244
245 if (r == 1) {
246 ustream_ssl_verify_cert(us);
247 return U_SSL_OK;
248 }
249
250 r = SSL_get_error(ssl, r);
251 if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
252 return U_SSL_PENDING;
253
254 ustream_ssl_error(us, r);
255 return U_SSL_ERROR;
256 }
257
258 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
259 {
260 void *ssl = us->ssl;
261 int ret = SSL_write(ssl, buf, len);
262
263 if (ret < 0) {
264 int err = SSL_get_error(ssl, ret);
265 if (err == SSL_ERROR_WANT_WRITE)
266 return 0;
267
268 ustream_ssl_error(us, err);
269 return -1;
270 }
271
272 return ret;
273 }
274
275 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
276 {
277 int ret = SSL_read(us->ssl, buf, len);
278
279 if (ret < 0) {
280 ret = SSL_get_error(us->ssl, ret);
281 if (ret == SSL_ERROR_WANT_READ)
282 return U_SSL_PENDING;
283
284 ustream_ssl_error(us, ret);
285 return U_SSL_ERROR;
286 }
287
288 return ret;
289 }
290