summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/apkzlib/sign/ManifestGenerationExtension.java
blob: 02e8f06e01faeb3870a17911c2354c51b2c94a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed 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 com.android.apkzlib.sign;

import com.android.apkzlib.utils.CachedSupplier;
import com.android.apkzlib.utils.IOExceptionRunnable;
import com.android.apkzlib.zfile.ManifestAttributes;
import com.android.apkzlib.zip.StoredEntry;
import com.android.apkzlib.zip.ZFile;
import com.android.apkzlib.zip.ZFileExtension;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import com.google.common.collect.Maps;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Extension to {@link ZFile} that will generate a manifest. The extension will register
 * automatically with the {@link ZFile}.
 *
 * <p>Creating this extension will ensure a manifest for the zip exists.
 * This extension will generate a manifest if one does not exist and will update an existing
 * manifest, if one does exist. The extension will also provide access to the manifest so that
 * others may update the manifest.
 *
 * <p>Apart from standard manifest elements, this extension does not handle any particular manifest
 * features such as signing or adding custom attributes. It simply generates a plain manifest and
 * provides infrastructure so that other extensions can add data in the manifest.
 *
 * <p>The manifest itself will only be written when the {@link ZFileExtension#beforeUpdate()}
 * notification is received, meaning all manifest manipulation is done in-memory.
 */
public class ManifestGenerationExtension {

    /**
     * Name of META-INF directory.
     */
    public static final String META_INF_DIR = "META-INF";

    /**
     * Name of the manifest file.
     */
    public static final String MANIFEST_NAME = META_INF_DIR + "/MANIFEST.MF";

    /**
     * Who should be reported as the manifest builder.
     */
    @Nonnull
    private final String mBuiltBy;

    /**
     * Who should be reported as the manifest creator.
     */
    @Nonnull
    private final String mCreatedBy;

    /**
     * The file this extension is attached to. {@code null} if not yet registered.
     */
    @Nullable
    private ZFile mZFile;

    /**
     * The zip file's manifest.
     */
    @Nonnull
    private final Manifest mManifest;

    /**
     * Byte representation of the manifest. There is no guarantee that two writes of the java's
     * {@code Manifest} object will yield the same byte array (there is no guaranteed order
     * of entries in the manifest).
     *
     * <p>Because we need the byte representation of the manifest to be stable if there are
     * no changes to the manifest, we cannot rely on {@code Manifest} to generate the byte
     * representation every time we need the byte representation.
     *
     * <p>This cache will ensure that we will request one byte generation from the {@code Manifest}
     * and will cache it. All further requests of the manifest's byte representation will
     * receive the same byte array.
     */
    @Nonnull
    private CachedSupplier<byte[]> mManifestBytes;

    /**
     * Has the current manifest been changed and not yet flushed? If {@link #mDirty} is
     * {@code true}, then {@link #mManifestBytes} should not be valid. This means that
     * marking the manifest as dirty should also invalidate {@link #mManifestBytes}. To avoid
     * breaking the invariant, instead of setting {@link #mDirty}, {@link #markDirty()} should
     * be called.
     */
    private boolean mDirty;

    /**
     * The extension to register with the {@link ZFile}. {@code null} if not registered.
     */
    @Nullable
    private ZFileExtension mExtension;

    /**
     * Creates a new extension. This will not register the extension with the provided
     * {@link ZFile}. Until {@link #register(ZFile)} is invoked, this extension is not used.
     *
     * @param builtBy who built the manifest?
     * @param createdBy who created the manifest?
     */
    public ManifestGenerationExtension(@Nonnull String builtBy, @Nonnull String createdBy) {
        mBuiltBy = builtBy;
        mCreatedBy = createdBy;
        mManifest = new Manifest();
        mDirty = false;
        mManifestBytes = new CachedSupplier<>(() -> {
            ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
            try {
                mManifest.write(outBytes);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }

            return outBytes.toByteArray();
        });
    }

    /**
     * Marks the manifest as being dirty, <i>i.e.</i>, its data has changed since it was last
     * read and/or written.
     */
    private void markDirty() {
        mDirty = true;
        mManifestBytes.reset();
    }

    /**
     * Registers the extension with the {@link ZFile} provided in the constructor.
     *
     * @param zFile the zip file to add the extension to
     * @throws IOException failed to analyze the zip
     */
    public void register(@Nonnull ZFile zFile) throws IOException {
        Preconditions.checkState(mExtension == null, "register() has already been invoked.");
        mZFile = zFile;

        rebuildManifest();

        mExtension = new ZFileExtension() {
            @Nullable
            @Override
            public IOExceptionRunnable beforeUpdate() {
                return ManifestGenerationExtension.this::updateManifest;
            }
        };

        mZFile.addZFileExtension(mExtension);
    }

    /**
     * Rebuilds the zip file's manifest, if it needs changes.
     */
    private void rebuildManifest() throws IOException {
        Verify.verifyNotNull(mZFile, "mZFile == null");

        StoredEntry manifestEntry = mZFile.get(MANIFEST_NAME);

        if (manifestEntry != null) {
            /*
             * Read the manifest entry in the zip file. Make sure we store these byte sequence
             * because writing the manifest may not generate the same byte sequence, which may
             * trigger an unnecessary re-sign of the jar.
             */
            mManifest.clear();
            byte[] manifestBytes = manifestEntry.read();
            mManifest.read(new ByteArrayInputStream(manifestBytes));
            mManifestBytes.precomputed(manifestBytes);
        }

        Attributes mainAttributes = mManifest.getMainAttributes();
        String currentVersion = mainAttributes.getValue(ManifestAttributes.MANIFEST_VERSION);
        if (currentVersion == null) {
            setMainAttribute(
                    ManifestAttributes.MANIFEST_VERSION,
                    ManifestAttributes.CURRENT_MANIFEST_VERSION);
        } else {
            if (!currentVersion.equals(ManifestAttributes.CURRENT_MANIFEST_VERSION)) {
                throw new IOException("Unsupported manifest version: " + currentVersion + ".");
            }
        }

        /*
         * We "blindly" override all other main attributes.
         */
        setMainAttribute(ManifestAttributes.BUILT_BY, mBuiltBy);
        setMainAttribute(ManifestAttributes.CREATED_BY, mCreatedBy);
    }

    /**
     * Sets the value of a main attribute.
     *
     * @param attribute the attribute
     * @param value the value
     */
    private void setMainAttribute(@Nonnull String attribute, @Nonnull String value) {
        Attributes mainAttributes = mManifest.getMainAttributes();
        String current = mainAttributes.getValue(attribute);
        if (!value.equals(current)) {
            mainAttributes.putValue(attribute, value);
            markDirty();
        }
    }

    /**
     * Updates the manifest in the zip file, if it has been changed.
     *
     * @throws IOException failed to update the manifest
     */
    private void updateManifest() throws IOException {
        Verify.verifyNotNull(mZFile, "mZFile == null");

        if (!mDirty) {
            return;
        }

        mZFile.add(MANIFEST_NAME, new ByteArrayInputStream(mManifestBytes.get()));
        mDirty = false;
    }

    /**
     * Obtains the {@link ZFile} this extension is associated with. This method can only be invoked
     * after {@link #register(ZFile)} has been invoked.
     *
     * @return the {@link ZFile}
     */
    @Nonnull
    public ZFile zFile() {
        Preconditions.checkNotNull(mZFile, "mZFile == null");
        return mZFile;
    }

    /**
     * Obtains the stored entry in the {@link ZFile} that contains the manifest. This method can
     * only be invoked after {@link #register(ZFile)} has been invoked.
     *
     * @return the entry, {@code null} if none
     */
    @Nullable
    public StoredEntry manifestEntry() {
        Preconditions.checkNotNull(mZFile, "mZFile == null");
        return mZFile.get(MANIFEST_NAME);
    }

    /**
     * Obtains an attribute of an entry.
     *
     * @param entryName the name of the entry
     * @param attr the name of the attribute
     * @return the attribute value or {@code null} if the entry does not have any attributes or
     * if it doesn't have the specified attribute
     */
    @Nullable
    public String getAttribute(@Nonnull String entryName, @Nonnull String attr) {
        Attributes attrs = mManifest.getAttributes(entryName);
        if (attrs == null) {
            return null;
        }

        return attrs.getValue(attr);
    }

    /**
     * Sets the value of an attribute of an entry. If this entry's attribute already has the given
     * value, this method does nothing.
     *
     * @param entryName the name of the entry
     * @param attr the name of the attribute
     * @param value the attribute value
     */
    public void setAttribute(@Nonnull String entryName, @Nonnull String attr,
            @Nonnull String value) {
        Attributes attrs = mManifest.getAttributes(entryName);
        if (attrs == null) {
            attrs = new Attributes();
            markDirty();
            mManifest.getEntries().put(entryName, attrs);
        }

        String current = attrs.getValue(attr);
        if (!value.equals(current)) {
            attrs.putValue(attr, value);
            markDirty();
        }
    }

    /**
     * Obtains the current manifest.
     *
     * @return a byte sequence representation of the manifest that is guaranteed not to change if
     * the manifest is not modified
     * @throws IOException failed to compute the manifest's byte representation
     */
    @Nonnull
    public byte[] getManifestBytes() throws IOException {
        return mManifestBytes.get();
    }

    /**
     * Obtains all entries and all attributes they have in the manifest.
     *
     * @return a map that relates entry names to entry attributes
     */
    @Nonnull
    public Map<String, Attributes> allEntries() {
        return Maps.newHashMap(mManifest.getEntries());
    }

    /**
     * Removes an entry from the manifest. If no entry exists with the given name, this operation
     * does nothing.
     *
     * @param name the entry's name
     */
    public void removeEntry(@Nonnull String name) {
        if (mManifest.getEntries().remove(name) != null) {
            markDirty();
        }
    }
}