Path: blob/master/test/jdk/sun/security/tools/jarsigner/SymLinkTest.java
41152 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 825096826* @summary Symlinks attributes not preserved when using jarsigner on zip files27* @modules jdk.jartool/sun.security.tools.jarsigner28* java.base/sun.security.tools.keytool29* @library /test/lib30* @run main/othervm SymLinkTest31*/3233import java.io.FileInputStream;34import java.io.IOException;35import java.nio.file.Files;36import java.nio.file.Path;37import java.util.Formatter;3839import jdk.test.lib.SecurityTools;4041public class SymLinkTest {42private final static int BYTES_PER_ROW = 8;43private final static String ZIPFILENAME = "8250968-test.zip";44private static final String WARNING_MSG = "POSIX file permission and/or symlink " +45"attributes detected. These attributes are ignored when signing and are not " +46"protected by the signature.";4748public static void main(String[] args) throws Exception {49// call main with an argument to print the prepared zipfile as byte array declaration50if (args.length > 0) {51System.out.println("Bytes of " + ZIPFILENAME + ":");52System.out.println(createByteArray(Files.readAllBytes(Path.of(ZIPFILENAME)), "ZIPBYTES"));53System.exit(0);54}5556Files.write(Path.of(ZIPFILENAME), ZIPBYTES);5758// check attributes before signing59verifyExtraAttrs(ZIPFILENAME);6061// generate key for signing62SecurityTools.keytool(63"-genkey",64"-keyalg", "RSA",65"-dname", "CN=Coffey, OU=JPG, O=Oracle, L=Santa Clara, ST=California, C=US",66"-alias", "examplekey",67"-storepass", "password",68"-keypass", "password",69"-keystore", "examplekeystore",70"-validity", "365")71.shouldHaveExitValue(0);7273// sign zip file - expect warning74SecurityTools.jarsigner(75"-keystore", "examplekeystore",76"-verbose", ZIPFILENAME,77"-storepass", "password",78"-keypass", "password",79"examplekey")80.shouldHaveExitValue(0)81.shouldContain(WARNING_MSG);8283// recheck attributes after signing84verifyExtraAttrs(ZIPFILENAME);8586// verify zip file - expect warning87SecurityTools.jarsigner(88"-keystore", "examplekeystore",89"-storepass", "password",90"-keypass", "password",91"-verbose",92"-verify", ZIPFILENAME)93.shouldHaveExitValue(0)94.shouldContain(WARNING_MSG);95}9697private static void verifyExtraAttrs(String zipFileName) throws IOException {98// the 16 bit extra attributes value should equal 0xa1ff - look for that pattern.99// Such values can be read from zip file via 'unzip -Z -l -v <zipfile>'100try (FileInputStream fis = new FileInputStream(ZIPFILENAME)) {101byte[] b = fis.readAllBytes();102boolean patternFound;103for (int i = 0; i < b.length -1; i++) {104patternFound = ((b[i] & 0xFF) == 0xFF) && ((b[i + 1] & 0xFF) == 0xA1);105if (patternFound) {106return;107}108}109throw new RuntimeException("extra attribute value not detected");110}111}112113/**114* Utility method which takes an byte array and converts to byte array115* declaration. For example:116* <pre>117* {@code118* var fooJar = Files.readAllBytes(Path.of("foo.jar"));119* var result = createByteArray(fooJar, "FOOBYTES");120* }121* </pre>122* @param bytes A byte array used to create a byte array declaration123* @param name Name to be used in the byte array declaration124* @return The formatted byte array declaration125*/126private static String createByteArray(byte[] bytes, String name) {127StringBuilder sb = new StringBuilder();128try (Formatter fmt = new Formatter(sb)) {129fmt.format(" public final static byte[] %s = {", name);130for (int i = 0; i < bytes.length; i++) {131int mod = i % BYTES_PER_ROW;132if (mod == 0) {133fmt.format("%n ");134} else {135fmt.format(" ");136}137fmt.format("(byte)0x%02x", bytes[i]);138if (i != bytes.length - 1) {139fmt.format(",");140}141}142fmt.format("%n };%n");143}144return sb.toString();145}146147/*148* The zipfile itself was created like this:149* $ ln -s ../z z150* $ ls -l z151* lrwxrwxrwx 1 test test 4 Aug 27 18:33 z -> ../z152* $ zip -ry 8250968-test.zip z153*154* The byte array representation was generated using the createByteArray utility method:155* $ java SymLinkTest generate156*/157public final static byte[] ZIPBYTES = {158(byte)0x50, (byte)0x4b, (byte)0x03, (byte)0x04, (byte)0x0a, (byte)0x00, (byte)0x00, (byte)0x00,159(byte)0x00, (byte)0x00, (byte)0x2e, (byte)0x94, (byte)0x1b, (byte)0x51, (byte)0xb4, (byte)0xcc,160(byte)0xb6, (byte)0xf1, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x00,161(byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x1c, (byte)0x00, (byte)0x7a, (byte)0x55,162(byte)0x54, (byte)0x09, (byte)0x00, (byte)0x03, (byte)0x77, (byte)0xfc, (byte)0x47, (byte)0x5f,163(byte)0x78, (byte)0xfc, (byte)0x47, (byte)0x5f, (byte)0x75, (byte)0x78, (byte)0x0b, (byte)0x00,164(byte)0x01, (byte)0x04, (byte)0xec, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0xec,165(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x2e, (byte)0x2e, (byte)0x2f, (byte)0x7a, (byte)0x50,166(byte)0x4b, (byte)0x01, (byte)0x02, (byte)0x1e, (byte)0x03, (byte)0x0a, (byte)0x00, (byte)0x00,167(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2e, (byte)0x94, (byte)0x1b, (byte)0x51, (byte)0xb4,168(byte)0xcc, (byte)0xb6, (byte)0xf1, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04,169(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x18, (byte)0x00, (byte)0x00,170(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff,171(byte)0xa1, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x7a, (byte)0x55, (byte)0x54,172(byte)0x05, (byte)0x00, (byte)0x03, (byte)0x77, (byte)0xfc, (byte)0x47, (byte)0x5f, (byte)0x75,173(byte)0x78, (byte)0x0b, (byte)0x00, (byte)0x01, (byte)0x04, (byte)0xec, (byte)0x03, (byte)0x00,174(byte)0x00, (byte)0x04, (byte)0xec, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x50, (byte)0x4b,175(byte)0x05, (byte)0x06, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00,176(byte)0x01, (byte)0x00, (byte)0x47, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x3f, (byte)0x00,177(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00178};179}180181182