Source Code Cross Referenced for CertPathTest.java in  » Security » Bouncy-Castle » org » bouncycastle » jce » provider » 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.jce.provider.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.jce.provider.test;
002:
003:        import org.bouncycastle.jce.provider.BouncyCastleProvider;
004:        import org.bouncycastle.util.encoders.Base64;
005:        import org.bouncycastle.util.encoders.Hex;
006:        import org.bouncycastle.util.test.SimpleTest;
007:
008:        import java.io.ByteArrayInputStream;
009:        import java.io.ByteArrayOutputStream;
010:        import java.io.ObjectOutputStream;
011:        import java.security.InvalidKeyException;
012:        import java.security.NoSuchAlgorithmException;
013:        import java.security.NoSuchProviderException;
014:        import java.security.PublicKey;
015:        import java.security.Security;
016:        import java.security.SignatureException;
017:        import java.security.cert.CertPath;
018:        import java.security.cert.CertPathBuilder;
019:        import java.security.cert.CertPathBuilderException;
020:        import java.security.cert.CertPathBuilderResult;
021:        import java.security.cert.CertStore;
022:        import java.security.cert.Certificate;
023:        import java.security.cert.CertificateEncodingException;
024:        import java.security.cert.CertificateException;
025:        import java.security.cert.CertificateFactory;
026:        import java.security.cert.CollectionCertStoreParameters;
027:        import java.security.cert.PKIXBuilderParameters;
028:        import java.security.cert.TrustAnchor;
029:        import java.security.cert.X509CertSelector;
030:        import java.security.cert.X509Certificate;
031:        import java.util.ArrayList;
032:        import java.util.Collections;
033:        import java.util.HashSet;
034:        import java.util.Iterator;
035:        import java.util.List;
036:        import java.util.Set;
037:        import java.util.Vector;
038:
039:        public class CertPathTest extends SimpleTest {
040:            static byte[] rootCertBin = Hex
041:                    .decode("3082023c308201a5a003020102020101300d06092a864886f70d0101040500305c310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531233021060355040b131a426f756e6379205072696d617279204365727469666963617465301e170d3032303132323133353230385a170d3032303332333133353230385a305c310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531233021060355040b131a426f756e6379205072696d61727920436572746966696361746530819d300d06092a864886f70d010101050003818b0030818702818100b259d2d6e627a768c94be36164c2d9fc79d97aab9253140e5bf17751197731d6f7540d2509e7b9ffee0a70a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf3c298e05842535d9f064e6b0391cb7d306e0a2d20c4dfb4e7b49a9640bdea26c10ad69c3f05007ce2513cee44cfe01998e62b6c3637d3fc0391079b26ee36d5020111a310300e300c0603551d13040530030101ff300d06092a864886f70d0101040500038181002584a067f9d3e9a02efcf33d9fb870176311ad7741551397a3717cfa71f8724907bdfe9846d25205c9241631df9c0dabd5a980ccdb69fdfcad3694fbe6939f7dffd730d67242400b6fcc9aa718e87f1d7ea58832e4f47d253c7843cc6f4c0a206fb141b959ff639b986cc3470bd576f176cf4d4f402b549ec14e90349b8fb8f5");
042:            static byte[] interCertBin = Hex
043:                    .decode("308202fe30820267a003020102020102300d06092a864886f70d0101040500305c310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531233021060355040b131a426f756e6379205072696d617279204365727469666963617465301e170d3032303132323133353230395a170d3032303332333133353230395a3061310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531283026060355040b131f426f756e637920496e7465726d65646961746520436572746966696361746530819f300d06092a864886f70d010101050003818d00308189028181008de0d113c5e736969c8d2b047a243f8fe18edad64cde9e842d3669230ca486f7cfdde1f8eec54d1905fff04acc85e61093e180cadc6cea407f193d44bb0e9449b8dbb49784cd9e36260c39e06a947299978c6ed8300724e887198cfede20f3fbde658fa2bd078be946a392bd349f2b49c486e20c405588e306706c9017308e69020300ffffa381ca3081c7301d0603551d0e041604149408336f3240f78737dad120aaed2ea76ec9c91e3081840603551d23047d307b8014c0361907adc48897a85e726f6b09ebe5e6f1295ca160a45e305c310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531233021060355040b131a426f756e6379205072696d617279204365727469666963617465820101300c0603551d13040530030101ff301106096086480186f8420101040403020060300d06092a864886f70d010104050003818100a06b166b48c82ba1f81c8f142c14974050266f7b9d003e39e24e53d6f82ce43f4099937aa69b818a5193c5a842521cdb59a44b8837c2caddea70d8e013d6c9fd5e572010ee5cc6894c91783af13909eb53bd79d3c9bf6e268b0c13c41c6b16365287975683ece8a4dad9c8394faf707a00348ed01ac59287734411af4e878486");
044:            static byte[] finalCertBin = Hex
045:                    .decode("30820259308201c2a003020102020103300d06092a864886f70d01010405003061310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531283026060355040b131f426f756e637920496e7465726d656469617465204365727469666963617465301e170d3032303132323133353230395a170d3032303332333133353230395a3065310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531123010060355040713094d656c626f75726e65311830160603550403130f4572696320482e2045636869646e61305a300d06092a864886f70d01010105000349003046024100b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7020111a3633061301d0603551d0e04160414d06cec6d3583bc55121b0ccb3efed726a6166468301f0603551d230418301680149408336f3240f78737dad120aaed2ea76ec9c91e300c0603551d1304053003010100301106096086480186f8420101040403020001300d06092a864886f70d010104050003818100135db1857d0bb8bf108ce4df2cba4d1cf9e4a4578c0197b4da4e6ddd4c62d25debc5ed0916341aa577caa8eebf21409f065bb94369e3f006536a0a715c429c5888504b84030a181c88cb72fc99c11571d3171f869865cee722af474b5279df9ccd6ec3b04bf0fae272ca15266b74a5ce2d14548a0c76a07b4f97dbc25ed7d0ef");
046:            static byte[] rootCrlBin = Hex
047:                    .decode("3082012430818e020101300d06092a864886f70d0101050500305c310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531233021060355040b131a426f756e6379205072696d617279204365727469666963617465170d3032303132323133353230395a170d3032303332333133353230395a300d06092a864886f70d0101050500038181001255a218c620add68a7a8a561f331d2d510b42515c53f3701f2f49946ff2513a0c6e8e606e3488679f8354dc06a79a84c5233c9c9c9f746bbf4d19e49e730850b3bb7e672d59200d3da12512a91f7bc6f56036250789860ade5b0859a2a8fd24904b271624a544c8e894f293bb0f7018679e3499bf06548618ba473b7852a577");
048:            static byte[] interCrlBin = Hex
049:                    .decode("30820129308193020101300d06092a864886f70d01010505003061310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531283026060355040b131f426f756e637920496e7465726d656469617465204365727469666963617465170d3032303132323133353230395a170d3032303332333133353230395a300d06092a864886f70d01010505000381810046e2743d2faa0a3ed3555fc860a6fed78da96ce967c0db6ec8f40de95ec8cab9c720698d705f1cd8a75a400c0b15f23751cdfd5491abb9d416f0585f425e6802a3612a30cecd593abdcd15c632e0a4e2a7a3049649138ae0367431dd626d079c13c1449058547d796f53660acd5b432e7dacf31315ed3c21eb8948a7c043f418");
050:
051:            /*
052:             * certpath with a circular reference
053:             */
054:            static byte[] certA = Base64
055:                    .decode("MIIC6jCCAlOgAwIBAgIBBTANBgkqhkiG9w0BAQUFADCBjTEPMA0GA1UEAxMGSW50"
056:                            + "ZXIzMQswCQYDVQQGEwJDSDEPMA0GA1UEBxMGWnVyaWNoMQswCQYDVQQIEwJaSDEX"
057:                            + "MBUGA1UEChMOUHJpdmFzcGhlcmUgQUcxEDAOBgNVBAsTB1Rlc3RpbmcxJDAiBgkq"
058:                            + "hkiG9w0BCQEWFWFybWluQHByaXZhc3BoZXJlLmNvbTAeFw0wNzA0MDIwODQ2NTda"
059:                            + "Fw0xNzAzMzAwODQ0MDBaMIGlMScwJQYDVQQDHh4AQQByAG0AaQBuACAASADkAGIA"
060:                            + "ZQByAGwAaQBuAGcxCzAJBgNVBAYTAkNIMQ8wDQYDVQQHEwZadXJpY2gxCzAJBgNV"
061:                            + "BAgTAlpIMRcwFQYDVQQKEw5Qcml2YXNwaGVyZSBBRzEQMA4GA1UECxMHVGVzdGlu"
062:                            + "ZzEkMCIGCSqGSIb3DQEJARYVYXJtaW5AcHJpdmFzcGhlcmUuY29tMIGfMA0GCSqG"
063:                            + "SIb3DQEBAQUAA4GNADCBiQKBgQCfHfyVs5dbxG35H/Thd29qR4NZU88taCu/OWA1"
064:                            + "GdACI02lXWYpmLWiDgnU0ULP+GG8OnVp1IES9fz2zcrXKQ19xZzsen/To3h5sNte"
065:                            + "cJpS00XMM24q/jDwy5NvkBP9YIfFKQ1E/0hFHXcqwlw+b/y/v6YGsZCU2h6QDzc4"
066:                            + "5m0+BwIDAQABo0AwPjAMBgNVHRMBAf8EAjAAMA4GA1UdDwEB/wQEAwIE8DAeBglg"
067:                            + "hkgBhvhCAQ0EERYPeGNhIGNlcnRpZmljYXRlMA0GCSqGSIb3DQEBBQUAA4GBAJEu"
068:                            + "KiSfIwsY7SfobMLrv2v/BtLhGLi4RnmjiwzBhuv5rn4rRfBpq1ppmqQMJ2pmA67v"
069:                            + "UWCY+mNwuyjHyivpCCyJGsZ9d5H09g2vqxzkDBMz7X9VNMZYFH8j/R3/Cfvqks31"
070:                            + "z0OFslJkeKLa1I0P/dfVHsRKNkLRT3Ws5LKksErQ");
071:
072:            static byte[] certB = Base64
073:                    .decode("MIICtTCCAh6gAwIBAgIBBDANBgkqhkiG9w0BAQQFADCBjTEPMA0GA1UEAxMGSW50"
074:                            + "ZXIyMQswCQYDVQQGEwJDSDEPMA0GA1UEBxMGWnVyaWNoMQswCQYDVQQIEwJaSDEX"
075:                            + "MBUGA1UEChMOUHJpdmFzcGhlcmUgQUcxEDAOBgNVBAsTB1Rlc3RpbmcxJDAiBgkq"
076:                            + "hkiG9w0BCQEWFWFybWluQHByaXZhc3BoZXJlLmNvbTAeFw0wNzA0MDIwODQ2Mzha"
077:                            + "Fw0xNzAzMzAwODQ0MDBaMIGNMQ8wDQYDVQQDEwZJbnRlcjMxCzAJBgNVBAYTAkNI"
078:                            + "MQ8wDQYDVQQHEwZadXJpY2gxCzAJBgNVBAgTAlpIMRcwFQYDVQQKEw5Qcml2YXNw"
079:                            + "aGVyZSBBRzEQMA4GA1UECxMHVGVzdGluZzEkMCIGCSqGSIb3DQEJARYVYXJtaW5A"
080:                            + "cHJpdmFzcGhlcmUuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxCXIB"
081:                            + "QRnmVvl2h7Q+0SsRxDLnyM1dJG9jMa+UCCmHy0k/ZHs5VirSbjEJSjkQ9BGeh9SC"
082:                            + "7JwbMpXO7UE+gcVc2RnWUY+MA+fWIeTV4KtkYA8WPu8wVGCXbN8wwh/StOocszxb"
083:                            + "g+iLvGeh8CYSRqg6QN3S/02etH3o8H4e7Z0PZwIDAQABoyMwITAPBgNVHRMBAf8E"
084:                            + "BTADAQH/MA4GA1UdDwEB/wQEAwIB9jANBgkqhkiG9w0BAQQFAAOBgQCtWdirSsmt"
085:                            + "+CBBCNn6ZnbU3QqQfiiQIomjenNEHESJgaS/+PvPE5i3xWFXsunTHLW321/Km16I"
086:                            + "7+ZvT8Su1cqHg79NAT8QB0yke1saKSy2C0Pic4HwrNqVBWFNSxMU0hQzpx/ZXDbZ"
087:                            + "DqIXAp5EfyRYBy2ul+jm6Rot6aFgzuopKg==");
088:
089:            static byte[] certC = Base64
090:                    .decode("MIICtTCCAh6gAwIBAgIBAjANBgkqhkiG9w0BAQQFADCBjTEPMA0GA1UEAxMGSW50"
091:                            + "ZXIxMQswCQYDVQQGEwJDSDEPMA0GA1UEBxMGWnVyaWNoMQswCQYDVQQIEwJaSDEX"
092:                            + "MBUGA1UEChMOUHJpdmFzcGhlcmUgQUcxEDAOBgNVBAsTB1Rlc3RpbmcxJDAiBgkq"
093:                            + "hkiG9w0BCQEWFWFybWluQHByaXZhc3BoZXJlLmNvbTAeFw0wNzA0MDIwODQ0Mzla"
094:                            + "Fw0xNzAzMzAwODQ0MDBaMIGNMQ8wDQYDVQQDEwZJbnRlcjIxCzAJBgNVBAYTAkNI"
095:                            + "MQ8wDQYDVQQHEwZadXJpY2gxCzAJBgNVBAgTAlpIMRcwFQYDVQQKEw5Qcml2YXNw"
096:                            + "aGVyZSBBRzEQMA4GA1UECxMHVGVzdGluZzEkMCIGCSqGSIb3DQEJARYVYXJtaW5A"
097:                            + "cHJpdmFzcGhlcmUuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0rLr6"
098:                            + "f2/ONeJzTb0q9M/NNX+MnAFMSqiQGVBkT76u5nOH4KLkpHXkzI82JI7GuQMzoT3a"
099:                            + "+RP1hO6FneO92ms2soC6xiOFb4EC69Dfhh87Nww5O35JxVF0bzmbmIAWd6P/7zGh"
100:                            + "nd2S4tKkaZcubps+C0j9Fgi0hipVicAOUVVoDQIDAQABoyMwITAPBgNVHRMBAf8E"
101:                            + "BTADAQH/MA4GA1UdDwEB/wQEAwIB9jANBgkqhkiG9w0BAQQFAAOBgQCLPvc1IMA4"
102:                            + "YP+PmnEldyUoRWRnvPWjBGeu0WheBP7fdcnGBf93Nmc5j68ZN+eTZ5VMuZ99YdvH"
103:                            + "CXGNX6oodONLU//LlFKdLl5xjLAS5X9p1RbOEGytnalqeiEpjk4+C/7rIBG1kllO"
104:                            + "dItmI6LlEMV09Hkpg6ZRAUmRkb8KrM4X7A==");
105:
106:            static byte[] certD = Base64
107:                    .decode("MIICtTCCAh6gAwIBAgIBBjANBgkqhkiG9w0BAQQFADCBjTEPMA0GA1UEAxMGSW50"
108:                            + "ZXIzMQswCQYDVQQGEwJDSDEPMA0GA1UEBxMGWnVyaWNoMQswCQYDVQQIEwJaSDEX"
109:                            + "MBUGA1UEChMOUHJpdmFzcGhlcmUgQUcxEDAOBgNVBAsTB1Rlc3RpbmcxJDAiBgkq"
110:                            + "hkiG9w0BCQEWFWFybWluQHByaXZhc3BoZXJlLmNvbTAeFw0wNzA0MDIwODQ5NTNa"
111:                            + "Fw0xNzAzMzAwODQ0MDBaMIGNMQ8wDQYDVQQDEwZJbnRlcjExCzAJBgNVBAYTAkNI"
112:                            + "MQ8wDQYDVQQHEwZadXJpY2gxCzAJBgNVBAgTAlpIMRcwFQYDVQQKEw5Qcml2YXNw"
113:                            + "aGVyZSBBRzEQMA4GA1UECxMHVGVzdGluZzEkMCIGCSqGSIb3DQEJARYVYXJtaW5A"
114:                            + "cHJpdmFzcGhlcmUuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCae3TP"
115:                            + "jIVKeASqvNabaiUHAMGUgFxB7L0yUsIj39azLcLtUj4S7XkDf7SMGtYV0JY1XNaQ"
116:                            + "sHJAsnJivDZc50oiYvqDYfgFZx5+AsN5l5X5rjRzs/OX+Jo+k1OgsIyu6+mf9Kfb"
117:                            + "5IdWOVB2EcOg4f9tPjLM8CIj9Pp7RbKLyqUUgwIDAQABoyMwITAPBgNVHRMBAf8E"
118:                            + "BTADAQH/MA4GA1UdDwEB/wQEAwIB9jANBgkqhkiG9w0BAQQFAAOBgQCgr9kUdWUT"
119:                            + "Lt9UcztSzR3pnHRsyvS0E/z850OKQKS5/VxLEalpFvhj+3EcZ7Y6mFxaaS2B7vXg"
120:                            + "2YWyqV1PRb6iF7/u9EXkpSTKGrJahwANirCa3V/HTUuPdCE2GITlnWI8h3eVA+xQ"
121:                            + "D4LF0PXHOkXbwmhXRSb10lW1bSGkUxE9jg==");
122:
123:            private void testExceptions() throws Exception {
124:                byte[] enc = { (byte) 0, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
125:                MyCertPath mc = new MyCertPath(enc);
126:                ByteArrayOutputStream os = new ByteArrayOutputStream();
127:                ByteArrayInputStream is;
128:                byte[] arr;
129:
130:                ObjectOutputStream oOut = new ObjectOutputStream(os);
131:                oOut.writeObject(mc);
132:                oOut.flush();
133:                oOut.close();
134:
135:                try {
136:                    CertificateFactory cFac = CertificateFactory.getInstance(
137:                            "X.509", "BC");
138:                    arr = os.toByteArray();
139:                    is = new ByteArrayInputStream(arr);
140:                    cFac.generateCertPath(is);
141:                } catch (CertificateException e) {
142:                    // ignore okay
143:                }
144:
145:                CertificateFactory cf = CertificateFactory.getInstance("X.509");
146:                List certCol = new ArrayList();
147:
148:                certCol.add(cf.generateCertificate(new ByteArrayInputStream(
149:                        certA)));
150:                certCol.add(cf.generateCertificate(new ByteArrayInputStream(
151:                        certB)));
152:                certCol.add(cf.generateCertificate(new ByteArrayInputStream(
153:                        certC)));
154:                certCol.add(cf.generateCertificate(new ByteArrayInputStream(
155:                        certD)));
156:
157:                CertPathBuilder pathBuilder = CertPathBuilder.getInstance(
158:                        "PKIX", "BC");
159:                X509CertSelector select = new X509CertSelector();
160:                select.setSubject(((X509Certificate) certCol.get(0))
161:                        .getSubjectX500Principal().getEncoded());
162:
163:                Set trustanchors = new HashSet();
164:                trustanchors.add(new TrustAnchor((X509Certificate) cf
165:                        .generateCertificate(new ByteArrayInputStream(
166:                                rootCertBin)), null));
167:
168:                CertStore certStore = CertStore.getInstance("Collection",
169:                        new CollectionCertStoreParameters(certCol));
170:
171:                PKIXBuilderParameters params = new PKIXBuilderParameters(
172:                        trustanchors, select);
173:                params.addCertStore(certStore);
174:
175:                try {
176:                    CertPathBuilderResult result = pathBuilder.build(params);
177:                    CertPath path = result.getCertPath();
178:                    fail("found cert path in circular set");
179:                } catch (CertPathBuilderException e) {
180:                    // expected
181:                }
182:            }
183:
184:            public void performTest() throws Exception {
185:                CertificateFactory cf = CertificateFactory.getInstance("X.509",
186:                        "BC");
187:
188:                X509Certificate rootCert = (X509Certificate) cf
189:                        .generateCertificate(new ByteArrayInputStream(
190:                                rootCertBin));
191:                X509Certificate interCert = (X509Certificate) cf
192:                        .generateCertificate(new ByteArrayInputStream(
193:                                interCertBin));
194:                X509Certificate finalCert = (X509Certificate) cf
195:                        .generateCertificate(new ByteArrayInputStream(
196:                                finalCertBin));
197:
198:                //Testing CertPath generation from List
199:                List list = new ArrayList();
200:                list.add(interCert);
201:                CertPath certPath1 = cf.generateCertPath(list);
202:
203:                //Testing CertPath encoding as PkiPath
204:                byte[] encoded = certPath1.getEncoded("PkiPath");
205:
206:                //Testing CertPath generation from InputStream
207:                ByteArrayInputStream inStream = new ByteArrayInputStream(
208:                        encoded);
209:                CertPath certPath2 = cf.generateCertPath(inStream, "PkiPath");
210:
211:                //Comparing both CertPathes
212:                if (!certPath2.equals(certPath1)) {
213:                    fail("CertPath differ after encoding and decoding.");
214:                }
215:
216:                encoded = certPath1.getEncoded("PKCS7");
217:
218:                //Testing CertPath generation from InputStream
219:                inStream = new ByteArrayInputStream(encoded);
220:                certPath2 = cf.generateCertPath(inStream, "PKCS7");
221:
222:                //Comparing both CertPathes
223:                if (!certPath2.equals(certPath1)) {
224:                    fail("CertPath differ after encoding and decoding.");
225:                }
226:
227:                encoded = certPath1.getEncoded("PEM");
228:
229:                //Testing CertPath generation from InputStream
230:                inStream = new ByteArrayInputStream(encoded);
231:                certPath2 = cf.generateCertPath(inStream, "PEM");
232:
233:                //Comparing both CertPathes
234:                if (!certPath2.equals(certPath1)) {
235:                    fail("CertPath differ after encoding and decoding.");
236:                }
237:
238:                //
239:                // empty list test
240:                //
241:                list = new ArrayList();
242:
243:                CertPath certPath = CertificateFactory.getInstance("X.509",
244:                        "BC").generateCertPath(list);
245:                if (certPath.getCertificates().size() != 0) {
246:                    fail("list wrong size.");
247:                }
248:
249:                //
250:                // exception tests
251:                //
252:                testExceptions();
253:            }
254:
255:            public String getName() {
256:                return "CertPath";
257:            }
258:
259:            public static void main(String[] args) {
260:                Security.addProvider(new BouncyCastleProvider());
261:
262:                runTest(new CertPathTest());
263:            }
264:
265:            private static class MyCertificate extends Certificate {
266:                private final byte[] encoding;
267:
268:                public MyCertificate(String type, byte[] encoding) {
269:                    super (type);
270:                    // don't copy to allow null parameter in test
271:                    this .encoding = encoding;
272:                }
273:
274:                public byte[] getEncoded() throws CertificateEncodingException {
275:                    // do copy to force NPE in test
276:                    return (byte[]) encoding.clone();
277:                }
278:
279:                public void verify(PublicKey key) throws CertificateException,
280:                        NoSuchAlgorithmException, InvalidKeyException,
281:                        NoSuchProviderException, SignatureException {
282:                }
283:
284:                public void verify(PublicKey key, String sigProvider)
285:                        throws CertificateException, NoSuchAlgorithmException,
286:                        InvalidKeyException, NoSuchProviderException,
287:                        SignatureException {
288:                }
289:
290:                public String toString() {
291:                    return "[My test Certificate, type: " + getType() + "]";
292:                }
293:
294:                public PublicKey getPublicKey() {
295:                    return new PublicKey() {
296:                        public String getAlgorithm() {
297:                            return "TEST";
298:                        }
299:
300:                        public byte[] getEncoded() {
301:                            return new byte[] { (byte) 1, (byte) 2, (byte) 3 };
302:                        }
303:
304:                        public String getFormat() {
305:                            return "TEST_FORMAT";
306:                        }
307:                    };
308:                }
309:            }
310:
311:            private static class MyCertPath extends CertPath {
312:                private final Vector certificates;
313:
314:                private final Vector encodingNames;
315:
316:                private final byte[] encoding;
317:
318:                public MyCertPath(byte[] encoding) {
319:                    super ("MyEncoding");
320:                    this .encoding = encoding;
321:                    certificates = new Vector();
322:                    certificates.add(new MyCertificate("MyEncoding", encoding));
323:                    encodingNames = new Vector();
324:                    encodingNames.add("MyEncoding");
325:                }
326:
327:                public List getCertificates() {
328:                    return Collections.unmodifiableList(certificates);
329:                }
330:
331:                public byte[] getEncoded() throws CertificateEncodingException {
332:                    return (byte[]) encoding.clone();
333:                }
334:
335:                public byte[] getEncoded(String encoding)
336:                        throws CertificateEncodingException {
337:                    if (getType().equals(encoding)) {
338:                        return (byte[]) this .encoding.clone();
339:                    }
340:                    throw new CertificateEncodingException(
341:                            "Encoding not supported: " + encoding);
342:                }
343:
344:                public Iterator getEncodings() {
345:                    return Collections.unmodifiableCollection(encodingNames)
346:                            .iterator();
347:                }
348:            }
349:
350:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.