use of xades4j.verification.UnexpectedJCAException in project xades4j by luisgoncalves.
the class KeyStoreKeyingDataProvider method ensureInitialized.
private void ensureInitialized() throws UnexpectedJCAException {
synchronized (this.lockObj) {
if (!this.initialized) {
try {
KeyStore.CallbackHandlerProtection storeLoadProtec = null;
if (storePasswordProvider != null)
// Create the load protection with callback.
storeLoadProtec = new KeyStore.CallbackHandlerProtection(new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
PasswordCallback c = (PasswordCallback) callbacks[0];
c.setPassword(storePasswordProvider.getPassword());
}
});
else
// If no load password provider is supplied is because it shouldn't
// be needed. Create a dummy protection because the keystore
// builder needs it to be non-null.
storeLoadProtec = new KeyStore.CallbackHandlerProtection(new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
throw new UnsupportedOperationException("No KeyStorePasswordProvider");
}
});
this.keyStore = builderCreator.getBuilder(storeLoadProtec).getKeyStore();
} catch (KeyStoreException ex) {
throw new UnexpectedJCAException("The keystore couldn't be initialized", ex);
}
this.initialized = true;
}
}
}
use of xades4j.verification.UnexpectedJCAException in project xades4j by luisgoncalves.
the class KeyStoreKeyingDataProvider method getSigningKey.
@Override
public PrivateKey getSigningKey(X509Certificate signingCert) throws SigningKeyException, UnexpectedJCAException {
ensureInitialized();
try {
// The certificate supplied by the library is always the first certificate
// in the chain supplied by getSigningCertificateChain, which means
// that an entry will always be present. Also, this entry is always
// a PrivateKeyEntry.
String entryAlias = this.keyStore.getCertificateAlias(signingCert);
KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) this.keyStore.getEntry(entryAlias, getKeyProtection(entryAlias, signingCert, this.entryPasswordProvider));
return entry.getPrivateKey();
} catch (UnrecoverableKeyException ex) {
throw new SigningKeyException("Invalid key entry password", ex);
} catch (GeneralSecurityException ex) {
// KeyStoreException
throw new UnexpectedJCAException(ex.getMessage(), ex);
}
}
use of xades4j.verification.UnexpectedJCAException in project xades4j by luisgoncalves.
the class PKIXCertificateValidationProvider method validate.
@Override
public ValidationData validate(X509CertSelector certSelector, Date validationDate, Collection<X509Certificate> otherCerts) throws CertificateValidationException, UnexpectedJCAException {
PKIXBuilderParameters builderParams;
try {
builderParams = new PKIXBuilderParameters(trustAnchors, certSelector);
} catch (KeyStoreException ex) {
throw new CannotBuildCertificationPathException(certSelector, "Trust anchors KeyStore is not initialized", ex);
} catch (InvalidAlgorithmParameterException ex) {
throw new CannotBuildCertificationPathException(certSelector, "Trust anchors KeyStore has no trusted certificate entries", ex);
}
PKIXCertPathBuilderResult builderRes;
try {
// - The other certificates from the signature (e.g. from KeyInfo).
if (otherCerts != null) {
CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters(otherCerts);
CertStore othersCertStore = CertStore.getInstance("Collection", ccsp);
builderParams.addCertStore(othersCertStore);
}
// - The external certificates/CRLs.
for (int i = 0; i < intermCertsAndCrls.length; i++) {
builderParams.addCertStore(intermCertsAndCrls[i]);
}
builderParams.setRevocationEnabled(revocationEnabled);
builderParams.setMaxPathLength(maxPathLength);
builderParams.setDate(validationDate);
builderParams.setSigProvider(this.signatureProvider);
builderRes = (PKIXCertPathBuilderResult) certPathBuilder.build(builderParams);
} catch (CertPathBuilderException ex) {
throw new CannotBuildCertificationPathException(certSelector, ex.getMessage(), ex);
} catch (InvalidAlgorithmParameterException ex) {
// cannot be applied.
throw new CannotSelectCertificateException(certSelector, ex);
} catch (NoSuchAlgorithmException ex) {
// SHOULD NOT be thrown.
throw new UnexpectedJCAException("No provider for Collection CertStore", ex);
}
// The cert path returned by the builder ends in a certificate issued by
// the trust anchor. However, the complete path may be needed for property
// verification.
List<X509Certificate> certPath = (List<X509Certificate>) builderRes.getCertPath().getCertificates();
// - Create a new list since the previous is immutable.
certPath = new ArrayList<X509Certificate>(certPath);
// - Add the trust anchor certificate.
certPath.add(builderRes.getTrustAnchor().getTrustedCert());
if (revocationEnabled) {
return new ValidationData(certPath, getCRLsForCertPath(certPath, validationDate));
}
return new ValidationData(certPath);
}
use of xades4j.verification.UnexpectedJCAException in project xades4j by luisgoncalves.
the class KeyStoreKeyingDataProvider method getSigningCertificateChain.
@Override
public List<X509Certificate> getSigningCertificateChain() throws SigningCertChainException, UnexpectedJCAException {
ensureInitialized();
try {
List<X509Certificate> availableSignCerts = new ArrayList<X509Certificate>(keyStore.size());
for (Enumeration<String> aliases = keyStore.aliases(); aliases.hasMoreElements(); ) {
String alias = aliases.nextElement();
if (keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
Certificate cer = keyStore.getCertificate(alias);
if (cer instanceof X509Certificate)
availableSignCerts.add((X509Certificate) cer);
}
}
if (availableSignCerts.isEmpty())
throw new SigningCertChainException("No certificates available in the key store");
// Select the signing certificate from the available certificates.
X509Certificate signingCert = this.certificateSelector.selectCertificate(availableSignCerts);
String signingCertAlias = this.keyStore.getCertificateAlias(signingCert);
if (null == signingCertAlias)
throw new SigningCertChainException("Selected certificate not present in the key store");
Certificate[] signingCertChain = this.keyStore.getCertificateChain(signingCertAlias);
if (null == signingCertChain)
throw new SigningCertChainException("Selected certificate doesn't match a key and corresponding certificate chain");
if (this.returnFullChain) {
List lChain = Arrays.asList(signingCertChain);
return Collections.checkedList(lChain, X509Certificate.class);
} else
return Collections.singletonList((X509Certificate) signingCertChain[0]);
} catch (KeyStoreException ex) {
// keystore is not loaded.
throw new UnexpectedJCAException(ex.getMessage(), ex);
}
}
Aggregations