001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package org.apache.harmony.security.x509;
022:
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.harmony.security.asn1.ASN1SequenceOf;
029: import org.apache.harmony.security.asn1.ASN1Type;
030: import org.apache.harmony.security.asn1.BerInputStream;
031:
032: /**
033: * The class encapsulates the ASN.1 DER encoding/decoding work
034: * with the GeneralNames structure which is a part of X.509 certificate
035: * (as specified in RFC 3280 -
036: * Internet X.509 Public Key Infrastructure.
037: * Certificate and Certificate Revocation List (CRL) Profile.
038: * http://www.ietf.org/rfc/rfc3280.txt):
039: *
040: *
041: * <pre>
042: * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
043: * </pre>
044: *
045: * @see org.apache.harmony.security.x509.NameConstraints
046: * @see org.apache.harmony.security.x509.GeneralSubtree
047: */
048: public class GeneralNames {
049:
050: // the values of GeneralName
051: private List generalNames;
052: // the ASN.1 encoded form of GeneralNames
053: private byte[] encoding;
054:
055: /**
056: * Constructs an object representing the value of GeneralNames.
057: */
058: public GeneralNames() {
059: generalNames = new ArrayList();
060: }
061:
062: /**
063: * TODO
064: * @param generalNames: List
065: */
066: public GeneralNames(List generalNames) {
067: this .generalNames = generalNames;
068: }
069:
070: //
071: // TODO
072: // @param generalNames: List
073: // @param encoding: byte[]
074: //
075: private GeneralNames(List generalNames, byte[] encoding) {
076: this .generalNames = generalNames;
077: this .encoding = encoding;
078: }
079:
080: /**
081: * Returns the list of values.
082: * @return names
083: */
084: public List getNames() {
085: if ((generalNames == null) || (generalNames.size() == 0)) {
086: return null;
087: }
088: return new ArrayList(generalNames);
089: }
090:
091: /**
092: * Returns the collection of pairs: (Integer (tag), Object (name value))*
093: * @return the collection of pairs: (Integer (tag), Object (name value))*
094: */
095: public List getPairsList() {
096: ArrayList result = new ArrayList();
097: if (generalNames == null) {
098: return result;
099: }
100: Iterator it = generalNames.iterator();
101: while (it.hasNext()) {
102: result.add(((GeneralName) it.next()).getAsList());
103: }
104: return result;
105: }
106:
107: /**
108: * TODO
109: * @param name: GeneralName
110: * @return
111: */
112: public void addName(GeneralName name) {
113: encoding = null;
114: if (generalNames == null) {
115: generalNames = new ArrayList();
116: }
117: generalNames.add(name);
118: }
119:
120: /* *
121: * TODO
122: * @param name: GeneralName
123: * @return
124: *
125: public GeneralName getNameByTag(int tag) {
126: encoding = null;
127: if ((generalNames == null) || (generalNames.size() == 0)) {
128: return null;
129: }
130: for (int i=0; i<generalNames.size(); i++) {
131: if (((GeneralName) generalName.get(i)).getTag() == tag) {
132: }
133: }
134: generalNames.add(name);
135: }
136: */
137:
138: /**
139: * Returns ASN.1 encoded form of this X.509 GeneralNames value.
140: * @return a byte array containing ASN.1 encode form.
141: */
142: public byte[] getEncoded() {
143: if (encoding == null) {
144: encoding = ASN1.encode(this );
145: }
146: return encoding;
147: }
148:
149: /**
150: * Places the string representation of extension value
151: * into the StringBuffer object.
152: */
153: public void dumpValue(StringBuffer buffer, String prefix) {
154: if (generalNames == null) {
155: return;
156: }
157: for (Iterator it = generalNames.iterator(); it.hasNext();) {
158: buffer.append(prefix);
159: buffer.append(it.next());
160: buffer.append('\n');
161: }
162: }
163:
164: /**
165: * ASN.1 DER X.509 GeneralNames encoder/decoder class.
166: */
167: public static final ASN1Type ASN1 = new ASN1SequenceOf(
168: GeneralName.ASN1) {
169:
170: public Object getDecodedObject(BerInputStream in) {
171: return new GeneralNames((List) in.content, in.getEncoded());
172: }
173:
174: public Collection getValues(Object object) {
175: GeneralNames gns = (GeneralNames) object;
176: return gns.generalNames;
177: }
178: };
179: }
|