api - How do I use `SFCertificateTrustPanel` in swift? -
i working on test application connects service using https. certificate of connection using custom root certificate. implemented delegate `nsurlsessiondelegate' , implemented this:
func urlsession(session: nsurlsession, didreceivechallenge challenge: nsurlauthenticationchallenge, completionhandler: (nsurlsessionauthchallengedisposition, nsurlcredential?) -> void) { if challenge.protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust { let trust: sectrustref = challenge.protectionspace.servertrust! var secresult: sectrustresulttype = sectrustresulttype(ksectrustresultinvalid) if sectrustevaluate(trust, &secresult) == errsecsuccess { switch (int(secresult)) { case ksectrustresultunspecified: break case ksectrustresultproceed: let credential = nsurlcredential(fortrust: trust) completionhandler(.usecredential, credential) return default: print("default") } } } completionhandler(.cancelauthenticationchallenge, nil) }
i case ksectrustresultproceed
, code shown here results in endless loop. run error:
nsurlsession/nsurlconnection http load failed (kcfstreamerrordomainssl, -9802)
i suspect because certificate not trusted user, read sfcertificatetrustpanel
user can accept trust certificate. yet method seems not available swift.
how use sfcertificatetrustpanel
in swift?
is there way how can trust certificate, e.g. based on fingerprint?
i don't know reason couldn't use sfcertificatetrustpanel swift, assuming you're on os x. said, doesn't can't within app.
what need modify protection space allow specific tls certificate. recommend reading apple's article "overriding tls chain validation correctly". you'll need load tls certificate nsdata object, convert seccertificateref, , add sectrustref object. (but not all) of steps covered in document linked above.
if you're going using different certificates on different servers, right way handle make connection fail when see new cert, show dialog asking user whether trust cert (e.g. giving fingerprint). if user says yes, export cert nsdata object (e.g. in der form) , store in array of trusted certs in nsuserdefaults or something, , add every item in array of certs trust object in custom trust evaluation function.
one final note: ios versions of various tls libraries have bunch of methods make easier read keys , certs files on disk, iirc, , easier convert between nsdata representations , actual certs or rsa keys. these methods available on os x, despite documentation says, necessary ios simulator function. :-)
Comments
Post a Comment