|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io' as io; |
| 7 | +import 'dart:typed_data'; |
| 8 | + |
| 9 | +import 'package:flutter/services.dart' show rootBundle; |
| 10 | +import 'package:http/http.dart'; |
| 11 | +import 'package:integration_test/integration_test.dart'; |
| 12 | +import 'package:ok_http/ok_http.dart'; |
| 13 | +import 'package:ok_http/src/jni/bindings.dart' as bindings; |
| 14 | + |
| 15 | +import 'package:test/test.dart'; |
| 16 | + |
| 17 | +Future<Uint8List> loadCertificateBytes(String path) async { |
| 18 | + return (await rootBundle.load(path)).buffer.asUint8List(); |
| 19 | +} |
| 20 | + |
| 21 | +void main() async { |
| 22 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 23 | + |
| 24 | + group('TLS', () { |
| 25 | + group('loadPrivateKeyAndCertificateChainFromPKCS12', () { |
| 26 | + test('success', () async { |
| 27 | + final certBytes = |
| 28 | + await loadCertificateBytes('test_certs/test-combined.p12'); |
| 29 | + final (key, chain) = |
| 30 | + loadPrivateKeyAndCertificateChainFromPKCS12(certBytes, '1234'); |
| 31 | + expect( |
| 32 | + key |
| 33 | + .as(bindings.Key.type) |
| 34 | + .getFormat()! |
| 35 | + .toDartString(releaseOriginal: true), |
| 36 | + 'PKCS#8'); |
| 37 | + expect(chain.length, 1); |
| 38 | + expect(chain[0].getType()!.toDartString(), 'X.509'); |
| 39 | + }); |
| 40 | + |
| 41 | + test('bad password', () async { |
| 42 | + final certBytes = |
| 43 | + await loadCertificateBytes('test_certs/test-combined.p12'); |
| 44 | + expect( |
| 45 | + () => loadPrivateKeyAndCertificateChainFromPKCS12( |
| 46 | + certBytes, 'incorrectpassword'), |
| 47 | + throwsA(isA<io.IOException>().having( |
| 48 | + (e) => e.toString(), 'toString', contains('password')))); |
| 49 | + }); |
| 50 | + |
| 51 | + test('bad PKCS12 data', () async { |
| 52 | + final certBytes = Uint8List.fromList([1, 2, 3, 4]); |
| 53 | + expect( |
| 54 | + () => |
| 55 | + loadPrivateKeyAndCertificateChainFromPKCS12(certBytes, '1234'), |
| 56 | + throwsA(isA<io.IOException>().having( |
| 57 | + (e) => e.toString(), |
| 58 | + 'toString', |
| 59 | + contains('does not represent a PKCS12 key store')))); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + test('unknown server cert', () async { |
| 64 | + final serverContext = io.SecurityContext() |
| 65 | + ..useCertificateChainBytes( |
| 66 | + await loadCertificateBytes('test_certs/server_chain.p12'), |
| 67 | + password: 'dartdart') |
| 68 | + ..usePrivateKeyBytes( |
| 69 | + await loadCertificateBytes('test_certs/server_key.p12'), |
| 70 | + password: 'dartdart'); |
| 71 | + final server = |
| 72 | + await io.SecureServerSocket.bind('localhost', 0, serverContext); |
| 73 | + final serverException = Completer<void>(); |
| 74 | + server.listen((socket) async { |
| 75 | + serverException.complete(); |
| 76 | + await socket.close(); |
| 77 | + }, onError: (Object e) { |
| 78 | + serverException.completeError(e); |
| 79 | + }); |
| 80 | + addTearDown(server.close); |
| 81 | + |
| 82 | + final config = |
| 83 | + const OkHttpClientConfiguration(validateServerCertificates: true); |
| 84 | + final httpClient = OkHttpClient(configuration: config); |
| 85 | + addTearDown(httpClient.close); |
| 86 | + |
| 87 | + expect( |
| 88 | + () async => |
| 89 | + await httpClient.get(Uri.https('localhost:${server.port}', '/')), |
| 90 | + throwsA(isA<ClientException>() |
| 91 | + .having((e) => e.message, 'message', contains('Handshake')))); |
| 92 | + expect( |
| 93 | + () async => await serverException.future, |
| 94 | + throwsA(isA<io.HandshakeException>() |
| 95 | + .having((e) => e.message, 'message', contains('Handshake')))); |
| 96 | + }); |
| 97 | + |
| 98 | + test('ignore unknown server cert', () async { |
| 99 | + final serverContext = io.SecurityContext() |
| 100 | + ..useCertificateChainBytes( |
| 101 | + await loadCertificateBytes('test_certs/server_chain.p12'), |
| 102 | + password: 'dartdart') |
| 103 | + ..usePrivateKeyBytes( |
| 104 | + await loadCertificateBytes('test_certs/server_key.p12'), |
| 105 | + password: 'dartdart'); |
| 106 | + final server = |
| 107 | + await io.SecureServerSocket.bind('localhost', 0, serverContext); |
| 108 | + server.listen((socket) async { |
| 109 | + socket |
| 110 | + .writeAll(['HTTP/1.1 200 OK', 'Content-Length: 0', '\r\n'], '\r\n'); |
| 111 | + await socket.close(); |
| 112 | + }); |
| 113 | + addTearDown(server.close); |
| 114 | + |
| 115 | + final config = |
| 116 | + const OkHttpClientConfiguration(validateServerCertificates: false); |
| 117 | + final httpClient = OkHttpClient(configuration: config); |
| 118 | + addTearDown(httpClient.close); |
| 119 | + |
| 120 | + expect( |
| 121 | + (await httpClient.get(Uri.https('localhost:${server.port}', '/'))) |
| 122 | + .statusCode, |
| 123 | + 200); |
| 124 | + }); |
| 125 | + |
| 126 | + test('client cert', () async { |
| 127 | + final certBytes = |
| 128 | + await loadCertificateBytes('test_certs/test-combined.p12'); |
| 129 | + final serverContext = io.SecurityContext() |
| 130 | + ..useCertificateChainBytes( |
| 131 | + await loadCertificateBytes('test_certs/server_chain.p12'), |
| 132 | + password: 'dartdart') |
| 133 | + ..usePrivateKeyBytes( |
| 134 | + await loadCertificateBytes('test_certs/server_key.p12'), |
| 135 | + password: 'dartdart') |
| 136 | + ..setTrustedCertificatesBytes(certBytes, password: '1234'); |
| 137 | + |
| 138 | + final clientCertificate = Completer<io.X509Certificate?>(); |
| 139 | + final server = await io.SecureServerSocket.bind( |
| 140 | + 'localhost', 0, serverContext, |
| 141 | + requireClientCertificate: true); |
| 142 | + server.listen((socket) async { |
| 143 | + clientCertificate.complete(socket.peerCertificate); |
| 144 | + socket |
| 145 | + .writeAll(['HTTP/1.1 200 OK', 'Content-Length: 0', '\r\n'], '\r\n'); |
| 146 | + await socket.close(); |
| 147 | + }); |
| 148 | + addTearDown(server.close); |
| 149 | + |
| 150 | + final (key, chain) = |
| 151 | + loadPrivateKeyAndCertificateChainFromPKCS12(certBytes, '1234'); |
| 152 | + final config = OkHttpClientConfiguration( |
| 153 | + clientPrivateKey: key, |
| 154 | + clientCertificateChain: chain, |
| 155 | + validateServerCertificates: false); |
| 156 | + final httpClient = OkHttpClient(configuration: config); |
| 157 | + addTearDown(httpClient.close); |
| 158 | + |
| 159 | + expect( |
| 160 | + (await httpClient.get(Uri.https('localhost:${server.port}', '/'))) |
| 161 | + .statusCode, |
| 162 | + 200); |
| 163 | + expect((await clientCertificate.future)!.issuer, |
| 164 | + contains('Internet Widgits Pty Ltd')); |
| 165 | + }); |
| 166 | + |
| 167 | + test('private key without cert chain', () async { |
| 168 | + final certBytes = |
| 169 | + await loadCertificateBytes('test_certs/test-combined.p12'); |
| 170 | + |
| 171 | + final (key, chain) = |
| 172 | + loadPrivateKeyAndCertificateChainFromPKCS12(certBytes, '1234'); |
| 173 | + final config = OkHttpClientConfiguration(clientPrivateKey: key); |
| 174 | + expect(() => OkHttpClient(configuration: config), throwsArgumentError); |
| 175 | + }); |
| 176 | + |
| 177 | + test('private key without cert chain', () async { |
| 178 | + final certBytes = |
| 179 | + await loadCertificateBytes('test_certs/test-combined.p12'); |
| 180 | + |
| 181 | + final (key, chain) = |
| 182 | + loadPrivateKeyAndCertificateChainFromPKCS12(certBytes, '1234'); |
| 183 | + final config = OkHttpClientConfiguration(clientCertificateChain: chain); |
| 184 | + expect(() => OkHttpClient(configuration: config), throwsArgumentError); |
| 185 | + }); |
| 186 | + }); |
| 187 | +} |
0 commit comments