001: package org.bouncycastle.asn1.test;
002:
003: import java.io.ByteArrayInputStream;
004:
005: import org.bouncycastle.asn1.ASN1InputStream;
006: import org.bouncycastle.asn1.DEROctetString;
007: import org.bouncycastle.asn1.ocsp.BasicOCSPResponse;
008: import org.bouncycastle.asn1.ocsp.OCSPRequest;
009: import org.bouncycastle.asn1.ocsp.OCSPResponse;
010: import org.bouncycastle.asn1.ocsp.ResponseBytes;
011: import org.bouncycastle.util.encoders.Base64;
012: import org.bouncycastle.util.test.Test;
013: import org.bouncycastle.util.test.TestResult;
014: import org.bouncycastle.util.test.SimpleTestResult;
015:
016: public class OCSPTest implements Test {
017: private byte[] unsignedReq = Base64
018: .decode("MEIwQDA+MDwwOjAJBgUrDgMCGgUABBRDb9GODnq7lRhSkEqw4XX24huERwQUkY4j"
019: + "a6eKuDlkVP9hRgkEvIWqHPECAQE=");
020:
021: private byte[] signedReq = Base64
022: .decode("MIIC9jBAMD4wPDA6MAkGBSsOAwIaBQAEFENv0Y4OeruVGFKQSrDhdfbiG4RHBBTc"
023: + "Mr1fP+mZAxbF2ZdehWxn6mtAngIBAaCCArAwggKsMA0GCSqGSIb3DQEBBQUAA4GB"
024: + "AAzHBm4nL5AcRQB3Jkz7ScNeZF+GbRZ0p4kBDTnqi3IeESuso12yJhpqqyijdnj5"
025: + "gd4/GsSAgdluLHyYZ6wgozV7G9MDXCnFnG4PBUW05HaVX81JYAp+amVyU0NOgNrG"
026: + "90npVBsHb0o+UlkxNgMiEbSkp/TeGb6YURsYKhmwp7BgoIICFTCCAhEwggINMIIB"
027: + "dqADAgECAgEBMA0GCSqGSIb3DQEBBAUAMCUxFjAUBgNVBAoTDUJvdW5jeSBDYXN0"
028: + "bGUxCzAJBgNVBAYTAkFVMB4XDTA0MTAyNDEzNDc0M1oXDTA1MDIwMTEzNDc0M1ow"
029: + "JTEWMBQGA1UEChMNQm91bmN5IENhc3RsZTELMAkGA1UEBhMCQVUwgZ8wDQYJKoZI"
030: + "hvcNAQEBBQADgY0AMIGJAoGBAJBmLeIzthMHUeTkOeJ76iBxcMHY31o/i3a9VT12"
031: + "y2FcS/ejJmeUCMTdtwl5alOwXY66vF4DyT1VU/nJG3mHpSoqq7qrMXOIFGcXg1Wf"
032: + "oJRrQgTOLdQ6bod7i9ME/EjEJy70orh0nVS7NGcu0R5TjcbLde2J5zxjb/W9wqfy"
033: + "RovJAgMBAAGjTTBLMB0GA1UdDgQWBBTcMr1fP+mZAxbF2ZdehWxn6mtAnjAfBgNV"
034: + "HSMEGDAWgBTcMr1fP+mZAxbF2ZdehWxn6mtAnjAJBgNVHRMEAjAAMA0GCSqGSIb3"
035: + "DQEBBAUAA4GBAF/4EH1KkNrNxocJPIp7lThmG1KIVYESIadowMowrbok46ESofRF"
036: + "OIPku07W+e1Y1Y1KXLIiPMG3IGwrBrn04iLsbbBUiN37BcC/VyT4xKJ2MYscGjKL"
037: + "ua/9bU0lOyeTRAwqb8towWRd5lLYAI3RQ7dhStUTFp3Vqd803PJ/cpR6");
038:
039: private byte[] response = Base64
040: .decode("MIIFnAoBAKCCBZUwggWRBgkrBgEFBQcwAQEEggWCMIIFfjCCARehgZ8wgZwx"
041: + "CzAJBgNVBAYTAklOMRcwFQYDVQQIEw5BbmRocmEgcHJhZGVzaDESMBAGA1UE"
042: + "BxMJSHlkZXJhYmFkMQwwCgYDVQQKEwNUQ1MxDDAKBgNVBAsTA0FUQzEeMBwG"
043: + "A1UEAxMVVENTLUNBIE9DU1AgUmVzcG9uZGVyMSQwIgYJKoZIhvcNAQkBFhVv"
044: + "Y3NwQHRjcy1jYS50Y3MuY28uaW4YDzIwMDMwNDAyMTIzNDU4WjBiMGAwOjAJ"
045: + "BgUrDgMCGgUABBRs07IuoCWNmcEl1oHwIak1BPnX8QQUtGyl/iL9WJ1VxjxF"
046: + "j0hAwJ/s1AcCAQKhERgPMjAwMjA4MjkwNzA5MjZaGA8yMDAzMDQwMjEyMzQ1"
047: + "OFowDQYJKoZIhvcNAQEFBQADgYEAfbN0TCRF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kwNgYIKwYBBQUHAQEEKjAoMCYG"
064: + "CCsGAQUFBzABhhpodHRwOi8vMTcyLjE5LjQwLjExMDo3NzAwLzAtBgNVHR8E"
065: + "JjAkMCKgIKAehhxodHRwOi8vMTcyLjE5LjQwLjExMC9jcmwuY3JsMA0GCSqG"
066: + "SIb3DQEBBQUAA4IBAQB6FovM3B4VDDZ15o12gnADZsIk9fTAczLlcrmXLNN4"
067: + "PgmqgnwF0Ymj3bD5SavDOXxbA65AZJ7rBNAguLUo+xVkgxmoBH7R2sBxjTCc"
068: + "r07NEadxM3HQkt0aX5XYEl8eRoifwqYAI9h0ziZfTNes8elNfb3DoPPjqq6V"
069: + "mMg0f0iMS4W8LjNPorjRB+kIosa1deAGPhq0eJ8yr0/s2QR2/WFD5P4aXc8I"
070: + "KWleklnIImS3zqiPrq6tl2Bm8DZj7vXlTOwmraSQxUwzCKwYob1yGvNOUQTq"
071: + "pG6jxn7jgDawHU1+WjWQe4Q34/pWeGLysxTraMa+Ug9kPe+jy/qRX2xwvKBZ");
072:
073: private boolean isSameAs(byte[] a, byte[] b) {
074: if (a.length != b.length) {
075: return false;
076: }
077:
078: for (int i = 0; i != a.length; i++) {
079: if (a[i] != b[i]) {
080: return false;
081: }
082: }
083:
084: return true;
085: }
086:
087: private TestResult unsignedRequest() {
088: try {
089: ASN1InputStream aIn = new ASN1InputStream(
090: new ByteArrayInputStream(unsignedReq));
091: OCSPRequest req = OCSPRequest.getInstance(aIn.readObject());
092:
093: if (!isSameAs(req.getEncoded(), unsignedReq)) {
094: return new SimpleTestResult(false, getName()
095: + ": OCSP unsigned request failed to re-encode");
096: }
097:
098: return new SimpleTestResult(true, getName() + ": Okay");
099: } catch (Exception e) {
100: return new SimpleTestResult(false, getName()
101: + ": failed unsigned exception - " + e.toString(),
102: e);
103: }
104: }
105:
106: private TestResult signedRequest() {
107: try {
108: ASN1InputStream aIn = new ASN1InputStream(
109: new ByteArrayInputStream(signedReq));
110: OCSPRequest req = OCSPRequest.getInstance(aIn.readObject());
111:
112: if (!isSameAs(req.getEncoded(), signedReq)) {
113: return new SimpleTestResult(false, getName()
114: + ": OCSP signed request failed to re-encode");
115: }
116:
117: return new SimpleTestResult(true, getName() + ": Okay");
118: } catch (Exception e) {
119: return new SimpleTestResult(false, getName()
120: + ": failed signed exception - " + e.toString(), e);
121: }
122: }
123:
124: private TestResult response() {
125: try {
126: ASN1InputStream aIn = new ASN1InputStream(
127: new ByteArrayInputStream(response));
128: OCSPResponse resp = OCSPResponse.getInstance(aIn
129: .readObject());
130: ResponseBytes rBytes = ResponseBytes.getInstance(resp
131: .getResponseBytes());
132:
133: aIn = new ASN1InputStream(new ByteArrayInputStream(rBytes
134: .getResponse().getOctets()));
135:
136: BasicOCSPResponse bResp = BasicOCSPResponse.getInstance(aIn
137: .readObject());
138:
139: resp = new OCSPResponse(resp.getResponseStatus(),
140: new ResponseBytes(rBytes.getResponseType(),
141: new DEROctetString(bResp.getEncoded())));
142:
143: if (!isSameAs(resp.getEncoded(), response)) {
144: return new SimpleTestResult(false, getName()
145: + ": OCSP response failed to re-encode");
146: }
147:
148: return new SimpleTestResult(true, getName() + ": Okay");
149: } catch (Exception e) {
150: return new SimpleTestResult(false, getName()
151: + ": failed response exception - " + e.toString(),
152: e);
153: }
154: }
155:
156: public TestResult perform() {
157: TestResult res = unsignedRequest();
158:
159: if (!res.isSuccessful()) {
160: return res;
161: }
162:
163: res = signedRequest();
164: if (!res.isSuccessful()) {
165: return res;
166: }
167:
168: return response();
169: }
170:
171: public String getName() {
172: return "OCSP";
173: }
174:
175: public static void main(String[] args) {
176: OCSPTest test = new OCSPTest();
177: TestResult result = test.perform();
178:
179: System.out.println(result);
180: }
181: }
|