Source for java.security.cert.CertStore

   1: /* CertStore -- stores and retrieves certificates.
   2:    Copyright (C) 2003, 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 java.security.cert;
  40: 
  41: import gnu.java.security.Engine;
  42: 
  43: import java.lang.reflect.InvocationTargetException;
  44: import java.security.InvalidAlgorithmParameterException;
  45: import java.security.NoSuchAlgorithmException;
  46: import java.security.NoSuchProviderException;
  47: import java.security.PrivilegedAction;
  48: import java.security.Provider;
  49: import java.security.Security;
  50: import java.util.Collection;
  51: 
  52: /**
  53:  * A CertStore is a read-only repository for certificates and
  54:  * certificate revocation lists.
  55:  *
  56:  * @since 1.4
  57:  */
  58: public class CertStore
  59: {
  60: 
  61:   // Constants and fields.
  62:   // ------------------------------------------------------------------------
  63: 
  64:   /** Service name for CertStore. */
  65:   private static final String CERT_STORE = "CertStore";
  66: 
  67:   /** The underlying implementation. */
  68:   private CertStoreSpi storeSpi;
  69: 
  70:   /** This implementation's provider. */
  71:   private Provider provider;
  72: 
  73:   /** The name of this key store type. */
  74:   private String type;
  75: 
  76:   /** The parameters used to initialize this instance, if any. */
  77:   private CertStoreParameters params;
  78: 
  79:   // Constructor.
  80:   // ------------------------------------------------------------------------
  81: 
  82:   /**
  83:    * Create a new CertStore.
  84:    *
  85:    * @param storeSpi The underlying implementation.
  86:    * @param provider The provider of this implementation.
  87:    * @param type     The type of CertStore this class represents.
  88:    * @param params   The parameters used to initialize this instance, if any.
  89:    */
  90:   protected CertStore(CertStoreSpi storeSpi, Provider provider, String type,
  91:                       CertStoreParameters params)
  92:   {
  93:     this.storeSpi = storeSpi;
  94:     this.provider = provider;
  95:     this.type = type;
  96:     this.params = params;
  97:   }
  98: 
  99: // Class methods.
 100:   // ------------------------------------------------------------------------
 101: 
 102:   /**
 103:    * Returns the default certificate store type.
 104:    *
 105:    * <p>This value can be set at run-time via the security property
 106:    * "certstore.type"; if not specified than the default type will be
 107:    * "LDAP".
 108:    *
 109:    * @return The default CertStore type.
 110:    */
 111:   public static final synchronized String getDefaultType()
 112:   {
 113:     String type = null;
 114:     type = (String) java.security.AccessController.doPrivileged(
 115:       new PrivilegedAction() {
 116:         public Object run() {
 117:           return Security.getProperty("certstore.type");
 118:         }
 119:       }
 120:     );
 121:     if (type == null)
 122:       type = "LDAP";
 123:     return type;
 124:   }
 125: 
 126:   /**
 127:    * Returns an instance of the given certificate store type from the first
 128:    * installed provider.
 129:    * 
 130:    * @param type The type of <code>CertStore</code> to create.
 131:    * @param params The parameters to initialize this cert store with.
 132:    * @return The new instance.
 133:    * @throws InvalidAlgorithmParameterException If the instance rejects the
 134:    *           specified parameters.
 135:    * @throws NoSuchAlgorithmException If no installed provider implements the
 136:    *           specified CertStore.
 137:    * @throws IllegalArgumentException if <code>type</code> is
 138:    *           <code>null</code> or is an empty string.
 139:    */
 140:   public static CertStore getInstance(String type, CertStoreParameters params)
 141:     throws InvalidAlgorithmParameterException, NoSuchAlgorithmException
 142:   {
 143:     Provider[] p = Security.getProviders();
 144:     NoSuchAlgorithmException lastException = null;
 145:     for (int i = 0; i < p.length; i++)
 146:       try
 147:         {
 148:           return getInstance(type, params, p[i]);
 149:         }
 150:       catch (NoSuchAlgorithmException x)
 151:         {
 152:           lastException = x;
 153:         }
 154:     if (lastException != null)
 155:       throw lastException;
 156:     throw new NoSuchAlgorithmException(type);
 157:   }
 158: 
 159:   /**
 160:    * Returns an instance of the given certificate store type from a named
 161:    * provider.
 162:    * 
 163:    * @param type The type of <code>CertStore</code> to create.
 164:    * @param params The parameters to initialize this cert store with.
 165:    * @param provider The name of the provider to use.
 166:    * @return The new instance.
 167:    * @throws InvalidAlgorithmParameterException If the instance rejects the
 168:    *           specified parameters.
 169:    * @throws NoSuchAlgorithmException If the specified provider does not
 170:    *           implement the specified CertStore.
 171:    * @throws NoSuchProviderException If no provider named <i>provider</i> is
 172:    *           installed.
 173:    * @throws IllegalArgumentException if either <code>type</code> or
 174:    *           <code>provider</code> is <code>null</code>, or if
 175:    *           <code>type</code> is an empty string.
 176:    */
 177:   public static CertStore getInstance(String type, CertStoreParameters params,
 178:                                       String provider)
 179:     throws InvalidAlgorithmParameterException, NoSuchAlgorithmException,
 180:            NoSuchProviderException
 181:   {
 182:     if (provider == null)
 183:       throw new IllegalArgumentException("provider MUST NOT be null");
 184:     Provider p = Security.getProvider(provider);
 185:     if (p == null)
 186:       throw new NoSuchProviderException(provider);
 187:     return getInstance(type, params, p);
 188:   }
 189: 
 190:   /**
 191:    * Returns an instance of the given certificate store type from a given
 192:    * provider.
 193:    *
 194:    * @param type The type of <code>CertStore</code> to create.
 195:    * @param params   The parameters to initialize this cert store with.
 196:    * @param provider The provider to use.
 197:    * @return The new instance.
 198:    * @throws InvalidAlgorithmParameterException If the instance rejects
 199:    *         the specified parameters.
 200:    * @throws NoSuchAlgorithmException If the specified provider does not
 201:    *         implement the specified CertStore.
 202:    * @throws IllegalArgumentException if either <code>type</code> or
 203:    *           <code>provider</code> is <code>null</code>, or if
 204:    *           <code>type</code> is an empty string.
 205:    */
 206:   public static CertStore getInstance(String type, CertStoreParameters params,
 207:                                       Provider provider)
 208:       throws InvalidAlgorithmParameterException, NoSuchAlgorithmException
 209:   {
 210:     StringBuilder sb = new StringBuilder("CertStore of type [")
 211:         .append(type).append("] from provider[")
 212:         .append(provider).append("] could not be created");
 213:     Throwable cause;
 214:     try
 215:       {
 216:         Object[] args = new Object[] { params };
 217:         Object spi = Engine.getInstance(CERT_STORE, type, provider, args);
 218:         return new CertStore((CertStoreSpi) spi, provider, type, params);
 219:       }
 220:     catch (InvocationTargetException x)
 221:       {
 222:         cause = x.getCause();
 223:         if (cause instanceof NoSuchAlgorithmException)
 224:           throw (NoSuchAlgorithmException) cause;
 225:         if (cause == null)
 226:           cause = x;
 227:       }
 228:     catch (ClassCastException x)
 229:       {
 230:         cause = x;
 231:       }
 232:     NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
 233:     x.initCause(cause);
 234:     throw x;
 235:   }
 236: 
 237:   /**
 238:    * Return the type of certificate store this instance represents.
 239:    *
 240:    * @return The CertStore type.
 241:    */
 242:   public final String getType()
 243:   {
 244:     return type;
 245:   }
 246: 
 247:   /**
 248:    * Return the provider of this implementation.
 249:    *
 250:    * @return The provider.
 251:    */
 252:   public final Provider getProvider()
 253:   {
 254:     return provider;
 255:   }
 256: 
 257:   /**
 258:    * Get the parameters this instance was created with, if any. The
 259:    * parameters will be cloned before they are returned.
 260:    *
 261:    * @return The parameters, or null.
 262:    */
 263:   public final CertStoreParameters getCertStoreParameters()
 264:   {
 265:     return params != null ? (CertStoreParameters) params.clone() : null;
 266:   }
 267: 
 268:   /**
 269:    * Get a collection of certificates from this CertStore, optionally
 270:    * filtered by the specified CertSelector. The Collection returned may
 271:    * be empty, but will never be null.
 272:    *
 273:    * <p>Implementations may not allow a null argument, even if no
 274:    * filtering is desired.
 275:    *
 276:    * @param selector The certificate selector.
 277:    * @return The collection of certificates.
 278:    * @throws CertStoreException If the certificates cannot be retrieved.
 279:    */
 280:   public final Collection<? extends Certificate> getCertificates(CertSelector selector)
 281:     throws CertStoreException
 282:   {
 283:     return storeSpi.engineGetCertificates(selector);
 284:   }
 285: 
 286:   /**
 287:    * Get a collection of certificate revocation lists from this CertStore,
 288:    * optionally filtered by the specified CRLSelector. The Collection
 289:    * returned may be empty, but will never be null.
 290:    *
 291:    * <p>Implementations may not allow a null argument, even if no
 292:    * filtering is desired.
 293:    *
 294:    * @param selector The certificate selector.
 295:    * @return The collection of certificate revocation lists.
 296:    * @throws CertStoreException If the CRLs cannot be retrieved.
 297:    */
 298:   public final Collection<? extends CRL> getCRLs(CRLSelector selector)
 299:     throws CertStoreException
 300:   {
 301:     return storeSpi.engineGetCRLs(selector);
 302:   }
 303: }