< prev index next >

src/share/classes/java/security/UnresolvedPermission.java

Print this page
rev 12546 : 8181432: Better processing of unresolved permissions
Reviewed-by: mullan
   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  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 import java.io.IOException;
  29 import java.io.ByteArrayInputStream;

  30 import java.util.ArrayList;
  31 import java.util.Hashtable;
  32 import java.lang.reflect.*;
  33 import java.security.cert.*;

  34 
  35 /**
  36  * The UnresolvedPermission class is used to hold Permissions that
  37  * were "unresolved" when the Policy was initialized.
  38  * An unresolved permission is one whose actual Permission class
  39  * does not yet exist at the time the Policy is initialized (see below).
  40  *
  41  * <p>The policy for a Java runtime (specifying
  42  * which permissions are available for code from various principals)
  43  * is represented by a Policy object.
  44  * Whenever a Policy is initialized or refreshed, Permission objects of
  45  * appropriate classes are created for all permissions
  46  * allowed by the Policy.
  47  *
  48  * <p>Many permission class types
  49  * referenced by the policy configuration are ones that exist
  50  * locally (i.e., ones that can be found on CLASSPATH).
  51  * Objects for such permissions can be instantiated during
  52  * Policy initialization. For example, it is always possible
  53  * to instantiate a java.io.FilePermission, since the


 532                 try {
 533                     oos.writeUTF(cert.getType());
 534                     byte[] encoded = cert.getEncoded();
 535                     oos.writeInt(encoded.length);
 536                     oos.write(encoded);
 537                 } catch (CertificateEncodingException cee) {
 538                     throw new IOException(cee.getMessage());
 539                 }
 540             }
 541         }
 542     }
 543 
 544     /**
 545      * Restores this object from a stream (i.e., deserializes it).
 546      */
 547     private void readObject(java.io.ObjectInputStream ois)
 548         throws IOException, ClassNotFoundException
 549     {
 550         CertificateFactory cf;
 551         Hashtable<String, CertificateFactory> cfs = null;

 552 
 553         ois.defaultReadObject();
 554 
 555         if (type == null)
 556                 throw new NullPointerException("type can't be null");
 557 
 558         // process any new-style certs in the stream (if present)
 559         int size = ois.readInt();
 560         if (size > 0) {
 561             // we know of 3 different cert types: X.509, PGP, SDSI, which
 562             // could all be present in the stream at the same time
 563             cfs = new Hashtable<String, CertificateFactory>(3);
 564             this.certs = new java.security.cert.Certificate[size];


 565         }
 566 
 567         for (int i=0; i<size; i++) {
 568             // read the certificate type, and instantiate a certificate
 569             // factory of that type (reuse existing factory if possible)
 570             String certType = ois.readUTF();
 571             if (cfs.containsKey(certType)) {
 572                 // reuse certificate factory
 573                 cf = cfs.get(certType);
 574             } else {
 575                 // create new certificate factory
 576                 try {
 577                     cf = CertificateFactory.getInstance(certType);
 578                 } catch (CertificateException ce) {
 579                     throw new ClassNotFoundException
 580                         ("Certificate factory for "+certType+" not found");
 581                 }
 582                 // store the certificate factory so we can reuse it later
 583                 cfs.put(certType, cf);
 584             }
 585             // parse the certificate
 586             byte[] encoded=null;
 587             try {
 588                 encoded = new byte[ois.readInt()];
 589             } catch (OutOfMemoryError oome) {
 590                 throw new IOException("Certificate too big");
 591             }
 592             ois.readFully(encoded);
 593             ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
 594             try {
 595                 this.certs[i] = cf.generateCertificate(bais);
 596             } catch (CertificateException ce) {
 597                 throw new IOException(ce.getMessage());
 598             }
 599             bais.close();




 600         }
 601     }
 602 }
   1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  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 import sun.misc.IOUtils;
  29 
  30 import java.io.IOException;
  31 import java.io.ByteArrayInputStream;
  32 import java.security.cert.Certificate;
  33 import java.util.ArrayList;
  34 import java.util.Hashtable;
  35 import java.lang.reflect.*;
  36 import java.security.cert.*;
  37 import java.util.List;
  38 
  39 /**
  40  * The UnresolvedPermission class is used to hold Permissions that
  41  * were "unresolved" when the Policy was initialized.
  42  * An unresolved permission is one whose actual Permission class
  43  * does not yet exist at the time the Policy is initialized (see below).
  44  *
  45  * <p>The policy for a Java runtime (specifying
  46  * which permissions are available for code from various principals)
  47  * is represented by a Policy object.
  48  * Whenever a Policy is initialized or refreshed, Permission objects of
  49  * appropriate classes are created for all permissions
  50  * allowed by the Policy.
  51  *
  52  * <p>Many permission class types
  53  * referenced by the policy configuration are ones that exist
  54  * locally (i.e., ones that can be found on CLASSPATH).
  55  * Objects for such permissions can be instantiated during
  56  * Policy initialization. For example, it is always possible
  57  * to instantiate a java.io.FilePermission, since the


 536                 try {
 537                     oos.writeUTF(cert.getType());
 538                     byte[] encoded = cert.getEncoded();
 539                     oos.writeInt(encoded.length);
 540                     oos.write(encoded);
 541                 } catch (CertificateEncodingException cee) {
 542                     throw new IOException(cee.getMessage());
 543                 }
 544             }
 545         }
 546     }
 547 
 548     /**
 549      * Restores this object from a stream (i.e., deserializes it).
 550      */
 551     private void readObject(java.io.ObjectInputStream ois)
 552         throws IOException, ClassNotFoundException
 553     {
 554         CertificateFactory cf;
 555         Hashtable<String, CertificateFactory> cfs = null;
 556         List<Certificate> certList = null;
 557 
 558         ois.defaultReadObject();
 559 
 560         if (type == null)
 561                 throw new NullPointerException("type can't be null");
 562 
 563         // process any new-style certs in the stream (if present)
 564         int size = ois.readInt();
 565         if (size > 0) {
 566             // we know of 3 different cert types: X.509, PGP, SDSI, which
 567             // could all be present in the stream at the same time
 568             cfs = new Hashtable<>(3);
 569             certList = new ArrayList<>(size > 20 ? 20 : size);
 570         } else if (size < 0) {
 571             throw new IOException("size cannot be negative");
 572         }
 573 
 574         for (int i=0; i<size; i++) {
 575             // read the certificate type, and instantiate a certificate
 576             // factory of that type (reuse existing factory if possible)
 577             String certType = ois.readUTF();
 578             if (cfs.containsKey(certType)) {
 579                 // reuse certificate factory
 580                 cf = cfs.get(certType);
 581             } else {
 582                 // create new certificate factory
 583                 try {
 584                     cf = CertificateFactory.getInstance(certType);
 585                 } catch (CertificateException ce) {
 586                     throw new ClassNotFoundException
 587                         ("Certificate factory for "+certType+" not found");
 588                 }
 589                 // store the certificate factory so we can reuse it later
 590                 cfs.put(certType, cf);
 591             }
 592             // parse the certificate
 593             byte[] encoded = IOUtils.readNBytes(ois, ois.readInt());






 594             ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
 595             try {
 596                 certList.add(cf.generateCertificate(bais));
 597             } catch (CertificateException ce) {
 598                 throw new IOException(ce.getMessage());
 599             }
 600             bais.close();
 601         }
 602         if (certList != null) {
 603             this.certs = certList.toArray(
 604                     new java.security.cert.Certificate[size]);
 605         }
 606     }
 607 }
< prev index next >