当前位置:  开发笔记 > 后端 > 正文

客户端证书iOS

如何解决《客户端证书iOS》经验,为你挑选了1个好方法。

我正在尝试提取.p12文件并将其用于针对我自己的服务器的双向身份验证.当我正在尝试编译时,我得到一些链接错误.错误是指:

_kSecImportExportPassphrase

_SecIdentityCopyCertificate

_kSecImportItemTrust

_SecPKCS12Import

_kSecImportItemIdentity

这是我用来提取p12文件的代码:

        -(void)clientCert   
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"torbix" ofType:@"p12"];
            NSData *p12data = [NSData dataWithContentsOfFile:path];
            CFDataRef inP12data = (CFDataRef)p12data;

            SecIdentityRef myIdentity;
            SecTrustRef myTrust;
            OSStatus status = extractIdentityAndTrust(inP12data, &myIdentity, &myTrust);

            SecCertificateRef myCertificate;
            SecIdentityCopyCertificate(myIdentity, &myCertificate);
            const void *certs[] = { myCertificate };
            CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL);

        }
        OSStatus extractIdentityAndTrust(CFDataRef inP12data, SecIdentityRef *identity, SecTrustRef *trust)
        {
            OSStatus securityError = errSecSuccess;

            CFStringRef password = CFSTR("password");
            const void *keys[] = { kSecImportExportPassphrase };
            const void *values[] = { password };

            CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

            CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
            securityError = SecPKCS12Import(inP12data, options, &items);

            if (securityError == 0) {
                CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex(items, 0);
                const void *tempIdentity = NULL;
                tempIdentity = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemIdentity);
                *identity = (SecIdentityRef)tempIdentity;
                const void *tempTrust = NULL;
                tempTrust = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemTrust);
                *trust = (SecTrustRef)tempTrust;
            }

            if (options) {
                CFRelease(options);
            }

        return securityError;
    }

为什么我会收到这些错误?



1> zaph..:

添加Security.framework到您的项目.

在目标的Xcode 4.2+中,转到"Build Phases"选项卡,"Link BinariesWith Libraries",单击"+"并添加"Security.framework".

在此输入图像描述

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有