use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.
the class KeyInfoBuilder method buildKeyInfo.
void buildKeyInfo(X509Certificate signingCertificate, XMLSignature xmlSig) throws KeyingDataException, UnsupportedAlgorithmException {
// Check key usage.
// - KeyUsage[0] = digitalSignature
// - KeyUsage[1] = nonRepudiation
boolean[] keyUsage = signingCertificate.getKeyUsage();
if (keyUsage != null && !keyUsage[0] && !keyUsage[1]) {
throw new SigningCertKeyUsageException(signingCertificate);
}
try {
signingCertificate.checkValidity();
} catch (CertificateException ce) {
// CertificateExpiredException or CertificateNotYetValidException
throw new SigningCertValidityException(signingCertificate);
}
if (this.basicSignatureOptionsProvider.includeSigningCertificate()) {
try {
X509Data x509Data = new X509Data(xmlSig.getDocument());
x509Data.addCertificate(signingCertificate);
x509Data.addSubjectName(signingCertificate);
x509Data.addIssuerSerial(signingCertificate.getIssuerX500Principal().getName(), signingCertificate.getSerialNumber());
xmlSig.getKeyInfo().add(x509Data);
if (this.basicSignatureOptionsProvider.signSigningCertificate()) {
String keyInfoId = xmlSig.getId() + "-keyinfo";
xmlSig.getKeyInfo().setId(keyInfoId);
// Use same canonicalization URI as specified in the ds:CanonicalizationMethod for Signature.
Algorithm canonAlg = this.algorithmsProvider.getCanonicalizationAlgorithmForSignature();
CanonicalizerUtils.checkC14NAlgorithm(canonAlg);
Transforms transforms = TransformUtils.createTransforms(canonAlg, this.algorithmsParametersMarshaller, xmlSig.getDocument());
xmlSig.addDocument('#' + keyInfoId, transforms, this.algorithmsProvider.getDigestAlgorithmForDataObjsReferences());
}
} catch (XMLSignatureException ex) {
throw new UnsupportedAlgorithmException("Digest algorithm not supported in the XML Signature provider", this.algorithmsProvider.getDigestAlgorithmForDataObjsReferences(), ex);
} catch (XMLSecurityException ex) {
throw new KeyingDataException(ex.getMessage(), ex);
}
}
if (this.basicSignatureOptionsProvider.includePublicKey()) {
xmlSig.addKeyInfo(signingCertificate.getPublicKey());
}
}
use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.
the class DataGenBaseTimeStamp method generatePropertyData.
@Override
public final PropertyDataObject generatePropertyData(TProp prop, PropertiesDataGenerationContext ctx) throws PropertyDataGenerationException {
Algorithm c14n = this.algsProvider.getCanonicalizationAlgorithmForTimeStampProperties();
try {
TimeStampDigestInput digestInput = this.tsInputFactory.newTimeStampDigestInput(c14n);
addPropSpecificTimeStampInput(prop, digestInput, ctx);
TimeStampTokenRes tsTknRes = this.tsTokenProvider.getTimeStampToken(digestInput.getBytes(), this.algsProvider.getDigestAlgorithmForTimeStampProperties());
return createPropDataObj(prop, c14n, tsTknRes, ctx);
} catch (UnsupportedAlgorithmException ex) {
throw new PropertyDataGenerationException(prop, ex.getMessage(), ex);
} catch (CannotAddDataToDigestInputException ex) {
throw new PropertyDataGenerationException(prop, "cannot create time stamp input", ex);
} catch (TimeStampTokenGenerationException ex) {
throw new PropertyDataGenerationException(prop, "cannot get a time-stamp", ex);
}
}
use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.
the class FromXmlBaseTimeStampConverter method convertTimeStamps.
protected void convertTimeStamps(List<XmlXAdESTimeStampType> xmlTimeStamps, QualifyingPropertiesDataCollector propertyDataCollector) throws PropertyUnmarshalException {
if (null == xmlTimeStamps || xmlTimeStamps.isEmpty())
return;
for (XmlXAdESTimeStampType xmlTS : xmlTimeStamps) {
if (!xmlTS.getReferenceInfo().isEmpty())
throw new PropertyUnmarshalException("ReferenceInfo is not supported in XAdESTimeStamp", propName);
Algorithm c14n;
XmlCanonicalizationMethodType xmlCanonMethod = xmlTS.getCanonicalizationMethod();
if (null == xmlCanonMethod) {
c14n = new GenericAlgorithm(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
} else {
List params = CollectionUtils.filterByType(xmlCanonMethod.getContent(), Element.class);
c14n = new GenericAlgorithm(xmlCanonMethod.getAlgorithm(), params);
}
TPropData tsData = createTSData(c14n);
List<Object> tsTokens = xmlTS.getEncapsulatedTimeStampOrXMLTimeStamp();
if (tsTokens.isEmpty())
throw new PropertyUnmarshalException("No time-stamp tokens", propName);
for (Object tkn : tsTokens) {
if (!(tkn instanceof XmlEncapsulatedPKIDataType))
throw new PropertyUnmarshalException("XML time-stamps are not supported", propName);
tsData.addTimeStampToken(((XmlEncapsulatedPKIDataType) tkn).getValue());
}
doSpecificConvert(xmlTS, tsData);
setTSData(tsData, propertyDataCollector);
}
}
use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.
the class ToXmlBaseTimeStampConverter method convertIntoObjectTree.
@Override
public final void convertIntoObjectTree(PropertyDataObject propData, TXml xmlProps, Document doc) {
TData tsData = (TData) propData;
XmlXAdESTimeStampType xmlTimeStamp = new XmlXAdESTimeStampType();
// Canonicalization method
XmlCanonicalizationMethodType xmlCanon = new XmlCanonicalizationMethodType();
xmlTimeStamp.setCanonicalizationMethod(xmlCanon);
Algorithm c14n = tsData.getCanonicalizationAlgorithm();
xmlCanon.setAlgorithm(c14n.getUri());
try {
List<Node> c14nParams = this.algorithmsParametersMarshallingProvider.marshalParameters(c14n, doc);
if (c14nParams != null) {
xmlCanon.getContent().addAll(c14nParams);
}
} catch (UnsupportedAlgorithmException ex) {
// Do not throw any specific exception for now.
throw new IllegalArgumentException("Cannot marshall algorithm parameters", ex);
}
// Time-stamp tokens
List<byte[]> tsTokens = tsData.getTimeStampTokens();
List<Object> xmlTSTokens = xmlTimeStamp.getEncapsulatedTimeStampOrXMLTimeStamp();
for (byte[] tsToken : tsTokens) {
XmlEncapsulatedPKIDataType xmlTSTkn = new XmlEncapsulatedPKIDataType();
xmlTSTkn.setValue(tsToken);
xmlTSTokens.add(xmlTSTkn);
}
insertIntoObjectTree(xmlTimeStamp, xmlProps, tsData);
}
use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.
the class SignerBES method createSignature.
private XMLSignature createSignature(Document signatureDocument, String baseUri, String signingKeyAlgorithm) throws XAdES4jXMLSigException, UnsupportedAlgorithmException {
Algorithm signatureAlg = this.algorithmsProvider.getSignatureAlgorithm(signingKeyAlgorithm);
if (null == signatureAlg) {
throw new NullPointerException("Signature algorithm not provided");
}
Element signatureAlgElem = createElementForAlgorithm(signatureAlg, Constants._TAG_SIGNATUREMETHOD, signatureDocument);
Algorithm canonAlg = this.algorithmsProvider.getCanonicalizationAlgorithmForSignature();
if (null == canonAlg) {
throw new NullPointerException("Canonicalization algorithm not provided");
}
Element canonAlgElem = createElementForAlgorithm(canonAlg, Constants._TAG_CANONICALIZATIONMETHOD, signatureDocument);
try {
return new XMLSignature(signatureDocument, baseUri, signatureAlgElem, canonAlgElem);
} catch (XMLSecurityException ex) {
// Following the code, doesn't seem to be thrown at all.
throw new XAdES4jXMLSigException(ex.getMessage(), ex);
}
}
Aggregations