Index: tls.c ================================================================== --- tls.c +++ tls.c @@ -643,15 +643,19 @@ } statePtr = (State *)Tcl_GetChannelInstanceData(chan); if (!SSL_is_init_finished(statePtr->ssl)) { int err = 0; + dprintf("Calling Tls_WaitForConnect"); ret = Tls_WaitForConnect(statePtr, &err); + dprintf("Tls_WaitForConnect returned: %i", ret); + if ((statePtr->flags & TLS_TCL_ASYNC) && err == EAGAIN) { dprintf("Async set and err = EAGAIN"); ret = 0; } + if (ret < 0) { CONST char *errStr = statePtr->err; Tcl_ResetResult(interp); Tcl_SetErrno(err); Index: tlsBIO.c ================================================================== --- tlsBIO.c +++ tlsBIO.c @@ -4,10 +4,31 @@ * Provides BIO layer to interface openssl to Tcl. */ #include "tlsInt.h" +#if OPENSSL_VERSION_NUMBER < 0x10100000L +#define BIO_get_data(bio) ((bio)->ptr) +#define BIO_get_init(bio) ((bio)->init) +#define BIO_get_shutdown(bio) ((bio)->shutdown) +#define BIO_set_data(bio, val) (bio)->ptr = (val) +#define BIO_set_init(bio, val) (bio)->init = (val) +#define BIO_set_shutdown(bio, val) (bio)->shutdown = (val) + +/* XXX: This assumes the variable being assigned to is BioMethods */ +#define BIO_meth_new(type_, name_) (BIO_METHOD *)Tcl_Alloc(sizeof(BIO_METHOD)); \ + memset(BioMethods, 0, sizeof(BIO_METHOD)); \ + BioMethods->type = type_; \ + BioMethods->name = name_; +#define BIO_meth_set_write(bio, val) (bio)->bwrite = val; +#define BIO_meth_set_read(bio, val) (bio)->bread = val; +#define BIO_meth_set_puts(bio, val) (bio)->bputs = val; +#define BIO_meth_set_ctrl(bio, val) (bio)->ctrl = val; +#define BIO_meth_set_create(bio, val) (bio)->create = val; +#define BIO_meth_set_destroy(bio, val) (bio)->destroy = val; +#endif + /* * Forward declarations */ static int BioWrite _ANSI_ARGS_ ((BIO *h, CONST char *buf, int num)); @@ -190,10 +211,11 @@ if (channelTypeVersion == TLS_CHANNEL_VERSION_2) { ret = ((Tcl_WriteRaw(chan, "", 0) >= 0) ? 1 : -1); } else { ret = ((Tcl_Flush(chan) == TCL_OK) ? 1 : -1); } + dprintf("BIO_CTRL_FLUSH returning value %li", ret); break; default: ret = 0; break; } Index: tlsIO.c ================================================================== --- tlsIO.c +++ tlsIO.c @@ -341,10 +341,11 @@ bytesRead = 0; goto input; } if (!SSL_is_init_finished(statePtr->ssl)) { + dprintf("Calling Tls_WaitForConnect"); bytesRead = Tls_WaitForConnect(statePtr, errorCodePtr); if (bytesRead <= 0) { dprintf("Got an error (bytesRead = %i)", bytesRead); if (*errorCodePtr == ECONNRESET) { @@ -435,10 +436,11 @@ *errorCodePtr = EAGAIN; goto output; } if (!SSL_is_init_finished(statePtr->ssl)) { + dprintf("Calling Tls_WaitForConnect"); written = Tls_WaitForConnect(statePtr, errorCodePtr); if (written <= 0) { dprintf("Tls_WaitForConnect returned %i (err = %i)", written, *errorCodePtr); goto output; @@ -739,21 +741,25 @@ Tcl_DeleteTimerHandler(statePtr->timer); statePtr->timer = (Tcl_TimerToken) NULL; } if (statePtr->flags & TLS_TCL_CALLBACK) { + dprintf("Returning 0 due to callback"); return 0; } - if (statePtr->flags & TLS_TCL_INIT - && !SSL_is_init_finished(statePtr->ssl)) { + if ((statePtr->flags & TLS_TCL_INIT) && !SSL_is_init_finished(statePtr->ssl)) { int errorCode = 0; + + dprintf("Calling Tls_WaitForConnect"); if (Tls_WaitForConnect(statePtr, &errorCode) <= 0 && errorCode == EAGAIN) { - dprintf("Async flag could be set (didn't check) and errorCode == EAGAIN"); + dprintf("Async flag could be set (didn't check) and errorCode == EAGAIN: Returning 0"); return 0; } } + + dprintf("Returning %i", mask); return mask; } /* @@ -940,11 +946,11 @@ *errorCodePtr = EAGAIN; return -1; } else { continue; } - } else if (err == 0) { + } else if (err <= 0) { if (SSL_in_init(statePtr->ssl)) { dprintf("SSL_in_init() is true"); } if (Tcl_Eof(statePtr->self)) { @@ -954,10 +960,11 @@ dprintf("Error from some reason other than our BIO, returning 0"); return 0; } } dprintf("CR! "); + statePtr->flags |= TLS_TCL_HANDSHAKE_FAILED; *errorCodePtr = ECONNRESET; return -1; } if (statePtr->flags & TLS_TCL_SERVER) { err = SSL_get_verify_result(statePtr->ssl); Index: tlsInt.h ================================================================== --- tlsInt.h +++ tlsInt.h @@ -50,10 +50,11 @@ #include #else #include #include #include +#include #endif #ifndef NO_TLS1_1 # ifndef SSL_OP_NO_TLSv1_1 # define NO_TLS1_1 @@ -260,27 +261,6 @@ int Tls_WaitForConnect _ANSI_ARGS_(( State *statePtr, int *errorCodePtr)); BIO * BIO_new_tcl _ANSI_ARGS_((State* statePtr, int flags)); -#if OPENSSL_VERSION_NUMBER < 0x10100000L -#define BIO_get_data(bio) ((bio)->ptr) -#define BIO_get_init(bio) ((bio)->init) -#define BIO_get_shutdown(bio) ((bio)->shutdown) -#define BIO_set_data(bio, val) (bio)->ptr = (val) -#define BIO_set_init(bio, val) (bio)->init = (val) -#define BIO_set_shutdown(bio, val) (bio)->shutdown = (val) - -/* XXX: This assumes the variable being assigned to is BioMethods */ -#define BIO_meth_new(type_, name_) (BIO_METHOD *)Tcl_Alloc(sizeof(BIO_METHOD)); \ - memset(BioMethods, 0, sizeof(BIO_METHOD)); \ - BioMethods->type = type_; \ - BioMethods->name = name_; -#define BIO_meth_set_write(bio, val) (bio)->bwrite = val; -#define BIO_meth_set_read(bio, val) (bio)->bread = val; -#define BIO_meth_set_puts(bio, val) (bio)->bputs = val; -#define BIO_meth_set_ctrl(bio, val) (bio)->ctrl = val; -#define BIO_meth_set_create(bio, val) (bio)->create = val; -#define BIO_meth_set_destroy(bio, val) (bio)->destroy = val; -#endif - #endif /* _TLSINT_H */