jdk/src/share/classes/sun/security/x509/X509CertImpl.java
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 10709 d865c9f21240
equal deleted inserted replaced
10335:3c7eda3ab2f5 10336:0bb1999251f8
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   194     public X509CertImpl(byte[] certData) throws CertificateException {
   194     public X509CertImpl(byte[] certData) throws CertificateException {
   195         try {
   195         try {
   196             parse(new DerValue(certData));
   196             parse(new DerValue(certData));
   197         } catch (IOException e) {
   197         } catch (IOException e) {
   198             signedCert = null;
   198             signedCert = null;
   199             CertificateException ce = new
   199             throw new CertificateException("Unable to initialize, " + e, e);
   200                 CertificateException("Unable to initialize, " + e);
       
   201             ce.initCause(e);
       
   202             throw ce;
       
   203         }
   200         }
   204     }
   201     }
   205 
   202 
   206     /**
   203     /**
   207      * unmarshals an X.509 certificate from an input stream.  If the
   204      * unmarshals an X.509 certificate from an input stream.  If the
   229             try {
   226             try {
   230                 // Next, try reading stream as raw DER-encoded bytes
   227                 // Next, try reading stream as raw DER-encoded bytes
   231                 inBuffered.reset();
   228                 inBuffered.reset();
   232                 der = new DerValue(inBuffered);
   229                 der = new DerValue(inBuffered);
   233             } catch (IOException ioe1) {
   230             } catch (IOException ioe1) {
   234                 CertificateException ce = new
   231                 throw new CertificateException("Input stream must be " +
   235                     CertificateException("Input stream must be " +
   232                                                "either DER-encoded bytes " +
   236                                          "either DER-encoded bytes " +
   233                                                "or RFC1421 hex-encoded " +
   237                                          "or RFC1421 hex-encoded " +
   234                                                "DER-encoded bytes: " +
   238                                          "DER-encoded bytes: " +
   235                                                ioe1.getMessage(), ioe1);
   239                                          ioe1.getMessage());
       
   240                 ce.initCause(ioe1);
       
   241                 throw ce;
       
   242             }
   236             }
   243         }
   237         }
   244         try {
   238         try {
   245             parse(der);
   239             parse(der);
   246         } catch (IOException ioe) {
   240         } catch (IOException ioe) {
   247             signedCert = null;
   241             signedCert = null;
   248             CertificateException ce = new
   242             throw new CertificateException("Unable to parse DER value of " +
   249                 CertificateException("Unable to parse DER value of " +
   243                                            "certificate, " + ioe, ioe);
   250                                      "certificate, " + ioe);
       
   251             ce.initCause(ioe);
       
   252             throw ce;
       
   253         }
   244         }
   254     }
   245     }
   255 
   246 
   256     /**
   247     /**
   257      * read input stream as HEX-encoded DER-encoded bytes
   248      * read input stream as HEX-encoded DER-encoded bytes
   318     public X509CertImpl(DerValue derVal) throws CertificateException {
   309     public X509CertImpl(DerValue derVal) throws CertificateException {
   319         try {
   310         try {
   320             parse(derVal);
   311             parse(derVal);
   321         } catch (IOException e) {
   312         } catch (IOException e) {
   322             signedCert = null;
   313             signedCert = null;
   323             CertificateException ce = new
   314             throw new CertificateException("Unable to initialize, " + e, e);
   324                 CertificateException("Unable to initialize, " + e);
       
   325             ce.initCause(e);
       
   326             throw ce;
       
   327         }
   315         }
   328     }
   316     }
   329 
   317 
   330     /**
   318     /**
   331      * Appends the certificate to an output stream.
   319      * Appends the certificate to an output stream.
  1277             }
  1265             }
  1278             if (extensions == null) {
  1266             if (extensions == null) {
  1279                 return null;
  1267                 return null;
  1280             } else {
  1268             } else {
  1281                 for (Extension ex : extensions.getAllExtensions()) {
  1269                 for (Extension ex : extensions.getAllExtensions()) {
  1282                     if (ex.getExtensionId().equals(oid)) {
  1270                     if (ex.getExtensionId().equals((Object)oid)) {
  1283                         //XXXX May want to consider cloning this
  1271                         //XXXX May want to consider cloning this
  1284                         return ex;
  1272                         return ex;
  1285                     }
  1273                     }
  1286                 }
  1274                 }
  1287                 /* no such extension in this certificate */
  1275                 /* no such extension in this certificate */
  1432 
  1420 
  1433             ExtendedKeyUsageExtension ekuExt =
  1421             ExtendedKeyUsageExtension ekuExt =
  1434                 new ExtendedKeyUsageExtension(Boolean.FALSE, data);
  1422                 new ExtendedKeyUsageExtension(Boolean.FALSE, data);
  1435             return Collections.unmodifiableList(ekuExt.getExtendedKeyUsage());
  1423             return Collections.unmodifiableList(ekuExt.getExtendedKeyUsage());
  1436         } catch (IOException ioe) {
  1424         } catch (IOException ioe) {
  1437             CertificateParsingException cpe =
  1425             throw new CertificateParsingException(ioe);
  1438                 new CertificateParsingException();
       
  1439             cpe.initCause(ioe);
       
  1440             throw cpe;
       
  1441         }
  1426         }
  1442     }
  1427     }
  1443 
  1428 
  1444     /**
  1429     /**
  1445      * Get the certificate constraints path length from the
  1430      * Get the certificate constraints path length from the
  1576         if (subjectAltNameExt == null) {
  1561         if (subjectAltNameExt == null) {
  1577             return null;
  1562             return null;
  1578         }
  1563         }
  1579         GeneralNames names;
  1564         GeneralNames names;
  1580         try {
  1565         try {
  1581             names = (GeneralNames) subjectAltNameExt.get
  1566             names = subjectAltNameExt.get(
  1582                 (SubjectAlternativeNameExtension.SUBJECT_NAME);
  1567                     SubjectAlternativeNameExtension.SUBJECT_NAME);
  1583         } catch (IOException ioe) {
  1568         } catch (IOException ioe) {
  1584             // should not occur
  1569             // should not occur
  1585             return Collections.<List<?>>emptySet();
  1570             return Collections.<List<?>>emptySet();
  1586         }
  1571         }
  1587         subjectAlternativeNames = makeAltNames(names);
  1572         subjectAlternativeNames = makeAltNames(names);
  1608                 new SubjectAlternativeNameExtension(Boolean.FALSE,
  1593                 new SubjectAlternativeNameExtension(Boolean.FALSE,
  1609                                                     data);
  1594                                                     data);
  1610 
  1595 
  1611             GeneralNames names;
  1596             GeneralNames names;
  1612             try {
  1597             try {
  1613                 names = (GeneralNames) subjectAltNameExt.get
  1598                 names = subjectAltNameExt.get(
  1614                     (SubjectAlternativeNameExtension.SUBJECT_NAME);
  1599                         SubjectAlternativeNameExtension.SUBJECT_NAME);
  1615             }  catch (IOException ioe) {
  1600             }  catch (IOException ioe) {
  1616                 // should not occur
  1601                 // should not occur
  1617                 return Collections.<List<?>>emptySet();
  1602                 return Collections.<List<?>>emptySet();
  1618             }
  1603             }
  1619             return makeAltNames(names);
  1604             return makeAltNames(names);
  1620         } catch (IOException ioe) {
  1605         } catch (IOException ioe) {
  1621             CertificateParsingException cpe =
  1606             throw new CertificateParsingException(ioe);
  1622                 new CertificateParsingException();
       
  1623             cpe.initCause(ioe);
       
  1624             throw cpe;
       
  1625         }
  1607         }
  1626     }
  1608     }
  1627 
  1609 
  1628     /**
  1610     /**
  1629      * This method are the overridden implementation of
  1611      * This method are the overridden implementation of
  1642         if (issuerAltNameExt == null) {
  1624         if (issuerAltNameExt == null) {
  1643             return null;
  1625             return null;
  1644         }
  1626         }
  1645         GeneralNames names;
  1627         GeneralNames names;
  1646         try {
  1628         try {
  1647             names = (GeneralNames) issuerAltNameExt.get
  1629             names = issuerAltNameExt.get(
  1648                 (IssuerAlternativeNameExtension.ISSUER_NAME);
  1630                     IssuerAlternativeNameExtension.ISSUER_NAME);
  1649         } catch (IOException ioe) {
  1631         } catch (IOException ioe) {
  1650             // should not occur
  1632             // should not occur
  1651             return Collections.<List<?>>emptySet();
  1633             return Collections.<List<?>>emptySet();
  1652         }
  1634         }
  1653         issuerAlternativeNames = makeAltNames(names);
  1635         issuerAlternativeNames = makeAltNames(names);
  1674             IssuerAlternativeNameExtension issuerAltNameExt =
  1656             IssuerAlternativeNameExtension issuerAltNameExt =
  1675                 new IssuerAlternativeNameExtension(Boolean.FALSE,
  1657                 new IssuerAlternativeNameExtension(Boolean.FALSE,
  1676                                                     data);
  1658                                                     data);
  1677             GeneralNames names;
  1659             GeneralNames names;
  1678             try {
  1660             try {
  1679                 names = (GeneralNames) issuerAltNameExt.get
  1661                 names = issuerAltNameExt.get(
  1680                     (IssuerAlternativeNameExtension.ISSUER_NAME);
  1662                         IssuerAlternativeNameExtension.ISSUER_NAME);
  1681             }  catch (IOException ioe) {
  1663             }  catch (IOException ioe) {
  1682                 // should not occur
  1664                 // should not occur
  1683                 return Collections.<List<?>>emptySet();
  1665                 return Collections.<List<?>>emptySet();
  1684             }
  1666             }
  1685             return makeAltNames(names);
  1667             return makeAltNames(names);
  1686         } catch (IOException ioe) {
  1668         } catch (IOException ioe) {
  1687             CertificateParsingException cpe =
  1669             throw new CertificateParsingException(ioe);
  1688                 new CertificateParsingException();
       
  1689             cpe.initCause(ioe);
       
  1690             throw cpe;
       
  1691         }
  1670         }
  1692     }
  1671     }
  1693 
  1672 
  1694     public AuthorityInfoAccessExtension getAuthorityInfoAccessExtension() {
  1673     public AuthorityInfoAccessExtension getAuthorityInfoAccessExtension() {
  1695         return (AuthorityInfoAccessExtension)
  1674         return (AuthorityInfoAccessExtension)