Testing with untrusted Https

 

Testing web applications in developmental environments that attempt to utilize Https through unsigned certificates can be challenging, especially if you’ve never had the pleasure of working with Sun’s keytool utility and X.509 security certificates.

This issue manifests itself as javax.net.ssl.SSLHandshakeExceptions and sun.security.validator.ValidatorExceptions. For example, attempting to access untrusted Https through Java may yield stack traces with these tidbits:

Javax.net.ssl.SSLHandshakeException: 
 sun.security.validator.ValidatorException: 
  PKIX path building failed: 
    sun.security.provider.certpath.SunCertPathBuilderException:
	 unable to find valid certification path to requested target
...
Caused by: sun.security.validator.ValidatorException: 
 PKIX path building failed: 
  sun.security.provider.certpath.SunCertPathBuilderException:
   unable to find valid certification path to requested target
...
Caused by: sun.security.provider.certpath.SunCertPathBuilderException:
 unable to find valid certification path to requested target

Solving this problem requires two steps. First, the web server’s certificate must be captured. Then, the certificate must be imported with Sun’s keytool utility (which comes with Java).

Obtaining a copy of the certificate in X.509 format requires Microsoft’s Internet Explorer. By placing the https URL into the browser window, a dialog will pop up requesting permission to accept the certificate. Click the View Certificate button and then the Details tab. In this tab, click the Copy to File button, then click Next and select the Base-64 encoded X.509 (.CER) option. After that, click Next to save the resulting file.

Importing the .cer file requires using the keytool utility, which can be found in bin directory of a Java installation. Via this tool, the .cer file is imported into a cacerts file, which is located in the lib/security directory of a Java installation. The easiest thing to do is to copy the .cer file obtained via Internet Explorer to my Java home dir/lib/security.

For example, if using the Java sdk for 1.4.2, the location on windows could be something like: C:/j2sdk1.4.2_05/jre/lib/security.

Once the .cer file has been copied to that directory, open a command prompt and either go to the security directory or use qualified paths. Type the following command:

$ ../../bin/keytool.exe -import -storepass changeit -file mycert.cer 
 -keystore cacerts -alias mycert

The only aspects requiring changes is the name of the certificate (in this case mycert.cer) and the alias (mycert).
The keytool will issue a series of statements describing the certificate and finally request whether or not to trust the certificate. Type yes and hit enter.

The problem should be solved. Verifying things is as easy as writing a test case. For instance, the following JUnit test verifies an untrusted Https site can be hit via Jakarta’s HttpClient.

package test.com.srv.rls.https.submit;

import junit.framework.TestCase;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class HttpsSubmitTest extends TestCase {

 public void testHttpsConnection() throws Exception{
  HttpClient httpclient = new HttpClient();
  GetMethod httpget =
   new GetMethod("https://prf.acme.com:4175/invoke/ir/rve");
  try {
   httpclient.executeMethod(httpget);
   assertEquals("should have been 200",
      200, httpget.getStatusLine().getStatusCode());
  }finally {
   httpget.releaseConnection();
  }
 }
}

Now testing web applications via JUnit extensions like jWebUnit or HttpUnit is a breeze, so long as they run in the VM which contains the updated keystore.

### 解决方案概述 当遇到 `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、付费专栏及课程。

余额充值