001: package org.bouncycastle.jce.provider.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.math.BigInteger;
005: import java.security.KeyFactory;
006: import java.security.PrivateKey;
007: import java.security.PublicKey;
008: import java.security.Security;
009: import java.security.cert.Certificate;
010: import java.security.cert.CertificateFactory;
011: import java.security.cert.X509Certificate;
012: import java.security.spec.RSAPrivateCrtKeySpec;
013: import java.security.spec.RSAPublicKeySpec;
014: import java.util.Date;
015: import java.util.Hashtable;
016: import java.util.Vector;
017:
018: import org.bouncycastle.asn1.ASN1InputStream;
019: import org.bouncycastle.jce.PKCS7SignedData;
020: import org.bouncycastle.jce.X509Principal;
021: import org.bouncycastle.x509.X509V1CertificateGenerator;
022: import org.bouncycastle.x509.X509V3CertificateGenerator;
023: import org.bouncycastle.jce.provider.BouncyCastleProvider;
024: import org.bouncycastle.util.encoders.Base64;
025: import org.bouncycastle.util.encoders.Hex;
026: import org.bouncycastle.util.test.SimpleTestResult;
027: import org.bouncycastle.util.test.Test;
028: import org.bouncycastle.util.test.TestResult;
029:
030: /**
031: **/
032: public class PKCS7SignedDataTest implements Test {
033: byte[] sample1 = Base64
034: .decode("MIINBwYJKoZIhvcNAQcCoIIM+DCCDPQCAQExDjAMBggqhkiG9w0CBQUAMAsG"
035: + "CSqGSIb3DQEHAaCCC0EwggNiMIICy6ADAgECAhAL2gsXwT+JjqsJdHq0zi4z"
036: + "MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMRcwFQYDVQQKEw5WZXJp"
037: + "U2lnbiwgSW5jLjE3MDUGA1UECxMuQ2xhc3MgMSBQdWJsaWMgUHJpbWFyeSBD"
038: + "ZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05ODA1MTIwMDAwMDBaFw0wODA1"
039: + "MTIyMzU5NTlaMIHMMRcwFQYDVQQKEw5WZXJpU2lnbiwgSW5jLjEfMB0GA1UE"
040: + "CxMWVmVyaVNpZ24gVHJ1c3QgTmV0d29yazFGMEQGA1UECxM9d3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qqvIcV1HY0ijm0odsCzk"
089: + "oKd/zKsECUEYYEy+aWscexAbVBpc0tU8KczxbaaApOKDUlC9eGBtAhTkvkXJ"
090: + "s48CAwEAAaOCAQYwggECMAkGA1UdEwQCMAAwgawGA1UdIASBpDCBoTCBngYL"
091: + "YIZIAYb4RQEHAQEwgY4wKAYIKwYBBQUHAgEWHGh0dHBzOi8vd3d3LnZlcmlz"
092: + "aWduLmNvbS9DUFMwYgYIKwYBBQUHAgIwVjAVFg5WZXJpU2lnbiwgSW5jLjAD"
093: + "AgEBGj1WZXJpU2lnbidzIENQUyBpbmNvcnAuIGJ5IHJlZmVyZW5jZSBsaWFi"
094: + "LiBsdGQuIChjKTk3IFZlcmlTaWduMBEGCWCGSAGG+EIBAQQEAwIHgDAzBgNV"
095: + "HR8ELDAqMCigJqAkhiJodHRwOi8vY3JsLnZlcmlzaWduLmNvbS9jbGFzczEu"
096: + "Y3JsMA0GCSqGSIb3DQEBBAUAA4GBAFCIm9xpgS9C64+B0hxEXDvJkYyBSwhd"
097: + "DT/650jbPHrdF7Bego3RozqNPSsP0DkYMJ8K4MAfAGnQ8u9+zx2pS4XxYm91"
098: + "j77Z7eqTW9dDraZc9r16r/RzxGV12+Bu8L++T+JyCAbGXnQrEYccTV+Pql46"
099: + "bJWSVkeCwtnxxZ0YIRTxMYIBizCCAYcCAQEwgeEwgcwxFzAVBgNVBAoTDlZl"
100: + "cmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3Jr"
101: + "MUYwRAYDVQQLEz13d3cudmVyaXNpZ24uY29tL3JlcG9zaXRvcnkvUlBBIElu"
102: + "Y29ycC4gQnkgUmVmLixMSUFCLkxURChjKTk4MUgwRgYDVQQDEz9WZXJpU2ln"
103: + "biBDbGFzcyAxIENBIEluZGl2aWR1YWwgU3Vic2NyaWJlci1QZXJzb25hIE5v"
104: + "dCBWYWxpZGF0ZWQCEC4XdWvd1KKU0izHVhcehNIwDAYIKoZIhvcNAgUFADAN"
105: + "BgkqhkiG9w0BAQEFAASBgAc1aYCUgUnXxRK5RfArNuu6FBQkEg4wZdOxHn+q"
106: + "UQpMZE1ON+9Z/H5p922XoM557EXU4YAdcsGqCXv4TqOXf2jMCZrBuAkaOXC2"
107: + "xiRdYihm2hPE7mi7NBTVmoUnstvkO+G5yOoNm41Ev1PyH6ijCKIWwjQYlYuG"
108: + "YGBH6F9KCk+sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
109: + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
110:
111: byte[] sample2 = Base64
112: .decode("MIIIlAYJKoZIhvcNAQcCoIIIhTCCCIECAQExCzAJBgUrDgMCGgUAMAsGCSqG"
113: + "SIb3DQEHAaCCB3UwggOtMIIDa6ADAgECAgEzMAsGByqGSM44BAMFADCBkDEL"
114: + "MAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRIwEAYDVQQHEwlQYWxvIEFsdG8x"
115: + "HTAbBgNVBAoTFFN1biBNaWNyb3N5c3RlbXMgSW5jMSMwIQYDVQQLExpKYXZh"
116: + "IFNvZnR3YXJlIENvZGUgU2lnbmluZzEcMBoGA1UEAxMTSkNFIENvZGUgU2ln"
117: + "bmluZyBDQTAeFw0wMTA1MjkxNjQ3MTFaFw0wNjA1MjgxNjQ3MTFaMG4xHTAb"
118: + "BgNVBAoTFFN1biBNaWNyb3N5c3RlbXMgSW5jMSMwIQYDVQQLExpKYXZhIFNv"
119: + "ZnR3YXJlIENvZGUgU2lnbmluZzEoMCYGA1UEAxMfVGhlIExlZ2lvbiBvZiB0"
120: + "aGUgQm91bmN5IENhc3RsZTCCAbcwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OB"
121: + "HXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2"
122: + "y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUP"
123: + "BPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvM"
124: + "spK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9"
125: + "B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCj"
126: + "rh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtV"
127: + "JWQBTDv+z0kqA4GEAAKBgBWry/FCAZ6miyy39+ftsa+h9lxoL+JtV0MJcUyQ"
128: + "E4VAhpAwWb8vyjba9AwOylYQTktHX5sAkFvjBiU0LOYDbFSTVZSHMRJgfjxB"
129: + "SHtICjOEvr1BJrrOrdzqdxcOUge5n7El124BCrv91x5Ol8UTwtiO9LrRXF/d"
130: + "SyK+RT5n1klRo3YwdDARBglghkgBhvhCAQEEBAMCAIcwDgYDVR0PAQH/BAQD"
131: + "AgHGMB0GA1UdDgQWBBQwMY4NRcco1AO3w1YsokfDLVseEjAPBgNVHRMBAf8E"
132: + "BTADAQH/MB8GA1UdIwQYMBaAFGXi9IbJ007wkU5Yomr12HhamsGmMAsGByqG"
133: + "SM44BAMFAAMvADAsAhRmigTu6QV0sTfEkVljgij/hhdVfAIUQZvMxAnIHc30"
134: + "y/u0C1T5UEG9glUwggPAMIIDfqADAgECAgEQMAsGByqGSM44BAMFADCBkDEL"
135: + "MAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRIwEAYDVQQHEwlQYWxvIEFsdG8x"
136: + "HTAbBgNVBAoTFFN1biBNaWNyb3N5c3RlbXMgSW5jMSMwIQYDVQQLExpKYXZh"
137: + "IFNvZnR3YXJlIENvZGUgU2lnbmluZzEcMBoGA1UEAxMTSkNFIENvZGUgU2ln"
138: + "bmluZyBDQTAeFw0wMTA0MjUwNzAwMDBaFw0yMDA0MjUwNzAwMDBaMIGQMQsw"
139: + "CQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEjAQBgNVBAcTCVBhbG8gQWx0bzEd"
140: + "MBsGA1UEChMUU3VuIE1pY3Jvc3lzdGVtcyBJbmMxIzAhBgNVBAsTGkphdmEg"
141: + "U29mdHdhcmUgQ29kZSBTaWduaW5nMRwwGgYDVQQDExNKQ0UgQ29kZSBTaWdu"
142: + "aW5nIENBMIIBtzCCASwGByqGSM44BAEwggEfAoGBAOuvNwQeylEeaV2w8o/2"
143: + "tUkfxqSZBdcpv3S3avUZ2B7kG/gKAZqY/3Cr4kpWhmxTs/zhyIGMMfDE87CL"
144: + "5nAG7PdpaNuDTHIpiSk2F1w7SgegIAIqRpdRHXDICBgLzgxum3b3BePn+9Nh"
145: + "eeFgmiSNBpWDPFEg4TDPOFeCphpyDc7TAhUAhCVF4bq5qWKreehbMLiJaxv/"
146: + "e3UCgYEAq8l0e3Tv7kK1alNNO92QBnJokQ8LpCl2LlU71a5NZVx+KjoEpmem"
147: + "0HGqpde34sFyDaTRqh6SVEwgAAmisAlBGTMAssNcrkL4sYvKfJbYEH83RFuq"
148: + "zHjI13J2N2tAmahVZvqoAx6LShECactMuCUGHKB30sms0j3pChD6dnC3+9wD"
149: + "gYQAAoGALQmYXKy4nMeZfu4gGSo0kPnXq6uu3WtylQ1m+O8nj0Sy7ShEx/6v"
150: + "sKYnbwBnRYJbB6hWVjvSKVFhXmk51y50dxLPGUr1LcjLcmHETm/6R0M/FLv6"
151: + "vBhmKMLZZot6LS/CYJJLFP5YPiF/aGK+bEhJ+aBLXoWdGRD5FUVRG3HU9wuj"
152: + "ZjBkMBEGCWCGSAGG+EIBAQQEAwIABzAPBgNVHRMBAf8EBTADAQH/MB8GA1Ud"
153: + "IwQYMBaAFGXi9IbJ007wkU5Yomr12HhamsGmMB0GA1UdDgQWBBRl4vSGydNO"
154: + "8JFOWKJq9dh4WprBpjALBgcqhkjOOAQDBQADLwAwLAIUKvfPPJdd+Xi2CNdB"
155: + "tNkNRUzktJwCFEXNdWkOIfod1rMpsun3Mx0z/fxJMYHoMIHlAgEBMIGWMIGQ"
156: + "MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEjAQBgNVBAcTCVBhbG8gQWx0"
157: + "bzEdMBsGA1UEChMUU3VuIE1pY3Jvc3lzdGVtcyBJbmMxIzAhBgNVBAsTGkph"
158: + "dmEgU29mdHdhcmUgQ29kZSBTaWduaW5nMRwwGgYDVQQDExNKQ0UgQ29kZSBT"
159: + "aWduaW5nIENBAgEzMAkGBSsOAwIaBQAwCwYHKoZIzjgEAQUABC8wLQIVAIGV"
160: + "khm+kbV4a/+EP45PHcq0hIViAhR4M9os6IrJnoEDS3Y3l7O6zrSosA==");
161:
162: public String getName() {
163: return "PKCS7SignedData";
164: }
165:
166: public TestResult parseTest(byte[] sample) {
167: try {
168: PKCS7SignedData signedData = new PKCS7SignedData(sample);
169:
170: Certificate[] certs = signedData.getCertificates();
171:
172: return new SimpleTestResult(true, getName() + ": Okay");
173: } catch (Exception e) {
174: return new SimpleTestResult(false, getName()
175: + ": exception - " + e.toString());
176: }
177: }
178:
179: /**
180: * we generate a self signed certificate for the sake of testing - RSA -
181: * and then try signing some data.
182: */
183: public TestResult checkCreation() {
184: //
185: // a sample key pair.
186: //
187: RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
188: new BigInteger(
189: "b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7",
190: 16), new BigInteger("11", 16));
191:
192: RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(
193: new BigInteger(
194: "b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7",
195: 16),
196: new BigInteger("11", 16),
197: new BigInteger(
198: "9f66f6b05410cd503b2709e88115d55daced94d1a34d4e32bf824d0dde6028ae79c5f07b580f5dce240d7111f7ddb130a7945cd7d957d1920994da389f490c89",
199: 16),
200: new BigInteger(
201: "c0a0758cdf14256f78d4708c86becdead1b50ad4ad6c5c703e2168fbf37884cb",
202: 16),
203: new BigInteger(
204: "f01734d7960ea60070f1b06f2bb81bfac48ff192ae18451d5e56c734a5aab8a5",
205: 16),
206: new BigInteger(
207: "b54bb9edff22051d9ee60f9351a48591b6500a319429c069a3e335a1d6171391",
208: 16),
209: new BigInteger(
210: "d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6fc483533d8297dd7884cd",
211: 16),
212: new BigInteger(
213: "b8f52fc6f38593dabb661d3f50f8897f8106eee68b1bce78a95b132b4e5b5d19",
214: 16));
215:
216: //
217: // set up the keys
218: //
219: PrivateKey privKey;
220: PublicKey pubKey;
221:
222: try {
223: KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
224:
225: privKey = fact.generatePrivate(privKeySpec);
226: pubKey = fact.generatePublic(pubKeySpec);
227: } catch (Exception e) {
228: return new SimpleTestResult(false, getName()
229: + ": error setting up keys - " + e.toString());
230: }
231:
232: //
233: // distinguished name table.
234: //
235: Hashtable attrs = new Hashtable();
236:
237: attrs.put(X509Principal.C, "AU");
238: attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");
239: attrs.put(X509Principal.L, "Melbourne");
240: attrs.put(X509Principal.ST, "Victoria");
241: attrs.put(X509Principal.E, "feedback-crypto@bouncycastle.org");
242:
243: Vector ord = new Vector();
244: Vector values = new Vector();
245:
246: ord.addElement(X509Principal.C);
247: ord.addElement(X509Principal.O);
248: ord.addElement(X509Principal.L);
249: ord.addElement(X509Principal.ST);
250: ord.addElement(X509Principal.E);
251:
252: values.addElement("AU");
253: values.addElement("The Legion of the Bouncy Castle");
254: values.addElement("Melbourne");
255: values.addElement("Victoria");
256: values.addElement("feedback-crypto@bouncycastle.org");
257:
258: //
259: // extensions
260: //
261:
262: //
263: // create the certificate - version 3
264: //
265: X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
266:
267: certGen.setSerialNumber(BigInteger.valueOf(1));
268: certGen.setIssuerDN(new X509Principal(attrs));
269: certGen.setNotBefore(new Date(
270: System.currentTimeMillis() - 50000));
271: certGen
272: .setNotAfter(new Date(
273: System.currentTimeMillis() + 50000));
274: certGen.setSubjectDN(new X509Principal(attrs));
275: certGen.setPublicKey(pubKey);
276: certGen.setSignatureAlgorithm("MD5WithRSAEncryption");
277:
278: try {
279: X509Certificate cert = certGen
280: .generateX509Certificate(privKey);
281:
282: cert.checkValidity(new Date());
283:
284: cert.verify(pubKey);
285:
286: ByteArrayInputStream sbIn = new ByteArrayInputStream(cert
287: .getEncoded());
288: ASN1InputStream sdIn = new ASN1InputStream(sbIn);
289: ByteArrayInputStream bIn = new ByteArrayInputStream(cert
290: .getEncoded());
291: CertificateFactory fact = CertificateFactory.getInstance(
292: "X.509", "BC");
293:
294: cert = (X509Certificate) fact.generateCertificate(bIn);
295:
296: Certificate[] certs = new Certificate[1];
297: certs[0] = cert;
298:
299: PKCS7SignedData pkcs7sd = new PKCS7SignedData(privKey,
300: certs, "MD5");
301:
302: byte[] bytes = Hex.decode("0102030405060708091011121314");
303:
304: pkcs7sd.update(bytes, 0, bytes.length);
305:
306: byte[] p = pkcs7sd.getEncoded();
307:
308: pkcs7sd = new PKCS7SignedData(p);
309:
310: pkcs7sd.update(bytes, 0, bytes.length);
311:
312: if (!pkcs7sd.verify()) {
313: return new SimpleTestResult(false,
314: "PKCS7 verification failed");
315: }
316: } catch (Exception e) {
317: return new SimpleTestResult(false, getName()
318: + ": error setting generating cert - "
319: + e.toString());
320: }
321:
322: //
323: // create the certificate - version 1
324: //
325: X509V1CertificateGenerator certGen1 = new X509V1CertificateGenerator();
326:
327: certGen1.setSerialNumber(BigInteger.valueOf(1));
328: certGen1.setIssuerDN(new X509Principal(ord, attrs));
329: certGen1.setNotBefore(new Date(
330: System.currentTimeMillis() - 50000));
331: certGen1.setNotAfter(new Date(
332: System.currentTimeMillis() + 50000));
333: certGen1.setSubjectDN(new X509Principal(ord, values));
334: certGen1.setPublicKey(pubKey);
335: certGen1.setSignatureAlgorithm("MD5WithRSAEncryption");
336:
337: try {
338: X509Certificate cert = certGen1
339: .generateX509Certificate(privKey);
340:
341: cert.checkValidity(new Date());
342:
343: cert.verify(pubKey);
344:
345: ByteArrayInputStream bIn = new ByteArrayInputStream(cert
346: .getEncoded());
347: CertificateFactory fact = CertificateFactory.getInstance(
348: "X.509", "BC");
349:
350: cert = (X509Certificate) fact.generateCertificate(bIn);
351:
352: // System.out.println(cert);
353: if (!cert.getIssuerDN().equals(cert.getSubjectDN())) {
354: return new SimpleTestResult(false, getName()
355: + ": name comparison fails");
356: }
357: } catch (Exception e) {
358: return new SimpleTestResult(false, getName()
359: + ": error setting generating cert - "
360: + e.toString());
361: }
362:
363: return new SimpleTestResult(true, getName() + ": Okay");
364: }
365:
366: public TestResult perform() {
367: TestResult res = parseTest(sample1);
368:
369: if (!res.isSuccessful()) {
370: return res;
371: }
372:
373: res = parseTest(sample2);
374: if (!res.isSuccessful()) {
375: return res;
376: }
377:
378: return checkCreation();
379: }
380:
381: public static void main(String[] args) {
382: Security.addProvider(new BouncyCastleProvider());
383:
384: Test test = new PKCS7SignedDataTest();
385: TestResult result = test.perform();
386:
387: System.out.println(result.toString());
388: }
389: }
|