commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r911781 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/zip/ test/java/org/apache/commons/compress/archivers/zip/ test/resources/
Date Fri, 19 Feb 2010 10:55:28 GMT
Author: bodewig
Date: Fri Feb 19 10:55:27 2010
New Revision: 911781

URL: http://svn.apache.org/viewvc?rev=911781&view=rev
Log:
detect encrypted entries and say that you can't read/write them.  COMPRESS-89

Added:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
  (with props)
    commons/proper/compress/trunk/src/test/resources/password-encrypted.zip   (with props)
Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java?rev=911781&r1=911780&r2=911781&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
Fri Feb 19 10:55:27 2010
@@ -74,6 +74,8 @@
     private LinkedHashMap/*<ZipShort, ZipExtraField>*/ extraFields = null;
     private UnparseableExtraFieldData unparseableExtra = null;
     private String name = null;
+    private boolean encrypted;
+    private boolean stronglyEncrypted;
 
     /**
      * Creates a new zip entry with the specified name.
@@ -491,6 +493,41 @@
     }
 
     /**
+     * Whether the entry is encrypted.
+     * @since Apache Commons Compress 1.1
+     */
+    public boolean isEncrypted() {
+        return encrypted;
+    }
+
+    /**
+     * Whether the entry is encrypted using strong encryption.
+     * @since Apache Commons Compress 1.1
+     */
+    public boolean isStronglyEncrypted() {
+        return stronglyEncrypted && encrypted;
+    }
+
+    /**
+     * Whether the entry is encrypted.
+     * @since Apache Commons Compress 1.1
+     */
+    public void setEncrypted(boolean b) {
+        encrypted = b;
+    }
+
+    /**
+     * Whether the entry is encrypted using strong encryption.
+     * @since Apache Commons Compress 1.1
+     */
+    public void setStronglyEncrypted(boolean b) {
+        stronglyEncrypted = b;
+        if (b) {
+            setEncrypted(true);
+        }
+    }
+
+    /**
      * If there are no extra fields, use the given fields as new extra
      * data - otherwise merge the fields assuming the existing fields
      * and the new fields stem from different locations inside the

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=911781&r1=911780&r2=911781&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
Fri Feb 19 10:55:27 2010
@@ -139,6 +139,8 @@
         final ZipEncoding entryEncoding =
             hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
         hasDataDescriptor = gpFlag.usesDataDescriptor();
+        current.setEncrypted(gpFlag.usesEncryption());
+        current.setStronglyEncrypted(gpFlag.usesStrongEncryption());
 
         off += SHORT;
 
@@ -187,6 +189,19 @@
         return getNextZipEntry();
     }
 
+    /**
+     * Whether this class is able to read the given entry.
+     *
+     * <p>May return false if it is set up to use encryption or a
+     * compression method that hasn't been implemented yet.</p>
+     * @since Apache Commons Compress 1.1
+     */
+    public boolean canRead(ZipArchiveEntry ze) {
+        return !ze.isEncrypted() &&
+            (ze.getMethod() == ZipArchiveEntry.STORED
+             || ze.getMethod() == ZipArchiveEntry.DEFLATED);
+    }
+
     public int read(byte[] buffer, int start, int length) throws IOException {
         if (closed) {
             throw new IOException("The stream is closed");
@@ -198,7 +213,12 @@
         // avoid int overflow, check null buffer
         if (start <= buffer.length && length >= 0 && start >= 0
             && buffer.length - start >= length) {
-            if (!current.isSupportedCompressionMethod()) {
+            if (current.isEncrypted()) {
+                throw new IOException("Encryption is not supported, used in "
+                                      + "entry " + current.getName());
+            }
+            if (current.getMethod() != ZipArchiveEntry.STORED
+                && current.getMethod() != ZipArchiveEntry.DEFLATED) {
                 throw new IOException(
                         "Unsupported compression method " + current.getMethod()
                         + " in ZIP archive entry " + current.getName());

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java?rev=911781&r1=911780&r2=911781&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
Fri Feb 19 10:55:27 2010
@@ -505,6 +505,18 @@
     }
 
     /**
+     * Whether this stream is able to write the given entry.
+     *
+     * <p>May return false if it is set up to use encryption or a
+     * compression method that hasn't been implemented yet.</p>
+     * @since Apache Commons Compress 1.1
+     */
+    public boolean canWrite(ZipArchiveEntry ze) {
+        return !ze.isEncrypted() &&
+            (ze.getMethod() == STORED || ze.getMethod() == DEFLATED);
+    }
+
+    /**
      * Writes bytes to ZIP entry.
      * @param b the byte array to write
      * @param offset the start position to write from
@@ -512,11 +524,16 @@
      * @throws IOException on error
      */
     public void write(byte[] b, int offset, int length) throws IOException {
-        if (!entry.isSupportedCompressionMethod()) {
+        if (entry.isEncrypted()) {
+            throw new IOException("Encryption is not supported, used in entry "
+                                  + entry.getName());
+        }
+        if (entry.getMethod() != STORED && entry.getMethod() != DEFLATED) {
             throw new IOException(
                     "Unsupported compression method " + entry.getMethod()
                     + " in ZIP archive entry " + entry.getName());
-        } else if (entry.getMethod() == DEFLATED) {
+        }
+        if (entry.getMethod() == DEFLATED) {
             if (length > 0) {
                 if (!def.finished()) {
                     if (length <= DEFLATER_BLOCK_SIZE) {

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=911781&r1=911780&r2=911781&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
Fri Feb 19 10:55:27 2010
@@ -244,6 +244,19 @@
     }
 
     /**
+     * Whether this class is able to read the given entry.
+     *
+     * <p>May return false if it is set up to use encryption or a
+     * compression method that hasn't been implemented yet.</p>
+     * @since Apache Commons Compress 1.1
+     */
+    public boolean canRead(ZipArchiveEntry ze) {
+        return !ze.isEncrypted() &&
+            (ze.getMethod() == ZipArchiveEntry.STORED
+             || ze.getMethod() == ZipArchiveEntry.DEFLATED);
+    }
+
+    /**
      * Returns an InputStream for reading the contents of the given entry.
      * @param ze the entry to get the stream for.
      * @return a stream to read the entry from.
@@ -257,6 +270,16 @@
         if (offsetEntry == null) {
             return null;
         }
+        if (ze.isEncrypted()) {
+            throw new IOException("Encryption is not supported, used in "
+                                  + "entry " + ze.getName());
+        }
+        if (ze.getMethod() != ZipArchiveEntry.STORED
+            && ze.getMethod() != ZipArchiveEntry.DEFLATED) {
+            throw new IOException("Unsupported compression method "
+                                  + ze.getMethod() + " in ZIP archive entry "
+                                  + ze.getName());
+        }
         long start = offsetEntry.dataOffset;
         BoundedInputStream bis =
             new BoundedInputStream(start, ze.getCompressedSize());
@@ -333,6 +356,8 @@
             final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
             final ZipEncoding entryEncoding =
                 hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
+            ze.setEncrypted(gpFlag.usesEncryption());
+            ze.setStronglyEncrypted(gpFlag.usesStrongEncryption());
 
             off += SHORT;
 

Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java?rev=911781&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
(added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
Fri Feb 19 10:55:27 2010
@@ -0,0 +1,78 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.commons.compress.archivers.zip;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import junit.framework.TestCase;
+
+public class EncryptedArchiveTest extends TestCase {
+
+    public void testReadPasswordEncryptedEntryViaZipFile()
+        throws IOException, URISyntaxException {
+	URL zip = getClass().getResource("/password-encrypted.zip");
+        File file = new File(new URI(zip.toString()));
+        ZipFile zf = null;
+        try {
+            zf = new ZipFile(file);
+            ZipArchiveEntry zae = zf.getEntry("LICENSE.txt");
+            assertTrue(zae.isEncrypted());
+            assertFalse(zae.isStronglyEncrypted());
+            assertFalse(zf.canRead(zae));
+            try {
+                zf.getInputStream(zae);
+                fail("expected an exception");
+            } catch (IOException ex) {
+                assertTrue(ex.getMessage().indexOf("Encryption") >= 0);
+            }
+        } finally {
+            ZipFile.closeQuietly(zf);
+        }
+    }
+
+    public void testReadPasswordEncryptedEntryViaStream()
+        throws IOException, URISyntaxException {
+	URL zip = getClass().getResource("/password-encrypted.zip");
+        File file = new File(new URI(zip.toString()));
+        ZipArchiveInputStream zin = null;
+        try {
+            zin = new ZipArchiveInputStream(new FileInputStream(file));
+            ZipArchiveEntry zae = zin.getNextZipEntry();
+            assertEquals("LICENSE.txt", zae.getName());
+            assertTrue(zae.isEncrypted());
+            assertFalse(zae.isStronglyEncrypted());
+            assertFalse(zin.canRead(zae));
+            try {
+                byte[] buf = new byte[1024];
+                zin.read(buf, 0, buf.length);
+                fail("expected an exception");
+            } catch (IOException ex) {
+                assertTrue(ex.getMessage().indexOf("Encryption") >= 0);
+            }
+        } finally {
+            if (zin != null) {
+                zin.close();
+            }
+        }
+    }
+}

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/compress/trunk/src/test/resources/password-encrypted.zip
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/resources/password-encrypted.zip?rev=911781&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/compress/trunk/src/test/resources/password-encrypted.zip
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message