mbedtls: Fix setting allowed cipher suites
[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         if (server)
39 #ifdef CYASSL_OPENSSL_H_
40                 m = SSLv23_server_method();
41 #else
42                 m = TLSv1_2_server_method();
43 #endif
44         else
45                 m = SSLv23_client_method();
46
47         c = SSL_CTX_new((void *) m);
48         if (!c)
49                 return NULL;
50
51         SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
52 #ifndef OPENSSL_NO_ECDH
53         SSL_CTX_set_ecdh_auto(c, 1);
54 #endif
55         if (server)
56                 SSL_CTX_set_cipher_list(c, "DEFAULT:!RC4:@STRENGTH");
57         SSL_CTX_set_quiet_shutdown(c, 1);
58
59         return (void *) c;
60 }
61
62 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
63 {
64         int ret;
65
66         ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
67         if (ret < 1)
68                 return -1;
69
70         return 0;
71 }
72
73 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
74 {
75         int ret;
76
77         ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
78         if (ret < 1)
79                 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
80
81         if (ret < 1)
82                 return -1;
83
84         return 0;
85 }
86
87 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
88 {
89         int ret;
90
91         ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
92         if (ret < 1)
93                 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
94
95         if (ret < 1)
96                 return -1;
97
98         return 0;
99 }
100
101 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
102 {
103         SSL_CTX_free((void *) ctx);
104 }
105
106 void __ustream_ssl_session_free(void *ssl)
107 {
108         SSL_shutdown(ssl);
109         SSL_free(ssl);
110 }
111
112 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
113 {
114         us->error = ret;
115         uloop_timeout_set(&us->error_timer, 0);
116 }
117
118 #ifndef CYASSL_OPENSSL_H_
119
120 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
121 {
122         int ret;
123
124         if (!us->peer_cn)
125                 return false;
126
127         ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
128         return ret == 1;
129 }
130
131
132 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
133 {
134         void *ssl = us->ssl;
135         X509 *cert;
136         int res;
137
138         res = SSL_get_verify_result(ssl);
139         if (res != X509_V_OK) {
140                 if (us->notify_verify_error)
141                         us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
142                 return;
143         }
144
145         cert = SSL_get_peer_certificate(ssl);
146         if (!cert)
147                 return;
148
149         us->valid_cert = true;
150         us->valid_cn = ustream_ssl_verify_cn(us, cert);
151         X509_free(cert);
152 }
153
154 #endif
155
156 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
157 {
158         void *ssl = us->ssl;
159         int r;
160
161         if (us->server)
162                 r = SSL_accept(ssl);
163         else
164                 r = SSL_connect(ssl);
165
166         if (r == 1) {
167 #ifndef CYASSL_OPENSSL_H_
168                 ustream_ssl_verify_cert(us);
169 #endif
170                 return U_SSL_OK;
171         }
172
173         r = SSL_get_error(ssl, r);
174         if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
175                 return U_SSL_PENDING;
176
177         ustream_ssl_error(us, r);
178         return U_SSL_ERROR;
179 }
180
181 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
182 {
183         void *ssl = us->ssl;
184         int ret = SSL_write(ssl, buf, len);
185
186         if (ret < 0) {
187                 int err = SSL_get_error(ssl, ret);
188                 if (err == SSL_ERROR_WANT_WRITE)
189                         return 0;
190
191                 ustream_ssl_error(us, err);
192                 return -1;
193         }
194
195         return ret;
196 }
197
198 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
199 {
200         int ret = SSL_read(us->ssl, buf, len);
201
202         if (ret < 0) {
203                 ret = SSL_get_error(us->ssl, ret);
204                 if (ret == SSL_ERROR_WANT_READ)
205                         return U_SSL_PENDING;
206
207                 ustream_ssl_error(us, ret);
208                 return U_SSL_ERROR;
209         }
210
211         return ret;
212 }
213