Source Code Cross Referenced for EnvelopedDataStreamTest.java in  » Security » Bouncy-Castle » org » bouncycastle » cms » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » Bouncy Castle » org.bouncycastle.cms.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.cms.test;
002:
003:        import junit.framework.Test;
004:        import junit.framework.TestCase;
005:        import junit.framework.TestSuite;
006:        import org.bouncycastle.asn1.ASN1InputStream;
007:        import org.bouncycastle.asn1.DEROutputStream;
008:        import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
009:        import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
010:        import org.bouncycastle.cms.CMSEnvelopedDataParser;
011:        import org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator;
012:        import org.bouncycastle.cms.CMSTypedStream;
013:        import org.bouncycastle.cms.RecipientId;
014:        import org.bouncycastle.cms.RecipientInformation;
015:        import org.bouncycastle.cms.RecipientInformationStore;
016:        import org.bouncycastle.jce.PrincipalUtil;
017:        import org.bouncycastle.util.encoders.Base64;
018:        import org.bouncycastle.util.encoders.Hex;
019:
020:        import javax.crypto.SecretKey;
021:        import java.io.BufferedOutputStream;
022:        import java.io.ByteArrayOutputStream;
023:        import java.io.InputStream;
024:        import java.io.OutputStream;
025:        import java.security.Key;
026:        import java.security.KeyFactory;
027:        import java.security.KeyPair;
028:        import java.security.Security;
029:        import java.security.cert.X509Certificate;
030:        import java.security.spec.PKCS8EncodedKeySpec;
031:        import java.util.Arrays;
032:        import java.util.Collection;
033:        import java.util.Iterator;
034:
035:        public class EnvelopedDataStreamTest extends TestCase {
036:            private static final int BUFFER_SIZE = 4000;
037:            private static String _signDN;
038:            private static KeyPair _signKP;
039:            private static X509Certificate _signCert;
040:
041:            private static String _origDN;
042:            private static KeyPair _origKP;
043:            private static X509Certificate _origCert;
044:
045:            private static String _reciDN;
046:            private static KeyPair _reciKP;
047:            private static X509Certificate _reciCert;
048:
049:            private static KeyPair _origEcKP;
050:            private static KeyPair _reciEcKP;
051:            private static X509Certificate _reciEcCert;
052:
053:            private static boolean _initialised = false;
054:
055:            public EnvelopedDataStreamTest() {
056:            }
057:
058:            private static void init() throws Exception {
059:                if (!_initialised) {
060:                    _initialised = true;
061:
062:                    _signDN = "O=Bouncy Castle, C=AU";
063:                    _signKP = CMSTestUtil.makeKeyPair();
064:                    _signCert = CMSTestUtil.makeCertificate(_signKP, _signDN,
065:                            _signKP, _signDN);
066:
067:                    _origDN = "CN=Bob, OU=Sales, O=Bouncy Castle, C=AU";
068:                    _origKP = CMSTestUtil.makeKeyPair();
069:                    _origCert = CMSTestUtil.makeCertificate(_origKP, _origDN,
070:                            _signKP, _signDN);
071:
072:                    _reciDN = "CN=Doug, OU=Sales, O=Bouncy Castle, C=AU";
073:                    _reciKP = CMSTestUtil.makeKeyPair();
074:                    _reciCert = CMSTestUtil.makeCertificate(_reciKP, _reciDN,
075:                            _signKP, _signDN);
076:
077:                    _origEcKP = CMSTestUtil.makeEcDsaKeyPair();
078:                    _reciEcKP = CMSTestUtil.makeEcDsaKeyPair();
079:                    _reciEcCert = CMSTestUtil.makeCertificate(_reciEcKP,
080:                            _reciDN, _signKP, _signDN);
081:                }
082:            }
083:
084:            public void setUp() throws Exception {
085:                init();
086:            }
087:
088:            public void testWorkingData() throws Exception {
089:                byte[] keyData = Base64
090:                        .decode("MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKrAz/SQKrcQ"
091:                                + "nj9IxHIfKDbuXsMqUpI06s2gps6fp7RDNvtUDDMOciWGFhD45YSy8GO0mPx3"
092:                                + "Nkc7vKBqX4TLcqLUz7kXGOHGOwiPZoNF+9jBMPNROe/B0My0PkWg9tuq+nxN"
093:                                + "64oD47+JvDwrpNOS5wsYavXeAW8Anv9ZzHLU7KwZAgMBAAECgYA/fqdVt+5K"
094:                                + "WKGfwr1Z+oAHvSf7xtchiw/tGtosZ24DOCNP3fcTXUHQ9kVqVkNyzt9ZFCT3"
095:                                + "bJUAdBQ2SpfuV4DusVeQZVzcROKeA09nPkxBpTefWbSDQGhb+eZq9L8JDRSW"
096:                                + "HyYqs+MBoUpLw7GKtZiJkZyY6CsYkAnQ+uYVWq/TIQJBAP5zafO4HUV/w4KD"
097:                                + "VJi+ua+GYF1Sg1t/dYL1kXO9GP1p75YAmtm6LdnOCas7wj70/G1YlPGkOP0V"
098:                                + "GFzeG5KAmAUCQQCryvKU9nwWA+kypcQT9Yr1P4vGS0APYoBThnZq7jEPc5Cm"
099:                                + "ZI82yseSxSeea0+8KQbZ5mvh1p3qImDLEH/iNSQFAkAghS+tboKPN10NeSt+"
100:                                + "uiGRRWNbiggv0YJ7Uldcq3ZeLQPp7/naiekCRUsHD4Qr97OrZf7jQ1HlRqTu"
101:                                + "eZScjMLhAkBNUMZCQnhwFAyEzdPkQ7LpU1MdyEopYmRssuxijZao5JLqQAGw"
102:                                + "YCzXokGFa7hz72b09F4DQurJL/WuDlvvu4jdAkEAxwT9lylvfSfEQw4/qQgZ"
103:                                + "MFB26gqB6Gqs1pHIZCzdliKx5BO3VDeUGfXMI8yOkbXoWbYx5xPid/+N8R//"
104:                                + "+sxLBw==");
105:
106:                byte[] envData = Base64
107:                        .decode("MIAGCSqGSIb3DQEHA6CAMIACAQAxgcQwgcECAQAwKjAlMRYwFAYDVQQKEw1C"
108:                                + "b3VuY3kgQ2FzdGxlMQswCQYDVQQGEwJBVQIBHjANBgkqhkiG9w0BAQEFAASB"
109:                                + "gDmnaDZ0vDJNlaUSYyEXsgbaUH+itNTjCOgv77QTX2ImXj+kTctM19PQF2I1"
110:                                + "0/NL0fjakvCgBTHKmk13a7jqB6cX3bysenHNrglHsgNGgeXQ7ggAq5fV/JQQ"
111:                                + "T7rSxEtuwpbuHQnoVUZahOHVKy/a0uLr9iIh1A3y+yZTZaG505ZJMIAGCSqG"
112:                                + "SIb3DQEHATAdBglghkgBZQMEAQIEENmkYNbDXiZxJWtq82qIRZKggAQgkOGr"
113:                                + "1JcTsADStez1eY4+rO4DtyBIyUYQ3pilnbirfPkAAAAAAAAAAAAA");
114:
115:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(envData);
116:
117:                RecipientInformationStore recipients = ep.getRecipientInfos();
118:
119:                assertEquals(ep.getEncryptionAlgOID(),
120:                        CMSEnvelopedDataGenerator.AES128_CBC);
121:
122:                Collection c = recipients.getRecipients();
123:                Iterator it = c.iterator();
124:
125:                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyData);
126:                KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
127:                Key priKey = keyFact.generatePrivate(keySpec);
128:                byte[] data = Hex
129:                        .decode("57616c6c6157616c6c6157617368696e67746f6e");
130:
131:                while (it.hasNext()) {
132:                    RecipientInformation recipient = (RecipientInformation) it
133:                            .next();
134:
135:                    assertEquals(recipient.getKeyEncryptionAlgOID(),
136:                            PKCSObjectIdentifiers.rsaEncryption.getId());
137:
138:                    CMSTypedStream recData = recipient.getContentStream(priKey,
139:                            "BC");
140:
141:                    assertEquals(true, Arrays.equals(data, CMSTestUtil
142:                            .streamToByteArray(recData.getContentStream())));
143:                }
144:            }
145:
146:            private void verifyData(ByteArrayOutputStream encodedStream,
147:                    String expectedOid, byte[] expectedData) throws Exception {
148:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(
149:                        encodedStream.toByteArray());
150:                RecipientInformationStore recipients = ep.getRecipientInfos();
151:
152:                assertEquals(ep.getEncryptionAlgOID(), expectedOid);
153:
154:                Collection c = recipients.getRecipients();
155:                Iterator it = c.iterator();
156:
157:                while (it.hasNext()) {
158:                    RecipientInformation recipient = (RecipientInformation) it
159:                            .next();
160:
161:                    assertEquals(recipient.getKeyEncryptionAlgOID(),
162:                            PKCSObjectIdentifiers.rsaEncryption.getId());
163:
164:                    CMSTypedStream recData = recipient.getContentStream(_reciKP
165:                            .getPrivate(), "BC");
166:
167:                    assertEquals(true, Arrays.equals(expectedData, CMSTestUtil
168:                            .streamToByteArray(recData.getContentStream())));
169:                }
170:            }
171:
172:            public void testKeyTransAES128BufferedStream() throws Exception {
173:                byte[] data = new byte[2000];
174:
175:                for (int i = 0; i != 2000; i++) {
176:                    data[i] = (byte) (i & 0xff);
177:                }
178:
179:                //
180:                // unbuffered
181:                //
182:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
183:
184:                edGen.addKeyTransRecipient(_reciCert);
185:
186:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
187:
188:                OutputStream out = edGen.open(bOut,
189:                        CMSEnvelopedDataGenerator.AES128_CBC, "BC");
190:
191:                for (int i = 0; i != 2000; i++) {
192:                    out.write(data[i]);
193:                }
194:
195:                out.close();
196:
197:                verifyData(bOut, CMSEnvelopedDataGenerator.AES128_CBC, data);
198:
199:                int unbufferedLength = bOut.toByteArray().length;
200:
201:                //
202:                // buffered
203:                //
204:                edGen = new CMSEnvelopedDataStreamGenerator();
205:
206:                edGen.addKeyTransRecipient(_reciCert);
207:
208:                bOut = new ByteArrayOutputStream();
209:
210:                out = edGen.open(bOut, CMSEnvelopedDataGenerator.AES128_CBC,
211:                        "BC");
212:
213:                BufferedOutputStream bfOut = new BufferedOutputStream(out, 300);
214:
215:                for (int i = 0; i != 2000; i++) {
216:                    bfOut.write(data[i]);
217:                }
218:
219:                bfOut.close();
220:
221:                verifyData(bOut, CMSEnvelopedDataGenerator.AES128_CBC, data);
222:
223:                assertTrue(bOut.toByteArray().length < unbufferedLength);
224:            }
225:
226:            public void testKeyTransAES128Buffered() throws Exception {
227:                byte[] data = new byte[2000];
228:
229:                for (int i = 0; i != 2000; i++) {
230:                    data[i] = (byte) (i & 0xff);
231:                }
232:
233:                //
234:                // unbuffered
235:                //
236:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
237:
238:                edGen.addKeyTransRecipient(_reciCert);
239:
240:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
241:
242:                OutputStream out = edGen.open(bOut,
243:                        CMSEnvelopedDataGenerator.AES128_CBC, "BC");
244:
245:                for (int i = 0; i != 2000; i++) {
246:                    out.write(data[i]);
247:                }
248:
249:                out.close();
250:
251:                verifyData(bOut, CMSEnvelopedDataGenerator.AES128_CBC, data);
252:
253:                int unbufferedLength = bOut.toByteArray().length;
254:
255:                //
256:                // buffered
257:                //
258:                edGen = new CMSEnvelopedDataStreamGenerator();
259:
260:                edGen.setBufferSize(300);
261:
262:                edGen.addKeyTransRecipient(_reciCert);
263:
264:                bOut = new ByteArrayOutputStream();
265:
266:                out = edGen.open(bOut, CMSEnvelopedDataGenerator.AES128_CBC,
267:                        "BC");
268:
269:                for (int i = 0; i != 2000; i++) {
270:                    out.write(data[i]);
271:                }
272:
273:                out.close();
274:
275:                verifyData(bOut, CMSEnvelopedDataGenerator.AES128_CBC, data);
276:
277:                assertTrue(bOut.toByteArray().length < unbufferedLength);
278:            }
279:
280:            public void testKeyTransAES128Der() throws Exception {
281:                byte[] data = new byte[2000];
282:
283:                for (int i = 0; i != 2000; i++) {
284:                    data[i] = (byte) (i & 0xff);
285:                }
286:
287:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
288:
289:                edGen.addKeyTransRecipient(_reciCert);
290:
291:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
292:
293:                OutputStream out = edGen.open(bOut,
294:                        CMSEnvelopedDataGenerator.AES128_CBC, "BC");
295:
296:                for (int i = 0; i != 2000; i++) {
297:                    out.write(data[i]);
298:                }
299:
300:                out.close();
301:
302:                // convert to DER
303:                ASN1InputStream aIn = new ASN1InputStream(bOut.toByteArray());
304:
305:                bOut.reset();
306:
307:                DEROutputStream dOut = new DEROutputStream(bOut);
308:
309:                dOut.writeObject(aIn.readObject());
310:
311:                verifyData(bOut, CMSEnvelopedDataGenerator.AES128_CBC, data);
312:            }
313:
314:            public void testKeyTransAES128Throughput() throws Exception {
315:                byte[] data = new byte[40001];
316:
317:                for (int i = 0; i != data.length; i++) {
318:                    data[i] = (byte) (i & 0xff);
319:                }
320:
321:                //
322:                // buffered
323:                //
324:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
325:
326:                edGen.setBufferSize(BUFFER_SIZE);
327:
328:                edGen.addKeyTransRecipient(_reciCert);
329:
330:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
331:
332:                OutputStream out = edGen.open(bOut,
333:                        CMSEnvelopedDataGenerator.AES128_CBC, "BC");
334:
335:                for (int i = 0; i != data.length; i++) {
336:                    out.write(data[i]);
337:                }
338:
339:                out.close();
340:
341:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(bOut
342:                        .toByteArray());
343:                RecipientInformationStore recipients = ep.getRecipientInfos();
344:                Collection c = recipients.getRecipients();
345:                Iterator it = c.iterator();
346:
347:                if (it.hasNext()) {
348:                    RecipientInformation recipient = (RecipientInformation) it
349:                            .next();
350:
351:                    assertEquals(recipient.getKeyEncryptionAlgOID(),
352:                            PKCSObjectIdentifiers.rsaEncryption.getId());
353:
354:                    CMSTypedStream recData = recipient.getContentStream(_reciKP
355:                            .getPrivate(), "BC");
356:
357:                    InputStream dataStream = recData.getContentStream();
358:                    ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
359:                    int len;
360:                    byte[] buf = new byte[BUFFER_SIZE];
361:                    int count = 0;
362:
363:                    while (count != 10 && (len = dataStream.read(buf)) > 0) {
364:                        assertEquals(buf.length, len);
365:
366:                        dataOut.write(buf);
367:                        count++;
368:                    }
369:
370:                    len = dataStream.read(buf);
371:                    dataOut.write(buf, 0, len);
372:
373:                    assertEquals(true, Arrays.equals(data, dataOut
374:                            .toByteArray()));
375:                } else {
376:                    fail("recipient not found.");
377:                }
378:            }
379:
380:            public void testKeyTransAES128() throws Exception {
381:                byte[] data = "WallaWallaWashington".getBytes();
382:
383:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
384:
385:                edGen.addKeyTransRecipient(_reciCert);
386:
387:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
388:
389:                OutputStream out = edGen.open(bOut,
390:                        CMSEnvelopedDataGenerator.AES128_CBC, "BC");
391:
392:                out.write(data);
393:
394:                out.close();
395:
396:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(bOut
397:                        .toByteArray());
398:
399:                RecipientInformationStore recipients = ep.getRecipientInfos();
400:
401:                assertEquals(ep.getEncryptionAlgOID(),
402:                        CMSEnvelopedDataGenerator.AES128_CBC);
403:
404:                Collection c = recipients.getRecipients();
405:                Iterator it = c.iterator();
406:
407:                while (it.hasNext()) {
408:                    RecipientInformation recipient = (RecipientInformation) it
409:                            .next();
410:
411:                    assertEquals(recipient.getKeyEncryptionAlgOID(),
412:                            PKCSObjectIdentifiers.rsaEncryption.getId());
413:
414:                    CMSTypedStream recData = recipient.getContentStream(_reciKP
415:                            .getPrivate(), "BC");
416:
417:                    assertEquals(true, Arrays.equals(data, CMSTestUtil
418:                            .streamToByteArray(recData.getContentStream())));
419:                }
420:
421:                ep.close();
422:            }
423:
424:            public void testKeyTransCAST5SunJCE() throws Exception {
425:                if (Security.getProvider("SunJCE") == null) {
426:                    return;
427:                }
428:
429:                String version = System.getProperty("java.version");
430:                if (version.startsWith("1.4") || version.startsWith("1.3")) {
431:                    return;
432:                }
433:
434:                byte[] data = "WallaWallaWashington".getBytes();
435:
436:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
437:
438:                edGen.addKeyTransRecipient(_reciCert);
439:
440:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
441:
442:                OutputStream out = edGen.open(bOut,
443:                        CMSEnvelopedDataGenerator.CAST5_CBC, "SunJCE");
444:
445:                out.write(data);
446:
447:                out.close();
448:
449:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(bOut
450:                        .toByteArray());
451:
452:                RecipientInformationStore recipients = ep.getRecipientInfos();
453:
454:                assertEquals(ep.getEncryptionAlgOID(),
455:                        CMSEnvelopedDataGenerator.CAST5_CBC);
456:
457:                Collection c = recipients.getRecipients();
458:                Iterator it = c.iterator();
459:
460:                while (it.hasNext()) {
461:                    RecipientInformation recipient = (RecipientInformation) it
462:                            .next();
463:
464:                    assertEquals(recipient.getKeyEncryptionAlgOID(),
465:                            PKCSObjectIdentifiers.rsaEncryption.getId());
466:
467:                    CMSTypedStream recData = recipient.getContentStream(_reciKP
468:                            .getPrivate(), "SunJCE");
469:
470:                    assertEquals(true, Arrays.equals(data, CMSTestUtil
471:                            .streamToByteArray(recData.getContentStream())));
472:                }
473:
474:                ep.close();
475:            }
476:
477:            public void testAESKEK() throws Exception {
478:                byte[] data = "WallaWallaWashington".getBytes();
479:                SecretKey kek = CMSTestUtil.makeAES192Key();
480:
481:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
482:
483:                byte[] kekId = new byte[] { 1, 2, 3, 4, 5 };
484:
485:                edGen.addKEKRecipient(kek, kekId);
486:
487:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
488:
489:                OutputStream out = edGen.open(bOut,
490:                        CMSEnvelopedDataGenerator.DES_EDE3_CBC, "BC");
491:                out.write(data);
492:
493:                out.close();
494:
495:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(bOut
496:                        .toByteArray());
497:
498:                RecipientInformationStore recipients = ep.getRecipientInfos();
499:
500:                assertEquals(ep.getEncryptionAlgOID(),
501:                        CMSEnvelopedDataGenerator.DES_EDE3_CBC);
502:
503:                Collection c = recipients.getRecipients();
504:                Iterator it = c.iterator();
505:
506:                while (it.hasNext()) {
507:                    RecipientInformation recipient = (RecipientInformation) it
508:                            .next();
509:
510:                    assertEquals(recipient.getKeyEncryptionAlgOID(),
511:                            "2.16.840.1.101.3.4.1.25");
512:
513:                    CMSTypedStream recData = recipient.getContentStream(kek,
514:                            "BC");
515:
516:                    assertEquals(true, Arrays.equals(data, CMSTestUtil
517:                            .streamToByteArray(recData.getContentStream())));
518:                }
519:
520:                ep.close();
521:            }
522:
523:            public void testTwoAESKEK() throws Exception {
524:                byte[] data = "WallaWallaWashington".getBytes();
525:                SecretKey kek1 = CMSTestUtil.makeAES192Key();
526:                SecretKey kek2 = CMSTestUtil.makeAES192Key();
527:
528:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
529:
530:                byte[] kekId1 = new byte[] { 1, 2, 3, 4, 5 };
531:                byte[] kekId2 = new byte[] { 5, 4, 3, 2, 1 };
532:
533:                edGen.addKEKRecipient(kek1, kekId1);
534:                edGen.addKEKRecipient(kek2, kekId2);
535:
536:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
537:
538:                OutputStream out = edGen.open(bOut,
539:                        CMSEnvelopedDataGenerator.DES_EDE3_CBC, "BC");
540:                out.write(data);
541:
542:                out.close();
543:
544:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(bOut
545:                        .toByteArray());
546:
547:                RecipientInformationStore recipients = ep.getRecipientInfos();
548:
549:                assertEquals(ep.getEncryptionAlgOID(),
550:                        CMSEnvelopedDataGenerator.DES_EDE3_CBC);
551:
552:                RecipientId recSel = new RecipientId();
553:
554:                recSel.setKeyIdentifier(kekId2);
555:
556:                RecipientInformation recipient = recipients.get(recSel);
557:
558:                assertEquals(recipient.getKeyEncryptionAlgOID(),
559:                        "2.16.840.1.101.3.4.1.25");
560:
561:                CMSTypedStream recData = recipient.getContentStream(kek2, "BC");
562:
563:                assertEquals(true, Arrays.equals(data, CMSTestUtil
564:                        .streamToByteArray(recData.getContentStream())));
565:
566:                ep.close();
567:            }
568:
569:            public void testECKeyAgree() throws Exception {
570:                byte[] data = Hex
571:                        .decode("504b492d4320434d5320456e76656c6f706564446174612053616d706c65");
572:
573:                CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
574:
575:                edGen.addKeyAgreementRecipient(
576:                        CMSEnvelopedDataGenerator.ECDH_SHA1KDF, _origEcKP
577:                                .getPrivate(), _origEcKP.getPublic(),
578:                        _reciEcCert, CMSEnvelopedDataGenerator.AES128_WRAP,
579:                        "BC");
580:
581:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
582:
583:                OutputStream out = edGen.open(bOut,
584:                        CMSEnvelopedDataGenerator.AES128_CBC, "BC");
585:                out.write(data);
586:
587:                out.close();
588:
589:                CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(bOut
590:                        .toByteArray());
591:
592:                RecipientInformationStore recipients = ep.getRecipientInfos();
593:
594:                assertEquals(ep.getEncryptionAlgOID(),
595:                        CMSEnvelopedDataGenerator.AES128_CBC);
596:
597:                RecipientId recSel = new RecipientId();
598:
599:                recSel.setIssuer(PrincipalUtil.getIssuerX509Principal(
600:                        _reciEcCert).getEncoded());
601:                recSel.setSerialNumber(_reciEcCert.getSerialNumber());
602:
603:                RecipientInformation recipient = recipients.get(recSel);
604:
605:                CMSTypedStream recData = recipient.getContentStream(_reciEcKP
606:                        .getPrivate(), "BC");
607:
608:                assertEquals(true, Arrays.equals(data, CMSTestUtil
609:                        .streamToByteArray(recData.getContentStream())));
610:
611:                ep.close();
612:            }
613:
614:            public void testOriginatorInfo() throws Exception {
615:                CMSEnvelopedDataParser env = new CMSEnvelopedDataParser(
616:                        CMSSampleMessages.originatorMessage);
617:
618:                RecipientInformationStore recipients = env.getRecipientInfos();
619:
620:                assertEquals(CMSEnvelopedDataGenerator.DES_EDE3_CBC, env
621:                        .getEncryptionAlgOID());
622:            }
623:
624:            public static Test suite() throws Exception {
625:                return new CMSTestSetup(new TestSuite(
626:                        EnvelopedDataStreamTest.class));
627:            }
628:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.