Server-side configuration
Nginx key configuration is as follows:
Listen 443;server_name localhost;ssl on;ssl_certificate /usr/local/opt/nginx/certificates/ Server.cer;ssl_certificate_key /USR/LOCAL/OPT/NGINX/CERTIFICATES/SERVER.KEY.PEM;SSL_CLIENT_CERTIFICATE/USR /local/opt/nginx/certificates/ca.cer;ssl_verify_client on ;
SSL turns on HTTPS
Ssl_certificate is the path to the server-side certificate, and Ssl_certificate_key is the path to the server's private key
Ssl_verify_client is configured for two-way authentication (client certificate)
Ssl_client_certificate is the root certificate that issued the client certificate
Why is the root certificate, because many client certificates can be issued, as long as it is issued by the root certificate, the service side is considered certified through
After the configuration is completed, it is generally necessary to jump the HTTP request of Port 80 to port 443, otherwise the user can be accessed by HTTP via 80 port, which means the security is lost.
Client code
On the internet to find a lot of iOS client certificate posts, either the code is not complete, or is very old post, or the use of Nsurlconnection,delegate method are deprecated, finally found a code snippet, changed a bit
Nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; Nsurlsession *session = [nsurlsession sessionwithconfiguration:configuration delegate:self DelegateQueue:nil]; Nsurl *url = [Nsurl urlwithstring:@ "https://localhost/svc/portal/setting"]; 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];
The code snippet above is to initiate an HTTPS request with Nsurlsessiondatatask. In the SSL handshake phase, the following delegate method is called 2 times
-(void) Urlsession: (Nsurlsession *) session Didreceivechallenge: (Nsurlauthenticationchallenge *) challenge Completionhandler: (void (^) (nsurlsessionauthchallengedisposition disposition, nsurlcredential *credential)) completionhandler{nsstring *method = Challenge.protectionSpace.authenticationMethod; NSLog (@ "%@", method); if ([method Isequaltostring:nsurlauthenticationmethodservertrust]) {NSString *host = Challenge.protectionspa Ce.host; NSLog (@ "%@", host); Nsurlcredential *credential = [nsurlcredential credentialForTrust:challenge.protectionSpace.serverTrust]; Completionhandler (nsurlsessionauthchallengeusecredential, credential); Return } nsstring *thepath = [[NSBundle mainbundle] pathforresource:@ "Client" oftype:@ "P12"]; NSData *pkcs12data = [[NSData alloc] initwithcontentsoffile:thepath]; Cfdataref Inpkcs12data = (cfdataref) cfbridgingretain (pkcs12data); Secidentityref identity; Read the contents of the P12 certificate 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);} -(Osstatus) Extractp12data: (cfdataref) inp12data toidentity: (secidentityref*) Identity {osstatus Securityerror = Err secsuccess; Cfstringref Password = cfstr ("The_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 ident = Cfarraygetvalueatindex (items,0); const void *tempidentity = NULL; Tempidentity = Cfdictionarygetvalue (ident, ksecimportitemidentity); *identity = (secidentityref) tempidentity; } if (options) {cfrelease (options); } return securityerror;}
The code snippet above, which is ready to use, is useful. This method is called 2 times, the first time the iOS app validates the server phase, and the second is the phase of the server validation iOS app, the client certificate. The key is to call the Completionhandler method after each phase of the validation is complete. Most of the posts on the Internet are
[[Challenge Sender] usecredential:credential Forauthenticationchallenge:challenge];
This line of code is not valid