Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit cd3d39a

Browse files
committed
Modernize the code a little
1 parent c428df9 commit cd3d39a

8 files changed

Lines changed: 125 additions & 51 deletions

src/SSLConnectionFailedException.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#import <ObjFW/OFConnectionFailedException.h>
2424

25+
OF_ASSUME_NONNULL_BEGIN
26+
2527
@class SSLSocket;
2628

2729
@interface SSLConnectionFailedException: OFConnectionFailedException
@@ -30,9 +32,16 @@
3032
long _verifyResult;
3133
}
3234

33-
@property (readonly) unsigned long SSLError;
34-
@property (readonly) long verifyResult;
35+
@property (readonly, nonatomic) unsigned long SSLError;
36+
@property (readonly, nonatomic) long verifyResult;
3537

38+
+ (instancetype)exceptionWithHost: (OFString *)host
39+
port: (uint16_t)port
40+
socket: (id)socket OF_UNAVAILABLE;
41+
+ (instancetype)exceptionWithHost: (OFString *)host
42+
port: (uint16_t)port
43+
socket: (id)socket
44+
errNo: (int)errNo OF_UNAVAILABLE;
3645
+ (instancetype)exceptionWithHost: (OFString *)host
3746
port: (uint16_t)port
3847
socket: (SSLSocket *)socket
@@ -42,6 +51,13 @@
4251
socket: (SSLSocket *)socket
4352
SSLError: (unsigned long)SSLError
4453
verifyResult: (long)verifyResult;
54+
- initWithHost: (OFString *)host
55+
port: (uint16_t)port
56+
socket: (SSLSocket *)socket OF_UNAVAILABLE;
57+
- initWithHost: (OFString *)host
58+
port: (uint16_t)port
59+
socket: (SSLSocket *)socket
60+
errNo: (int)errNo OF_UNAVAILABLE;
4561
- initWithHost: (OFString *)host
4662
port: (uint16_t)port
4763
socket: (SSLSocket *)socket
@@ -50,5 +66,7 @@
5066
port: (uint16_t)port
5167
socket: (SSLSocket *)socket
5268
SSLError: (unsigned long)SSLError
53-
verifyResult: (long)verifyResult;
69+
verifyResult: (long)verifyResult OF_DESIGNATED_INITIALIZER;
5470
@end
71+
72+
OF_ASSUME_NONNULL_END

src/SSLConnectionFailedException.m

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@
4242
@implementation SSLConnectionFailedException
4343
@synthesize SSLError = _SSLError, verifyResult = _verifyResult;
4444

45+
+ (instancetype)exceptionWithHost: (OFString *)host
46+
port: (uint16_t)port
47+
socket: (id)socket
48+
{
49+
OF_UNRECOGNIZED_SELECTOR
50+
}
51+
52+
+ (instancetype)exceptionWithHost: (OFString *)host
53+
port: (uint16_t)port
54+
socket: (id)socket
55+
errNo: (int)errNo
56+
{
57+
OF_UNRECOGNIZED_SELECTOR
58+
}
59+
4560
+ (instancetype)exceptionWithHost: (OFString *)host
4661
port: (uint16_t)port
4762
socket: (SSLSocket *)socket
@@ -69,16 +84,29 @@ + (instancetype)exceptionWithHost: (OFString *)host
6984

7085
- initWithHost: (OFString *)host
7186
port: (uint16_t)port
72-
socket: (SSLSocket *)socket
73-
SSLError: (unsigned long)SSLError
87+
socket: (id)socket
7488
{
75-
self = [super initWithHost: host
76-
port: port
77-
socket: socket];
89+
OF_INVALID_INIT_METHOD
90+
}
7891

79-
_SSLError = SSLError;
92+
- initWithHost: (OFString *)host
93+
port: (uint16_t)port
94+
socket: (id)socket
95+
errNo: (int)errNo
96+
{
97+
OF_INVALID_INIT_METHOD
98+
}
8099

81-
return self;
100+
- initWithHost: (OFString *)host
101+
port: (uint16_t)port
102+
socket: (SSLSocket *)socket
103+
SSLError: (unsigned long)SSLError
104+
{
105+
return [self initWithHost: host
106+
port: port
107+
socket: socket
108+
SSLError: SSLError
109+
verifyResult: 0];
82110
}
83111

84112
- initWithHost: (OFString *)host

src/SSLInvalidCertificateException.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@
2424
#import <ObjFW/OFString.h>
2525
#import <ObjFW/OFException.h>
2626

27+
OF_ASSUME_NONNULL_BEGIN
28+
2729
@interface SSLInvalidCertificateException: OFException
2830
{
2931
OFString *_reason;
3032
}
3133

3234
@property (readonly, nonatomic) OFString *reason;
3335

34-
+ exceptionWithReason: (OFString *)reason;
35-
- initWithReason: (OFString *)reason;
36+
+ (instancetype)exception;
37+
+ (instancetype)exceptionWithReason: (OFString *)reason;
38+
- init OF_UNAVAILABLE;
39+
- initWithReason: (OFString *)reason OF_DESIGNATED_INITIALIZER;
3640
@end
41+
42+
OF_ASSUME_NONNULL_END

src/SSLInvalidCertificateException.m

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,19 @@
3030
@implementation SSLInvalidCertificateException
3131
@synthesize reason = _reason;
3232

33-
+ exceptionWithReason: (OFString *)reason
33+
+ (instancetype)exception
34+
{
35+
OF_UNRECOGNIZED_SELECTOR
36+
}
37+
38+
+ (instancetype)exceptionWithReason: (OFString *)reason
3439
{
3540
return [[[self alloc] initWithReason: reason] autorelease];
3641
}
3742

3843
- init
3944
{
40-
@try {
41-
[self doesNotRecognizeSelector: _cmd];
42-
} @catch (id e) {
43-
[self release];
44-
@throw e;
45-
}
46-
47-
abort();
45+
OF_INVALID_INIT_METHOD
4846
}
4947

5048
- initWithReason: (OFString *)reason

src/SSLSocket.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#import <ObjFW/OFTCPSocket.h>
2727
#import <ObjFW/OFTLSSocket.h>
2828

29+
OF_ASSUME_NONNULL_BEGIN
30+
2931
@class X509Certificate;
3032

3133
@interface SSLSocket: OFTCPSocket <OFTLSSocket>
@@ -37,12 +39,14 @@
3739
bool _requestClientCertificatesEnabled;
3840
}
3941

40-
@property (getter=isRequestClientCertificatesEnabled)
42+
@property (nonatomic, getter=isRequestClientCertificatesEnabled)
4143
bool requestClientCertificatesEnabled;
44+
@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
45+
X509Certificate *peerCertificate;
4246

4347
- initWithSocket: (OFTCPSocket *)socket;
44-
- (void)SSL_super_close;
4548
- (OFDataArray *)channelBindingDataWithType: (OFString *)type;
46-
- (X509Certificate *)peerCertificate;
47-
- (void)verifyPeerCertificate;
49+
- (nullable X509Certificate *)peerCertificate;
4850
@end
51+
52+
OF_ASSUME_NONNULL_END

src/SSLSocket.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
of_mutex_unlock(&ssl_mutexes[n]);
9191
}
9292

93+
@interface SSLSocket ()
94+
- (void)SSL_super_close;
95+
@end
96+
9397
@implementation SSLSocket
9498
@synthesize delegate = _delegate, certificateFile = _certificateFile;
9599
@synthesize privateKeyFile = _privateKeyFile;
@@ -444,7 +448,7 @@ - (X509Certificate *)peerCertificate
444448
{
445449
X509 *certificate = SSL_get_peer_certificate(_SSL);
446450

447-
if (!certificate)
451+
if (certificate == NULL)
448452
return nil;
449453

450454
return [[[X509Certificate alloc]

src/X509Certificate.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#import <ObjFW/OFObject.h>
2727
#import <ObjFW/OFString.h>
2828

29-
@class OFDictionary;
29+
OF_ASSUME_NONNULL_BEGIN
3030

3131
/* OIDs: */
3232
#define OID_commonName @"2.5.4.3"
@@ -41,12 +41,15 @@
4141

4242
#define OID_SRVName @"1.3.6.1.5.5.7.8.7"
4343

44+
@class OFDictionary;
45+
4446
@interface X509OID: OFObject <OFCopying>
4547
{
4648
OFString *_string;
4749
}
4850

49-
- initWithUTF8String: (const char *)string;
51+
- init OF_UNAVAILABLE;
52+
- initWithUTF8String: (const char *)string OF_DESIGNATED_INITIALIZER;
5053
@end
5154

5255
@interface X509Certificate: OFObject
@@ -57,18 +60,17 @@
5760
OFDictionary *_subjectAlternativeName;
5861
}
5962

63+
@property (readonly, nonatomic) OFDictionary *issuer;
64+
@property (readonly, nonatomic) OFDictionary *subject;
65+
@property (readonly, nonatomic) OFDictionary *subjectAlternateName;
66+
67+
- init OF_UNAVAILABLE;
6068
- initWithFile: (OFString *)file;
6169
- initWithX509Struct: (X509 *)cert;
62-
- (OFDictionary *)issuer;
63-
- (OFDictionary *)subject;
64-
- (OFDictionary *)subjectAlternativeName;
6570
- (bool)hasCommonNameMatchingDomain: (OFString *)domain;
6671
- (bool)hasDNSNameMatchingDomain: (OFString *)domain;
6772
- (bool)hasSRVNameMatchingDomain: (OFString *)domain
6873
service: (OFString *)service;
69-
- (bool)X509_isAssertedDomain: (OFString *)asserted
70-
equalDomain: (OFString *)domain;
71-
- (OFDictionary *)X509_dictionaryFromX509Name: (X509_NAME *)name;
72-
- (X509OID *)X509_stringFromASN1Object: (ASN1_OBJECT *)obj;
73-
- (OFString *)X509_stringFromASN1String: (ASN1_STRING *)str;
7474
@end
75+
76+
OF_ASSUME_NONNULL_END

src/X509Certificate.m

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,27 @@
4848

4949
#import <ObjFW/macros.h>
5050

51+
OF_ASSUME_NONNULL_BEGIN
52+
53+
@interface X509Certificate ()
54+
- (bool)X509_isAssertedDomain: (OFString *)asserted
55+
equalDomain: (OFString *)domain;
56+
- (OFDictionary *)X509_dictionaryFromX509Name: (X509_NAME *)name;
57+
- (X509OID *)X509_stringFromASN1Object: (ASN1_OBJECT *)obj;
58+
- (OFString *)X509_stringFromASN1String: (ASN1_STRING *)str;
59+
@end
60+
61+
OF_ASSUME_NONNULL_END
62+
5163
@implementation X509Certificate
64+
- init
65+
{
66+
OF_INVALID_INIT_METHOD
67+
}
68+
5269
- initWithFile: (OFString *)path
5370
{
54-
self = [self init];
71+
self = [super init];
5572

5673
@try {
5774
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
@@ -75,7 +92,7 @@ @implementation X509Certificate
7592

7693
- initWithX509Struct: (X509 *)certificate
7794
{
78-
self = [self init];
95+
self = [super init];
7996

8097
@try {
8198
_certificate = X509_dup(certificate);
@@ -265,12 +282,9 @@ - (OFDictionary *)subjectAlternativeName
265282

266283
- (bool)hasCommonNameMatchingDomain: (OFString *)domain
267284
{
268-
OFString *name;
269285
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
270-
OFList *CNs = [[self subject] objectForKey: OID_commonName];
271-
OFEnumerator *enumerator = [CNs objectEnumerator];
272286

273-
while ((name = [enumerator nextObject]) != nil) {
287+
for (OFString *name in [[self subject] objectForKey: OID_commonName]) {
274288
if ([self X509_isAssertedDomain: name
275289
equalDomain: domain]) {
276290
[pool release];
@@ -284,13 +298,10 @@ - (bool)hasCommonNameMatchingDomain: (OFString *)domain
284298

285299
- (bool)hasDNSNameMatchingDomain: (OFString *)domain
286300
{
287-
OFString *name;
288301
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
289-
OFDictionary *SANs = [self subjectAlternativeName];
290-
OFList *assertedNames = [SANs objectForKey: @"dNSName"];
291-
OFEnumerator *enumerator = [assertedNames objectEnumerator];
292302

293-
while ((name = [enumerator nextObject]) != nil) {
303+
for (OFString *name in
304+
[[self subjectAlternativeName] objectForKey: @"dNSName"]) {
294305
if ([self X509_isAssertedDomain: name
295306
equalDomain: domain]) {
296307
[pool release];
@@ -306,20 +317,18 @@ - (bool)hasSRVNameMatchingDomain: (OFString *)domain
306317
service: (OFString *)service
307318
{
308319
size_t serviceLength;
309-
OFString *name;
310320
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
311321
OFDictionary *SANs = [self subjectAlternativeName];
312322
OFList *assertedNames = [[SANs objectForKey: @"otherName"]
313323
objectForKey: OID_SRVName];
314-
OFEnumerator *enumerator = [assertedNames objectEnumerator];
315324

316325
if (![service hasPrefix: @"_"])
317326
service = [service stringByPrependingString: @"_"];
318327

319328
service = [service stringByAppendingString: @"."];
320329
serviceLength = [service length];
321330

322-
while ((name = [enumerator nextObject]) != nil) {
331+
for (OFString *name in assertedNames) {
323332
if ([name hasPrefix: service]) {
324333
OFString *asserted;
325334
asserted = [name substringWithRange: of_range(
@@ -443,9 +452,14 @@ - (OFString *)X509_stringFromASN1String: (ASN1_STRING *)str
443452
@end
444453

445454
@implementation X509OID
455+
- init
456+
{
457+
OF_INVALID_INIT_METHOD
458+
}
459+
446460
- initWithUTF8String: (const char *)string
447461
{
448-
self = [self init];
462+
self = [super init];
449463

450464
@try {
451465
_string = [[OFString alloc] initWithUTF8String: string];

0 commit comments

Comments
 (0)