001: // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
002: //
003: // Author: Maik Stohn
004: //
005: // Permission is hereby granted, free of charge, to any person obtaining a copy of this
006: // software and associated documentation files (the "Software"), to deal in the Software
007: // without restriction, including without limitation the rights to use, copy, modify, merge,
008: // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
009: // to whom the Software is furnished to do so, subject to the following conditions:
010: //
011: // The above copyright notice and this permission notice shall be included in all copies or
012: // substantial portions of the Software.
013: //
014: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
015: // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
016: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
017: // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
018: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
019:
020: package com.novosec.pkix.asn1.crmf;
021:
022: import java.util.Enumeration;
023:
024: import org.bouncycastle.asn1.ASN1EncodableVector;
025: import org.bouncycastle.asn1.ASN1Sequence;
026: import org.bouncycastle.asn1.ASN1TaggedObject;
027: import org.bouncycastle.asn1.DERBitString;
028: import org.bouncycastle.asn1.DEREncodable;
029: import org.bouncycastle.asn1.DERInteger;
030: import org.bouncycastle.asn1.DERObject;
031: import org.bouncycastle.asn1.DERSequence;
032: import org.bouncycastle.asn1.DERTaggedObject;
033: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
034: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
035: import org.bouncycastle.asn1.x509.X509Extensions;
036: import org.bouncycastle.asn1.x509.X509Name;
037:
038: /**
039: * ASN.1 structure DER En/DeCoder.
040: *
041: * <pre>
042: * CertTemplate ::= SEQUENCE {
043: * version [0] Version OPTIONAL,
044: * serialNumber [1] INTEGER OPTIONAL,
045: * signingAlg [2] AlgorithmIdentifier OPTIONAL,
046: * issuer [3] Name OPTIONAL,
047: * validity [4] OptionalValidity OPTIONAL,
048: * subject [5] Name OPTIONAL,
049: * publicKey [6] SubjectPublicKeyInfo OPTIONAL,
050: * issuerUID [7] UniqueIdentifier OPTIONAL,
051: * subjectUID [8] UniqueIdentifier OPTIONAL,
052: * extensions [9] Extensions OPTIONAL }
053: *
054: * </pre>
055: */
056: public class CertTemplate implements DEREncodable {
057: // name is not a choice type --> tag it implicit...no but it should be explicit?
058: // Change to explicit by PrimeKey Solutions AB, Tomas Gustavsson
059: public static final boolean bNameIsExplicit = true;
060:
061: private DERInteger version = null;
062: private DERInteger serialNumber = null;
063: private AlgorithmIdentifier signingAlg = null;
064: private X509Name issuer = null;
065: private OptionalValidity validity = null;
066: private X509Name subject = null;
067: private SubjectPublicKeyInfo publicKey = null;
068: private DERBitString issuerUID = null;
069: private DERBitString subjectUID = null;
070: private X509Extensions extensions = null;
071:
072: public static CertTemplate getInstance(ASN1TaggedObject obj,
073: boolean explicit) {
074: return getInstance(ASN1Sequence.getInstance(obj, explicit));
075: }
076:
077: public static CertTemplate getInstance(Object obj) {
078: if (obj == null) {
079: return new CertTemplate();
080: } else if (obj instanceof CertTemplate) {
081: return (CertTemplate) obj;
082: } else if (obj instanceof ASN1Sequence) {
083: return new CertTemplate((ASN1Sequence) obj);
084: } else {
085: throw new IllegalArgumentException(
086: "unknown object in factory");
087: }
088: }
089:
090: public CertTemplate(ASN1Sequence seq) {
091: Enumeration e = (seq == null ? null : seq.getObjects());
092: while (e != null && e.hasMoreElements()) {
093: DERTaggedObject obj = (DERTaggedObject) e.nextElement();
094: int tagno = (obj == null ? -1 : obj.getTagNo());
095: switch (tagno) {
096: case 0:
097: this .version = DERInteger.getInstance(obj, false);
098: break;
099: case 1:
100: this .serialNumber = DERInteger.getInstance(obj, false);
101: break;
102: case 2:
103: this .signingAlg = AlgorithmIdentifier.getInstance(obj,
104: false);
105: break;
106: case 3:
107: this .issuer = X509Name
108: .getInstance(obj, bNameIsExplicit);
109: break;
110: case 4:
111: this .validity = OptionalValidity
112: .getInstance(obj, false);
113: break;
114: case 5:
115: this .subject = X509Name.getInstance(obj,
116: bNameIsExplicit);
117: break;
118: case 6:
119: this .publicKey = SubjectPublicKeyInfo.getInstance(obj,
120: false);
121: break;
122: case 7:
123: this .issuerUID = DERBitString.getInstance(obj, false);
124: break;
125: case 8:
126: this .subjectUID = DERBitString.getInstance(obj, false);
127: break;
128: case 9:
129: this .extensions = X509Extensions
130: .getInstance(obj, false);
131: break;
132: default:
133: throw new IllegalArgumentException(
134: "invalid asn1 sequence");
135: }
136: }
137: }
138:
139: public CertTemplate() {
140: }
141:
142: public DERInteger getVersion() {
143: return version;
144: }
145:
146: public void setVersion(DERInteger version) {
147: this .version = version;
148: }
149:
150: public DERInteger getSerialNumber() {
151: return serialNumber;
152: }
153:
154: public void setSerialNumber(DERInteger serialNumber) {
155: this .serialNumber = serialNumber;
156: }
157:
158: public AlgorithmIdentifier getSigningAlg() {
159: return signingAlg;
160: }
161:
162: public void setSigningAlg(AlgorithmIdentifier signingAlg) {
163: this .signingAlg = signingAlg;
164: }
165:
166: public X509Name getIssuer() {
167: return issuer;
168: }
169:
170: public void setIssuer(X509Name issuer) {
171: this .issuer = issuer;
172: }
173:
174: public OptionalValidity getValidity() {
175: return validity;
176: }
177:
178: public void setValidity(OptionalValidity validity) {
179: this .validity = validity;
180: }
181:
182: public X509Name getSubject() {
183: return subject;
184: }
185:
186: public void setSubject(X509Name subject) {
187: this .subject = subject;
188: }
189:
190: public SubjectPublicKeyInfo getPublicKey() {
191: return publicKey;
192: }
193:
194: public void setPublicKey(SubjectPublicKeyInfo publicKey) {
195: this .publicKey = publicKey;
196: }
197:
198: public DERBitString getIssuerUID() {
199: return issuerUID;
200: }
201:
202: public void setIssuerUID(DERBitString issuerUID) {
203: this .issuerUID = issuerUID;
204: }
205:
206: public DERBitString getSubjectUID() {
207: return subjectUID;
208: }
209:
210: public void setSubjectUID(DERBitString subjectUID) {
211: this .subjectUID = subjectUID;
212: }
213:
214: public X509Extensions getExtensions() {
215: return extensions;
216: }
217:
218: public void setExtensions(X509Extensions extensions) {
219: this .extensions = extensions;
220: }
221:
222: public DERObject getDERObject() {
223: ASN1EncodableVector v = new ASN1EncodableVector();
224:
225: if (version != null)
226: v.add(new DERTaggedObject(false, 0, version));
227: if (serialNumber != null)
228: v.add(new DERTaggedObject(false, 1, serialNumber));
229: if (signingAlg != null)
230: v.add(new DERTaggedObject(false, 2, signingAlg));
231: if (issuer != null)
232: v.add(new DERTaggedObject(bNameIsExplicit, 3, issuer));
233: if (validity != null)
234: v.add(new DERTaggedObject(false, 4, validity));
235: if (subject != null)
236: v.add(new DERTaggedObject(bNameIsExplicit, 5, subject));
237: if (publicKey != null)
238: v.add(new DERTaggedObject(false, 6, publicKey));
239: if (issuerUID != null)
240: v.add(new DERTaggedObject(false, 7, issuerUID));
241: if (subjectUID != null)
242: v.add(new DERTaggedObject(false, 8, subjectUID));
243: if (extensions != null)
244: v.add(new DERTaggedObject(false, 9, extensions));
245:
246: return new DERSequence(v);
247: }
248:
249: public String toString() {
250: StringBuffer sb = new StringBuffer(this .getClass().getName());
251: sb.append(" (");
252:
253: if (this .getVersion() != null)
254: sb.append("version: " + this .getVersion() + ", ");
255:
256: if (this .getSerialNumber() != null)
257: sb.append("serialNumber: " + this .getSerialNumber() + ", ");
258:
259: if (this .getSigningAlg() != null)
260: sb.append("signingAlg: " + this .getSigningAlg() + ", ");
261:
262: if (this .getIssuer() != null)
263: sb.append("issuer: " + this .getIssuer() + ", ");
264:
265: if (this .getValidity() != null)
266: sb.append("validity: " + this .getValidity() + ", ");
267:
268: if (this .getSubject() != null)
269: sb.append("subject: " + this .getSubject() + ", ");
270:
271: if (this .getPublicKey() != null)
272: sb.append("publicKey: " + this .getPublicKey() + ", ");
273:
274: if (this .getIssuerUID() != null)
275: sb.append("issuerUID: " + this .getIssuerUID() + ", ");
276:
277: if (this .getSubjectUID() != null)
278: sb.append("subjectUID: " + this .getSubjectUID() + ", ");
279:
280: if (this .getExtensions() != null)
281: sb.append("extensions: " + this .getExtensions() + ", ");
282:
283: sb.append("hashCode: " + Integer.toHexString(this .hashCode())
284: + ")");
285: return sb.toString();
286: }
287: }
|