ObjOpenSSL  Diff

Differences From Artifact [79d37925ec]:

To Artifact [d7426f238d]:


1
2
3

4
5
6
7
8
9
10
1
2

3
4
5
6
7
8
9
10


-
+







/*
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 * Copyright (c) 2011, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, 2013, Jonathan Schleifer <js@webkeks.org>
 *
 * https://webkeks.org/git/?p=objopenssl.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
36
37
38
39
40
41
42
43

44
45
46
47
48
49
50
51

52

53

54

55
56

57
58


59
60
61
62
63
64
65
66
67

68
69
70
71
72
73


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88



89
90
91


92
93
94
95
96
97
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
151
152

153
154
155
156
157
158
159
36
37
38
39
40
41
42

43
44
45
46
47
48



49

50
51
52

53


54
55
56
57
58
59
60
61
62
63
64
65
66

67
68
69
70
71


72
73
74
75
76
77
78
79
80
81
82
83
84
85



86
87
88
89


90
91
92
93
94
95
96
97
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
151
152

153
154
155
156
157
158
159
160







-
+





-
-
-
+
-
+

+
-
+
-
-
+


+
+








-
+




-
-
+
+












-
-
-
+
+
+

-
-
+
+




















-
-
+
+

-
-
+
+

-
+






-
-
+
+

-
-
+
+

-
+








-
-
+
+





-
+
+




-
+







#import <ObjFW/OFList.h>
#import <ObjFW/OFMutableDictionary.h>
#import <ObjFW/OFString.h>

#import <ObjFW/macros.h>

@implementation X509Certificate
- initWithFile: (OFString*)file
- initWithFile: (OFString*)path
{
	self = [self init];

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFFile *fd = [OFFile fileWithPath: file
					     mode: @"r"];
		OFDataArray *data = [fd readDataArrayTillEndOfStream];
		OFDataArray *data = [OFDataArray
		[fd close];
		    dataArrayWithContentsOfFile: path];
		const unsigned char *dataCArray = [data items];

		crt = d2i_X509(NULL, &dataCArray, [data count]);
		_certificate = d2i_X509(NULL, &dataCArray, [data count]);
		[pool release];
		if (crt == NULL)
		if (_certificate == NULL)
			@throw [OFInitializationFailedException
				    exceptionWithClass: [self class]];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithX509Struct: (X509*)cert
- initWithX509Struct: (X509*)certificate
{
	self = [self init];

	@try {
		crt = X509_dup(cert);
		if (crt == NULL)
		_certificate = X509_dup(certificate);
		if (_certificate == NULL)
			@throw [OFInitializationFailedException
				    exceptionWithClass: [self class]];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[issuer release];
	[subject release];
	[subjectAlternativeName release];
	[_issuer release];
	[_subject release];
	[_subjectAlternativeName release];

	if (crt != NULL)
		X509_free(crt);
	if (_certificate != NULL)
		X509_free(_certificate);

	[super dealloc];
}

- (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
{
	X509_NAME *name;

	if (issuer != nil)
		return [[issuer copy] autorelease];
	if (_issuer != nil)
		return [[_issuer copy] autorelease];

	name = X509_get_issuer_name(crt);
	issuer = [[self X509_dictionaryFromX509Name: name] retain];
	name = X509_get_issuer_name(_certificate);
	_issuer = [[self X509_dictionaryFromX509Name: name] retain];

	return issuer;
	return _issuer;
}

- (OFDictionary*)subject
{
	X509_NAME *name;

	if (subject != nil)
		return [[subject copy] autorelease];
	if (_subject != nil)
		return [[_subject copy] autorelease];

	name = X509_get_subject_name(crt);
	subject = [[self X509_dictionaryFromX509Name: name] retain];
	name = X509_get_subject_name(_certificate);
	_subject = [[self X509_dictionaryFromX509Name: name] retain];

	return subject;
	return _subject;
}

- (OFDictionary*)subjectAlternativeName
{
	OFAutoreleasePool *pool;
	OFMutableDictionary *ret;
	int i;

	if (subjectAlternativeName != nil)
		return [[subjectAlternativeName copy] autorelease];
	if (_subjectAlternativeName != nil)
		return [[_subjectAlternativeName copy] autorelease];

	ret = [OFMutableDictionary dictionary];
	pool = [[OFAutoreleasePool alloc] init];

	i = -1;
	while ((i = X509_get_ext_by_NID(crt, NID_subject_alt_name, i)) != -1) {
	while ((i = X509_get_ext_by_NID(_certificate,
	    NID_subject_alt_name, i)) != -1) {
		X509_EXTENSION *extension;
		STACK_OF(GENERAL_NAME) *values;
		int j, count;

		if ((extension = X509_get_ext(crt, i)) == NULL)
		if ((extension = X509_get_ext(_certificate, i)) == NULL)
			break;

		if ((values = X509V3_EXT_d2i(extension)) == NULL)
			break;

		count = sk_GENERAL_NAME_num(values);
		for (j = 0; j < count; j++) {
244
245
246
247
248
249
250
251

252
253
254
255
256
257
258
245
246
247
248
249
250
251

252
253
254
255
256
257
258
259







-
+







		i++; /* Next extension */
		[pool releaseObjects];
	}

	[pool release];

	[ret makeImmutable];
	subjectAlternativeName = [ret retain];
	_subjectAlternativeName = [ret retain];

	return ret;
}

- (BOOL)hasCommonNameMatchingDomain: (OFString*)domain
{
	OFString *name;
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
474
475

476
477
478
479
480
481
482
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
474
475

476
477
478
479
480
481
482
483







-
+




-
+










-
+






-
+







-
+






-
+







	}

	return ret;
}
@end

@implementation X509OID
- initWithUTF8String: (const char*) str
- initWithUTF8String: (const char*)string
{
	self = [self init];

	@try {
		string = [[OFString alloc] initWithUTF8String: str];
		_string = [[OFString alloc] initWithUTF8String: string];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[string release];
	[_string release];
	[super dealloc];
}

- (OFString*)description
{
	char tmp[1024];
	OBJ_obj2txt(tmp, sizeof(tmp), OBJ_txt2obj([string UTF8String], 1), 0);
	OBJ_obj2txt(tmp, sizeof(tmp), OBJ_txt2obj([_string UTF8String], 1), 0);
	return [OFString stringWithUTF8String: tmp];
}

- (BOOL)isEqual: (id)object
{
	if (([object isKindOfClass: [OFString class]]) ||
	    ([object isKindOfClass: [X509OID class]]))
		return [object isEqual: string];
		return [object isEqual: _string];

	return NO;
}

- (uint32_t)hash
{
	return [string hash];
	return [_string hash];
}

- copy
{
	return [self retain];
}
@end