Source for java.security.cert.CertPathBuilder

   1: /* CertPathBuilder.java -- bulids CertPath objects from 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.security.InvalidAlgorithmParameterException;
  44: import java.security.NoSuchAlgorithmException;
  45: import java.security.NoSuchProviderException;
  46: import java.security.Provider;
  47: import java.security.Security;
  48: 
  49: /**
  50:  * This class builds certificate paths (also called certificate chains),
  51:  * which can be used to establish trust for a particular certificate by
  52:  * building a path from a trusted certificate (a trust anchor) to the
  53:  * untrusted certificate.
  54:  *
  55:  * @see CertPath
  56:  */
  57: public class CertPathBuilder
  58: {
  59: 
  60:   // Constants and fields.
  61:   // ------------------------------------------------------------------------
  62: 
  63:   /** Service name for CertPathBuilder. */
  64:   private static final String CERT_PATH_BUILDER = "CertPathBuilder";
  65: 
  66:   /** The underlying implementation. */
  67:   private CertPathBuilderSpi cpbSpi;
  68: 
  69:   /** The provider of this implementation. */
  70:   private Provider provider;
  71: 
  72:   /** The name of this implementation. */
  73:   private String algorithm;
  74: 
  75:   // Constructor.
  76:   // ------------------------------------------------------------------------
  77: 
  78:   /**
  79:    * Creates a new CertPathBuilder.
  80:    *
  81:    * @param cpbSpi    The underlying implementation.
  82:    * @param provider  The provider of the implementation.
  83:    * @param algorithm This implementation's name.
  84:    */
  85:   protected CertPathBuilder(CertPathBuilderSpi cpbSpi, Provider provider,
  86:                             String algorithm)
  87:   {
  88:     this.cpbSpi = cpbSpi;
  89:     this.provider = provider;
  90:     this.algorithm = algorithm;
  91:   }
  92: 
  93:   // Class methods.
  94:   // ------------------------------------------------------------------------
  95: 
  96:   /**
  97:    * Get the default cert path builder type.
  98:    *
  99:    * <p>This value can be set at run-time by the security property
 100:    * <code>"certpathbuilder.type"</code>. If this property is not set,
 101:    * then the value returned is <code>"PKIX"</code>.
 102:    *
 103:    * @return The default CertPathBuilder algorithm.
 104:    */
 105:   public static final String getDefaultType()
 106:   {
 107:     String type = Security.getProperty("certpathbuilder.type");
 108:     if (type == null)
 109:       type = "PKIX";
 110:     return type;
 111:   }
 112: 
 113:   /**
 114:    * Get an instance of a named CertPathBuilder, from the first provider
 115:    * that implements it.
 116:    *
 117:    * @param algorithm The name of the CertPathBuilder to create.
 118:    * @return The new instance.
 119:    * @throws NoSuchAlgorithmException If no installed provider
 120:    *   implements the named algorithm.
 121:    */
 122:   public static CertPathBuilder getInstance(String algorithm)
 123:     throws NoSuchAlgorithmException
 124:   {
 125:     Provider[] p = Security.getProviders();
 126: 
 127:     for (int i = 0; i < p.length; i++)
 128:       {
 129:         try
 130:           {
 131:             return getInstance(algorithm, p[i]);
 132:           }
 133:         catch (NoSuchAlgorithmException e)
 134:           {
 135:         // Ignored.
 136:           }
 137:       }
 138: 
 139:     throw new NoSuchAlgorithmException(algorithm);
 140:   }
 141: 
 142:   /**
 143:    * Get an instance of a named CertPathBuilder from the named
 144:    * provider.
 145:    *
 146:    * @param algorithm The name of the CertPathBuilder to create.
 147:    * @param provider  The name of the provider from which to get the
 148:    *   implementation.
 149:    * @return The new instance.
 150:    * @throws NoSuchAlgorithmException If no installed provider
 151:    *   implements the named algorithm.
 152:    * @throws NoSuchProviderException If the named provider does not
 153:    *   exist.
 154:    */
 155:   public static CertPathBuilder getInstance(String algorithm, String provider)
 156:     throws NoSuchAlgorithmException, NoSuchProviderException
 157:   {
 158:     Provider p = Security.getProvider(provider);
 159:     if (p == null)
 160:       throw new NoSuchProviderException(provider);
 161:     return getInstance(algorithm, p);
 162:   }
 163: 
 164:   /**
 165:    * Get an instance of a named CertPathBuilder from the specified
 166:    * provider.
 167:    *
 168:    * @param algorithm The name of the CertPathBuilder to create.
 169:    * @param provider  The provider from which to get the implementation.
 170:    * @return The new instance.
 171:    * @throws NoSuchAlgorithmException If no installed provider
 172:    *   implements the named algorithm.
 173:    * @throws IllegalArgumentException If <i>provider</i> in
 174:    *   <tt>null</tt>.
 175:    */
 176:   public static CertPathBuilder getInstance(String algorithm, Provider provider)
 177:     throws NoSuchAlgorithmException
 178:   {
 179:     if (provider == null)
 180:       throw new IllegalArgumentException("null provider");
 181:     try
 182:       {
 183:         return new CertPathBuilder((CertPathBuilderSpi)
 184:           Engine.getInstance(CERT_PATH_BUILDER, algorithm, provider),
 185:           provider, algorithm);
 186:       }
 187:     catch (java.lang.reflect.InvocationTargetException ite)
 188:       {
 189:         throw new NoSuchAlgorithmException(algorithm);
 190:       }
 191:     catch (ClassCastException cce)
 192:       {
 193:         throw new NoSuchAlgorithmException(algorithm);
 194:       }
 195:   }
 196: 
 197:   // Instance methods.
 198:   // ------------------------------------------------------------------------
 199: 
 200:   /**
 201:    * Return the name of this CertPathBuilder algorithm.
 202:    *
 203:    * @return The algorithm name.
 204:    */
 205:   public final String getAlgorithm()
 206:   {
 207:     return algorithm;
 208:   }
 209: 
 210:   /**
 211:    * Return the provider of this instance's implementation.
 212:    *
 213:    * @return The provider.
 214:    */
 215:   public final Provider getProvider()
 216:   {
 217:     return provider;
 218:   }
 219: 
 220:   /**
 221:    * Builds a certificate path. The {@link CertPathParameters} parameter
 222:    * passed to this method is implementation-specific, but in general
 223:    * should contain some number of certificates and some number of
 224:    * trusted certificates (or "trust anchors").
 225:    *
 226:    * @param params The parameters.
 227:    * @retrun The certificate path result.
 228:    * @throws CertPathBuilderException If the certificate path cannot be
 229:    *   built.
 230:    * @throws InvalidAlgorithmParameterException If the implementation
 231:    *   rejects the specified parameters.
 232:    */
 233:   public final CertPathBuilderResult build(CertPathParameters params)
 234:     throws CertPathBuilderException, InvalidAlgorithmParameterException
 235:   {
 236:     return cpbSpi.engineBuild(params);
 237:   }
 238: }