Source for gnu.java.security.provider.X509CertificateFactory

   1: /* X509CertificateFactory.java -- generates X.509 certificates.
   2:    Copyright (C) 2003 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.provider;
  40: 
  41: import gnu.java.io.Base64InputStream;
  42: import gnu.java.security.x509.X509CRL;
  43: import gnu.java.security.x509.X509CertPath;
  44: import gnu.java.security.x509.X509Certificate;
  45: 
  46: import java.io.BufferedInputStream;
  47: import java.io.EOFException;
  48: import java.io.IOException;
  49: import java.io.InputStream;
  50: import java.security.cert.CRL;
  51: import java.security.cert.CRLException;
  52: import java.security.cert.CertPath;
  53: import java.security.cert.Certificate;
  54: import java.security.cert.CertificateEncodingException;
  55: import java.security.cert.CertificateException;
  56: import java.security.cert.CertificateFactorySpi;
  57: import java.util.Collection;
  58: import java.util.Iterator;
  59: import java.util.LinkedList;
  60: import java.util.List;
  61: 
  62: public class X509CertificateFactory extends CertificateFactorySpi
  63: {
  64: 
  65:   // Constants.
  66:   // ------------------------------------------------------------------------
  67: 
  68:   public static final String BEGIN_CERTIFICATE = "-----BEGIN CERTIFICATE-----";
  69:   public static final String END_CERTIFICATE = "-----END CERTIFICATE-----";
  70:   public static final String BEGIN_X509_CRL = "-----BEGIN X509 CRL-----";
  71:   public static final String END_X509_CRL = "-----END X509 CRL-----";
  72: 
  73:   // Constructors.
  74:   // ------------------------------------------------------------------------
  75: 
  76:   public X509CertificateFactory()
  77:   {
  78:     super();
  79:   }
  80: 
  81:   // Instance methods.
  82:   // ------------------------------------------------------------------------
  83: 
  84:   public Certificate engineGenerateCertificate(InputStream inStream)
  85:     throws CertificateException
  86:   {
  87:     try
  88:       {
  89:         return generateCert(inStream);
  90:       }
  91:     catch (IOException ioe)
  92:       {
  93:         CertificateException ce = new CertificateException(ioe.getMessage());
  94:         ce.initCause (ioe);
  95:         throw ce;
  96:       }
  97:   }
  98: 
  99:   public Collection engineGenerateCertificates(InputStream inStream)
 100:     throws CertificateException
 101:   {
 102:     LinkedList certs = new LinkedList();
 103:     while (true)
 104:       {
 105:         try
 106:           {
 107:             certs.add(generateCert(inStream));
 108:           }
 109:         catch (EOFException eof)
 110:           {
 111:             break;
 112:           }
 113:         catch (IOException ioe)
 114:           {
 115:             CertificateException ce = new CertificateException(ioe.getMessage());
 116:             ce.initCause (ioe);
 117:             throw ce;
 118:           }
 119:       }
 120:     return certs;
 121:   }
 122: 
 123:   public CRL engineGenerateCRL(InputStream inStream) throws CRLException
 124:   {
 125:     try
 126:       {
 127:         return generateCRL(inStream);
 128:       }
 129:     catch (IOException ioe)
 130:       {
 131:         CRLException crle = new CRLException(ioe.getMessage());
 132:         crle.initCause (ioe);
 133:         throw crle;
 134:       }
 135:   }
 136: 
 137:   public Collection engineGenerateCRLs(InputStream inStream)
 138:     throws CRLException
 139:   {
 140:     LinkedList crls = new LinkedList();
 141:     while (true)
 142:       {
 143:         try
 144:           {
 145:             crls.add(generateCRL(inStream));
 146:           }
 147:         catch (EOFException eof)
 148:           {
 149:             break;
 150:           }
 151:         catch (IOException ioe)
 152:           {
 153:             CRLException crle = new CRLException(ioe.getMessage());
 154:             crle.initCause (ioe);
 155:             throw crle;
 156:           }
 157:       }
 158:     return crls;
 159:   }
 160: 
 161:   public CertPath engineGenerateCertPath(List certs)
 162:   {
 163:     return new X509CertPath(certs);
 164:   }
 165: 
 166:   public CertPath engineGenerateCertPath(InputStream in)
 167:     throws CertificateEncodingException
 168:   {
 169:     return new X509CertPath(in);
 170:   }
 171: 
 172:   public CertPath engineGenerateCertPath(InputStream in, String encoding)
 173:     throws CertificateEncodingException
 174:   {
 175:     return new X509CertPath(in, encoding);
 176:   }
 177: 
 178:   public Iterator engineGetCertPathEncodings()
 179:   {
 180:     return X509CertPath.ENCODINGS.iterator();
 181:   }
 182: 
 183:   // Own methods.
 184:   // ------------------------------------------------------------------------
 185: 
 186:   private X509Certificate generateCert(InputStream inStream)
 187:     throws IOException, CertificateException
 188:   {
 189:     if (inStream == null)
 190:       throw new CertificateException("missing input stream");
 191:     if (!inStream.markSupported())
 192:       inStream = new BufferedInputStream(inStream, 8192);
 193:     inStream.mark(20);
 194:     int i = inStream.read();
 195:     if (i == -1)
 196:       throw new EOFException();
 197: 
 198:     // If the input is in binary DER format, the first byte MUST be
 199:     // 0x30, which stands for the ASN.1 [UNIVERSAL 16], which is the
 200:     // UNIVERSAL SEQUENCE, with the CONSTRUCTED bit (0x20) set.
 201:     //
 202:     // So if we do not see 0x30 here we will assume it is in Base-64.
 203:     if (i != 0x30)
 204:       {
 205:         inStream.reset();
 206:         StringBuffer line = new StringBuffer(80);
 207:         do
 208:           {
 209:             line.setLength(0);
 210:             do
 211:               {
 212:                 i = inStream.read();
 213:                 if (i == -1)
 214:                   throw new EOFException();
 215:                 if (i != '\n' && i != '\r')
 216:                   line.append((char) i);
 217:               }
 218:             while (i != '\n' && i != '\r');
 219:           }
 220:         while (!line.toString().equals(BEGIN_CERTIFICATE));
 221:         X509Certificate ret = new X509Certificate(
 222:            new BufferedInputStream(new Base64InputStream(inStream), 8192));
 223:         line.setLength(0);
 224:         line.append('-'); // Base64InputStream will eat this.
 225:         do
 226:           {
 227:             i = inStream.read();
 228:             if (i == -1)
 229:               throw new EOFException();
 230:             if (i != '\n' && i != '\r')
 231:               line.append((char) i);
 232:           }
 233:         while (i != '\n' && i != '\r');
 234:         // XXX ???
 235:         if (!line.toString().equals(END_CERTIFICATE))
 236:           throw new CertificateException("no end-of-certificate marker");
 237:         return ret;
 238:       }
 239:     else
 240:       {
 241:         inStream.reset();
 242:         return new X509Certificate(inStream);
 243:       }
 244:   }
 245: 
 246:   private X509CRL generateCRL(InputStream inStream)
 247:     throws IOException, CRLException
 248:   {
 249:     if (inStream == null)
 250:       throw new CRLException("missing input stream");
 251:     if (!inStream.markSupported())
 252:       inStream = new BufferedInputStream(inStream, 8192);
 253:     inStream.mark(20);
 254:     int i = inStream.read();
 255:     if (i == -1)
 256:       throw new EOFException();
 257: 
 258:     // If the input is in binary DER format, the first byte MUST be
 259:     // 0x30, which stands for the ASN.1 [UNIVERSAL 16], which is the
 260:     // UNIVERSAL SEQUENCE, with the CONSTRUCTED bit (0x20) set.
 261:     //
 262:     // So if we do not see 0x30 here we will assume it is in Base-64.
 263:     if (i != 0x30)
 264:       {
 265:         inStream.reset();
 266:         StringBuffer line = new StringBuffer(80);
 267:         do
 268:           {
 269:             line.setLength(0);
 270:             do
 271:               {
 272:                 i = inStream.read();
 273:                 if (i == -1)
 274:                   throw new EOFException();
 275:                 if (i != '\n' && i != '\r')
 276:                   line.append((char) i);
 277:               }
 278:             while (i != '\n' && i != '\r');
 279:           }
 280:         while (!line.toString().startsWith(BEGIN_X509_CRL));
 281:         X509CRL ret = new X509CRL(
 282:            new BufferedInputStream(new Base64InputStream(inStream), 8192));
 283:         line.setLength(0);
 284:         line.append('-'); // Base64InputStream will eat this.
 285:         do
 286:           {
 287:             i = inStream.read();
 288:             if (i == -1)
 289:               throw new EOFException();
 290:             if (i != '\n' && i != '\r')
 291:               line.append((char) i);
 292:           }
 293:         while (i != '\n' && i != '\r');
 294:         // XXX ???
 295:         if (!line.toString().startsWith(END_X509_CRL))
 296:           throw new CRLException("no end-of-CRL marker");
 297:         return ret;
 298:       }
 299:     else
 300:       {
 301:         inStream.reset();
 302:         return new X509CRL(inStream);
 303:       }
 304:   }
 305: }