@@ -411,16 +411,15 @@ static bool loadTrustSettings(JNIEnv *env,
411411 jmethodID jm_listAdd,
412412 jobject *inputTrust) {
413413 CFArrayRef trustSettings;
414- if (*inputTrust == NULL ) {
415- *inputTrust = (*env)->NewObject (env, jc_arrayListClass, jm_arrayListCons);
416- if (*inputTrust == NULL ) {
417- CFRelease (trustSettings);
418- return false ;
419- }
420- }
421-
422414 // Load trustSettings into inputTrust
423415 if (SecTrustSettingsCopyTrustSettings (certRef, domain, &trustSettings) == errSecSuccess && trustSettings != NULL ) {
416+ if (*inputTrust == NULL ) {
417+ *inputTrust = (*env)->NewObject (env, jc_arrayListClass, jm_arrayListCons);
418+ if (*inputTrust == NULL ) {
419+ CFRelease (trustSettings);
420+ return false ;
421+ }
422+ }
424423 addTrustSettingsToInputTrust (env, jm_listAdd, trustSettings, *inputTrust);
425424 CFRelease (trustSettings);
426425 }
@@ -459,6 +458,31 @@ static bool createTrustedCertEntry(JNIEnv *env, jobject keyStore,
459458 return true ;
460459}
461460
461+ static bool validateCertificate (SecCertificateRef certRef) {
462+ SecTrustRef secTrust = NULL ;
463+ CFMutableArrayRef subjCerts = CFArrayCreateMutable (NULL , 1 , &kCFTypeArrayCallBacks );
464+ CFArraySetValueAtIndex (subjCerts, 0 , certRef);
465+
466+ SecPolicyRef policy = SecPolicyCreateBasicX509 ();
467+ OSStatus ortn = SecTrustCreateWithCertificates (subjCerts, policy, &secTrust);
468+ bool result = false ;
469+ if (ortn) {
470+ /* should never happen */
471+ cssmPerror (" SecTrustCreateWithCertificates" , ortn);
472+ goto errOut;
473+ }
474+
475+ result = SecTrustEvaluateWithError (secTrust, NULL );
476+ errOut:
477+ if (policy) {
478+ CFRelease (policy);
479+ }
480+ if (secTrust) {
481+ CFRelease (secTrust);
482+ }
483+ return result;
484+ }
485+
462486static void addCertificatesToKeystore (JNIEnv *env, jobject keyStore,
463487 jmethodID jm_createTrustedCertEntry,
464488 jclass jc_arrayListClass,
@@ -493,6 +517,16 @@ static void addCertificatesToKeystore(JNIEnv *env, jobject keyStore,
493517 goto errOut;
494518 }
495519
520+ // If no trust settings we need to verify the certificate first
521+ if (inputTrust == NULL ) {
522+ bool valid = validateCertificate (certRef);
523+ if (valid) {
524+ inputTrust = (*env)->NewObject (env, jc_arrayListClass, jm_arrayListCons);
525+ } else {
526+ continue ;
527+ }
528+ }
529+
496530 // Create java object for certificate with trust settings
497531 if (!createTrustedCertEntry (env, keyStore, certRef, jm_createTrustedCertEntry, inputTrust)) {
498532 goto errOut;
0 commit comments