unable to check if a configuration profile exists

2019-04-09 22:56发布

问题:

I am trying to check if a configuration profile exists on the iPhone , I found the following tutorial on how to do it : http://alex.tapmania.org/2011/09/check_conf_prof_is_installed_ios.html

which , for me , translates into the following code :

NSString * certPath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"mobileconfig"];
    SecTrustRef trust;
    NSData * certData = [NSData dataWithContentsOfFile:certPath];
    SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef) certData);
    SecPolicyRef policy = SecPolicyCreateBasicX509();
    OSStatus err = SecTrustCreateWithCertificates((__bridge CFArrayRef) [NSArray arrayWithObject:(__bridge id)cert],policy, &trust);

    SecTrustResultType  trustResult = -1;
    err = SecTrustEvaluate(trust, &trustResult);


    if (trustResult ==4) {
        label.text=@"Profile installed";
    }
    else{
        label.text=@"Profile not installed";
    }

the application crashes at the line :

OSStatus err = SecTrustCreateWithCertificates((__bridge CFArrayRef) [NSArray arrayWithObject:(__bridge id)cert],policy, &trust);

I also noticed that at this stage cert doesn t have any memory allocated in it .

What is wrong? is this the correct procedure? if not, is there another tutorial that is more beneficial?

Thanks!

回答1:

I am not sure as to why you'd want to do this through code. There are Mac OSx apps that already do this for you. If what you're intending to do doesn't have to involve code, you can download this Mac app called iPhone Configuration Utility from this Apple link. Has many features including configuring/view/etc profiles installed on an iPhone.



回答2:

I have recently solved the same problem that you are having. The code that you have posted and the advice from your link can indeed be used to check for the existence of a configuration profile. The snag that you have hit is undoubtedly a problem with your certificate.

I created my pair of certificates using openSSL for win32 by ShiningLight. Full instructions on how to do it can be found here: http://www.dylanbeattie.net/docs/openssl_iis_ssl_howto.html

note however, that there are a couple of gotchas:

  1. when creating your root certificate, the basicconstraints=CA setting in the config file must be set to TRUE (FALSE is needed for the other certificate).

  2. For IOs to be able to read them, both certificates must be encoded using DER. To do this using openssl you need to run a command similar to openssl x509 -in certs/cert.cer -out certs/certDER.cer -outform DER on both certificates. I strongly suspect it is the wrong encoding that is crashing your code.