< prev index next >

src/share/classes/java/security/CodeSource.java

Print this page
rev 12546 : 8181432: Better processing of unresolved permissions
Reviewed-by: mullan


  17  * You should have received a copy of the GNU General Public License version
  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 java.security;
  27 
  28 
  29 import java.net.URL;
  30 import java.net.SocketPermission;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 import java.util.Hashtable;
  34 import java.io.ByteArrayInputStream;
  35 import java.io.IOException;
  36 import java.security.cert.*;

  37 
  38 /**
  39  *
  40  * <p>This class extends the concept of a codebase to
  41  * encapsulate not only the location (URL) but also the certificate chains
  42  * that were used to verify signed code originating from that location.
  43  *
  44  * @author Li Gong
  45  * @author Roland Schemers
  46  */
  47 
  48 public class CodeSource implements java.io.Serializable {
  49 
  50     private static final long serialVersionUID = 4977541819976013951L;
  51 
  52     /**
  53      * The code location.
  54      *
  55      * @serial
  56      */


 529 
 530     /**
 531      * Restores this object from a stream (i.e., deserializes it).
 532      */
 533     private void readObject(java.io.ObjectInputStream ois)
 534         throws IOException, ClassNotFoundException
 535     {
 536         CertificateFactory cf;
 537         Hashtable<String, CertificateFactory> cfs = null;
 538         List<java.security.cert.Certificate> certList = null;
 539 
 540         ois.defaultReadObject(); // location
 541 
 542         // process any new-style certs in the stream (if present)
 543         int size = ois.readInt();
 544         if (size > 0) {
 545             // we know of 3 different cert types: X.509, PGP, SDSI, which
 546             // could all be present in the stream at the same time
 547             cfs = new Hashtable<String, CertificateFactory>(3);
 548             certList = new ArrayList<>(size > 20 ? 20 : size);


 549         }
 550 
 551         for (int i = 0; i < size; i++) {
 552             // read the certificate type, and instantiate a certificate
 553             // factory of that type (reuse existing factory if possible)
 554             String certType = ois.readUTF();
 555             if (cfs.containsKey(certType)) {
 556                 // reuse certificate factory
 557                 cf = cfs.get(certType);
 558             } else {
 559                 // create new certificate factory
 560                 try {
 561                     cf = CertificateFactory.getInstance(certType);
 562                 } catch (CertificateException ce) {
 563                     throw new ClassNotFoundException
 564                         ("Certificate factory for " + certType + " not found");
 565                 }
 566                 // store the certificate factory so we can reuse it later
 567                 cfs.put(certType, cf);
 568             }
 569             // parse the certificate
 570             byte[] encoded = null;
 571             try {
 572                 encoded = new byte[ois.readInt()];
 573             } catch (OutOfMemoryError oome) {
 574                 throw new IOException("Certificate too big");
 575             }
 576             ois.readFully(encoded);
 577             ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
 578             try {
 579                 certList.add(cf.generateCertificate(bais));
 580             } catch (CertificateException ce) {
 581                 throw new IOException(ce.getMessage());
 582             }
 583             bais.close();
 584         }
 585 
 586         if (certList != null) {
 587             this.certs = certList.toArray(
 588                     new java.security.cert.Certificate[size]);
 589         }
 590         // Deserialize array of code signers (if any)
 591         try {
 592             this.signers = ((CodeSigner[])ois.readObject()).clone();
 593         } catch (IOException ioe) {
 594             // no signers present
 595         }
 596     }




  17  * You should have received a copy of the GNU General Public License version
  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 java.security;
  27 
  28 
  29 import java.net.URL;
  30 import java.net.SocketPermission;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 import java.util.Hashtable;
  34 import java.io.ByteArrayInputStream;
  35 import java.io.IOException;
  36 import java.security.cert.*;
  37 import sun.misc.IOUtils;
  38 
  39 /**
  40  *
  41  * <p>This class extends the concept of a codebase to
  42  * encapsulate not only the location (URL) but also the certificate chains
  43  * that were used to verify signed code originating from that location.
  44  *
  45  * @author Li Gong
  46  * @author Roland Schemers
  47  */
  48 
  49 public class CodeSource implements java.io.Serializable {
  50 
  51     private static final long serialVersionUID = 4977541819976013951L;
  52 
  53     /**
  54      * The code location.
  55      *
  56      * @serial
  57      */


 530 
 531     /**
 532      * Restores this object from a stream (i.e., deserializes it).
 533      */
 534     private void readObject(java.io.ObjectInputStream ois)
 535         throws IOException, ClassNotFoundException
 536     {
 537         CertificateFactory cf;
 538         Hashtable<String, CertificateFactory> cfs = null;
 539         List<java.security.cert.Certificate> certList = null;
 540 
 541         ois.defaultReadObject(); // location
 542 
 543         // process any new-style certs in the stream (if present)
 544         int size = ois.readInt();
 545         if (size > 0) {
 546             // we know of 3 different cert types: X.509, PGP, SDSI, which
 547             // could all be present in the stream at the same time
 548             cfs = new Hashtable<String, CertificateFactory>(3);
 549             certList = new ArrayList<>(size > 20 ? 20 : size);
 550         } else if (size < 0) {
 551             throw new IOException("size cannot be negative");
 552         }
 553 
 554         for (int i = 0; i < size; i++) {
 555             // read the certificate type, and instantiate a certificate
 556             // factory of that type (reuse existing factory if possible)
 557             String certType = ois.readUTF();
 558             if (cfs.containsKey(certType)) {
 559                 // reuse certificate factory
 560                 cf = cfs.get(certType);
 561             } else {
 562                 // create new certificate factory
 563                 try {
 564                     cf = CertificateFactory.getInstance(certType);
 565                 } catch (CertificateException ce) {
 566                     throw new ClassNotFoundException
 567                         ("Certificate factory for " + certType + " not found");
 568                 }
 569                 // store the certificate factory so we can reuse it later
 570                 cfs.put(certType, cf);
 571             }
 572             // parse the certificate
 573             byte[] encoded = IOUtils.readNBytes(ois, ois.readInt());






 574             ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
 575             try {
 576                 certList.add(cf.generateCertificate(bais));
 577             } catch (CertificateException ce) {
 578                 throw new IOException(ce.getMessage());
 579             }
 580             bais.close();
 581         }
 582 
 583         if (certList != null) {
 584             this.certs = certList.toArray(
 585                     new java.security.cert.Certificate[size]);
 586         }
 587         // Deserialize array of code signers (if any)
 588         try {
 589             this.signers = ((CodeSigner[])ois.readObject()).clone();
 590         } catch (IOException ioe) {
 591             // no signers present
 592         }
 593     }


< prev index next >