Github user gadLinux commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/1185#discussion_r100681388
  
    --- Diff: lib/c_glib/src/thrift/c_glib/transport/thrift_ssl_socket.c ---
    @@ -0,0 +1,772 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements. See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership. The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License. You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied. See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +
    +#include <errno.h>
    +#include <netdb.h>
    +#include <stdlib.h>
    +#include <string.h>
    +#include <unistd.h>
    +#include <sys/socket.h>
    +#include <netinet/in.h>
    +#include <openssl/ssl.h>
    +#include <pthread.h>
    +
    +#include <thrift/c_glib/thrift.h>
    +#include <thrift/c_glib/transport/thrift_transport.h>
    +#include <thrift/c_glib/transport/thrift_socket.h>
    +#include <thrift/c_glib/transport/thrift_ssl_socket.h>
    +
    +#if defined(WIN32)
    +#define MUTEX_TYPE            HANDLE
    +#define MUTEX_SETUP(x)        (x) = CreateMutex(NULL, FALSE, NULL)
    +#define MUTEX_CLEANUP(x)      CloseHandle(x)
    +#define MUTEX_LOCK(x)         WaitForSingleObject((x), INFINITE)
    +#define MUTEX_UNLOCK(x)       ReleaseMutex(x)
    +#else
    +#define MUTEX_TYPE            pthread_mutex_t
    +#define MUTEX_SETUP(x)        pthread_mutex_init(&(x), NULL)
    +#define MUTEX_CLEANUP(x)      pthread_mutex_destroy(&(x))
    +#define MUTEX_LOCK(x)         pthread_mutex_lock(&(x))
    +#define MUTEX_UNLOCK(x)       pthread_mutex_unlock(&(x))
    +#endif
    +
    +#define OPENSSL_VERSION_NO_THREAD_ID 0x10000000L
    +
    +
    +/* object properties */
    +enum _ThriftSSLSocketProperties
    +{
    +  PROP_THRIFT_SSL_SOCKET_CONTEXT = 3,
    +  PROP_THRIFT_SSL_SELF_SIGNED
    +};
    +
    +/* To hold a global state management of openssl for all instances */
    +static gboolean thrift_ssl_socket_openssl_initialized=FALSE;
    +/* Should this be keept at class level? */
    +static SSL_CTX* thrift_ssl_socket_global_context=NULL;
    +/* This array will store all of the mutexes available to OpenSSL. */
    +static MUTEX_TYPE *thrift_ssl_socket_global_mutex_buf=NULL;
    +
    +
    +/**
    + * OpenSSL uniq id function.
    + *
    + * @return    thread id
    + */
    +static unsigned long thrift_ssl_socket_static_id_function(void)
    +{
    +#if defined(WIN32)
    +  return GetCurrentThreadId();
    +#else
    +  return ((unsigned long) pthread_self());
    +#endif
    +}
    +
    +static void thrift_ssl_socket_static_locking_callback(int mode, int n, 
const char* unk, int id) {
    +  if (mode & CRYPTO_LOCK)
    +    MUTEX_LOCK(thrift_ssl_socket_global_mutex_buf[n]);
    +  else
    +    MUTEX_UNLOCK(thrift_ssl_socket_global_mutex_buf[n]);
    +}
    +
    +static int thrift_ssl_socket_static_thread_setup(void)
    +{
    +  int i;
    +
    +  thrift_ssl_socket_global_mutex_buf = malloc(CRYPTO_num_locks() * 
sizeof(MUTEX_TYPE));
    +  if (!thrift_ssl_socket_global_mutex_buf)
    +    return 0;
    +  for (i = 0;  i < CRYPTO_num_locks(  );  i++)
    +    MUTEX_SETUP(thrift_ssl_socket_global_mutex_buf[i]);
    +  CRYPTO_set_id_callback(thrift_ssl_socket_static_id_function);
    +  CRYPTO_set_locking_callback(thrift_ssl_socket_static_locking_callback);
    +  return 1;
    +}
    +
    +static int thrift_ssl_socket_static_thread_cleanup(void)
    +{
    +  int i;
    +  if (!thrift_ssl_socket_global_mutex_buf)
    +    return 0;
    +  CRYPTO_set_id_callback(NULL);
    +  CRYPTO_set_locking_callback(NULL);
    +  for (i = 0;  i < CRYPTO_num_locks(  );  i++)
    +    MUTEX_CLEANUP(thrift_ssl_socket_global_mutex_buf[i]);
    +  free(thrift_ssl_socket_global_mutex_buf);
    +  thrift_ssl_socket_global_mutex_buf = NULL;
    +  return 1;
    +}
    +
    +/*
    +static void* thrift_ssl_socket_dyn_lock_create_callback(const char* unk, 
int id) {
    +  g_print("We should create a lock\n");
    +  return NULL;
    +}
    +
    +static void thrift_ssl_socket_dyn_lock_callback(int mode, void* lock, 
const char* unk, int id) {
    +  if (lock != NULL) {
    +    if (mode & CRYPTO_LOCK) {
    +      g_printf("We should lock thread %d\n");
    +    } else {
    +      g_printf("We should unlock thread %d\n");
    +    }
    +  }
    +}
    +
    +static void thrift_ssl_socket_dyn_lock_destroy_callback(void* lock, const 
char* unk, int id) {
    +  g_printf("We must destroy the lock\n");
    +}
    + */
    +
    +
    +G_DEFINE_TYPE(ThriftSSLSocket, thrift_ssl_socket, THRIFT_TYPE_SOCKET)
    +
    +
    +/* implements thrift_transport_is_open */
    +gboolean
    +thrift_ssl_socket_is_open (ThriftTransport *transport)
    +{
    +  return thrift_socket_is_open(transport);
    +}
    +
    +/* overrides thrift_transport_peek */
    +gboolean
    +thrift_ssl_socket_peek (ThriftTransport *transport, GError **error)
    +{
    +  gboolean retval = FALSE;
    +  ThriftSSLSocket *ssl_socket = THRIFT_SSL_SOCKET (transport);
    +  if(ssl_socket!=NULL && 
THRIFT_SSL_SOCKET_GET_CLASS(ssl_socket)->handle_handshake(transport, error)){
    +      if (thrift_ssl_socket_is_open (transport))
    +   {
    +     int rc;
    +     gchar byte;
    +     rc = SSL_peek(ssl_socket->ssl, &byte, 1);
    +     if (rc < 0) {
    +         g_set_error (error,
    +                      THRIFT_TRANSPORT_ERROR,
    +                      THRIFT_SSL_SOCKET_ERROR_SSL,
    +                      "failed to peek at socket - id?");
    +     }
    +     if (rc == 0) {
    +         ERR_clear_error();
    +     }
    +     retval = (rc > 0);
    +   }
    +  }
    +  return retval;
    +}
    +
    +/* implements thrift_transport_open */
    +gboolean
    +thrift_ssl_socket_open (ThriftTransport *transport, GError **error)
    +{
    +  return thrift_socket_open(transport, error);
    +}
    +
    +/* implements thrift_transport_close */
    +gboolean
    +thrift_ssl_socket_close (ThriftTransport *transport, GError **error)
    +{
    +  gboolean retval = FALSE;
    +  if(THRIFT_SSL_SOCKET(transport)->ssl) {
    +      int rc = SSL_shutdown(THRIFT_SSL_SOCKET(transport)->ssl);
    +      if (rc < 0) {
    +     int errno_copy = THRIFT_SSL_SOCKET_ERROR_SSL;
    +      }
    +      SSL_free(THRIFT_SSL_SOCKET(transport)->ssl);
    +      THRIFT_SSL_SOCKET(transport)->ssl = NULL;
    +      ERR_remove_state(0);
    +  }
    +  return thrift_socket_close(transport, error);
    +}
    +
    +/* implements thrift_transport_read */
    +gint32
    +thrift_ssl_socket_read (ThriftTransport *transport, gpointer buf,
    +                   guint32 len, GError **error)
    +{
    +  guint maxRecvRetries_ = 10;
    +  ThriftSSLSocket *ssl_socket = THRIFT_SSL_SOCKET (transport);
    +  guint bytes = 0;
    +  guint retries = 0;
    +  if(ssl_socket!=NULL && 
THRIFT_SSL_SOCKET_GET_CLASS(ssl_socket)->handle_handshake(transport, error)){
    +      for (retries=0; retries < maxRecvRetries_; retries++) {
    +     bytes = SSL_read(ssl_socket->ssl, buf, len);
    +     if (bytes >= 0)
    +       break;
    +     int errno_copy = THRIFT_GET_SOCKET_ERROR;
    +     if (SSL_get_error(ssl_socket->ssl, bytes) == SSL_ERROR_SYSCALL) {
    +         if (ERR_get_error() == 0 && errno_copy == THRIFT_EINTR) {
    +             continue;
    +         }
    +     }
    +     g_set_error (error, THRIFT_TRANSPORT_ERROR,
    +                  THRIFT_TRANSPORT_ERROR_RECEIVE,
    +                  "failed to read %d bytes - %s", len, strerror(errno));
    +     return -1;
    +      }
    +  }
    +  return bytes;
    +}
    +
    +/* implements thrift_transport_read_end
    + * called when write is complete.  nothing to do on our end. */
    +gboolean
    +thrift_ssl_socket_read_end (ThriftTransport *transport, GError **error)
    +{
    +  /* satisfy -Wall */
    +  THRIFT_UNUSED_VAR (transport);
    +  THRIFT_UNUSED_VAR (error);
    +  return TRUE;
    +}
    +
    +/* implements thrift_transport_write */
    +gboolean
    +thrift_ssl_socket_write (ThriftTransport *transport, const gpointer buf,
    +                    const guint32 len, GError **error)
    +{
    +  ThriftSSLSocket *ssl_socket = THRIFT_SSL_SOCKET (transport);
    +  gint ret = 0;
    +  guint sent = 0;
    +  if(THRIFT_SSL_SOCKET_GET_CLASS(ssl_socket)->handle_handshake(transport, 
error)){
    +
    +      ThriftSocket *socket = THRIFT_SSL_SOCKET (transport);
    +      g_return_val_if_fail (socket->sd != THRIFT_INVALID_SOCKET, FALSE);
    +
    +      while (sent < len)
    +   {
    +     ret = SSL_write (ssl_socket->ssl, (guint8 *)buf + sent, len - sent);
    +     if (ret < 0)
    +       {
    +         g_set_error (error, THRIFT_TRANSPORT_ERROR,
    +                      THRIFT_TRANSPORT_ERROR_SEND,
    +                      "failed to send %d bytes - %s", len, 
strerror(errno));
    +         return FALSE;
    +       }
    +     sent += ret;
    +   }
    +
    +  }
    +  return sent==len;
    +}
    +
    +/* implements thrift_transport_write_end
    + * called when write is complete.  nothing to do on our end. */
    +gboolean
    +thrift_ssl_socket_write_end (ThriftTransport *transport, GError **error)
    +{
    +  /* satisfy -Wall */
    +  THRIFT_UNUSED_VAR (transport);
    +  THRIFT_UNUSED_VAR (error);
    +  return TRUE;
    +}
    +
    +/* implements thrift_transport_flush
    + * flush pending data.  since we are not buffered, this is a no-op */
    +gboolean
    +thrift_ssl_socket_flush (ThriftTransport *transport, GError **error)
    +{
    +  ThriftSSLSocket *ssl_socket = THRIFT_SSL_SOCKET (transport);
    +  gint ret = 0;
    +  guint sent = 0;
    +  if(THRIFT_SSL_SOCKET_GET_CLASS(ssl_socket)->handle_handshake(transport, 
error)){
    +
    +      BIO* bio = SSL_get_wbio(ssl_socket->ssl);
    +      if (bio == NULL) {
    +     g_set_error (error, THRIFT_TRANSPORT_ERROR,
    +                  THRIFT_TRANSPORT_ERROR_SEND,
    +                  "failed to flush, wbio returned null");
    +     return FALSE;
    +      }
    +      if (BIO_flush(bio) != 1) {
    +     g_set_error (error, THRIFT_TRANSPORT_ERROR,
    +                  THRIFT_TRANSPORT_ERROR_SEND,
    +                  "failed to flush it returned error");
    +     return FALSE;
    +      }
    +
    +  }
    +  return TRUE;
    +}
    +
    +
    +gboolean
    +thrift_ssl_socket_handle_handshake(ThriftTransport * transport, GError 
**error)
    +{
    +  ThriftSSLSocket *ssl_socket = THRIFT_SSL_SOCKET (transport);
    +  ThriftSocket *socket = THRIFT_SOCKET (transport);
    +  g_return_val_if_fail (thrift_transport_is_open (transport), FALSE);
    +
    +  if(ssl_socket==NULL || ssl_socket->ssl!=NULL){
    +      return TRUE;
    +  }
    +
    +  
if(THRIFT_SSL_SOCKET_GET_CLASS(ssl_socket)->create_ssl_context(transport, 
error)){
    +      /*Context created*/
    +      SSL_set_fd(ssl_socket->ssl, socket->sd);
    +      int rc;
    +      if(ssl_socket->server){
    +     rc = SSL_accept(ssl_socket->ssl);
    +      }else{
    +     rc = SSL_connect(ssl_socket->ssl);
    +      }
    +      if (rc <= 0) {
    +     fprintf(stderr,"The error returned was %d\n", 
SSL_get_error(ssl_socket->ssl, rc));
    +     thrift_ssl_socket_get_error(error, "Not possible to connect", 
THRIFT_SSL_SOCKET_ERROR_CIPHER_NOT_AVAILABLE);
    +     return FALSE;
    +      }
    +  }else
    +    return FALSE;
    +
    +  return thrift_ssl_socket_authorize(transport, error);
    +}
    +
    +gboolean
    +thrift_ssl_socket_create_ssl_context(ThriftTransport * transport, GError 
**error)
    +{
    +  ThriftSSLSocket *socket = THRIFT_SSL_SOCKET (transport);
    +
    +  if(socket->ctx!=NULL){
    +      if(socket->ssl!=NULL) {
    +     return TRUE;
    +      }
    +
    +      socket->ssl = SSL_new(socket->ctx);
    +      if (socket->ssl == NULL) {
    +     g_set_error (error, THRIFT_TRANSPORT_ERROR,
    +                  THRIFT_SSL_SOCKET_ERROR_TRANSPORT,
    +                  "Unable to create SSL context");
    +     return FALSE;
    +      }
    +  }
    +
    +  return TRUE;
    +}
    +
    +/**
    + *
    + * @param ssl_socket The ssl socket
    + * @param file_name The file name of the PEM certificate chain
    + * @return
    + */
    +gboolean thrift_ssl_load_cert_from_file(ThriftSSLSocket *ssl_socket, const 
char *file_name)
    +{
    +  char error_buffer[255];
    +  if (!thrift_ssl_socket_openssl_initialized) {
    +      g_error("OpenSSL is not initialized yet");
    +      return FALSE;
    +  }
    +  int rc = SSL_CTX_load_verify_locations(ssl_socket->ctx, file_name, NULL);
    +  if (rc != 1) { /*verify authentication result*/
    +      ERR_error_string_n(ERR_get_error(), error_buffer, 254);
    +      g_warning("Load of certificates failed: %s!", error_buffer);
    +      return FALSE;
    +  }
    +  return TRUE;
    +}
    +
    +/**
    + * Load a certificate chain from memory
    + * @param ssl_socket the ssl socket
    + * @param chain_certs the buffer to load PEM from
    + * @return
    + */
    +gboolean thrift_ssl_load_cert_from_buffer(ThriftSSLSocket *ssl_socket, 
const char chain_certs[])
    +{
    +  gboolean retval = FALSE;
    +  /* Load chain of certs*/
    +  X509 *cacert=NULL;
    +  BIO *mem = BIO_new_mem_buf(chain_certs,strlen(chain_certs));
    +  X509_STORE *cert_store = SSL_CTX_get_cert_store(ssl_socket->ctx);
    +
    +  if(cert_store!=NULL){
    +      int index = 0;
    +      while ((cacert = PEM_read_bio_X509(mem, NULL, 0, NULL))!=NULL) {
    +     if(cacert) {
    +         g_debug("Our certificate name is %s", cacert->name);
    +         X509_STORE_add_cert(cert_store, cacert);
    +         X509_free(cacert);
    +         cacert=NULL;
    +     } /* Free immediately */
    +     index++;
    +      }
    +      retval=TRUE;
    +  }
    +  BIO_free(mem);
    +  return retval;
    +}
    +
    +gboolean
    +thrift_ssl_socket_authorize(ThriftTransport * transport, GError **error)
    +{
    +  ThriftSocket *socket = THRIFT_SOCKET (transport);
    +  ThriftSSLSocket *ssl_socket = THRIFT_SSL_SOCKET (transport);
    +  ThriftSSLSocketClass *cls = THRIFT_SSL_SOCKET_GET_CLASS(ssl_socket);
    +  gboolean authorization_result = FALSE;
    +  /* We still don't support it */
    +  if(cls!=NULL && ssl_socket->ssl!=NULL){
    +      int rc = SSL_get_verify_result(ssl_socket->ssl);
    +      if (rc != X509_V_OK) { /* verify authentication result */
    +     if(rc==X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT && 
ssl_socket->allow_selfsigned){
    --- End diff --
    
    The selfsigned verification can be also skipped by doing other kind of 
initialization in openssl. This is why we provide our own function. To make 
sure it will enforce the check. So I'm not happy with it but it's necessary for 
now. Hope we can remove in the near future.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to