Home | History | Annotate | Download | only in x509
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19 * @author Alexander Y. Kleymenov
     20 * @version $Revision$
     21 */
     22 
     23 package org.apache.harmony.security.x509;
     24 
     25 import java.io.IOException;
     26 import javax.security.auth.x500.X500Principal;
     27 import org.apache.harmony.security.asn1.ASN1Choice;
     28 import org.apache.harmony.security.asn1.ASN1Implicit;
     29 import org.apache.harmony.security.asn1.ASN1Type;
     30 import org.apache.harmony.security.asn1.BerInputStream;
     31 import org.apache.harmony.security.x501.Name;
     32 
     33 /**
     34  * The class encapsulates the ASN.1 DER encoding/decoding work
     35  * with the DistributionPointName structure which is the part
     36  * of X.509 CRL
     37  * (as specified in RFC 3280 -
     38  *  Internet X.509 Public Key Infrastructure.
     39  *  Certificate and Certificate Revocation List (CRL) Profile.
     40  *  http://www.ietf.org/rfc/rfc3280.txt):
     41  *
     42  * <pre>
     43  *  CRLDistributionPoints ::= SEQUENCE SIZE (1..MAX) OF DistributionPoint
     44  *
     45  *  DistributionPoint ::= SEQUENCE {
     46  *        distributionPoint       [0]     DistributionPointName OPTIONAL,
     47  *        reasons                 [1]     ReasonFlags OPTIONAL,
     48  *        cRLIssuer               [2]     GeneralNames OPTIONAL
     49  *  }
     50  *
     51  *  DistributionPointName ::= CHOICE {
     52  *        fullName                [0]     GeneralNames,
     53  *        nameRelativeToCRLIssuer [1]     RelativeDistinguishedName
     54  *  }
     55  *
     56  *  ReasonFlags ::= BIT STRING {
     57  *        unused                  (0),
     58  *        keyCompromise           (1),
     59  *        cACompromise            (2),
     60  *        affiliationChanged      (3),
     61  *        superseded              (4),
     62  *        cessationOfOperation    (5),
     63  *        certificateHold         (6),
     64  *        privilegeWithdrawn      (7),
     65  *        aACompromise            (8)
     66  *  }
     67  * </pre>
     68  */
     69 public class DistributionPointName {
     70 
     71     private final GeneralNames fullName;
     72     private final Name nameRelativeToCRLIssuer;
     73 
     74 
     75     public DistributionPointName(GeneralNames fullName) {
     76         this.fullName = fullName;
     77         this.nameRelativeToCRLIssuer = null;
     78     }
     79 
     80     public DistributionPointName(Name nameRelativeToCRLIssuer) {
     81         this.fullName = null;
     82         this.nameRelativeToCRLIssuer = nameRelativeToCRLIssuer;
     83     }
     84 
     85     /**
     86      * Places the string representation of extension value
     87      * into the StringBuffer object.
     88      */
     89     public void dumpValue(StringBuffer buffer, String prefix) {
     90         buffer.append(prefix);
     91         buffer.append("Distribution Point Name: [\n");
     92         if (fullName != null) {
     93             fullName.dumpValue(buffer, prefix + "  ");
     94         } else {
     95             buffer.append(prefix);
     96             buffer.append("  ");
     97             buffer.append(nameRelativeToCRLIssuer.getName(
     98                         X500Principal.RFC2253));
     99         }
    100         buffer.append(prefix);
    101         buffer.append("]\n");
    102     }
    103 
    104     public static final ASN1Choice ASN1 = new ASN1Choice(new ASN1Type[] {
    105             new ASN1Implicit(0, GeneralNames.ASN1),
    106             new ASN1Implicit(1, Name.ASN1_RDN) }) {
    107 
    108         public int getIndex(java.lang.Object object) {
    109             DistributionPointName dpn = (DistributionPointName) object;
    110             return (dpn.fullName == null) ? 1 : 0;
    111         }
    112 
    113         protected Object getDecodedObject(BerInputStream in) throws IOException {
    114             DistributionPointName result = null;
    115             if (in.choiceIndex == 0) {
    116                 result = new DistributionPointName((GeneralNames) in.content);
    117             } else {
    118                 // note: ASN.1 decoder will report an error if index
    119                 // is neither 0 or 1
    120                 result = new DistributionPointName((Name) in.content);
    121             }
    122             return result;
    123         }
    124 
    125         public Object getObjectToEncode(Object object) {
    126             DistributionPointName dpn = (DistributionPointName) object;
    127             if (dpn.fullName == null) {
    128                 return dpn.nameRelativeToCRLIssuer;
    129             } else {
    130                 return dpn.fullName;
    131             }
    132         }
    133     };
    134 }
    135 
    136