ObjOpenSSL  Check-in [ce85c1fa59]

Overview
Comment:Implement setting the private key and certificate.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ce85c1fa591a7455cbb55ff44932b4ff8059e2db4aba614809039aafc1eaae88
User & Date: js on 2011-03-28 22:23:32
Other Links: manifest | tags
Context
2011-03-28
23:59
Fix stupid typo in Makefile. check-in: 10eba7be60 user: js tags: trunk
22:23
Implement setting the private key and certificate. check-in: ce85c1fa59 user: js tags: trunk
21:38
Have one global ctx. check-in: daea63b67c user: js tags: trunk
Changes

Modified src/SSLSocket.h from [2bdb858cf2] to [9b9ce91e67].

1
2
3
4
5
6
7


8
9
10




11

12
13




14
1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
19
20
21
22
23
24







+
+


-
+
+
+
+

+


+
+
+
+

#include <openssl/ssl.h>

#import <ObjFW/OFTCPSocket.h>

@interface SSLSocket: OFTCPSocket
{
	SSL *ssl;
	OFString *privateKeyFile;
	OFString *certificateFile;
}

- initWithSocket: (OFTCPSocket*)socket;
#ifdef OF_HAVE_PROPERTIES
@property (copy) OFString *privateKeyFile;
@property (copy) OFString *certificateFile;
#endif

- initWithSocket: (OFTCPSocket*)socket;
/* Change the return type */
- (SSLSocket*)accept;
- (void)setPrivateKeyFile: (OFString*)file;
- (OFString*)privateKeyFile;
- (void)setCertificateFile: (OFString*)file;
- (OFString*)certificateFile;
@end

Modified src/SSLSocket.m from [cc5dffdd1c] to [3aca96c73c].

71
72
73
74
75
76
77



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







+
+
+







}

- (void)dealloc
{
	SSL_CTX *ctx_ = ctx;
	SSL *ssl_ = ssl;

	[privateKeyFile release];
	[certificateFile release];

	[super dealloc];

	if (ssl_ != NULL)
		SSL_free(ssl_);
	if (ctx_ != NULL)
		SSL_CTX_free(ctx_);
}
108
109
110
111
112
113
114
115
116







117
118
119
120
121

122



123
124






125
126
127
128
129
130
131
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







-
-
+
+
+
+
+
+
+




-
+

+
+
+
-
-
+
+
+
+
+
+







	}
}

- (SSLSocket*)accept
{
	SSLSocket *newsock = (SSLSocket*)[super accept];

	if ((ssl = SSL_new(ctx)) == NULL || !SSL_set_fd(ssl, sock)) {
		[super close];
	if ((newsock->ssl = SSL_new(ctx)) == NULL ||
	    !SSL_set_fd(newsock->ssl, newsock->sock)) {
		/* We only want to close the OFTCPSocket */
		newsock->isa = [OFTCPSocket class];
		[newsock close];
		newsock->isa = isa;

		@throw [OFAcceptFailedException newWithClass: isa
						      socket: self];
	}

	SSL_set_accept_state(ssl);
	SSL_set_accept_state(newsock->ssl);

	if (!SSL_use_PrivateKey_file(newsock->ssl, [privateKeyFile cString],
	    SSL_FILETYPE_PEM) || !SSL_use_certificate_file(newsock->ssl,
	    [certificateFile cString], SSL_FILETYPE_PEM) ||
	if (SSL_connect(ssl) != 1) {
		[super close];
	    SSL_accept(newsock->ssl) != 1) {
		/* We only want to close the OFTCPSocket */
		newsock->isa = [OFTCPSocket class];
		[newsock close];
		newsock->isa = isa;

		@throw [OFAcceptFailedException newWithClass: isa
						      socket: self];
	}

	return newsock;
}

205
206
207
208
209
210
211
























212
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

	if ((ret = SSL_write(ssl, buf, (int)size)) < 1)
		@throw [OFWriteFailedException newWithClass: isa
						     stream: self
					      requestedSize: size];

	return ret;
}

- (void)setPrivateKeyFile: (OFString*)file
{
	OFString *old = privateKeyFile;
	privateKeyFile = [file copy];
	[old release];
}

- (OFString*)privateKeyFile
{
	return [[privateKeyFile copy] autorelease];
}

- (void)setCertificateFile: (OFString*)file
{
	OFString *old = certificateFile;
	certificateFile = [file copy];
	[old release];
}

- (OFString*)certificateFile
{
	return [[certificateFile copy] autorelease];
}
@end