src/share/classes/sun/security/provider/X509Factory.java

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.provider;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 import java.security.cert.*;
  31 import sun.security.x509.X509CertImpl;
  32 import sun.security.x509.X509CRLImpl;
  33 import sun.security.pkcs.PKCS7;
  34 import sun.security.provider.certpath.X509CertPath;
  35 import sun.security.provider.certpath.X509CertificatePair;
  36 import sun.security.util.DerValue;
  37 import sun.security.util.Cache;
  38 import sun.misc.BASE64Decoder;
  39 import sun.security.pkcs.ParsingException;
  40 
  41 /**
  42  * This class defines a certificate factory for X.509 v3 certificates &
  43  * certification paths, and X.509 v2 certificate revocation lists (CRLs).
  44  *
  45  * @author Jan Luehe
  46  * @author Hemma Prafullchandra
  47  * @author Sean Mullan
  48  *
  49  *
  50  * @see java.security.cert.CertificateFactorySpi
  51  * @see java.security.cert.Certificate
  52  * @see java.security.cert.CertPath
  53  * @see java.security.cert.CRL
  54  * @see java.security.cert.X509Certificate
  55  * @see java.security.cert.X509CRL
  56  * @see sun.security.x509.X509CertImpl
  57  * @see sun.security.x509.X509CRLImpl
  58  */


 558                     }
 559                 } else {
 560                     break;
 561                 }
 562             }
 563 
 564             // Step 4: Consume the footer
 565             StringBuffer footer = new StringBuffer("-");
 566             while (true) {
 567                 int next = is.read();
 568                 // Add next == '\n' for maximum safety, in case endline
 569                 // is not consistent.
 570                 if (next == -1 || next == end || next == '\n') {
 571                     break;
 572                 }
 573                 if (next != '\r') footer.append((char)next);
 574             }
 575 
 576             checkHeaderFooter(header.toString(), footer.toString());
 577 
 578             BASE64Decoder decoder = new BASE64Decoder();
 579             return decoder.decodeBuffer(new String(data, 0, pos));
 580         }
 581     }
 582 
 583     private static void checkHeaderFooter(String header,
 584             String footer) throws IOException {
 585         if (header.length() < 16 || !header.startsWith("-----BEGIN ") ||
 586                 !header.endsWith("-----")) {
 587             throw new IOException("Illegal header: " + header);
 588         }
 589         if (footer.length() < 14 || !footer.startsWith("-----END ") ||
 590                 !footer.endsWith("-----")) {
 591             throw new IOException("Illegal footer: " + footer);
 592         }
 593         String headerType = header.substring(11, header.length()-5);
 594         String footerType = footer.substring(9, footer.length()-5);
 595         if (!headerType.equals(footerType)) {
 596             throw new IOException("Header and footer do not match: " +
 597                     header + " " + footer);
 598         }
 599     }




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.provider;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 import java.security.cert.*;
  31 import sun.security.x509.X509CertImpl;
  32 import sun.security.x509.X509CRLImpl;
  33 import sun.security.pkcs.PKCS7;
  34 import sun.security.provider.certpath.X509CertPath;
  35 import sun.security.provider.certpath.X509CertificatePair;
  36 import sun.security.util.DerValue;
  37 import sun.security.util.Cache;
  38 import java.util.Base64;
  39 import sun.security.pkcs.ParsingException;
  40 
  41 /**
  42  * This class defines a certificate factory for X.509 v3 certificates &
  43  * certification paths, and X.509 v2 certificate revocation lists (CRLs).
  44  *
  45  * @author Jan Luehe
  46  * @author Hemma Prafullchandra
  47  * @author Sean Mullan
  48  *
  49  *
  50  * @see java.security.cert.CertificateFactorySpi
  51  * @see java.security.cert.Certificate
  52  * @see java.security.cert.CertPath
  53  * @see java.security.cert.CRL
  54  * @see java.security.cert.X509Certificate
  55  * @see java.security.cert.X509CRL
  56  * @see sun.security.x509.X509CertImpl
  57  * @see sun.security.x509.X509CRLImpl
  58  */


 558                     }
 559                 } else {
 560                     break;
 561                 }
 562             }
 563 
 564             // Step 4: Consume the footer
 565             StringBuffer footer = new StringBuffer("-");
 566             while (true) {
 567                 int next = is.read();
 568                 // Add next == '\n' for maximum safety, in case endline
 569                 // is not consistent.
 570                 if (next == -1 || next == end || next == '\n') {
 571                     break;
 572                 }
 573                 if (next != '\r') footer.append((char)next);
 574             }
 575 
 576             checkHeaderFooter(header.toString(), footer.toString());
 577 
 578             return Base64.getMimeDecoder().decode(new String(data, 0, pos));

 579         }
 580     }
 581 
 582     private static void checkHeaderFooter(String header,
 583             String footer) throws IOException {
 584         if (header.length() < 16 || !header.startsWith("-----BEGIN ") ||
 585                 !header.endsWith("-----")) {
 586             throw new IOException("Illegal header: " + header);
 587         }
 588         if (footer.length() < 14 || !footer.startsWith("-----END ") ||
 589                 !footer.endsWith("-----")) {
 590             throw new IOException("Illegal footer: " + footer);
 591         }
 592         String headerType = header.substring(11, header.length()-5);
 593         String footerType = footer.substring(9, footer.length()-5);
 594         if (!headerType.equals(footerType)) {
 595             throw new IOException("Header and footer do not match: " +
 596                     header + " " + footer);
 597         }
 598     }