IOS - CredStore - performQuery - Error copying matching creds - ios

I am getting the erro below when i try to call the custom API that i have, i am using alamofire
let API_URL = "https://localhost:5001/api"
func registerNewUser(parameters: Parameters) {
let urlString = API_URL + "/register/PostTest"
let url = URL.init(string: urlString)
print("URL: \(url!)")
Alamofire.request(url!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).debugLog().responseJSON{ //--> point of failure
response in
if response.result.isSuccess {
print("Success! the request to the API is successful")
let userJson : JSON = JSON(response.result.value!)
print(userJson)
} else {
print("Error \(response.result.error!)")
}
}
}
info.plist file
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>https://localhost</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
The Error is:
CredStore - performQuery - Error copying matching creds. Error=-25300, query={
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = htps;
"r_Attributes" = 1;
sdmn = localhost;
srvr = localhost;
sync = syna;
}

Related

SWIFT - Https call to private restfull api is failing

I need to convert my SWIFT code to fit HTTPS requests on private API.
I just need to understand how to make the request in Swift, at the moment changed the URL from http://...:_port/... to https://...:_port/... but I am getting those errors:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9814)
nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue: [-9814]
nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue: [-9814]
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9814)
I also update my info.plist to this, but still getting errors
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>domain.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
</plist>
And this is the code
let task = session.dataTask(with: requestType as URLRequest) { (data, response, error) in
guard let _: Data = data, let _: URLResponse = response, error == nil else {
return
}
self.list = try! JSONDecoder().decode(_myObj.self, from:data!)
}
task.resume()

TIC SSL Trust Error while trying to read a txt file

I'm having this problem with reading from a txt file from a website without a certificate even though I have allowed it in Info.plist.
My Swift code looks like this:
let url = URL(string: "http://newskit.matsworld.io/domaci/text.txt")!
let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
if let localURL = localURL {
if let string = try? String(contentsOf: localURL) {
print(string)
}
}
}
task.resume()
And the Info.plist looks like this:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://newskit.matsworld.io</key>
<dict>
<key>ExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>IncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
I'm stuck with this and can't figure it out. Thanks for help!
The domain key is supposed to be just the domain without the scheme (http) and without subdomains if IncludesSubdomains is specified
<key>matsworld.io</key>

NSURLSession - DownloadTask doesn't work with some URL [duplicate]

This question already has answers here:
NSURLSession/NSURLConnection HTTP load failed on iOS 9
(13 answers)
Closed 6 years ago.
Here the code:
let url = NSURL(string: "http://a337.phobos.apple.com/us/r30/Music/d5/a8/6b/mzi.msfqeogi.aac.p.m4a")
let downloadTask = self.downloadSession.downloadTaskWithURL(url!)
downloadTask.resume()
-> OK
Replace the URL with:
http://s1mp3.r41s223.vcdn.vn/d3f549abeeef07b15efe/2937387453157939420?key=c2WuQChtsmR9yfdz9I-jnw&expires=1480695588&filename=Only%20Love%20-%20Trademark.mp3
-> it doesn't work; nothing happens
By default starting in iOS 9 all connections must be https. If the website for the second URL doesn't support https:// then the download would fail.
I suggest checking the error code you get back from the Download
If you want to use url with HTTP connection, then in your info.plist file add the following
App Transport Security Setting and in that add subsection Allow Arbitrary Loads and set the value to YES.
Please add in Plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>akamaihd.net</key>
<dict>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>facebook.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>graph.facebook.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
</plist>
Then using Alamofire 4.0 Swift 3.0
Using below method, you can download any thing, ex. PDF
// Mark:- Download File
func DownloadFile(strFileUrl : String, strFileName : String, success:#escaping (_ strResultURL: String) -> Void , failure:#escaping (_ responseObject:AnyObject) -> Void) {
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("\(strFileName)")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(strFileUrl, to: destination).response { response in
if SVProgressHUD.isVisible() {
SVProgressHUD.dismiss()
}
if response.error == nil {
success("\(response.destinationURL!)")
}
else {
failure(response.error as AnyObject)
}
}.downloadProgress { (progress) in
print("Download Progress: \(progress.fractionCompleted)")
SVProgressHUD.showProgress(Float(progress.fractionCompleted), status: AppAlertMsg.KDownloadingPDFs)
}
}

Swift: How to Make Https Request Using Server SSL Certificate

Hi I want to make Https Request in Swift. Currently im accessing local server through ip address. Local Server has one SSL Certificate by accessing the certificate want to make request to server currently im doing like this.
Alamofire.request(.GET, https://ipaddress//, parameters: [param], headers: headers)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
I have used the above code for making request and in plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>192.168.2.223:1021(my local ip)</key>
<dict>
Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow insecure HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
in plist i have given like this but im still getting error like
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
IMPORTANT: DO NOT USE THIS IN PRODUCTION. Basically, with Alamofire, you can bypass the authentication for app development and testing purpose. Make sure you remove it before the app is on the App Store or in production:-
func bypassURLAuthentication() {
let manager = Alamofire.Manager.sharedInstance
manager.delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
var credential: NSURLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = NSURLSessionAuthChallengeDisposition.UseCredential
credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .CancelAuthenticationChallenge
} else {
credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
if credential != nil {
disposition = .UseCredential
}
}
}
return (disposition, credential)
}
}
Thank You!
Let me know if this helps. :)

Geting NSURLConnection error while calling HTTPS Soap service

I am trying to get data from Soap request but getting my app crashed with this error:
2016-02-08 11:12:11.524 Example[567:128898] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
Response: nil
fatal error: unexpectedly found nil while unwrapping an Optional value
I have enabled Security settings in plist and calling other services successfully.
Note: The service is on HTTPS, am i getting this error because of this? is this required any certificate?
i am calling soap service for the first time, please also guide me on how to add email parameter in service: (https://[IP]:9897/JLIPolicyinfo.asmx?op=GetEmail_Poldetail)
here is my code that i get from here: (IOS Swift Call Web Service using SOAP)
let is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"><soapenv:Header/><soapenv:Body><cgs:GetSystemStatus/></soapenv:Body></soapenv:Envelope>"
let is_URL: String = "https://58.27.241.203:9897/JLIPolicyinfo.asmx"
let lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
let session = NSURLSession.sharedSession()
let err: NSError?
lobj_Request.HTTPMethod = "POST"
lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
lobj_Request.addValue("58.27.241.203", forHTTPHeaderField: "Host")
lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String((is_SoapMessage.characters.count)), forHTTPHeaderField: "Content-Length")
//lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
lobj_Request.addValue("http://tempuri.org/GetEmail_Poldetail", forHTTPHeaderField: "SOAPAction")
let task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")
let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)")
if error != nil
{
print("Error: " + error!.description)
}
})
task.resume()
Here is my plist addition:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>https://58.27.241.203:9897/JLIPolicyinfo.asmx</key>
<dict>
<key>NSIncludesSubdomains</key>
<false/>
<key>NSExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
You might have a self signed certificate that can be also a problem .
Solution:-
It will work if you renew your server's ssl certificate to use SHA2 and enabling TLS v1.2.
Possibly a solution:-
You have to add this in your info.plist file, so that it can allow non-secure connection:-
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Or even you can add like this:-
Edit:-
Add this in your info.plist for specific exception.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>testdomain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<false/>
<key>NSExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>

Resources