[ 
https://issues.apache.org/jira/browse/THRIFT-3369?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15191882#comment-15191882
 ] 

ASF GitHub Bot commented on THRIFT-3369:
----------------------------------------

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

    https://github.com/apache/thrift/pull/930#discussion_r55928808
  
    --- Diff: lib/c_glib/src/thrift/c_glib/transport/thrift_ssl_socket.c ---
    @@ -0,0 +1,700 @@
    +/*
    + * 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 <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>
    +
    +#define OPENSSL_VERSION_NO_THREAD_ID 0x10000000L
    +
    +
    +/* object properties */
    +enum _ThriftSSLSocketProperties
    +{
    +   PROP_THRIFT_SSL_SOCKET_CONTEXT = 3
    +};
    +
    +/* for errors coming from socket() and connect() */
    +// extern int errno;
    +
    +// To hold a global state management of openssl for all instances
    +static gboolean thrift_ssl_socket_openssl_initialized=FALSE;
    +static SSL_CTX* thrift_ssl_socket_global_context=NULL; // Should this be 
keept at class level?
    +
    +#if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_NO_THREAD_ID)
    +static unsigned long callbackThreadID() {
    +   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) {
    +           //              g_printf("We should lock thread %d\n", n);
    +           //mutexes[n].lock();
    +   } else {
    +           //              g_printf("We should unlock thread %d\n", n);
    +           //mutexes[n].unlock();
    +   }
    +}
    +
    +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?");
    +                           //                          int errno_copy = 
THRIFT_GET_SOCKET_ERROR;
    +                           //                          string errors;
    +                           //                          buildErrors(errors, 
errno_copy);
    +                           //                          throw 
TSSLException("SSL_peek: " + errors);
    +                   }
    +                   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;
    +                   // FIXME build error
    +                   //            string errors;
    +                   //            buildErrors(errors, errno_copy);
    +                   //            GlobalOutput(("SSL_shutdown: " + 
errors).c_str());
    +           }
    +           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;
    +                   //                      string errors;
    +                   //                      buildErrors(errors, errno_copy);
    +                   //                      throw TSSLException("SSL_read: 
" + errors);
    +           }
    +   }
    +   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");
    +                   //                          throw 
TSSLException("SSL_get_wbio returns NULL");
    --- End diff --
    
    Shall we return here ?


> Implement SSL/TLS support on C with c_glib
> ------------------------------------------
>
>                 Key: THRIFT-3369
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3369
>             Project: Thrift
>          Issue Type: Improvement
>          Components: C glib - Library
>    Affects Versions: 0.9.1, 0.9.2, 0.9.3, 1.0
>            Reporter: Gonzalo Aguilar
>              Labels: features, patch
>             Fix For: 0.10.0, 1.0
>
>
> Implement SSL/TLS based on plain openssl instead of going through the way 
> defined in THRIFT-1016. 
> This help us to maintain a reference implementation and later switch over GIO 
> or whatever is defined. But also does not add any other dependencies to the 
> project. So bare minimum installation for supporting SSL/TLS is there and 
> aligned with CPP counterpart. 



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to