RemoteTimestampService { public TimestampResponseDTO getTimestampResponse(final DigestAlgorithm digestAlgorithm, final byte[] value) throws DSSException { Objects.requireNonNull(tspSource, "TSPSource must be not null!"); LOG.info("Timestamp request in process..."); Objects.requireNonNull(digestAlgorithm, "digestAlgorithm must be not null!"); Objects.requireNonNull(value, "value must be not null!"); TimestampBinary timestampBinary = tspSource.getTimeStampResponse(digestAlgorithm, value); if (timestampBinary != null && Utils.isArrayNotEmpty(timestampBinary.getBytes())) { LOG.info("Timestamp is obtained."); return toTimestampResponseDTO(timestampBinary); } throw new DSSException("The obtained TimestampToken response is null or empty!"); }  void setTSPSource(TSPSource tspSource); TimestampResponseDTO getTimestampResponse(final DigestAlgorithm digestAlgorithm, final byte[] value);  }
@Test public void simpleTest() { byte[] contentToBeTimestamped = "Hello World!".getBytes(); byte[] digestValue = DSSUtils.digest(DigestAlgorithm.SHA1, contentToBeTimestamped); TimestampResponseDTO timestampResponse = timestampService.getTimestampResponse(DigestAlgorithm.SHA1, digestValue); assertNotNull(timestampResponse); assertTrue(Utils.isArrayNotEmpty(timestampResponse.getBinaries())); }
@Test public void signatureWithContentTimestamp() throws Exception { DSSDocument documentToSign = new FileDocument(new File("src/test/resources/sample.xml")); String canonicalizationAlgo = CanonicalizationMethod.EXCLUSIVE; DigestAlgorithm digestAlgorithm = DigestAlgorithm.SHA1; XAdESSignatureParameters signatureParameters = new XAdESSignatureParameters(); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setDigestAlgorithm(digestAlgorithm); byte[] digest = DSSUtils.digest(digestAlgorithm, DSSXMLUtils.canonicalize(canonicalizationAlgo, DSSUtils.toByteArray(documentToSign))); TimestampResponseDTO timeStampResponse = timestampService.getTimestampResponse(digestAlgorithm, digest); TimestampToken timestampToken = new TimestampToken(timeStampResponse.getBinaries(), TimestampType.ALL_DATA_OBJECTS_TIMESTAMP); timestampToken.setCanonicalizationMethod(canonicalizationAlgo); signatureParameters.setContentTimestamps(Arrays.asList(timestampToken)); XAdESService service = new XAdESService(getCompleteCertificateVerifier()); ToBeSigned dataToSign = service.getDataToSign(documentToSign, signatureParameters); SignatureValue signatureValue = getToken().sign(dataToSign, signatureParameters.getDigestAlgorithm(), signatureParameters.getMaskGenerationFunction(), getPrivateKeyEntry()); DSSDocument signedDocument = service.signDocument(documentToSign, signatureParameters, signatureValue); SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(signedDocument); validator.setCertificateVerifier(getOfflineCertificateVerifier()); validator.setTokenExtractionStategy(TokenExtractionStategy.EXTRACT_TIMESTAMPS_ONLY); Reports reports = validator.validateDocument(); assertNotNull(reports); DiagnosticData diagnosticData = reports.getDiagnosticData(); List<TimestampWrapper> timestampList = diagnosticData.getTimestampList(); assertNotNull(timestampList); assertEquals(1, timestampList.size()); TimestampWrapper timestamp = timestampList.get(0); assertTrue(timestamp.getType().isContentTimestamp()); assertEquals(TimestampType.ALL_DATA_OBJECTS_TIMESTAMP, timestamp.getType()); assertTrue(Arrays.equals(timeStampResponse.getBinaries(), timestamp.getBinaries())); }
@Test public void noTSPSourceDefinedTest() { RemoteTimestampService remoteTimestampService = new RemoteTimestampService(); byte[] contentToBeTimestamped = "Hello World!".getBytes(); byte[] digestValue = DSSUtils.digest(DigestAlgorithm.SHA1, contentToBeTimestamped); Exception exception = assertThrows(NullPointerException.class, () -> remoteTimestampService.getTimestampResponse(DigestAlgorithm.SHA1, digestValue)); assertEquals("TSPSource must be not null!", exception.getMessage()); }