Conform to OFTLSSocket.

Still a few FIXMEs / TODOs.
This commit is contained in:
Jonathan Schleifer 2013-03-31 12:04:53 +02:00
parent 89c705d9ed
commit ecbaa8ed20
3 changed files with 64 additions and 72 deletions

View file

@ -21,12 +21,12 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <stdlib.h>
#import "SSLInvalidCertificateException.h" #import "SSLInvalidCertificateException.h"
#import <ObjFW/macros.h> #import <ObjFW/macros.h>
#import <ObjFW/OFNotImplementedException.h>
@implementation SSLInvalidCertificateException @implementation SSLInvalidCertificateException
+ exceptionWithClass: (Class)class + exceptionWithClass: (Class)class
reason: (OFString*)reason reason: (OFString*)reason
@ -37,10 +37,14 @@
- initWithClass: (Class)class - initWithClass: (Class)class
{ {
Class c = [self class]; @try {
[self doesNotRecognizeSelector: _cmd];
} @catch (id e) {
[self release]; [self release];
@throw [OFNotImplementedException exceptionWithClass: c @throw e;
selector: _cmd]; }
abort();
} }
- initWithClass: (Class)class - initWithClass: (Class)class

View file

@ -24,31 +24,24 @@
#include <openssl/ssl.h> #include <openssl/ssl.h>
#import <ObjFW/OFTCPSocket.h> #import <ObjFW/OFTCPSocket.h>
#import <ObjFW/OFTLSSocket.h>
@class X509Certificate; @class X509Certificate;
@interface SSLSocket: OFTCPSocket @interface SSLSocket: OFTCPSocket <OFTLSSocket>
{ {
SSL *_SSL; SSL *_SSL;
OFString *_privateKeyFile, *_certificateFile; OFString *_certificateFile, *_privateKeyFile;
const char *_privateKeyPassphrase;
bool _requestsClientCertificates; bool _requestsClientCertificates;
} }
#ifdef OF_HAVE_PROPERTIES #ifdef OF_HAVE_PROPERTIES
@property (copy) OFString *privateKeyFile, *certificateFile;
@property bool requestsClientCertificates; @property bool requestsClientCertificates;
#endif #endif
- initWithSocket: (OFTCPSocket*)socket; - initWithSocket: (OFTCPSocket*)socket;
- initWithSocket: (OFTCPSocket*)socket
privateKeyFile: (OFString*)privateKeyFile
certificateFile: (OFString*)certificateFile;
- (void)SSL_super_close; - (void)SSL_super_close;
- (SSLSocket*)accept; /* Changes the return type */
- (void)setPrivateKeyFile: (OFString*)file;
- (OFString*)privateKeyFile;
- (void)setCertificateFile: (OFString*)file;
- (OFString*)certificateFile;
- (void)setRequestsClientCertificates: (bool)enabled; - (void)setRequestsClientCertificates: (bool)enabled;
- (bool)requestsClientCertificates; - (bool)requestsClientCertificates;
- (OFDataArray*)channelBindingDataWithType: (OFString*)type; - (OFDataArray*)channelBindingDataWithType: (OFString*)type;

View file

@ -114,53 +114,11 @@ locking_callback(int mode, int n, const char *file, int line)
} }
- initWithSocket: (OFTCPSocket*)socket - initWithSocket: (OFTCPSocket*)socket
{
return [self initWithSocket: socket
privateKeyFile: nil
certificateFile: nil];
}
- initWithSocket: (OFTCPSocket*)socket
privateKeyFile: (OFString*)privateKeyFile
certificateFile: (OFString*)certificateFile
{ {
self = [self init]; self = [self init];
@try {
/* FIXME: Also allow with accepted sockets */
_privateKeyFile = [privateKeyFile copy];
_certificateFile = [certificateFile copy];
_socket = dup(socket->_socket); _socket = dup(socket->_socket);
if ((_SSL = SSL_new(ctx)) == NULL ||
!SSL_set_fd(_SSL, _socket)) {
close(_socket);
_socket = INVALID_SOCKET;
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
}
SSL_set_connect_state(_SSL);
if ((_privateKeyFile != nil && !SSL_use_PrivateKey_file(_SSL,
[_privateKeyFile cStringWithEncoding:
OF_STRING_ENCODING_NATIVE], SSL_FILETYPE_PEM)) ||
(_certificateFile != nil && !SSL_use_certificate_file(_SSL,
[_certificateFile cStringWithEncoding:
OF_STRING_ENCODING_NATIVE], SSL_FILETYPE_PEM)) ||
SSL_connect(_SSL) != 1) {
close(_socket);
_socket = INVALID_SOCKET;
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
}
} @catch (id e) {
[self release];
@throw e;
}
return self; return self;
} }
@ -177,19 +135,15 @@ locking_callback(int mode, int n, const char *file, int line)
SSL_free(SSL_); SSL_free(SSL_);
} }
- (void)connectToHost: (OFString*)host - (void)startTLS
port: (uint16_t)port
{ {
[super connectToHost: host
port: port];
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] exceptionWithClass: [self class]
socket: self socket: self
host: host host: nil
port: port]; port: 0];
} }
SSL_set_connect_state(_SSL); SSL_set_connect_state(_SSL);
@ -201,6 +155,23 @@ locking_callback(int mode, int n, const char *file, int line)
cStringWithEncoding: OF_STRING_ENCODING_NATIVE], cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
SSL_FILETYPE_PEM)) || SSL_connect(_SSL) != 1) { SSL_FILETYPE_PEM)) || SSL_connect(_SSL) != 1) {
[super close]; [super close];
@throw [OFConnectionFailedException
exceptionWithClass: [self class]
socket: self
host: nil
port: 0];
}
}
- (void)connectToHost: (OFString*)host
port: (uint16_t)port
{
[super connectToHost: host
port: port];
@try {
[self startTLS];
} @catch (OFConnectionFailedException *e) {
@throw [OFConnectionFailedException @throw [OFConnectionFailedException
exceptionWithClass: [self class] exceptionWithClass: [self class]
socket: self socket: self
@ -209,7 +180,7 @@ locking_callback(int mode, int n, const char *file, int line)
} }
} }
- (SSLSocket*)accept - (instancetype)accept
{ {
SSLSocket *client = (SSLSocket*)[super accept]; SSLSocket *client = (SSLSocket*)[super accept];
@ -333,14 +304,17 @@ locking_callback(int mode, int n, const char *file, int line)
return [super numberOfBytesInReadBuffer] + SSL_pending(_SSL); return [super numberOfBytesInReadBuffer] + SSL_pending(_SSL);
} }
- (void)setPrivateKeyFile: (OFString*)privateKeyFile - (void)setDelegate: (id <OFTLSSocketDelegate>)delegate
{ {
OF_SETTER(_privateKeyFile, privateKeyFile, true, 1) /* FIXME */
[self doesNotRecognizeSelector: _cmd];
abort();
} }
- (OFString*)privateKeyFile - (id <OFTLSSocketDelegate>)delegate
{ {
OF_GETTER(_privateKeyFile, true) /* FIXME */
return nil;
} }
- (void)setCertificateFile: (OFString*)certificateFile - (void)setCertificateFile: (OFString*)certificateFile
@ -353,6 +327,27 @@ locking_callback(int mode, int n, const char *file, int line)
OF_GETTER(_certificateFile, true) OF_GETTER(_certificateFile, true)
} }
- (void)setPrivateKeyFile: (OFString*)privateKeyFile
{
OF_SETTER(_privateKeyFile, privateKeyFile, true, 1)
}
- (OFString*)privateKeyFile
{
OF_GETTER(_privateKeyFile, true)
}
- (void)setPrivateKeyPassphrase: (const char*)privateKeyPassphrase
{
/* FIXME */
}
- (const char*)privateKeyPassphrase
{
/* FIXME */
return NULL;
}
- (void)setRequestsClientCertificates: (bool)enabled - (void)setRequestsClientCertificates: (bool)enabled
{ {
_requestsClientCertificates = enabled; _requestsClientCertificates = enabled;