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.DEREncodable;
028: import org.bouncycastle.asn1.DERObject;
029: import org.bouncycastle.asn1.DERSequence;
030: import org.bouncycastle.asn1.DERTaggedObject;
031: import org.bouncycastle.asn1.x509.Time;
032:
033: /**
034: * ASN.1 structure DER En/DeCoder.
035: *
036: * <pre>
037: * OptionalValidity ::= SEQUENCE {
038: * notBefore [0] Time OPTIONAL,
039: * notAfter [1] Time OPTIONAL } --at least one MUST be present
040: *
041: * </pre>
042: */
043: public class OptionalValidity implements DEREncodable {
044: // time is a choice type --> tag it explicit
045: public static final boolean bTimeIsExplicit = true;
046:
047: private Time notBefore = null;
048: private Time notAfter = null;
049:
050: public static OptionalValidity getInstance(ASN1TaggedObject obj,
051: boolean explicit) {
052: return getInstance(ASN1Sequence.getInstance(obj, explicit));
053: }
054:
055: public static OptionalValidity getInstance(Object obj) {
056: if (obj == null) {
057: return new OptionalValidity();
058: } else if (obj instanceof OptionalValidity) {
059: return (OptionalValidity) obj;
060: } else if (obj instanceof ASN1Sequence) {
061: return new OptionalValidity((ASN1Sequence) obj);
062: } else {
063: throw new IllegalArgumentException(
064: "unknown object in factory");
065: }
066: }
067:
068: public OptionalValidity(ASN1Sequence seq) {
069: Enumeration e = (seq == null ? null : seq.getObjects());
070: while (e != null && e.hasMoreElements()) {
071: DERTaggedObject obj = (DERTaggedObject) e.nextElement();
072: int tagno = (obj == null ? -1 : obj.getTagNo());
073: switch (tagno) {
074: case 0:
075: this .notBefore = Time.getInstance(obj, bTimeIsExplicit);
076: break;
077: case 1:
078: this .notAfter = Time.getInstance(obj, bTimeIsExplicit);
079: break;
080: default:
081: throw new IllegalArgumentException(
082: "invalid asn1 sequence");
083: }
084: }
085: }
086:
087: public OptionalValidity() {
088: }
089:
090: public void setNotBefore(Time notBefore) {
091: this .notBefore = notBefore;
092: }
093:
094: public Time getNotBefore() {
095: return notBefore;
096: }
097:
098: public void setNotAfter(Time notAfter) {
099: this .notAfter = notAfter;
100: }
101:
102: public Time getNotAfter() {
103: return notAfter;
104: }
105:
106: public DERObject getDERObject() {
107: ASN1EncodableVector v = new ASN1EncodableVector();
108:
109: if (notBefore != null)
110: v.add(new DERTaggedObject(bTimeIsExplicit, 0, notBefore));
111:
112: if (notAfter != null)
113: v.add(new DERTaggedObject(bTimeIsExplicit, 1, notAfter));
114:
115: return new DERSequence(v);
116: }
117:
118: public String toString() {
119: StringBuffer sb = new StringBuffer(this .getClass().getName());
120: sb.append(" (");
121:
122: if (this .getNotBefore() != null)
123: sb.append("notBefore: " + this .getNotBefore() + ", ");
124:
125: if (this .getNotBefore() != null)
126: sb.append("notAfter: " + this .getNotAfter() + ", ");
127:
128: sb.append("hashCode: " + Integer.toHexString(this .hashCode())
129: + ")");
130: return sb.toString();
131: }
132: }
|