2 * ustream-ssl - library for SSL over ustream
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #include <sys/types.h>
20 #include <sys/random.h>
26 #include "ustream-ssl.h"
27 #include "ustream-internal.h"
28 #include <psa/crypto.h>
30 static int s_ustream_read(void *ctx
, unsigned char *buf
, size_t len
)
32 struct ustream
*s
= ctx
;
39 sbuf
= ustream_get_read_buf(s
, &slen
);
40 if ((size_t) slen
> len
)
44 return MBEDTLS_ERR_SSL_WANT_READ
;
46 memcpy(buf
, sbuf
, slen
);
47 ustream_consume(s
, slen
);
52 static int s_ustream_write(void *ctx
, const unsigned char *buf
, size_t len
)
54 struct ustream
*s
= ctx
;
57 ret
= ustream_write(s
, (const char *) buf
, len
, false);
58 if (ret
< 0 || s
->write_error
)
59 return MBEDTLS_ERR_NET_SEND_FAILED
;
64 __hidden
void ustream_set_io(struct ustream_ssl_ctx
*ctx
, void *ssl
, struct ustream
*conn
)
66 mbedtls_ssl_set_bio(ssl
, conn
, s_ustream_write
, s_ustream_read
, NULL
);
69 static int _random(void *ctx
, unsigned char *out
, size_t len
)
72 if (getrandom(out
, len
, 0) != (ssize_t
) len
)
73 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
;
78 f
= fopen("/dev/urandom", "r");
79 if (fread(out
, len
, 1, f
) != 1)
80 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
;
86 #define AES_GCM_CIPHERS(v) \
87 MBEDTLS_TLS_##v##_WITH_AES_128_GCM_SHA256, \
88 MBEDTLS_TLS_##v##_WITH_AES_256_GCM_SHA384
90 #define AES_CBC_CIPHERS(v) \
91 MBEDTLS_TLS_##v##_WITH_AES_128_CBC_SHA, \
92 MBEDTLS_TLS_##v##_WITH_AES_256_CBC_SHA
94 #define AES_CIPHERS(v) \
98 static const int default_ciphersuites_server
[] =
100 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
,
101 AES_GCM_CIPHERS(ECDHE_ECDSA
),
102 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
,
103 AES_GCM_CIPHERS(ECDHE_RSA
),
104 AES_CBC_CIPHERS(ECDHE_RSA
),
109 static const int default_ciphersuites_client
[] =
111 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
,
112 AES_GCM_CIPHERS(ECDHE_ECDSA
),
113 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
,
114 AES_GCM_CIPHERS(ECDHE_RSA
),
115 MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
,
116 AES_GCM_CIPHERS(DHE_RSA
),
117 AES_CBC_CIPHERS(ECDHE_ECDSA
),
118 AES_CBC_CIPHERS(ECDHE_RSA
),
119 AES_CBC_CIPHERS(DHE_RSA
),
120 /* Removed in Mbed TLS 3.0.0 */
121 #ifdef MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
122 MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
,
125 /* Removed in Mbed TLS 3.0.0 */
126 #ifdef MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA
127 MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA
,
133 __hidden
struct ustream_ssl_ctx
*
134 __ustream_ssl_context_new(bool server
)
136 struct ustream_ssl_ctx
*ctx
;
137 mbedtls_ssl_config
*conf
;
140 #ifdef MBEDTLS_PSA_CRYPTO_C
141 static bool psa_init
;
143 if (!psa_init
&& !psa_crypto_init())
147 ctx
= calloc(1, sizeof(*ctx
));
151 ctx
->server
= server
;
152 mbedtls_pk_init(&ctx
->key
);
153 mbedtls_x509_crt_init(&ctx
->cert
);
154 mbedtls_x509_crt_init(&ctx
->ca_cert
);
156 #if defined(MBEDTLS_SSL_CACHE_C)
157 mbedtls_ssl_cache_init(&ctx
->cache
);
158 mbedtls_ssl_cache_set_timeout(&ctx
->cache
, 30 * 60);
159 mbedtls_ssl_cache_set_max_entries(&ctx
->cache
, 5);
163 mbedtls_ssl_config_init(conf
);
165 ep
= server
? MBEDTLS_SSL_IS_SERVER
: MBEDTLS_SSL_IS_CLIENT
;
167 mbedtls_ssl_config_defaults(conf
, ep
, MBEDTLS_SSL_TRANSPORT_STREAM
,
168 MBEDTLS_SSL_PRESET_DEFAULT
);
169 mbedtls_ssl_conf_rng(conf
, _random
, NULL
);
172 mbedtls_ssl_conf_authmode(conf
, MBEDTLS_SSL_VERIFY_NONE
);
173 mbedtls_ssl_conf_ciphersuites(conf
, default_ciphersuites_server
);
174 mbedtls_ssl_conf_min_version(conf
, MBEDTLS_SSL_MAJOR_VERSION_3
,
175 MBEDTLS_SSL_MINOR_VERSION_3
);
177 mbedtls_ssl_conf_authmode(conf
, MBEDTLS_SSL_VERIFY_OPTIONAL
);
178 mbedtls_ssl_conf_ciphersuites(conf
, default_ciphersuites_client
);
181 #if defined(MBEDTLS_SSL_CACHE_C)
182 mbedtls_ssl_conf_session_cache(conf
, &ctx
->cache
,
183 mbedtls_ssl_cache_get
,
184 mbedtls_ssl_cache_set
);
189 static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx
*ctx
)
191 if (!ctx
->cert
.version
)
194 if (mbedtls_pk_get_type(&ctx
->key
) == MBEDTLS_PK_NONE
)
197 mbedtls_ssl_conf_own_cert(&ctx
->conf
, &ctx
->cert
, &ctx
->key
);
200 __hidden
int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
204 ret
= mbedtls_x509_crt_parse_file(&ctx
->ca_cert
, file
);
208 mbedtls_ssl_conf_ca_chain(&ctx
->conf
, &ctx
->ca_cert
, NULL
);
209 mbedtls_ssl_conf_authmode(&ctx
->conf
, MBEDTLS_SSL_VERIFY_OPTIONAL
);
213 __hidden
int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
217 ret
= mbedtls_x509_crt_parse_file(&ctx
->cert
, file
);
221 ustream_ssl_update_own_cert(ctx
);
225 __hidden
int __ustream_ssl_set_key_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
229 #if (MBEDTLS_VERSION_NUMBER >= 0x03000000)
230 ret
= mbedtls_pk_parse_keyfile(&ctx
->key
, file
, NULL
, _random
, NULL
);
232 ret
= mbedtls_pk_parse_keyfile(&ctx
->key
, file
, NULL
);
237 ustream_ssl_update_own_cert(ctx
);
241 __hidden
int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx
*ctx
, const char *ciphers
)
243 int *ciphersuites
= NULL
, *tmp
, id
;
244 char *cipherstr
, *p
, *last
, c
;
250 cipherstr
= strdup(ciphers
);
252 if (cipherstr
== NULL
)
255 for (p
= cipherstr
, last
= p
;; p
++) {
256 if (*p
== ':' || *p
== 0) {
260 id
= mbedtls_ssl_get_ciphersuite_id(last
);
263 tmp
= realloc(ciphersuites
, (len
+ 2) * sizeof(int));
273 ciphersuites
[len
++] = id
;
274 ciphersuites
[len
] = 0;
284 * mbedTLS expects cipher names with dashes while many sources elsewhere
285 * like the Firefox wiki or Wireshark specify ciphers with underscores,
286 * so simply convert all underscores to dashes to accept both notations.
288 else if (*p
== '_') {
298 mbedtls_ssl_conf_ciphersuites(&ctx
->conf
, ciphersuites
);
299 free(ctx
->ciphersuites
);
301 ctx
->ciphersuites
= ciphersuites
;
306 __hidden
int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx
*ctx
, bool require
)
308 int mode
= MBEDTLS_SSL_VERIFY_OPTIONAL
;
311 mode
= MBEDTLS_SSL_VERIFY_NONE
;
313 mbedtls_ssl_conf_authmode(&ctx
->conf
, mode
);
318 __hidden
void __ustream_ssl_context_free(struct ustream_ssl_ctx
*ctx
)
320 #if defined(MBEDTLS_SSL_CACHE_C)
321 mbedtls_ssl_cache_free(&ctx
->cache
);
323 mbedtls_pk_free(&ctx
->key
);
324 mbedtls_x509_crt_free(&ctx
->ca_cert
);
325 mbedtls_x509_crt_free(&ctx
->cert
);
326 mbedtls_ssl_config_free(&ctx
->conf
);
327 free(ctx
->ciphersuites
);
331 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
334 uloop_timeout_set(&us
->error_timer
, 0);
337 static bool ssl_do_wait(int ret
)
340 case MBEDTLS_ERR_SSL_WANT_READ
:
341 case MBEDTLS_ERR_SSL_WANT_WRITE
:
348 static void ustream_ssl_verify_cert(struct ustream_ssl
*us
)
351 const char *msg
= NULL
;
355 r
= mbedtls_ssl_get_verify_result(ssl
);
356 cn_mismatch
= r
& MBEDTLS_X509_BADCERT_CN_MISMATCH
;
357 r
&= ~MBEDTLS_X509_BADCERT_CN_MISMATCH
;
359 if (r
& MBEDTLS_X509_BADCERT_EXPIRED
)
360 msg
= "certificate has expired";
361 else if (r
& MBEDTLS_X509_BADCERT_REVOKED
)
362 msg
= "certificate has been revoked";
363 else if (r
& MBEDTLS_X509_BADCERT_NOT_TRUSTED
)
364 msg
= "certificate is self-signed or not signed by a trusted CA";
366 msg
= "unknown error";
369 if (us
->notify_verify_error
)
370 us
->notify_verify_error(us
, r
, msg
);
378 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
383 r
= mbedtls_ssl_handshake(ssl
);
385 ustream_ssl_verify_cert(us
);
390 return U_SSL_PENDING
;
392 ustream_ssl_error(us
, r
);
396 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
399 int done
= 0, ret
= 0;
401 while (done
!= len
) {
402 ret
= mbedtls_ssl_write(ssl
, (const unsigned char *) buf
+ done
, len
- done
);
405 if (ssl_do_wait(ret
))
408 ustream_ssl_error(us
, ret
);
418 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
420 int ret
= mbedtls_ssl_read(us
->ssl
, (unsigned char *) buf
, len
);
423 if (ssl_do_wait(ret
))
424 return U_SSL_PENDING
;
426 if (ret
== MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY
)
429 ustream_ssl_error(us
, ret
);
436 __hidden
void *__ustream_ssl_session_new(struct ustream_ssl_ctx
*ctx
)
438 mbedtls_ssl_context
*ssl
;
440 ssl
= calloc(1, sizeof(*ssl
));
444 mbedtls_ssl_init(ssl
);
446 if (mbedtls_ssl_setup(ssl
, &ctx
->conf
)) {
454 __hidden
void __ustream_ssl_session_free(void *ssl
)
456 mbedtls_ssl_free(ssl
);