Index: src/SSLSocket.m ================================================================== --- src/SSLSocket.m +++ src/SSLSocket.m @@ -1,7 +1,7 @@ /* - * Copyright (c) 2011, 2012, 2013, Jonathan Schleifer + * Copyright (c) 2011, 2012, 2013, 2014, Jonathan Schleifer * Copyright (c) 2011, Florian Zeitz * Copyright (c) 2011, Jos Kuijpers * * https://webkeks.org/git/?p=objopenssl.git * @@ -135,20 +135,23 @@ SSL_free(SSL_); } - (void)startTLS { - of_string_encoding_t encoding = [OFString nativeOSEncoding]; + of_string_encoding_t encoding; + if ((_SSL = SSL_new(ctx)) == NULL || !SSL_set_fd(_SSL, _socket)) { [super close]; @throw [OFConnectionFailedException exceptionWithHost: nil port: 0 socket: self]; } SSL_set_connect_state(_SSL); + + encoding = [OFString nativeOSEncoding]; if ((_privateKeyFile != nil && !SSL_use_PrivateKey_file(_SSL, [_privateKeyFile cStringWithEncoding: encoding], SSL_FILETYPE_PEM)) || (_certificateFile != nil && !SSL_use_certificate_file(_SSL, [_certificateFile @@ -171,12 +174,12 @@ [self startTLS]; } - (instancetype)accept { - of_string_encoding_t encoding = [OFString nativeOSEncoding]; SSLSocket *client = (SSLSocket*)[super accept]; + of_string_encoding_t encoding; if ((client->_SSL = SSL_new(ctx)) == NULL || !SSL_set_fd(client->_SSL, client->_socket)) { [client SSL_super_close]; @throw [OFAcceptFailedException exceptionWithSocket: self]; @@ -185,10 +188,11 @@ if (_requestsClientCertificates) SSL_set_verify(client->_SSL, SSL_VERIFY_PEER, NULL); SSL_set_accept_state(client->_SSL); + encoding = [OFString nativeOSEncoding]; if (!SSL_use_PrivateKey_file(client->_SSL, [_privateKeyFile cStringWithEncoding: encoding], SSL_FILETYPE_PEM) || !SSL_use_certificate_file(client->_SSL, [_certificateFile cStringWithEncoding: encoding], SSL_FILETYPE_PEM) || SSL_accept(client->_SSL) != 1) { @@ -224,11 +228,11 @@ @throw [OFNotConnectedException exceptionWithSocket: self]; if (_atEndOfStream) { OFReadFailedException *e; - e = [OFReadFailedException exceptionWithStream: self + e = [OFReadFailedException exceptionWithObject: self requestedLength: length]; #ifndef _WIN32 e->_errNo = ENOTCONN; #else e->_errNo = WSAENOTCONN; @@ -236,14 +240,14 @@ @throw e; } if ((ret = SSL_read(_SSL, buffer, (int)length)) < 0) { - if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ) + if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ) return 0; - @throw [OFReadFailedException exceptionWithStream: self + @throw [OFReadFailedException exceptionWithObject: self requestedLength: length]; } if (ret == 0) _atEndOfStream = true; @@ -261,11 +265,11 @@ @throw [OFNotConnectedException exceptionWithSocket: self]; if (_atEndOfStream) { OFWriteFailedException *e; - e = [OFWriteFailedException exceptionWithStream: self + e = [OFWriteFailedException exceptionWithObject: self requestedLength: length]; #ifndef _WIN32 e->_errNo = ENOTCONN; #else @@ -274,20 +278,20 @@ @throw e; } if (SSL_write(_SSL, buffer, (int)length) < length) - @throw [OFWriteFailedException exceptionWithStream: self + @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; } -- (size_t)numberOfBytesInReadBuffer +- (bool)hasDataInReadBuffer { - if (_SSL == NULL) - return [super numberOfBytesInReadBuffer]; + if (_SSL != NULL && SSL_pending(_SSL) > 0) + return true; - return [super numberOfBytesInReadBuffer] + SSL_pending(_SSL); + return [super hasDataInReadBuffer]; } - (void)setDelegate: (id )delegate { /* FIXME */