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


001:        package org.bouncycastle.jce;
002:
003:        import org.bouncycastle.asn1.ASN1EncodableVector;
004:        import org.bouncycastle.asn1.ASN1InputStream;
005:        import org.bouncycastle.asn1.ASN1Sequence;
006:        import org.bouncycastle.asn1.DERBitString;
007:        import org.bouncycastle.asn1.DERInteger;
008:        import org.bouncycastle.asn1.DERNull;
009:        import org.bouncycastle.asn1.DERObjectIdentifier;
010:        import org.bouncycastle.asn1.DEROutputStream;
011:        import org.bouncycastle.asn1.DERSequence;
012:        import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
013:        import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
014:        import org.bouncycastle.asn1.x509.TBSCertificateStructure;
015:        import org.bouncycastle.asn1.x509.Time;
016:        import org.bouncycastle.asn1.x509.V1TBSCertificateGenerator;
017:        import org.bouncycastle.asn1.x509.X509CertificateStructure;
018:        import org.bouncycastle.asn1.x509.X509Name;
019:        import org.bouncycastle.jce.provider.X509CertificateObject;
020:        import org.bouncycastle.util.Strings;
021:
022:        import java.io.ByteArrayInputStream;
023:        import java.io.ByteArrayOutputStream;
024:        import java.math.BigInteger;
025:        import java.security.InvalidKeyException;
026:        import java.security.NoSuchAlgorithmException;
027:        import java.security.NoSuchProviderException;
028:        import java.security.PrivateKey;
029:        import java.security.PublicKey;
030:        import java.security.SecureRandom;
031:        import java.security.Signature;
032:        import java.security.SignatureException;
033:        import java.security.cert.X509Certificate;
034:        import java.util.Date;
035:        import java.util.Hashtable;
036:
037:        /**
038:         * class to produce an X.509 Version 1 certificate.
039:         * 
040:         * @deprecated use the equivalent class in org.bouncycastle.x509
041:         */
042:        public class X509V1CertificateGenerator {
043:            private V1TBSCertificateGenerator tbsGen;
044:            private DERObjectIdentifier sigOID;
045:            private AlgorithmIdentifier sigAlgId;
046:            private String signatureAlgorithm;
047:
048:            private static Hashtable algorithms = new Hashtable();
049:
050:            static {
051:                algorithms.put("MD2WITHRSAENCRYPTION", new DERObjectIdentifier(
052:                        "1.2.840.113549.1.1.2"));
053:                algorithms.put("MD2WITHRSA", new DERObjectIdentifier(
054:                        "1.2.840.113549.1.1.2"));
055:                algorithms.put("MD5WITHRSAENCRYPTION", new DERObjectIdentifier(
056:                        "1.2.840.113549.1.1.4"));
057:                algorithms.put("MD5WITHRSA", new DERObjectIdentifier(
058:                        "1.2.840.113549.1.1.4"));
059:                algorithms.put("SHA1WITHRSAENCRYPTION",
060:                        new DERObjectIdentifier("1.2.840.113549.1.1.5"));
061:                algorithms.put("SHA1WITHRSA", new DERObjectIdentifier(
062:                        "1.2.840.113549.1.1.5"));
063:                algorithms.put("RIPEMD160WITHRSAENCRYPTION",
064:                        new DERObjectIdentifier("1.3.36.3.3.1.2"));
065:                algorithms.put("RIPEMD160WITHRSA", new DERObjectIdentifier(
066:                        "1.3.36.3.3.1.2"));
067:                algorithms.put("SHA1WITHDSA", new DERObjectIdentifier(
068:                        "1.2.840.10040.4.3"));
069:                algorithms.put("DSAWITHSHA1", new DERObjectIdentifier(
070:                        "1.2.840.10040.4.3"));
071:                algorithms.put("SHA1WITHECDSA", new DERObjectIdentifier(
072:                        "1.2.840.10045.4.1"));
073:                algorithms.put("ECDSAWITHSHA1", new DERObjectIdentifier(
074:                        "1.2.840.10045.4.1"));
075:            }
076:
077:            public X509V1CertificateGenerator() {
078:                tbsGen = new V1TBSCertificateGenerator();
079:            }
080:
081:            /**
082:             * reset the generator
083:             */
084:            public void reset() {
085:                tbsGen = new V1TBSCertificateGenerator();
086:            }
087:
088:            /**
089:             * set the serial number for the certificate.
090:             */
091:            public void setSerialNumber(BigInteger serialNumber) {
092:                tbsGen.setSerialNumber(new DERInteger(serialNumber));
093:            }
094:
095:            /**
096:             * Set the issuer distinguished name - the issuer is the entity whose private key is used to sign the
097:             * certificate.
098:             */
099:            public void setIssuerDN(X509Name issuer) {
100:                tbsGen.setIssuer(issuer);
101:            }
102:
103:            public void setNotBefore(Date date) {
104:                tbsGen.setStartDate(new Time(date));
105:            }
106:
107:            public void setNotAfter(Date date) {
108:                tbsGen.setEndDate(new Time(date));
109:            }
110:
111:            /**
112:             * Set the subject distinguished name. The subject describes the entity associated with the public key.
113:             */
114:            public void setSubjectDN(X509Name subject) {
115:                tbsGen.setSubject(subject);
116:            }
117:
118:            public void setPublicKey(PublicKey key) {
119:                try {
120:                    tbsGen.setSubjectPublicKeyInfo(new SubjectPublicKeyInfo(
121:                            (ASN1Sequence) new ASN1InputStream(
122:                                    new ByteArrayInputStream(key.getEncoded()))
123:                                    .readObject()));
124:                } catch (Exception e) {
125:                    throw new IllegalArgumentException(
126:                            "unable to process key - " + e.toString());
127:                }
128:            }
129:
130:            public void setSignatureAlgorithm(String signatureAlgorithm) {
131:                this .signatureAlgorithm = signatureAlgorithm;
132:
133:                sigOID = (DERObjectIdentifier) algorithms.get(Strings
134:                        .toUpperCase(signatureAlgorithm));
135:
136:                if (sigOID == null) {
137:                    throw new IllegalArgumentException(
138:                            "Unknown signature type requested");
139:                }
140:
141:                sigAlgId = new AlgorithmIdentifier(this .sigOID, new DERNull());
142:
143:                tbsGen.setSignature(sigAlgId);
144:            }
145:
146:            /**
147:             * generate an X509 certificate, based on the current issuer and subject
148:             * using the default provider "BC".
149:             */
150:            public X509Certificate generateX509Certificate(PrivateKey key)
151:                    throws SecurityException, SignatureException,
152:                    InvalidKeyException {
153:                try {
154:                    return generateX509Certificate(key, "BC", null);
155:                } catch (NoSuchProviderException e) {
156:                    throw new SecurityException("BC provider not installed!");
157:                }
158:            }
159:
160:            /**
161:             * generate an X509 certificate, based on the current issuer and subject
162:             * using the default provider "BC" and the passed in source of randomness
163:             */
164:            public X509Certificate generateX509Certificate(PrivateKey key,
165:                    SecureRandom random) throws SecurityException,
166:                    SignatureException, InvalidKeyException {
167:                try {
168:                    return generateX509Certificate(key, "BC", random);
169:                } catch (NoSuchProviderException e) {
170:                    throw new SecurityException("BC provider not installed!");
171:                }
172:            }
173:
174:            /**
175:             * generate an X509 certificate, based on the current issuer and subject,
176:             * using the passed in provider for the signing, and the passed in source
177:             * of randomness (if required).
178:             */
179:            public X509Certificate generateX509Certificate(PrivateKey key,
180:                    String provider) throws NoSuchProviderException,
181:                    SecurityException, SignatureException, InvalidKeyException {
182:                return generateX509Certificate(key, provider, null);
183:            }
184:
185:            /**
186:             * generate an X509 certificate, based on the current issuer and subject,
187:             * using the passed in provider for the signing, and the passed in source
188:             * of randomness (if required).
189:             */
190:            public X509Certificate generateX509Certificate(PrivateKey key,
191:                    String provider, SecureRandom random)
192:                    throws NoSuchProviderException, SecurityException,
193:                    SignatureException, InvalidKeyException {
194:                Signature sig = null;
195:
196:                try {
197:                    sig = Signature.getInstance(sigOID.getId(), provider);
198:                } catch (NoSuchAlgorithmException ex) {
199:                    try {
200:                        sig = Signature.getInstance(signatureAlgorithm,
201:                                provider);
202:                    } catch (NoSuchAlgorithmException e) {
203:                        throw new SecurityException(
204:                                "exception creating signature: " + e.toString());
205:                    }
206:                }
207:
208:                if (random != null) {
209:                    sig.initSign(key, random);
210:                } else {
211:                    sig.initSign(key);
212:                }
213:
214:                TBSCertificateStructure tbsCert = tbsGen
215:                        .generateTBSCertificate();
216:
217:                try {
218:                    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
219:                    DEROutputStream dOut = new DEROutputStream(bOut);
220:
221:                    dOut.writeObject(tbsCert);
222:
223:                    sig.update(bOut.toByteArray());
224:
225:                    ASN1EncodableVector v = new ASN1EncodableVector();
226:
227:                    v.add(tbsCert);
228:                    v.add(sigAlgId);
229:                    v.add(new DERBitString(sig.sign()));
230:
231:                    return new X509CertificateObject(
232:                            new X509CertificateStructure(new DERSequence(v)));
233:                } catch (Exception e) {
234:                    throw new SecurityException(
235:                            "exception encoding TBS cert - " + e);
236:                }
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.