1 /* $OpenBSD: tls.c,v 1.38 2016/05/27 14:38:40 jsing Exp $ */ 2 /* 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/socket.h> 19 20 #include <errno.h> 21 #include <limits.h> 22 #include <stdlib.h> 23 #include <unistd.h> 24 25 #include <openssl/bio.h> 26 #include <openssl/err.h> 27 #include <openssl/evp.h> 28 #include <openssl/pem.h> 29 #include <openssl/x509.h> 30 31 #include <tls.h> 32 #include "tls_internal.h" 33 34 static struct tls_config *tls_config_default; 35 36 int 37 tls_init(void) 38 { 39 static int tls_initialised = 0; 40 41 if (tls_initialised) 42 return (0); 43 44 SSL_load_error_strings(); 45 SSL_library_init(); 46 47 if (BIO_sock_init() != 1) 48 return (-1); 49 50 if ((tls_config_default = tls_config_new()) == NULL) 51 return (-1); 52 53 tls_initialised = 1; 54 55 return (0); 56 } 57 58 const char * 59 tls_error(struct tls *ctx) 60 { 61 return ctx->error.msg; 62 } 63 64 static int 65 tls_error_vset(struct tls_error *error, int errnum, const char *fmt, va_list ap) 66 { 67 char *errmsg = NULL; 68 int rv = -1; 69 70 free(error->msg); 71 error->msg = NULL; 72 error->num = errnum; 73 74 if (vasprintf(&errmsg, fmt, ap) == -1) { 75 errmsg = NULL; 76 goto err; 77 } 78 79 if (errnum == -1) { 80 error->msg = errmsg; 81 return (0); 82 } 83 84 if (asprintf(&error->msg, "%s: %s", errmsg, strerror(errnum)) == -1) { 85 error->msg = NULL; 86 goto err; 87 } 88 rv = 0; 89 90 err: 91 free(errmsg); 92 93 return (rv); 94 } 95 96 int 97 tls_error_set(struct tls_error *error, const char *fmt, ...) 98 { 99 va_list ap; 100 int errnum, rv; 101 102 errnum = errno; 103 104 va_start(ap, fmt); 105 rv = tls_error_vset(error, errnum, fmt, ap); 106 va_end(ap); 107 108 return (rv); 109 } 110 111 int 112 tls_error_setx(struct tls_error *error, const char *fmt, ...) 113 { 114 va_list ap; 115 int rv; 116 117 va_start(ap, fmt); 118 rv = tls_error_vset(error, -1, fmt, ap); 119 va_end(ap); 120 121 return (rv); 122 } 123 124 int 125 tls_config_set_error(struct tls_config *config, const char *fmt, ...) 126 { 127 va_list ap; 128 int errnum, rv; 129 130 errnum = errno; 131 132 va_start(ap, fmt); 133 rv = tls_error_vset(&config->error, errnum, fmt, ap); 134 va_end(ap); 135 136 return (rv); 137 } 138 139 int 140 tls_config_set_errorx(struct tls_config *config, const char *fmt, ...) 141 { 142 va_list ap; 143 int rv; 144 145 va_start(ap, fmt); 146 rv = tls_error_vset(&config->error, -1, fmt, ap); 147 va_end(ap); 148 149 return (rv); 150 } 151 152 int 153 tls_set_error(struct tls *ctx, const char *fmt, ...) 154 { 155 va_list ap; 156 int errnum, rv; 157 158 errnum = errno; 159 160 va_start(ap, fmt); 161 rv = tls_error_vset(&ctx->error, errnum, fmt, ap); 162 va_end(ap); 163 164 return (rv); 165 } 166 167 int 168 tls_set_errorx(struct tls *ctx, const char *fmt, ...) 169 { 170 va_list ap; 171 int rv; 172 173 va_start(ap, fmt); 174 rv = tls_error_vset(&ctx->error, -1, fmt, ap); 175 va_end(ap); 176 177 return (rv); 178 } 179 180 struct tls * 181 tls_new(void) 182 { 183 struct tls *ctx; 184 185 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) 186 return (NULL); 187 188 ctx->config = tls_config_default; 189 190 tls_reset(ctx); 191 192 return (ctx); 193 } 194 195 int 196 tls_configure(struct tls *ctx, struct tls_config *config) 197 { 198 if (config == NULL) 199 config = tls_config_default; 200 201 ctx->config = config; 202 203 if ((ctx->flags & TLS_SERVER) != 0) 204 return (tls_configure_server(ctx)); 205 206 return (0); 207 } 208 209 int 210 tls_configure_keypair(struct tls *ctx, SSL_CTX *ssl_ctx, 211 struct tls_keypair *keypair, int required) 212 { 213 EVP_PKEY *pkey = NULL; 214 X509 *cert = NULL; 215 BIO *bio = NULL; 216 217 if (!required && 218 keypair->cert_mem == NULL && 219 keypair->key_mem == NULL && 220 keypair->cert_file == NULL && 221 keypair->key_file == NULL) 222 return(0); 223 224 if (keypair->cert_mem != NULL) { 225 if (keypair->cert_len > INT_MAX) { 226 tls_set_errorx(ctx, "certificate too long"); 227 goto err; 228 } 229 230 if (SSL_CTX_use_certificate_chain_mem(ssl_ctx, 231 keypair->cert_mem, keypair->cert_len) != 1) { 232 tls_set_errorx(ctx, "failed to load certificate"); 233 goto err; 234 } 235 cert = NULL; 236 } 237 if (keypair->key_mem != NULL) { 238 if (keypair->key_len > INT_MAX) { 239 tls_set_errorx(ctx, "key too long"); 240 goto err; 241 } 242 243 if ((bio = BIO_new_mem_buf(keypair->key_mem, 244 keypair->key_len)) == NULL) { 245 tls_set_errorx(ctx, "failed to create buffer"); 246 goto err; 247 } 248 if ((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, 249 NULL)) == NULL) { 250 tls_set_errorx(ctx, "failed to read private key"); 251 goto err; 252 } 253 if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1) { 254 tls_set_errorx(ctx, "failed to load private key"); 255 goto err; 256 } 257 BIO_free(bio); 258 bio = NULL; 259 EVP_PKEY_free(pkey); 260 pkey = NULL; 261 } 262 263 if (keypair->cert_file != NULL) { 264 if (SSL_CTX_use_certificate_chain_file(ssl_ctx, 265 keypair->cert_file) != 1) { 266 tls_set_errorx(ctx, "failed to load certificate file"); 267 goto err; 268 } 269 } 270 if (keypair->key_file != NULL) { 271 if (SSL_CTX_use_PrivateKey_file(ssl_ctx, 272 keypair->key_file, SSL_FILETYPE_PEM) != 1) { 273 tls_set_errorx(ctx, "failed to load private key file"); 274 goto err; 275 } 276 } 277 278 if (SSL_CTX_check_private_key(ssl_ctx) != 1) { 279 tls_set_errorx(ctx, "private/public key mismatch"); 280 goto err; 281 } 282 283 return (0); 284 285 err: 286 EVP_PKEY_free(pkey); 287 X509_free(cert); 288 BIO_free(bio); 289 290 return (1); 291 } 292 293 int 294 tls_configure_ssl(struct tls *ctx) 295 { 296 SSL_CTX_set_mode(ctx->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE); 297 SSL_CTX_set_mode(ctx->ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); 298 299 SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv2); 300 SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv3); 301 302 SSL_CTX_clear_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1); 303 SSL_CTX_clear_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1_1); 304 SSL_CTX_clear_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1_2); 305 306 if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_0) == 0) 307 SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1); 308 if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_1) == 0) 309 SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1_1); 310 if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_2) == 0) 311 SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1_2); 312 313 if (ctx->config->ciphers != NULL) { 314 if (SSL_CTX_set_cipher_list(ctx->ssl_ctx, 315 ctx->config->ciphers) != 1) { 316 tls_set_errorx(ctx, "failed to set ciphers"); 317 goto err; 318 } 319 } 320 321 if (ctx->config->verify_time == 0) { 322 X509_VERIFY_PARAM_set_flags(ctx->ssl_ctx->param, 323 X509_V_FLAG_NO_CHECK_TIME); 324 } 325 326 return (0); 327 328 err: 329 return (-1); 330 } 331 332 int 333 tls_configure_ssl_verify(struct tls *ctx, int verify) 334 { 335 SSL_CTX_set_verify(ctx->ssl_ctx, verify, NULL); 336 337 if (ctx->config->ca_mem != NULL) { 338 /* XXX do this in set. */ 339 if (ctx->config->ca_len > INT_MAX) { 340 tls_set_errorx(ctx, "ca too long"); 341 goto err; 342 } 343 if (SSL_CTX_load_verify_mem(ctx->ssl_ctx, 344 ctx->config->ca_mem, ctx->config->ca_len) != 1) { 345 tls_set_errorx(ctx, "ssl verify memory setup failure"); 346 goto err; 347 } 348 } else if (SSL_CTX_load_verify_locations(ctx->ssl_ctx, 349 ctx->config->ca_file, ctx->config->ca_path) != 1) { 350 tls_set_errorx(ctx, "ssl verify setup failure"); 351 goto err; 352 } 353 if (ctx->config->verify_depth >= 0) 354 SSL_CTX_set_verify_depth(ctx->ssl_ctx, 355 ctx->config->verify_depth); 356 357 return (0); 358 359 err: 360 return (-1); 361 } 362 363 void 364 tls_free(struct tls *ctx) 365 { 366 if (ctx == NULL) 367 return; 368 tls_reset(ctx); 369 free(ctx); 370 } 371 372 void 373 tls_reset(struct tls *ctx) 374 { 375 SSL_CTX_free(ctx->ssl_ctx); 376 SSL_free(ctx->ssl_conn); 377 X509_free(ctx->ssl_peer_cert); 378 379 ctx->ssl_conn = NULL; 380 ctx->ssl_ctx = NULL; 381 ctx->ssl_peer_cert = NULL; 382 383 ctx->socket = -1; 384 ctx->state = 0; 385 386 free(ctx->servername); 387 ctx->servername = NULL; 388 389 free(ctx->error.msg); 390 ctx->error.msg = NULL; 391 ctx->error.num = -1; 392 393 tls_free_conninfo(ctx->conninfo); 394 free(ctx->conninfo); 395 ctx->conninfo = NULL; 396 } 397 398 int 399 tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, const char *prefix) 400 { 401 const char *errstr = "unknown error"; 402 unsigned long err; 403 int ssl_err; 404 405 ssl_err = SSL_get_error(ssl_conn, ssl_ret); 406 switch (ssl_err) { 407 case SSL_ERROR_NONE: 408 case SSL_ERROR_ZERO_RETURN: 409 return (0); 410 411 case SSL_ERROR_WANT_READ: 412 return (TLS_WANT_POLLIN); 413 414 case SSL_ERROR_WANT_WRITE: 415 return (TLS_WANT_POLLOUT); 416 417 case SSL_ERROR_SYSCALL: 418 if ((err = ERR_peek_error()) != 0) { 419 errstr = ERR_error_string(err, NULL); 420 } else if (ssl_ret == 0) { 421 ctx->state |= TLS_EOF_NO_CLOSE_NOTIFY; 422 return (0); 423 } else if (ssl_ret == -1) { 424 errstr = strerror(errno); 425 } 426 tls_set_errorx(ctx, "%s failed: %s", prefix, errstr); 427 return (-1); 428 429 case SSL_ERROR_SSL: 430 if ((err = ERR_peek_error()) != 0) { 431 errstr = ERR_error_string(err, NULL); 432 } 433 tls_set_errorx(ctx, "%s failed: %s", prefix, errstr); 434 return (-1); 435 436 case SSL_ERROR_WANT_CONNECT: 437 case SSL_ERROR_WANT_ACCEPT: 438 case SSL_ERROR_WANT_X509_LOOKUP: 439 default: 440 tls_set_errorx(ctx, "%s failed (%i)", prefix, ssl_err); 441 return (-1); 442 } 443 } 444 445 int 446 tls_handshake(struct tls *ctx) 447 { 448 int rv = -1; 449 450 if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) { 451 tls_set_errorx(ctx, "invalid operation for context"); 452 goto out; 453 } 454 455 if (ctx->conninfo == NULL && 456 (ctx->conninfo = calloc(1, sizeof(*ctx->conninfo))) == NULL) 457 goto out; 458 459 if ((ctx->flags & TLS_CLIENT) != 0) 460 rv = tls_handshake_client(ctx); 461 else if ((ctx->flags & TLS_SERVER_CONN) != 0) 462 rv = tls_handshake_server(ctx); 463 464 if (rv == 0) { 465 ctx->ssl_peer_cert = SSL_get_peer_certificate(ctx->ssl_conn); 466 if (tls_get_conninfo(ctx) == -1) 467 rv = -1; 468 } 469 out: 470 /* Prevent callers from performing incorrect error handling */ 471 errno = 0; 472 return (rv); 473 } 474 475 ssize_t 476 tls_read(struct tls *ctx, void *buf, size_t buflen) 477 { 478 ssize_t rv = -1; 479 int ssl_ret; 480 481 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) { 482 if ((rv = tls_handshake(ctx)) != 0) 483 goto out; 484 } 485 486 if (buflen > INT_MAX) { 487 tls_set_errorx(ctx, "buflen too long"); 488 goto out; 489 } 490 491 ERR_clear_error(); 492 if ((ssl_ret = SSL_read(ctx->ssl_conn, buf, buflen)) > 0) { 493 rv = (ssize_t)ssl_ret; 494 goto out; 495 } 496 rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "read"); 497 498 out: 499 /* Prevent callers from performing incorrect error handling */ 500 errno = 0; 501 return (rv); 502 } 503 504 ssize_t 505 tls_write(struct tls *ctx, const void *buf, size_t buflen) 506 { 507 ssize_t rv = -1; 508 int ssl_ret; 509 510 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) { 511 if ((rv = tls_handshake(ctx)) != 0) 512 goto out; 513 } 514 515 if (buflen > INT_MAX) { 516 tls_set_errorx(ctx, "buflen too long"); 517 goto out; 518 } 519 520 ERR_clear_error(); 521 if ((ssl_ret = SSL_write(ctx->ssl_conn, buf, buflen)) > 0) { 522 rv = (ssize_t)ssl_ret; 523 goto out; 524 } 525 rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "write"); 526 527 out: 528 /* Prevent callers from performing incorrect error handling */ 529 errno = 0; 530 return (rv); 531 } 532 533 int 534 tls_close(struct tls *ctx) 535 { 536 int ssl_ret; 537 int rv = 0; 538 539 if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) { 540 tls_set_errorx(ctx, "invalid operation for context"); 541 rv = -1; 542 goto out; 543 } 544 545 if (ctx->ssl_conn != NULL) { 546 ERR_clear_error(); 547 ssl_ret = SSL_shutdown(ctx->ssl_conn); 548 if (ssl_ret < 0) { 549 rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, 550 "shutdown"); 551 if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT) 552 goto out; 553 } 554 } 555 556 if (ctx->socket != -1) { 557 if (shutdown(ctx->socket, SHUT_RDWR) != 0) { 558 if (rv == 0 && 559 errno != ENOTCONN && errno != ECONNRESET) { 560 tls_set_error(ctx, "shutdown"); 561 rv = -1; 562 } 563 } 564 if (close(ctx->socket) != 0) { 565 if (rv == 0) { 566 tls_set_error(ctx, "close"); 567 rv = -1; 568 } 569 } 570 ctx->socket = -1; 571 } 572 573 if ((ctx->state & TLS_EOF_NO_CLOSE_NOTIFY) != 0) { 574 tls_set_errorx(ctx, "EOF without close notify"); 575 rv = -1; 576 } 577 578 out: 579 /* Prevent callers from performing incorrect error handling */ 580 errno = 0; 581 return (rv); 582 } 583