Search in sources :

Example 1 with TimeStampResp

use of com.github.zhenwei.pkix.util.asn1.tsp.TimeStampResp in project gdmatrix by gdmatrix.

the class P7MUtils method createTimeStamp.

public static ContentInfo createTimeStamp(String serviceURI, byte[] message) throws Exception {
    String nonce = String.valueOf((int) (Math.random() * 1000000));
    // es crea la peticio a la TSA
    TimeStampReq timeStampRequest = createTimeStampRequest(// message
    message, // nonce
    nonce, // requireCert
    true, // extensions
    null, // digestAlgorithm identifier
    "1.3.14.3.2.26", // timestampPolicy
    "0.4.0.2023.1.1");
    // s'envia la peticio creada
    TimeStampResp timeStampResponse = sendTimestampRequest(timeStampRequest, serviceURI);
    ContentInfo contentInfo = timeStampResponse.getTimeStampToken();
    return contentInfo;
}
Also used : TimeStampReq(org.bouncycastle.asn1.tsp.TimeStampReq) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) TimeStampResp(org.bouncycastle.asn1.tsp.TimeStampResp)

Example 2 with TimeStampResp

use of com.github.zhenwei.pkix.util.asn1.tsp.TimeStampResp in project gdmatrix by gdmatrix.

the class CMSUtils method sendData.

private static TimeStampResp sendData(InputStream dataToBeSent, String serviceURI) throws Exception {
    URL url = new URL(serviceURI);
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    // post request data
    OutputStream os = conn.getOutputStream();
    byte[] buffer = new byte[4096];
    int numRead = dataToBeSent.read(buffer);
    while (numRead > 0) {
        os.write(buffer, 0, numRead);
        numRead = dataToBeSent.read(buffer);
    }
    os.flush();
    // read response
    InputStream response = conn.getInputStream();
    ASN1InputStream asn1Is = new ASN1InputStream(response);
    // TimeStampResp tspResp = new TimeStampResp((ASN1Sequence)asn1Is.readObject());
    Enumeration e = ((ASN1Sequence) asn1Is.readObject()).getObjects();
    PKIStatusInfo pkiStatusInfo = PKIStatusInfo.getInstance(e.nextElement());
    ContentInfo timeStampToken = null;
    if (e.hasMoreElements()) {
        timeStampToken = ContentInfo.getInstance(e.nextElement());
    }
    TimeStampResp tspResp = new TimeStampResp(pkiStatusInfo, timeStampToken);
    return tspResp;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) Enumeration(java.util.Enumeration) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PKIStatusInfo(org.bouncycastle.asn1.cmp.PKIStatusInfo) TimeStampResp(org.bouncycastle.asn1.tsp.TimeStampResp) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 3 with TimeStampResp

use of com.github.zhenwei.pkix.util.asn1.tsp.TimeStampResp in project LinLong-Java by zhenwei1108.

the class TimeStampResponseGenerator method generateFailResponse.

/**
 * Generate a non-granted TimeStampResponse with chosen status and FailInfoField.
 *
 * @param status        the PKIStatus to set.
 * @param failInfoField the FailInfoField to set.
 * @param statusString  an optional string describing the failure.
 * @return a TimeStampResponse with a failInfoField and optional statusString
 * @throws TSPException in case the response could not be created
 */
public TimeStampResponse generateFailResponse(int status, int failInfoField, String statusString) throws TSPException {
    this.status = status;
    this.statusStrings = new ASN1EncodableVector();
    this.setFailInfoField(failInfoField);
    if (statusString != null) {
        this.addStatusString(statusString);
    }
    PKIStatusInfo pkiStatusInfo = getPKIStatusInfo();
    TimeStampResp resp = new TimeStampResp(pkiStatusInfo, null);
    try {
        return new TimeStampResponse(resp);
    } catch (IOException e) {
        throw new TSPException("created badly formatted response!");
    }
}
Also used : PKIStatusInfo(com.github.zhenwei.pkix.util.asn1.cmp.PKIStatusInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) IOException(java.io.IOException) TimeStampResp(com.github.zhenwei.pkix.util.asn1.tsp.TimeStampResp)

Example 4 with TimeStampResp

use of com.github.zhenwei.pkix.util.asn1.tsp.TimeStampResp in project X-Road by nordic-institute.

the class TimestamperUtil method getTimestampResponse.

static TimeStampResponse getTimestampResponse(InputStream in) throws Exception {
    TimeStampResp response = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
    if (response == null) {
        throw new RuntimeException("Could not read time-stamp response");
    }
    BigInteger status = response.getStatus().getStatus();
    log.trace("getTimestampDer() - TimeStampResp.status: {}", status);
    if (!PKIStatus.granted.getValue().equals(status) && !PKIStatus.grantedWithMods.getValue().equals(status)) {
        PKIFreeText statusString = response.getStatus().getStatusString();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < statusString.size(); i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append("\"" + statusString.getStringAt(i) + "\"");
        }
        log.error("getTimestampDer() - TimeStampResp.status is not " + "\"granted\" neither \"grantedWithMods\": {}, {}", status, sb);
        throw new RuntimeException("TimeStampResp.status: " + status + ", .statusString: " + sb);
    }
    return new TimeStampResponse(response);
}
Also used : TimeStampResponse(org.bouncycastle.tsp.TimeStampResponse) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) BigInteger(java.math.BigInteger) TimeStampResp(org.bouncycastle.asn1.tsp.TimeStampResp) PKIFreeText(org.bouncycastle.asn1.cmp.PKIFreeText)

Example 5 with TimeStampResp

use of com.github.zhenwei.pkix.util.asn1.tsp.TimeStampResp in project gdmatrix by gdmatrix.

the class P7MUtils method sendData.

private static TimeStampResp sendData(InputStream dataToBeSent, String serviceURI) throws Exception {
    URL url = new URL(serviceURI);
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    // post request data
    OutputStream os = conn.getOutputStream();
    byte[] buffer = new byte[4096];
    int numRead = dataToBeSent.read(buffer);
    while (numRead > 0) {
        os.write(buffer, 0, numRead);
        numRead = dataToBeSent.read(buffer);
    }
    os.flush();
    // read response
    InputStream response = conn.getInputStream();
    ASN1InputStream asn1Is = new ASN1InputStream(response);
    Enumeration e = ((ASN1Sequence) asn1Is.readObject()).getObjects();
    PKIStatusInfo pkiStatusInfo = PKIStatusInfo.getInstance(e.nextElement());
    ContentInfo timeStampToken = null;
    if (e.hasMoreElements()) {
        timeStampToken = ContentInfo.getInstance(e.nextElement());
    }
    TimeStampResp tspResp = new TimeStampResp(pkiStatusInfo, timeStampToken);
    return tspResp;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) Enumeration(java.util.Enumeration) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PKIStatusInfo(org.bouncycastle.asn1.cmp.PKIStatusInfo) TimeStampResp(org.bouncycastle.asn1.tsp.TimeStampResp) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

TimeStampResp (org.bouncycastle.asn1.tsp.TimeStampResp)5 ContentInfo (org.bouncycastle.asn1.cms.ContentInfo)4 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 Enumeration (java.util.Enumeration)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)2 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)2 DEROctetString (org.bouncycastle.asn1.DEROctetString)2 PKIStatusInfo (org.bouncycastle.asn1.cmp.PKIStatusInfo)2 TimeStampReq (org.bouncycastle.asn1.tsp.TimeStampReq)2 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)1 PKIStatusInfo (com.github.zhenwei.pkix.util.asn1.cmp.PKIStatusInfo)1 TimeStampResp (com.github.zhenwei.pkix.util.asn1.tsp.TimeStampResp)1 IOException (java.io.IOException)1