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.util.ArrayList;
     26 import java.util.Collection;
     27 import java.util.List;
     28 import org.apache.harmony.security.asn1.ASN1SequenceOf;
     29 import org.apache.harmony.security.asn1.ASN1Type;
     30 import org.apache.harmony.security.asn1.BerInputStream;
     31 
     32 
     33 /**
     34  * The class encapsulates the ASN.1 DER encoding/decoding work
     35  * with the GeneralNames structure which is a part of X.509 certificate
     36  * (as specified in RFC 3280 -
     37  *  Internet X.509 Public Key Infrastructure.
     38  *  Certificate and Certificate Revocation List (CRL) Profile.
     39  *  http://www.ietf.org/rfc/rfc3280.txt):
     40  *
     41  *
     42  * <pre>
     43  *   GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
     44  * </pre>
     45  *
     46  * @see org.apache.harmony.security.x509.NameConstraints
     47  * @see org.apache.harmony.security.x509.GeneralSubtree
     48  */
     49 public final class GeneralNames {
     50     /** the values of GeneralName */
     51     private List<GeneralName> generalNames;
     52     /** the ASN.1 encoded form of GeneralNames */
     53     private byte[] encoding;
     54 
     55     public GeneralNames() {
     56         generalNames = new ArrayList<GeneralName>();
     57     }
     58 
     59     public GeneralNames(List<GeneralName> generalNames) {
     60         this.generalNames = generalNames;
     61     }
     62 
     63     private GeneralNames(List<GeneralName> generalNames, byte[] encoding) {
     64         this.generalNames = generalNames;
     65         this.encoding = encoding;
     66     }
     67 
     68     /**
     69      * Returns the list of values.
     70      */
     71     public List<GeneralName> getNames() {
     72         if ((generalNames == null) || (generalNames.size() == 0)) {
     73             return null;
     74         }
     75         return new ArrayList<GeneralName>(generalNames);
     76     }
     77 
     78     /**
     79      * Returns the collection of pairs: (Integer (tag), Object (name value))*
     80      */
     81     public Collection<List<?>> getPairsList() {
     82         Collection<List<?>> result = new ArrayList<List<?>>();
     83         if (generalNames == null) {
     84             return result;
     85         }
     86         for (GeneralName generalName : generalNames) {
     87             result.add(generalName.getAsList());
     88         }
     89         return result;
     90     }
     91 
     92     public void addName(GeneralName name) {
     93         encoding = null;
     94         if (generalNames == null) {
     95             generalNames = new ArrayList<GeneralName>();
     96         }
     97         generalNames.add(name);
     98     }
     99 
    100     /**
    101      * Returns ASN.1 encoded form of this X.509 GeneralNames value.
    102      */
    103     public byte[] getEncoded() {
    104         if (encoding == null) {
    105             encoding = ASN1.encode(this);
    106         }
    107         return encoding;
    108     }
    109 
    110     public void dumpValue(StringBuilder sb, String prefix) {
    111         if (generalNames == null) {
    112             return;
    113         }
    114         for (GeneralName generalName : generalNames) {
    115             sb.append(prefix);
    116             sb.append(generalName);
    117             sb.append('\n');
    118         }
    119     }
    120 
    121     /**
    122      * ASN.1 DER X.509 GeneralNames encoder/decoder class.
    123      */
    124     public static final ASN1Type ASN1 = new ASN1SequenceOf(GeneralName.ASN1) {
    125         @Override public Object getDecodedObject(BerInputStream in) {
    126             return new GeneralNames((List<GeneralName>) in.content, in.getEncoded());
    127         }
    128 
    129         @Override public Collection getValues(Object object) {
    130             GeneralNames gns = (GeneralNames) object;
    131             return gns.generalNames;
    132         }
    133     };
    134 }
    135