Skip to content

Commit d7840f3

Browse files
wangx0404wangxiang13Lukasa
authored
fix https fail on Android (#453)
* fix https fail on Android * add compatibility for https failure after Android14 --------- Co-authored-by: wangxiang13 <wangxiang13@lixiang.com> Co-authored-by: Cory Benfield <lukasa@apple.com>
1 parent 3b2a01c commit d7840f3

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2019-2021 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#if os(Android)
16+
/// The path to the root CA bundle directory.
17+
///
18+
/// May be nil if we could not find the root CA bundle directory.
19+
internal let rootCADirectoryPath: String? = locateRootCADirectory()
20+
21+
/// This is a list of root CA directory search paths.
22+
///
23+
/// This list contains paths as validated against several distributions. If you are aware of a CA bundle on a specific distribution
24+
/// that is not present here, please open a pull request that adds the appropriate search path.
25+
/// Some distributions do not ship CA directories: as such, it is not a problem if a distribution that is present in rootCAFileSearchPaths
26+
/// is not present in this list.
27+
//see https://android.googlesource.com/platform/frameworks/base/+/8b192b19f264a8829eac2cfaf0b73f6fc188d933%5E%21/#F0
28+
private let rootCADirectorySearchPaths = [
29+
"/apex/com.android.conscrypt/cacerts", // >= Android14
30+
"/system/etc/security/cacerts", // < Android14
31+
]
32+
33+
private func locateRootCADirectory() -> String? {
34+
return rootCADirectorySearchPaths.first(where: { FileSystemObject.pathType(path: $0) == .directory })
35+
}
36+
#endif

Sources/NIOSSL/SSLContext.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,15 @@ extension NIOSSLContext {
671671
}
672672
}
673673

674+
if result == 0 {
675+
let errorStack = BoringSSLError.buildErrorStack()
676+
throw BoringSSLError.unknownError(errorStack)
677+
}
678+
#elseif os(Android)
679+
let result = rootCADirectoryPath.withCString { rootCADirectoryPointer in
680+
CNIOBoringSSL_SSL_CTX_load_verify_locations(context, nil, rootCADirectoryPointer)
681+
}
682+
674683
if result == 0 {
675684
let errorStack = BoringSSLError.buildErrorStack()
676685
throw BoringSSLError.unknownError(errorStack)

0 commit comments

Comments
 (0)