Adjust to reworked exception API.

This commit is contained in:
Jonathan Schleifer 2013-06-23 12:55:23 +02:00
parent fd21d0c65d
commit bdd815ec47
5 changed files with 36 additions and 65 deletions

View file

@ -50,7 +50,7 @@ AS_IF([test x"$GOBJC" = x"yes"], [
OBJCFLAGS="$OBJCFLAGS -Wwrite-strings -Wpointer-arith" OBJCFLAGS="$OBJCFLAGS -Wwrite-strings -Wpointer-arith"
dnl We need -Wno-deprecated-declarations as OpenSSL is deprecated on dnl We need -Wno-deprecated-declarations as OpenSSL is deprecated on
dnl OS X. dnl OS X.
OBJCFLAGS="$OBJCFLAGS -Wno-deprecated-declarations" # -Werror OBJCFLAGS="$OBJCFLAGS -Wno-deprecated-declarations"
]) ])
BUILDSYS_INIT BUILDSYS_INIT

View file

@ -33,9 +33,7 @@
@property (readonly, assign) OFString *reason; @property (readonly, assign) OFString *reason;
#endif #endif
+ exceptionWithClass: (Class)class + exceptionWithReason: (OFString*)reason;
reason: (OFString*)reason; - initWithReason: (OFString*)reason;
- initWithClass: (Class)class
reason: (OFString*)reason;
- (OFString*)reason; - (OFString*)reason;
@end @end

View file

@ -28,14 +28,12 @@
#import <ObjFW/macros.h> #import <ObjFW/macros.h>
@implementation SSLInvalidCertificateException @implementation SSLInvalidCertificateException
+ exceptionWithClass: (Class)class + exceptionWithReason: (OFString*)reason
reason: (OFString*)reason
{ {
return [[[self alloc] initWithClass: class return [[[self alloc] initWithReason: reason] autorelease];
reason: reason] autorelease];
} }
- initWithClass: (Class)class - init
{ {
@try { @try {
[self doesNotRecognizeSelector: _cmd]; [self doesNotRecognizeSelector: _cmd];
@ -47,10 +45,9 @@
abort(); abort();
} }
- initWithClass: (Class)class - initWithReason: (OFString*)reason
reason: (OFString*)reason
{ {
self = [super initWithClass: class]; self = [super init];
@try { @try {
_reason = [reason copy]; _reason = [reason copy];
@ -72,8 +69,7 @@
- (OFString*)description - (OFString*)description
{ {
return [OFString stringWithFormat: return [OFString stringWithFormat:
@"Invalid certificate in class %@! Reason: %@", [self inClass], @"Invalid certificate! Reason: %@", _reason];
_reason];
} }
- (OFString*)reason - (OFString*)reason

View file

@ -140,10 +140,9 @@ locking_callback(int mode, int n, const char *file, int line)
if ((_SSL = SSL_new(ctx)) == NULL || !SSL_set_fd(_SSL, _socket)) { if ((_SSL = SSL_new(ctx)) == NULL || !SSL_set_fd(_SSL, _socket)) {
[super close]; [super close];
@throw [OFConnectionFailedException @throw [OFConnectionFailedException
exceptionWithClass: [self class] exceptionWithHost: nil
socket: self port: 0
host: nil socket: self];
port: 0];
} }
SSL_set_connect_state(_SSL); SSL_set_connect_state(_SSL);
@ -156,10 +155,9 @@ locking_callback(int mode, int n, const char *file, int line)
SSL_FILETYPE_PEM)) || SSL_connect(_SSL) != 1) { SSL_FILETYPE_PEM)) || SSL_connect(_SSL) != 1) {
[super close]; [super close];
@throw [OFConnectionFailedException @throw [OFConnectionFailedException
exceptionWithClass: [self class] exceptionWithHost: nil
socket: self port: 0
host: nil socket: self];
port: 0];
} }
} }
@ -169,15 +167,7 @@ locking_callback(int mode, int n, const char *file, int line)
[super connectToHost: host [super connectToHost: host
port: port]; port: port];
@try { [self startTLS];
[self startTLS];
} @catch (OFConnectionFailedException *e) {
@throw [OFConnectionFailedException
exceptionWithClass: [self class]
socket: self
host: host
port: port];
}
} }
- (instancetype)accept - (instancetype)accept
@ -187,8 +177,7 @@ locking_callback(int mode, int n, const char *file, int line)
if ((client->_SSL = SSL_new(ctx)) == NULL || if ((client->_SSL = SSL_new(ctx)) == NULL ||
!SSL_set_fd(client->_SSL, client->_socket)) { !SSL_set_fd(client->_SSL, client->_socket)) {
[client SSL_super_close]; [client SSL_super_close];
@throw [OFAcceptFailedException exceptionWithClass: [self class] @throw [OFAcceptFailedException exceptionWithSocket: self];
socket: self];
} }
if (_requestsClientCertificates) if (_requestsClientCertificates)
@ -202,8 +191,7 @@ locking_callback(int mode, int n, const char *file, int line)
[_certificateFile cStringWithEncoding: OF_STRING_ENCODING_NATIVE], [_certificateFile cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
SSL_FILETYPE_PEM) || SSL_accept(client->_SSL) != 1) { SSL_FILETYPE_PEM) || SSL_accept(client->_SSL) != 1) {
[client SSL_super_close]; [client SSL_super_close];
@throw [OFAcceptFailedException exceptionWithClass: [self class] @throw [OFAcceptFailedException exceptionWithSocket: self];
socket: self];
} }
return client; return client;
@ -228,18 +216,16 @@ locking_callback(int mode, int n, const char *file, int line)
ssize_t ret; ssize_t ret;
if (length > INT_MAX) if (length > INT_MAX)
@throw [OFOutOfRangeException exceptionWithClass: [self class]]; @throw [OFOutOfRangeException exception];
if (_socket == INVALID_SOCKET) if (_socket == INVALID_SOCKET)
@throw [OFNotConnectedException exceptionWithClass: [self class] @throw [OFNotConnectedException exceptionWithSocket: self];
socket: self];
if (_atEndOfStream) { if (_atEndOfStream) {
OFReadFailedException *e; OFReadFailedException *e;
e = [OFReadFailedException exceptionWithClass: [self class] e = [OFReadFailedException exceptionWithStream: self
stream: self requestedLength: length];
requestedLength: length];
#ifndef _WIN32 #ifndef _WIN32
e->_errNo = ENOTCONN; e->_errNo = ENOTCONN;
#else #else
@ -253,9 +239,8 @@ locking_callback(int mode, int n, const char *file, int line)
if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ) if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ)
return 0; return 0;
@throw [OFReadFailedException exceptionWithClass: [self class] @throw [OFReadFailedException exceptionWithStream: self
stream: self requestedLength: length];
requestedLength: length];
} }
if (ret == 0) if (ret == 0)
@ -268,18 +253,16 @@ locking_callback(int mode, int n, const char *file, int line)
length: (size_t)length length: (size_t)length
{ {
if (length > INT_MAX) if (length > INT_MAX)
@throw [OFOutOfRangeException exceptionWithClass: [self class]]; @throw [OFOutOfRangeException exception];
if (_socket == INVALID_SOCKET) if (_socket == INVALID_SOCKET)
@throw [OFNotConnectedException exceptionWithClass: [self class] @throw [OFNotConnectedException exceptionWithSocket: self];
socket: self];
if (_atEndOfStream) { if (_atEndOfStream) {
OFWriteFailedException *e; OFWriteFailedException *e;
e = [OFWriteFailedException exceptionWithClass: [self class] e = [OFWriteFailedException exceptionWithStream: self
stream: self requestedLength: length];
requestedLength: length];
#ifndef _WIN32 #ifndef _WIN32
e->_errNo = ENOTCONN; e->_errNo = ENOTCONN;
@ -291,9 +274,8 @@ locking_callback(int mode, int n, const char *file, int line)
} }
if (SSL_write(_SSL, buffer, (int)length) < length) if (SSL_write(_SSL, buffer, (int)length) < length)
@throw [OFWriteFailedException exceptionWithClass: [self class] @throw [OFWriteFailedException exceptionWithStream: self
stream: self requestedLength: length];
requestedLength: length];
} }
- (size_t)numberOfBytesInReadBuffer - (size_t)numberOfBytesInReadBuffer
@ -365,9 +347,7 @@ locking_callback(int mode, int n, const char *file, int line)
OFDataArray *data; OFDataArray *data;
if (![type isEqual: @"tls-unique"]) if (![type isEqual: @"tls-unique"])
@throw [OFInvalidArgumentException @throw [OFInvalidArgumentException exception];
exceptionWithClass: [self class]
selector: _cmd];
if (SSL_session_reused(_SSL) ^ !_listening) { if (SSL_session_reused(_SSL) ^ !_listening) {
/* /*
@ -407,12 +387,10 @@ locking_callback(int mode, int n, const char *file, int line)
const char *tmp = X509_verify_cert_error_string(ret); const char *tmp = X509_verify_cert_error_string(ret);
OFString *reason = [OFString stringWithUTF8String: tmp]; OFString *reason = [OFString stringWithUTF8String: tmp];
@throw [SSLInvalidCertificateException @throw [SSLInvalidCertificateException
exceptionWithClass: [self class] exceptionWithReason: reason];
reason: reason];
} }
} else } else
@throw [SSLInvalidCertificateException @throw [SSLInvalidCertificateException
exceptionWithClass: [self class] exceptionWithReason: @"No certificate"];
reason: @"No certificate"];
} }
@end @end

View file

@ -53,7 +53,7 @@
_certificate = d2i_X509(NULL, &dataCArray, [data count]); _certificate = d2i_X509(NULL, &dataCArray, [data count]);
if (_certificate == NULL) if (_certificate == NULL)
@throw [OFInitializationFailedException @throw [OFInitializationFailedException
exceptionWithClass: [self class]]; exceptionWithClass: [self class]];
[pool release]; [pool release];
} @catch (id e) { } @catch (id e) {
@ -72,7 +72,7 @@
_certificate = X509_dup(certificate); _certificate = X509_dup(certificate);
if (_certificate == NULL) if (_certificate == NULL)
@throw [OFInitializationFailedException @throw [OFInitializationFailedException
exceptionWithClass: [self class]]; exceptionWithClass: [self class]];
} @catch (id e) { } @catch (id e) {
[self release]; [self release];
@throw e; @throw e;
@ -421,8 +421,7 @@
char *buffer; char *buffer;
if (ASN1_STRING_to_UTF8((unsigned char**)&buffer, str) < 0) if (ASN1_STRING_to_UTF8((unsigned char**)&buffer, str) < 0)
@throw [OFInvalidEncodingException @throw [OFInvalidEncodingException exception];
exceptionWithClass: [self class]];
@try { @try {
ret = [OFString stringWithUTF8String: buffer]; ret = [OFString stringWithUTF8String: buffer];