Merge pull request #491 from RonnieRen/bugfix/certificates_path

support public asset path name instead of www for capacitor engine
This commit is contained in:
Sefa Ilkimen
2023-05-24 02:00:55 +02:00
committed by GitHub
2 changed files with 21 additions and 3 deletions

View File

@@ -71,7 +71,7 @@ class CordovaServerTrust implements Runnable {
this.tlsConfiguration.setTrustManagers(this.noOpTrustManagers);
} else if ("pinned".equals(this.mode)) {
this.tlsConfiguration.setHostnameVerifier(null);
this.tlsConfiguration.setTrustManagers(this.getTrustManagers(this.getCertsFromBundle("www/certificates")));
this.tlsConfiguration.setTrustManagers(this.getTrustManagers(this.getCertsFromBundle(getWebAssetDir() + "/certificates")));
} else {
this.tlsConfiguration.setHostnameVerifier(null);
this.tlsConfiguration.setTrustManagers(this.getTrustManagers(this.getCertsFromKeyStore("AndroidCAStore")));
@@ -84,6 +84,14 @@ class CordovaServerTrust implements Runnable {
}
}
private String getWebAssetDir() {
return isRunningOnCapacitor()? "public" : "www";
}
private boolean isRunningOnCapacitor() {
return this.activity.getClass().getSuperclass().getName().contains("com.getcapacitor");
}
private TrustManager[] getTrustManagers(KeyStore store) throws GeneralSecurityException {
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);

View File

@@ -155,8 +155,18 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) {
@implementation SM_AFSecurityPolicy
+(BOOL) isRunningOnCapacitor {
return NSClassFromString(@"CAPPlugin") != nil;
}
+ (NSSet *)certificatesInBundle:(NSBundle *)bundle {
NSArray *paths = [bundle pathsForResourcesOfType:@"cer" inDirectory:@"www/certificates"];
NSString* assetDir = @"www";
if([self isRunningOnCapacitor]) {
// we are running on capacitor and its assets dir is 'public'
assetDir = @"public";
}
NSArray *paths = [bundle pathsForResourcesOfType:@"cer" inDirectory: [NSString stringWithFormat:@"%@/certificates", assetDir]];
NSMutableSet *certificates = [NSMutableSet setWithCapacity:[paths count]];
for (NSString *path in paths) {
@@ -171,7 +181,7 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) {
static NSSet *_defaultPinnedCertificates = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSBundle *bundle = [self isRunningOnCapacitor] ? [NSBundle mainBundle] : [NSBundle bundleForClass:[self class]];
_defaultPinnedCertificates = [self certificatesInBundle:bundle];
});