雙向認證第二步: p.12文件進行ssl雙向認證

將上一章創建好的client.p12拖進新建的工程

在ViewController.m文件中

- (void)viewDidLoad {

[super viewDidLoad];

// 請求數據

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

NSURL *url = [NSURL URLWithString:@”https://www.zhuangzhuangyong.com“];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

[request setHTTPShouldHandleCookies:NO];

[request setTimeoutInterval:30];

[request setHTTPMethod:@”GET”];

NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@”%@”, message);

}];

[task resume];

}

#pragma mark - NSURLSessionDelegate

- (void)URLSession:(NSURLSession )session didReceiveChallenge:(NSURLAuthenticationChallenge )challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler

{

NSString *method = challenge.protectionSpace.authenticationMethod;

NSLog(@”%s,,,\nmethod==%@”,func, method);

//客戶端驗證服務器端

if([method isEqualToString:NSURLAuthenticationMethodServerTrust]){

NSString *host = challenge.protectionSpace.host;

NSLog(@”%s,,,,,\nhost==%@”,func, host);

NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

completionHandler(NSURLSessionAuthChallengeUseCredential, credential);

return;

}

//服務器驗證客戶端

if ([method isEqualToString:NSURLAuthenticationMethodClientCertificate])

{

NSString *thePath = [[NSBundle mainBundle] pathForResource:@”client” ofType:@”p12”];

NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];

CFDataRef inPKCS12Data = (CFDataRef)CFBridgingRetain(PKCS12Data);

SecIdentityRef identity;

// 讀取p12證書中的內容

OSStatus result = [self extractP12Data:inPKCS12Data toIdentity:&identity];

if(result != errSecSuccess){

completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);

return;

}

SecCertificateRef certificate = NULL;

SecIdentityCopyCertificate (identity, &certificate);

const void *certs[] = {certificate};

CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL);

NSURLCredential credential = [NSURLCredential credentialWithIdentity:identity certificates:(NSArray)CFBridgingRelease(certArray) persistence:NSURLCredentialPersistencePermanent];

completionHandler(NSURLSessionAuthChallengeUseCredential, credential);

}

}

#pragma mark - Private method

/**

* @brief 解密證書

*

* @param inP12Data 證書數據

* @param identity

*

* @return 32 字節 錯誤碼

*/

-(OSStatus) extractP12Data:(CFDataRef)inP12Data toIdentity:(SecIdentityRef*)identity {

OSStatus securityError = errSecSuccess;

CFStringRef password = CFSTR(“123456”);

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 ident = CFArrayGetValueAtIndex(items,0);

const void *tempIdentity = NULL;

tempIdentity = CFDictionaryGetValue(ident, kSecImportItemIdentity);

*identity = (SecIdentityRef)tempIdentity;

}

if (options) {

CFRelease(options);

}

return securityError;

}

控制檯輸出

It works!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章