author | weijun |
Thu, 18 Jul 2019 08:53:06 +0800 | |
changeset 57488 | 94691d8e746f |
parent 49778 | 3e6ce108d269 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.util; |
|
27 |
||
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
49778
diff
changeset
|
28 |
import java.security.MessageDigest; |
49778 | 29 |
import java.util.ArrayList; |
2 | 30 |
import java.util.HashMap; |
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 | 34 |
import java.util.List; |
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 | 37 |
|
38 |
/** |
|
39 |
* This class is used to compute digests on sections of the Manifest. |
|
49778 | 40 |
* Please note that multiple sections might have the same name, and they |
41 |
* all belong to a single Entry. |
|
2 | 42 |
*/ |
43 |
public class ManifestDigester { |
|
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 | 57 |
public static final String MF_MAIN_ATTRS = "Manifest-Main-Attributes"; |
58 |
||
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 | 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 | 66 |
|
67 |
/** state returned by findSection */ |
|
68 |
static class Position { |
|
69 |
int endOfFirstLine; // not including newline character |
|
70 |
||
71 |
int endOfSection; // end of section, not including the blank line |
|
72 |
// between sections |
|
73 |
int startOfNext; // the start of the next section |
|
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* find a section in the manifest. |
|
78 |
* |
|
79 |
* @param offset should point to the starting offset with in the |
|
80 |
* raw bytes of the next section. |
|
81 |
* |
|
82 |
* @pos set by |
|
83 |
* |
|
30374 | 84 |
* @return false if end of bytes has been reached, otherwise returns |
2 | 85 |
* true |
86 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
87 |
@SuppressWarnings("fallthrough") |
2 | 88 |
private boolean findSection(int offset, Position pos) |
89 |
{ |
|
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 | 92 |
int next; |
93 |
boolean allBlank = true; |
|
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 | 101 |
|
102 |
while (i < len) { |
|
103 |
byte b = rawBytes[i]; |
|
104 |
switch(b) { |
|
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 | 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 | 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 | 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 | 113 |
pos.endOfFirstLine = i-1; |
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 | 116 |
pos.startOfNext = i+1; |
117 |
return true; |
|
118 |
} |
|
119 |
else { |
|
120 |
// start of a new line |
|
121 |
last = i; |
|
122 |
allBlank = true; |
|
123 |
} |
|
124 |
break; |
|
125 |
default: |
|
126 |
allBlank = false; |
|
127 |
break; |
|
128 |
} |
|
129 |
i++; |
|
130 |
} |
|
131 |
return false; |
|
132 |
} |
|
133 |
||
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30374
diff
changeset
|
134 |
public ManifestDigester(byte[] bytes) |
2 | 135 |
{ |
136 |
rawBytes = bytes; |
|
137 |
||
138 |
Position pos = new Position(); |
|
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 | 142 |
return; // XXX: exception? |
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
49778
diff
changeset
|
143 |
} |
2 | 144 |
|
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 | 148 |
|
149 |
int start = pos.startOfNext; |
|
150 |
while(findSection(start, pos)) { |
|
151 |
int len = pos.endOfFirstLine-start+1; |
|
152 |
int sectionLen = pos.endOfSection-start+1; |
|
153 |
int sectionLenWithBlank = pos.startOfNext-start; |
|
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 | 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 | 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 | 168 |
} |
47273
f60a42d4b8cd
6695402: Jarsigner with multi-byte characters in class names
weijun
parents:
47216
diff
changeset
|
169 |
} |
2 | 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 | 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 | 190 |
} |
47273
f60a42d4b8cd
6695402: Jarsigner with multi-byte characters in class names
weijun
parents:
47216
diff
changeset
|
191 |
} |
2 | 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 | 194 |
dummy -> new Entry()) |
195 |
.addSection(new Section(start, sectionLen, |
|
196 |
sectionLenWithBlank, rawBytes)); |
|
2 | 197 |
} |
198 |
} |
|
199 |
start = pos.startOfNext; |
|
200 |
} |
|
201 |
} |
|
202 |
||
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30374
diff
changeset
|
203 |
private boolean isNameAttr(byte[] bytes, int start) |
2 | 204 |
{ |
205 |
return ((bytes[start] == 'N') || (bytes[start] == 'n')) && |
|
206 |
((bytes[start+1] == 'a') || (bytes[start+1] == 'A')) && |
|
207 |
((bytes[start+2] == 'm') || (bytes[start+2] == 'M')) && |
|
208 |
((bytes[start+3] == 'e') || (bytes[start+3] == 'E')) && |
|
209 |
(bytes[start+4] == ':') && |
|
210 |
(bytes[start+5] == ' '); |
|
211 |
} |
|
212 |
||
213 |
public static class Entry { |
|
49778 | 214 |
|
215 |
// One Entry for one name, and one name can have multiple sections. |
|
216 |
// According to the JAR File Specification: "If there are multiple |
|
217 |
// individual sections for the same file entry, the attributes in |
|
218 |
// these sections are merged." |
|
219 |
private List<Section> sections = new ArrayList<>(); |
|
220 |
boolean oldStyle; |
|
221 |
||
222 |
private Entry addSection(Section sec) |
|
223 |
{ |
|
224 |
sections.add(sec); |
|
225 |
return this; |
|
226 |
} |
|
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 | 248 |
public byte[] digest(MessageDigest md) |
249 |
{ |
|
250 |
md.reset(); |
|
251 |
for (Section sec : sections) { |
|
252 |
if (oldStyle) { |
|
253 |
Section.doOldStyle(md, sec.rawBytes, sec.offset, sec.lengthWithBlankLine); |
|
254 |
} else { |
|
255 |
md.update(sec.rawBytes, sec.offset, sec.lengthWithBlankLine); |
|
256 |
} |
|
257 |
} |
|
258 |
return md.digest(); |
|
259 |
} |
|
260 |
||
261 |
/** Netscape doesn't include the new line. Intel and JavaSoft do */ |
|
262 |
||
263 |
public byte[] digestWorkaround(MessageDigest md) |
|
264 |
{ |
|
265 |
md.reset(); |
|
266 |
for (Section sec : sections) { |
|
267 |
md.update(sec.rawBytes, sec.offset, sec.length); |
|
268 |
} |
|
269 |
return md.digest(); |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
private static class Section { |
|
2 | 274 |
int offset; |
275 |
int length; |
|
276 |
int lengthWithBlankLine; |
|
277 |
byte[] rawBytes; |
|
278 |
||
49778 | 279 |
public Section(int offset, int length, |
2 | 280 |
int lengthWithBlankLine, byte[] rawBytes) |
281 |
{ |
|
282 |
this.offset = offset; |
|
283 |
this.length = length; |
|
284 |
this.lengthWithBlankLine = lengthWithBlankLine; |
|
285 |
this.rawBytes = rawBytes; |
|
286 |
} |
|
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 | 297 |
private static void doOldStyle(MessageDigest md, |
2 | 298 |
byte[] bytes, |
299 |
int offset, |
|
300 |
int length) |
|
301 |
{ |
|
302 |
// this is too gross to even document, but here goes |
|
303 |
// the 1.1 jar verification code ignored spaces at the |
|
304 |
// end of lines when calculating digests, so that is |
|
305 |
// what this code does. It only gets called if we |
|
306 |
// are parsing a 1.1 signed signature file |
|
307 |
int i = offset; |
|
308 |
int start = offset; |
|
309 |
int max = offset + length; |
|
310 |
int prev = -1; |
|
311 |
while(i <max) { |
|
312 |
if ((bytes[i] == '\r') && (prev == ' ')) { |
|
313 |
md.update(bytes, start, i-start-1); |
|
314 |
start = i; |
|
315 |
} |
|
316 |
prev = bytes[i]; |
|
317 |
i++; |
|
318 |
} |
|
319 |
md.update(bytes, start, i-start); |
|
320 |
} |
|
321 |
} |
|
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 | 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 | 348 |
e.oldStyle = oldStyle; |
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
49778
diff
changeset
|
349 |
} |
2 | 350 |
return e; |
351 |
} |
|
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 | 358 |
|
359 |
} |