1 20 package com.novosec.pkix.asn1.crmf; 21 22 import java.util.Enumeration ; 23 24 import org.bouncycastle.asn1.ASN1EncodableVector; 25 import org.bouncycastle.asn1.ASN1Sequence; 26 import org.bouncycastle.asn1.ASN1TaggedObject; 27 import org.bouncycastle.asn1.DEREncodable; 28 import org.bouncycastle.asn1.DERObject; 29 import org.bouncycastle.asn1.DERSequence; 30 import org.bouncycastle.asn1.DERTaggedObject; 31 import org.bouncycastle.asn1.x509.Time; 32 33 43 public class OptionalValidity implements DEREncodable 44 { 45 public static final boolean bTimeIsExplicit = true; 47 48 private Time notBefore = null; 49 private Time notAfter = null; 50 51 public static OptionalValidity getInstance( ASN1TaggedObject obj, boolean explicit ) 52 { 53 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 54 } 55 56 public static OptionalValidity getInstance( Object obj ) 57 { 58 if (obj == null) { 59 return new OptionalValidity(); 60 } 61 else if (obj instanceof OptionalValidity) { 62 return (OptionalValidity)obj; 63 } 64 else if (obj instanceof ASN1Sequence) { 65 return new OptionalValidity((ASN1Sequence)obj); 66 } 67 else { 68 throw new IllegalArgumentException ("unknown object in factory"); 69 } 70 } 71 72 public OptionalValidity( ASN1Sequence seq ) 73 { 74 Enumeration e = (seq == null ? null : seq.getObjects()); 75 while (e != null && e.hasMoreElements()) 76 { 77 DERTaggedObject obj = (DERTaggedObject)e.nextElement(); 78 int tagno = (obj == null ? -1 : obj.getTagNo()); 79 switch( tagno ) 80 { 81 case 0: this.notBefore = Time.getInstance( obj, bTimeIsExplicit ); break; 82 case 1: this.notAfter = Time.getInstance( obj, bTimeIsExplicit ); break; 83 default : throw new IllegalArgumentException ("invalid asn1 sequence"); 84 } 85 } 86 } 87 88 public OptionalValidity() 89 { 90 } 91 92 public void setNotBefore( Time notBefore ) 93 { 94 this.notBefore = notBefore; 95 } 96 97 public Time getNotBefore() 98 { 99 return notBefore; 100 } 101 102 public void setNotAfter( Time notAfter ) 103 { 104 this.notAfter = notAfter; 105 } 106 107 public Time getNotAfter() 108 { 109 return notAfter; 110 } 111 112 public DERObject getDERObject() 113 { 114 ASN1EncodableVector v = new ASN1EncodableVector(); 115 116 if( notBefore != null ) 117 v.add( new DERTaggedObject( bTimeIsExplicit, 0, notBefore ) ); 118 119 if( notAfter != null ) 120 v.add( new DERTaggedObject( bTimeIsExplicit, 1, notAfter ) ); 121 122 return new DERSequence(v); 123 } 124 125 public String toString() { 126 StringBuffer sb = new StringBuffer (this.getClass().getName()); 127 sb.append(" ("); 128 129 if( this.getNotBefore() != null ) 130 sb.append("notBefore: " + this.getNotBefore() + ", "); 131 132 if( this.getNotBefore() != null ) 133 sb.append("notAfter: " + this.getNotAfter() + ", "); 134 135 sb.append("hashCode: " + Integer.toHexString(this.hashCode()) + ")"); 136 return sb.toString(); 137 } 138 } 139 | Popular Tags |