View Javadoc
1   /*
2    * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.security.x509;
27  
28  import java.util.*;
29  import java.io.IOException;
30  
31  import sun.security.util.*;
32  
33  /**
34   * This object class represents the GeneralNames type required in
35   * X509 certificates.
36   * <p>The ASN.1 syntax for this is:
37   * <pre>
38   * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
39   * </pre>
40   *
41   * @author Amit Kapoor
42   * @author Hemma Prafullchandra
43   *
44   */
45  public class GeneralNames {
46  
47      private final List<GeneralName> names;
48  
49      /**
50       * Create the GeneralNames, decoding from the passed DerValue.
51       *
52       * @param derVal the DerValue to construct the GeneralNames from.
53       * @exception IOException on error.
54       */
55      public GeneralNames(DerValue derVal) throws IOException {
56          this();
57          if (derVal.tag != DerValue.tag_Sequence) {
58              throw new IOException("Invalid encoding for GeneralNames.");
59          }
60          if (derVal.data.available() == 0) {
61              throw new IOException("No data available in "
62                                        + "passed DER encoded value.");
63          }
64          // Decode all the GeneralName's
65          while (derVal.data.available() != 0) {
66              DerValue encName = derVal.data.getDerValue();
67  
68              GeneralName name = new GeneralName(encName);
69              add(name);
70          }
71      }
72  
73      /**
74       * The default constructor for this class.
75       */
76      public GeneralNames() {
77          names = new ArrayList<GeneralName>();
78      }
79  
80      public GeneralNames add(GeneralName name) {
81          if (name == null) {
82              throw new NullPointerException();
83          }
84          names.add(name);
85          return this;
86      }
87  
88      public GeneralName get(int index) {
89          return names.get(index);
90      }
91  
92      public boolean isEmpty() {
93          return names.isEmpty();
94      }
95  
96      public int size() {
97          return names.size();
98      }
99  
100     public Iterator<GeneralName> iterator() {
101         return names.iterator();
102     }
103 
104     public List<GeneralName> names() {
105         return names;
106     }
107 
108     /**
109      * Write the extension to the DerOutputStream.
110      *
111      * @param out the DerOutputStream to write the extension to.
112      * @exception IOException on error.
113      */
114     public void encode(DerOutputStream out) throws IOException {
115         if (isEmpty()) {
116             return;
117         }
118 
119         DerOutputStream temp = new DerOutputStream();
120         for (GeneralName gn : names) {
121             gn.encode(temp);
122         }
123         out.write(DerValue.tag_Sequence, temp);
124     }
125 
126     /**
127      * compare this GeneralNames to other object for equality
128      *
129      * @returns true iff this equals other
130      */
131     public boolean equals(Object obj) {
132         if (this == obj) {
133             return true;
134         }
135         if (obj instanceof GeneralNames == false) {
136             return false;
137         }
138         GeneralNames other = (GeneralNames)obj;
139         return this.names.equals(other.names);
140     }
141 
142     public int hashCode() {
143         return names.hashCode();
144     }
145 
146     public String toString() {
147         return names.toString();
148     }
149 
150 }