Afnetworking HTTPS bidirectional authentication

Source: Internet
Author: User

Client Authentication Service-side certificate:


Requires domain name for HTTP configuration path

1: First in the project to pour the service-side certificate Sever.cer,

2. Then set the Afsecuritypolicy

Self.manager = [Afhttprequestoperationmanager manager];

Self.manager.responseSerializer = [[Afhttpresponseserializer alloc] init];

[Self.manager.requestSerializer setvalue:@ "iphone" forhttpheaderfield:@ "Header-platform"];

Self.manager.securityPolicy = [Afsecuritypolicy Policywithpinningmode:afsslpinningmodepublickey];

Self.manager.securityPolicy.allowInvalidCertificates = YES;

Self.manager.securityPolicy.validatesDomainName = NO;

Self.manager.securityPolicy.validatesCertificateChain = NO;

The client changes the certificate in the project and the server-side certificate match



Server-side Authentication client certificate, first import the server certificate CLIENT.P12 into the server-side key store


Then add the following method to the AFURLCONNECTIONOPERATION.M


-(Osstatus) Extractidentity: (cfdataref) Inp12data:(secidentityref*) identity {

Osstatus securityerror = errsecsuccess;

Cfstringref Password = cfstr ("clic1234");

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;

}


Put the AFURLCONNECTIONOPERATION.M in the

-(void) connection: (Nsurlconnection *) connection

Willsendrequestforauthenticationchallenge: (Nsurlauthenticationchallenge *) challenge

Replaced by


-(void) connection: (Nsurlconnection *) connection

Willsendrequestforauthenticationchallenge: (Nsurlauthenticationchallenge *) challenge

{


NSString *thepath = [[NSBundle mainbundle] pathforresource:@ "Client" oftype:@ "P12"];

NSLog (@ "thepath===========%@", thepath);

NSData *pkcs12data = [[NSData alloc] initwithcontentsoffile:thepath];

Cfdataref Inpkcs12data = (__bridge cfdataref) pkcs12data;

Secidentityref identity = NULL;

Extract the ideneity from the certificate

[Self Extractidentity:inpkcs12data:&identity];

Seccertificateref certificate = NULL;

Secidentitycopycertificate (identity, &certificate);

const void *certs[] = {certificate};

Cfarrayref Certarray = cfarraycreate (Kcfallocatordefault, certs, 1, NULL);

Create a credential from the certificate and ideneity, then reply to the challenge with the credential

NSLog (@ "identity=========%@", identity);

Nsurlcredential *credential = [nsurlcredential credentialwithidentity:identity certificates:nil Persistence: Nsurlcredentialpersistencepermanent];

credential = [nsurlcredential credentialwithidentity:identity certificates: (__bridge NSArray*) CertArray Persi Stence:nsurlcredentialpersistencepermanent];

[Challenge.sender usecredential:credential Forauthenticationchallenge:challenge];


}


And then we can do two-way authentication.

Afnetworking HTTPS bidirectional authentication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.