| 1 |
/* |
| 2 |
* tls-mbed.c - tls layer using mbedTLS |
| 3 |
* |
| 4 |
* Copyright (c) 2018 YOKOTA Hiroshi |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "gauche-tls.h" |
| 35 |
#include <gauche/extend.h> |
| 36 |
|
| 37 |
#if defined(GAUCHE_USE_MBEDTLS) |
| 38 |
|
| 39 |
#include <mbedtls/version.h> |
| 40 |
#include <mbedtls/error.h> |
| 41 |
#include <mbedtls/ssl.h> |
| 42 |
#include <mbedtls/ctr_drbg.h> |
| 43 |
#include <mbedtls/entropy.h> |
| 44 |
#include <mbedtls/net_sockets.h> |
| 45 |
#include <mbedtls/debug.h> |
| 46 |
#include <psa/crypto.h> /* for psa_crypto_init */ |
| 47 |
|
| 48 |
SCM_CLASS_DECL(Scm_MbedTLSClass); |
| 49 |
|
| 50 |
#ifdef HAVE_WINCRYPT_H |
| 51 |
#include <wincrypt.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
/* |
| 55 |
* Class |
| 56 |
*/ |
| 57 |
|
| 58 |
static ScmObj mbed_allocate(ScmClass *klass, ScmObj initargs); |
| 59 |
static void mbedtls_print(ScmObj, ScmPort*, ScmWriteContext*); |
| 60 |
#ifdef MBEDTLS_DEBUG_C |
| 61 |
static void mbed_debug(void*, int, const char*, int, const char *); |
| 62 |
#endif |
| 63 |
|
| 64 |
/* NB: We avoid referring Scm_TLSClass statically, since it is in another |
| 65 |
DSO module and some OS doesn't resolve inter-DSO static data. We |
| 66 |
set the CPA field in init routine. */ |
| 67 |
SCM_DEFINE_BUILTIN_CLASS(Scm_MbedTLSClass, mbedtls_print, NULL, |
| 68 |
NULL, mbed_allocate, NULL); |
| 69 |
|
| 70 |
static ScmObj k_server_name; |
| 71 |
static ScmObj k_skip_verification; |
| 72 |
|
| 73 |
enum MbedState { |
| 74 |
UNCONNECTED, |
| 75 |
CONNECTED, |
| 76 |
BOUND, |
| 77 |
CLOSED |
| 78 |
}; |
| 79 |
|
| 80 |
/* |
| 81 |
* Instance |
| 82 |
*/ |
| 83 |
typedef struct ScmMbedTLSRec { |
| 84 |
ScmTLS common; |
| 85 |
enum MbedState state; |
| 86 |
mbedtls_ssl_context ctx; |
| 87 |
mbedtls_net_context conn; |
| 88 |
mbedtls_entropy_context entropy; |
| 89 |
mbedtls_ctr_drbg_context ctr_drbg; |
| 90 |
mbedtls_ssl_config conf; |
| 91 |
mbedtls_x509_crt ca; |
| 92 |
mbedtls_pk_context pk; |
| 93 |
ScmObj server_name; |
| 94 |
_Bool skip_verification; |
| 95 |
} ScmMbedTLS; |
| 96 |
|
| 97 |
/* |
| 98 |
* 'system ca-bundle support |
| 99 |
*/ |
| 100 |
#include "load_system_cert.c" |
| 101 |
|
| 102 |
#ifdef HAVE_WINCRYPT_H |
| 103 |
static int mem_loader(ScmTLS *t, BYTE *pbCertEncoded, DWORD cbCertEncoded) |
| 104 |
{ |
| 105 |
return mbedtls_x509_crt_parse_der(&((ScmMbedTLS*)t)->ca, |
| 106 |
pbCertEncoded, |
| 107 |
cbCertEncoded); |
| 108 |
} |
| 109 |
|
| 110 |
static ScmObj load_system_cert(ScmMbedTLS *t) |
| 111 |
{ |
| 112 |
return system_cert_loader((ScmTLS*)t, mem_loader); |
| 113 |
} |
| 114 |
#else |
| 115 |
static int file_loader(ScmTLS *t, const char *path) |
| 116 |
{ |
| 117 |
return mbedtls_x509_crt_parse_file(&((ScmMbedTLS*)t)->ca, path); |
| 118 |
} |
| 119 |
|
| 120 |
static ScmObj load_system_cert(ScmMbedTLS *t SCM_UNUSED) { |
| 121 |
return system_cert_loader((ScmTLS*)t, file_loader); |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
/* fmt must contain one %s and one %d. */ |
| 126 |
static void mbed_error(const char *fmt, int errcode) |
| 127 |
{ |
| 128 |
const int bufsiz = 4096; |
| 129 |
char buf[bufsiz]; |
| 130 |
mbedtls_strerror(errcode, buf, bufsiz); |
| 131 |
Scm_Error(fmt, buf, errcode); |
| 132 |
} |
| 133 |
|
| 134 |
static void mbed_context_check(ScmMbedTLS *t SCM_UNUSED, |
| 135 |
const char* op SCM_UNUSED) |
| 136 |
{ |
| 137 |
/* nothing to do (for now) */ |
| 138 |
} |
| 139 |
|
| 140 |
static void mbed_close_check(ScmMbedTLS *t, const char *op) |
| 141 |
{ |
| 142 |
if (t->state != CONNECTED) { |
| 143 |
Scm_Error("attempt to %s unconnected or closed TLS: %S", op, t); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
static ScmObj mbed_connect(ScmTLS *tls, |
| 149 |
const char *host, |
| 150 |
const char *port, |
| 151 |
int proto) |
| 152 |
{ |
| 153 |
ScmMbedTLS* t = (ScmMbedTLS*)tls; |
| 154 |
|
| 155 |
mbed_context_check(t, "connect"); |
| 156 |
const char* pers = "Gauche"; |
| 157 |
int r = mbedtls_ctr_drbg_seed(&t->ctr_drbg, mbedtls_entropy_func, |
| 158 |
&t->entropy, |
| 159 |
(const unsigned char *)pers, strlen(pers)); |
| 160 |
if (r != 0) mbed_error("mbedtls_ctr_drbg_seed() failed: %s (%d)", r); |
| 161 |
|
| 162 |
int mbedtls_proto = MBEDTLS_NET_PROTO_TCP; |
| 163 |
if (proto == SCM_TLS_PROTO_UDP) { |
| 164 |
mbedtls_proto = MBEDTLS_NET_PROTO_UDP; |
| 165 |
} |
| 166 |
|
| 167 |
r = mbedtls_net_connect(&t->conn, host, port, mbedtls_proto); |
| 168 |
if (r != 0) mbed_error("mbedtls_net_connect() failed: %s (%d)", r); |
| 169 |
|
| 170 |
r = mbedtls_ssl_config_defaults(&t->conf, |
| 171 |
MBEDTLS_SSL_IS_CLIENT, |
| 172 |
MBEDTLS_SSL_TRANSPORT_STREAM, |
| 173 |
MBEDTLS_SSL_PRESET_DEFAULT); |
| 174 |
if (r != 0) mbed_error("mbedtls_ssl_config_defaults() failed: %s (%d)", r); |
| 175 |
mbedtls_ssl_conf_rng(&t->conf, mbedtls_ctr_drbg_random, &t->ctr_drbg); |
| 176 |
|
| 177 |
ScmObj ca_bundle_path = SCM_UNDEFINED; |
| 178 |
SCM_BIND_PROC(ca_bundle_path, "tls-ca-bundle-path", |
| 179 |
SCM_FIND_MODULE("rfc.tls", 0)); |
| 180 |
ScmObj s_ca_file = Scm_ApplyRec0(ca_bundle_path); |
| 181 |
if (SCM_FALSEP(s_ca_file)) { |
| 182 |
Scm_Error("mbedTLS: tls-ca-bundle-path isn't set. It is required to" |
| 183 |
" validate server certs."); |
| 184 |
} else if(Scm_EqP(s_ca_file, SCM_INTERN(GAUCHE_CA_SYSTEM))) { |
| 185 |
if(SCM_FALSEP(load_system_cert(t))) { |
| 186 |
Scm_Error("Can't load certificates from system certificate store"); |
| 187 |
} |
| 188 |
} else if(SCM_STRINGP(s_ca_file)) { |
| 189 |
const char *ca_file = Scm_GetStringConst(SCM_STRING(s_ca_file)); |
| 190 |
int r = mbedtls_x509_crt_parse_file(&t->ca, ca_file); |
| 191 |
if (r != 0) { |
| 192 |
const int bufsiz = 4096; |
| 193 |
char buf[bufsiz]; |
| 194 |
mbedtls_strerror(r, buf, bufsiz); |
| 195 |
Scm_Error("mbedtls_x509_crt_parse_file() failed on %S: %s (%d)", |
| 196 |
s_ca_file, buf, r); |
| 197 |
} |
| 198 |
} else { |
| 199 |
Scm_Error("Parameter tls-ca-bundle-path must have a string value " |
| 200 |
"or 'system, but got: %S", s_ca_file); |
| 201 |
} |
| 202 |
|
| 203 |
mbedtls_ssl_conf_ca_chain(&t->conf, &t->ca, NULL); |
| 204 |
if (t->skip_verification) { |
| 205 |
mbedtls_ssl_conf_authmode(&t->conf, MBEDTLS_SSL_VERIFY_NONE); |
| 206 |
} else { |
| 207 |
mbedtls_ssl_conf_authmode(&t->conf, MBEDTLS_SSL_VERIFY_REQUIRED); |
| 208 |
} |
| 209 |
|
| 210 |
r = mbedtls_ssl_setup(&t->ctx, &t->conf); |
| 211 |
if (r != 0) mbed_error("mbedtls_ssl_setup() failed: %s (%d)", r); |
| 212 |
|
| 213 |
const char* hostname = (SCM_STRINGP(t->server_name) |
| 214 |
? Scm_GetStringConst(SCM_STRING(t->server_name)) |
| 215 |
: NULL); |
| 216 |
r = mbedtls_ssl_set_hostname(&t->ctx, hostname); |
| 217 |
if (r != 0) mbed_error("mbedtls_ssl_set_hostname() failed: %s (%d)", r); |
| 218 |
|
| 219 |
mbedtls_ssl_set_bio(&t->ctx, &t->conn, mbedtls_net_send, mbedtls_net_recv, NULL); |
| 220 |
|
| 221 |
r = mbedtls_ssl_handshake(&t->ctx); |
| 222 |
if (r != 0) mbed_error("TLS handshake failed: %s (%d)", r); |
| 223 |
|
| 224 |
t->state = CONNECTED; |
| 225 |
return SCM_OBJ(t); |
| 226 |
} |
| 227 |
|
| 228 |
static ScmObj mbed_bind(ScmTLS *tls, |
| 229 |
const char *ip, |
| 230 |
const char *port, /* numeric or service name */ |
| 231 |
int proto) |
| 232 |
{ |
| 233 |
SCM_ASSERT(SCM_XTYPEP(tls, &Scm_MbedTLSClass)); |
| 234 |
ScmMbedTLS *t = (ScmMbedTLS*)tls; |
| 235 |
if (t->state != UNCONNECTED) { |
| 236 |
Scm_Error("TLS already bound or connected: %S", SCM_OBJ(tls)); |
| 237 |
} |
| 238 |
|
| 239 |
int mbedtls_proto = MBEDTLS_NET_PROTO_TCP; |
| 240 |
if (proto == SCM_TLS_PROTO_UDP) { |
| 241 |
mbedtls_proto = MBEDTLS_NET_PROTO_UDP; |
| 242 |
} |
| 243 |
|
| 244 |
int r = mbedtls_net_bind(&t->conn, ip, port, mbedtls_proto); |
| 245 |
if (r != 0) { |
| 246 |
mbed_error("mbedtls_net_bind() failed: %s (%d)", r); |
| 247 |
} |
| 248 |
|
| 249 |
const char* pers = "Gauche"; |
| 250 |
r = mbedtls_ctr_drbg_seed(&t->ctr_drbg, mbedtls_entropy_func, |
| 251 |
&t->entropy, |
| 252 |
(const unsigned char *)pers, strlen(pers)); |
| 253 |
if (r != 0) { |
| 254 |
mbed_error("mbedtls_ctr_drbg_seed() failed: %s (%d)", r); |
| 255 |
} |
| 256 |
|
| 257 |
r = mbedtls_ssl_config_defaults(&t->conf, |
| 258 |
MBEDTLS_SSL_IS_SERVER, |
| 259 |
MBEDTLS_SSL_TRANSPORT_STREAM, |
| 260 |
MBEDTLS_SSL_PRESET_DEFAULT); |
| 261 |
if (r != 0) { |
| 262 |
mbed_error("mbedtls_ssl_config_defaults() failed: %s (%d)", r); |
| 263 |
} |
| 264 |
|
| 265 |
mbedtls_ssl_conf_rng(&t->conf, mbedtls_ctr_drbg_random, &t->ctr_drbg); |
| 266 |
|
| 267 |
r = mbedtls_ssl_conf_own_cert(&t->conf, &t->ca, &t->pk); |
| 268 |
if (r != 0) { |
| 269 |
mbed_error("mbedtls_ssl_confown_cert() failed: %s (%d)", r); |
| 270 |
} |
| 271 |
|
| 272 |
t->state = BOUND; |
| 273 |
return SCM_OBJ(t); |
| 274 |
} |
| 275 |
|
| 276 |
static ScmObj mbed_accept(ScmTLS *tls) /* tls must already be bound */ |
| 277 |
{ |
| 278 |
SCM_ASSERT(SCM_XTYPEP(tls, &Scm_MbedTLSClass)); |
| 279 |
ScmMbedTLS *servt = (ScmMbedTLS*)tls; |
| 280 |
ScmMbedTLS *t = (ScmMbedTLS*)mbed_allocate(Scm_ClassOf(SCM_OBJ(tls)), |
| 281 |
SCM_NIL); |
| 282 |
if (servt->state != BOUND) { |
| 283 |
Scm_Error("TLS is not bound: %S", SCM_OBJ(tls)); |
| 284 |
} |
| 285 |
|
| 286 |
int r = mbedtls_ssl_setup(&t->ctx, &servt->conf); |
| 287 |
if (r != 0) { |
| 288 |
mbed_error("mbedtls_ssl_setup() failed: %s (%d)", r); |
| 289 |
} |
| 290 |
|
| 291 |
/* TODO: Take client address info and save it to newt. */ |
| 292 |
r = mbedtls_net_accept(&servt->conn, &t->conn, NULL, 0, NULL); |
| 293 |
if (r != 0) { |
| 294 |
mbed_error("mbedtls_net_accept() failed: %s (%d)", r); |
| 295 |
} |
| 296 |
|
| 297 |
mbedtls_ssl_set_bio(&t->ctx, &t->conn, |
| 298 |
mbedtls_net_send, mbedtls_net_recv, NULL); |
| 299 |
|
| 300 |
r = mbedtls_ssl_handshake(&t->ctx); |
| 301 |
if (r != 0) { |
| 302 |
mbed_error("TLS handshake failed: %s (%d)", r); |
| 303 |
} |
| 304 |
t->state = CONNECTED; |
| 305 |
return SCM_OBJ(t); |
| 306 |
} |
| 307 |
|
| 308 |
static ScmObj mbed_read(ScmTLS *tls) |
| 309 |
{ |
| 310 |
ScmMbedTLS *t = (ScmMbedTLS*)tls; |
| 311 |
mbed_context_check(t, "read"); |
| 312 |
mbed_close_check(t, "read"); |
| 313 |
uint8_t buf[1024] = {}; |
| 314 |
|
| 315 |
int nread = 0; |
| 316 |
do { |
| 317 |
int r = mbedtls_ssl_read(&t->ctx, buf+nread, sizeof(buf)); |
| 318 |
if (r == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) return SCM_EOF; |
| 319 |
if (r == MBEDTLS_ERR_SSL_WANT_READ) continue; |
| 320 |
#if defined(MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) |
| 321 |
if (r == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) continue; |
| 322 |
#endif |
| 323 |
if (r < 0) mbed_error("mbedtls_ssl_read() failed: %s (%d)", r); |
| 324 |
nread += r; |
| 325 |
} while (nread == 0); |
| 326 |
|
| 327 |
return Scm_MakeString((char *)buf, nread, nread, |
| 328 |
SCM_STRING_INCOMPLETE | SCM_STRING_COPYING); |
| 329 |
} |
| 330 |
|
| 331 |
static ScmObj mbed_write(ScmTLS *tls, ScmObj msg) |
| 332 |
{ |
| 333 |
ScmMbedTLS *t = (ScmMbedTLS*)tls; |
| 334 |
mbed_context_check(t, "write"); |
| 335 |
mbed_close_check(t, "write"); |
| 336 |
|
| 337 |
ScmSize size; |
| 338 |
const uint8_t* cmsg = Scm_GetBytes(msg, &size); |
| 339 |
|
| 340 |
if (cmsg == NULL) { |
| 341 |
Scm_TypeError("TLS message", "uniform vector or string", msg); |
| 342 |
} |
| 343 |
int nsent = 0; |
| 344 |
do { |
| 345 |
int r = mbedtls_ssl_write(&t->ctx, cmsg+nsent, size-nsent); |
| 346 |
if (r == MBEDTLS_ERR_SSL_WANT_WRITE) continue; |
| 347 |
#if defined(MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) |
| 348 |
if (r == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) continue; |
| 349 |
#endif |
| 350 |
if (r < 0) mbed_error("mbedtls_ssl_write() failed: %s (%d)", r); |
| 351 |
nsent += r; |
| 352 |
} while (nsent < size); |
| 353 |
return SCM_MAKE_INT(nsent); |
| 354 |
} |
| 355 |
|
| 356 |
static u_long mbed_poll(ScmTLS *tls, u_long rwflags, ScmTimeSpec *timeout) |
| 357 |
{ |
| 358 |
ScmMbedTLS *t = (ScmMbedTLS*)tls; |
| 359 |
if (t->state != CONNECTED && t->state != BOUND) return 0; |
| 360 |
uint32_t rw = 0; |
| 361 |
if (rwflags & TLS_POLL_READ) rw |= MBEDTLS_NET_POLL_READ; |
| 362 |
if (rwflags & TLS_POLL_WRITE) rw |= MBEDTLS_NET_POLL_WRITE; |
| 363 |
|
| 364 |
uint32_t timeout_delay; /* mbed takes ms from now */ |
| 365 |
if (timeout == NULL) { |
| 366 |
timeout_delay = (uint32_t)-1; /* wait indefinitely */ |
| 367 |
} else { |
| 368 |
ScmTime *now = SCM_TIME(Scm_CurrentTime()); |
| 369 |
long ds = timeout->tv_sec - now->sec; |
| 370 |
long dns = timeout->tv_nsec - now->nsec; |
| 371 |
long dms = ds * 1000 + dns / 1000000; |
| 372 |
if (dms <= 0) timeout_delay = 0; |
| 373 |
else timeout_delay = (uint32_t)dms; |
| 374 |
} |
| 375 |
|
| 376 |
int r = mbedtls_net_poll(&t->conn, rw, timeout_delay); |
| 377 |
if (r < 0) { |
| 378 |
mbed_error("mbedtls_net_poll failed: %s (%d)", r); |
| 379 |
} |
| 380 |
u_long result = 0; |
| 381 |
if (r & MBEDTLS_NET_POLL_READ) result |= TLS_POLL_READ; |
| 382 |
if (r & MBEDTLS_NET_POLL_WRITE) result |= TLS_POLL_WRITE; |
| 383 |
return result; |
| 384 |
} |
| 385 |
|
| 386 |
/* Cleanup is called to release resources other than Scheme managed ones. |
| 387 |
This can be called from the finalizer, so any Scheme-allocated object |
| 388 |
might have been collected. */ |
| 389 |
static void mbed_cleanup(ScmMbedTLS *t) |
| 390 |
{ |
| 391 |
if (t->state == CLOSED) return; |
| 392 |
|
| 393 |
t->state = CLOSED; |
| 394 |
t->server_name = NULL; |
| 395 |
t->common.in_port = t->common.out_port = SCM_UNDEFINED; |
| 396 |
|
| 397 |
/* MbedTLS is sensitive about the order of cleanup. we haven't |
| 398 |
quite figured it out, but delaying net_free is apparently a bad |
| 399 |
idea. Also, ssl_context should be cleaned up before ssl_config. */ |
| 400 |
mbedtls_ssl_close_notify(&t->ctx); |
| 401 |
mbedtls_net_free(&t->conn); |
| 402 |
mbedtls_entropy_free(&t->entropy); |
| 403 |
mbedtls_pk_free(&t->pk); |
| 404 |
mbedtls_x509_crt_free(&t->ca); |
| 405 |
mbedtls_ssl_free(&t->ctx); |
| 406 |
mbedtls_ctr_drbg_free(&t->ctr_drbg); |
| 407 |
mbedtls_ssl_config_free(&t->conf); |
| 408 |
} |
| 409 |
|
| 410 |
static ScmObj mbed_close(ScmTLS *tls) |
| 411 |
{ |
| 412 |
mbed_cleanup((ScmMbedTLS*)tls); |
| 413 |
return SCM_TRUE; |
| 414 |
} |
| 415 |
|
| 416 |
static void mbed_finalize(ScmObj obj, void *data SCM_UNUSED) |
| 417 |
{ |
| 418 |
mbed_cleanup((ScmMbedTLS*)obj); |
| 419 |
} |
| 420 |
|
| 421 |
static ScmObj mbed_load_certificate(ScmTLS *tls, |
| 422 |
const char *filename) |
| 423 |
{ |
| 424 |
ScmMbedTLS *t = (ScmMbedTLS*)tls; |
| 425 |
int r = mbedtls_x509_crt_parse_file(&t->ca, filename); |
| 426 |
if (r != 0) { |
| 427 |
const int bufsiz = 4096; |
| 428 |
char buf[bufsiz]; |
| 429 |
mbedtls_strerror(r, buf, bufsiz); |
| 430 |
Scm_Error("Couldn't load certificate %s: %s (%d)", |
| 431 |
filename, buf, r); |
| 432 |
} |
| 433 |
return SCM_OBJ(tls); |
| 434 |
} |
| 435 |
|
| 436 |
#if MBEDTLS_VERSION_MAJOR >= 3 |
| 437 |
static int rng_get(void *prng, unsigned char *output, size_t output_len) |
| 438 |
{ |
| 439 |
mbedtls_ctr_drbg_context *rng = prng; |
| 440 |
return mbedtls_ctr_drbg_random(rng, output, output_len); |
| 441 |
} |
| 442 |
#endif /*MBEDTLS_VERSION_MAJOR >= 3*/ |
| 443 |
|
| 444 |
static ScmObj mbed_load_private_key(ScmTLS *tls, |
| 445 |
const char *filename, |
| 446 |
const char *password) |
| 447 |
{ |
| 448 |
ScmMbedTLS *t = (ScmMbedTLS*)tls; |
| 449 |
#if MBEDTLS_VERSION_MAJOR < 3 |
| 450 |
int r = mbedtls_pk_parse_keyfile(&t->pk, filename, password); |
| 451 |
#else /*MBEDTLS_VERSION_MAJOR >= 3*/ |
| 452 |
mbedtls_ctr_drbg_context rng; |
| 453 |
mbedtls_ctr_drbg_init(&rng); |
| 454 |
int r = mbedtls_pk_parse_keyfile(&t->pk, filename, password, |
| 455 |
rng_get, &rng); |
| 456 |
#endif /*MBEDTLS_VERSION_MAJOR >= 3*/ |
| 457 |
if (r != 0) { |
| 458 |
const int bufsiz = 4096; |
| 459 |
char buf[bufsiz]; |
| 460 |
mbedtls_strerror(r, buf, bufsiz); |
| 461 |
Scm_Error("Couldn't load certificate %s: %s (%d)", |
| 462 |
filename, buf, r); |
| 463 |
} |
| 464 |
return SCM_OBJ(tls); |
| 465 |
} |
| 466 |
|
| 467 |
/* |
| 468 |
* Set debug level. 0 <= level < 10 |
| 469 |
* mbedtls uses 0 (No debug) to 4 (Verbose) |
| 470 |
*/ |
| 471 |
static ScmObj mbed_set_debug_level(ScmObj *args, int nargs, |
| 472 |
void *data SCM_UNUSED) |
| 473 |
{ |
| 474 |
SCM_ASSERT(nargs == 1); |
| 475 |
if (!SCM_INTP(args[0])) Scm_Error("fixnum required, but got: %S", args[0]); |
| 476 |
int level = (int)SCM_INT_VALUE(args[0]); |
| 477 |
int mlevel = (level+1)/2; |
| 478 |
if (mlevel > 4) mlevel = 4; |
| 479 |
mbedtls_debug_set_threshold(mlevel); |
| 480 |
return SCM_UNDEFINED; |
| 481 |
} |
| 482 |
|
| 483 |
/* |
| 484 |
* Get connection info. MbedTLS once tried to make 'fd' hidden, |
| 485 |
* but it was subsequently reverted. We abstract it just in case |
| 486 |
* MbedTLS changes it again. |
| 487 |
*/ |
| 488 |
|
| 489 |
static int get_connection_fd(ScmMbedTLS *t) |
| 490 |
{ |
| 491 |
return t->conn.fd; |
| 492 |
} |
| 493 |
|
| 494 |
static ScmObj mbed_connection_address(ScmTLS *tls, int who) |
| 495 |
{ |
| 496 |
ScmMbedTLS *t = (ScmMbedTLS*)tls; |
| 497 |
switch (who) { |
| 498 |
case TLS_SELF_ADDRESS: |
| 499 |
if (t->state != CONNECTED && t->state != BOUND) return SCM_FALSE; |
| 500 |
return Scm_GetSockName(get_connection_fd(t)); |
| 501 |
case TLS_PEER_ADDRESS: |
| 502 |
if (t->state != CONNECTED) return SCM_FALSE; |
| 503 |
return Scm_GetPeerName(get_connection_fd(t)); |
| 504 |
default: |
| 505 |
return SCM_FALSE; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
static void mbedtls_print(ScmObj obj, ScmPort* port, |
| 510 |
ScmWriteContext* ctx SCM_UNUSED) |
| 511 |
{ |
| 512 |
ScmMbedTLS *t = (ScmMbedTLS*)obj; |
| 513 |
|
| 514 |
Scm_Printf(port, "#<%A", Scm_ShortClassName(SCM_CLASS_OF(obj))); |
| 515 |
if (t->server_name) { |
| 516 |
Scm_Printf(port, " %S", t->server_name); |
| 517 |
} |
| 518 |
switch (t->state) { |
| 519 |
case UNCONNECTED: Scm_Printf(port, " (unconnected)"); break; |
| 520 |
case CONNECTED: Scm_Printf(port, " (connected)"); break; |
| 521 |
case BOUND: Scm_Printf(port, " (bound)"); break; |
| 522 |
case CLOSED: Scm_Printf(port, " (closed)"); break; |
| 523 |
} |
| 524 |
Scm_Printf(port, ">"); |
| 525 |
} |
| 526 |
|
| 527 |
#ifdef MBEDTLS_DEBUG_C |
| 528 |
void mbed_debug(void *ctx, int level SCM_UNUSED, |
| 529 |
const char *file, int line, const char *str) |
| 530 |
{ |
| 531 |
fprintf((FILE*)ctx, "%s:%d: %s", file, line, str); |
| 532 |
fflush((FILE*)ctx); |
| 533 |
} |
| 534 |
#endif /*MBEDTLS_DEBUG_C*/ |
| 535 |
|
| 536 |
static ScmObj mbed_allocate(ScmClass *klass, ScmObj initargs) |
| 537 |
{ |
| 538 |
ScmMbedTLS* t = SCM_NEW_INSTANCE(ScmMbedTLS, klass); |
| 539 |
|
| 540 |
ScmObj server_name = Scm_GetKeyword(k_server_name, initargs, SCM_FALSE); |
| 541 |
if (!SCM_STRINGP(server_name) && !SCM_FALSEP(server_name)) { |
| 542 |
Scm_TypeError("mbed-tls server-name", "string or #f", server_name); |
| 543 |
} |
| 544 |
|
| 545 |
t->state = UNCONNECTED; |
| 546 |
mbedtls_ssl_config_init(&t->conf); |
| 547 |
mbedtls_ctr_drbg_init(&t->ctr_drbg); |
| 548 |
mbedtls_net_init(&t->conn); |
| 549 |
mbedtls_ssl_init(&t->ctx); |
| 550 |
mbedtls_x509_crt_init(&t->ca); |
| 551 |
mbedtls_pk_init(&t->pk); |
| 552 |
mbedtls_entropy_init(&t->entropy); |
| 553 |
|
| 554 |
#ifdef MBEDTLS_DEBUG_C |
| 555 |
mbedtls_ssl_conf_dbg(&t->conf, mbed_debug, stderr); |
| 556 |
#endif |
| 557 |
|
| 558 |
t->server_name = server_name; |
| 559 |
t->skip_verification = |
| 560 |
SCM_BOOL_VALUE(Scm_GetKeyword(k_skip_verification, initargs, SCM_FALSE)); |
| 561 |
t->common.in_port = t->common.out_port = SCM_UNDEFINED; |
| 562 |
|
| 563 |
t->common.connect = mbed_connect; |
| 564 |
t->common.bind = mbed_bind; |
| 565 |
t->common.accept = mbed_accept; |
| 566 |
t->common.read = mbed_read; |
| 567 |
t->common.write = mbed_write; |
| 568 |
t->common.poll = mbed_poll; |
| 569 |
t->common.close = mbed_close; |
| 570 |
t->common.loadCertificate = mbed_load_certificate; |
| 571 |
t->common.loadPrivateKey = mbed_load_private_key; |
| 572 |
t->common.getConnectionAddress = mbed_connection_address; |
| 573 |
t->common.finalize = mbed_finalize; |
| 574 |
Scm_RegisterFinalizer(SCM_OBJ(t), mbed_finalize, NULL); |
| 575 |
return SCM_OBJ(t); |
| 576 |
} |
| 577 |
|
| 578 |
#endif /* defined(GAUCHE_USE_MBEDTLS) */ |
| 579 |
|
| 580 |
#ifdef MBEDTLS_THREADING_ALT |
| 581 |
/* Alternative mutex configuration for Windows/MinGW. |
| 582 |
See mbedtls/threading.h in the mbedtls source tree for the details. |
| 583 |
On POSIX platform, we use MBEDTLS_THREADING_PTHREADS so these are |
| 584 |
not used. |
| 585 |
These must be in sync with tools/tls/threading_alt.h. tools/tls/Makefile |
| 586 |
takes care of putting that file in the source tree. */ |
| 587 |
static void win_mutex_init(mbedtls_threading_mutex_t *m) |
| 588 |
{ |
| 589 |
m->mutex = (void*)Scm__WinCreateMutex(); |
| 590 |
} |
| 591 |
|
| 592 |
static void win_mutex_free(mbedtls_threading_mutex_t *m) |
| 593 |
{ |
| 594 |
if ((HANDLE)m->mutex != INVALID_HANDLE_VALUE) { |
| 595 |
CloseHandle((HANDLE)m->mutex); |
| 596 |
m->mutex = (void*)INVALID_HANDLE_VALUE; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
static int win_mutex_lock(mbedtls_threading_mutex_t *m) |
| 601 |
{ |
| 602 |
if ((HANDLE)m->mutex != INVALID_HANDLE_VALUE) { |
| 603 |
Scm__WinMutexLock((HANDLE)m->mutex); |
| 604 |
return 0; |
| 605 |
} else { |
| 606 |
return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
static int win_mutex_unlock(mbedtls_threading_mutex_t *m) |
| 611 |
{ |
| 612 |
if ((HANDLE)m->mutex != INVALID_HANDLE_VALUE) { |
| 613 |
ReleaseMutex((HANDLE)m->mutex); |
| 614 |
return 0; |
| 615 |
} else { |
| 616 |
return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
| 617 |
} |
| 618 |
} |
| 619 |
#endif //MBEDTLS_THREADING_ALT |
| 620 |
|
| 621 |
/* |
| 622 |
* Initialization |
| 623 |
*/ |
| 624 |
|
| 625 |
void Scm_Init_rfc__tls__mbed() |
| 626 |
{ |
| 627 |
ScmModule *mod = SCM_MODULE(SCM_FIND_MODULE("rfc.tls.mbed", 0)); |
| 628 |
#if defined(GAUCHE_USE_MBEDTLS) |
| 629 |
ScmClass **cpa = SCM_NEW_ARRAY(ScmClass*, 4); |
| 630 |
cpa[0] = (ScmClass*)Scm_GlobalVariableRef(SCM_MODULE(SCM_FIND_MODULE("rfc.tls", 0)), |
| 631 |
SCM_SYMBOL(SCM_INTERN("<tls>")), |
| 632 |
0); |
| 633 |
cpa[1] = SCM_CLASS_CONNECTION; |
| 634 |
cpa[2] = SCM_CLASS_TOP; |
| 635 |
cpa[3] = NULL; |
| 636 |
Scm_MbedTLSClass.cpa = cpa; |
| 637 |
Scm_InitStaticClass(&Scm_MbedTLSClass, "<mbed-tls>", mod, NULL, 0); |
| 638 |
k_server_name = SCM_MAKE_KEYWORD("server-name"); |
| 639 |
k_skip_verification = SCM_MAKE_KEYWORD("skip-verification"); |
| 640 |
|
| 641 |
ScmObj set_debug_level = Scm_MakeSubr(mbed_set_debug_level, NULL, 1, 0, |
| 642 |
SCM_FALSE); |
| 643 |
Scm_Define(mod, SCM_SYMBOL(SCM_INTERN("mbed-set-debug-level!")), |
| 644 |
set_debug_level); |
| 645 |
|
| 646 |
# ifdef MBEDTLS_THREADING_ALT |
| 647 |
mbedtls_threading_set_alt(win_mutex_init, |
| 648 |
win_mutex_free, |
| 649 |
win_mutex_lock, |
| 650 |
win_mutex_unlock); |
| 651 |
# endif //MBEDTLS_THREADING_ALT |
| 652 |
|
| 653 |
/* We need this before the first TLS handshake. |
| 654 |
Cf. https://github.com/Mbed-TLS/mbedtls/issues/8401 */ |
| 655 |
psa_crypto_init(); |
| 656 |
|
| 657 |
#else |
| 658 |
/* insert dummy binding */ |
| 659 |
SCM_DEFINE(mod, "<mbed-tls>", SCM_FALSE); |
| 660 |
#endif /* defined(GAUCHE_USE_MBEDTLS) */ |
| 661 |
} |