📄 protocolocsphttptest.java
字号:
* @return KeyPair the generated key pair * * @throws Exception if en error occurs... */ private static KeyPair genKeys() throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA", "BC"); keygen.initialize(512); log.debug("Generating keys, please wait..."); KeyPair rsaKeys = keygen.generateKeyPair(); log.debug("Generated " + rsaKeys.getPrivate().getAlgorithm() + " keys with length" + ((RSAPrivateKey) rsaKeys.getPrivate()).getModulus().bitLength()); return rsaKeys; } // genKeys public void test01Access() throws Exception { WebConversation wc = new WebConversation(); // Hit with GET gives a 405 with OCSP: BAD_METHOD WebRequest request = new GetMethodWebRequest(httpReqPath + '/' + resourceOcsp); WebResponse response = wc.getResponse(request); assertEquals("Response code", 405, response.getResponseCode()); } /** Tests ocsp message * @throws Exception error */ public void test02OcspGood() throws Exception { log.debug(">test02OcspGood()"); // find a CA (TestCA?) create a user and generate his cert // send OCSP req to server and get good response // change status of cert to bad status // send OCSP req and get bad status // (send crap message and get good error) // Make user that we know... boolean userExists = false; try { usersession.addUser(admin,"ocsptest","foo123","C=SE,O=AnaTom,CN=OCSPTest",null,"ocsptest@anatom.se",false,SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_ENDUSER,SecConst.TOKEN_SOFT_PEM,0,caid); log.debug("created user: ocsptest, foo123, C=SE, O=AnaTom, CN=OCSPTest"); } catch (RemoteException re) { if (re.detail instanceof DuplicateKeyException) { userExists = true; } } catch (DuplicateKeyException dke) { userExists = true; } if (userExists) { log.debug("User ocsptest already exists."); usersession.changeUser(admin, "ocsptest", "foo123", "C=SE,O=AnaTom,CN=OCSPTest",null,"ocsptest@anatom.se",false, SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_ENDUSER,SecConst.TOKEN_SOFT_PEM,0,UserDataConstants.STATUS_NEW, caid); //usersession.setUserStatus(admin,"ocsptest",UserDataConstants.STATUS_NEW); log.debug("Reset status to NEW"); } // Generate certificate for the new user KeyPair keys = genKeys(); // user that we know exists... ocspTestCert = (X509Certificate) remote.createCertificate(admin, "ocsptest", "foo123", keys.getPublic()); assertNotNull("Misslyckades skapa cert", ocspTestCert); // And an OCSP request OCSPReqGenerator gen = new OCSPReqGenerator(); gen.addRequest(new CertificateID(CertificateID.HASH_SHA1, cacert, ocspTestCert.getSerialNumber())); OCSPReq req = gen.generate(); // Send the request and receive a singleResponse SingleResp singleResp = sendOCSPPost(req.getEncoded()); CertificateID certId = singleResp.getCertID(); assertEquals("Serno in response does not match serno in request.", certId.getSerialNumber(), ocspTestCert.getSerialNumber()); Object status = singleResp.getCertStatus(); assertEquals("Status is not null (good)", status, null); log.debug("<test02OcspGood()"); } /** Tests ocsp message * @throws Exception error */ public void test03OcspRevoked() throws Exception { log.debug(">test03OcspRevoked()"); // Now revoke the certificate and try again CertificateDataPK pk = new CertificateDataPK(); pk.fingerprint = CertTools.getFingerprintAsString(ocspTestCert); ICertificateStoreSessionRemote store = storehome.create(); store.revokeCertificate(admin, ocspTestCert,null,RevokedCertInfo.REVOKATION_REASON_KEYCOMPROMISE); // And an OCSP request OCSPReqGenerator gen = new OCSPReqGenerator(); gen.addRequest(new CertificateID(CertificateID.HASH_SHA1, cacert, ocspTestCert.getSerialNumber())); OCSPReq req = gen.generate(); // Send the request and receive a singleResponse SingleResp singleResp = sendOCSPPost(req.getEncoded()); CertificateID certId = singleResp.getCertID(); assertEquals("Serno in response does not match serno in request.", certId.getSerialNumber(), ocspTestCert.getSerialNumber()); Object status = singleResp.getCertStatus(); assertTrue("Status is not RevokedStatus", status instanceof RevokedStatus); RevokedStatus rev = (RevokedStatus) status; assertTrue("Status does not have reason", rev.hasRevocationReason()); int reason = rev.getRevocationReason(); assertEquals("Wrong revocation reason", reason, RevokedCertInfo.REVOKATION_REASON_KEYCOMPROMISE); log.debug("<test03OcspRevoked()"); } /** Tests ocsp message * @throws Exception error */ public void test04OcspUnknown() throws Exception { log.debug(">test04OcspUnknown()"); // An OCSP request for an unknown certificate (not exist in db) OCSPReqGenerator gen = new OCSPReqGenerator(); gen.addRequest(new CertificateID(CertificateID.HASH_SHA1, cacert, new BigInteger("1"))); OCSPReq req = gen.generate(); // Send the request and receive a singleResponse SingleResp singleResp = sendOCSPPost(req.getEncoded()); CertificateID certId = singleResp.getCertID(); assertEquals("Serno in response does not match serno in request.", certId.getSerialNumber(), new BigInteger("1")); Object status = singleResp.getCertStatus(); assertTrue("Status is not Unknown", status instanceof UnknownStatus); log.debug("<test04OcspUnknown()"); } /** Tests ocsp message * @throws Exception error */ public void test05OcspUnknownCA() throws Exception { log.debug(">test05OcspUnknownCA()"); // An OCSP request for a certificate from an unknwon CA OCSPReqGenerator gen = new OCSPReqGenerator(); gen.addRequest(new CertificateID(CertificateID.HASH_SHA1, unknowncacert, new BigInteger("1"))); OCSPReq req = gen.generate(); // Send the request and receive a singleResponse SingleResp singleResp = sendOCSPPost(req.getEncoded()); CertificateID certId = singleResp.getCertID(); assertEquals("Serno in response does not match serno in request.", certId.getSerialNumber(), new BigInteger("1")); Object status = singleResp.getCertStatus(); assertTrue("Status is not Unknown", status instanceof UnknownStatus); log.debug("<test05OcspUnknownCA()"); } public void test06OcspSendWrongContentType() throws Exception { // An OCSP request for a certificate from an unknwon CA OCSPReqGenerator gen = new OCSPReqGenerator(); gen.addRequest(new CertificateID(CertificateID.HASH_SHA1, unknowncacert, new BigInteger("1"))); OCSPReq req = gen.generate(); // POST the OCSP request URL url = new URL(httpReqPath + '/' + resourceOcsp); HttpURLConnection con = (HttpURLConnection)url.openConnection(); // we are going to do a POST con.setDoOutput(true); con.setRequestMethod("POST"); // POST it, but don't add content type OutputStream os = con.getOutputStream(); os.write(req.getEncoded()); os.close(); assertEquals("Response code", 400, con.getResponseCode()); } // // Private helper methods // private SingleResp sendOCSPPost(byte[] ocspPackage) throws IOException, OCSPException, NoSuchProviderException { // POST the OCSP request URL url = new URL(httpReqPath + '/' + resourceOcsp); HttpURLConnection con = (HttpURLConnection)url.openConnection(); // we are going to do a POST con.setDoOutput(true); con.setRequestMethod("POST"); // POST it con.setRequestProperty("Content-Type", "application/ocsp-request"); OutputStream os = con.getOutputStream(); os.write(ocspPackage); os.close(); assertEquals("Response code", 200, con.getResponseCode()); assertEquals("Content-Type", "application/ocsp-response", con.getContentType()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // This works for small requests, and OCSP requests are small InputStream in = con.getInputStream(); int b = in.read(); while (b != -1) { baos.write(b); b = in.read(); } baos.flush(); in.close(); byte[] respBytes = baos.toByteArray(); OCSPResp response = new OCSPResp(new ByteArrayInputStream(respBytes)); assertEquals("Response status not zero.", response.getStatus(), 0); BasicOCSPResp brep = (BasicOCSPResp) response.getResponseObject(); X509Certificate[] chain = brep.getCerts("BC"); boolean verify = brep.verify(chain[0].getPublicKey(), "BC"); assertTrue("Response failed to verify.", verify); SingleResp[] singleResps = brep.getResponses(); assertEquals("No of SingResps shoudl be 1.", singleResps.length, 1); SingleResp singleResp = singleResps[0]; return singleResp; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -