@@ -1,6 +1,8 @@ +#include #include +#include #import #import "SSLSocket.h" @@ -35,14 +37,44 @@ @try { if ((ctx = SSL_CTX_new(SSLv23_method())) == NULL) @throw [OFInitializationFailedException newWithClass: isa]; -// if ((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & -// SSL_OP_NO_SSLv2) == 0) -// @throw [OFInitializationFailedException -// newWithClass: isa]; + if ((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & + SSL_OP_NO_SSLv2) == 0) + @throw [OFInitializationFailedException + newWithClass: isa]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithSocket: (OFTCPSocket*)socket +{ + self = [self init]; + + @try { + sock = dup(socket->sock); + + if ((ssl = SSL_new(ctx)) == NULL || !SSL_set_fd(ssl, sock)) { + close(sock); + sock = INVALID_SOCKET; + @throw [OFInitializationFailedException + newWithClass: isa]; + } + + SSL_set_connect_state(ssl); + + if (SSL_connect(ssl) != 1) { + close(sock); + sock = INVALID_SOCKET; + @throw [OFInitializationFailedException + newWithClass: isa]; + } } @catch (id e) { [self release]; @throw e; } @@ -67,21 +99,21 @@ { [super connectToHost: host onPort: port]; if ((ssl = SSL_new(ctx)) == NULL || !SSL_set_fd(ssl, sock)) { - [self close]; + [super close]; @throw [OFConnectionFailedException newWithClass: isa socket: self host: host port: port]; } SSL_set_connect_state(ssl); if (SSL_connect(ssl) != 1) { - [self close]; + [super close]; @throw [OFConnectionFailedException newWithClass: isa socket: self host: host port: port]; } @@ -90,19 +122,19 @@ - (SSLSocket*)accept { SSLSocket *newsock = (SSLSocket*)[super accept]; if ((ssl = SSL_new(ctx)) == NULL || !SSL_set_fd(ssl, sock)) { - [self close]; + [super close]; @throw [OFAcceptFailedException newWithClass: isa socket: self]; } SSL_set_accept_state(ssl); if (SSL_connect(ssl) != 1) { - [self close]; + [super close]; @throw [OFAcceptFailedException newWithClass: isa socket: self]; } return newsock;