美文网首页
ASIHTTPRequest 信任自签名证书

ASIHTTPRequest 信任自签名证书

作者: 权宜平和 | 来源:发表于2018-07-26 19:46 被阅读74次

    关于ASIHTTPRequest如何信任自签名证书,长话短说,如下几步:
    1、找到如图所示的文件

    image.png ,
    进入.m文件,找到initWithURL:方法,如下图所示,添加setCertificate(自定义方法,用来导入自签名证书的)。
    image.png
    2、setCertificate的具体实现,如下图:
    image.png
    具体代码如下:
    + (BOOL)extractIdentity:(SecIdentityRef *)outIdentity andTrust:(SecTrustRef*)outTrust fromPKCS12Data:(NSData *)inPKCS12Data
    {
        OSStatus securityError = errSecSuccess;
        
        NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObject:@"ZJMC@EAM" forKey:(id)kSecImportExportPassphrase];
        
        CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
        securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,(CFDictionaryRef)optionsDictionary,&items);
        
        if (securityError == 0) {
            CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
            const void *tempIdentity = NULL;
            tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);
            *outIdentity = (SecIdentityRef)tempIdentity;
            const void *tempTrust = NULL;
            tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
            *outTrust = (SecTrustRef)tempTrust;
        } else {
            NSLog(@"Failed with error code %d",(int)securityError);
            return NO;
        }
        return YES;
    }
    
    - (void)setCertificate {
        SecIdentityRef identity = NULL;
        SecTrustRef trust = NULL;
        NSData *PKCS12Data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zj-server" ofType:@"p12"]];
        [ASIHTTPRequest extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data];
        [self setClientCertificateIdentity:identity];
    }
    

    3、说明:p12文件是自签名证书对应的一种格式,这里必须是p12类型的,文件来源于创建自签名证书的人员。

    其它介绍
    ASIHTTPRequest详细介绍

    相关文章

      网友评论

          本文标题:ASIHTTPRequest 信任自签名证书

          本文链接:https://www.haomeiwen.com/subject/wiibmftx.html