diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 6529452e13..c5fd115851 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,17 @@ past entries belonging to author(s): Viktor Szakats. */ +2009-07-18 19:46 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) + * contrib/hbssl/Makefile + * contrib/hbssl/hbssl.h + * contrib/hbssl/hbssl.ch + * contrib/hbssl/sslctx.c + * contrib/hbssl/ssl.c + + contrib/hbssl/sslbio.c + * contrib/hbssl/tests/test.prg + ! Fixed a few bugs and typos, so now the test works. + + Added very basic BIO interface. + 2009-07-18 14:35 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * contrib/hbssl/hbssl.hbc + External libraries added for *nixes, OS/2 and adjusted diff --git a/harbour/contrib/hbssl/Makefile b/harbour/contrib/hbssl/Makefile index 08fc62c23c..8594a7b7ce 100644 --- a/harbour/contrib/hbssl/Makefile +++ b/harbour/contrib/hbssl/Makefile @@ -27,6 +27,7 @@ HB_USER_CFLAGS += $(foreach d, $(HB_INC_OPENSSL_OK), -I$(d)) C_SOURCES=\ ssl.c \ + sslbio.c \ sslciph.c \ sslctx.c \ sslrand.c \ diff --git a/harbour/contrib/hbssl/hbssl.ch b/harbour/contrib/hbssl/hbssl.ch index e54521ba0a..e2afed5394 100644 --- a/harbour/contrib/hbssl/hbssl.ch +++ b/harbour/contrib/hbssl/hbssl.ch @@ -55,6 +55,8 @@ /* NOTE: This file is also used by C code. */ +#define HB_SSL_CTX_NEW_METHOD_UNKNOWN ( -2 ) +#define HB_SSL_CTX_NEW_METHOD_DEFAULT ( -1 ) #define HB_SSL_CTX_NEW_METHOD_SSLV2 0 #define HB_SSL_CTX_NEW_METHOD_SSLV2_SERVER 1 #define HB_SSL_CTX_NEW_METHOD_SSLV2_CLIENT 2 @@ -74,4 +76,17 @@ #define HB_SSLEAY_PLATFORM 3 #define HB_SSLEAY_DIR 4 +#define SSL_ERROR_NONE 0 +#define SSL_ERROR_SSL 1 +#define SSL_ERROR_WANT_READ 2 +#define SSL_ERROR_WANT_WRITE 3 +#define SSL_ERROR_WANT_X509_LOOKUP 4 +#define SSL_ERROR_SYSCALL 5 +#define SSL_ERROR_ZERO_RETURN 6 +#define SSL_ERROR_WANT_CONNECT 7 +#define SSL_ERROR_WANT_ACCEPT 8 + +#define BIO_NOCLOSE 0x00 +#define BIO_CLOSE 0x01 + #endif /* HBSSL_CH_ */ diff --git a/harbour/contrib/hbssl/hbssl.h b/harbour/contrib/hbssl/hbssl.h index 97b71fa711..e51d966c59 100644 --- a/harbour/contrib/hbssl/hbssl.h +++ b/harbour/contrib/hbssl/hbssl.h @@ -59,6 +59,9 @@ extern SSL_METHOD * hb_ssl_method_id_to_ptr( int n ); +extern void * hb_BIO_is( int iParam ); +extern BIO * hb_BIO_par( int iParam ); + extern void * hb_SSL_CTX_is( int iParam ); extern SSL_CTX * hb_SSL_CTX_par( int iParam ); diff --git a/harbour/contrib/hbssl/ssl.c b/harbour/contrib/hbssl/ssl.c index ab54504e6b..86c0ccdfa1 100644 --- a/harbour/contrib/hbssl/ssl.c +++ b/harbour/contrib/hbssl/ssl.c @@ -177,6 +177,22 @@ HB_FUNC( SSL_PENDING ) hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); } +HB_FUNC( SSL_SET_BIO ) +{ + BIO * rbio = ( BIO * ) hb_parptr( 2 ); + BIO * wbio = ( BIO * ) hb_parptr( 2 ); + + if( hb_SSL_is( 1 ) && rbio && wbio ) + { + SSL * ssl = hb_SSL_par( 1 ); + + if( ssl ) + SSL_set_bio( ssl, rbio, wbio ); + } + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + HB_FUNC( SSL_CONNECT ) { if( hb_SSL_is( 1 ) ) @@ -357,9 +373,9 @@ HB_FUNC( SSL_READ ) PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING ); int nRead; - if( pBuffer && HB_ISBYREF( 2 ) && HB_ISNUM( 3 ) ) + if( pBuffer && HB_ISBYREF( 2 ) ) { - nRead = hb_parni( 3 ); + nRead = HB_ISNUM( 3 ) ? hb_parni( 3 ) : ( int ) hb_parclen( 2 ); if( ( ULONG ) nRead <= hb_parcsiz( 2 ) ) { @@ -445,7 +461,7 @@ HB_FUNC( SSL_WRITE ) nLen = nWrite; } - hb_retni( SSL_read( ssl, ( void * ) hb_itemGetCPtr( pBuffer ), ( int ) nLen ) ); + hb_retni( SSL_write( ssl, hb_itemGetCPtr( pBuffer ), ( int ) nLen ) ); } } else @@ -488,7 +504,7 @@ HB_FUNC( SSL_GET_SSL_METHOD ) else if( method == SSLv23_method() ) nMethod = HB_SSL_CTX_NEW_METHOD_SSLV23; else if( method == SSLv23_server_method() ) nMethod = HB_SSL_CTX_NEW_METHOD_SSLV23_SERVER; else if( method == SSLv23_client_method() ) nMethod = HB_SSL_CTX_NEW_METHOD_SSLV23_CLIENT; - else nMethod = 0; + else nMethod = HB_SSL_CTX_NEW_METHOD_UNKNOWN; hb_retni( nMethod ); } diff --git a/harbour/contrib/hbssl/sslbio.c b/harbour/contrib/hbssl/sslbio.c new file mode 100644 index 0000000000..4a90fa35bc --- /dev/null +++ b/harbour/contrib/hbssl/sslbio.c @@ -0,0 +1,100 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * OpenSSL API (BIO) - Harbour interface. + * + * Copyright 2009 Viktor Szakats (harbour.01 syenar.hu) + * www - http://www.harbour-project.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/). + * + * As a special exception, the Harbour Project gives permission for + * additional uses of the text contained in its release of Harbour. + * + * The exception is that, if you link the Harbour libraries with other + * files to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the Harbour library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the Harbour + * Project under the name Harbour. If you copy code from other + * Harbour Project or Free Software Foundation releases into a copy of + * Harbour, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for Harbour, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. + * + */ + +#include "hbapi.h" +#include "hbapierr.h" + +#include "hbssl.h" + +void * hb_BIO_is( int iParam ) +{ + return hb_parptr( iParam ); +} + +BIO * hb_BIO_par( int iParam ) +{ + return ( BIO * ) hb_parptr( iParam ); +} + +HB_FUNC( BIO_NEW_SOCKET ) +{ + if( HB_ISNUM( 1 ) ) + hb_retptr( BIO_new_socket( hb_parni( 1 ), HB_ISNUM( 2 ) ? hb_parni( 2 ) : BIO_NOCLOSE ) ); + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + +HB_FUNC( BIO_NEW_DGRAM ) +{ + if( HB_ISNUM( 1 ) ) + hb_retptr( BIO_new_dgram( hb_parni( 1 ), HB_ISNUM( 2 ) ? hb_parni( 2 ) : BIO_NOCLOSE ) ); + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + +HB_FUNC( BIO_NEW_FD ) +{ + if( HB_ISNUM( 1 ) ) + hb_retptr( BIO_new_fd( hb_parni( 1 ), HB_ISNUM( 2 ) ? hb_parni( 2 ) : BIO_NOCLOSE ) ); + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + +HB_FUNC( BIO_FREE ) +{ + BIO * bio = hb_BIO_par( 1 ); + + if( bio ) + hb_retni( BIO_free( bio ) ); + else + hb_errRT_BASE( EG_ARG, 2010, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} diff --git a/harbour/contrib/hbssl/sslctx.c b/harbour/contrib/hbssl/sslctx.c index 3b7ee9c33c..1531767442 100644 --- a/harbour/contrib/hbssl/sslctx.c +++ b/harbour/contrib/hbssl/sslctx.c @@ -57,8 +57,8 @@ HB_FUNC( SSL_INIT ) { - SSL_load_error_strings(); SSL_library_init(); + SSL_load_error_strings(); } HB_FUNC( SSLEAY_VERSION ) @@ -132,7 +132,7 @@ HB_FUNC( SSL_CTX_NEW ) { void ** ph = ( void ** ) hb_gcAlloc( sizeof( SSL_CTX * ), SSL_CTX_release ); - SSL_CTX * ctx = SSL_CTX_new( hb_ssl_method_id_to_ptr( hb_parni( 1 ) ) ); + SSL_CTX * ctx = SSL_CTX_new( hb_ssl_method_id_to_ptr( HB_ISNUM( 1 ) ? hb_parni( 1 ) : HB_SSL_CTX_NEW_METHOD_DEFAULT ) ); * ph = ( void * ) ctx; diff --git a/harbour/contrib/hbssl/tests/test.prg b/harbour/contrib/hbssl/tests/test.prg index c7ace77cf9..2e89462b1d 100644 --- a/harbour/contrib/hbssl/tests/test.prg +++ b/harbour/contrib/hbssl/tests/test.prg @@ -7,24 +7,46 @@ * www - http://www.harbour-project.org */ +#include "simpleio.ch" + #include "hbssl.ch" PROCEDURE Main() LOCAL ssl_ctx LOCAL ssl + LOCAL bio LOCAL cipher LOCAL socket - LOCAL buffer := Space( 1000 ) + LOCAL buffer LOCAL bits + LOCAL tmp // hb_inetInit() + + ? "-------" + + socket := hb_inetCreate() + ? "INETTIMEOUT", hb_inetTimeout( socket, 500 ) + ? "INETCONN", hb_inetConnect( "www.fortify.net", 80, socket ) + ? "INETERR", hb_inetErrorCode( socket ) + ? "INETFD", hb_inetFD( socket ) + ? "INETSEND", hb_inetSend( socket, "GET / http/1.1" + hb_inetCRLF() + "Host: " + "www.syenar.hu" + hb_inetCRLF() + hb_inetCRLF() ) + ? "INETERR", hb_inetErrorCode( socket ) + buffer := Space( 1024 ) + ? "INETRECVALL", hb_inetRecvAll( socket, @buffer, Len( buffer ) ) + ? "BUFFER", ">" + AllTrim( buffer ) + "<" + ? "INETCLOSE", hb_inetClose( socket ) + + ? "-------" + socket := hb_inetCreate() ? hb_inetTimeout( socket, 500 ) ? hb_inetConnect( "www.fortify.net", 443, socket ) + ? hb_inetErrorCode( socket ) // @@ -46,8 +68,11 @@ PROCEDURE Main() ? "SSL_VERSION", SSL_VERSION( ssl ) ? "SSL_GET_VERSION", SSL_GET_VERSION( ssl ) + ? "INET FD", hb_inetFD( socket ) + ? "SSL_SET_FD", SSL_SET_FD( ssl, hb_inetFD( socket ) ) - ? "SSL_CONNECT", SSL_CONNECT( ssl ) + ? "SSL_CONNECT", tmp := SSL_CONNECT( ssl ) + ? "SSL_GET_ERROR", SSL_GET_ERROR( ssl, tmp ) ? "SSL_GET_CIPHER_BITS" , SSL_GET_CIPHER_BITS( ssl, @bits ), bits ? "SSL_GET_CIPHER_LIST" , SSL_GET_CIPHER_LIST( ssl ) @@ -58,11 +83,13 @@ PROCEDURE Main() ? "SSL_CIPHER_GET_NAME" , SSL_CIPHER_GET_NAME( cipher ) ? "SSL_CIPHER_GET_VERSION", SSL_CIPHER_GET_VERSION( cipher ) ? "SSL_CIPHER_GET_BITS" , SSL_CIPHER_GET_BITS( cipher, @bits ), bits - ? "SSL_CIPHER_DESCRIPTION", SSL_CIPHER_DESCRIPTION( cipher ) - - ? "SSL_WRITE", SSL_WRITE( ssl, "GET / http/1.1" ) - ? "SSL_READ", SSL_READ( ssl, @buffer ) +// ? "SSL_CIPHER_DESCRIPTION", SSL_CIPHER_DESCRIPTION( cipher ) + ? "SSL_WRITE", tmp := SSL_WRITE( ssl, "GET / http/1.1" + hb_inetCRLF() + "Host: " + "www.fortify.net" + hb_inetCRLF() + hb_inetCRLF() ) + ? "SSL_GET_ERROR", SSL_GET_ERROR( ssl, tmp ) + buffer := Space( 1024 ) + ? "SSL_READ", tmp := SSL_READ( ssl, @buffer ) + ? "SSL_GET_ERROR", SSL_GET_ERROR( ssl, tmp ) ? buffer ? hb_inetClose( socket )