Example usage for org.bouncycastle.tsp TimeStampResponse validate

List of usage examples for org.bouncycastle.tsp TimeStampResponse validate

Introduction

In this page you can find the example usage for org.bouncycastle.tsp TimeStampResponse validate.

Prototype

public void validate(TimeStampRequest request) throws TSPException 

Source Link

Document

Check this response against to see if it a well formed response for the passed in request.

Usage

From source file:org.signserver.module.tsa.TimeStampSignerTest.java

License:Open Source License

private void assertTokenGranted(int workerId) throws Exception {
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    timeStampRequestGenerator.setCertReq(true);
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(123124, requestBytes);
    try {/*from  ww w.  ja  v a2 s. c  o m*/
        final GenericSignResponse res = (GenericSignResponse) workerSession.process(workerId, signRequest,
                new RequestContext());

        final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
        timeStampResponse.validate(timeStampRequest);

        assertEquals(PKIStatus.GRANTED, timeStampResponse.getStatus());
    } catch (CryptoTokenOfflineException ex) {
        fail(ex.getMessage());
    }
}

From source file:org.signserver.module.tsa.TimeStampSignerTest.java

License:Open Source License

private void assertTokenNotGranted(int workerId) throws Exception {
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    timeStampRequestGenerator.setCertReq(true);
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(123124, requestBytes);
    try {/*ww  w.  j  a v a  2  s.c o m*/
        final GenericSignResponse res = (GenericSignResponse) workerSession.process(workerId, signRequest,
                new RequestContext());

        final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
        timeStampResponse.validate(timeStampRequest);

        assertFalse(PKIStatus.GRANTED == timeStampResponse.getStatus());
    } catch (CryptoTokenOfflineException ignored) { //NOPMD
        // OK
    }
}

From source file:org.signserver.module.tsa.TimeStampSignerUnitTest.java

License:Open Source License

/**
 * Tests that the log contains the TSA_TIMESOURCE entry.
 * @throws Exception// w  w  w. j  a v  a2  s  .c  o  m
 */
@Test
public void testLogTimeSource() throws Exception {
    LOG.info("testLogTimeSource");
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(100, requestBytes);
    final RequestContext requestContext = new RequestContext();
    final GenericSignResponse res = (GenericSignResponse) workerSession.process(WORKER1, signRequest,
            requestContext);

    final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
    timeStampResponse.validate(timeStampRequest);

    LogMap logMap = LogMap.getInstance(requestContext);
    assertEquals("timesource", LocalComputerTimeSource.class.getSimpleName(), logMap.get("TSA_TIMESOURCE"));
}

From source file:org.signserver.module.tsa.TimeStampSignerUnitTest.java

License:Open Source License

/**
 * Test that the base 64-encoded log entries for request and response
 * are not encoded with newlines, as this causes an extra base 64 encoding
 * with a B64: prefix by Base64PutHashMap.
 * /*from w w  w  . j  a  v  a 2s.c o  m*/
 * @throws Exception 
 */
@Test
public void testLogBase64Entries() throws Exception {
    LOG.info("testLogBase64Entries");
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[2000],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(100, requestBytes);
    final RequestContext requestContext = new RequestContext();
    final GenericSignResponse res = (GenericSignResponse) workerSession.process(WORKER1, signRequest,
            requestContext);

    final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
    timeStampResponse.validate(timeStampRequest);

    LogMap logMap = LogMap.getInstance(requestContext);
    assertNotNull("response", logMap.get(ITimeStampLogger.LOG_TSA_TIMESTAMPRESPONSE_ENCODED));
    assertEquals("log line doesn't contain newlines", -1,
            logMap.get(ITimeStampLogger.LOG_TSA_TIMESTAMPRESPONSE_ENCODED).lastIndexOf('\n'));
    assertNotNull("request", logMap.get(ITimeStampLogger.LOG_TSA_TIMESTAMPREQUEST_ENCODED));
    assertEquals("log line doesn't contain newlines", -1,
            logMap.get(ITimeStampLogger.LOG_TSA_TIMESTAMPREQUEST_ENCODED).lastIndexOf('\n'));
}

From source file:org.signserver.module.tsa.TimeStampSignerUnitTest.java

License:Open Source License

/**
 * Tests that a request including an extension not listed will cause a
 * rejection./*from  w w w .  j a va2  s .  co  m*/
 * @throws Exception
 */
@Test
public void testNotAcceptedExtensionPrevented() throws Exception {
    LOG.info("testNotAcceptedExtensionPrevented");
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    timeStampRequestGenerator.addExtension(new ASN1ObjectIdentifier("1.2.7.9"), false,
            new DEROctetString("Value".getBytes("UTF-8")));
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(100, requestBytes);
    final RequestContext requestContext = new RequestContext();
    final GenericSignResponse res = (GenericSignResponse) workerSession.process(WORKER2, signRequest,
            requestContext);

    final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
    timeStampResponse.validate(timeStampRequest);
    assertEquals("rejection", PKIStatus.REJECTION, timeStampResponse.getStatus());
    assertEquals("unacceptedExtension", PKIFailureInfo.unacceptedExtension,
            timeStampResponse.getFailInfo().intValue());
}

From source file:org.signserver.module.tsa.TimeStampSignerUnitTest.java

License:Open Source License

/**
 * Tests that a request including an extension listed will accept
 * the extension.//ww w  . j  a v a  2s  .  c  o m
 * @throws Exception
 */
@Test
public void testAcceptedExtensions() throws Exception {
    LOG.info("testAcceptedExtensions");
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    timeStampRequestGenerator.addExtension(new ASN1ObjectIdentifier("1.2.7.2"), false,
            new DEROctetString("Value".getBytes("UTF-8")));
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(100, requestBytes);
    final RequestContext requestContext = new RequestContext();
    final GenericSignResponse res = (GenericSignResponse) workerSession.process(WORKER2, signRequest,
            requestContext);

    final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
    timeStampResponse.validate(timeStampRequest);
    assertEquals("granted", PKIStatus.GRANTED, timeStampResponse.getStatus());
    assertEquals("extensions in token",
            Arrays.toString(new ASN1ObjectIdentifier[] { new ASN1ObjectIdentifier("1.2.7.2") }),
            Arrays.toString(timeStampResponse.getTimeStampToken().getTimeStampInfo().toASN1Structure()
                    .getExtensions().getExtensionOIDs()));
}

From source file:org.signserver.module.tsa.TimeStampSignerUnitTest.java

License:Open Source License

/**
 * Tests that a request including an extension listed will accept
 * the extension also when ACCEPTEDEXTENSIONS contains spaces.
 * @throws Exception//  w w w.  jav a2s. c  om
 */
@Test
public void testAcceptedExtensionsWithSpaces() throws Exception {
    LOG.info("testAcceptedExtensionsWithSpaces");
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    timeStampRequestGenerator.addExtension(new ASN1ObjectIdentifier("1.2.7.2"), false,
            new DEROctetString("Value".getBytes("UTF-8")));
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(100, requestBytes);
    final RequestContext requestContext = new RequestContext();
    final GenericSignResponse res = (GenericSignResponse) workerSession.process(WORKER4, signRequest,
            requestContext);

    final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
    timeStampResponse.validate(timeStampRequest);
    assertEquals("granted", PKIStatus.GRANTED, timeStampResponse.getStatus());
    assertEquals("extensions in token",
            Arrays.toString(new ASN1ObjectIdentifier[] { new ASN1ObjectIdentifier("1.2.7.2") }),
            Arrays.toString(timeStampResponse.getTimeStampToken().getTimeStampInfo().toASN1Structure()
                    .getExtensions().getExtensionOIDs()));
}

From source file:org.signserver.module.tsa.TimeStampSignerUnitTest.java

License:Open Source License

/**
 * Tests that a request without extension is accepted also when the list of
 * extensions is empty.// w w  w .  j a  v  a2 s.c  o m
 * @throws Exception
 */
@Test
public void testEmptyAcceptedExtensionsOk() throws Exception {
    LOG.info("testEmptyAcceptedExtensions");
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(100, requestBytes);
    final RequestContext requestContext = new RequestContext();
    final GenericSignResponse res = (GenericSignResponse) workerSession.process(WORKER3, signRequest,
            requestContext);

    final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
    timeStampResponse.validate(timeStampRequest);
    assertEquals("granted", PKIStatus.GRANTED, timeStampResponse.getStatus());
    assertNull("extensions in token",
            timeStampResponse.getTimeStampToken().getTimeStampInfo().toASN1Structure().getExtensions());
}

From source file:org.signserver.module.tsa.TimeStampSignerUnitTest.java

License:Open Source License

/**
 * Tests that a request including an extension not listed will cause a
 * rejection also when the list of extensions is empty.
 * @throws Exception//from w w  w.  ja va2s .  co m
 */
@Test
public void testEmptyAcceptedExtensionsPreventsExtension() throws Exception {
    LOG.info("testEmptyAcceptedExtensionsPreventsExtension");
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    timeStampRequestGenerator.addExtension(new ASN1ObjectIdentifier("1.2.7.9"), false,
            new DEROctetString("Value".getBytes("UTF-8")));
    TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes = timeStampRequest.getEncoded();
    GenericSignRequest signRequest = new GenericSignRequest(100, requestBytes);
    final RequestContext requestContext = new RequestContext();
    final GenericSignResponse res = (GenericSignResponse) workerSession.process(WORKER3, signRequest,
            requestContext);

    final TimeStampResponse timeStampResponse = new TimeStampResponse((byte[]) res.getProcessedData());
    timeStampResponse.validate(timeStampRequest);
    assertEquals("rejection", PKIStatus.REJECTION, timeStampResponse.getStatus());
    assertEquals("unacceptedExtension", PKIFailureInfo.unacceptedExtension,
            timeStampResponse.getFailInfo().intValue());
}

From source file:org.signserver.protocol.ws.MainWebServiceTestSeparately.java

License:Open Source License

@Test
public void test02BasicWSProcess() throws Exception {
    TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
    TimeStampRequest timeStampRequest1 = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes1 = timeStampRequest1.getEncoded();
    GenericSignRequest signRequest1 = new GenericSignRequest(12, requestBytes1);
    ProcessRequestWS req1 = new ProcessRequestWS(signRequest1);

    TimeStampRequest timeStampRequest2 = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20],
            BigInteger.valueOf(100));
    byte[] requestBytes2 = timeStampRequest2.getEncoded();
    GenericSignRequest signRequest2 = new GenericSignRequest(13, requestBytes2);
    ProcessRequestWS req2 = new ProcessRequestWS(signRequest2);

    ArrayList<ProcessRequestWS> reqs = new ArrayList<ProcessRequestWS>();
    reqs.add(req1);/*from ww w.j  ava 2s  . c o  m*/
    reqs.add(req2);

    try {
        signServerWS.process("9", WSClientUtil.convertProcessRequestWS(reqs));
        assertTrue(false);
    } catch (IllegalRequestException_Exception e) {
    }

    workerSession.setWorkerProperty(9, "AUTHTYPE", "NOAUTH");
    workerSession.reloadConfiguration(9);

    workerSession.deactivateSigner(9);
    try {
        signServerWS.process("9", WSClientUtil.convertProcessRequestWS(reqs));
        assertTrue(false);
    } catch (CryptoTokenOfflineException_Exception e) {
    }

    workerSession.activateSigner(9, "foo123");

    List<ProcessResponseWS> resps = signServerWS.process("TestTimeStamp",
            WSClientUtil.convertProcessRequestWS(reqs));
    assertTrue(resps.size() == 2);
    assertTrue(resps.get(0).getRequestID() == 12);
    assertTrue(resps.get(1).getRequestID() == 13);
    assertNotNull(resps.get(0).getWorkerCertificate());

    GenericSignResponse resp = (GenericSignResponse) RequestAndResponseManager
            .parseProcessResponse(WSClientUtil.convertProcessResponseWS(resps).get(0).getResponseData());

    TimeStampResponse timeStampResponse = new TimeStampResponse(resp.getProcessedData());
    timeStampResponse.validate(timeStampRequest1);

    try {
        signServerWS.process("1991817", WSClientUtil.convertProcessRequestWS(reqs));
        assertTrue(false);
    } catch (InvalidWorkerIdException_Exception e) {
    }

    ValidateRequest req = new ValidateRequest(validCert1, ValidationServiceConstants.CERTPURPOSE_NO_PURPOSE);

    req1 = new ProcessRequestWS(req);

    reqs = new ArrayList<ProcessRequestWS>();
    reqs.add(req1);

    resps = signServerWS.process("16", WSClientUtil.convertProcessRequestWS(reqs));
    assertTrue(resps.size() == 1);
    ValidateResponse res = (ValidateResponse) RequestAndResponseManager
            .parseProcessResponse(WSClientUtil.convertProcessResponseWS(resps).get(0).getResponseData());

    Validation val = res.getValidation();
    assertTrue(val != null);
    assertTrue(val.getStatus().equals(Validation.Status.VALID));
    assertTrue(val.getStatusMessage() != null);
    List<java.security.cert.Certificate> cAChain = val.getCAChain();
    assertTrue(cAChain != null);
    assertTrue(CertTools.getSubjectDN(cAChain.get(0)).equals("CN=ValidSubCA1"));
    assertTrue(CertTools.getSubjectDN(cAChain.get(1)).equals("CN=ValidRootCA1"));
}