1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @modules java.base/sun.security.tools.keytool
* @summary JARs with pending block files (where .RSA comes before .SF) should verify correctly
*/
import jdk.security.jarsigner.JarSigner;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.util.Collections;
import java.util.jar.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class SignedJarPendingBlock {
public static void main(String[] args) throws Exception {
Path jar = createJarFile();
Path signed = signJarFile(jar);
Path pendingBlocks = moveBlockFirst(signed);
Path invalid = invalidate(pendingBlocks);
// 1: Regular signed JAR with no pending blocks should verify
checkSigned(signed);
// 2: Signed jar with pending blocks should verify
checkSigned(pendingBlocks);
// 3: Invalid signed jar with pending blocks should throw SecurityException
try {
checkSigned(invalid);
throw new Exception("Expected invalid digest to be detected");
} catch (SecurityException se) {
// Ignore
}
}
private static void checkSigned(Path b) throws Exception {
try (JarFile jf = new JarFile(b.toFile(), true)) {
JarEntry je = jf.getJarEntry("a.txt");
try (InputStream in = jf.getInputStream(je)) {
in.transferTo(OutputStream.nullOutputStream());
}
}
}
/**
* Invalidate signed file by modifying the contents of "a.txt"
*/
private static Path invalidate(Path s) throws Exception{
Path invalid = Path.of("pending-block-file-invalidated.jar");
try (ZipFile zip = new ZipFile(s.toFile());
ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(invalid))) {
for (ZipEntry ze : Collections.list(zip.entries())) {
String name = ze.getName();
out.putNextEntry(new ZipEntry(name));
if (name.equals("a.txt")) {
// Change the contents of a.txt to trigger SignatureException
out.write("b".getBytes(StandardCharsets.UTF_8));
} else {
try (InputStream in = zip.getInputStream(ze)) {
in.transferTo(out);
}
}
}
}
return invalid;
}
private static Path moveBlockFirst(Path s) throws Exception {
Path b = Path.of("pending-block-file-blockfirst.jar");
try (ZipFile in = new ZipFile(s.toFile());
ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(b))) {
copy("META-INF/MANIFEST.MF", in, out);
// Switch the order of the RSA and SF files
copy("META-INF/SIGNER.RSA", in, out);
copy("META-INF/SIGNER.SF", in, out);
copy("a.txt", in, out);
}
return b;
}
/**
* Copy an entry from a ZipFile to a ZipOutputStream
*/
private static void copy(String name, ZipFile in, ZipOutputStream out) throws Exception {
out.putNextEntry(new ZipEntry(name));
try (InputStream is = in.getInputStream(in.getEntry(name))) {
is.transferTo(out);
}
}
private static Path signJarFile(Path j) throws Exception {
Path s = Path.of("pending-block-file-signed.jar");
Files.deleteIfExists(Path.of("ks"));
sun.security.tools.keytool.Main.main(
("-keystore ks -storepass changeit -keypass changeit -dname" +
" CN=SIGNER" +" -alias r -genkeypair -keyalg rsa").split(" "));
char[] pass = "changeit".toCharArray();
KeyStore ks = KeyStore.getInstance(new File("ks"), pass);
KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry)
ks.getEntry("r", new KeyStore.PasswordProtection(pass));
JarSigner signer = new JarSigner.Builder(pke)
.digestAlgorithm("SHA-256")
.signatureAlgorithm("SHA256withRSA")
.signerName("SIGNER")
.build();
try (ZipFile in = new ZipFile(j.toFile());
OutputStream out = Files.newOutputStream(s)) {
signer.sign(in, out);
}
return s;
}
/**
* Create a jar file with single entry "a.txt" containing "a"
*/
private static Path createJarFile() throws Exception {
Path jar = Path.of("pending-block-file.jar");
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(jar),manifest)) {
out.putNextEntry(new JarEntry("a.txt"));
out.write("a".getBytes(StandardCharsets.UTF_8));
}
return jar;
}
}
|