Esempio n. 1
0
  @Before
  public void init() throws Exception {
    documentToSign = new FileDocument(new File("src/test/resources/sample.xml"));

    CertificateService certificateService = new CertificateService();
    privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);

    signatureParameters = new XAdESSignatureParameters();
    signatureParameters.bLevel().setSigningDate(new Date());
    signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
    signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
    signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
    signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA);

    CertificateVerifier certificateVerifier = new CommonCertificateVerifier();
    service = new XAdESService(certificateVerifier);
    service.setTspSource(
        new MockTSPSource(
            certificateService.generateTspCertificate(SignatureAlgorithm.RSA_SHA256), new Date()));
  }
  @Test
  public void test() throws Exception {
    DSSDocument documentToSign = new InMemoryDocument("Hello World".getBytes());

    CertificateService certificateService = new CertificateService();
    MockPrivateKeyEntry privateKeyEntry =
        certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);

    CAdESSignatureParameters signatureParameters = new CAdESSignatureParameters();
    signatureParameters.bLevel().setSigningDate(new Date());
    signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
    signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
    signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
    signatureParameters.setSignatureLevel(SignatureLevel.CAdES_BASELINE_LTA);

    CertificateVerifier certificateVerifier = new CommonCertificateVerifier();
    CAdESService service = new CAdESService(certificateVerifier);
    service.setTspSource(
        new MockTSPSource(certificateService.generateTspCertificate(SignatureAlgorithm.RSA_SHA1)));

    ToBeSigned toBeSigned = service.getDataToSign(documentToSign, signatureParameters);
    SignatureValue signatureValue =
        TestUtils.sign(signatureParameters.getSignatureAlgorithm(), privateKeyEntry, toBeSigned);
    final DSSDocument signedDocument =
        service.signDocument(documentToSign, signatureParameters, signatureValue);

    SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(signedDocument);
    validator.setCertificateVerifier(new CommonCertificateVerifier());

    Reports report = validator.validateDocument();
    // report.print();
    DiagnosticData diagnostic = report.getDiagnosticData();
    String timestampId = diagnostic.getSignatures().get(0).getTimestampList().get(0).getId();
    for (TimestampWrapper wrapper : diagnostic.getTimestampList(diagnostic.getFirstSignatureId())) {
      if (wrapper.getType().equals(TimestampType.ARCHIVE_TIMESTAMP.toString())) {
        Assert.assertEquals(
            timestampId, wrapper.getSignedObjects().getTimestampedTimestamp().get(0).getId());
      }
    }
  }