У меня есть этот код Java:
Код: Выделить всё
public static CertPath createCertPath(X509Certificate certificate, Set trustAnchors)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException, CertPathBuilderException, CertStoreException {
Security.addProvider(new BouncyCastleProvider());
CertStore certStore = CertStoreCreator.createCertStore(certificate, trustAnchors); // Verificado
// Get CertPathParameters using the previous function
CertPathParameters certPathParameters = getCertPathParameters(certificate, trustAnchors);
/*
// Add the CertStore to the PKIX parameters
if (certPathParameters instanceof PKIXBuilderParameters) {
((PKIXBuilderParameters) certPathParameters).addCertStore(certStore);
System.out.println("CertStore adicionado aos parâmetros PKIX.");
}
//System.out.println(certPathParameters);
*/
CertStore intermediate = CertStore.getInstance("Collection", new CollectionCertStoreParameters(Arrays.asList(certStore)));
((PKIXBuilderParameters) certPathParameters).addCertStore(intermediate);
try {
CertPathBuilder certPathBuilder = CertPathBuilder.getInstance("PKIX", "BC");
CertPathBuilderResult result = certPathBuilder.build(certPathParameters);
CertPath certPath = result.getCertPath();
// Imprimir todos os certificados no CertPath resultante
System.out.println("Certificados no CertPath:");
for (Certificate cert : certPath.getCertificates()) {
System.out.println(((X509Certificate) cert).getSubjectX500Principal());
}
return certPath;
} catch (CertPathBuilderException e) {
System.out.println("ERRO: Não foi possível construir o CertPath: " + e.getMessage());
} catch (Exception e) {
System.out.println("ERRO inesperado: " + e.getMessage());
}
return null;
}
/**
* Cria os parâmetros para a construção do caminho de certificação
*
* @param certificate O certificado final do caminho de certificação
* @param trustAnchors As possíveis âncoras de confiança para o caminho de certificação
* @return Os parâmetros para a construção do caminho de certificação
* @see CertPathCreator#createCertPath(X509Certificate, Set)
*/
@ImplementMe
public static CertPathParameters getCertPathParameters(X509Certificate certificate,
Set trustAnchors)
throws InvalidAlgorithmParameterException {
// Código adaptado da resposta de:
// https://stackoverflow.com/questions/13671487/generate-x509certificate-certpath-in-java
Security.addProvider(new BouncyCastleProvider());
// Define o certificado final
X509CertSelector certSelector = new X509CertSelector();
certSelector.setCertificate(certificate);
// Cria os parametros
PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(trustAnchors, certSelector);
pkixParams.setRevocationEnabled(false);
return pkixParams;
}
Я ожидал certPath со всеми сертификатами в цепочке доверия, но я получил только последний в цепочке доверия.< /п>
Подробнее здесь: https://stackoverflow.com/questions/791 ... th-in-java
Мобильная версия