use of xades4j.providers.impl.DirectKeyingDataProvider in project xades4j by luisgoncalves.
the class SignerSpecificTest method signWithNationalCertificate.
@Test
public void signWithNationalCertificate() throws Exception {
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
keyGen.initialize(1024, new SecureRandom());
Date validityBeginDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
// 1 year
long add = (1L * 365L * 24L * 60L * 60L * 1000L);
Date validityEndDate = new Date(System.currentTimeMillis() + add);
KeyPair keyPair = keyGen.generateKeyPair();
X509Certificate certWithNationalSymbols;
{
// generate certificate with national symbols in DN
X500NameBuilder x500NameBuilder = new X500NameBuilder();
AttributeTypeAndValue attr = new AttributeTypeAndValue(RFC4519Style.cn, commonName);
x500NameBuilder.addRDN(attr);
X500Name dn = x500NameBuilder.build();
X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(// issuer authority
dn, // serial number of certificate
BigInteger.valueOf(new Random().nextInt()), // start of validity
validityBeginDate, // end of certificate validity
validityEndDate, // subject name of certificate
dn, // public key of certificate
keyPair.getPublic());
// key usage restrictions
builder.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.keyCertSign | KeyUsage.digitalSignature | KeyUsage.keyEncipherment | KeyUsage.dataEncipherment | KeyUsage.cRLSign));
builder.addExtension(Extension.basicConstraints, false, new BasicConstraints(true));
certWithNationalSymbols = new JcaX509CertificateConverter().getCertificate(builder.build(new JcaContentSignerBuilder("SHA256withRSA").setProvider(BouncyCastleProvider.PROVIDER_NAME).build(keyPair.getPrivate())));
}
XadesSigner signer = new XadesBesSigningProfile(new DirectKeyingDataProvider(certWithNationalSymbols, keyPair.getPrivate())).newSigner();
Document doc1 = getTestDocument();
Element elemToSign = doc1.getDocumentElement();
DataObjectDesc obj1 = new DataObjectReference('#' + elemToSign.getAttribute("Id")).withTransform(new EnvelopedSignatureTransform());
SignedDataObjects signDataObject = new SignedDataObjects(obj1);
signer.sign(signDataObject, doc1.getDocumentElement());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
outputDOM(doc1, baos);
String str = new String(baos.toByteArray());
// expected without parsing exception
Document doc = parseDocument(new ByteArrayInputStream(baos.toByteArray()));
}
use of xades4j.providers.impl.DirectKeyingDataProvider in project ref-GemLibPki by gematik.
the class TslSigner method sign.
/**
* Signs a given tsl
*
* @param tsl The tsl to sign
* @param signer {@link P12Container} with x509certificate an key (RSA/ECC) for signature
* @throws XAdES4jException during signature process or signer reading errors
*/
public static void sign(final Document tsl, final P12Container signer) throws XAdES4jException {
final Element elemToSign = getTslWithoutSignature(tsl);
final KeyingDataProvider kdp = new DirectKeyingDataProvider(signer.getCertificate(), signer.getPrivateKey());
final XadesSigner xSigner = new XadesBesSigningProfile(kdp).withSignatureAlgorithms(new SignatureAlgorithms().withSignatureAlgorithm("RSA", ALGO_ID_SIGNATURE_RSA_SHA256_MGF1).withCanonicalizationAlgorithmForSignature(new ExclusiveCanonicalXMLWithoutComments()).withCanonicalizationAlgorithmForTimeStampProperties(new ExclusiveCanonicalXMLWithoutComments())).withBasicSignatureOptions(new BasicSignatureOptions().includeIssuerSerial(false).includeSubjectName(false)).newSigner();
final DataObjectDesc dod = new DataObjectReference("").withTransform(new EnvelopedSignatureTransform()).withTransform(new ExclusiveCanonicalXMLWithoutComments()).withDataObjectFormat(new DataObjectFormatProperty("text/xml", ""));
xSigner.sign(new SignedDataObjects(dod), elemToSign);
}
Aggregations