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 java.util.ArrayList; 27 import java.util.Collection; 28 import java.util.List; 29 import org.apache.harmony.security.asn1.ASN1SequenceOf; 30 import org.apache.harmony.security.asn1.ASN1Type; 31 import org.apache.harmony.security.asn1.BerInputStream; 32 33 /** 34 * The class encapsulates the ASN.1 DER encoding/decoding work 35 * with Certificate Policies 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 * <pre> 42 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation 43 * </pre> 44 */ 45 public final class CertificatePolicies extends ExtensionValue { 46 /** the values of policyInformation field of the structure */ 47 private List<PolicyInformation> policyInformations; 48 /** the ASN.1 encoded form of CertificatePolicies */ 49 private byte[] encoding; 50 51 /** 52 * Constructs an object representing the value of CertificatePolicies. 53 */ 54 public CertificatePolicies() {} 55 56 public static CertificatePolicies decode(byte[] encoding) throws IOException { 57 CertificatePolicies cps = ((CertificatePolicies) ASN1.decode(encoding)); 58 cps.encoding = encoding; 59 return cps; 60 } 61 62 private CertificatePolicies(List<PolicyInformation> policyInformations, byte[] encoding) { 63 this.policyInformations = policyInformations; 64 this.encoding = encoding; 65 } 66 67 /** 68 * Returns the values of policyInformation field of the structure. 69 */ 70 public List<PolicyInformation> getPolicyInformations() { 71 return new ArrayList<PolicyInformation>(policyInformations); 72 } 73 74 public CertificatePolicies addPolicyInformation(PolicyInformation policyInformation) { 75 encoding = null; 76 if (policyInformations == null) { 77 policyInformations = new ArrayList<PolicyInformation>(); 78 } 79 policyInformations.add(policyInformation); 80 return this; 81 } 82 83 /** 84 * Returns ASN.1 encoded form of this X.509 CertificatePolicies value. 85 */ 86 @Override public byte[] getEncoded() { 87 if (encoding == null) { 88 encoding = ASN1.encode(this); 89 } 90 return encoding; 91 } 92 93 @Override public void dumpValue(StringBuilder sb, String prefix) { 94 sb.append(prefix).append("CertificatePolicies [\n"); 95 for (PolicyInformation policyInformation : policyInformations) { 96 sb.append(prefix); 97 sb.append(" "); 98 policyInformation.dumpValue(sb); 99 sb.append('\n'); 100 } 101 sb.append(prefix).append("]\n"); 102 } 103 104 /** 105 * ASN.1 DER X.509 CertificatePolicies encoder/decoder class. 106 */ 107 public static final ASN1Type ASN1 = new ASN1SequenceOf(PolicyInformation.ASN1) { 108 @Override public Object getDecodedObject(BerInputStream in) { 109 return new CertificatePolicies((List<PolicyInformation>) in.content, in.getEncoded()); 110 } 111 112 @Override public Collection getValues(Object object) { 113 CertificatePolicies cps = (CertificatePolicies) object; 114 return cps.policyInformations; 115 } 116 }; 117 } 118