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.io.IOException;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.apache.harmony.security.asn1.ASN1SequenceOf;
030: import org.apache.harmony.security.asn1.ASN1Type;
031: import org.apache.harmony.security.asn1.BerInputStream;
032:
033: /**
034: * The class encapsulates the ASN.1 DER encoding/decoding work
035: * with Certificate Policies structure which is a part of X.509 certificate
036: * (as specified in RFC 3280 -
037: * Internet X.509 Public Key Infrastructure.
038: * Certificate and Certificate Revocation List (CRL) Profile.
039: * http://www.ietf.org/rfc/rfc3280.txt):
040: *
041: * <pre>
042: * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
043: * </pre>
044: *
045: */
046:
047: public class CertificatePolicies extends ExtensionValue {
048:
049: // the values of policyInformation field of the structure
050: private List policyInformations;
051: // the ASN.1 encoded form of CertificatePolicies
052: private byte[] encoding;
053:
054: /**
055: * Constructs an object representing the value of CertificatePolicies.
056: */
057: public CertificatePolicies() {
058: }
059:
060: /**
061: * TODO
062: * @param policyInformations: List
063: */
064: public CertificatePolicies(List policyInformations) {
065: this .policyInformations = policyInformations;
066: }
067:
068: public static CertificatePolicies decode(byte[] encoding)
069: throws IOException {
070: CertificatePolicies cps = ((CertificatePolicies) ASN1
071: .decode(encoding));
072: cps.encoding = encoding;
073: return cps;
074: }
075:
076: //
077: // TODO
078: // @param policyInformations: List
079: // @param encoding: byte[]
080: //
081: private CertificatePolicies(List policyInformations, byte[] encoding) {
082: this .policyInformations = policyInformations;
083: this .encoding = encoding;
084: }
085:
086: /**
087: * Returns the values of policyInformation field of the structure.
088: * @return policyInformations
089: */
090: public List getPolicyInformations() {
091: return new ArrayList(policyInformations);
092: }
093:
094: /**
095: * TODO
096: * @param policyInformation: PolicyInformation
097: * @return
098: */
099: public CertificatePolicies addPolicyInformation(
100: PolicyInformation policyInformation) {
101: encoding = null;
102: if (policyInformations == null) {
103: policyInformations = new ArrayList();
104: }
105: policyInformations.add(policyInformation);
106: return this ;
107: }
108:
109: /**
110: * Returns ASN.1 encoded form of this X.509 CertificatePolicies value.
111: * @return a byte array containing ASN.1 encode form.
112: */
113: public byte[] getEncoded() {
114: if (encoding == null) {
115: encoding = ASN1.encode(this );
116: }
117: return encoding;
118: }
119:
120: /**
121: * Places the string representation of extension value
122: * into the StringBuffer object.
123: */
124: public void dumpValue(StringBuffer buffer, String prefix) {
125: buffer.append(prefix).append("CertificatePolicies [\n"); //$NON-NLS-1$
126: for (Iterator it = policyInformations.iterator(); it.hasNext();) {
127: buffer.append(prefix);
128: buffer.append(" "); //$NON-NLS-1$
129: ((PolicyInformation) it.next()).dumpValue(buffer);
130: buffer.append('\n');
131: }
132: buffer.append(prefix).append("]\n"); //$NON-NLS-1$
133: }
134:
135: /**
136: * ASN.1 DER X.509 CertificatePolicies encoder/decoder class.
137: */
138: public static final ASN1Type ASN1 = new ASN1SequenceOf(
139: PolicyInformation.ASN1) {
140:
141: public Object getDecodedObject(BerInputStream in) {
142: return new CertificatePolicies((List) in.content, in
143: .getEncoded());
144: }
145:
146: public Collection getValues(Object object) {
147: CertificatePolicies cps = (CertificatePolicies) object;
148: return cps.policyInformations;
149: }
150: };
151: }
|