001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.util.asn1.x509;
019
020 import org.apache.geronimo.util.asn1.ASN1Encodable;
021 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
022 import org.apache.geronimo.util.asn1.DERInteger;
023 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
024 import org.apache.geronimo.util.asn1.DERSequence;
025 import org.apache.geronimo.util.asn1.DERGeneralizedTime;
026 import org.apache.geronimo.util.asn1.DERBitString;
027 import org.apache.geronimo.util.asn1.DERSet;
028
029 /**
030 * Generator for Version 2 AttributeCertificateInfo
031 * <pre>
032 * AttributeCertificateInfo ::= SEQUENCE {
033 * version AttCertVersion -- version is v2,
034 * holder Holder,
035 * issuer AttCertIssuer,
036 * signature AlgorithmIdentifier,
037 * serialNumber CertificateSerialNumber,
038 * attrCertValidityPeriod AttCertValidityPeriod,
039 * attributes SEQUENCE OF Attribute,
040 * issuerUniqueID UniqueIdentifier OPTIONAL,
041 * extensions Extensions OPTIONAL
042 * }
043 * </pre>
044 *
045 */
046 public class V2AttributeCertificateInfoGenerator
047 {
048 private DERInteger version;
049 private Holder holder;
050 private AttCertIssuer issuer;
051 private AlgorithmIdentifier signature;
052 private DERInteger serialNumber;
053 private AttCertValidityPeriod attrCertValidityPeriod;
054 private ASN1EncodableVector attributes;
055 private DERBitString issuerUniqueID;
056 private X509Extensions extensions;
057 private DERGeneralizedTime startDate, endDate;
058
059 public V2AttributeCertificateInfoGenerator()
060 {
061 this.version = new DERInteger(1);
062 attributes = new ASN1EncodableVector();
063 }
064
065 public void setHolder(Holder holder)
066 {
067 this.holder = holder;
068 }
069
070 public void addAttribute(String oid, ASN1Encodable value)
071 {
072 attributes.add(new Attribute(new DERObjectIdentifier(oid), new DERSet(value)));
073 }
074
075 /**
076 * @param attribute
077 */
078 public void addAttribute(Attribute attribute)
079 {
080 attributes.add(attribute);
081 }
082
083 public void setSerialNumber(
084 DERInteger serialNumber)
085 {
086 this.serialNumber = serialNumber;
087 }
088
089 public void setSignature(
090 AlgorithmIdentifier signature)
091 {
092 this.signature = signature;
093 }
094
095 public void setIssuer(
096 AttCertIssuer issuer)
097 {
098 this.issuer = issuer;
099 }
100
101 public void setStartDate(
102 DERGeneralizedTime startDate)
103 {
104 this.startDate = startDate;
105 }
106
107 public void setEndDate(
108 DERGeneralizedTime endDate)
109 {
110 this.endDate = endDate;
111 }
112
113 public void setIssuerUniqueID(
114 DERBitString issuerUniqueID)
115 {
116 this.issuerUniqueID = issuerUniqueID;
117 }
118
119 public void setExtensions(
120 X509Extensions extensions)
121 {
122 this.extensions = extensions;
123 }
124
125 public AttributeCertificateInfo generateAttributeCertificateInfo()
126 {
127 if ((serialNumber == null) || (signature == null)
128 || (issuer == null) || (startDate == null) || (endDate == null)
129 || (holder == null) || (attributes == null))
130 {
131 throw new IllegalStateException("not all mandatory fields set in V2 AttributeCertificateInfo generator");
132 }
133
134 ASN1EncodableVector v = new ASN1EncodableVector();
135
136 v.add(version);
137 v.add(holder);
138 v.add(issuer);
139 v.add(signature);
140 v.add(serialNumber);
141
142 //
143 // before and after dates => AttCertValidityPeriod
144 //
145 AttCertValidityPeriod validity = new AttCertValidityPeriod(startDate, endDate);
146 v.add(validity);
147
148 // Attributes
149 v.add(new DERSequence(attributes));
150
151 if (issuerUniqueID != null)
152 {
153 v.add(issuerUniqueID);
154 }
155
156 if (extensions != null)
157 {
158 v.add(extensions);
159 }
160
161 return new AttributeCertificateInfo(new DERSequence(v));
162 }
163 }