1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
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
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 8250968
27  * @summary Symlinks attributes not preserved when using jarsigner on zip files
28  * @modules jdk.jartool/sun.security.tools.jarsigner
29  *          java.base/sun.security.tools.keytool
30  * @library /test/lib
31  * @run main/othervm SymLinkTest
32  */
33 
34 import java.io.*;
35 import java.net.URI;
36 import java.nio.file.*;
37 import java.util.Formatter;
38 
39 import jdk.test.lib.SecurityTools;
40 
41 public class SymLinkTest {
42     private final static String ZIPFILENAME = "8250968-test.zip";
43     private static final String WARNING_MSG = "POSIX file permission and/or symlink " +
44             "attributes detected. These attributes are ignored when signing and are not " +
45             "protected by the signature.";
46 
main(String[] args)47     public static void main(String[] args) throws Exception {
48         Files.deleteIfExists(Paths.get(ZIPFILENAME));
49         try (FileOutputStream fos = new FileOutputStream(ZIPFILENAME)) {
50             fos.write(ZIPBYTES);
51         }
52 
53         // check permissions before signing
54         verifyExtraAttrs(ZIPFILENAME);
55 
56         SecurityTools.keytool(
57                 "-genkey",
58                 "-keyalg", "RSA",
59                 "-dname", "CN=Coffey, OU=JPG, O=Oracle, L=Santa Clara, ST=California, C=US",
60                 "-alias", "examplekey",
61                 "-storepass", "password",
62                 "-keypass", "password",
63                 "-keystore", "examplekeystore",
64                 "-validity", "365")
65                 .shouldHaveExitValue(0);
66 
67         SecurityTools.jarsigner(
68                 "-keystore", "examplekeystore",
69                 "-verbose", ZIPFILENAME,
70                 "-storepass", "password",
71                 "-keypass", "password",
72                 "examplekey")
73                 .shouldHaveExitValue(0)
74                 .shouldContain(WARNING_MSG);
75 
76         // zip file now signed. Recheck attributes
77         verifyExtraAttrs(ZIPFILENAME);
78 
79         SecurityTools.jarsigner("-keystore", "examplekeystore",
80                 "-storepass", "password",
81                 "-keypass", "password",
82                 "-verbose",
83                 "-verify", ZIPFILENAME)
84                 .shouldHaveExitValue(0)
85                 .shouldContain(WARNING_MSG);
86     }
87 
verifyExtraAttrs(String zipFileName)88     private static void verifyExtraAttrs(String zipFileName) throws IOException {
89         // the 16 bit extra attributes value should equal 0xa1ff - look for that pattern.
90         // Such values can be read from zip file via 'unzip -Z -l -v <zipfile>'
91         try (FileInputStream fis = new FileInputStream(ZIPFILENAME)) {
92             byte[] b = fis.readAllBytes();
93             boolean patternFound;
94             for (int i = 0; i < b.length -1; i++) {
95                 patternFound = ((b[i] & 0xFF) == 0xFF) &&  ((b[i + 1] & 0xFF) == 0xA1);
96                 if (patternFound) {
97                     return;
98                 }
99             }
100             throw new RuntimeException("extra attribute value not detected");
101         }
102     }
103 
104     /**
105      * Utility method which takes an byte array and converts to byte array
106      * declaration.  For example:
107      * <pre>
108      *     {@code
109      *        var fooJar = Files.readAllBytes(Path.of("foo.jar"));
110      *        var result = createByteArray(fooJar, "FOOBYTES");
111      *      }
112      * </pre>
113      * @param bytes A byte array used to create a byte array declaration
114      * @param name Name to be used in the byte array declaration
115      * @return The formatted byte array declaration
116      */
createByteArray(byte[] bytes, String name)117     public static String createByteArray(byte[] bytes, String name) {
118         StringBuilder sb = new StringBuilder(bytes.length * 5);
119         Formatter fmt = new Formatter(sb);
120         fmt.format("    public static byte[] %s = {", name);
121         final int linelen = 8;
122         for (int i = 0; i < bytes.length; i++) {
123             if (i % linelen == 0) {
124                 fmt.format("%n        ");
125             }
126             fmt.format(" (byte) 0x%x,", bytes[i] & 0xff);
127         }
128         fmt.format("%n    };%n");
129         return sb.toString();
130     }
131 
132     /*
133      * Created using the createByteArray utility method.
134      * The zipfile itself was created via this example:
135      * $ ls -l z
136      * lrwxrwxrwx 1 test test 4 Aug 27 18:33 z -> ../z
137      * $ zip -ry test.zip z
138      */
139     public final static byte[] ZIPBYTES = {
140             (byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0xa, (byte) 0x0, (byte) 0x0, (byte) 0x0,
141             (byte) 0x0, (byte) 0x0, (byte) 0x2e, (byte) 0x94, (byte) 0x1b, (byte) 0x51, (byte) 0xb4, (byte) 0xcc,
142             (byte) 0xb6, (byte) 0xf1, (byte) 0x4, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x4, (byte) 0x0,
143             (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x1c, (byte) 0x0, (byte) 0x7a, (byte) 0x55,
144             (byte) 0x54, (byte) 0x9, (byte) 0x0, (byte) 0x3, (byte) 0x77, (byte) 0xfc, (byte) 0x47, (byte) 0x5f,
145             (byte) 0x78, (byte) 0xfc, (byte) 0x47, (byte) 0x5f, (byte) 0x75, (byte) 0x78, (byte) 0xb, (byte) 0x0,
146             (byte) 0x1, (byte) 0x4, (byte) 0xec, (byte) 0x3, (byte) 0x0, (byte) 0x0, (byte) 0x4, (byte) 0xec,
147             (byte) 0x3, (byte) 0x0, (byte) 0x0, (byte) 0x2e, (byte) 0x2e, (byte) 0x2f, (byte) 0x7a, (byte) 0x50,
148             (byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x1e, (byte) 0x3, (byte) 0xa, (byte) 0x0, (byte) 0x0,
149             (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2e, (byte) 0x94, (byte) 0x1b, (byte) 0x51, (byte) 0xb4,
150             (byte) 0xcc, (byte) 0xb6, (byte) 0xf1, (byte) 0x4, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x4,
151             (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x18, (byte) 0x0, (byte) 0x0,
152             (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xff,
153             (byte) 0xa1, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x7a, (byte) 0x55, (byte) 0x54,
154             (byte) 0x5, (byte) 0x0, (byte) 0x3, (byte) 0x77, (byte) 0xfc, (byte) 0x47, (byte) 0x5f, (byte) 0x75,
155             (byte) 0x78, (byte) 0xb, (byte) 0x0, (byte) 0x1, (byte) 0x4, (byte) 0xec, (byte) 0x3, (byte) 0x0,
156             (byte) 0x0, (byte) 0x4, (byte) 0xec, (byte) 0x3, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b,
157             (byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0,
158             (byte) 0x1, (byte) 0x0, (byte) 0x47, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x3f, (byte) 0x0,
159             (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
160     };
161 }
162