git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25866 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2008-01-05 09:06:12 +00:00
parent 8980e0d975
commit f050dae554

View file

@ -45,11 +45,8 @@
#include <gcrypt.h> #include <gcrypt.h>
#endif #endif
#undef HAVE_PTHREAD_H /* Set up locking callbacks for gcrypt so that it will be thread-safe.
#ifdef HAVE_PTHREAD_H */
#include <pthread.h>
GCRY_THREAD_OPTION_PTHREAD_IMPL;
#else
static int gcry_mutex_init (void **priv) static int gcry_mutex_init (void **priv)
{ {
NSLock *lock = [NSLock new]; NSLock *lock = [NSLock new];
@ -79,7 +76,7 @@ static struct gcry_thread_cbs gcry_threads_other = {
gcry_mutex_lock, gcry_mutex_lock,
gcry_mutex_unlock gcry_mutex_unlock
}; };
#endif
@interface GSTLS : NSObject @interface GSTLS : NSObject
{ {
@ -98,9 +95,9 @@ static struct gcry_thread_cbs gcry_threads_other = {
- (GSSocketInputStream*) input; - (GSSocketInputStream*) input;
- (GSSocketOutputStream*) output; - (GSSocketOutputStream*) output;
- (BOOL) bye; /* Close down the TLS session. */ - (void) bye; /* Close down the TLS session. */
- (BOOL) handshake; /* A handshake/hello is in progress. */ - (BOOL) handshake; /* A handshake/hello is in progress. */
- (BOOL) hello; /* Start up the TLS session. */ - (void) hello; /* Start up the TLS session handshake. */
- (int) read: (uint8_t *)buffer maxLength: (unsigned int)len; - (int) read: (uint8_t *)buffer maxLength: (unsigned int)len;
- (void) stream: (NSStream*)stream handleEvent: (NSStreamEvent)event; - (void) stream: (NSStream*)stream handleEvent: (NSStreamEvent)event;
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len; - (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len;
@ -178,34 +175,37 @@ static gnutls_anon_client_credentials_t anoncred;
{ {
beenHere = YES; beenHere = YES;
#ifdef HAVE_PTHREAD_H /* Make gcrypt thread-safe
gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); */
#else
gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_other); gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_other);
#endif /* Initialise gnutls
*/
gnutls_global_init (); gnutls_global_init ();
/* Allocate global credential information for anonymous tls
*/
gnutls_anon_allocate_client_credentials (&anoncred); gnutls_anon_allocate_client_credentials (&anoncred);
/* Enable gnutls logging via NSLog
*/
gnutls_global_set_log_function (GSTLSLog); gnutls_global_set_log_function (GSTLSLog);
// gnutls_global_set_log_level (11); // gnutls_global_set_log_level (11);
} }
#endif /* HAVE_GNUTLS */ #endif /* HAVE_GNUTLS */
} }
- (BOOL) bye - (void) bye
{ {
#if defined(HAVE_GNUTLS) #if defined(HAVE_GNUTLS)
if (handshake == NO) if (handshake == NO)
{ {
if (active == NO) if (active == NO)
{ {
return YES; return;
} }
active = NO; active = NO;
gnutls_bye (session, GNUTLS_SHUT_RDWR); gnutls_bye (session, GNUTLS_SHUT_RDWR);
} }
gnutls_deinit (session); gnutls_deinit (session);
#endif /* HAVE_GNUTLS */ #endif /* HAVE_GNUTLS */
return YES;
} }
- (void) dealloc - (void) dealloc
@ -219,25 +219,29 @@ static gnutls_anon_client_credentials_t anoncred;
return handshake; return handshake;
} }
- (BOOL) hello - (void) hello
{
if (active == NO)
{ {
#if defined(HAVE_GNUTLS) #if defined(HAVE_GNUTLS)
int ret; int ret;
if (active == YES)
{
return YES;
}
if (handshake == NO) if (handshake == NO)
{ {
/* Configure this session to support certificate based
* operation.
*/
gnutls_certificate_allocate_credentials (&certcred); gnutls_certificate_allocate_credentials (&certcred);
/* FIXME ... should get the trusted authority certificates
* from somewhere sensible to validate the remote end!
*/
gnutls_certificate_set_x509_trust_file gnutls_certificate_set_x509_trust_file
(certcred, "ca.pem", GNUTLS_X509_FMT_PEM); (certcred, "ca.pem", GNUTLS_X509_FMT_PEM);
/* Initialise session and set default priorities foir key exchange.
*/
gnutls_init (&session, GNUTLS_CLIENT); gnutls_init (&session, GNUTLS_CLIENT);
/* Use default priorities */
gnutls_set_default_priority (session); gnutls_set_default_priority (session);
/* /*
@ -254,31 +258,34 @@ static gnutls_anon_client_credentials_t anoncred;
} }
*/ */
/* Set certificate credentials for this session.
*/
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, certcred); gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, certcred);
/* Set transport layer to use our low level stream code.
*/
gnutls_transport_set_lowat (session, 0); gnutls_transport_set_lowat (session, 0);
gnutls_transport_set_pull_function (session, GSTLSPull); gnutls_transport_set_pull_function (session, GSTLSPull);
gnutls_transport_set_push_function (session, GSTLSPush); gnutls_transport_set_push_function (session, GSTLSPush);
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t)self); gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t)self);
}
/* Set flag to say we are now doing a handshake.
*/
handshake = YES; handshake = YES;
}
ret = gnutls_handshake (session); ret = gnutls_handshake (session);
if (ret < 0) if (ret < 0)
{ {
fprintf (stderr, "*** Handshake failed\n"); NSDebugMLog(@"NSThread", @"Handshake status %d", ret);
gnutls_perror (ret);
return NO;
} }
else else
{ {
handshake = NO; handshake = NO; // Handshake is now complete.
active = YES; active = YES; // The TLS session is now active.
printf ("- Handshake was completed\n");
} }
#endif /* HAVE_GNUTLS */ #endif /* HAVE_GNUTLS */
return YES; }
} }
- (id) initWithInput: (GSSocketInputStream*)i - (id) initWithInput: (GSSocketInputStream*)i