1 /* $NetBSD: tls_o.c,v 1.8 2020/08/11 13:15:38 christos Exp $ */ 2 3 /* tls_o.c - Handle tls/ssl using OpenSSL */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 2008-2020 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 /* ACKNOWLEDGEMENTS: Rewritten by Howard Chu 19 */ 20 21 #include <sys/cdefs.h> 22 __RCSID("$NetBSD: tls_o.c,v 1.8 2020/08/11 13:15:38 christos Exp $"); 23 24 #include "portable.h" 25 26 #ifdef HAVE_OPENSSL 27 28 #include "ldap_config.h" 29 30 #include <stdio.h> 31 32 #include <ac/stdlib.h> 33 #include <ac/errno.h> 34 #include <ac/socket.h> 35 #include <ac/string.h> 36 #include <ac/ctype.h> 37 #include <ac/time.h> 38 #include <ac/unistd.h> 39 #include <ac/param.h> 40 #include <ac/dirent.h> 41 42 #include "ldap-int.h" 43 #include "ldap-tls.h" 44 45 #ifdef HAVE_OPENSSL_SSL_H 46 #include <openssl/ssl.h> 47 #include <openssl/x509v3.h> 48 #include <openssl/err.h> 49 #include <openssl/rand.h> 50 #include <openssl/safestack.h> 51 #include <openssl/bn.h> 52 #include <openssl/rsa.h> 53 #include <openssl/dh.h> 54 #elif defined( HAVE_SSL_H ) 55 #include <ssl.h> 56 #endif 57 58 #if OPENSSL_VERSION_NUMBER >= 0x10100000 59 #define ASN1_STRING_data(x) ASN1_STRING_get0_data(x) 60 #endif 61 62 typedef SSL_CTX tlso_ctx; 63 typedef SSL tlso_session; 64 65 static BIO_METHOD * tlso_bio_method = NULL; 66 static BIO_METHOD * tlso_bio_setup( void ); 67 68 static int tlso_opt_trace = 1; 69 70 static void tlso_report_error( void ); 71 72 static void tlso_info_cb( const SSL *ssl, int where, int ret ); 73 static int tlso_verify_cb( int ok, X509_STORE_CTX *ctx ); 74 static int tlso_verify_ok( int ok, X509_STORE_CTX *ctx ); 75 static int tlso_seed_PRNG( const char *randfile ); 76 #if OPENSSL_VERSION_NUMBER < 0x10100000 77 /* 78 * OpenSSL 1.1 API and later has new locking code 79 */ 80 static RSA * tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length ); 81 82 #ifdef LDAP_R_COMPILE 83 /* 84 * provide mutexes for the OpenSSL library. 85 */ 86 static ldap_pvt_thread_mutex_t tlso_mutexes[CRYPTO_NUM_LOCKS]; 87 88 static void tlso_locking_cb( int mode, int type, const char *file, int line ) 89 { 90 if ( mode & CRYPTO_LOCK ) { 91 ldap_pvt_thread_mutex_lock( &tlso_mutexes[type] ); 92 } else { 93 ldap_pvt_thread_mutex_unlock( &tlso_mutexes[type] ); 94 } 95 } 96 97 #if OPENSSL_VERSION_NUMBER >= 0x0909000 98 static void tlso_thread_self( CRYPTO_THREADID *id ) 99 { 100 CRYPTO_THREADID_set_pointer( id, (void *)ldap_pvt_thread_self() ); 101 } 102 #define CRYPTO_set_id_callback(foo) CRYPTO_THREADID_set_callback(foo) 103 #else 104 static unsigned long tlso_thread_self( void ) 105 { 106 /* FIXME: CRYPTO_set_id_callback only works when ldap_pvt_thread_t 107 * is an integral type that fits in an unsigned long 108 */ 109 110 /* force an error if the ldap_pvt_thread_t type is too large */ 111 enum { ok = sizeof( ldap_pvt_thread_t ) <= sizeof( unsigned long ) }; 112 typedef struct { int dummy: ok ? 1 : -1; } Check[ok ? 1 : -1]; 113 114 return (unsigned long) ldap_pvt_thread_self(); 115 } 116 #endif 117 118 static void tlso_thr_init( void ) 119 { 120 int i; 121 122 for( i=0; i< CRYPTO_NUM_LOCKS ; i++ ) { 123 ldap_pvt_thread_mutex_init( &tlso_mutexes[i] ); 124 } 125 CRYPTO_set_locking_callback( tlso_locking_cb ); 126 CRYPTO_set_id_callback( tlso_thread_self ); 127 } 128 #endif /* LDAP_R_COMPILE */ 129 #else 130 #ifdef LDAP_R_COMPILE 131 static void tlso_thr_init( void ) {} 132 #endif 133 #endif /* OpenSSL 1.1 */ 134 135 #if OPENSSL_VERSION_NUMBER < 0x10100000 136 /* 137 * OpenSSL 1.1 API and later makes the BIO method concrete types internal. 138 */ 139 140 static BIO_METHOD * 141 BIO_meth_new( int type, const char *name ) 142 { 143 BIO_METHOD *method = LDAP_MALLOC( sizeof(BIO_METHOD) ); 144 memset( method, 0, sizeof(BIO_METHOD) ); 145 146 method->type = type; 147 method->name = name; 148 149 return method; 150 } 151 152 static void 153 BIO_meth_free( BIO_METHOD *meth ) 154 { 155 if ( meth == NULL ) { 156 return; 157 } 158 159 LDAP_FREE( meth ); 160 } 161 162 #define BIO_meth_set_write(m, f) (m)->bwrite = (f) 163 #define BIO_meth_set_read(m, f) (m)->bread = (f) 164 #define BIO_meth_set_puts(m, f) (m)->bputs = (f) 165 #define BIO_meth_set_gets(m, f) (m)->bgets = (f) 166 #define BIO_meth_set_ctrl(m, f) (m)->ctrl = (f) 167 #define BIO_meth_set_create(m, f) (m)->create = (f) 168 #define BIO_meth_set_destroy(m, f) (m)->destroy = (f) 169 170 #endif /* OpenSSL 1.1 */ 171 172 static STACK_OF(X509_NAME) * 173 tlso_ca_list( char * bundle, char * dir ) 174 { 175 STACK_OF(X509_NAME) *ca_list = NULL; 176 177 if ( bundle ) { 178 ca_list = SSL_load_client_CA_file( bundle ); 179 } 180 #if defined(HAVE_DIRENT_H) || defined(dirent) 181 if ( dir ) { 182 int freeit = 0; 183 184 if ( !ca_list ) { 185 ca_list = sk_X509_NAME_new_null(); 186 freeit = 1; 187 } 188 if ( !SSL_add_dir_cert_subjects_to_stack( ca_list, dir ) && 189 freeit ) { 190 sk_X509_NAME_free( ca_list ); 191 ca_list = NULL; 192 } 193 } 194 #endif 195 return ca_list; 196 } 197 198 /* 199 * Initialize TLS subsystem. Should be called only once. 200 */ 201 static int 202 tlso_init( void ) 203 { 204 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT(); 205 #ifdef HAVE_EBCDIC 206 { 207 char *file = LDAP_STRDUP( lo->ldo_tls_randfile ); 208 if ( file ) __atoe( file ); 209 (void) tlso_seed_PRNG( file ); 210 LDAP_FREE( file ); 211 } 212 #else 213 (void) tlso_seed_PRNG( lo->ldo_tls_randfile ); 214 #endif 215 216 #if OPENSSL_VERSION_NUMBER < 0x10100000 217 SSL_load_error_strings(); 218 SSL_library_init(); 219 OpenSSL_add_all_digests(); 220 #else 221 OPENSSL_init_ssl(0, NULL); 222 #endif 223 224 /* FIXME: mod_ssl does this */ 225 X509V3_add_standard_extensions(); 226 227 tlso_bio_method = tlso_bio_setup(); 228 229 return 0; 230 } 231 232 /* 233 * Tear down the TLS subsystem. Should only be called once. 234 */ 235 static void 236 tlso_destroy( void ) 237 { 238 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT(); 239 240 BIO_meth_free( tlso_bio_method ); 241 242 #if OPENSSL_VERSION_NUMBER < 0x10100000 243 EVP_cleanup(); 244 #if OPENSSL_VERSION_NUMBER < 0x10000000 245 ERR_remove_state(0); 246 #else 247 ERR_remove_thread_state(NULL); 248 #endif 249 ERR_free_strings(); 250 #endif 251 252 if ( lo->ldo_tls_randfile ) { 253 LDAP_FREE( lo->ldo_tls_randfile ); 254 lo->ldo_tls_randfile = NULL; 255 } 256 } 257 258 static tls_ctx * 259 tlso_ctx_new( struct ldapoptions *lo ) 260 { 261 return (tls_ctx *) SSL_CTX_new( SSLv23_method() ); 262 } 263 264 static void 265 tlso_ctx_ref( tls_ctx *ctx ) 266 { 267 tlso_ctx *c = (tlso_ctx *)ctx; 268 #if OPENSSL_VERSION_NUMBER < 0x10100000 269 #define SSL_CTX_up_ref(ctx) CRYPTO_add( &(ctx->references), 1, CRYPTO_LOCK_SSL_CTX ) 270 #endif 271 SSL_CTX_up_ref( c ); 272 } 273 274 static void 275 tlso_ctx_free ( tls_ctx *ctx ) 276 { 277 tlso_ctx *c = (tlso_ctx *)ctx; 278 SSL_CTX_free( c ); 279 } 280 281 /* 282 * initialize a new TLS context 283 */ 284 static int 285 tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server ) 286 { 287 tlso_ctx *ctx = (tlso_ctx *)lo->ldo_tls_ctx; 288 int i; 289 290 if ( is_server ) { 291 SSL_CTX_set_session_id_context( ctx, 292 (const unsigned char *) "OpenLDAP", sizeof("OpenLDAP")-1 ); 293 } 294 295 #ifdef SSL_OP_NO_TLSv1 296 #ifdef SSL_OP_NO_TLSv1_1 297 #ifdef SSL_OP_NO_TLSv1_2 298 if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_TLS1_2) 299 SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | 300 SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | 301 SSL_OP_NO_TLSv1_2 ); 302 else 303 #endif 304 if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_TLS1_1) 305 SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | 306 SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 ); 307 else 308 #endif 309 if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_TLS1_0) 310 SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | 311 SSL_OP_NO_TLSv1); 312 else 313 #endif 314 if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 ) 315 SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 ); 316 else if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL2 ) 317 SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 ); 318 319 if ( lo->ldo_tls_ciphersuite && 320 !SSL_CTX_set_cipher_list( ctx, lt->lt_ciphersuite ) ) 321 { 322 Debug( LDAP_DEBUG_ANY, 323 "TLS: could not set cipher list %s.\n", 324 lo->ldo_tls_ciphersuite, 0, 0 ); 325 tlso_report_error(); 326 return -1; 327 } 328 329 if ( lo->ldo_tls_cacertfile == NULL && lo->ldo_tls_cacertdir == NULL ) { 330 if ( !SSL_CTX_set_default_verify_paths( ctx ) ) { 331 Debug( LDAP_DEBUG_ANY, "TLS: " 332 "could not use default certificate paths", 0, 0, 0 ); 333 tlso_report_error(); 334 return -1; 335 } 336 } else { 337 if ( !SSL_CTX_load_verify_locations( ctx, 338 lt->lt_cacertfile, lt->lt_cacertdir ) ) 339 { 340 Debug( LDAP_DEBUG_ANY, "TLS: " 341 "could not load verify locations (file:`%s',dir:`%s').\n", 342 lo->ldo_tls_cacertfile ? lo->ldo_tls_cacertfile : "", 343 lo->ldo_tls_cacertdir ? lo->ldo_tls_cacertdir : "", 344 0 ); 345 tlso_report_error(); 346 return -1; 347 } 348 349 if ( is_server ) { 350 STACK_OF(X509_NAME) *calist; 351 /* List of CA names to send to a client */ 352 calist = tlso_ca_list( lt->lt_cacertfile, lt->lt_cacertdir ); 353 if ( !calist ) { 354 Debug( LDAP_DEBUG_ANY, "TLS: " 355 "could not load client CA list (file:`%s',dir:`%s').\n", 356 lo->ldo_tls_cacertfile ? lo->ldo_tls_cacertfile : "", 357 lo->ldo_tls_cacertdir ? lo->ldo_tls_cacertdir : "", 358 0 ); 359 tlso_report_error(); 360 return -1; 361 } 362 363 SSL_CTX_set_client_CA_list( ctx, calist ); 364 } 365 } 366 367 if ( lo->ldo_tls_certfile && 368 !SSL_CTX_use_certificate_file( ctx, 369 lt->lt_certfile, SSL_FILETYPE_PEM ) ) 370 { 371 Debug( LDAP_DEBUG_ANY, 372 "TLS: could not use certificate `%s'.\n", 373 lo->ldo_tls_certfile,0,0); 374 tlso_report_error(); 375 return -1; 376 } 377 378 /* Key validity is checked automatically if cert has already been set */ 379 if ( lo->ldo_tls_keyfile && 380 !SSL_CTX_use_PrivateKey_file( ctx, 381 lt->lt_keyfile, SSL_FILETYPE_PEM ) ) 382 { 383 Debug( LDAP_DEBUG_ANY, 384 "TLS: could not use key file `%s'.\n", 385 lo->ldo_tls_keyfile,0,0); 386 tlso_report_error(); 387 return -1; 388 } 389 390 if ( is_server && lo->ldo_tls_dhfile ) { 391 DH *dh; 392 BIO *bio; 393 394 if (( bio=BIO_new_file( lt->lt_dhfile,"r" )) == NULL ) { 395 Debug( LDAP_DEBUG_ANY, 396 "TLS: could not use DH parameters file `%s'.\n", 397 lo->ldo_tls_dhfile,0,0); 398 tlso_report_error(); 399 return -1; 400 } 401 if (!( dh=PEM_read_bio_DHparams( bio, NULL, NULL, NULL ))) { 402 Debug( LDAP_DEBUG_ANY, 403 "TLS: could not read DH parameters file `%s'.\n", 404 lo->ldo_tls_dhfile,0,0); 405 tlso_report_error(); 406 BIO_free( bio ); 407 return -1; 408 } 409 BIO_free( bio ); 410 SSL_CTX_set_tmp_dh( ctx, dh ); 411 SSL_CTX_set_options( ctx, SSL_OP_SINGLE_DH_USE ); 412 DH_free( dh ); 413 } 414 415 if ( is_server && lo->ldo_tls_ecname ) { 416 #ifdef OPENSSL_NO_EC 417 Debug( LDAP_DEBUG_ANY, 418 "TLS: Elliptic Curves not supported.\n", 0,0,0 ); 419 return -1; 420 #else 421 EC_KEY *ecdh; 422 423 int nid = OBJ_sn2nid( lt->lt_ecname ); 424 if ( nid == NID_undef ) { 425 Debug( LDAP_DEBUG_ANY, 426 "TLS: could not use EC name `%s'.\n", 427 lo->ldo_tls_ecname,0,0); 428 tlso_report_error(); 429 return -1; 430 } 431 ecdh = EC_KEY_new_by_curve_name( nid ); 432 if ( ecdh == NULL ) { 433 Debug( LDAP_DEBUG_ANY, 434 "TLS: could not generate key for EC name `%s'.\n", 435 lo->ldo_tls_ecname,0,0); 436 tlso_report_error(); 437 return -1; 438 } 439 SSL_CTX_set_tmp_ecdh( ctx, ecdh ); 440 SSL_CTX_set_options( ctx, SSL_OP_SINGLE_ECDH_USE ); 441 EC_KEY_free( ecdh ); 442 #endif 443 } 444 445 if ( tlso_opt_trace ) { 446 SSL_CTX_set_info_callback( ctx, tlso_info_cb ); 447 } 448 449 i = SSL_VERIFY_NONE; 450 if ( lo->ldo_tls_require_cert ) { 451 i = SSL_VERIFY_PEER; 452 if ( lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_DEMAND || 453 lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_HARD ) { 454 i |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; 455 } 456 } 457 458 SSL_CTX_set_verify( ctx, i, 459 lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_ALLOW ? 460 tlso_verify_ok : tlso_verify_cb ); 461 #if OPENSSL_VERSION_NUMBER < 0x10100000 462 SSL_CTX_set_tmp_rsa_callback( ctx, tlso_tmp_rsa_cb ); 463 #endif 464 #ifdef HAVE_OPENSSL_CRL 465 if ( lo->ldo_tls_crlcheck ) { 466 X509_STORE *x509_s = SSL_CTX_get_cert_store( ctx ); 467 if ( lo->ldo_tls_crlcheck == LDAP_OPT_X_TLS_CRL_PEER ) { 468 X509_STORE_set_flags( x509_s, X509_V_FLAG_CRL_CHECK ); 469 } else if ( lo->ldo_tls_crlcheck == LDAP_OPT_X_TLS_CRL_ALL ) { 470 X509_STORE_set_flags( x509_s, 471 X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL ); 472 } 473 } 474 #endif 475 return 0; 476 } 477 478 static tls_session * 479 tlso_session_new( tls_ctx *ctx, int is_server ) 480 { 481 tlso_ctx *c = (tlso_ctx *)ctx; 482 return (tls_session *)SSL_new( c ); 483 } 484 485 static int 486 tlso_session_connect( LDAP *ld, tls_session *sess ) 487 { 488 tlso_session *s = (tlso_session *)sess; 489 490 /* Caller expects 0 = success, OpenSSL returns 1 = success */ 491 int rc = SSL_connect( s ) - 1; 492 #ifdef LDAP_USE_NON_BLOCKING_TLS 493 if ( rc < 0 ) { 494 int sockerr = sock_errno(); 495 int sslerr = SSL_get_error( s, rc+1 ); 496 if ( sslerr == SSL_ERROR_WANT_READ || sslerr == SSL_ERROR_WANT_WRITE ) { 497 rc = 0; 498 } else if ( sslerr == SSL_ERROR_SYSCALL && 499 ( sockerr == EAGAIN || sockerr == ENOTCONN )) { 500 rc = 0; 501 } 502 } 503 #endif /* LDAP_USE_NON_BLOCKING_TLS */ 504 return rc; 505 } 506 507 static int 508 tlso_session_accept( tls_session *sess ) 509 { 510 tlso_session *s = (tlso_session *)sess; 511 512 /* Caller expects 0 = success, OpenSSL returns 1 = success */ 513 return SSL_accept( s ) - 1; 514 } 515 516 static int 517 tlso_session_upflags( Sockbuf *sb, tls_session *sess, int rc ) 518 { 519 tlso_session *s = (tlso_session *)sess; 520 521 /* 1 was subtracted above, offset it back now */ 522 rc = SSL_get_error(s, rc+1); 523 if (rc == SSL_ERROR_WANT_READ) { 524 sb->sb_trans_needs_read = 1; 525 return 1; 526 527 } else if (rc == SSL_ERROR_WANT_WRITE) { 528 sb->sb_trans_needs_write = 1; 529 return 1; 530 531 } else if (rc == SSL_ERROR_WANT_CONNECT) { 532 return 1; 533 } 534 return 0; 535 } 536 537 static char * 538 tlso_session_errmsg( tls_session *sess, int rc, char *buf, size_t len ) 539 { 540 char err[256] = ""; 541 const char *certerr=NULL; 542 tlso_session *s = (tlso_session *)sess; 543 544 rc = ERR_peek_error(); 545 if ( rc ) { 546 ERR_error_string_n( rc, err, sizeof(err) ); 547 if ( ( ERR_GET_LIB(rc) == ERR_LIB_SSL ) && 548 ( ERR_GET_REASON(rc) == SSL_R_CERTIFICATE_VERIFY_FAILED ) ) { 549 int certrc = SSL_get_verify_result(s); 550 certerr = (char *)X509_verify_cert_error_string(certrc); 551 } 552 snprintf(buf, len, "%s%s%s%s", err, certerr ? " (" :"", 553 certerr ? certerr : "", certerr ? ")" : "" ); 554 return buf; 555 } 556 return NULL; 557 } 558 559 static int 560 tlso_session_my_dn( tls_session *sess, struct berval *der_dn ) 561 { 562 tlso_session *s = (tlso_session *)sess; 563 X509 *x; 564 X509_NAME *xn; 565 566 x = SSL_get_certificate( s ); 567 568 if (!x) return LDAP_INVALID_CREDENTIALS; 569 570 xn = X509_get_subject_name(x); 571 #if OPENSSL_VERSION_NUMBER < 0x10100000 572 der_dn->bv_len = i2d_X509_NAME( xn, NULL ); 573 der_dn->bv_val = xn->bytes->data; 574 #else 575 { 576 size_t len = 0; 577 der_dn->bv_val = NULL; 578 X509_NAME_get0_der( xn, (const unsigned char **)&der_dn->bv_val, &len ); 579 der_dn->bv_len = len; 580 } 581 #endif 582 /* Don't X509_free, the session is still using it */ 583 return 0; 584 } 585 586 static X509 * 587 tlso_get_cert( SSL *s ) 588 { 589 /* If peer cert was bad, treat as if no cert was given */ 590 if (SSL_get_verify_result(s)) { 591 return NULL; 592 } 593 return SSL_get_peer_certificate(s); 594 } 595 596 static int 597 tlso_session_peer_dn( tls_session *sess, struct berval *der_dn ) 598 { 599 tlso_session *s = (tlso_session *)sess; 600 X509 *x = tlso_get_cert( s ); 601 X509_NAME *xn; 602 603 if ( !x ) 604 return LDAP_INVALID_CREDENTIALS; 605 606 xn = X509_get_subject_name(x); 607 #if OPENSSL_VERSION_NUMBER < 0x10100000 608 der_dn->bv_len = i2d_X509_NAME( xn, NULL ); 609 der_dn->bv_val = xn->bytes->data; 610 #else 611 { 612 size_t len = 0; 613 der_dn->bv_val = NULL; 614 X509_NAME_get0_der( xn, (const unsigned char **)&der_dn->bv_val, &len ); 615 der_dn->bv_len = len; 616 } 617 #endif 618 X509_free(x); 619 return 0; 620 } 621 622 /* what kind of hostname were we given? */ 623 #define IS_DNS 0 624 #define IS_IP4 1 625 #define IS_IP6 2 626 627 static int 628 tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in ) 629 { 630 tlso_session *s = (tlso_session *)sess; 631 int i, ret = LDAP_LOCAL_ERROR; 632 X509 *x; 633 const char *name; 634 char *ptr; 635 int ntype = IS_DNS, nlen; 636 #ifdef LDAP_PF_INET6 637 struct in6_addr addr; 638 #else 639 struct in_addr addr; 640 #endif 641 642 if( ldap_int_hostname && 643 ( !name_in || !strcasecmp( name_in, "localhost" ) ) ) 644 { 645 name = ldap_int_hostname; 646 } else { 647 name = name_in; 648 } 649 nlen = strlen(name); 650 651 x = tlso_get_cert(s); 652 if (!x) { 653 Debug( LDAP_DEBUG_ANY, 654 "TLS: unable to get peer certificate.\n", 655 0, 0, 0 ); 656 /* If this was a fatal condition, things would have 657 * aborted long before now. 658 */ 659 return LDAP_SUCCESS; 660 } 661 662 #ifdef LDAP_PF_INET6 663 if (inet_pton(AF_INET6, name, &addr)) { 664 ntype = IS_IP6; 665 } else 666 #endif 667 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) { 668 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4; 669 } 670 671 i = X509_get_ext_by_NID(x, NID_subject_alt_name, -1); 672 if (i >= 0) { 673 X509_EXTENSION *ex; 674 STACK_OF(GENERAL_NAME) *alt; 675 676 ex = X509_get_ext(x, i); 677 alt = X509V3_EXT_d2i(ex); 678 if (alt) { 679 int n, len2 = 0; 680 char *domain = NULL; 681 GENERAL_NAME *gn; 682 683 if (ntype == IS_DNS) { 684 domain = strchr(name, '.'); 685 if (domain) { 686 len2 = nlen - (domain-name); 687 } 688 } 689 n = sk_GENERAL_NAME_num(alt); 690 for (i=0; i<n; i++) { 691 char *sn; 692 int sl; 693 gn = sk_GENERAL_NAME_value(alt, i); 694 if (gn->type == GEN_DNS) { 695 if (ntype != IS_DNS) continue; 696 697 sn = (char *) ASN1_STRING_data(gn->d.ia5); 698 sl = ASN1_STRING_length(gn->d.ia5); 699 700 /* ignore empty */ 701 if (sl == 0) continue; 702 703 /* Is this an exact match? */ 704 if ((nlen == sl) && !strncasecmp(name, sn, nlen)) { 705 break; 706 } 707 708 /* Is this a wildcard match? */ 709 if (domain && (sn[0] == '*') && (sn[1] == '.') && 710 (len2 == sl-1) && !strncasecmp(domain, &sn[1], len2)) 711 { 712 break; 713 } 714 715 } else if (gn->type == GEN_IPADD) { 716 if (ntype == IS_DNS) continue; 717 718 sn = (char *) ASN1_STRING_data(gn->d.ia5); 719 sl = ASN1_STRING_length(gn->d.ia5); 720 721 #ifdef LDAP_PF_INET6 722 if (ntype == IS_IP6 && sl != sizeof(struct in6_addr)) { 723 continue; 724 } else 725 #endif 726 if (ntype == IS_IP4 && sl != sizeof(struct in_addr)) { 727 continue; 728 } 729 if (!memcmp(sn, &addr, sl)) { 730 break; 731 } 732 } 733 } 734 735 GENERAL_NAMES_free(alt); 736 if (i < n) { /* Found a match */ 737 ret = LDAP_SUCCESS; 738 } 739 } 740 } 741 742 if (ret != LDAP_SUCCESS) { 743 X509_NAME *xn; 744 X509_NAME_ENTRY *ne; 745 ASN1_OBJECT *obj; 746 ASN1_STRING *cn = NULL; 747 int navas; 748 749 /* find the last CN */ 750 obj = OBJ_nid2obj( NID_commonName ); 751 if ( !obj ) goto no_cn; /* should never happen */ 752 753 xn = X509_get_subject_name(x); 754 navas = X509_NAME_entry_count( xn ); 755 for ( i=navas-1; i>=0; i-- ) { 756 ne = X509_NAME_get_entry( xn, i ); 757 if ( !OBJ_cmp( X509_NAME_ENTRY_get_object(ne), obj )) { 758 cn = X509_NAME_ENTRY_get_data( ne ); 759 break; 760 } 761 } 762 763 if( !cn ) 764 { 765 no_cn: 766 Debug( LDAP_DEBUG_ANY, 767 "TLS: unable to get common name from peer certificate.\n", 768 0, 0, 0 ); 769 ret = LDAP_CONNECT_ERROR; 770 if ( ld->ld_error ) { 771 LDAP_FREE( ld->ld_error ); 772 } 773 ld->ld_error = LDAP_STRDUP( 774 _("TLS: unable to get CN from peer certificate")); 775 776 } else if ( cn->length == nlen && 777 strncasecmp( name, (char *) cn->data, nlen ) == 0 ) { 778 ret = LDAP_SUCCESS; 779 780 } else if (( cn->data[0] == '*' ) && ( cn->data[1] == '.' )) { 781 char *domain = strchr(name, '.'); 782 if( domain ) { 783 int dlen; 784 785 dlen = nlen - (domain-name); 786 787 /* Is this a wildcard match? */ 788 if ((dlen == cn->length-1) && 789 !strncasecmp(domain, (char *) &cn->data[1], dlen)) { 790 ret = LDAP_SUCCESS; 791 } 792 } 793 } 794 795 if( ret == LDAP_LOCAL_ERROR ) { 796 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match " 797 "common name in certificate (%.*s).\n", 798 name, cn->length, cn->data ); 799 ret = LDAP_CONNECT_ERROR; 800 if ( ld->ld_error ) { 801 LDAP_FREE( ld->ld_error ); 802 } 803 ld->ld_error = LDAP_STRDUP( 804 _("TLS: hostname does not match CN in peer certificate")); 805 } 806 } 807 X509_free(x); 808 return ret; 809 } 810 811 static int 812 tlso_session_strength( tls_session *sess ) 813 { 814 tlso_session *s = (tlso_session *)sess; 815 816 return SSL_CIPHER_get_bits(SSL_get_current_cipher(s), NULL); 817 } 818 819 /* 820 * TLS support for LBER Sockbufs 821 */ 822 823 struct tls_data { 824 tlso_session *session; 825 Sockbuf_IO_Desc *sbiod; 826 }; 827 828 #if OPENSSL_VERSION_NUMBER < 0x10100000 829 #define BIO_set_init(b, x) b->init = x 830 #define BIO_set_data(b, x) b->ptr = x 831 #define BIO_clear_flags(b, x) b->flags &= ~(x) 832 #define BIO_get_data(b) b->ptr 833 #endif 834 static int 835 tlso_bio_create( BIO *b ) { 836 BIO_set_init( b, 1 ); 837 BIO_set_data( b, NULL ); 838 BIO_clear_flags( b, ~0 ); 839 return 1; 840 } 841 842 static int 843 tlso_bio_destroy( BIO *b ) 844 { 845 if ( b == NULL ) return 0; 846 847 BIO_set_data( b, NULL ); /* sb_tls_remove() will free it */ 848 BIO_set_init( b, 0 ); 849 BIO_clear_flags( b, ~0 ); 850 return 1; 851 } 852 853 static int 854 tlso_bio_read( BIO *b, char *buf, int len ) 855 { 856 struct tls_data *p; 857 int ret; 858 859 if ( buf == NULL || len <= 0 ) return 0; 860 861 p = (struct tls_data *)BIO_get_data(b); 862 863 if ( p == NULL || p->sbiod == NULL ) { 864 return 0; 865 } 866 867 ret = LBER_SBIOD_READ_NEXT( p->sbiod, buf, len ); 868 869 BIO_clear_retry_flags( b ); 870 if ( ret < 0 ) { 871 int err = sock_errno(); 872 if ( err == EAGAIN || err == EWOULDBLOCK ) { 873 BIO_set_retry_read( b ); 874 } 875 } 876 877 return ret; 878 } 879 880 static int 881 tlso_bio_write( BIO *b, const char *buf, int len ) 882 { 883 struct tls_data *p; 884 int ret; 885 886 if ( buf == NULL || len <= 0 ) return 0; 887 888 p = (struct tls_data *)BIO_get_data(b); 889 890 if ( p == NULL || p->sbiod == NULL ) { 891 return 0; 892 } 893 894 ret = LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len ); 895 896 BIO_clear_retry_flags( b ); 897 if ( ret < 0 ) { 898 int err = sock_errno(); 899 if ( err == EAGAIN || err == EWOULDBLOCK ) { 900 BIO_set_retry_write( b ); 901 } 902 } 903 904 return ret; 905 } 906 907 static long 908 tlso_bio_ctrl( BIO *b, int cmd, long num, void *ptr ) 909 { 910 if ( cmd == BIO_CTRL_FLUSH ) { 911 /* The OpenSSL library needs this */ 912 return 1; 913 } 914 return 0; 915 } 916 917 static int 918 tlso_bio_gets( BIO *b, char *buf, int len ) 919 { 920 return -1; 921 } 922 923 static int 924 tlso_bio_puts( BIO *b, const char *str ) 925 { 926 return tlso_bio_write( b, str, strlen( str ) ); 927 } 928 929 static BIO_METHOD * 930 tlso_bio_setup( void ) 931 { 932 /* it's a source/sink BIO */ 933 BIO_METHOD * method = BIO_meth_new( 100 | 0x400, "sockbuf glue" ); 934 BIO_meth_set_write( method, tlso_bio_write ); 935 BIO_meth_set_read( method, tlso_bio_read ); 936 BIO_meth_set_puts( method, tlso_bio_puts ); 937 BIO_meth_set_gets( method, tlso_bio_gets ); 938 BIO_meth_set_ctrl( method, tlso_bio_ctrl ); 939 BIO_meth_set_create( method, tlso_bio_create ); 940 BIO_meth_set_destroy( method, tlso_bio_destroy ); 941 942 return method; 943 } 944 945 static int 946 tlso_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg ) 947 { 948 struct tls_data *p; 949 BIO *bio; 950 951 assert( sbiod != NULL ); 952 953 p = LBER_MALLOC( sizeof( *p ) ); 954 if ( p == NULL ) { 955 return -1; 956 } 957 958 p->session = arg; 959 p->sbiod = sbiod; 960 bio = BIO_new( tlso_bio_method ); 961 BIO_set_data( bio, p ); 962 SSL_set_bio( p->session, bio, bio ); 963 sbiod->sbiod_pvt = p; 964 return 0; 965 } 966 967 static int 968 tlso_sb_remove( Sockbuf_IO_Desc *sbiod ) 969 { 970 struct tls_data *p; 971 972 assert( sbiod != NULL ); 973 assert( sbiod->sbiod_pvt != NULL ); 974 975 p = (struct tls_data *)sbiod->sbiod_pvt; 976 SSL_free( p->session ); 977 LBER_FREE( sbiod->sbiod_pvt ); 978 sbiod->sbiod_pvt = NULL; 979 return 0; 980 } 981 982 static int 983 tlso_sb_close( Sockbuf_IO_Desc *sbiod ) 984 { 985 struct tls_data *p; 986 987 assert( sbiod != NULL ); 988 assert( sbiod->sbiod_pvt != NULL ); 989 990 p = (struct tls_data *)sbiod->sbiod_pvt; 991 SSL_shutdown( p->session ); 992 return 0; 993 } 994 995 static int 996 tlso_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) 997 { 998 struct tls_data *p; 999 1000 assert( sbiod != NULL ); 1001 assert( sbiod->sbiod_pvt != NULL ); 1002 1003 p = (struct tls_data *)sbiod->sbiod_pvt; 1004 1005 if ( opt == LBER_SB_OPT_GET_SSL ) { 1006 *((tlso_session **)arg) = p->session; 1007 return 1; 1008 1009 } else if ( opt == LBER_SB_OPT_DATA_READY ) { 1010 if( SSL_pending( p->session ) > 0 ) { 1011 return 1; 1012 } 1013 } 1014 1015 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg ); 1016 } 1017 1018 static ber_slen_t 1019 tlso_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len) 1020 { 1021 struct tls_data *p; 1022 ber_slen_t ret; 1023 int err; 1024 1025 assert( sbiod != NULL ); 1026 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) ); 1027 1028 p = (struct tls_data *)sbiod->sbiod_pvt; 1029 1030 ret = SSL_read( p->session, (char *)buf, len ); 1031 #ifdef HAVE_WINSOCK 1032 errno = WSAGetLastError(); 1033 #endif 1034 err = SSL_get_error( p->session, ret ); 1035 if (err == SSL_ERROR_WANT_READ ) { 1036 sbiod->sbiod_sb->sb_trans_needs_read = 1; 1037 sock_errset(EWOULDBLOCK); 1038 } 1039 else 1040 sbiod->sbiod_sb->sb_trans_needs_read = 0; 1041 return ret; 1042 } 1043 1044 static ber_slen_t 1045 tlso_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len) 1046 { 1047 struct tls_data *p; 1048 ber_slen_t ret; 1049 int err; 1050 1051 assert( sbiod != NULL ); 1052 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) ); 1053 1054 p = (struct tls_data *)sbiod->sbiod_pvt; 1055 1056 ret = SSL_write( p->session, (char *)buf, len ); 1057 #ifdef HAVE_WINSOCK 1058 errno = WSAGetLastError(); 1059 #endif 1060 err = SSL_get_error( p->session, ret ); 1061 if (err == SSL_ERROR_WANT_WRITE ) { 1062 sbiod->sbiod_sb->sb_trans_needs_write = 1; 1063 sock_errset(EWOULDBLOCK); 1064 1065 } else { 1066 sbiod->sbiod_sb->sb_trans_needs_write = 0; 1067 } 1068 return ret; 1069 } 1070 1071 static Sockbuf_IO tlso_sbio = 1072 { 1073 tlso_sb_setup, /* sbi_setup */ 1074 tlso_sb_remove, /* sbi_remove */ 1075 tlso_sb_ctrl, /* sbi_ctrl */ 1076 tlso_sb_read, /* sbi_read */ 1077 tlso_sb_write, /* sbi_write */ 1078 tlso_sb_close /* sbi_close */ 1079 }; 1080 1081 /* Derived from openssl/apps/s_cb.c */ 1082 static void 1083 tlso_info_cb( const SSL *ssl, int where, int ret ) 1084 { 1085 int w; 1086 char *op; 1087 char *state = (char *) SSL_state_string_long( (SSL *)ssl ); 1088 1089 w = where & ~SSL_ST_MASK; 1090 if ( w & SSL_ST_CONNECT ) { 1091 op = "SSL_connect"; 1092 } else if ( w & SSL_ST_ACCEPT ) { 1093 op = "SSL_accept"; 1094 } else { 1095 op = "undefined"; 1096 } 1097 1098 #ifdef HAVE_EBCDIC 1099 if ( state ) { 1100 state = LDAP_STRDUP( state ); 1101 __etoa( state ); 1102 } 1103 #endif 1104 if ( where & SSL_CB_LOOP ) { 1105 Debug( LDAP_DEBUG_TRACE, 1106 "TLS trace: %s:%s\n", 1107 op, state, 0 ); 1108 1109 } else if ( where & SSL_CB_ALERT ) { 1110 char *atype = (char *) SSL_alert_type_string_long( ret ); 1111 char *adesc = (char *) SSL_alert_desc_string_long( ret ); 1112 op = ( where & SSL_CB_READ ) ? "read" : "write"; 1113 #ifdef HAVE_EBCDIC 1114 if ( atype ) { 1115 atype = LDAP_STRDUP( atype ); 1116 __etoa( atype ); 1117 } 1118 if ( adesc ) { 1119 adesc = LDAP_STRDUP( adesc ); 1120 __etoa( adesc ); 1121 } 1122 #endif 1123 Debug( LDAP_DEBUG_TRACE, 1124 "TLS trace: SSL3 alert %s:%s:%s\n", 1125 op, atype, adesc ); 1126 #ifdef HAVE_EBCDIC 1127 if ( atype ) LDAP_FREE( atype ); 1128 if ( adesc ) LDAP_FREE( adesc ); 1129 #endif 1130 } else if ( where & SSL_CB_EXIT ) { 1131 if ( ret == 0 ) { 1132 Debug( LDAP_DEBUG_TRACE, 1133 "TLS trace: %s:failed in %s\n", 1134 op, state, 0 ); 1135 } else if ( ret < 0 ) { 1136 Debug( LDAP_DEBUG_TRACE, 1137 "TLS trace: %s:error in %s\n", 1138 op, state, 0 ); 1139 } 1140 } 1141 #ifdef HAVE_EBCDIC 1142 if ( state ) LDAP_FREE( state ); 1143 #endif 1144 } 1145 1146 static int 1147 tlso_verify_cb( int ok, X509_STORE_CTX *ctx ) 1148 { 1149 X509 *cert; 1150 int errnum; 1151 int errdepth; 1152 X509_NAME *subject; 1153 X509_NAME *issuer; 1154 char *sname; 1155 char *iname; 1156 char *certerr = NULL; 1157 1158 cert = X509_STORE_CTX_get_current_cert( ctx ); 1159 errnum = X509_STORE_CTX_get_error( ctx ); 1160 errdepth = X509_STORE_CTX_get_error_depth( ctx ); 1161 1162 /* 1163 * X509_get_*_name return pointers to the internal copies of 1164 * those things requested. So do not free them. 1165 */ 1166 subject = X509_get_subject_name( cert ); 1167 issuer = X509_get_issuer_name( cert ); 1168 /* X509_NAME_oneline, if passed a NULL buf, allocate memomry */ 1169 sname = X509_NAME_oneline( subject, NULL, 0 ); 1170 iname = X509_NAME_oneline( issuer, NULL, 0 ); 1171 if ( !ok ) certerr = (char *)X509_verify_cert_error_string( errnum ); 1172 #ifdef HAVE_EBCDIC 1173 if ( sname ) __etoa( sname ); 1174 if ( iname ) __etoa( iname ); 1175 if ( certerr ) { 1176 certerr = LDAP_STRDUP( certerr ); 1177 __etoa( certerr ); 1178 } 1179 #endif 1180 Debug( LDAP_DEBUG_TRACE, 1181 "TLS certificate verification: depth: %d, err: %d, subject: %s,", 1182 errdepth, errnum, 1183 sname ? sname : "-unknown-" ); 1184 Debug( LDAP_DEBUG_TRACE, " issuer: %s\n", iname ? iname : "-unknown-", 0, 0 ); 1185 if ( !ok ) { 1186 Debug( LDAP_DEBUG_ANY, 1187 "TLS certificate verification: Error, %s\n", 1188 certerr, 0, 0 ); 1189 } 1190 if ( sname ) 1191 OPENSSL_free ( sname ); 1192 if ( iname ) 1193 OPENSSL_free ( iname ); 1194 #ifdef HAVE_EBCDIC 1195 if ( certerr ) LDAP_FREE( certerr ); 1196 #endif 1197 return ok; 1198 } 1199 1200 static int 1201 tlso_verify_ok( int ok, X509_STORE_CTX *ctx ) 1202 { 1203 (void) tlso_verify_cb( ok, ctx ); 1204 return 1; 1205 } 1206 1207 /* Inspired by ERR_print_errors in OpenSSL */ 1208 static void 1209 tlso_report_error( void ) 1210 { 1211 unsigned long l; 1212 char buf[200]; 1213 const char *file; 1214 int line; 1215 1216 while ( ( l = ERR_get_error_line( &file, &line ) ) != 0 ) { 1217 ERR_error_string_n( l, buf, sizeof( buf ) ); 1218 #ifdef HAVE_EBCDIC 1219 if ( file ) { 1220 file = LDAP_STRDUP( file ); 1221 __etoa( (char *)file ); 1222 } 1223 __etoa( buf ); 1224 #endif 1225 Debug( LDAP_DEBUG_ANY, "TLS: %s %s:%d\n", 1226 buf, file, line ); 1227 #ifdef HAVE_EBCDIC 1228 if ( file ) LDAP_FREE( (void *)file ); 1229 #endif 1230 } 1231 } 1232 1233 #if OPENSSL_VERSION_NUMBER < 0x10100000 1234 static RSA * 1235 tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length ) 1236 { 1237 RSA *tmp_rsa; 1238 /* FIXME: Pregenerate the key on startup */ 1239 /* FIXME: Who frees the key? */ 1240 #if OPENSSL_VERSION_NUMBER >= 0x00908000 1241 BIGNUM *bn = BN_new(); 1242 tmp_rsa = NULL; 1243 if ( bn ) { 1244 if ( BN_set_word( bn, RSA_F4 )) { 1245 tmp_rsa = RSA_new(); 1246 if ( tmp_rsa && !RSA_generate_key_ex( tmp_rsa, key_length, bn, NULL )) { 1247 RSA_free( tmp_rsa ); 1248 tmp_rsa = NULL; 1249 } 1250 } 1251 BN_free( bn ); 1252 } 1253 #else 1254 tmp_rsa = RSA_generate_key( key_length, RSA_F4, NULL, NULL ); 1255 #endif 1256 1257 if ( !tmp_rsa ) { 1258 Debug( LDAP_DEBUG_ANY, 1259 "TLS: Failed to generate temporary %d-bit %s RSA key\n", 1260 key_length, is_export ? "export" : "domestic", 0 ); 1261 } 1262 return tmp_rsa; 1263 } 1264 #endif /* OPENSSL_VERSION_NUMBER < 1.1 */ 1265 1266 static int 1267 tlso_seed_PRNG( const char *randfile ) 1268 { 1269 #ifndef URANDOM_DEVICE 1270 /* no /dev/urandom (or equiv) */ 1271 long total=0; 1272 char buffer[MAXPATHLEN]; 1273 1274 if (randfile == NULL) { 1275 /* The seed file is $RANDFILE if defined, otherwise $HOME/.rnd. 1276 * If $HOME is not set or buffer too small to hold the pathname, 1277 * an error occurs. - From RAND_file_name() man page. 1278 * The fact is that when $HOME is NULL, .rnd is used. 1279 */ 1280 randfile = RAND_file_name( buffer, sizeof( buffer ) ); 1281 } 1282 #ifndef OPENSSL_NO_EGD 1283 else if (RAND_egd(randfile) > 0) { 1284 /* EGD socket */ 1285 return 0; 1286 } 1287 #endif 1288 1289 if (randfile == NULL) { 1290 Debug( LDAP_DEBUG_ANY, 1291 "TLS: Use configuration file or $RANDFILE to define seed PRNG\n", 1292 0, 0, 0); 1293 return -1; 1294 } 1295 1296 total = RAND_load_file(randfile, -1); 1297 1298 if (RAND_status() == 0) { 1299 Debug( LDAP_DEBUG_ANY, 1300 "TLS: PRNG not been seeded with enough data\n", 1301 0, 0, 0); 1302 return -1; 1303 } 1304 1305 /* assume if there was enough bits to seed that it's okay 1306 * to write derived bits to the file 1307 */ 1308 RAND_write_file(randfile); 1309 1310 #endif 1311 1312 return 0; 1313 } 1314 1315 1316 tls_impl ldap_int_tls_impl = { 1317 "OpenSSL", 1318 1319 tlso_init, 1320 tlso_destroy, 1321 1322 tlso_ctx_new, 1323 tlso_ctx_ref, 1324 tlso_ctx_free, 1325 tlso_ctx_init, 1326 1327 tlso_session_new, 1328 tlso_session_connect, 1329 tlso_session_accept, 1330 tlso_session_upflags, 1331 tlso_session_errmsg, 1332 tlso_session_my_dn, 1333 tlso_session_peer_dn, 1334 tlso_session_chkhost, 1335 tlso_session_strength, 1336 1337 &tlso_sbio, 1338 1339 #ifdef LDAP_R_COMPILE 1340 tlso_thr_init, 1341 #else 1342 NULL, 1343 #endif 1344 1345 0 1346 }; 1347 1348 #endif /* HAVE_OPENSSL */ 1349