Merge from the pain train
[official-gcc.git] / libjava / gnu / java / security / x509 / ext / CertificatePolicies.java
blob9c14dc96ef7f8ab03118d7c8ea1e27e446b5e3f9
1 /* CertificatePolicies.java -- certificate policy extension.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.x509.ext;
41 import java.io.IOException;
42 import java.security.cert.PolicyQualifierInfo;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.LinkedList;
49 import java.util.List;
50 import java.util.Map;
52 import gnu.java.security.OID;
53 import gnu.java.security.der.DER;
54 import gnu.java.security.der.DERReader;
55 import gnu.java.security.der.DERValue;
57 public class CertificatePolicies extends Extension.Value
60 // Constants and fields.
61 // -------------------------------------------------------------------------
63 public static final OID ID = new OID("2.5.29.32");
65 private final List policies;
66 private final Map policyQualifierInfos;
68 // Constructor.
69 // -------------------------------------------------------------------------
71 public CertificatePolicies(final byte[] encoded) throws IOException
73 super(encoded);
74 DERReader der = new DERReader(encoded);
75 DERValue pol = der.read();
76 if (!pol.isConstructed())
77 throw new IOException("malformed CertificatePolicies");
79 int len = 0;
80 LinkedList policyList = new LinkedList();
81 HashMap qualifierMap = new HashMap();
82 while (len < pol.getLength())
84 DERValue policyInfo = der.read();
85 if (!policyInfo.isConstructed())
86 throw new IOException("malformed PolicyInformation");
87 DERValue val = der.read();
88 if (val.getTag() != DER.OBJECT_IDENTIFIER)
89 throw new IOException("malformed CertPolicyId");
90 OID policyId = (OID) val.getValue();
91 policyList.add(policyId);
92 if (val.getEncodedLength() < policyInfo.getLength())
94 DERValue qual = der.read();
95 int len2 = 0;
96 LinkedList quals = new LinkedList();
97 while (len2 < qual.getLength())
99 val = der.read();
100 quals.add(new PolicyQualifierInfo(val.getEncoded()));
101 der.skip(val.getLength());
102 len2 += val.getEncodedLength();
104 qualifierMap.put(policyId, quals);
106 len += policyInfo.getEncodedLength();
109 policies = Collections.unmodifiableList(policyList);
110 policyQualifierInfos = Collections.unmodifiableMap(qualifierMap);
113 public CertificatePolicies (final List policies,
114 final Map policyQualifierInfos)
116 for (Iterator it = policies.iterator(); it.hasNext(); )
117 if (!(it.next() instanceof OID))
118 throw new IllegalArgumentException ("policies must be OIDs");
119 for (Iterator it = policyQualifierInfos.entrySet().iterator(); it.hasNext();)
121 Map.Entry e = (Map.Entry) it.next();
122 if (!(e.getKey() instanceof OID) || !policies.contains (e.getKey()))
123 throw new IllegalArgumentException
124 ("policyQualifierInfos keys must be OIDs");
125 if (!(e.getValue() instanceof List))
126 throw new IllegalArgumentException
127 ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
128 for (Iterator it2 = ((List) e.getValue()).iterator(); it.hasNext(); )
129 if (!(it2.next() instanceof PolicyQualifierInfo))
130 throw new IllegalArgumentException
131 ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
133 this.policies = Collections.unmodifiableList (new ArrayList (policies));
134 this.policyQualifierInfos = Collections.unmodifiableMap
135 (new HashMap (policyQualifierInfos));
138 // Instance methods.
139 // -------------------------------------------------------------------------
141 public List getPolicies()
143 return policies;
146 public List getPolicyQualifierInfos(OID oid)
148 return (List) policyQualifierInfos.get(oid);
151 public byte[] getEncoded()
153 if (encoded == null)
155 List pol = new ArrayList (policies.size());
156 for (Iterator it = policies.iterator(); it.hasNext(); )
158 OID policy = (OID) it.next();
159 List qualifiers = getPolicyQualifierInfos (policy);
160 List l = new ArrayList (qualifiers == null ? 1 : 2);
161 l.add (new DERValue (DER.OBJECT_IDENTIFIER, policy));
162 if (qualifiers != null)
164 List ll = new ArrayList (qualifiers.size());
165 for (Iterator it2 = qualifiers.iterator(); it.hasNext(); )
167 PolicyQualifierInfo info = (PolicyQualifierInfo) it2.next();
170 ll.add (DERReader.read (info.getEncoded()));
172 catch (IOException ioe)
176 l.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, ll));
178 pol.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, l));
180 encoded = new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, pol).getEncoded();
182 return (byte[]) encoded.clone();
185 public String toString()
187 return CertificatePolicies.class.getName() + " [ policies=" + policies +
188 " policyQualifierInfos=" + policyQualifierInfos + " ]";