List of usage examples for org.bouncycastle.tsp TSPException TSPException
public TSPException(String message)
From source file:be.fedict.trust.service.util.ClockDriftUtil.java
License:Open Source License
public static Date executeTSP(ClockDriftConfigEntity clockDriftConfig, NetworkConfig networkConfig) throws IOException, TSPException { LOG.debug("clock drift detection: " + clockDriftConfig.toString()); TimeStampRequestGenerator requestGen = new TimeStampRequestGenerator(); TimeStampRequest request = requestGen.generate(TSPAlgorithms.SHA1, new byte[20], BigInteger.valueOf(100)); byte[] requestData = request.getEncoded(); HttpClient httpClient = new HttpClient(); if (null != networkConfig) { httpClient.getHostConfiguration().setProxy(networkConfig.getProxyHost(), networkConfig.getProxyPort()); }//from ww w. ja va 2 s .c o m PostMethod postMethod = new PostMethod(clockDriftConfig.getServer()); postMethod.setRequestEntity(new ByteArrayRequestEntity(requestData, "application/timestamp-query")); int statusCode = httpClient.executeMethod(postMethod); if (statusCode != HttpStatus.SC_OK) { throw new TSPException("Error contacting TSP server " + clockDriftConfig.getServer()); } TimeStampResponse tspResponse = new TimeStampResponse(postMethod.getResponseBodyAsStream()); postMethod.releaseConnection(); return tspResponse.getTimeStampToken().getTimeStampInfo().getGenTime(); }
From source file:br.gov.jfrj.siga.cd.TimeStamper.java
License:Open Source License
public static TimeStampToken gerarCarimboTempo(byte[] assinatura) throws URISyntaxException, IOException, TSPException, NoSuchAlgorithmException { TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator(); reqGen.setCertReq(true);//from w w w . j a va 2 s. c o m log.info("Criando requisio para recuperar carimbo"); MessageDigest md = MessageDigest.getInstance("SHA"); md.update(assinatura); assinatura = md.digest(); TimeStampRequest request = reqGen.generate(TSPAlgorithms.SHA1, assinatura); log.info("Enviando requisio para " + SERVIDOR_CARIMBO); TimeStampResponse response = sendRequest(request, SERVIDOR_CARIMBO); response.validate(request); TimeStampToken respToken = response.getTimeStampToken(); byte[] token = respToken.getEncoded(); if (token == null) { throw new TSPException("Nenhum token retornado"); } log.info("Recebidos " + token.length + " bytes do carimbador"); return respToken; }
From source file:br.gov.jfrj.siga.cd.TimeStamper.java
License:Open Source License
private static TimeStampResponse sendRequest(TimeStampRequest timestampreq, String servidor) throws URISyntaxException, IOException, TSPException { URI uri = new URI(servidor); String host = uri.getHost();/*from w w w . j a v a2 s .c om*/ int porta = uri.getPort(); byte[] token = timestampreq.getEncoded(); TimeStampResponse tsptcpipresponse = null; Socket socket = new Socket(); log.info("Criando socket em: host=" + host + ", porta=" + porta); socket.connect(new InetSocketAddress(host, porta), 15000); log.debug("Socket conectada"); DataInputStream datainputstream = new DataInputStream(socket.getInputStream()); DataOutputStream dataoutputstream = new DataOutputStream(socket.getOutputStream()); log.debug("Escrevendo na socket"); dataoutputstream.writeInt(token.length + 1); // length (32-bits) dataoutputstream.writeByte(0); // flag (8-bits) dataoutputstream.write(token); // value (defined below) dataoutputstream.flush(); log.debug("OutputStream atualizada"); int i = datainputstream.readInt(); byte byte0 = datainputstream.readByte(); log.debug("Lendo primeiro byte do inputStream '" + byte0 + "'"); if (byte0 == 5) { byte abyte1[] = new byte[i - 1]; log.debug("Lendo todo o input stream"); datainputstream.readFully(abyte1); log.debug("Criando novo time stam response: " + abyte1); tsptcpipresponse = new TimeStampResponse(abyte1); log.debug("Novo TimeStampResponde criado com sucesso: " + tsptcpipresponse); } else { datainputstream.close(); dataoutputstream.close(); socket.close(); throw new TSPException("Token invlido"); } log.debug("Fechando streams de entrada e sada"); datainputstream.close(); dataoutputstream.close(); log.info("Fechando conexo socket"); socket.close(); return tsptcpipresponse; }
From source file:de.rub.dez6a3.jpdfsigner.TimeStampToken.java
License:Open Source License
TimeStampTokenInfo(TSTInfo tstInfo) throws TSPException, IOException { this.tstInfo = tstInfo; try {// ww w. j a v a2 s. c o m this.genTime = tstInfo.getGenTime().getDate(); } catch (ParseException e) { throw new TSPException("unable to parse genTime field"); } }
From source file:net.sf.jsignpdf.verify.VerifierLogic.java
License:Mozilla Public License
public Exception validateTimeStampToken(TimeStampToken token) { if (token == null) { return null; }//from ww w . j ava2s . com try { SignerId signer = token.getSID(); X509Certificate certificate = null; X500Principal sign_cert_issuer = signer.getIssuer(); BigInteger sign_cert_serial = signer.getSerialNumber(); CertStore store = token.getCertificatesAndCRLs("Collection", "BC"); // Iterate CertStore to find a signing certificate Collection<? extends Certificate> certs = store.getCertificates(null); Iterator<? extends Certificate> iter = certs.iterator(); while (iter.hasNext()) { X509Certificate cert = (X509Certificate) iter.next(); if (cert.getIssuerX500Principal().equals(sign_cert_issuer) && cert.getSerialNumber().equals(sign_cert_serial)) { certificate = cert; break; } } if (certificate == null) { throw new TSPException("Missing signing certificate for TSA."); } // check TS token's certificate against keystore if (certs.size() == 1) { boolean verifyTimestampCertificates = PdfPKCS7.verifyTimestampCertificates(token, kall, null); if (!verifyTimestampCertificates) { throw new Exception("Timestamp certificate can't be verified."); } } else { int certSize = certs.size(); Certificate[] array = certs.toArray(new Certificate[certSize]); Certificate[] certArray = new Certificate[certSize]; // reverse order for (int i = 0; i < certSize; i++) { certArray[i] = array[certSize - 1 - i]; } // token.validate(SignerInformationVerifier) will check if certificate has been valid at the time the timestamp was created Object[] verifyCertificates = PdfPKCS7.verifyCertificates(certArray, kall, null, null); if (verifyCertificates != null) { throw new Exception("Timestamp certificate can't be verified."); } } SignerInformationVerifier verifier = new JcaSimpleSignerInfoVerifierBuilder().build(certificate); token.validate(verifier); } catch (Exception e) { return e; } return null; }
From source file:org.linagora.linshare.core.service.impl.TimeStampingServiceImpl.java
License:Open Source License
private URI getUriFromUrl(String urlTSA) throws URISyntaxException, TSPException { URI uriTSA;//from ww w . jav a2s. c o m if (urlTSA == null || urlTSA.equals("")) { throw new TSPException("no TSA url"); } else { try { uriTSA = new URI(urlTSA); //check url syntax } catch (URISyntaxException e) { throw e; } } return uriTSA; }
From source file:org.linagora.linshare.core.service.impl.TimeStampingServiceImpl.java
License:Open Source License
private TimeStampResponse getTimeStamp(URI uriTSA, byte[] sha1Digest) throws TSPException { TimeStampResponse response = null;/*w ww. java 2 s .co m*/ ByteArrayInputStream bis = null; OutputStream out = null; try { TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator(); SecureRandom randomGenerator = SecureRandom.getInstance("SHA1PRNG"); long nonce = randomGenerator.nextLong(); // request with digestAlgorithmOID, byte[] digest, java.math.BigInteger nonce TimeStampRequest request = reqGen.generate(TSPAlgorithms.SHA1, sha1Digest, BigInteger.valueOf(nonce)); byte[] reqData = request.getEncoded(); HttpURLConnection conn = (HttpURLConnection) uriTSA.toURL().openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/timestamp-query"); conn.setRequestProperty("Content-Length", Long.toString(reqData.length)); conn.setRequestMethod("POST"); out = conn.getOutputStream(); bis = new ByteArrayInputStream(reqData); byte[] tab = new byte[1024]; int lu = bis.read(tab); while (lu >= 0) { out.write(tab, 0, lu); lu = bis.read(tab); } out.flush(); int returnCode = conn.getResponseCode(); if (returnCode == HttpURLConnection.HTTP_OK) { InputStream in = conn.getInputStream(); response = new TimeStampResponse(in); response.validate(request); // if it fails a TSPException is raised } else { //404 or 500 ... throw new TSPException("service TSA is not available"); } } catch (ProtocolException e) { throw new TSPException(e.getMessage(), e); } catch (IOException e) { throw new TSPException(e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new TSPException(e.getMessage(), e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { logger.error(e.toString()); } } if (bis != null) { try { bis.close(); } catch (IOException e) { logger.error(e.toString()); } } } return response; }
From source file:org.votingsystem.signature.util.TimeStampResponseGenerator.java
License:Open Source License
public TimeStampResponse generateFailResponse(int status, int failInfoField, String statusString) throws TSPException { this.status = status; this.setFailInfoField(failInfoField); if (statusString != null) { this.addStatusString(statusString); }/*from w w w. j a va 2 s . co m*/ PKIStatusInfo pkiStatusInfo = this.getPKIStatusInfo(); TimeStampResp resp = new TimeStampResp(pkiStatusInfo, (ContentInfo) null); try { return new TimeStampResponse(resp); } catch (IOException var7) { throw new TSPException("created badly formatted response!"); } }