︙ | | |
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
-
+
|
self = [super init];
_certificateVerificationEnabled = true;
return self;
}
- initWithSocket: (OFTCPSocket*)socket
- initWithSocket: (OFTCPSocket *)socket
{
self = [self init];
@try {
if ((_socket = dup(socket->_socket)) < 0)
@throw [OFInitializationFailedException exception];
} @catch (id e) {
|
︙ | | |
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
-
+
|
[super dealloc];
if (SSL_ != NULL)
SSL_free(SSL_);
}
- (void)SSL_startTLSWithExpectedHost: (OFString*)host
- (void)SSL_startTLSWithExpectedHost: (OFString *)host
port: (uint16_t)port
{
of_string_encoding_t encoding;
if ((_SSL = SSL_new(ctx)) == NULL || SSL_set_fd(_SSL, _socket) != 1) {
unsigned long error = ERR_get_error();
|
︙ | | |
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
-
+
-
+
-
+
|
exceptionWithHost: host
port: port
socket: self
SSLError: error];
}
}
- (void)startTLSWithExpectedHost: (OFString*)host
- (void)startTLSWithExpectedHost: (OFString *)host
{
[self SSL_startTLSWithExpectedHost: host
port: 0];
}
- (void)connectToHost: (OFString*)host
- (void)connectToHost: (OFString *)host
port: (uint16_t)port
{
[super connectToHost: host
port: port];
[self SSL_startTLSWithExpectedHost: host
port: port];
}
- (instancetype)accept
{
SSLSocket *client = (SSLSocket*)[super accept];
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];
/* FIXME: Get a proper errno */
@throw [OFAcceptFailedException exceptionWithSocket: self
|
︙ | | |
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
-
+
|
}
- (void)SSL_super_close
{
[super close];
}
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
- (size_t)lowlevelReadIntoBuffer: (void *)buffer
length: (size_t)length
{
ssize_t ret;
if (length > INT_MAX)
@throw [OFOutOfRangeException exception];
|
︙ | | |
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
-
+
|
if (ret == 0)
_atEndOfStream = true;
return ret;
}
- (void)lowlevelWriteBuffer: (const void*)buffer
- (void)lowlevelWriteBuffer: (const void *)buffer
length: (size_t)length
{
if (length > INT_MAX)
@throw [OFOutOfRangeException exception];
if (_socket == INVALID_SOCKET)
@throw [OFNotOpenException exceptionWithObject: self];
|
︙ | | |
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
-
-
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
{
if (_SSL != NULL && SSL_pending(_SSL) > 0)
return true;
return [super hasDataInReadBuffer];
}
- (void)setCertificateFile: (OFString*)certificateFile
forSNIHost: (OFString*)SNIHost
- (void)setCertificateFile: (OFString *)certificateFile
forSNIHost: (OFString *)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (OFString*)certificateFileForSNIHost: (OFString*)SNIHost
- (OFString *)certificateFileForSNIHost: (OFString *)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (void)setPrivateKeyFile: (OFString*)privateKeyFile
forSNIHost: (OFString*)SNIHost
- (void)setPrivateKeyFile: (OFString *)privateKeyFile
forSNIHost: (OFString *)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (OFString*)privateKeyFileForSNIHost: (OFString*)SNIHost
- (OFString *)privateKeyFileForSNIHost: (OFString *)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (void)setPrivateKeyPassphrase: (const char *)privateKeyPassphrase
forSNIHost: (OFString *)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (const char *)privateKeyPassphraseForSNIHost: (OFString *)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (void)setPrivateKeyPassphrase: (const char*)privateKeyPassphrase
forSNIHost: (OFString*)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (const char*)privateKeyPassphraseForSNIHost: (OFString*)SNIHost
{
/* TODO */
OF_UNRECOGNIZED_SELECTOR
}
- (OFDataArray*)channelBindingDataWithType: (OFString*)type
- (OFDataArray *)channelBindingDataWithType: (OFString *)type
{
size_t length;
char buffer[64];
OFDataArray *data;
if (![type isEqual: @"tls-unique"])
@throw [OFInvalidArgumentException exception];
|
︙ | | |
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
-
+
|
data = [OFDataArray dataArray];
[data addItems: buffer
count: length];
return data;
}
- (X509Certificate*)peerCertificate
- (X509Certificate *)peerCertificate
{
X509 *certificate = SSL_get_peer_certificate(_SSL);
if (!certificate)
return nil;
return [[[X509Certificate alloc]
|
︙ | | |
︙ | | |
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
-
+
|
#import <ObjFW/OFList.h>
#import <ObjFW/OFMutableDictionary.h>
#import <ObjFW/OFString.h>
#import <ObjFW/macros.h>
@implementation X509Certificate
- initWithFile: (OFString*)path
- initWithFile: (OFString *)path
{
self = [self init];
@try {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFDataArray *data = [OFDataArray
dataArrayWithContentsOfFile: path];
|
︙ | | |
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
-
+
|
[self release];
@throw e;
}
return self;
}
- initWithX509Struct: (X509*)certificate
- initWithX509Struct: (X509 *)certificate
{
self = [self init];
@try {
_certificate = X509_dup(certificate);
if (_certificate == NULL)
@throw [OFInitializationFailedException
|
︙ | | |
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
-
+
-
+
-
+
-
+
|
if (_certificate != NULL)
X509_free(_certificate);
[super dealloc];
}
- (OFString*)description
- (OFString *)description
{
OFMutableString *ret = [OFMutableString string];
[ret appendFormat: @"Issuer: %@\n\n", [self issuer]];
[ret appendFormat: @"Subject: %@\n\n", [self subject]];
[ret appendFormat: @"SANs: %@", [self subjectAlternativeName]];
[ret makeImmutable];
return ret;
}
- (OFDictionary*)issuer
- (OFDictionary *)issuer
{
X509_NAME *name;
if (_issuer != nil)
return [[_issuer copy] autorelease];
name = X509_get_issuer_name(_certificate);
_issuer = [[self X509_dictionaryFromX509Name: name] retain];
return _issuer;
}
- (OFDictionary*)subject
- (OFDictionary *)subject
{
X509_NAME *name;
if (_subject != nil)
return [[_subject copy] autorelease];
name = X509_get_subject_name(_certificate);
_subject = [[self X509_dictionaryFromX509Name: name] retain];
return _subject;
}
- (OFDictionary*)subjectAlternativeName
- (OFDictionary *)subjectAlternativeName
{
OFAutoreleasePool *pool;
OFMutableDictionary *ret;
int i;
if (_subjectAlternativeName != nil)
return [[_subjectAlternativeName copy] autorelease];
|
︙ | | |
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
-
+
-
+
-
-
+
+
|
[ret makeImmutable];
_subjectAlternativeName = [ret retain];
return ret;
}
- (bool)hasCommonNameMatchingDomain: (OFString*)domain
- (bool)hasCommonNameMatchingDomain: (OFString *)domain
{
OFString *name;
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFList *CNs = [[self subject] objectForKey: OID_commonName];
OFEnumerator *enumerator = [CNs objectEnumerator];
while ((name = [enumerator nextObject]) != nil) {
if ([self X509_isAssertedDomain: name
equalDomain: domain]) {
[pool release];
return true;
}
}
[pool release];
return false;
}
- (bool)hasDNSNameMatchingDomain: (OFString*)domain
- (bool)hasDNSNameMatchingDomain: (OFString *)domain
{
OFString *name;
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFDictionary *SANs = [self subjectAlternativeName];
OFList *assertedNames = [SANs objectForKey: @"dNSName"];
OFEnumerator *enumerator = [assertedNames objectEnumerator];
while ((name = [enumerator nextObject]) != nil) {
if ([self X509_isAssertedDomain: name
equalDomain: domain]) {
[pool release];
return true;
}
}
[pool release];
return false;
}
- (bool)hasSRVNameMatchingDomain: (OFString*)domain
service: (OFString*)service
- (bool)hasSRVNameMatchingDomain: (OFString *)domain
service: (OFString *)service
{
size_t serviceLength;
OFString *name;
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFDictionary *SANs = [self subjectAlternativeName];
OFList *assertedNames = [[SANs objectForKey: @"otherName"]
objectForKey: OID_SRVName];
|
︙ | | |
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
-
-
+
+
|
}
}
[pool release];
return false;
}
- (bool)X509_isAssertedDomain: (OFString*)asserted
equalDomain: (OFString*)domain
- (bool)X509_isAssertedDomain: (OFString *)asserted
equalDomain: (OFString *)domain
{
/*
* In accordance with RFC 6125 this only allows a wildcard as the
* left-most label and matches only the left-most label with it.
* E.g. *.example.com matches foo.example.com,
* but not foo.bar.example.com
*/
|
︙ | | |
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
-
+
|
if (![asserted caseInsensitiveCompare: domain])
return true;
return false;
}
- (OFDictionary*)X509_dictionaryFromX509Name: (X509_NAME*)name
- (OFDictionary *)X509_dictionaryFromX509Name: (X509_NAME *)name
{
OFMutableDictionary *dict = [OFMutableDictionary dictionary];
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
int i, count = X509_NAME_entry_count(name);
for (i = 0; i < count; i++) {
X509OID *key;
|
︙ | | |
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
-
+
|
[pool release];
[dict makeImmutable];
return dict;
}
- (X509OID*)X509_stringFromASN1Object: (ASN1_OBJECT*)object
- (X509OID *)X509_stringFromASN1Object: (ASN1_OBJECT *)object
{
X509OID *ret;
int length, bufferLength = 256;
char *buffer = [self allocMemoryWithSize: bufferLength];
@try {
while ((length = OBJ_obj2txt(buffer, bufferLength, object,
|
︙ | | |
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
-
+
-
+
-
+
-
+
|
} @finally {
[self freeMemory: buffer];
}
return ret;
}
- (OFString*)X509_stringFromASN1String: (ASN1_STRING*)str
- (OFString *)X509_stringFromASN1String: (ASN1_STRING *)str
{
OFString *ret;
char *buffer;
if (ASN1_STRING_to_UTF8((unsigned char**)&buffer, str) < 0)
if (ASN1_STRING_to_UTF8((unsigned char **)&buffer, str) < 0)
@throw [OFInvalidEncodingException exception];
@try {
ret = [OFString stringWithUTF8String: buffer];
} @finally {
OPENSSL_free(buffer);
}
return ret;
}
@end
@implementation X509OID
- initWithUTF8String: (const char*)string
- initWithUTF8String: (const char *)string
{
self = [self init];
@try {
_string = [[OFString alloc] initWithUTF8String: string];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_string release];
[super dealloc];
}
- (OFString*)description
- (OFString *)description
{
char tmp[1024];
OBJ_obj2txt(tmp, sizeof(tmp), OBJ_txt2obj([_string UTF8String], 1), 0);
return [OFString stringWithUTF8String: tmp];
}
- (bool)isEqual: (id)object
|
︙ | | |