-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHTTP2TransportTLSEnabledTests.swift
538 lines (495 loc) · 17.1 KB
/
HTTP2TransportTLSEnabledTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
import GRPCCore
import GRPCNIOTransportHTTP2Posix
import GRPCNIOTransportHTTP2TransportServices
import NIOSSL
import Testing
#if canImport(Network)
import Network
#endif
@Suite("HTTP/2 transport E2E tests with TLS enabled")
struct HTTP2TransportTLSEnabledTests {
// - MARK: Tests
@Test(
"When using defaults, server does not perform client verification",
arguments: TransportKind.supported,
TransportKind.supported
)
func testRPC_Defaults_OK(
clientTransport: TransportKind,
serverTransport: TransportKind
) async throws {
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
let clientConfig = self.makeDefaultTLSClientConfig(
for: clientTransport,
certificateKeyPairs: certificateKeyPairs
)
let serverConfig = self.makeDefaultTLSServerConfig(
for: serverTransport,
certificateKeyPairs: certificateKeyPairs
)
try await self.withClientAndServer(
clientConfig: clientConfig,
serverConfig: serverConfig
) { control in
await #expect(throws: Never.self) {
try await self.executeUnaryRPC(control: control)
}
}
}
@Test(
"When using mTLS defaults, both client and server verify each others' certificates",
arguments: TransportKind.supported,
TransportKind.supported
)
func testRPC_mTLS_OK(
clientTransport: TransportKind,
serverTransport: TransportKind
) async throws {
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
let clientConfig = self.makeMTLSClientConfig(
for: clientTransport,
certificateKeyPairs: certificateKeyPairs,
serverHostname: "localhost"
)
let serverConfig = self.makeMTLSServerConfig(
for: serverTransport,
certificateKeyPairs: certificateKeyPairs,
includeClientCertificateInTrustRoots: true
)
try await self.withClientAndServer(
clientConfig: clientConfig,
serverConfig: serverConfig
) { control in
await #expect(throws: Never.self) {
try await self.executeUnaryRPC(control: control)
}
}
}
@Test(
"Error is surfaced when client fails server verification",
arguments: TransportKind.supported,
TransportKind.supported
)
// Verification should fail because the custom hostname is missing on the client.
func testClientFailsServerValidation(
clientTransport: TransportKind,
serverTransport: TransportKind
) async throws {
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
let clientTransportConfig = self.makeDefaultTLSClientConfig(
for: clientTransport,
certificateKeyPairs: certificateKeyPairs,
authority: "wrong-hostname"
)
let serverTransportConfig = self.makeDefaultTLSServerConfig(
for: serverTransport,
certificateKeyPairs: certificateKeyPairs
)
await #expect {
try await self.withClientAndServer(
clientConfig: clientTransportConfig,
serverConfig: serverTransportConfig
) { control in
try await self.executeUnaryRPC(control: control)
}
} throws: { error in
let rootError = try #require(error as? RPCError)
#expect(rootError.code == .unavailable)
switch clientTransport {
case .posix:
#expect(
rootError.message
== "The server accepted the TCP connection but closed the connection before completing the HTTP/2 connection preface."
)
let sslError = try #require(rootError.cause as? NIOSSLExtraError)
guard sslError == .failedToValidateHostname else {
Issue.record(
"Should be a NIOSSLExtraError.failedToValidateHostname error, but was: \(String(describing: rootError.cause))"
)
return false
}
#if canImport(Network)
case .transportServices:
#expect(rootError.message.starts(with: "Could not establish a connection to"))
let nwError = try #require(rootError.cause as? NWError)
guard case .tls(Security.errSSLBadCert) = nwError else {
Issue.record(
"Should be a NWError.tls(-9808/errSSLBadCert) error, but was: \(String(describing: rootError.cause))"
)
return false
}
#endif
}
return true
}
}
@Test(
"Error is surfaced when server fails client verification",
arguments: TransportKind.supported,
TransportKind.supported
)
// Verification should fail because the client does not offer a cert that
// the server can use for mutual verification.
func testServerFailsClientValidation(
clientTransport: TransportKind,
serverTransport: TransportKind
) async throws {
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
let clientTransportConfig = self.makeDefaultTLSClientConfig(
for: clientTransport,
certificateKeyPairs: certificateKeyPairs
)
let serverTransportConfig = self.makeMTLSServerConfig(
for: serverTransport,
certificateKeyPairs: certificateKeyPairs,
includeClientCertificateInTrustRoots: true
)
await #expect {
try await self.withClientAndServer(
clientConfig: clientTransportConfig,
serverConfig: serverTransportConfig
) { control in
try await self.executeUnaryRPC(control: control)
}
} throws: { error in
let rootError = try #require(error as? RPCError)
#expect(rootError.code == .unavailable)
#expect(
rootError.message
== "The server accepted the TCP connection but closed the connection before completing the HTTP/2 connection preface."
)
switch clientTransport {
case .posix:
let sslError = try #require(rootError.cause as? NIOSSL.BoringSSLError)
guard case .sslError = sslError else {
Issue.record(
"Should be a NIOSSL.sslError error, but was: \(String(describing: rootError.cause))"
)
return false
}
#if canImport(Network)
case .transportServices:
let nwError = try #require(rootError.cause as? NWError)
guard case .tls(Security.errSSLPeerCertUnknown) = nwError else {
// When the TLS handshake fails, the connection will be closed from the client.
// Network.framework will generally surface the right SSL error (in this case, an "unknown
// certificate" from the server), but it will sometimes instead return the broken pipe
// error caused by the underlying TLS handshake handler closing the connection:
// we should tolerate this.
if case .posix(POSIXErrorCode.EPIPE) = nwError {
return true
}
Issue.record(
"Should be a NWError.tls(-9829/errSSLPeerCertUnknown) error, but was: \(String(describing: rootError.cause))"
)
return false
}
#endif
}
return true
}
}
// - MARK: Test Utilities
enum TLSEnabledTestsError: Error {
case failedToImportPKCS12
case unexpectedListeningAddress
}
struct Config<Transport, Security> {
var security: Security
var transport: Transport
}
enum ClientConfig {
typealias Posix = Config<
HTTP2ClientTransport.Posix.Config,
HTTP2ClientTransport.Posix.TransportSecurity
>
case posix(Posix)
#if canImport(Network)
typealias TransportServices = Config<
HTTP2ClientTransport.TransportServices.Config,
HTTP2ClientTransport.TransportServices.TransportSecurity
>
case transportServices(TransportServices)
#endif
}
enum ServerConfig {
typealias Posix = Config<
HTTP2ServerTransport.Posix.Config,
HTTP2ServerTransport.Posix.TransportSecurity
>
case posix(Posix)
#if canImport(Network)
typealias TransportServices = Config<
HTTP2ServerTransport.TransportServices.Config,
HTTP2ServerTransport.TransportServices.TransportSecurity
>
case transportServices(TransportServices)
#endif
}
private func makeDefaultPlaintextPosixClientConfig() -> ClientConfig.Posix {
ClientConfig.Posix(
security: .plaintext,
transport: .defaults { config in
config.backoff.initial = .milliseconds(100)
config.backoff.multiplier = 1
config.backoff.jitter = 0
}
)
}
#if canImport(Network)
private func makeDefaultPlaintextTSClientConfig() -> ClientConfig.TransportServices {
ClientConfig.TransportServices(
security: .plaintext,
transport: .defaults { config in
config.backoff.initial = .milliseconds(100)
config.backoff.multiplier = 1
config.backoff.jitter = 0
}
)
}
#endif
private func makeDefaultTLSClientConfig(
for transportSecurity: TransportKind,
certificateKeyPairs: SelfSignedCertificateKeyPairs,
authority: String? = "localhost"
) -> ClientConfig {
switch transportSecurity {
case .posix:
var config = self.makeDefaultPlaintextPosixClientConfig()
config.security = .tls {
$0.trustRoots = .certificates([
.bytes(certificateKeyPairs.server.certificate, format: .der)
])
}
config.transport.http2.authority = authority
return .posix(config)
#if canImport(Network)
case .transportServices:
var config = self.makeDefaultPlaintextTSClientConfig()
config.security = .tls {
$0.trustRoots = .certificates([
.bytes(certificateKeyPairs.server.certificate, format: .der)
])
}
config.transport.http2.authority = authority
return .transportServices(config)
#endif
}
}
#if canImport(Network)
private func makeSecIdentityProvider(
certificateBytes: [UInt8],
privateKeyBytes: [UInt8]
) throws -> SecIdentity {
let password = "somepassword"
let bundle = NIOSSLPKCS12Bundle(
certificateChain: [try NIOSSLCertificate(bytes: certificateBytes, format: .der)],
privateKey: try NIOSSLPrivateKey(bytes: privateKeyBytes, format: .der)
)
let pkcs12Bytes = try bundle.serialize(passphrase: password.utf8)
let options =
[
kSecImportExportPassphrase as String: password,
kSecImportToMemoryOnly: kCFBooleanTrue!,
] as [AnyHashable: Any]
var rawItems: CFArray?
let status = SecPKCS12Import(
Data(pkcs12Bytes) as CFData,
options as CFDictionary,
&rawItems
)
guard status == errSecSuccess else {
Issue.record("Failed to import PKCS12 bundle: status \(status).")
throw TLSEnabledTestsError.failedToImportPKCS12
}
let items = rawItems! as! [[String: Any]]
let firstItem = items[0]
let identity = firstItem[kSecImportItemIdentity as String] as! SecIdentity
return identity
}
#endif
private func makeMTLSClientConfig(
for transportKind: TransportKind,
certificateKeyPairs: SelfSignedCertificateKeyPairs,
serverHostname: String?
) -> ClientConfig {
switch transportKind {
case .posix:
var config = self.makeDefaultPlaintextPosixClientConfig()
config.security = .mTLS(
certificateChain: [.bytes(certificateKeyPairs.client.certificate, format: .der)],
privateKey: .bytes(certificateKeyPairs.client.key, format: .der)
) {
$0.trustRoots = .certificates([
.bytes(certificateKeyPairs.server.certificate, format: .der)
])
}
config.transport.http2.authority = serverHostname
return .posix(config)
#if canImport(Network)
case .transportServices:
var config = self.makeDefaultPlaintextTSClientConfig()
config.security = .mTLS {
try self.makeSecIdentityProvider(
certificateBytes: certificateKeyPairs.client.certificate,
privateKeyBytes: certificateKeyPairs.client.key
)
} configure: {
$0.trustRoots = .certificates([
.bytes(certificateKeyPairs.server.certificate, format: .der)
])
}
config.transport.http2.authority = serverHostname
return .transportServices(config)
#endif
}
}
private func makeDefaultPlaintextPosixServerConfig() -> ServerConfig.Posix {
ServerConfig.Posix(security: .plaintext, transport: .defaults)
}
#if canImport(Network)
private func makeDefaultPlaintextTSServerConfig() -> ServerConfig.TransportServices {
ServerConfig.TransportServices(security: .plaintext, transport: .defaults)
}
#endif
private func makeDefaultTLSServerConfig(
for transportKind: TransportKind,
certificateKeyPairs: SelfSignedCertificateKeyPairs
) -> ServerConfig {
switch transportKind {
case .posix:
var config = self.makeDefaultPlaintextPosixServerConfig()
config.security = .tls(
certificateChain: [.bytes(certificateKeyPairs.server.certificate, format: .der)],
privateKey: .bytes(certificateKeyPairs.server.key, format: .der)
)
return .posix(config)
#if canImport(Network)
case .transportServices:
var config = self.makeDefaultPlaintextTSServerConfig()
config.security = .tls {
try self.makeSecIdentityProvider(
certificateBytes: certificateKeyPairs.server.certificate,
privateKeyBytes: certificateKeyPairs.server.key
)
}
return .transportServices(config)
#endif
}
}
private func makeMTLSServerConfig(
for transportKind: TransportKind,
certificateKeyPairs: SelfSignedCertificateKeyPairs,
includeClientCertificateInTrustRoots: Bool
) -> ServerConfig {
switch transportKind {
case .posix:
var config = self.makeDefaultPlaintextPosixServerConfig()
config.security = .mTLS(
certificateChain: [.bytes(certificateKeyPairs.server.certificate, format: .der)],
privateKey: .bytes(certificateKeyPairs.server.key, format: .der)
) {
if includeClientCertificateInTrustRoots {
$0.trustRoots = .certificates([
.bytes(certificateKeyPairs.client.certificate, format: .der)
])
}
}
return .posix(config)
#if canImport(Network)
case .transportServices:
var config = self.makeDefaultPlaintextTSServerConfig()
config.security = .mTLS {
try self.makeSecIdentityProvider(
certificateBytes: certificateKeyPairs.server.certificate,
privateKeyBytes: certificateKeyPairs.server.key
)
} configure: {
if includeClientCertificateInTrustRoots {
$0.trustRoots = .certificates([
.bytes(certificateKeyPairs.client.certificate, format: .der)
])
}
}
return .transportServices(config)
#endif
}
}
func withClientAndServer(
clientConfig: ClientConfig,
serverConfig: ServerConfig,
_ test: (ControlClient<NIOClientTransport>) async throws -> Void
) async throws {
let serverTransport: NIOServerTransport
switch serverConfig {
case .posix(let posix):
serverTransport = NIOServerTransport(
.http2NIOPosix(
address: .ipv4(host: "127.0.0.1", port: 0),
transportSecurity: posix.security,
config: posix.transport
)
)
#if canImport(Network)
case .transportServices(let config):
serverTransport = NIOServerTransport(
.http2NIOTS(
address: .ipv4(host: "127.0.0.1", port: 0),
transportSecurity: config.security,
config: config.transport
)
)
#endif
}
try await withGRPCServer(transport: serverTransport, services: [ControlService()]) { server in
guard let address = try await server.listeningAddress?.ipv4 else {
throw TLSEnabledTestsError.unexpectedListeningAddress
}
let target: any ResolvableTarget = .ipv4(host: address.host, port: address.port)
let clientTransport: NIOClientTransport
switch clientConfig {
case .posix(let config):
clientTransport = try NIOClientTransport(
.http2NIOPosix(
target: target,
transportSecurity: config.security,
config: config.transport
)
)
#if canImport(Network)
case .transportServices(let config):
clientTransport = try NIOClientTransport(
.http2NIOTS(target: target, transportSecurity: config.security, config: config.transport)
)
#endif
}
try await withGRPCClient(transport: clientTransport) { client in
let control = ControlClient(wrapping: client)
try await test(control)
}
}
}
private func executeUnaryRPC(control: ControlClient<NIOClientTransport>) async throws {
let input = ControlInput.with { $0.numberOfMessages = 1 }
let request = ClientRequest(message: input)
try await control.unary(request: request) { response in
#expect(throws: Never.self) { try response.message }
}
}
}