Marca Temporale

Buonasera,
chiedo anticipatamente scusa se ho sbagliato “sezione” dove scrivere ero indeciso tra questa e sicurezza.
Ho la seguente problematica:
Un’applicazione web java permette ai cittadini di caricare dei documenti elettronici PDF firmati digitalmente (senza obbligo di marca temporale) ma per inviare i documenti in “conservazione” devo apporre la marca temporale, cercando su internet trovo esempi ecc per ambiente .Net ma il nostro sistema è JAVA.
Ho implementato la chiamata al servizio che mi ritorna la marca ma non so minimamente come aggiungerla all’interno di un P7M o precisamente creare un file TSD con all’interno la marca ed il P7M.
Qualcuno può darmi una mano, un consiglio
Grazie mille

Risolto creando un jar che prelevata la marca temporale produce un file TSD.
prima di produrre tale file devo reperire i byte del file TST (Marca temporale)

Ciao afocone sto tentando di fare la stessa cosa anche io in JAVA per l’invio in conservazione, puoi condividere la tua soluzione?

Grazie.

private static TimeStampToken sendHash(byte[] hash, boolean proxyPresente, String urlSrvMarca, String credenzialiMarca)throws Exception{
HttpsURLConnection con = null;
TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
reqgen.setCertReq(true);
TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA256, hash);
byte[] request = req.getEncoded();
URL url = new URL(urlSrvMarca);
if (proxyPresente) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(“IPPROXY”, PORTA));
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“NOMEUYTENTE”, “PASSWORDE”.toCharArray());
}
};
Authenticator.setDefault(authenticator);
con = (HttpsURLConnection)url.openConnection(proxy);
} else {
con = (HttpsURLConnection)url.openConnection();
}
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod(“POST”);
con.setRequestProperty(“Content-type”, “application/timestamp-query”);
con.setRequestProperty(“Content-length”, String.valueOf(request.length));
con.setRequestProperty(“Authorization”, credenziali);
OutputStream out = con.getOutputStream();
out.write(request);
out.flush();
con.getContent();
if (con.getResponseCode() != 200) {
throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
}
TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(con.getInputStream()).readObject());
TimeStampResponse response = new TimeStampResponse(resp);
response.validate(req);
TimeStampToken token = response.getTimeStampToken();
return token;
}

private static byte[] hashFile(File file) throws Exception { MessageDigest digest = MessageDigest.getInstance(“SHA-256”);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
byte[] bytesBuffer = new byte[‘Ѐ’];
int bytesRead = 0;
while ((bytesRead = fis.read(bytesBuffer)) != -1) {
digest.update(bytesBuffer, 0, bytesRead);
}
byte[] hashed = digest.digest();
return hashed;
}

private static void saveM7m(OutputStream fos, String p7mName, byte[] p7mContent, String tsrName, byte[] tsrContent) throws IOException { String mimeBoundary = “UIBM”;
String mimeHeader = “Mime-Version: 1.0\nContent-Type: multipart/mixed; boundary=”" + mimeBoundary + “”";
String p7mContentType = “Content-Type: application/pkcs7-mime; smime-type=signed-data; name=”" + p7mName + “”";
String p7mContentTransferEncoding = “Content-Transfer-Encoding: binary”;
String p7mContentDisposition = “Content-Disposition: attachment; filename=”" + p7mName + “”";
String p7mContentDescription = “Content-Description: Signed envelope”;
String tsrContentType = “Content-Type: application/timestamp-reply; name=”" + tsrName + “”";
String tsrContentTransferEncoding = “Content-Transfer-Encoding: base64”;
String tsrContentDisposition = “Content-Disposition: attachment; filename=”" + tsrName + “”";
String tsrContentDescription = “Content-Description: time-stamp response”;
fos.write(mimeHeader.getBytes());
fos.write("\r\n".getBytes());
fos.write("\r\n".getBytes());
fos.write(("–" + mimeBoundary).getBytes());fos.write("\r\n".getBytes());
fos.write(p7mContentType.getBytes());fos.write("\r\n".getBytes());
fos.write(p7mContentTransferEncoding.getBytes());fos.write("\r\n".getBytes());
fos.write(p7mContentDisposition.getBytes());fos.write("\r\n".getBytes());
fos.write(p7mContentDescription.getBytes());fos.write("\r\n".getBytes());
fos.write("\r\n".getBytes());
fos.write(p7mContent);
fos.write("\r\n".getBytes());
fos.write(("–" + mimeBoundary).getBytes());fos.write("\r\n".getBytes());
fos.write(tsrContentType.getBytes());fos.write("\r\n".getBytes());
fos.write(tsrContentTransferEncoding.getBytes());fos.write("\r\n".getBytes());
fos.write(tsrContentDisposition.getBytes());fos.write("\r\n".getBytes());
fos.write(tsrContentDescription.getBytes());fos.write("\r\n".getBytes());
fos.write("\r\n".getBytes());
BASE64Encoder encoder = new BASE64Encoder();
fos.write(encoder.encode(tsrContent).getBytes());
fos.write("\r\n".getBytes());
fos.write(("–" + mimeBoundary + “–”).getBytes());
fos.write("\r\n".getBytes());
}