use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.
the class CAdESSigner method getAttached.
/**
* Extracts the signed content from the digital signature structure, if it
* is a signature with attached content.
*
* @param signed
* Signature and signed content.
* @param validateOnExtract
* TRUE (to execute validation) or FALSE (not execute validation)
*
* @return content for attached signature
*/
@Override
public byte[] getAttached(byte[] signed, boolean validateOnExtract) {
byte[] result = null;
if (validateOnExtract) {
this.check(null, signed);
}
CMSSignedData signedData = null;
try {
signedData = new CMSSignedData(signed);
} catch (CMSException exception) {
throw new SignerException(cadesMessagesBundle.getString("error.invalid.bytes.pkcs7"), exception);
}
try {
CMSProcessable contentProcessable = signedData.getSignedContent();
if (contentProcessable != null) {
result = (byte[]) contentProcessable.getContent();
}
} catch (Exception exception) {
throw new SignerException(cadesMessagesBundle.getString("error.get.content.pkcs7"), exception);
}
return result;
}
use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.
the class CAdESSigner method validateTimestamp.
/**
* validade a timestampo on signature
* @param attributeTimeStamp
* @param varSignature
* @return
*/
@Deprecated
private Timestamp validateTimestamp(Attribute attributeTimeStamp, byte[] varSignature) {
try {
TimeStampOperator timeStampOperator = new TimeStampOperator();
byte[] varTimeStamp = attributeTimeStamp.getAttrValues().getObjectAt(0).toASN1Primitive().getEncoded();
TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(varTimeStamp));
Timestamp timeStampSigner = new Timestamp(timeStampToken);
timeStampOperator.validate(varSignature, varTimeStamp, null);
return timeStampSigner;
} catch (CertificateCoreException | IOException | TSPException | CMSException e) {
throw new SignerException(e);
}
}
use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.
the class GenericFactory method factoryFromClassName.
/**
* Instantiate an object from the name of your class
* @param className class name of new instance
* @return new instance
*/
@SuppressWarnings("all")
public F factoryFromClassName(String className) {
F result = null;
Class clazz = null;
try {
clazz = Class.forName(className);
} catch (Throwable error) {
throw new SignerException(cadesMessagesBundle.getString("error.class.not.exist", className), error);
}
if (clazz != null) {
try {
result = (F) clazz.newInstance();
} catch (Throwable error) {
throw new SignerException(cadesMessagesBundle.getString("error.class.incompatible", clazz.getCanonicalName()), error);
}
}
return result;
}
use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.
the class PKCS1SignerImpl method doSign.
/**
* Performs the signature using the Java API.
* It uses the algorithm value on property: algorithm.
* If this property is not set, the {@link SignerAlgorithmEnum.DEFAULT} enumeration algorithm
* will be used.
* For this method it is necessary to inform the content and the private key.
*
* @param content Content to be signed.
*/
private byte[] doSign(byte[] content) {
if (content == null) {
throw new SignerException(cadesMessagesBundle.getString("error.value.null"));
}
if (this.privateKey == null) {
throw new SignerException(cadesMessagesBundle.getString("error.private.key.null"));
}
if (this.algorithm == null) {
this.algorithm = SignerAlgorithmEnum.DEFAULT.getAlgorithm();
}
Signature sign = null;
byte[] result = null;
try {
if (this.provider != null) {
sign = Signature.getInstance(this.algorithm, this.provider);
} else {
sign = Signature.getInstance(this.algorithm);
}
sign.initSign(this.privateKey);
sign.update(content);
result = sign.sign();
} catch (NoSuchAlgorithmException exception) {
throw new SignerException(cadesMessagesBundle.getString("error.load.algorithm", algorithm), exception);
} catch (InvalidKeyException exception) {
throw new SignerException(cadesMessagesBundle.getString("error.private.key.invalid"), exception);
} catch (SignatureException exception) {
throw new SignerException(cadesMessagesBundle.getString("error.sign.exception"), exception);
}
return result;
}
use of org.demoiselle.signer.policy.impl.cades.SignerException in project signer by demoiselle.
the class PKCS1SignerImpl method check.
/**
* Performs checking for signed content using the Java API.
* You must enter the original content and signature for verification.
* It uses the value algorithm of property: algorithm. If this property is not set,
* the @link SignerAlgorithmEnum.DEFAULT enumeration algorithm will be used.
* For this method it is necessary to inform the original content, signed content and the public key.
*
* @param content Original content to be compared to signed content.
* @param signed Signed content to be verified.
*/
@Override
public boolean check(byte[] content, byte[] signed) {
if (content == null) {
throw new SignerException(cadesMessagesBundle.getString("error.value.null"));
}
if (signed == null) {
throw new SignerException(cadesMessagesBundle.getString("error.content.signed.null"));
}
if (this.publicKey == null) {
throw new SignerException(cadesMessagesBundle.getString("error.public.key.null"));
}
if (this.algorithm == null) {
this.algorithm = SignerAlgorithmEnum.DEFAULT.getAlgorithm();
}
Signature signature = null;
boolean result = false;
try {
if (this.provider != null) {
signature = Signature.getInstance(this.algorithm, this.provider);
} else {
signature = Signature.getInstance(this.algorithm);
}
signature.initVerify(this.publicKey);
signature.update(content);
result = signature.verify(signed);
} catch (NoSuchAlgorithmException exception) {
throw new SignerException(cadesMessagesBundle.getString("error.load.algorithm", this.algorithm), exception);
} catch (InvalidKeyException exception) {
throw new SignerException(cadesMessagesBundle.getString("error.public.key.invalid"), exception);
} catch (SignatureException exception) {
throw new SignerException(cadesMessagesBundle.getString("error.check.exception"), exception);
}
return result;
}
Aggregations