]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/tls_o.c
ITS#8753 Public key pinning support in libldap
[openldap] / libraries / libldap / tls_o.c
index 7b9cafc1e96d6b5eb82cde568fc17cb5bc3a982f..d3b6ceb35fae5752f8f2382a85b186bf930bc405 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2008-2014 The OpenLDAP Foundation.
+ * Copyright 2008-2017 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include <ssl.h>
 #endif
 
+#if OPENSSL_VERSION_NUMBER >= 0x10100000
+#define ASN1_STRING_data(x)    ASN1_STRING_get0_data(x)
+#endif
+
 typedef SSL_CTX tlso_ctx;
 typedef SSL tlso_session;
 
@@ -57,9 +61,12 @@ static void tlso_report_error( void );
 static void tlso_info_cb( const SSL *ssl, int where, int ret );
 static int tlso_verify_cb( int ok, X509_STORE_CTX *ctx );
 static int tlso_verify_ok( int ok, X509_STORE_CTX *ctx );
-static RSA * tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length );
-
 static int tlso_seed_PRNG( const char *randfile );
+#if OPENSSL_VERSION_NUMBER < 0x10100000
+/*
+ * OpenSSL 1.1 API and later has new locking code
+*/
+static RSA * tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length );
 
 #ifdef LDAP_R_COMPILE
 /*
@@ -100,9 +107,14 @@ static void tlso_thr_init( void )
        CRYPTO_set_id_callback( tlso_thread_self );
 }
 #endif /* LDAP_R_COMPILE */
+#else
+#ifdef LDAP_R_COMPILE
+static void tlso_thr_init( void ) {}
+#endif
+#endif /* OpenSSL 1.1 */
 
 static STACK_OF(X509_NAME) *
-tlso_ca_list( char * bundle, char * dir )
+tlso_ca_list( char * bundle, char * dir, X509 *cert )
 {
        STACK_OF(X509_NAME) *ca_list = NULL;
 
@@ -124,6 +136,14 @@ tlso_ca_list( char * bundle, char * dir )
                }
        }
 #endif
+       if ( cert ) {
+               X509_NAME *xn = X509_get_subject_name( cert );
+               xn = X509_NAME_dup( xn );
+               if ( !ca_list )
+                       ca_list = sk_X509_NAME_new_null();
+               if ( xn && ca_list )
+                       sk_X509_NAME_push( ca_list, xn );
+       }
        return ca_list;
 }
 
@@ -145,9 +165,13 @@ tlso_init( void )
        (void) tlso_seed_PRNG( lo->ldo_tls_randfile );
 #endif
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000
        SSL_load_error_strings();
        SSL_library_init();
        OpenSSL_add_all_digests();
+#else
+       OPENSSL_init_ssl(0, NULL);
+#endif
 
        /* FIXME: mod_ssl does this */
        X509V3_add_standard_extensions();
@@ -163,9 +187,15 @@ tlso_destroy( void )
 {
        struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();   
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000
        EVP_cleanup();
+#if OPENSSL_VERSION_NUMBER < 0x10000000
        ERR_remove_state(0);
+#else
+       ERR_remove_thread_state(NULL);
+#endif
        ERR_free_strings();
+#endif
 
        if ( lo->ldo_tls_randfile ) {
                LDAP_FREE( lo->ldo_tls_randfile );
@@ -183,7 +213,10 @@ static void
 tlso_ctx_ref( tls_ctx *ctx )
 {
        tlso_ctx *c = (tlso_ctx *)ctx;
-       CRYPTO_add( &c->references, 1, CRYPTO_LOCK_SSL_CTX );
+#if OPENSSL_VERSION_NUMBER < 0x10100000
+#define        SSL_CTX_up_ref(ctx)     CRYPTO_add( &(ctx->references), 1, CRYPTO_LOCK_SSL_CTX )
+#endif
+       SSL_CTX_up_ref( c );
 }
 
 static void
@@ -241,10 +274,29 @@ tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
                return -1;
        }
 
-       if (lo->ldo_tls_cacertfile != NULL || lo->ldo_tls_cacertdir != NULL) {
-               if ( !SSL_CTX_load_verify_locations( ctx,
-                               lt->lt_cacertfile, lt->lt_cacertdir ) ||
-                       !SSL_CTX_set_default_verify_paths( ctx ) )
+       if ( lo->ldo_tls_cacertfile == NULL && lo->ldo_tls_cacertdir == NULL &&
+               lo->ldo_tls_cacert.bv_val == NULL ) {
+               if ( !SSL_CTX_set_default_verify_paths( ctx ) ) {
+                       Debug( LDAP_DEBUG_ANY, "TLS: "
+                               "could not use default certificate paths", 0, 0, 0 );
+                       tlso_report_error();
+                       return -1;
+               }
+       } else {
+               X509 *cert = NULL;
+               if ( lo->ldo_tls_cacert.bv_val ) {
+                       const unsigned char *pp = lo->ldo_tls_cacert.bv_val;
+                       cert = d2i_X509( NULL, &pp, lo->ldo_tls_cacert.bv_len );
+                       X509_STORE *store = SSL_CTX_get_cert_store( ctx );
+                       if ( !X509_STORE_add_cert( store, cert )) {
+                               Debug( LDAP_DEBUG_ANY, "TLS: "
+                                       "could not use CA certificate", 0, 0, 0 );
+                               tlso_report_error();
+                               return -1;
+                       }
+               }
+               if (( lt->lt_cacertfile || lt->lt_cacertdir ) && !SSL_CTX_load_verify_locations( ctx,
+                               lt->lt_cacertfile, lt->lt_cacertdir ) )
                {
                        Debug( LDAP_DEBUG_ANY, "TLS: "
                                "could not load verify locations (file:`%s',dir:`%s').\n",
@@ -258,7 +310,7 @@ tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
                if ( is_server ) {
                        STACK_OF(X509_NAME) *calist;
                        /* List of CA names to send to a client */
-                       calist = tlso_ca_list( lt->lt_cacertfile, lt->lt_cacertdir );
+                       calist = tlso_ca_list( lt->lt_cacertfile, lt->lt_cacertdir, cert );
                        if ( !calist ) {
                                Debug( LDAP_DEBUG_ANY, "TLS: "
                                        "could not load client CA list (file:`%s',dir:`%s').\n",
@@ -271,20 +323,47 @@ tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
 
                        SSL_CTX_set_client_CA_list( ctx, calist );
                }
+               if ( cert )
+                       X509_free( cert );
        }
 
+       if ( lo->ldo_tls_cert.bv_val )
+       {
+               const unsigned char *pp = lo->ldo_tls_cert.bv_val;
+               X509 *cert = d2i_X509( NULL, &pp, lo->ldo_tls_cert.bv_len );
+               if ( !SSL_CTX_use_certificate( ctx, cert )) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "TLS: could not use certificate.\n", 0,0,0);
+                       tlso_report_error();
+                       return -1;
+               }
+               X509_free( cert );
+       } else
        if ( lo->ldo_tls_certfile &&
                !SSL_CTX_use_certificate_file( ctx,
                        lt->lt_certfile, SSL_FILETYPE_PEM ) )
        {
                Debug( LDAP_DEBUG_ANY,
-                       "TLS: could not use certificate `%s'.\n",
+                       "TLS: could not use certificate file `%s'.\n",
                        lo->ldo_tls_certfile,0,0);
                tlso_report_error();
                return -1;
        }
 
        /* Key validity is checked automatically if cert has already been set */
+       if ( lo->ldo_tls_key.bv_val )
+       {
+               const unsigned char *pp = lo->ldo_tls_key.bv_val;
+               EVP_PKEY *pkey = d2i_AutoPrivateKey( NULL, &pp, lo->ldo_tls_key.bv_len );
+               if ( !SSL_CTX_use_PrivateKey( ctx, pkey ))
+               {
+                       Debug( LDAP_DEBUG_ANY,
+                               "TLS: could not use private key.\n", 0,0,0);
+                       tlso_report_error();
+                       return -1;
+               }
+               EVP_PKEY_free( pkey );
+       } else
        if ( lo->ldo_tls_keyfile &&
                !SSL_CTX_use_PrivateKey_file( ctx,
                        lt->lt_keyfile, SSL_FILETYPE_PEM ) )
@@ -367,7 +446,9 @@ tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
        SSL_CTX_set_verify( ctx, i,
                lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_ALLOW ?
                tlso_verify_ok : tlso_verify_cb );
+#if OPENSSL_VERSION_NUMBER < 0x10100000
        SSL_CTX_set_tmp_rsa_callback( ctx, tlso_tmp_rsa_cb );
+#endif
 #ifdef HAVE_OPENSSL_CRL
        if ( lo->ldo_tls_crlcheck ) {
                X509_STORE *x509_s = SSL_CTX_get_cert_store( ctx );
@@ -462,8 +543,17 @@ tlso_session_my_dn( tls_session *sess, struct berval *der_dn )
        if (!x) return LDAP_INVALID_CREDENTIALS;
        
        xn = X509_get_subject_name(x);
+#if OPENSSL_VERSION_NUMBER < 0x10100000
        der_dn->bv_len = i2d_X509_NAME( xn, NULL );
        der_dn->bv_val = xn->bytes->data;
+#else
+       {
+               size_t len = 0;
+               der_dn->bv_val = NULL;
+               X509_NAME_get0_der( xn, (const unsigned char **)&der_dn->bv_val, &len );
+               der_dn->bv_len = len;
+       }
+#endif
        /* Don't X509_free, the session is still using it */
        return 0;
 }
@@ -489,8 +579,17 @@ tlso_session_peer_dn( tls_session *sess, struct berval *der_dn )
                return LDAP_INVALID_CREDENTIALS;
 
        xn = X509_get_subject_name(x);
+#if OPENSSL_VERSION_NUMBER < 0x10100000
        der_dn->bv_len = i2d_X509_NAME( xn, NULL );
        der_dn->bv_val = xn->bytes->data;
+#else
+       {
+               size_t len = 0;
+               der_dn->bv_val = NULL;
+               X509_NAME_get0_der( xn, (const unsigned char **)&der_dn->bv_val, &len );
+               der_dn->bv_len = len;
+       }
+#endif
        X509_free(x);
        return 0;
 }
@@ -630,7 +729,7 @@ tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
                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 )) {
+                       if ( !OBJ_cmp( X509_NAME_ENTRY_get_object(ne), obj )) {
                                cn = X509_NAME_ENTRY_get_data( ne );
                                break;
                        }
@@ -736,6 +835,69 @@ tlso_session_peercert( tls_session *sess, struct berval *der )
        return 0;
 }
 
+static int
+tlso_session_pinning( LDAP *ld, tls_session *sess, char *hashalg, struct berval *hash )
+{
+       tlso_session *s = (tlso_session *)sess;
+       char *tmp, digest[EVP_MAX_MD_SIZE];
+       struct berval key,
+                                 keyhash = { .bv_val = digest, .bv_len = sizeof(digest) };
+       X509 *cert = SSL_get_peer_certificate(s);
+       int len, rc = LDAP_SUCCESS;
+
+       len = i2d_X509_PUBKEY( X509_get_X509_PUBKEY(cert), NULL );
+
+       key.bv_val = tmp = LDAP_MALLOC( len );
+       if ( !key.bv_val ) {
+               return -1;
+       }
+
+       key.bv_len = i2d_X509_PUBKEY( X509_get_X509_PUBKEY(cert), &tmp );
+
+       if ( hashalg ) {
+               const EVP_MD *md;
+               EVP_MD_CTX *mdctx;
+               unsigned int len = keyhash.bv_len;
+
+               md = EVP_get_digestbyname( hashalg );
+               if ( !md ) {
+                       Debug( LDAP_DEBUG_TRACE, "tlso_session_pinning: "
+                                       "hash %s not recognised by OpenSSL\n", hashalg, 0, 0 );
+                       rc = -1;
+                       goto done;
+               }
+
+               mdctx = EVP_MD_CTX_new();
+               if ( !mdctx ) {
+                       rc = -1;
+                       goto done;
+               }
+
+               EVP_DigestInit_ex( mdctx, md, NULL );
+               EVP_DigestUpdate( mdctx, key.bv_val, key.bv_len );
+               EVP_DigestFinal_ex( mdctx, (unsigned char *)keyhash.bv_val, &len );
+               keyhash.bv_len = len;
+               EVP_MD_CTX_free( mdctx );
+       } else {
+               keyhash = key;
+       }
+
+       if ( ber_bvcmp( hash, &keyhash ) ) {
+               rc = LDAP_CONNECT_ERROR;
+               Debug( LDAP_DEBUG_ANY, "tlso_session_pinning: "
+                               "public key hash does not match provided pin.\n", 0, 0, 0 );
+               if ( ld->ld_error ) {
+                       LDAP_FREE( ld->ld_error );
+               }
+               ld->ld_error = LDAP_STRDUP(
+                       _("TLS: public key hash does not match provided pin"));
+       }
+
+done:
+       LDAP_FREE( key.bv_val );
+       return rc;
+}
+
 /*
  * TLS support for LBER Sockbufs
  */
@@ -745,12 +907,17 @@ struct tls_data {
        Sockbuf_IO_Desc         *sbiod;
 };
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000
+#define BIO_set_init(b, x)     b->init = x
+#define BIO_set_data(b, x)     b->ptr = x
+#define BIO_clear_flags(b, x)  b->flags &= ~(x)
+#define BIO_get_data(b)        b->ptr
+#endif
 static int
 tlso_bio_create( BIO *b ) {
-       b->init = 1;
-       b->num = 0;
-       b->ptr = NULL;
-       b->flags = 0;
+       BIO_set_init( b, 1 );
+       BIO_set_data( b, NULL );
+       BIO_clear_flags( b, ~0 );
        return 1;
 }
 
@@ -759,9 +926,9 @@ tlso_bio_destroy( BIO *b )
 {
        if ( b == NULL ) return 0;
 
-       b->ptr = NULL;          /* sb_tls_remove() will free it */
-       b->init = 0;
-       b->flags = 0;
+       BIO_set_data( b, NULL );                /* sb_tls_remove() will free it */
+       BIO_set_init( b, 0 );
+       BIO_clear_flags( b, ~0 );
        return 1;
 }
 
@@ -773,7 +940,7 @@ tlso_bio_read( BIO *b, char *buf, int len )
                
        if ( buf == NULL || len <= 0 ) return 0;
 
-       p = (struct tls_data *)b->ptr;
+       p = (struct tls_data *)BIO_get_data(b);
 
        if ( p == NULL || p->sbiod == NULL ) {
                return 0;
@@ -800,7 +967,7 @@ tlso_bio_write( BIO *b, const char *buf, int len )
        
        if ( buf == NULL || len <= 0 ) return 0;
        
-       p = (struct tls_data *)b->ptr;
+       p = (struct tls_data *)BIO_get_data(b);
 
        if ( p == NULL || p->sbiod == NULL ) {
                return 0;
@@ -840,7 +1007,22 @@ tlso_bio_puts( BIO *b, const char *str )
 {
        return tlso_bio_write( b, str, strlen( str ) );
 }
-       
+
+#if OPENSSL_VERSION_NUMBER >= 0x10100000
+struct bio_method_st {
+    int type;
+    const char *name;
+    int (*bwrite) (BIO *, const char *, int);
+    int (*bread) (BIO *, char *, int);
+    int (*bputs) (BIO *, const char *);
+    int (*bgets) (BIO *, char *, int);
+    long (*ctrl) (BIO *, int, long, void *);
+    int (*create) (BIO *);
+    int (*destroy) (BIO *);
+    long (*callback_ctrl) (BIO *, int, bio_info_cb *);
+};
+#endif
+
 static BIO_METHOD tlso_bio_method =
 {
        ( 100 | 0x400 ),                /* it's a source/sink BIO */
@@ -870,7 +1052,7 @@ tlso_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
        p->session = arg;
        p->sbiod = sbiod;
        bio = BIO_new( &tlso_bio_method );
-       bio->ptr = (void *)p;
+       BIO_set_data( bio, p );
        SSL_set_bio( p->session, bio, bio );
        sbiod->sbiod_pvt = p;
        return 0;
@@ -1077,7 +1259,7 @@ tlso_verify_cb( int ok, X509_STORE_CTX *ctx )
         */
        subject = X509_get_subject_name( cert );
        issuer = X509_get_issuer_name( cert );
-       /* X509_NAME_oneline, if passed a NULL buf, allocate memomry */
+       /* X509_NAME_oneline, if passed a NULL buf, allocate memory */
        sname = X509_NAME_oneline( subject, NULL, 0 );
        iname = X509_NAME_oneline( issuer, NULL, 0 );
        if ( !ok ) certerr = (char *)X509_verify_cert_error_string( errnum );
@@ -1100,9 +1282,9 @@ tlso_verify_cb( int ok, X509_STORE_CTX *ctx )
                        certerr, 0, 0 );
        }
        if ( sname )
-               CRYPTO_free ( sname );
+               OPENSSL_free ( sname );
        if ( iname )
-               CRYPTO_free ( iname );
+               OPENSSL_free ( iname );
 #ifdef HAVE_EBCDIC
        if ( certerr ) LDAP_FREE( certerr );
 #endif
@@ -1142,6 +1324,7 @@ tlso_report_error( void )
        }
 }
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000
 static RSA *
 tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
 {
@@ -1172,6 +1355,7 @@ tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
        }
        return tmp_rsa;
 }
+#endif /* OPENSSL_VERSION_NUMBER < 1.1 */
 
 static int
 tlso_seed_PRNG( const char *randfile )
@@ -1188,11 +1372,13 @@ tlso_seed_PRNG( const char *randfile )
                 * The fact is that when $HOME is NULL, .rnd is used.
                 */
                randfile = RAND_file_name( buffer, sizeof( buffer ) );
-
-       } else if (RAND_egd(randfile) > 0) {
+       }
+#ifndef OPENSSL_NO_EGD
+       else if (RAND_egd(randfile) > 0) {
                /* EGD socket */
                return 0;
        }
+#endif
 
        if (randfile == NULL) {
                Debug( LDAP_DEBUG_ANY,
@@ -1245,6 +1431,7 @@ tls_impl ldap_int_tls_impl = {
        tlso_session_version,
        tlso_session_cipher,
        tlso_session_peercert,
+       tlso_session_pinning,
 
        &tlso_sbio,