001: package org.bouncycastle.asn1.crmf;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERBitString;
008: import org.bouncycastle.asn1.DERInteger;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERSequence;
011: import org.bouncycastle.asn1.DERTaggedObject;
012: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
013: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
014: import org.bouncycastle.asn1.x509.X509Extensions;
015: import org.bouncycastle.asn1.x509.X509Name;
016:
017: import java.util.Enumeration;
018:
019: public class CertTemplate extends ASN1Encodable {
020: private DERInteger version;
021: private DERInteger serialNumber;
022: private AlgorithmIdentifier signingAlg;
023: private X509Name issuer;
024: private OptionalValidity validity;
025: private X509Name subject;
026: private SubjectPublicKeyInfo publicKey;
027: private DERBitString issuerUID;
028: private DERBitString subjectUID;
029: private X509Extensions extensions;
030:
031: private CertTemplate(ASN1Sequence seq) {
032: Enumeration en = seq.getObjects();
033: while (en.hasMoreElements()) {
034: ASN1TaggedObject tObj = (ASN1TaggedObject) en.nextElement();
035:
036: switch (tObj.getTagNo()) {
037: case 0:
038: version = DERInteger.getInstance(tObj, false);
039: break;
040: case 1:
041: serialNumber = DERInteger.getInstance(tObj, false);
042: break;
043: case 2:
044: signingAlg = AlgorithmIdentifier.getInstance(tObj,
045: false);
046: break;
047: case 3:
048: issuer = X509Name.getInstance(tObj, false);
049: break;
050: case 4:
051: validity = OptionalValidity.getInstance(ASN1Sequence
052: .getInstance(tObj, false));
053: break;
054: case 5:
055: subject = X509Name.getInstance(tObj, false);
056: break;
057: case 6:
058: publicKey = SubjectPublicKeyInfo.getInstance(tObj,
059: false);
060: break;
061: case 7:
062: issuerUID = DERBitString.getInstance(tObj, false);
063: break;
064: case 8:
065: subjectUID = DERBitString.getInstance(tObj, false);
066: break;
067: case 9:
068: extensions = X509Extensions.getInstance(tObj, false);
069: break;
070: default:
071: throw new IllegalArgumentException("unknown tag: "
072: + tObj.getTagNo());
073: }
074: }
075: }
076:
077: public static CertTemplate getInstance(Object o) {
078: if (o instanceof CertTemplate) {
079: return (CertTemplate) o;
080: }
081:
082: if (o instanceof ASN1Sequence) {
083: return new CertTemplate((ASN1Sequence) o);
084: }
085:
086: throw new IllegalArgumentException("Invalid object: "
087: + o.getClass().getName());
088: }
089:
090: /**
091: * <pre>
092: * CertTemplate ::= SEQUENCE {
093: * version [0] Version OPTIONAL,
094: * serialNumber [1] INTEGER OPTIONAL,
095: * signingAlg [2] AlgorithmIdentifier OPTIONAL,
096: * issuer [3] Name OPTIONAL,
097: * validity [4] OptionalValidity OPTIONAL,
098: * subject [5] Name OPTIONAL,
099: * publicKey [6] SubjectPublicKeyInfo OPTIONAL,
100: * issuerUID [7] UniqueIdentifier OPTIONAL,
101: * subjectUID [8] UniqueIdentifier OPTIONAL,
102: * extensions [9] Extensions OPTIONAL }
103: * </pre>
104: * @return a basic ASN.1 object representation.
105: */
106: public DERObject toASN1Object() {
107: ASN1EncodableVector v = new ASN1EncodableVector();
108:
109: addOptional(v, 0, version);
110: addOptional(v, 1, serialNumber);
111: addOptional(v, 2, signingAlg);
112: addOptional(v, 3, issuer);
113: addOptional(v, 4, validity);
114: addOptional(v, 5, subject);
115: addOptional(v, 6, publicKey);
116: addOptional(v, 7, issuerUID);
117: addOptional(v, 8, subjectUID);
118: addOptional(v, 9, extensions);
119:
120: return new DERSequence(v);
121: }
122:
123: private void addOptional(ASN1EncodableVector v, int tagNo,
124: ASN1Encodable obj) {
125: if (obj != null) {
126: v.add(new DERTaggedObject(false, tagNo, obj));
127: }
128: }
129: }
|