test/jdk/sun/security/tools/jarsigner/PreserveRawManifestEntryAndDigest.java
author weijun
Thu, 18 Jul 2019 08:53:06 +0800
changeset 57488 94691d8e746f
permissions -rw-r--r--
8217375: jarsigner breaks old signature with long lines in manifest Reviewed-by: jjiang, weijun Contributed-by: Philipp Kunz <philipp.kunz@paratix.ch>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     1
/*
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     4
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     8
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    13
 * accompanied this code).
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    14
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    18
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    21
 * questions.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    22
 */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    23
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    25
import java.io.File;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    26
import java.io.FilterOutputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    27
import java.io.IOException;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    28
import java.io.InputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    29
import java.io.OutputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    30
import java.io.PrintStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    31
import java.nio.file.Files;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    32
import java.nio.file.Path;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    33
import java.util.ArrayList;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    34
import java.util.Arrays;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    35
import java.util.List;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    36
import java.util.Map;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    37
import java.util.Collections;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    38
import java.util.stream.Collectors;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    39
import java.util.function.Function;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    40
import java.util.jar.Attributes;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    41
import java.util.jar.Attributes.Name;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    42
import java.util.jar.Manifest;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    43
import java.util.jar.JarEntry;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    44
import java.util.jar.JarFile;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    45
import java.util.zip.ZipFile;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    46
import java.util.zip.ZipEntry;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    47
import jdk.test.lib.process.OutputAnalyzer;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    48
import jdk.test.lib.Platform;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    49
import jdk.test.lib.SecurityTools;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    50
import jdk.test.lib.util.JarUtils;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    51
import org.testng.annotations.BeforeTest;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    52
import org.testng.annotations.DataProvider;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    53
import org.testng.annotations.Test;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    54
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    55
import static java.nio.charset.StandardCharsets.UTF_8;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    56
import static org.testng.Assert.*;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    57
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    58
/**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    59
 * @test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    60
 * @bug 8217375
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    61
 * @library /test/lib
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    62
 * @modules jdk.jartool/sun.security.tools.jarsigner
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    63
 * @run testng/timeout=1200 PreserveRawManifestEntryAndDigest
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    64
 * @summary Verifies that JarSigner does not change manifest file entries
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    65
 * in a binary view if its decoded map view does not change so that an
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    66
 * unchanged (individual section) entry continues to produce the same digest.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    67
 * The same manifest (in terms of {@link Manifest#equals}) could be encoded
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    68
 * with different line breaks ("{@code \r}", "{@code \n}", or "{@code \r\n}")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    69
 * or with arbitrary line break positions (as is also the case with the change
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    70
 * of the default line width in JDK 11, bug 6372077) resulting in a different
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    71
 * digest for manifest entries with identical values.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    72
 *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    73
 * <p>See also:<ul>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    74
 * <li>{@code oldsig.sh} and {@code diffend.sh} in
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    75
 * {@code /test/jdk/sun/security/tools/jarsigner/}</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    76
 * <li>{@code Compatibility.java} in
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    77
 * {@code /test/jdk/sun/security/tools/jarsigner/compatibility}</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    78
 * <li>{@link ReproduceRaw} testing relevant
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    79
 * {@sun.security.util.ManifestDigester} api in much more detail</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    80
 * </ul>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    81
 */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    82
/*
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    83
 * debug with "run testng" += "/othervm -Djava.security.debug=jar"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    84
 */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    85
public class PreserveRawManifestEntryAndDigest {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    86
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    87
    static final String KEYSTORE_FILENAME = "test.jks";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    88
    static final String FILENAME_INITIAL_CONTENTS = "initial-contents";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    89
    static final String FILENAME_UPDATED_CONTENTS = "updated-contents";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    90
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    91
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    92
     * @see sun.security.tools.jarsigner.Main#run
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    93
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    94
    static final int NOTSIGNEDBYALIASORALIASNOTINSTORE = 32;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    95
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    96
    @BeforeTest
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    97
    public void prepareContentFiles() throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    98
        Files.write(Path.of(FILENAME_INITIAL_CONTENTS),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
    99
                FILENAME_INITIAL_CONTENTS.getBytes(UTF_8));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   100
        Files.write(Path.of(FILENAME_UPDATED_CONTENTS),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   101
                FILENAME_UPDATED_CONTENTS.getBytes(UTF_8));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   102
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   103
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   104
    @BeforeTest
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   105
    public void prepareCertificates() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   106
        SecurityTools.keytool("-genkeypair -keyalg DSA -keystore "
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   107
                + KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   108
                + " -alias a -dname CN=A").shouldHaveExitValue(0);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   109
        SecurityTools.keytool("-genkeypair -keyalg DSA -keystore "
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   110
                + KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   111
                + " -alias b -dname CN=B").shouldHaveExitValue(0);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   112
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   113
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   114
    static class TeeOutputStream extends FilterOutputStream {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   115
        final OutputStream tee; // don't flush or close
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   116
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   117
        public TeeOutputStream(OutputStream out, OutputStream tee) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   118
            super(out);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   119
            this.tee = tee;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   120
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   121
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   122
        @Override
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   123
        public void write(int b) throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   124
            super.write(b);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   125
            tee.write(b);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   126
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   127
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   128
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   129
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   130
     * runs jarsigner in its own child process and captures exit code and the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   131
     * output of stdout and stderr, as opposed to {@link #karsignerMain}
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   132
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   133
    OutputAnalyzer jarsignerProc(String args) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   134
        long start = System.currentTimeMillis();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   135
        try {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   136
            return SecurityTools.jarsigner(args);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   137
        } finally {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   138
            long end = System.currentTimeMillis();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   139
            System.out.println("jarsignerProc duration [ms]: " + (end - start));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   140
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   141
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   142
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   143
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   144
     * assume non-zero exit code would call System.exit but is faster than
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   145
     * {@link #jarsignerProc}
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   146
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   147
    void jarsignerMain(String args) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   148
        long start = System.currentTimeMillis();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   149
        try {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   150
            new sun.security.tools.jarsigner.Main().run(args.split("\\s+"));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   151
        } finally {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   152
            long end = System.currentTimeMillis();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   153
            System.out.println("jarsignerMain duration [ms]: " + (end - start));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   154
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   155
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   156
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   157
    void createSignedJarA(String jarFilename, Manifest manifest,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   158
            String additionalJarsignerOptions, String dummyContentsFilename)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   159
                    throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   160
        JarUtils.createJarFile(Path.of(jarFilename), manifest, Path.of("."),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   161
                dummyContentsFilename == null ? new Path[]{} :
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   162
                    new Path[] { Path.of(dummyContentsFilename) });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   163
        jarsignerMain("-keystore " + KEYSTORE_FILENAME + " -storepass changeit"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   164
                + (additionalJarsignerOptions == null ? "" :
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   165
                    " " + additionalJarsignerOptions) +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   166
                " -verbose -debug " + jarFilename + " a");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   167
        Utils.echoManifest(Utils.readJarManifestBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   168
                jarFilename), "original signed jar by signer a");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   169
        // check assumption that jar is valid at this point
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   170
        jarsignerMain("-verify -keystore " + KEYSTORE_FILENAME +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   171
                " -storepass changeit -verbose -debug " + jarFilename + " a");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   172
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   173
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   174
    void manipulateManifestSignAgainA(String srcJarFilename, String tmpFilename,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   175
            String dstJarFilename, String additionalJarsignerOptions,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   176
            Function<Manifest, byte[]> manifestManipulation) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   177
        Manifest mf;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   178
        try (JarFile jar = new JarFile(srcJarFilename)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   179
            mf = jar.getManifest();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   180
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   181
        byte[] manipulatedManifest = manifestManipulation.apply(mf);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   182
        Utils.echoManifest(manipulatedManifest, "manipulated manifest");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   183
        JarUtils.updateJar(srcJarFilename, tmpFilename, Map.of(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   184
                JarFile.MANIFEST_NAME, manipulatedManifest,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   185
                // add a fake sig-related file to trigger wasSigned in JarSigner
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   186
                "META-INF/.SF", Name.SIGNATURE_VERSION + ": 1.0\r\n"));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   187
        jarsignerMain("-keystore " + KEYSTORE_FILENAME + " -storepass changeit"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   188
                + (additionalJarsignerOptions == null ? "" :
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   189
                    " " + additionalJarsignerOptions) +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   190
                " -verbose -debug " + tmpFilename + " a");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   191
        // remove META-INF/.SF from signed jar again which would not validate
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   192
        JarUtils.updateJar(tmpFilename, dstJarFilename,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   193
                Map.of("META-INF/.SF", false));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   194
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   195
        Utils.echoManifest(Utils.readJarManifestBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   196
                dstJarFilename), "manipulated jar signed again with a");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   197
        // check assumption that jar is valid at this point
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   198
        jarsignerMain("-verify -keystore " + KEYSTORE_FILENAME + " " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   199
                "-storepass changeit -verbose -debug " + dstJarFilename + " a");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   200
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   201
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   202
    OutputAnalyzer signB(String jarFilename, String additionalJarsignerOptions,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   203
            int updateExitCodeVerifyA) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   204
        jarsignerMain("-keystore " + KEYSTORE_FILENAME + " -storepass changeit"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   205
                + (additionalJarsignerOptions == null ? "" :
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   206
                    " " + additionalJarsignerOptions)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   207
                + " -verbose -debug " + jarFilename + " b");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   208
        Utils.echoManifest(Utils.readJarManifestBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   209
                jarFilename), "signed again with signer b");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   210
        // check assumption that jar is valid at this point with any alias
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   211
        jarsignerMain("-verify -strict -keystore " + KEYSTORE_FILENAME +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   212
                " -storepass changeit -debug -verbose " + jarFilename);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   213
        // check assumption that jar is valid at this point with b just signed
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   214
        jarsignerMain("-verify -strict -keystore " + KEYSTORE_FILENAME +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   215
                " -storepass changeit -debug -verbose " + jarFilename + " b");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   216
        // return result of verification of signature by a before update
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   217
        return jarsignerProc("-verify -strict " + "-keystore " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   218
                KEYSTORE_FILENAME + " -storepass changeit " + "-debug " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   219
                "-verbose " + jarFilename + " a")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   220
                .shouldHaveExitValue(updateExitCodeVerifyA);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   221
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   222
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   223
    String[] fromFirstToSecondEmptyLine(String[] lines) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   224
        int from = 0;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   225
        for (int i = 0; i < lines.length; i++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   226
            if ("".equals(lines[i])) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   227
                from = i + 1;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   228
                break;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   229
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   230
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   231
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   232
        int to = lines.length - 1;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   233
        for (int i = from; i < lines.length; i++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   234
            if ("".equals(lines[i])) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   235
                to = i - 1;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   236
                break;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   237
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   238
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   239
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   240
        return Arrays.copyOfRange(lines, from, to + 1);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   241
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   242
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   243
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   244
     * @see "concise_jarsigner.sh"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   245
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   246
    String[] getExpectedJarSignerOutputUpdatedContentNotValidatedBySignerA(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   247
            String jarFilename, String digestalg,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   248
            String firstAddedFilename, String secondAddedFilename) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   249
        final String TS = ".{28,29}"; // matches a timestamp
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   250
        List<String> expLines = new ArrayList<>();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   251
        expLines.add("s k   *\\d+ " + TS + " META-INF/MANIFEST[.]MF");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   252
        expLines.add("      *\\d+ " + TS + " META-INF/B[.]SF");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   253
        expLines.add("      *\\d+ " + TS + " META-INF/B[.]DSA");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   254
        expLines.add("      *\\d+ " + TS + " META-INF/A[.]SF");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   255
        expLines.add("      *\\d+ " + TS + " META-INF/A[.]DSA");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   256
        if (firstAddedFilename != null) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   257
            expLines.add("smk   *\\d+ " + TS + " " + firstAddedFilename);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   258
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   259
        if (secondAddedFilename != null) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   260
            expLines.add("smkX  *\\d+ " + TS + " " + secondAddedFilename);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   261
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   262
        return expLines.toArray(new String[expLines.size()]);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   263
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   264
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   265
    void assertMatchByLines(String[] actLines, String[] expLines) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   266
        for (int i = 0; i < actLines.length && i < expLines.length; i++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   267
            String actLine = actLines[i];
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   268
            String expLine = expLines[i];
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   269
            assertTrue(actLine.matches("^" + expLine + "$"),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   270
                "\"" + actLine + "\" should have matched \"" + expLine + "\"");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   271
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   272
        assertEquals(actLines.length, expLines.length);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   273
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   274
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   275
    String test(String name, Function<Manifest, byte[]> mm) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   276
        return test(name, FILENAME_INITIAL_CONTENTS, FILENAME_UPDATED_CONTENTS,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   277
                mm);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   278
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   279
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   280
    String test(String name,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   281
            String firstAddedFilename, String secondAddedFilename,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   282
            Function<Manifest, byte[]> mm) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   283
        return test(name, firstAddedFilename, secondAddedFilename, mm, null,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   284
                true, true);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   285
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   286
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   287
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   288
     * Essentially, creates a first signed JAR file with a single contained
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   289
     * file or without and a manipulation applied to its manifest signed by
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   290
     * signer a and then signes it again with a different signer b.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   291
     * The jar file is signed twice with signer a in order to make the digests
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   292
     * available to the manipulation function that might use it.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   293
     *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   294
     * @param name Prefix for the JAR filenames used throughout the test.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   295
     * @param firstAddedFilename Name of a file to add before the first
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   296
     * signature by signer a or null. The name will also become the contents
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   297
     * if not null.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   298
     * @param secondAddedFilename Name of a file to add after the first
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   299
     * signature by signer a and before the second signature by signer b or
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   300
     * null. The name will also become the contents if not null.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   301
     * @param manifestManipulation A callback hook to manipulate the manifest
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   302
     * after the first signature by signer a and before the second signature by
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   303
     * signer b.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   304
     * @param digestalg The digest algorithm name to be used or null for
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   305
     * default.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   306
     * @param assertMainAttrsDigestsUnchanged Assert that the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   307
     * manifest main attributes digests have not changed. In any case the test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   308
     * also checks that the digests are still valid whether changed or not
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   309
     * by {@code jarsigner -verify} which might use
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   310
     * {@link ManifestDigester.Entry#digestWorkaround}
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   311
     * @param assertFirstAddedFileDigestsUnchanged Assert that the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   312
     * digest of the file firstAddedFilename has not changed with the second
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   313
     * signature. In any case the test checks that the digests are valid whether
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   314
     * changed or not by {@code jarsigner -verify} which might use
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   315
     * {@link ManifestDigester.Entry#digestWorkaround}
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   316
     * @return The name of the resulting JAR file that has passed the common
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   317
     * assertions ready for further examination
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   318
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   319
    String test(String name,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   320
            String firstAddedFilename, String secondAddedFilename,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   321
            Function<Manifest, byte[]> manifestManipulation,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   322
            String digestalg, boolean assertMainAttrsDigestsUnchanged,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   323
            boolean assertFirstAddedFileDigestsUnchanged)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   324
                    throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   325
        String digOpts = (digestalg != null ? "-digestalg " + digestalg : "");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   326
        String jarFilename1 = "test-" + name + "-step1.jar";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   327
        createSignedJarA(jarFilename1,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   328
                /* no manifest will let jarsigner create a default one */ null,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   329
                digOpts, firstAddedFilename);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   330
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   331
        // manipulate the manifest, write it back, and sign the jar again with
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   332
        // the same certificate a as before overwriting the first signature
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   333
        String jarFilename2 = "test-" + name + "-step2.jar";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   334
        String jarFilename3 = "test-" + name + "-step3.jar";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   335
        manipulateManifestSignAgainA(jarFilename1, jarFilename2, jarFilename3,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   336
                digOpts, manifestManipulation);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   337
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   338
        // add another file, sign it with the other certificate, and verify it
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   339
        String jarFilename4 = "test-" + name + "-step4.jar";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   340
        JarUtils.updateJar(jarFilename3, jarFilename4,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   341
                secondAddedFilename != null ?
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   342
                Map.of(secondAddedFilename, secondAddedFilename)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   343
                : Collections.EMPTY_MAP);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   344
        OutputAnalyzer o = signB(jarFilename4, digOpts,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   345
           secondAddedFilename != null ? NOTSIGNEDBYALIASORALIASNOTINSTORE : 0);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   346
        // check that secondAddedFilename is the only entry which is not signed
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   347
        // by signer with alias "a" unless secondAddedFilename is null
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   348
        assertMatchByLines(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   349
                fromFirstToSecondEmptyLine(o.getStdout().split("\\R")),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   350
                getExpectedJarSignerOutputUpdatedContentNotValidatedBySignerA(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   351
                        jarFilename4, digestalg,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   352
                        firstAddedFilename, secondAddedFilename));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   353
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   354
        // double-check reading the files with a verifying JarFile
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   355
        try (JarFile jar = new JarFile(jarFilename4, true)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   356
            if (firstAddedFilename != null) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   357
                JarEntry je1 = jar.getJarEntry(firstAddedFilename);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   358
                jar.getInputStream(je1).readAllBytes();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   359
                assertTrue(je1.getCodeSigners().length > 0);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   360
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   361
            if (secondAddedFilename != null) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   362
                JarEntry je2 = jar.getJarEntry(secondAddedFilename);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   363
                jar.getInputStream(je2).readAllBytes();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   364
                assertTrue(je2.getCodeSigners().length > 0);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   365
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   366
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   367
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   368
        // assert that the signature of firstAddedFilename signed by signer
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   369
        // with alias "a" is not lost and its digest remains the same
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   370
        try (ZipFile zip = new ZipFile(jarFilename4)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   371
            ZipEntry ea = zip.getEntry("META-INF/A.SF");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   372
            Manifest sfa = new Manifest(zip.getInputStream(ea));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   373
            ZipEntry eb = zip.getEntry("META-INF/B.SF");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   374
            Manifest sfb = new Manifest(zip.getInputStream(eb));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   375
            if (assertMainAttrsDigestsUnchanged) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   376
                String mainAttrsDigKey =
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   377
                        (digestalg != null ? digestalg : "SHA-256") +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   378
                        "-Digest-Manifest-Main-Attributes";
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   379
                assertEquals(sfa.getMainAttributes().getValue(mainAttrsDigKey),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   380
                             sfb.getMainAttributes().getValue(mainAttrsDigKey));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   381
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   382
            if (assertFirstAddedFileDigestsUnchanged) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   383
                assertEquals(sfa.getAttributes(firstAddedFilename),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   384
                             sfb.getAttributes(firstAddedFilename));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   385
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   386
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   387
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   388
        return jarFilename4;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   389
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   390
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   391
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   392
     * Test that signing a jar with manifest entries with arbitrary line break
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   393
     * positions in individual section headers does not destroy an existing
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   394
     * signature<ol>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   395
     * <li>create two self-signed certificates</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   396
     * <li>sign a jar with at least one non-META-INF file in it with a JDK
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   397
     * before 11 or place line breaks not at 72 bytes in an individual section
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   398
     * header</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   399
     * <li>add a new file to the jar</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   400
     * <li>sign the jar with a JDK 11, 12, or 13 with bug 8217375 not yet
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   401
     * resolved with a different signer</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   402
     * </ol>&rarr; first signature will not validate anymore even though it
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   403
     * should.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   404
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   405
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   406
    public void arbitraryLineBreaksSectionName() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   407
        test("arbitraryLineBreaksSectionName", m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   408
            return (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   409
                Name.MANIFEST_VERSION + ": 1.0\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   410
                "Created-By: " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   411
                        m.getMainAttributes().getValue("Created-By") + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   412
                "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   413
                "Name: Test\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   414
                " -\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   415
                " Section\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   416
                "Key: Value \r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   417
                "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   418
                "Name: " + FILENAME_INITIAL_CONTENTS.substring(0, 1) + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   419
                " " + FILENAME_INITIAL_CONTENTS.substring(1, 8) + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   420
                " " + FILENAME_INITIAL_CONTENTS.substring(8) + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   421
                "SHA-256-Digest: " + m.getAttributes(FILENAME_INITIAL_CONTENTS)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   422
                        .getValue("SHA-256-Digest") + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   423
                "\r\n"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   424
            ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   425
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   426
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   427
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   428
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   429
     * Test that signing a jar with manifest entries with arbitrary line break
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   430
     * positions in individual section headers does not destroy an existing
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   431
     * signature<ol>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   432
     * <li>create two self-signed certificates</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   433
     * <li>sign a jar with at least one non-META-INF file in it with a JDK
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   434
     * before 11 or place line breaks not at 72 bytes in an individual section
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   435
     * header</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   436
     * <li>add a new file to the jar</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   437
     * <li>sign the jar with a JDK 11 or 12 with a different signer</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   438
     * </ol>&rarr; first signature will not validate anymore even though it
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   439
     * should.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   440
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   441
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   442
    public void arbitraryLineBreaksHeader() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   443
        test("arbitraryLineBreaksHeader", m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   444
            String digest = m.getAttributes(FILENAME_INITIAL_CONTENTS)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   445
                    .getValue("SHA-256-Digest");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   446
            return (
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   447
                Name.MANIFEST_VERSION + ": 1.0\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   448
                "Created-By: " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   449
                        m.getMainAttributes().getValue("Created-By") + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   450
                "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   451
                "Name: Test-Section\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   452
                "Key: Value \r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   453
                " with\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   454
                "  strange \r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   455
                " line breaks.\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   456
                "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   457
                "Name: " + FILENAME_INITIAL_CONTENTS + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   458
                "SHA-256-Digest: " + digest.substring(0, 11) + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   459
                " " + digest.substring(11) + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   460
                "\r\n"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   461
            ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   462
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   463
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   464
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   465
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   466
     * Breaks {@code line} at 70 bytes even though the name says 72 but when
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   467
     * also counting the line delimiter ("{@code \r\n}") the line totals to 72
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   468
     * bytes.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   469
     * Borrowed from {@link Manifest#make72Safe} before JDK 11
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   470
     *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   471
     * @see Manifest#make72Safe
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   472
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   473
    static void make72Safe(StringBuffer line) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   474
        int length = line.length();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   475
        if (length > 72) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   476
            int index = 70;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   477
            while (index < length - 2) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   478
                line.insert(index, "\r\n ");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   479
                index += 72;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   480
                length += 3;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   481
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   482
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   483
        return;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   484
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   485
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   486
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   487
     * Test that signing a jar with manifest entries with line breaks at
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   488
     * position where Manifest would not place them now anymore (72 instead of
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   489
     * 70 bytes after line starts) does not destroy an existing signature<ol>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   490
     * <li>create two self-signed certificates</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   491
     * <li>simulate a manifest as it would have been written by a JDK before 11
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   492
     * by re-positioning line breaks at 70 bytes (which makes a difference by
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   493
     * digests that grow headers longer than 70 characters such as SHA-512 as
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   494
     * opposed to default SHA-256, long file names, or manual editing)</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   495
     * <li>add a new file to the jar</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   496
     * <li>sign the jar with a JDK 11 or 12 with a different signer</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   497
     * </ol><p>&rarr;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   498
     * The first signature will not validate anymore even though it should.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   499
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   500
    public void lineWidth70(String name, String digestalg) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   501
        Files.write(Path.of(name), name.getBytes(UTF_8));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   502
        test(name, name, FILENAME_UPDATED_CONTENTS, m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   503
            // force a line break with a header exceeding line width limit
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   504
            m.getEntries().put("Test-Section", new Attributes());
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   505
            m.getAttributes("Test-Section").put(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   506
                    Name.IMPLEMENTATION_VERSION, "1" + "0".repeat(100));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   507
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   508
            StringBuilder sb = new StringBuilder();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   509
            StringBuffer[] buf = new StringBuffer[] { null };
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   510
            manifestToString(m).lines().forEach(line -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   511
                if (line.startsWith(" ")) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   512
                    buf[0].append(line.substring(1));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   513
                } else {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   514
                    if (buf[0] != null) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   515
                        make72Safe(buf[0]);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   516
                        sb.append(buf[0].toString());
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   517
                        sb.append("\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   518
                    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   519
                    buf[0] = new StringBuffer();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   520
                    buf[0].append(line);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   521
                }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   522
            });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   523
            make72Safe(buf[0]);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   524
            sb.append(buf[0].toString());
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   525
            sb.append("\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   526
            return sb.toString().getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   527
        }, digestalg, false, false);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   528
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   529
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   530
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   531
    public void lineWidth70Filename() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   532
        lineWidth70(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   533
            "lineWidth70".repeat(6) /* 73 chars total with "Name: " */, null);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   534
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   535
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   536
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   537
    public void lineWidth70Digest() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   538
        lineWidth70("lineWidth70digest", "SHA-512");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   539
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   540
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   541
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   542
     * Test that signing a jar with a manifest with line delimiter other than
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   543
     * "{@code \r\n}" does not destroy an existing signature<ol>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   544
     * <li>create two self-signed certificates</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   545
     * <li>sign a jar with at least one non-META-INF file in it</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   546
     * <li>extract the manifest, and change its line delimiters
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   547
     * (for example dos2unix)</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   548
     * <li>update the jar with the updated manifest</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   549
     * <li>sign it again with the same signer as before</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   550
     * <li>add a new file to the jar</li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   551
     * <li>sign the jar with a JDK before 13 with a different signer<li>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   552
     * </ol><p>&rarr;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   553
     * The first signature will not validate anymore even though it should.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   554
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   555
    public void lineBreak(String lineBreak) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   556
        test("lineBreak" + byteArrayToIntList(lineBreak.getBytes(UTF_8)).stream
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   557
                ().map(i -> "" + i).collect(Collectors.joining("")), m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   558
            StringBuilder sb = new StringBuilder();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   559
            manifestToString(m).lines().forEach(l -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   560
                sb.append(l);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   561
                sb.append(lineBreak);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   562
            });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   563
            return sb.toString().getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   564
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   565
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   566
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   567
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   568
    public void lineBreakCr() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   569
        lineBreak("\r");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   570
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   571
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   572
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   573
    public void lineBreakLf() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   574
        lineBreak("\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   575
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   576
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   577
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   578
    public void lineBreakCrLf() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   579
        lineBreak("\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   580
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   581
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   582
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   583
    public void testAdjacentRepeatedSection() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   584
        test("adjacent", m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   585
            return (manifestToString(m) +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   586
                    "Name: " + FILENAME_INITIAL_CONTENTS + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   587
                    "Foo: Bar\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   588
                    "\r\n"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   589
            ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   590
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   591
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   592
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   593
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   594
    public void testIntermittentRepeatedSection() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   595
        test("intermittent", m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   596
            return (manifestToString(m) +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   597
                    "Name: don't know\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   598
                    "Foo: Bar\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   599
                    "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   600
                    "Name: " + FILENAME_INITIAL_CONTENTS + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   601
                    "Foo: Bar\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   602
                    "\r\n"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   603
            ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   604
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   605
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   606
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   607
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   608
    public void testNameImmediatelyContinued() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   609
        test("testNameImmediatelyContinued", m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   610
            // places a continuation line break and space at the first allowed
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   611
            // position after ": " and before the first character of the value
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   612
            return (manifestToString(m).replaceAll(FILENAME_INITIAL_CONTENTS,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   613
                    "\r\n " + FILENAME_INITIAL_CONTENTS + "\r\nFoo: Bar")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   614
            ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   615
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   616
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   617
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   618
    /*
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   619
     * "malicious" '\r' after continuation line continued
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   620
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   621
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   622
    public void testNameContinuedContinuedWithCr() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   623
        test("testNameContinuedContinuedWithCr", m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   624
            return (manifestToString(m).replaceAll(FILENAME_INITIAL_CONTENTS,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   625
                    FILENAME_INITIAL_CONTENTS.substring(0, 1) + "\r\n " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   626
                    FILENAME_INITIAL_CONTENTS.substring(1, 4) + "\r " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   627
                    FILENAME_INITIAL_CONTENTS.substring(4) + "\r\n" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   628
                    "Foo: Bar")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   629
            ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   630
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   631
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   632
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   633
    /*
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   634
     * "malicious" '\r' after continued continuation line
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   635
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   636
    @Test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   637
    public void testNameContinuedContinuedEndingWithCr() throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   638
        test("testNameContinuedContinuedEndingWithCr", m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   639
            return (manifestToString(m).replaceAll(FILENAME_INITIAL_CONTENTS,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   640
                    FILENAME_INITIAL_CONTENTS.substring(0, 1) + "\r\n " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   641
                    FILENAME_INITIAL_CONTENTS.substring(1, 4) + "\r\n " +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   642
                    FILENAME_INITIAL_CONTENTS.substring(4) + "\r" + // no '\n'
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   643
                    "Foo: Bar")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   644
            ).getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   645
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   646
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   647
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   648
    @DataProvider(name = "trailingSeqParams", parallel = true)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   649
    public static Object[][] trailingSeqParams() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   650
        return new Object[][] {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   651
            {""},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   652
            {"\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   653
            {"\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   654
            {"\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   655
            {"\r\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   656
            {"\n\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   657
            {"\n\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   658
            {"\r\r\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   659
            {"\r\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   660
            {"\r\n\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   661
            {"\r\n\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   662
            {"\n\r\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   663
            {"\n\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   664
            {"\n\n\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   665
            {"\n\n\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   666
            {"\r\r\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   667
            {"\r\r\n\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   668
            {"\r\r\n\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   669
            {"\r\n\r\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   670
            {"\r\n\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   671
            {"\r\n\n\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   672
            {"\r\n\n\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   673
            {"\n\r\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   674
            {"\n\r\n\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   675
            {"\n\r\n\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   676
            {"\n\n\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   677
            {"\r\r\n\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   678
            {"\r\n\r\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   679
            {"\r\n\r\n\r"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   680
            {"\r\n\r\n\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   681
            {"\r\n\n\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   682
            {"\n\r\n\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   683
            {"\r\n\r\n\r\n"},
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   684
            {"\r\n\r\n\r\n\r\n"}
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   685
        };
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   686
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   687
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   688
    boolean isSufficientSectionDelimiter(String trailingSeq) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   689
        if (trailingSeq.length() < 2) return false;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   690
        if (trailingSeq.startsWith("\r\n")) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   691
            trailingSeq = trailingSeq.substring(2);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   692
        } else if (trailingSeq.startsWith("\r") ||
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   693
                   trailingSeq.startsWith("\n")) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   694
            trailingSeq = trailingSeq.substring(1);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   695
        } else {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   696
            return false;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   697
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   698
        if (trailingSeq.startsWith("\r\n")) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   699
            return true;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   700
        } else if (trailingSeq.startsWith("\r") ||
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   701
                trailingSeq.startsWith("\n")) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   702
            return true;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   703
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   704
        return false;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   705
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   706
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   707
    Function<Manifest, byte[]> replaceTrailingLineBreaksManipulation(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   708
            String trailingSeq) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   709
        return m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   710
            StringBuilder sb = new StringBuilder(manifestToString(m));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   711
            // cut off default trailing line break characters
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   712
            while ("\r\n".contains(sb.substring(sb.length() - 1))) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   713
                sb.deleteCharAt(sb.length() - 1);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   714
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   715
            // and instead add another trailing sequence
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   716
            sb.append(trailingSeq);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   717
            return sb.toString().getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   718
        };
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   719
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   720
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   721
    boolean abSigFilesEqual(String jarFilename,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   722
            Function<Manifest,Object> getter) throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   723
        try (ZipFile zip = new ZipFile(jarFilename)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   724
            ZipEntry ea = zip.getEntry("META-INF/A.SF");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   725
            Manifest sfa = new Manifest(zip.getInputStream(ea));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   726
            ZipEntry eb = zip.getEntry("META-INF/B.SF");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   727
            Manifest sfb = new Manifest(zip.getInputStream(eb));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   728
            return getter.apply(sfa).equals(getter.apply(sfb));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   729
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   730
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   731
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   732
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   733
     * Create a signed JAR file with a strange sequence of line breaks after
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   734
     * the main attributes and no individual section and hence no file contained
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   735
     * within the JAR file in order not to produce an individual section,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   736
     * then add no other file and sign it with a different signer.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   737
     * The manifest is not expected to be changed during the second signature.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   738
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   739
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   740
    public void emptyJarTrailingSeq(String trailingSeq) throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   741
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   742
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   743
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   744
        if (trailingSeq.isEmpty()) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   745
            return; // invalid manifest without trailing line break
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   746
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   747
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   748
        test("emptyJarTrailingSeq" + trailingSeqEscaped, null, null,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   749
                replaceTrailingLineBreaksManipulation(trailingSeq));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   750
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   751
        // test called above already asserts by default that the main attributes
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   752
        // digests have not changed.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   753
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   754
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   755
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   756
     * Create a signed JAR file with a strange sequence of line breaks after
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   757
     * the main attributes and no individual section and hence no file contained
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   758
     * within the JAR file in order not to produce an individual section,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   759
     * then add another file and sign it with a different signer so that the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   760
     * originally trailing sequence after the main attributes might have to be
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   761
     * completed to a full section delimiter or reproduced only partially
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   762
     * before the new individual section with the added file digest can be
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   763
     * appended. The main attributes digests are expected to change if the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   764
     * first signed trailing sequence did not contain a blank line and are not
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   765
     * expected to change if superfluous parts of the trailing sequence were
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   766
     * not reproduced. All digests are expected to validate either with digest
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   767
     * or with digestWorkaround.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   768
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   769
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   770
    public void emptyJarTrailingSeqAddFile(String trailingSeq) throws Exception{
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   771
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   772
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   773
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   774
        if (!isSufficientSectionDelimiter(trailingSeq)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   775
            return; // invalid manifest without trailing blank line
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   776
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   777
        boolean expectUnchangedDigests =
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   778
                isSufficientSectionDelimiter(trailingSeq);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   779
        System.out.println("expectUnchangedDigests = " + expectUnchangedDigests);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   780
        String jarFilename = test("emptyJarTrailingSeqAddFile" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   781
                trailingSeqEscaped, null, FILENAME_UPDATED_CONTENTS,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   782
                replaceTrailingLineBreaksManipulation(trailingSeq),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   783
                null, expectUnchangedDigests, false);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   784
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   785
        // Check that the digests have changed only if another line break had
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   786
        // to be added before a new individual section. That both also are valid
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   787
        // with either digest or digestWorkaround has been checked by test
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   788
        // before.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   789
        assertEquals(abSigFilesEqual(jarFilename, sf -> sf.getMainAttributes()
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   790
                        .getValue("SHA-256-Digest-Manifest-Main-Attributes")),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   791
                     expectUnchangedDigests);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   792
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   793
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   794
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   795
     * Create a signed JAR file with a strange sequence of line breaks after
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   796
     * the only individual section holding the digest of the only file contained
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   797
     * within the JAR file,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   798
     * then add no other file and sign it with a different signer.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   799
     * The manifest is expected to be changed during the second signature only
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   800
     * by removing superfluous line break characters which are not digested
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   801
     * and the manifest entry digest is expected not to change.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   802
     * The individual section is expected to be reproduced without additional
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   803
     * line breaks even if the trailing sequence does not properly delimit
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   804
     * another section.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   805
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   806
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   807
    public void singleIndividualSectionTrailingSeq(String trailingSeq)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   808
            throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   809
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   810
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   811
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   812
        if (trailingSeq.isEmpty()) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   813
            return; // invalid manifest without trailing line break
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   814
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   815
        String jarFilename = test("singleIndividualSectionTrailingSeq"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   816
                + trailingSeqEscaped, FILENAME_INITIAL_CONTENTS, null,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   817
                replaceTrailingLineBreaksManipulation(trailingSeq));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   818
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   819
        assertTrue(abSigFilesEqual(jarFilename, sf -> sf.getAttributes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   820
                FILENAME_INITIAL_CONTENTS).getValue("SHA-256-Digest")));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   821
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   822
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   823
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   824
     * Create a signed JAR file with a strange sequence of line breaks after
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   825
     * the first individual section holding the digest of the only file
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   826
     * contained within the JAR file and a second individual section with the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   827
     * same name to be both digested into the same entry digest,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   828
     * then add no other file and sign it with a different signer.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   829
     * The manifest is expected to be changed during the second signature
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   830
     * by removing superfluous line break characters which are not digested
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   831
     * anyway or if the trailingSeq is not a sufficient delimiter that both
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   832
     * intially provided sections are treated as only one which is maybe not
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   833
     * perfect but does at least not result in an invalid signed jar file.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   834
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   835
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   836
    public void firstIndividualSectionTrailingSeq(String trailingSeq)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   837
            throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   838
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   839
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   840
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   841
        String jarFilename;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   842
        jarFilename =  test("firstIndividualSectionTrailingSeq"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   843
                + trailingSeqEscaped, FILENAME_INITIAL_CONTENTS, null, m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   844
                StringBuilder sb = new StringBuilder(manifestToString(m));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   845
                // cut off default trailing line break characters
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   846
                while ("\r\n".contains(sb.substring(sb.length() - 1))) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   847
                    sb.deleteCharAt(sb.length() - 1);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   848
                }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   849
                // and instead add another trailing sequence
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   850
                sb.append(trailingSeq);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   851
                // now add another section with the same name assuming sb
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   852
                // already contains one entry for FILENAME_INITIAL_CONTENTS
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   853
                sb.append("Name: " + FILENAME_INITIAL_CONTENTS + "\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   854
                sb.append("Foo: Bar\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   855
                sb.append("\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   856
                return sb.toString().getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   857
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   858
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   859
        assertTrue(abSigFilesEqual(jarFilename, sf -> sf.getAttributes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   860
                FILENAME_INITIAL_CONTENTS).getValue("SHA-256-Digest")));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   861
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   862
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   863
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   864
     * Create a signed JAR file with two individual sections for the same
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   865
     * contained file (corresponding by name) the first of which properly
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   866
     * delimited and the second of which followed by a strange sequence of
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   867
     * line breaks both digested into the same entry digest,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   868
     * then add no other file and sign it with a different signer.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   869
     * The manifest is expected to be changed during the second signature
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   870
     * by removing superfluous line break characters which are not digested
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   871
     * anyway.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   872
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   873
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   874
    public void secondIndividualSectionTrailingSeq(String trailingSeq)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   875
            throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   876
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   877
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   878
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   879
        String jarFilename = test("secondIndividualSectionTrailingSeq" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   880
                trailingSeqEscaped, FILENAME_INITIAL_CONTENTS, null, m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   881
            StringBuilder sb = new StringBuilder(manifestToString(m));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   882
            sb.append("Name: " + FILENAME_INITIAL_CONTENTS + "\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   883
            sb.append("Foo: Bar");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   884
            sb.append(trailingSeq);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   885
            return sb.toString().getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   886
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   887
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   888
        assertTrue(abSigFilesEqual(jarFilename, sf -> sf.getAttributes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   889
                FILENAME_INITIAL_CONTENTS).getValue("SHA-256-Digest")));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   890
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   891
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   892
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   893
     * Create a signed JAR file with a strange sequence of line breaks after
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   894
     * the only individual section holding the digest of the only file contained
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   895
     * within the JAR file,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   896
     * then add another file and sign it with a different signer.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   897
     * The manifest is expected to be changed during the second signature by
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   898
     * removing superfluous line break characters which are not digested
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   899
     * anyway or adding another line break to complete to a proper section
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   900
     * delimiter blank line.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   901
     * The first file entry digest is expected to change only if another
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   902
     * line break has been added.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   903
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   904
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   905
    public void singleIndividualSectionTrailingSeqAddFile(String trailingSeq)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   906
            throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   907
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   908
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   909
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   910
        if (!isSufficientSectionDelimiter(trailingSeq)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   911
            return; // invalid manifest without trailing blank line
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   912
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   913
        String jarFilename = test("singleIndividualSectionTrailingSeqAddFile"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   914
                + trailingSeqEscaped,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   915
                FILENAME_INITIAL_CONTENTS, FILENAME_UPDATED_CONTENTS,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   916
                replaceTrailingLineBreaksManipulation(trailingSeq),
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   917
                null, true, true);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   918
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   919
        assertTrue(abSigFilesEqual(jarFilename, sf -> sf.getAttributes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   920
                        FILENAME_INITIAL_CONTENTS).getValue("SHA-256-Digest")));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   921
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   922
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   923
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   924
     * Create a signed JAR file with a strange sequence of line breaks after
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   925
     * the first individual section holding the digest of the only file
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   926
     * contained within the JAR file and a second individual section with the
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   927
     * same name to be both digested into the same entry digest,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   928
     * then add another file and sign it with a different signer.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   929
     * The manifest is expected to be changed during the second signature
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   930
     * by removing superfluous line break characters which are not digested
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   931
     * anyway or if the trailingSeq is not a sufficient delimiter that both
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   932
     * intially provided sections are treated as only one which is maybe not
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   933
     * perfect but does at least not result in an invalid signed jar file.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   934
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   935
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   936
    public void firstIndividualSectionTrailingSeqAddFile(String trailingSeq)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   937
            throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   938
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   939
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   940
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   941
        String jarFilename =  test("firstIndividualSectionTrailingSeqAddFile"
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   942
                + trailingSeqEscaped,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   943
                FILENAME_INITIAL_CONTENTS, FILENAME_UPDATED_CONTENTS, m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   944
                StringBuilder sb = new StringBuilder(manifestToString(m));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   945
                // cut off default trailing line break characters
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   946
                while ("\r\n".contains(sb.substring(sb.length() - 1))) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   947
                    sb.deleteCharAt(sb.length() - 1);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   948
                }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   949
                // and instead add another trailing sequence
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   950
                sb.append(trailingSeq);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   951
                // now add another section with the same name assuming sb
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   952
                // already contains one entry for FILENAME_INITIAL_CONTENTS
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   953
                sb.append("Name: " + FILENAME_INITIAL_CONTENTS + "\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   954
                sb.append("Foo: Bar\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   955
                sb.append("\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   956
                return sb.toString().getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   957
        });
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   958
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   959
        assertTrue(abSigFilesEqual(jarFilename, sf -> sf.getAttributes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   960
                FILENAME_INITIAL_CONTENTS).getValue("SHA-256-Digest")));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   961
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   962
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   963
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   964
     * Create a signed JAR file with two individual sections for the same
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   965
     * contained file (corresponding by name) the first of which properly
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   966
     * delimited and the second of which followed by a strange sequence of
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   967
     * line breaks both digested into the same entry digest,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   968
     * then add another file and sign it with a different signer.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   969
     * The manifest is expected to be changed during the second signature
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   970
     * by removing superfluous line break characters which are not digested
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   971
     * anyway or by adding a proper section delimiter.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   972
     * The digests are expected to be changed only if another line break is
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   973
     * added to properly delimit the next section both digests of which are
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   974
     * expected to validate with either digest or digestWorkaround.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   975
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   976
    @Test(dataProvider = "trailingSeqParams")
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   977
    public void secondIndividualSectionTrailingSeqAddFile(String trailingSeq)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   978
            throws Exception {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   979
        String trailingSeqEscaped = byteArrayToIntList(trailingSeq.getBytes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   980
              UTF_8)).stream().map(i -> "" + i).collect(Collectors.joining(""));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   981
        System.out.println("trailingSeq = " + trailingSeqEscaped);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   982
        if (!isSufficientSectionDelimiter(trailingSeq)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   983
            return; // invalid manifest without trailing blank line
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   984
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   985
        String jarFilename = test("secondIndividualSectionTrailingSeqAddFile" +
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   986
                trailingSeqEscaped,
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   987
                FILENAME_INITIAL_CONTENTS, FILENAME_UPDATED_CONTENTS, m -> {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   988
            StringBuilder sb = new StringBuilder(manifestToString(m));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   989
            sb.append("Name: " + FILENAME_INITIAL_CONTENTS + "\r\n");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   990
            sb.append("Foo: Bar");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   991
            sb.append(trailingSeq);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   992
            return sb.toString().getBytes(UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   993
        }, null, true, true);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   994
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   995
        assertTrue(abSigFilesEqual(jarFilename, sf -> sf.getAttributes(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   996
                FILENAME_INITIAL_CONTENTS).getValue("SHA-256-Digest")));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   997
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   998
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
   999
    String manifestToString(Manifest mf) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1000
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1001
            mf.write(out);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1002
            return new String(out.toByteArray(), UTF_8);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1003
        } catch (IOException e) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1004
            throw new RuntimeException(e);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1005
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1006
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1007
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1008
    static List<Integer> byteArrayToIntList(byte[] bytes) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1009
        List<Integer> list = new ArrayList<>();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1010
        for (int i = 0; i < bytes.length; i++) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1011
            list.add((int) bytes[i]);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1012
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1013
        return list;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1014
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1015
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
diff changeset
  1016
}