iPhone HTTPS 访问非信任站点(使用非信任证书,untrusted certificate)

本文介绍了一种通过私有API实现对自签名或不受信任的证书进行HTTPS连接的方法。提供了NSURLConnection代理方法的具体实现细节,允许应用程序接受特定主机的信任。此外,还推荐了一个开源库ASIHTTPRequest,它支持使用自签名证书的HTTPS连接。
1. There is a supported API for accomplishing this! Add something like this to your NSURLConnection delegate:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    if ([trustedHosts containsObject:challenge.protectionSpace.host])
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

  [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

Note that connection:didReceiveAuthenticationChallenge: can send its message to challenge.sender (much) later, after presenting a dialog box to the user if necessary, etc.

2.If you're unwilling (or unable) to use private APIs, there's an open source (BSD license) library called ASIHTTPRequest that provides a wrapper around the lower-level CFNetwork APIs. They recently introduced the ability to allow HTTPS connections using self-signed or untrusted certificates with the -setValidatesSecureCertificate: API. If you don't want to pull in the whole library, you could use the source as a reference for implementing the same functionality yourself.

### 解决方案概述 当遇到 `500 Error` 并提示 `SSL peer certificate untrusted` 时,通常是因为服务器无法验证客户端证书的有效性。这可能是由于以下几个原因引起的: 1. 客户端证书未被信任的 CA 验证。 2. 服务器配置不正确,缺少中间证书或根证书。 3. OpenSSL 或其他加密库未能正确解析或加载证书链。 以下是针对该问题的具体分析和解决方案[^1]。 --- ### 可能的原因及对应措施 #### 1. **客户端证书未被信任** 如果客户端证书是由不受信任的 CA 签发的,则服务器会拒绝连接并返回错误。可以通过以下方法解决: - 确认客户端使用的证书是否由受信 CA 签名。 - 如果使用的是自签名证书,需将其导入到服务器的信任存储中。 ```bash openssl x509 -in client-cert.pem -text -noout ``` 通过上述命令检查证书的签发者字段 (`Issuer`) 是否为已知可信 CA[^3]。 --- #### 2. **服务器配置缺失中间证书** 某些情况下,CA 提供的根证书可能不足以完成完整的证书链验证。需要额外提供中间证书来补全链条。可以按照以下步骤操作: - 获取完整的证书链文件(通常是 `.pem` 格式),其中包含根证书、中间证书以及最终实体证书。 - 将其配置到 Web 服务器中。例如,在 Nginx 中设置如下参数: ```nginx ssl_certificate /path/to/fullchain.pem; ssl_certificate_key /path/to/private.key; ``` 对于 HAProxy 用户,可参考以下配置片段[^2]: ```haproxy frontend https-in bind *:443 ssl crt /etc/haproxy/certs/ req_ssl_sni var(txn.sni) acl valid_cert ssl_crt_file(/etc/haproxy/certs/%[req.ssl_sni]) http-request deny unless valid_cert ``` 确保路径下的证书文件支持 SNI 扩展功能,并启用 TLS 扩展支持。 --- #### 3. **OpenSSL 库版本过旧** 如果运行环境中的 OpenSSL 版本较老,可能会存在兼容性问题或者缺乏必要的安全特性。建议升级至最新稳定版(如 OpenSSL 1.1.x 或更高)。更新完成后重新编译依赖的服务程序以应用新功能。 --- #### 4. **调试工具辅助排查** 为了进一步定位具体失败环节,推荐利用以下工具捕获详细的握手日志数据: - 使用 Wireshark 抓取网络流量包,观察是否存在异常断开事件。 - 启用 OpenSSL 的 verbose 日志模式查看更详尽的信息输出: ```c SSL_CTX_set_info_callback(ctx, app_info_cb); void app_info_cb(const SSL *ssl, int type, int val) { printf("Info callback called with %d:%d\n", type, val); } ``` 此回调函数能够展示每一步状态变化情况以便于诊断潜在隐患所在位置[^1]。 --- ### 总结 综上所述,“500 error SSL peer certificate untrusted”的根本原因是认证过程中出现了不可接受的情况。通过对以上几个方面的逐一核查调整即可有效消除此类故障现象。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值