2012年1月25日水曜日

続 NSURLConnectionDelegateで証明書(ry..方法

前回の記事が適当すぎたので、少ししっかり調べたーー

参考になると幸いです

結論

SSL周りのリリースノート、仕様書とかきちんとでてますか?

知っている方いたら教えてください

環境

  • iOS 5.0
  • tomcat 7.0.23 / 6.0.32
  • xcode 4.2.1

適当にする方法(改め)

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  NSURLProtectionSpace * protectionSpace = [challenge protectionSpace];
  NSURLCredential* credentail = [NSURLCredential credentialForTrust:[protectionSpace serverTrust]];
  [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}

ハマった事

結論

RSAはOKでDSAはNG

ハマっている世界

iOS 5.0 は 証明書に MD5 を非サポートになったので、

keytool のデフォルト動作だと MD5withRSA なので

自己証明のSSL通信が軒並み エラーになると思っていた

SHA1withRSA とかkeystoreを作り直すと上のコード動いたと勘違いした。

安心したので,MD5の証明書でもやってみたら案の定動作したので、アレ?と思い

DSA で作成したら軒並みエラーなった。

--- 以上 ----

エラーの違い

xcode: Console

SHA1withRAS, MD5withRAS

SHA1withDAS

iPad : Safari : RSA

iPad : Safari : DSA

証明書一部

2012年1月23日月曜日

NSURLConnectionDelegateで証明書周りを適当にする方法

自己証明書だと、通信に失敗するでの軒並みOKにするには下のコードで行ける!!

おすすめはしない!!

※ コマンドラインでしか試してません ※


- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  return YES;
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
  return YES;
}

- (void) connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  NSURLProtectionSpace * protectionSpace = [challenge protectionSpace];
  NSURLCredential* credentail = [NSURLCredential credentialForTrust:[protectionSpace serverTrust]];
  [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}

リンク

参考またはこうすべきサイト

サンプルコード