src/share/classes/sun/security/util/ManifestEntryVerifier.java

Print this page




  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 sun.security.util;
  27 
  28 import java.security.*;
  29 import java.io.*;
  30 import java.security.CodeSigner;
  31 import java.util.*;
  32 import java.util.jar.*;
  33 
  34 import sun.misc.BASE64Decoder;
  35 
  36 import sun.security.jca.Providers;
  37 
  38 /**
  39  * This class is used to verify each entry in a jar file with its
  40  * manifest value.
  41  */
  42 
  43 public class ManifestEntryVerifier {
  44 
  45     private static final Debug debug = Debug.getInstance("jar");
  46 
  47     /**
  48      * Holder class to lazily load Sun provider. NOTE: if
  49      * Providers.getSunProvider returned a cached provider, we could avoid the
  50      * need for caching the provider with this holder class; we should try to
  51      * revisit this in JDK 8.
  52      */
  53     private static class SunProviderHolder {
  54         private static final Provider instance = Providers.getSunProvider();
  55     }
  56 
  57     /** the created digest objects */
  58     HashMap<String, MessageDigest> createdDigests;
  59 
  60     /** the digests in use for a given entry*/
  61     ArrayList<MessageDigest> digests;
  62 
  63     /** the manifest hashes for the digests in use */
  64     ArrayList<byte[]> manifestHashes;
  65 
  66     private BASE64Decoder decoder = null;
  67     private String name = null;
  68     private Manifest man;
  69 
  70     private boolean skip = true;
  71 
  72     private JarEntry entry;
  73 
  74     private CodeSigner[] signers = null;
  75 
  76     /**
  77      * Create a new ManifestEntryVerifier object.
  78      */
  79     public ManifestEntryVerifier(Manifest man)
  80     {
  81         createdDigests = new HashMap<String, MessageDigest>(11);
  82         digests = new ArrayList<MessageDigest>();
  83         manifestHashes = new ArrayList<byte[]>();
  84         decoder = new BASE64Decoder();
  85         this.man = man;
  86     }
  87 
  88     /**
  89      * Find the hashes in the
  90      * manifest for this entry, save them, and set the MessageDigest
  91      * objects to calculate the hashes on the fly. If name is
  92      * null it signifies that update/verify should ignore this entry.
  93      */
  94     public void setEntry(String name, JarEntry entry)
  95         throws IOException
  96     {
  97         digests.clear();
  98         manifestHashes.clear();
  99         this.name = name;
 100         this.entry = entry;
 101 
 102         skip = true;
 103         signers = null;
 104 


 130                 String algorithm = key.substring(0, key.length()-7);
 131 
 132                 MessageDigest digest = createdDigests.get(algorithm);
 133 
 134                 if (digest == null) {
 135                     try {
 136 
 137                         digest = MessageDigest.getInstance
 138                                         (algorithm, SunProviderHolder.instance);
 139                         createdDigests.put(algorithm, digest);
 140                     } catch (NoSuchAlgorithmException nsae) {
 141                         // ignore
 142                     }
 143                 }
 144 
 145                 if (digest != null) {
 146                     skip = false;
 147                     digest.reset();
 148                     digests.add(digest);
 149                     manifestHashes.add(
 150                                 decoder.decodeBuffer((String)se.getValue()));
 151                 }
 152             }
 153         }
 154     }
 155 
 156     /**
 157      * update the digests for the digests we are interested in
 158      */
 159     public void update(byte buffer) {
 160         if (skip) return;
 161 
 162         for (int i=0; i < digests.size(); i++) {
 163             digests.get(i).update(buffer);
 164         }
 165     }
 166 
 167     /**
 168      * update the digests for the digests we are interested in
 169      */
 170     public void update(byte buffer[], int off, int len) {




  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 sun.security.util;
  27 
  28 import java.security.*;
  29 import java.io.*;
  30 import java.security.CodeSigner;
  31 import java.util.*;
  32 import java.util.jar.*;
  33 
  34 import java.util.Base64;
  35 
  36 import sun.security.jca.Providers;
  37 
  38 /**
  39  * This class is used to verify each entry in a jar file with its
  40  * manifest value.
  41  */
  42 
  43 public class ManifestEntryVerifier {
  44 
  45     private static final Debug debug = Debug.getInstance("jar");
  46 
  47     /**
  48      * Holder class to lazily load Sun provider. NOTE: if
  49      * Providers.getSunProvider returned a cached provider, we could avoid the
  50      * need for caching the provider with this holder class; we should try to
  51      * revisit this in JDK 8.
  52      */
  53     private static class SunProviderHolder {
  54         private static final Provider instance = Providers.getSunProvider();
  55     }
  56 
  57     /** the created digest objects */
  58     HashMap<String, MessageDigest> createdDigests;
  59 
  60     /** the digests in use for a given entry*/
  61     ArrayList<MessageDigest> digests;
  62 
  63     /** the manifest hashes for the digests in use */
  64     ArrayList<byte[]> manifestHashes;
  65 

  66     private String name = null;
  67     private Manifest man;
  68 
  69     private boolean skip = true;
  70 
  71     private JarEntry entry;
  72 
  73     private CodeSigner[] signers = null;
  74 
  75     /**
  76      * Create a new ManifestEntryVerifier object.
  77      */
  78     public ManifestEntryVerifier(Manifest man)
  79     {
  80         createdDigests = new HashMap<String, MessageDigest>(11);
  81         digests = new ArrayList<MessageDigest>();
  82         manifestHashes = new ArrayList<byte[]>();

  83         this.man = man;
  84     }
  85 
  86     /**
  87      * Find the hashes in the
  88      * manifest for this entry, save them, and set the MessageDigest
  89      * objects to calculate the hashes on the fly. If name is
  90      * null it signifies that update/verify should ignore this entry.
  91      */
  92     public void setEntry(String name, JarEntry entry)
  93         throws IOException
  94     {
  95         digests.clear();
  96         manifestHashes.clear();
  97         this.name = name;
  98         this.entry = entry;
  99 
 100         skip = true;
 101         signers = null;
 102 


 128                 String algorithm = key.substring(0, key.length()-7);
 129 
 130                 MessageDigest digest = createdDigests.get(algorithm);
 131 
 132                 if (digest == null) {
 133                     try {
 134 
 135                         digest = MessageDigest.getInstance
 136                                         (algorithm, SunProviderHolder.instance);
 137                         createdDigests.put(algorithm, digest);
 138                     } catch (NoSuchAlgorithmException nsae) {
 139                         // ignore
 140                     }
 141                 }
 142 
 143                 if (digest != null) {
 144                     skip = false;
 145                     digest.reset();
 146                     digests.add(digest);
 147                     manifestHashes.add(
 148                                 Base64.getMimeDecoder().decode((String)se.getValue()));
 149                 }
 150             }
 151         }
 152     }
 153 
 154     /**
 155      * update the digests for the digests we are interested in
 156      */
 157     public void update(byte buffer) {
 158         if (skip) return;
 159 
 160         for (int i=0; i < digests.size(); i++) {
 161             digests.get(i).update(buffer);
 162         }
 163     }
 164 
 165     /**
 166      * update the digests for the digests we are interested in
 167      */
 168     public void update(byte buffer[], int off, int len) {