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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
+
-
+
-
+
+
-
+
-
+
|
if (SSL_ != NULL)
SSL_free(SSL_);
}
- (void)startTLS
{
of_string_encoding_t encoding = [OFString nativeOSEncoding];
if ((_SSL = SSL_new(ctx)) == NULL || !SSL_set_fd(_SSL, _socket)) {
[super close];
@throw [OFConnectionFailedException
exceptionWithHost: nil
port: 0
socket: self];
}
SSL_set_connect_state(_SSL);
if ((_privateKeyFile != nil && !SSL_use_PrivateKey_file(_SSL,
[_privateKeyFile cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[_privateKeyFile cStringWithEncoding: encoding],
SSL_FILETYPE_PEM)) || (_certificateFile != nil &&
!SSL_use_certificate_file(_SSL, [_certificateFile
cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
cStringWithEncoding: encoding],
SSL_FILETYPE_PEM)) || SSL_connect(_SSL) != 1) {
[super close];
@throw [OFConnectionFailedException
exceptionWithHost: nil
port: 0
socket: self];
}
}
- (void)connectToHost: (OFString*)host
port: (uint16_t)port
{
[super connectToHost: host
port: port];
[self startTLS];
}
- (instancetype)accept
{
of_string_encoding_t encoding = [OFString nativeOSEncoding];
SSLSocket *client = (SSLSocket*)[super accept];
if ((client->_SSL = SSL_new(ctx)) == NULL ||
!SSL_set_fd(client->_SSL, client->_socket)) {
[client SSL_super_close];
@throw [OFAcceptFailedException exceptionWithSocket: self];
}
if (_requestsClientCertificates)
SSL_set_verify(client->_SSL, SSL_VERIFY_PEER, NULL);
SSL_set_accept_state(client->_SSL);
if (!SSL_use_PrivateKey_file(client->_SSL, [_privateKeyFile
cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
cStringWithEncoding: encoding],
SSL_FILETYPE_PEM) || !SSL_use_certificate_file(client->_SSL,
[_certificateFile cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[_certificateFile cStringWithEncoding: encoding],
SSL_FILETYPE_PEM) || SSL_accept(client->_SSL) != 1) {
[client SSL_super_close];
@throw [OFAcceptFailedException exceptionWithSocket: self];
}
return client;
}
|