001package org.apache.commons.ssl.org.bouncycastle.asn1.crmf; 002 003import java.util.Enumeration; 004 005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector; 006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object; 007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive; 008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence; 009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject; 010import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence; 011import org.apache.commons.ssl.org.bouncycastle.asn1.DERTaggedObject; 012import org.apache.commons.ssl.org.bouncycastle.asn1.x509.Time; 013 014public class OptionalValidity 015 extends ASN1Object 016{ 017 private Time notBefore; 018 private Time notAfter; 019 020 private OptionalValidity(ASN1Sequence seq) 021 { 022 Enumeration en = seq.getObjects(); 023 while (en.hasMoreElements()) 024 { 025 ASN1TaggedObject tObj = (ASN1TaggedObject)en.nextElement(); 026 027 if (tObj.getTagNo() == 0) 028 { 029 notBefore = Time.getInstance(tObj, true); 030 } 031 else 032 { 033 notAfter = Time.getInstance(tObj, true); 034 } 035 } 036 } 037 038 public static OptionalValidity getInstance(Object o) 039 { 040 if (o instanceof OptionalValidity) 041 { 042 return (OptionalValidity)o; 043 } 044 045 if (o != null) 046 { 047 return new OptionalValidity(ASN1Sequence.getInstance(o)); 048 } 049 050 return null; 051 } 052 053 public OptionalValidity(Time notBefore, Time notAfter) 054 { 055 if (notBefore == null && notAfter == null) 056 { 057 throw new IllegalArgumentException("at least one of notBefore/notAfter must not be null."); 058 } 059 060 this.notBefore = notBefore; 061 this.notAfter = notAfter; 062 } 063 064 public Time getNotBefore() 065 { 066 return notBefore; 067 } 068 069 public Time getNotAfter() 070 { 071 return notAfter; 072 } 073 074 /** 075 * <pre> 076 * OptionalValidity ::= SEQUENCE { 077 * notBefore [0] Time OPTIONAL, 078 * notAfter [1] Time OPTIONAL } --at least one MUST be present 079 * </pre> 080 * @return a basic ASN.1 object representation. 081 */ 082 public ASN1Primitive toASN1Primitive() 083 { 084 ASN1EncodableVector v = new ASN1EncodableVector(); 085 086 if (notBefore != null) 087 { 088 v.add(new DERTaggedObject(true, 0, notBefore)); 089 } 090 091 if (notAfter != null) 092 { 093 v.add(new DERTaggedObject(true, 1, notAfter)); 094 } 095 096 return new DERSequence(v); 097 } 098}