]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/tls_o.c
Don't ignore all Makefiles
[openldap] / libraries / libldap / tls_o.c
index 4bb500cae3b61efd390a67ac3b85daa3a0f631bc..8a3876779da711e7cef0e9dd8c5b9fa8fba1c4df 100644 (file)
@@ -1,8 +1,8 @@
-/* tls_o.c - Handle tls/ssl using SSLeay or OpenSSL */
+/* tls_o.c - Handle tls/ssl using OpenSSL */
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2008-2009 The OpenLDAP Foundation.
+ * Copyright 2008-2011 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "ldap-int.h"
 #include "ldap-tls.h"
 
-#ifdef LDAP_R_COMPILE
-#include <ldap_pvt_thread.h>
-#endif
-
 #ifdef HAVE_OPENSSL_SSL_H
 #include <openssl/ssl.h>
 #include <openssl/x509v3.h>
@@ -77,7 +73,7 @@ static int tlso_seed_PRNG( const char *randfile );
 
 #ifdef LDAP_R_COMPILE
 /*
- * provide mutexes for the SSLeay library.
+ * provide mutexes for the OpenSSL library.
  */
 static ldap_pvt_thread_mutex_t tlso_mutexes[CRYPTO_NUM_LOCKS];
 static ldap_pvt_thread_mutex_t tlso_dh_mutex;
@@ -162,7 +158,8 @@ tlso_init( void )
 #endif
 
        SSL_load_error_strings();
-       SSLeay_add_ssl_algorithms();
+       SSL_library_init();
+       OpenSSL_add_all_digests();
 
        /* FIXME: mod_ssl does this */
        X509V3_add_standard_extensions();
@@ -222,6 +219,11 @@ tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
                        (const unsigned char *) "OpenLDAP", sizeof("OpenLDAP")-1 );
        }
 
+       if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 )
+               SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 );
+       else if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL2 )
+               SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 );
+
        if ( lo->ldo_tls_ciphersuite &&
                !SSL_CTX_set_cipher_list( ctx, lt->lt_ciphersuite ) )
        {
@@ -392,11 +394,22 @@ tlso_session_upflags( Sockbuf *sb, tls_session *sess, int rc )
 }
 
 static char *
-tlso_session_errmsg( int rc, char *buf, size_t len )
+tlso_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
 {
+       char err[256] = "";
+       const char *certerr=NULL;
+       tlso_session *s = (tlso_session *)sess;
+
        rc = ERR_peek_error();
        if ( rc ) {
-               ERR_error_string_n( rc, buf, len );
+               ERR_error_string_n( rc, err, sizeof(err) );
+               if ( ( ERR_GET_LIB(rc) == ERR_LIB_SSL ) && 
+                               ( ERR_GET_REASON(rc) == SSL_R_CERTIFICATE_VERIFY_FAILED ) ) {
+                       int certrc = SSL_get_verify_result(s);
+                       certerr = (char *)X509_verify_cert_error_string(certrc);
+               }
+               snprintf(buf, len, "%s%s%s%s", err, certerr ? " (" :"", 
+                               certerr ? certerr : "", certerr ?  ")" : "" );
                return buf;
        }
        return NULL;
@@ -416,7 +429,7 @@ tlso_session_my_dn( tls_session *sess, struct berval *der_dn )
        xn = X509_get_subject_name(x);
        der_dn->bv_len = i2d_X509_NAME( xn, NULL );
        der_dn->bv_val = xn->bytes->data;
-       X509_free(x);
+       /* Don't X509_free, the session is still using it */
        return 0;
 }
 
@@ -460,7 +473,7 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
        X509 *x;
        const char *name;
        char *ptr;
-       int ntype = IS_DNS;
+       int ntype = IS_DNS, nlen;
 #ifdef LDAP_PF_INET6
        struct in6_addr addr;
 #else
@@ -474,6 +487,7 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
        } else {
                name = name_in;
        }
+       nlen = strlen(name);
 
        x = tlso_get_cert(s);
        if (!x) {
@@ -489,7 +503,7 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
 #ifdef LDAP_PF_INET6
        if (name[0] == '[' && strchr(name, ']')) {
                char *n2 = ldap_strdup(name+1);
-               *strchr(n2, ']') = 2;
+               *strchr(n2, ']') = 0;
                if (inet_pton(AF_INET6, n2, &addr))
                        ntype = IS_IP6;
                LDAP_FREE(n2);
@@ -507,15 +521,14 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
                ex = X509_get_ext(x, i);
                alt = X509V3_EXT_d2i(ex);
                if (alt) {
-                       int n, len1 = 0, len2 = 0;
+                       int n, len2 = 0;
                        char *domain = NULL;
                        GENERAL_NAME *gn;
 
                        if (ntype == IS_DNS) {
-                               len1 = strlen(name);
                                domain = strchr(name, '.');
                                if (domain) {
-                                       len2 = len1 - (domain-name);
+                                       len2 = nlen - (domain-name);
                                }
                        }
                        n = sk_GENERAL_NAME_num(alt);
@@ -533,7 +546,7 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
                                        if (sl == 0) continue;
 
                                        /* Is this an exact match? */
-                                       if ((len1 == sl) && !strncasecmp(name, sn, len1)) {
+                                       if ((nlen == sl) && !strncasecmp(name, sn, nlen)) {
                                                break;
                                        }
 
@@ -573,13 +586,28 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
 
        if (ret != LDAP_SUCCESS) {
                X509_NAME *xn;
-               char buf[2048];
-               buf[0] = '\0';
+               X509_NAME_ENTRY *ne;
+               ASN1_OBJECT *obj;
+               ASN1_STRING *cn = NULL;
+               int navas;
+
+               /* find the last CN */
+               obj = OBJ_nid2obj( NID_commonName );
+               if ( !obj ) goto no_cn; /* should never happen */
 
                xn = X509_get_subject_name(x);
-               if( X509_NAME_get_text_by_NID( xn, NID_commonName,
-                       buf, sizeof(buf)) == -1)
+               navas = X509_NAME_entry_count( xn );
+               for ( i=navas-1; i>=0; i-- ) {
+                       ne = X509_NAME_get_entry( xn, i );
+                       if ( !OBJ_cmp( ne->object, obj )) {
+                               cn = X509_NAME_ENTRY_get_data( ne );
+                               break;
+                       }
+               }
+
+               if( !cn )
                {
+no_cn:
                        Debug( LDAP_DEBUG_ANY,
                                "TLS: unable to get common name from peer certificate.\n",
                                0, 0, 0 );
@@ -590,21 +618,20 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
                        ld->ld_error = LDAP_STRDUP(
                                _("TLS: unable to get CN from peer certificate"));
 
-               } else if (strcasecmp(name, buf) == 0 ) {
+               } else if ( cn->length == nlen &&
+                       strncasecmp( name, (char *) cn->data, nlen ) == 0 ) {
                        ret = LDAP_SUCCESS;
 
-               } else if (( buf[0] == '*' ) && ( buf[1] == '.' )) {
+               } else if (( cn->data[0] == '*' ) && ( cn->data[1] == '.' )) {
                        char *domain = strchr(name, '.');
                        if( domain ) {
-                               size_t dlen = 0;
-                               size_t sl;
+                               int dlen;
 
-                               sl = strlen(name);
-                               dlen = sl - (domain-name);
-                               sl = strlen(buf);
+                               dlen = nlen - (domain-name);
 
                                /* Is this a wildcard match? */
-                               if ((dlen == sl-1) && !strncasecmp(domain, &buf[1], dlen)) {
+                               if ((dlen == cn->length-1) &&
+                                       !strncasecmp(domain, (char *) &cn->data[1], dlen)) {
                                        ret = LDAP_SUCCESS;
                                }
                        }
@@ -612,8 +639,8 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
 
                if( ret == LDAP_LOCAL_ERROR ) {
                        Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
-                               "common name in certificate (%s).\n", 
-                               name, buf, 0 );
+                               "common name in certificate (%.*s).\n", 
+                               name, cn->length, cn->data );
                        ret = LDAP_CONNECT_ERROR;
                        if ( ld->ld_error ) {
                                LDAP_FREE( ld->ld_error );
@@ -1046,16 +1073,29 @@ static RSA *
 tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
 {
        RSA *tmp_rsa;
-
        /* FIXME:  Pregenerate the key on startup */
        /* FIXME:  Who frees the key? */
+#if OPENSSL_VERSION_NUMBER >= 0x00908000
+       BIGNUM *bn = BN_new();
+       tmp_rsa = NULL;
+       if ( bn ) {
+               if ( BN_set_word( bn, RSA_F4 )) {
+                       tmp_rsa = RSA_new();
+                       if ( tmp_rsa && !RSA_generate_key_ex( tmp_rsa, key_length, bn, NULL )) {
+                               RSA_free( tmp_rsa );
+                               tmp_rsa = NULL;
+                       }
+               }
+               BN_free( bn );
+       }
+#else
        tmp_rsa = RSA_generate_key( key_length, RSA_F4, NULL, NULL );
+#endif
 
        if ( !tmp_rsa ) {
                Debug( LDAP_DEBUG_ANY,
                        "TLS: Failed to generate temporary %d-bit %s RSA key\n",
                        key_length, is_export ? "export" : "domestic", 0 );
-               return NULL;
        }
        return tmp_rsa;
 }
@@ -1170,14 +1210,10 @@ tlso_tmp_dh_cb( SSL *ssl, int is_export, int key_length )
        int i;
 
        /* Do we have params of this length already? */
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_lock( &tlso_dh_mutex );
-#endif
+       LDAP_MUTEX_LOCK( &tlso_dh_mutex );
        for ( p = tlso_dhparams; p; p=p->next ) {
                if ( p->keylength == key_length ) {
-#ifdef LDAP_R_COMPILE
-                       ldap_pvt_thread_mutex_unlock( &tlso_dh_mutex );
-#endif
+                       LDAP_MUTEX_UNLOCK( &tlso_dh_mutex );
                        return p->param;
                }
        }
@@ -1210,9 +1246,7 @@ tlso_tmp_dh_cb( SSL *ssl, int is_export, int key_length )
                }
        }
 
-#ifdef LDAP_R_COMPILE
-       ldap_pvt_thread_mutex_unlock( &tlso_dh_mutex );
-#endif
+       LDAP_MUTEX_UNLOCK( &tlso_dh_mutex );
        return dh;
 }