gpt4 book ai didi

java - 未使用 goDaddy 证书验证 HTTPS 主机名

转载 作者:行者123 更新时间:2023-11-30 01:08:01 33 4
gpt4 key购买 nike

我正在尝试使用端点下列出的两个 goDaddy 证书访问我的服务器。堆栈中的三个证书是 My cert > Go Daddy Secure Certificate Authority -G2 > Go Daddy Root Certificate Authority - G2。我从 Go Daddy 存储库下载了安全证书和根证书,现在已将它们添加到我的 android 应用程序原始资源文件夹中。即使两者都在那里,它仍然给我这个错误

javax.net.ssl.SSLPeerUnverifiedException: Hostname  not verified:

我不知道下一步该做什么。我尝试了很多组合,所以我认为我需要一种不同的方式来做到这一点。

这是我目前所拥有的;我的 HttpsClient 代码;

public class MyHttpsGet extends AsyncTask<String, Void, String> {

Context context;

int cert;
int interCert;
boolean allowHost;
private String username;
private String password;

//this is used if you need a password and username
//mainly for logins to a webserver
public MyHttpsGet(String username, String password, Context context, int cert, int intermedCert)
{
this.context = context;
this.cert = cert;
this.interCert = intermedCert;
this.username = username;
this.password = password;

}

//used for image downloading
public MyHttpsGet(){}

@Override
protected String doInBackground(String... params) {
String url = params[0];
return httpsDownloadData(url, context, cert, interCert);
}

public String httpsDownloadData (String urlString, Context context, int certRawResId, int certIntermedResId)
{
String respone = null;

try {
// build key store with ca certificate
KeyStore keyStore = buildKeyStore(context, certRawResId, certIntermedResId);


// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

// Create an SSLContext that uses our TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);

// Create a connection from url
URL url = new URL(urlString);
if (username != null) {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());


int statusCode = urlConnection.getResponseCode();
Log.d("Status code: ", Integer.toString(statusCode));


InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
respone = streamToString(inputStream);
inputStream.close();
}

}catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}

Log.d("MyHttps Respones: ", respone);
return respone;
}


private static KeyStore buildKeyStore(Context context, int certRawResId, int interCert){
// init a default key store
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);

// read and add certificate authority
Certificate cert2 = readCert(context, interCert);
Certificate cert = readCert(context, certRawResId);
keyStore.setCertificateEntry("ca" , cert2);
keyStore.setCertificateEntry("ca", cert);



} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return keyStore;

}

private static Certificate readCert(Context context, int certResourceId) throws IOException {

// read certificate resource
InputStream caInput = context.getResources().openRawResource(certResourceId);

Certificate ca = null;
try {
// generate a certificate
CertificateFactory cf = CertificateFactory.getInstance("X.509");
ca = cf.generateCertificate(caInput);
} catch (CertificateException e) {
e.printStackTrace();
} finally {
caInput.close();
}

return ca;
}

//this is used for downloading strings from an http or https connection
private String streamToString(InputStream is) throws IOException {

StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}

return sb.toString();
}


}

我是这样称呼/使用它的:

 MyHttpsGet task = new MyHttpsGet(username, password,myContext, R.raw.gdroot_g2, R.raw.gdintermed);
try {
myJson = task.execute(myUrl).get();
Log.d("Json: " , myJson);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
new runningMan().execute();

感谢您对此的任何帮助。

这是我的证书链的图片 enter image description here

最佳答案

错误消息显示 apivitacrm.elasticbeanstalk.com,但您随后涂掉了证书中的通配符名称。为什么?

好吧,不管它是什么,它看起来都是以a开头的,所以它肯定不是*.elasticbeanstalk.com通配符证书。

这意味着错误信息是正确的。证书不属于给定的域名。

即使它是 *.apivitacrm.elasticbeanstalk.com 通配符(尽管如此,blackout 似乎还不够宽),它仍然不匹配 apivitacrm.elasticbeanstalk。 com,因为它只匹配子域。

关于java - 未使用 goDaddy 证书验证 HTTPS 主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711241/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com