Source for gnu.java.security.x509.ext.CertificatePolicies

   1: /* CertificatePolicies.java -- certificate policy extension.
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package gnu.java.security.x509.ext;
  40: 
  41: import gnu.java.security.OID;
  42: import gnu.java.security.der.DER;
  43: import gnu.java.security.der.DERReader;
  44: import gnu.java.security.der.DERValue;
  45: 
  46: import java.io.IOException;
  47: import java.security.cert.PolicyQualifierInfo;
  48: import java.util.ArrayList;
  49: import java.util.Collections;
  50: import java.util.HashMap;
  51: import java.util.Iterator;
  52: import java.util.LinkedList;
  53: import java.util.List;
  54: import java.util.Map;
  55: 
  56: public class CertificatePolicies extends Extension.Value
  57: {
  58: 
  59:   // Constants and fields.
  60:   // -------------------------------------------------------------------------
  61: 
  62:   public static final OID ID = new OID("2.5.29.32");
  63: 
  64:   private final List policies;
  65:   private final Map policyQualifierInfos;
  66: 
  67:   // Constructor.
  68:   // -------------------------------------------------------------------------
  69: 
  70:   public CertificatePolicies(final byte[] encoded) throws IOException
  71:   {
  72:     super(encoded);
  73:     DERReader der = new DERReader(encoded);
  74:     DERValue pol = der.read();
  75:     if (!pol.isConstructed())
  76:       throw new IOException("malformed CertificatePolicies");
  77: 
  78:     int len = 0;
  79:     LinkedList policyList = new LinkedList();
  80:     HashMap qualifierMap = new HashMap();
  81:     while (len < pol.getLength())
  82:       {
  83:         DERValue policyInfo = der.read();
  84:         if (!policyInfo.isConstructed())
  85:           throw new IOException("malformed PolicyInformation");
  86:         DERValue val = der.read();
  87:         if (val.getTag() != DER.OBJECT_IDENTIFIER)
  88:           throw new IOException("malformed CertPolicyId");
  89:         OID policyId = (OID) val.getValue();
  90:         policyList.add(policyId);
  91:         if (val.getEncodedLength() < policyInfo.getLength())
  92:           {
  93:             DERValue qual = der.read();
  94:             int len2 = 0;
  95:             LinkedList quals = new LinkedList();
  96:             while (len2 < qual.getLength())
  97:               {
  98:                 val = der.read();
  99:                 quals.add(new PolicyQualifierInfo(val.getEncoded()));
 100:                 der.skip(val.getLength());
 101:                 len2 += val.getEncodedLength();
 102:               }
 103:             qualifierMap.put(policyId, quals);
 104:           }
 105:         len += policyInfo.getEncodedLength();
 106:       }
 107: 
 108:     policies = Collections.unmodifiableList(policyList);
 109:     policyQualifierInfos = Collections.unmodifiableMap(qualifierMap);
 110:   }
 111: 
 112:   public CertificatePolicies (final List policies,
 113:                               final Map policyQualifierInfos)
 114:   {
 115:     for (Iterator it = policies.iterator(); it.hasNext(); )
 116:       if (!(it.next() instanceof OID))
 117:         throw new IllegalArgumentException ("policies must be OIDs");
 118:     for (Iterator it = policyQualifierInfos.entrySet().iterator(); it.hasNext();)
 119:       {
 120:         Map.Entry e = (Map.Entry) it.next();
 121:         if (!(e.getKey() instanceof OID) || !policies.contains (e.getKey()))
 122:           throw new IllegalArgumentException
 123:             ("policyQualifierInfos keys must be OIDs");
 124:         if (!(e.getValue() instanceof List))
 125:           throw new IllegalArgumentException
 126:             ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
 127:         for (Iterator it2 = ((List) e.getValue()).iterator(); it.hasNext(); )
 128:           if (!(it2.next() instanceof PolicyQualifierInfo))
 129:             throw new IllegalArgumentException
 130:               ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
 131:       }
 132:     this.policies = Collections.unmodifiableList (new ArrayList (policies));
 133:     this.policyQualifierInfos = Collections.unmodifiableMap
 134:       (new HashMap (policyQualifierInfos));
 135:   }
 136: 
 137:   // Instance methods.
 138:   // -------------------------------------------------------------------------
 139: 
 140:   public List getPolicies()
 141:   {
 142:     return policies;
 143:   }
 144: 
 145:   public List getPolicyQualifierInfos(OID oid)
 146:   {
 147:     return (List) policyQualifierInfos.get(oid);
 148:   }
 149: 
 150:   public byte[] getEncoded()
 151:   {
 152:     if (encoded == null)
 153:       {
 154:         List pol = new ArrayList (policies.size());
 155:         for (Iterator it = policies.iterator(); it.hasNext(); )
 156:           {
 157:             OID policy = (OID) it.next();
 158:             List qualifiers = getPolicyQualifierInfos (policy);
 159:             List l = new ArrayList (qualifiers == null ? 1 : 2);
 160:             l.add (new DERValue (DER.OBJECT_IDENTIFIER, policy));
 161:             if (qualifiers != null)
 162:               {
 163:                 List ll = new ArrayList (qualifiers.size());
 164:                 for (Iterator it2 = qualifiers.iterator(); it.hasNext(); )
 165:                   {
 166:                     PolicyQualifierInfo info = (PolicyQualifierInfo) it2.next();
 167:                     try
 168:                       {
 169:                         ll.add (DERReader.read (info.getEncoded()));
 170:                       }
 171:                     catch (IOException ioe)
 172:                       {
 173:                       }
 174:                   }
 175:                 l.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, ll));
 176:               }
 177:             pol.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, l));
 178:           }
 179:         encoded = new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, pol).getEncoded();
 180:       }
 181:     return (byte[]) encoded.clone();
 182:   }
 183: 
 184:   public String toString()
 185:   {
 186:     return CertificatePolicies.class.getName() + " [ policies=" + policies +
 187:       " policyQualifierInfos=" + policyQualifierInfos + " ]";
 188:   }
 189: }