ios - Authorizing a certificate for MCSession -
i have created self signed certificate in keychain , using multipeerconnectivity framework transfer data between devices.
when create session can pass securityidentity when invite peers in mcsession.
- (instancetype)initwithpeer:(mcpeerid *)mypeerid securityidentity:(nsarray *)identity encryptionpreference:(mcencryptionpreference)encryptionpreference;
but securityidentity nsarray, how can pass certificate in nsarray , how can authenticate in
- (void)session:(mcsession *)session didreceivecertificate:(nsarray *)certificate frompeer:(mcpeerid *)peerid certificatehandler:(void(^)(bool accept))certificatehandler;
it array containing information can used identify local peer other nearby peers.
the array contains objects helps identify peer others. first secidentityref object has seckeyref object , related seccertificateref object. (it apple developer certificate , private key pair)
the other elements in array can seccertificateref objects representing intermediate certificates may needed verifying secidentityref .
the receiving peer has validate identity represented secidentityref.
below code obtain secidentityref p12 file
- (secidentityref)getclientcertificate { secidentityref identity = nil; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectorypath = [paths objectatindex:0]; nsstring *myfilepath = [documentsdirectorypath stringbyappendingpathcomponent:@"cert_key_pair.p12"]; nsdata *pkcs12data = [nsdata datawithcontentsoffile:myfilepath]; cfdataref inpkcs12data = (__bridge cfdataref)pkcs12data; cfstringref password = cfstr("password"); const void *keys[] = { ksecimportexportpassphrase };//ksecimportexportpassphrase }; const void *values[] = { password }; cfdictionaryref options = cfdictionarycreate(null, keys, values, 1, null, null); cfarrayref items = cfarraycreate(null, 0, 0, null); osstatus securityerror = secpkcs12import(inpkcs12data, options, &items); cfrelease(options); cfrelease(password); if (securityerror == errsecsuccess) { nslog(@"success opening p12 certificate. items: %ld", cfarraygetcount(items)); cfdictionaryref identitydict = cfarraygetvalueatindex(items, 0); identity = (secidentityref)cfdictionarygetvalue(identitydict, ksecimportitemidentity); } else { nslog(@"error opening certificate."); } return identity; }
obtaining policy reference object , evaluating trust
- (void)session:(mcsession *)session didreceivecertificate:(nsarray *)certificate frompeer: (mcpeerid *)peerid certificatehandler:(void (^)(bool accept))certificatehandler { seccertificateref mycert; mycert = [certificate objectatindex:0]; // 1 secpolicyref mypolicy = secpolicycreatebasicx509(); // 2 seccertificateref certarray[1] = { mycert }; cfarrayref mycerts = cfarraycreate( null, (void *)certarray, 1, null); sectrustref mytrust; osstatus status = sectrustcreatewithcertificates( mycerts, mypolicy, &mytrust); // 3 sectrustresulttype trustresult; if (status == noerr) { status = sectrustevaluate(mytrust, &trustresult); // 4 } //... if (trustresult == ksectrustresultconfirm || trustresult == ksectrustresultproceed || trustresult == ksectrustresultunspecified) // 5 { certificatehandler(yes); } // ... if (mypolicy) cfrelease(mypolicy); }
Comments
Post a Comment