gpt4 book ai didi

JAVA-Android-根据 CA 证书(颁发者证书)验证 X509 证书

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:06 26 4
gpt4 key购买 nike

可能这是重复的问题,但我没有从上一个问题中完全清楚,这就是我发布新问题的原因。请看看这个。我会将 Ca 证书放在我的资源文件夹中以验证 ca 认证的证书,服务器中也会有相同的 ca 证书。

  1. 我正在创建没有任何证书签名的 .crt 文件并将其发送到服务器。
  2. 服务器将使用 ca 证书签署 .crt 文件并将该文件再次发回给我。
  3. 收到签名的 crt 文件后,我需要使用资源文件夹中已有的 ca 证书进行验证..

我可以使用以下代码用我的 ca 证书创建一个信任管理器:

AssetManager assetManager = getResources().getAssets();
InputStream inputStream = null;

try {
inputStream = assetManager.open("Issuer certificate");
if (inputStream != null)
} catch (IOException e) {
e.printStackTrace();
}
InputStream caInput = new BufferedInputStream(inputStream);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
System.out.println("ca="
+ ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

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

TrustManagerFactory tmf = TrustManagerFactory
.getInstance(tmfAlgorithm);
tmf.init(keyStore);

在获得这个信任管理器后,我应该如何比较我从服务器获得的 crt 证书...我的疑问:我是否需要创建另一个信任管理器,并且在让这两个信任管理器比较任何提供者名称之后???如果我错了,请提供有关此过程的任何信息。

最佳答案

最终能够通过以下过程验证证书。我希望这对其他人有帮助...

public void validateCertificate() throws Exception {
try {
String issuerCertPath = "Issuer Certifate";
String certPath = "Issued Certificate";
X509Certificate issuerCert = getCertFromFile(issuerCertPath);
X509Certificate c1 = getCertFromFile(certPath);
TrustAnchor anchor = new TrustAnchor(issuerCert, null);
Set anchors = Collections.singleton(anchor);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
List list = Arrays.asList(new Certificate[] { c1 });
CertPath path = cf.generateCertPath(list);
PKIXParameters params = new PKIXParameters(anchors);
params.setRevocationEnabled(false);
CertPathValidator validator = CertPathValidator.getInstance("PKIX");
PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult) validator
.validate(path, params);
// If
// not
// valid
// will
// throw
System.out.println("VALID");
} catch (Exception e) {
System.out.println("EXCEPTION " + e.getMessage());
e.printStackTrace();
}
}

private X509Certificate getCertFromFile(String path) throws Exception {
AssetManager assetManager = MyActivity.this.getResources().getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open(path);
} catch (IOException e) {
e.printStackTrace();
}
InputStream caInput = new BufferedInputStream(inputStream);
X509Certificate cert = null;
CertificateFactory cf = CertificateFactory.getInstance("X509");
cert = (X509Certificate) cf.generateCertificate(caInput);
cert.getSerialNumber();
return cert;
}

关于JAVA-Android-根据 CA 证书(颁发者证书)验证 X509 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822516/

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