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: }
|