iOS: Pre install SSL certificate in keychain - programmatically

前端 未结 2 1751
忘了有多久
忘了有多久 2020-11-29 04:10

I want to install/save a certificate in the keychain before the user visits the site. I have a HTTPS server, and my app authenticates the user before they go to https://mys

2条回答
  •  醉梦人生
    2020-11-29 04:43

    Once you have the server certificate in der format you can try the following code:

    + (void) addCertToKeychain:(NSData*)certInDer
    {
        OSStatus            err = noErr;
        SecCertificateRef   cert;
    
        cert = SecCertificateCreateWithData(NULL, (CFDataRef) certInDer);
        assert(cert != NULL);
    
        CFTypeRef result;
    
        NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              (id)kSecClassCertificate, kSecClass,
                              cert, kSecValueRef, 
                              nil];
    
        err = SecItemAdd((CFDictionaryRef)dict, &result);
        assert(err == noErr || err == errSecDuplicateItem);
    
        CFRelease(cert);
    }
    

    It will add the certificate to the keychain sandbox of your application i.e. no other application will trust your cert.

提交回复
热议问题