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.FileInputStream;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.util.Formatter;
39 
40 import jdk.test.lib.SecurityTools;
41 
42 public class SymLinkTest {
43     private final static int BYTES_PER_ROW = 8;
44     private final static String ZIPFILENAME = "8250968-test.zip";
45     private static final String WARNING_MSG = "POSIX file permission and/or symlink " +
46             "attributes detected. These attributes are ignored when signing and are not " +
47             "protected by the signature.";
48 
main(String[] args)49     public static void main(String[] args) throws Exception {
50         // call main with an argument to print the prepared zipfile as byte array declaration
51         if (args.length > 0) {
52             System.out.println("Bytes of " + ZIPFILENAME + ":");
53             System.out.println(createByteArray(Files.readAllBytes(Path.of(ZIPFILENAME)), "ZIPBYTES"));
54             System.exit(0);
55         }
56 
57         Files.write(Path.of(ZIPFILENAME), ZIPBYTES);
58 
59         // check attributes before signing
60         verifyExtraAttrs(ZIPFILENAME);
61 
62         // generate key for signing
63         SecurityTools.keytool(
64                 "-genkey",
65                 "-keyalg", "RSA",
66                 "-dname", "CN=Coffey, OU=JPG, O=Oracle, L=Santa Clara, ST=California, C=US",
67                 "-alias", "examplekey",
68                 "-storepass", "password",
69                 "-keypass", "password",
70                 "-keystore", "examplekeystore",
71                 "-validity", "365")
72                 .shouldHaveExitValue(0);
73 
74         // sign zip file - expect warning
75         SecurityTools.jarsigner(
76                 "-keystore", "examplekeystore",
77                 "-verbose", ZIPFILENAME,
78                 "-storepass", "password",
79                 "-keypass", "password",
80                 "examplekey")
81                 .shouldHaveExitValue(0)
82                 .shouldContain(WARNING_MSG);
83 
84         // recheck attributes after signing
85         verifyExtraAttrs(ZIPFILENAME);
86 
87         // verify zip file - expect warning
88         SecurityTools.jarsigner(
89                 "-keystore", "examplekeystore",
90                 "-storepass", "password",
91                 "-keypass", "password",
92                 "-verbose",
93                 "-verify", ZIPFILENAME)
94                 .shouldHaveExitValue(0)
95                 .shouldContain(WARNING_MSG);
96     }
97 
verifyExtraAttrs(String zipFileName)98     private static void verifyExtraAttrs(String zipFileName) throws IOException {
99         // the 16 bit extra attributes value should equal 0xa1ff - look for that pattern.
100         // Such values can be read from zip file via 'unzip -Z -l -v <zipfile>'
101         try (FileInputStream fis = new FileInputStream(ZIPFILENAME)) {
102             byte[] b = fis.readAllBytes();
103             boolean patternFound;
104             for (int i = 0; i < b.length -1; i++) {
105                 patternFound = ((b[i] & 0xFF) == 0xFF) &&  ((b[i + 1] & 0xFF) == 0xA1);
106                 if (patternFound) {
107                     return;
108                 }
109             }
110             throw new RuntimeException("extra attribute value not detected");
111         }
112     }
113 
114     /**
115      * Utility method which takes an byte array and converts to byte array
116      * declaration.  For example:
117      * <pre>
118      *     {@code
119      *        var fooJar = Files.readAllBytes(Path.of("foo.jar"));
120      *        var result = createByteArray(fooJar, "FOOBYTES");
121      *      }
122      * </pre>
123      * @param bytes A byte array used to create a byte array declaration
124      * @param name Name to be used in the byte array declaration
125      * @return The formatted byte array declaration
126      */
createByteArray(byte[] bytes, String name)127     private static String createByteArray(byte[] bytes, String name) {
128         StringBuilder sb = new StringBuilder();
129         try (Formatter fmt = new Formatter(sb)) {
130             fmt.format("    public final static byte[] %s = {", name);
131             for (int i = 0; i < bytes.length; i++) {
132                 int mod = i % BYTES_PER_ROW;
133                 if (mod == 0) {
134                     fmt.format("%n        ");
135                 } else {
136                     fmt.format(" ");
137                 }
138                 fmt.format("(byte)0x%02x", bytes[i]);
139                 if (i != bytes.length - 1) {
140                     fmt.format(",");
141                 }
142             }
143             fmt.format("%n    };%n");
144         }
145         return sb.toString();
146     }
147 
148     /*
149      * The zipfile itself was created like this:
150      * $ ln -s ../z z
151      * $ ls -l z
152      * lrwxrwxrwx 1 test test 4 Aug 27 18:33 z -> ../z
153      * $ zip -ry 8250968-test.zip z
154      *
155      * The byte array representation was generated using the createByteArray utility method:
156      * $ java SymLinkTest generate
157      */
158     public final static byte[] ZIPBYTES = {
159         (byte)0x50, (byte)0x4b, (byte)0x03, (byte)0x04, (byte)0x0a, (byte)0x00, (byte)0x00, (byte)0x00,
160         (byte)0x00, (byte)0x00, (byte)0x2e, (byte)0x94, (byte)0x1b, (byte)0x51, (byte)0xb4, (byte)0xcc,
161         (byte)0xb6, (byte)0xf1, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x00,
162         (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x1c, (byte)0x00, (byte)0x7a, (byte)0x55,
163         (byte)0x54, (byte)0x09, (byte)0x00, (byte)0x03, (byte)0x77, (byte)0xfc, (byte)0x47, (byte)0x5f,
164         (byte)0x78, (byte)0xfc, (byte)0x47, (byte)0x5f, (byte)0x75, (byte)0x78, (byte)0x0b, (byte)0x00,
165         (byte)0x01, (byte)0x04, (byte)0xec, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0xec,
166         (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x2e, (byte)0x2e, (byte)0x2f, (byte)0x7a, (byte)0x50,
167         (byte)0x4b, (byte)0x01, (byte)0x02, (byte)0x1e, (byte)0x03, (byte)0x0a, (byte)0x00, (byte)0x00,
168         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2e, (byte)0x94, (byte)0x1b, (byte)0x51, (byte)0xb4,
169         (byte)0xcc, (byte)0xb6, (byte)0xf1, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04,
170         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x18, (byte)0x00, (byte)0x00,
171         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff,
172         (byte)0xa1, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x7a, (byte)0x55, (byte)0x54,
173         (byte)0x05, (byte)0x00, (byte)0x03, (byte)0x77, (byte)0xfc, (byte)0x47, (byte)0x5f, (byte)0x75,
174         (byte)0x78, (byte)0x0b, (byte)0x00, (byte)0x01, (byte)0x04, (byte)0xec, (byte)0x03, (byte)0x00,
175         (byte)0x00, (byte)0x04, (byte)0xec, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x50, (byte)0x4b,
176         (byte)0x05, (byte)0x06, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00,
177         (byte)0x01, (byte)0x00, (byte)0x47, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x3f, (byte)0x00,
178         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
179     };
180 }
181