Artifact 53f8754aca56af1cfdb450900fbd2985d3e986aa1c6ef677bae76fb3b9004f5a:
- File
src/OFTLSStream.m
— part of check-in
[d30efa8bbf]
at
2021-11-13 13:04:13
on branch trunk
— Completely rework the TLS/SSL API
The previous API could never work cleanly and would always require
hacks, as it needed intercepting all interactions of OFTCPSocket with
the raw socket and did not work at all if the OFTCPSocket had anything
in its read buffer before starting the TLS handshake. This also could
not be fixed easily, as it would have required the object to contain two
read buffers, one for the unencrypted connection and one for the
encrypted connection. There was also no clean way to perform the
handshake in a non-blocking way.The new API is a lot cleaner and requires none of the hacks, but using
it requires slightly more work. But this is more than made up for by
making a fully asynchronous handshake possible. It uses the concept of a
stream wrapping another stream, meaning the entire connecting part is
being handled by OFTCPSocket and then the connected socket is passed off
to OFTLSStream to wrap it. This also makes for a lot cleaner separation
of concerns. (user: js, size: 3817) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFTLSStream.h" #import "OFDate.h" #import "OFNotImplementedException.h" Class OFTLSStreamImplementation = Nil; static const OFRunLoopMode handshakeRunLoopMode = @"OFTLSStreamHandshakeRunLoopMode"; @interface OFTLSStreamHandshakeDelegate: OFObject <OFTLSStreamDelegate> { @public bool _done; id _exception; } @end @implementation OFTLSStreamHandshakeDelegate - (void)dealloc { [_exception release]; [super dealloc]; } - (void)stream: (OFTLSStream *)stream didPerformClientHandshakeWithHost: (OFString *)host exception: (id)exception { _done = true; _exception = [exception retain]; } @end @implementation OFTLSStream @synthesize wrappedStream = _wrappedStream; @dynamic delegate; @synthesize verifiesCertificates = _verifiesCertificates; + (instancetype)alloc { if (self == [OFTLSStream class]) { if (OFTLSStreamImplementation != Nil) return [OFTLSStreamImplementation alloc]; @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; } return [super alloc]; } + (instancetype)streamWithStream: (OFStream <OFReadyForReadingObserving, OFReadyForWritingObserving> *)stream { return [[[self alloc] initWithStream: stream] autorelease]; } - (instancetype)init { OF_INVALID_INIT_METHOD } - (instancetype)initWithStream: (OFStream <OFReadyForReadingObserving, OFReadyForWritingObserving> *)stream { self = [super init]; @try { _wrappedStream = [stream retain]; _verifiesCertificates = true; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_wrappedStream release]; [super dealloc]; } - (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { OF_UNRECOGNIZED_SELECTOR } - (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { OF_UNRECOGNIZED_SELECTOR } - (bool)hasDataInReadBuffer { return (super.hasDataInReadBuffer || _wrappedStream.hasDataInReadBuffer); } - (bool)lowlevelIsAtEndOfStream { return _wrappedStream.atEndOfStream; } - (int)fileDescriptorForReading { return _wrappedStream.fileDescriptorForReading; } - (int)fileDescriptorForWriting { return _wrappedStream.fileDescriptorForWriting; } - (void)asyncPerformClientHandshakeWithHost: (OFString *)host { [self asyncPerformClientHandshakeWithHost: host runLoopMode: OFDefaultRunLoopMode]; } - (void)asyncPerformClientHandshakeWithHost: (OFString *)host runLoopMode: (OFRunLoopMode)runLoopMode { OF_UNRECOGNIZED_SELECTOR } - (void)performClientHandshakeWithHost: (OFString *)host { void *pool = objc_autoreleasePoolPush(); id <OFTLSStreamDelegate> delegate = _delegate; OFTLSStreamHandshakeDelegate *handshakeDelegate = [[[OFTLSStreamHandshakeDelegate alloc] init] autorelease]; OFRunLoop *runLoop = [OFRunLoop currentRunLoop]; _delegate = handshakeDelegate; [self asyncPerformClientHandshakeWithHost: host runLoopMode: handshakeRunLoopMode]; while (!handshakeDelegate->_done) [runLoop runMode: handshakeRunLoopMode beforeDate: nil]; /* Cleanup */ [runLoop runMode: handshakeRunLoopMode beforeDate: [OFDate date]]; _delegate = delegate; if (handshakeDelegate->_exception != nil) @throw handshakeDelegate->_exception; objc_autoreleasePoolPop(pool); } @end