src/java.base/share/classes/sun/security/util/ManifestDigester.java
author weijun
Thu, 18 Jul 2019 08:53:06 +0800
changeset 57488 94691d8e746f
parent 49778 3e6ce108d269
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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3048
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3048
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3048
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3048
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3048
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    28
import java.security.MessageDigest;
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
    29
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    32
import java.io.OutputStream;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    33
import java.io.IOException;
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
    34
import java.util.List;
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
    35
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
    36
import static java.nio.charset.StandardCharsets.UTF_8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class is used to compute digests on sections of the Manifest.
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
    40
 * Please note that multiple sections might have the same name, and they
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
    41
 * all belong to a single Entry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class ManifestDigester {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    45
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    46
     * The part "{@code Manifest-Main-Attributes}" of the main attributes
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    47
     * digest header name in a signature file as described in the jar
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    48
     * specification:
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    49
     * <blockquote>{@code x-Digest-Manifest-Main-Attributes}
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    50
     * (where x is the standard name of a {@link MessageDigest} algorithm):
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    51
     * The value of this attribute is the digest value of the main attributes
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    52
     * of the manifest.</blockquote>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    53
     * @see <a href="{@docRoot}/../specs/jar/jar.html#signature-file">
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    54
     * JAR File Specification, section Signature File</a>
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    55
     * @see #getMainAttsEntry
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    56
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final String MF_MAIN_ATTRS = "Manifest-Main-Attributes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** the raw bytes of the manifest */
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    60
    private final byte[] rawBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    62
    private final Entry mainAttsEntry;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    63
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    64
    /** individual sections by their names */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    65
    private final HashMap<String, Entry> entries = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /** state returned by findSection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static class Position {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        int endOfFirstLine; // not including newline character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int endOfSection; // end of section, not including the blank line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                          // between sections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int startOfNext;  // the start of the next section
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * find a section in the manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param offset should point to the starting offset with in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * raw bytes of the next section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @pos set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 30033
diff changeset
    84
     * @return false if end of bytes has been reached, otherwise returns
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *          true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    87
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private boolean findSection(int offset, Position pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int i = offset, len = rawBytes.length;
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    91
        int last = offset - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        boolean allBlank = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    95
        /* denotes that a position is not yet assigned.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    96
         * As a primitive type int it cannot be null
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    97
         * and -1 would be confused with (i - 1) when i == 0 */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    98
        final int UNASSIGNED = Integer.MIN_VALUE;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
    99
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   100
        pos.endOfFirstLine = UNASSIGNED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        while (i < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            byte b = rawBytes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            switch(b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            case '\r':
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   106
                if (pos.endOfFirstLine == UNASSIGNED)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    pos.endOfFirstLine = i-1;
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   108
                if (i < len - 1 && rawBytes[i + 1] == '\n')
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    i++;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   110
                /* fall through */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            case '\n':
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   112
                if (pos.endOfFirstLine == UNASSIGNED)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    pos.endOfFirstLine = i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (allBlank || (i == len-1)) {
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   115
                    pos.endOfSection = allBlank ? last : i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    pos.startOfNext = i+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    // start of a new line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    last = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    allBlank = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                allBlank = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
   134
    public ManifestDigester(byte[] bytes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        rawBytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Position pos = new Position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   140
        if (!findSection(0, pos)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   141
            mainAttsEntry = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return; // XXX: exception?
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   143
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // create an entry for main attributes
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   146
        mainAttsEntry = new Entry().addSection(new Section(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   147
                0, pos.endOfSection + 1, pos.startOfNext, rawBytes));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int start = pos.startOfNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        while(findSection(start, pos)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int len = pos.endOfFirstLine-start+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int sectionLen = pos.endOfSection-start+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int sectionLenWithBlank = pos.startOfNext-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   155
            if (len >= 6) { // 6 == "Name: ".length()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (isNameAttr(bytes, start)) {
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   157
                    ByteArrayOutputStream nameBuf = new ByteArrayOutputStream();
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   158
                    nameBuf.write(bytes, start+6, len-6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   160
                    int i = start + len;
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   161
                    if ((i-start) < sectionLen) {
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   162
                        if (bytes[i] == '\r'
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   163
                                && i + 1 - start < sectionLen
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   164
                                && bytes[i + 1] == '\n') {
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   165
                            i += 2;
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   166
                        } else {
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   167
                            i += 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        }
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   169
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   171
                    while ((i-start) < sectionLen) {
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   172
                        if (bytes[i++] == ' ') {
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   173
                            // name is wrapped
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   174
                            int wrapStart = i;
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   175
                            while (((i-start) < sectionLen)
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   176
                                    && (bytes[i] != '\r')
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   177
                                    && (bytes[i] != '\n')) i++;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   178
                            int wrapLen = i - wrapStart;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   179
                            if (i - start < sectionLen) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   180
                                i++;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   181
                                if (bytes[i - 1] == '\r'
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   182
                                    && i - start < sectionLen
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   183
                                    && bytes[i] == '\n')
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   184
                                        i++;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   185
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   187
                            nameBuf.write(bytes, wrapStart, wrapLen);
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   188
                        } else {
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   189
                            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        }
47273
f60a42d4b8cd 6695402: Jarsigner with multi-byte characters in class names
weijun
parents: 47216
diff changeset
   191
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   193
                    entries.computeIfAbsent(nameBuf.toString(UTF_8),
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   194
                                            dummy -> new Entry())
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   195
                            .addSection(new Section(start, sectionLen,
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   196
                                    sectionLenWithBlank, rawBytes));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            start = pos.startOfNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
   203
    private boolean isNameAttr(byte[] bytes, int start)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return ((bytes[start] == 'N') || (bytes[start] == 'n')) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
               ((bytes[start+1] == 'a') || (bytes[start+1] == 'A')) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
               ((bytes[start+2] == 'm') || (bytes[start+2] == 'M')) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
               ((bytes[start+3] == 'e') || (bytes[start+3] == 'E')) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
               (bytes[start+4] == ':') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
               (bytes[start+5] == ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static class Entry {
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   214
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   215
        // One Entry for one name, and one name can have multiple sections.
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   216
        // According to the JAR File Specification: "If there are multiple
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   217
        // individual sections for the same file entry, the attributes in
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   218
        // these sections are merged."
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   219
        private List<Section> sections = new ArrayList<>();
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   220
        boolean oldStyle;
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   221
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   222
        private Entry addSection(Section sec)
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   223
        {
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   224
            sections.add(sec);
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   225
            return this;
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   226
        }
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   227
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   228
        /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   229
         * Check if the sections (particularly the last one of usually only one)
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   230
         * are properly delimited with a trailing blank line so that another
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   231
         * section can be correctly appended and return {@code true} or return
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   232
         * {@code false} to indicate that reproduction is not advised and should
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   233
         * be carried out with a clean "normalized" newly-written manifest.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   234
         *
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   235
         * @see #reproduceRaw
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   236
         */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   237
        public boolean isProperlyDelimited() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   238
            return sections.stream().allMatch(
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   239
                    Section::isProperlySectionDelimited);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   240
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   241
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   242
        public void reproduceRaw(OutputStream out) throws IOException {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   243
            for (Section sec : sections) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   244
                out.write(sec.rawBytes, sec.offset, sec.lengthWithBlankLine);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   245
            }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   246
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   247
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   248
        public byte[] digest(MessageDigest md)
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   249
        {
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   250
            md.reset();
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   251
            for (Section sec : sections) {
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   252
                if (oldStyle) {
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   253
                    Section.doOldStyle(md, sec.rawBytes, sec.offset, sec.lengthWithBlankLine);
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   254
                } else {
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   255
                    md.update(sec.rawBytes, sec.offset, sec.lengthWithBlankLine);
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   256
                }
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   257
            }
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   258
            return md.digest();
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   259
        }
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   260
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   261
        /** Netscape doesn't include the new line. Intel and JavaSoft do */
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   262
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   263
        public byte[] digestWorkaround(MessageDigest md)
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   264
        {
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   265
            md.reset();
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   266
            for (Section sec : sections) {
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   267
                md.update(sec.rawBytes, sec.offset, sec.length);
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   268
            }
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   269
            return md.digest();
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   270
        }
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   271
    }
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   272
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   273
    private static class Section {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int lengthWithBlankLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        byte[] rawBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   279
        public Section(int offset, int length,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                     int lengthWithBlankLine, byte[] rawBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            this.lengthWithBlankLine = lengthWithBlankLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            this.rawBytes = rawBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   288
        /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   289
         * Returns {@code true} if the raw section is terminated with a blank
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   290
         * line so that another section can possibly be appended resulting in a
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   291
         * valid manifest and {@code false} otherwise.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   292
         */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   293
        private boolean isProperlySectionDelimited() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   294
            return lengthWithBlankLine > length;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   295
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   296
49778
3e6ce108d269 8189969: Manifest better manifest entries
weijun
parents: 47273
diff changeset
   297
        private static void doOldStyle(MessageDigest md,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                byte[] bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // this is too gross to even document, but here goes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // the 1.1 jar verification code ignored spaces at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // end of lines when calculating digests, so that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // what this code does. It only gets called if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // are parsing a 1.1 signed signature file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int i = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            int start = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            int max = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            int prev = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            while(i <max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if ((bytes[i] == '\r') && (prev == ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    md.update(bytes, start, i-start-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    start = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                prev = bytes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            md.update(bytes, start, i-start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   323
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   324
     * @see #MF_MAIN_ATTRS
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   325
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   326
    public Entry getMainAttsEntry() {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   327
        return mainAttsEntry;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   328
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   329
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   330
    /**
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   331
     * @see #MF_MAIN_ATTRS
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   332
     */
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   333
    public Entry getMainAttsEntry(boolean oldStyle) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   334
        mainAttsEntry.oldStyle = oldStyle;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   335
        return mainAttsEntry;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   336
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   337
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   338
    public Entry get(String name) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   339
        return entries.get(name);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   340
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   341
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public Entry get(String name, boolean oldStyle) {
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   343
        Entry e = get(name);
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   344
        if (e == null && MF_MAIN_ATTRS.equals(name)) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   345
            e = getMainAttsEntry();
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   346
        }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   347
        if (e != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            e.oldStyle = oldStyle;
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 49778
diff changeset
   349
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
   353
    public byte[] manifestDigest(MessageDigest md) {
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
   354
        md.reset();
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
   355
        md.update(rawBytes, 0, rawBytes.length);
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
   356
        return md.digest();
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
   357
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
}