KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > archive > zip > raes > AbstractZip32RaesDriver


1 /*
2  * Copyright 2006 Schlichtherle IT Services
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package de.schlichtherle.io.archive.zip.raes;
18
19 import de.schlichtherle.crypto.io.raes.*;
20 import de.schlichtherle.io.archive.*;
21 import de.schlichtherle.io.archive.spi.*;
22 import de.schlichtherle.io.archive.zip.*;
23 import de.schlichtherle.io.rof.*;
24
25 import java.io.*;
26 import java.util.zip.*;
27
28 import javax.swing.*;
29
30 /**
31  * An abstract archive driver which builds RAES encrypted ZIP32 archives
32  * and can authenticate the cipher data of the input archive files presented
33  * to it.
34  * <p>
35  * The constructor takes an optional authentication trigger parameter which
36  * can be used by subclasses to fine tune the authentication process.
37  * When omitted, the RAES Message Authentication Code (MAC) is <em>always</em>
38  * validated for the cipher text of input archive files.
39  *
40  * @see UnsafeZip32RaesDriver
41  * @see SafeZip32RaesDriver
42  * @author Christian Schlichtherle
43  * @version @version@
44  * @since TrueZIP 6.0
45  */

46 public abstract class AbstractZip32RaesDriver extends JarDriver {
47
48     private final long authenticationTrigger;
49
50     /**
51      * Creates an abstract ZIP32 RAES driver which <em>always</em>
52      * authenticates input archive files using the RAES Message
53      * Authentication Code (MAC).
54      */

55     protected AbstractZip32RaesDriver() {
56         this(Long.MAX_VALUE);
57     }
58
59     /**
60      * Creates an abstract ZIP32 RAES driver which uses the given
61      * authentication trigger.
62      * Note that the given parameter only affects the authentication of the
63      * <em>cipher text</em> in input archives - the <em>cipher key</em> and
64      * <em>file length</em> are always authenticated with RAES.
65      * The overhead in the RAES wrapper file format is subtracted from the
66      * file size which is compared to this trigger.
67      *
68      * @param authenticationTrigger For input archive files up to this size
69      * in bytes the cipher text gets authenticated using the RAES
70      * Message Authentication Code (MAC).
71      * If set to a negative value, input archive files
72      * <em>never</em> get authenticated.
73      * If set to {@link Long#MAX_VALUE}, input archive files
74      * <em>always</em> get authenticated.
75      */

76     protected AbstractZip32RaesDriver(final long authenticationTrigger) {
77         this.authenticationTrigger = authenticationTrigger;
78     }
79
80     /**
81      * Returns the authentication trigger provided to the constructor.
82      * Note that this method is final for security reasons.
83      */

84     public final long getAuthenticationTrigger() {
85         return authenticationTrigger;
86     }
87
88     public Icon getOpenIcon(Archive archive) {
89         return UIManager.getIcon("FileView.directoryIcon");
90     }
91
92     public Icon getClosedIcon(Archive archive) {
93         return UIManager.getIcon("FileView.directoryIcon");
94     }
95
96     /**
97      * Implementation of {@link ArchiveDriver#createInputArchive}.
98      * Note that this method is final for security reasons.
99      */

100     public final InputArchive createInputArchive(
101             final Archive archive,
102             final ReadOnlyFile rof)
103     throws UnsupportedEncodingException, FileNotFoundException, IOException {
104         final RaesReadOnlyFile rrof;
105         try {
106             rrof = RaesReadOnlyFile.getInstance(rof, getRaesParameters(archive));
107         } catch (RaesKeyException failure) {
108             throw new TransientIOException(failure);
109         }
110
111         if (rrof.length() <= getAuthenticationTrigger()) {
112             // Note: If authentication fails, this is reported through some
113
// sort of IOException, not a FileNotFoundException!
114
// This allows the client to treat the tampered archive like an
115
// ordinary file which may be read, written or deleted.
116
rrof.authenticate();
117         }
118
119         return createZip32InputArchive(
120                 rrof, getEncoding(), getPreambled(), getPostambled());
121     }
122
123     abstract protected Zip32InputArchive createZip32InputArchive(
124             ReadOnlyFile rof,
125             String JavaDoc encoding,
126             boolean preambled,
127             boolean postambled)
128     throws NullPointerException JavaDoc,
129             UnsupportedEncodingException,
130             FileNotFoundException,
131             ZipException,
132             IOException;
133
134     /**
135      * Implementation of {@link ArchiveDriver#createOutputArchive}.
136      * Note that this method is final for security reasons.
137      */

138     public final OutputArchive createOutputArchive(
139             final Archive archive,
140             final OutputStream out,
141             final InputArchive source)
142     throws UnsupportedEncodingException, IOException {
143         final RaesOutputStream ros;
144         try {
145             ros = RaesOutputStream.getInstance(out, getRaesParameters(archive));
146         } catch (RaesKeyException failure) {
147             throw new TransientIOException(failure);
148         }
149
150         return createZip32OutputArchive(
151                 ros, getEncoding(), (Zip32InputArchive) source);
152     }
153
154     abstract protected Zip32OutputArchive createZip32OutputArchive(
155             final OutputStream out,
156             final String JavaDoc encoding,
157             final Zip32InputArchive source)
158     throws NullPointerException JavaDoc,
159             UnsupportedEncodingException,
160             IOException;
161
162     /**
163      * Returns the {@link RaesParameters} for the given canonical path name.
164      *
165      * @param archive The abstract archive representation which TrueZIP's
166      * internal <code>ArchiveController</code> is processing
167      * - never <code>null</code>.
168      *
169      * @return The {@link RaesParameters} to use for accessing the
170      * prospective RAES encrypted ZIP file.
171      */

172     public RaesParameters getRaesParameters(Archive archive) {
173         return new KeyManagerRaesParameters(archive.getPath());
174     }
175 }
176
Popular Tags