1 /* 2 * Copyright (c) 2019, 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 import java.nio.file.Path; 25 import java.util.List; 26 27 import jdk.jpackage.test.TKit; 28 import jdk.jpackage.test.Executor; 29 30 public class SigningBase { 31 32 public static String DEV_NAME = "jpackage.openjdk.java.net"; 33 public static String APP_CERT 34 = "Developer ID Application: " + DEV_NAME; 35 public static String INSTALLER_CERT 36 = "Developer ID Installer: " + DEV_NAME; 37 public static String KEYCHAIN = "jpackagerTest.keychain"; 38 39 private static void checkString(List<String> result, String lookupString) { 40 TKit.assertTextStream(lookupString).predicate( 41 (line, what) -> line.trim().equals(what)).apply(result.stream()); 42 } 43 44 private static List<String> codesignResult(Path target, boolean signed) { 45 int exitCode = signed ? 0 : 1; 46 List<String> result = new Executor() 47 .setExecutable("codesign") 48 .addArguments("--verify", "--deep", "--strict", "--verbose=2", 49 target.toString()) 50 .saveOutput() 51 .execute() 52 .assertExitCodeIs(exitCode).getOutput(); 53 54 return result; 55 } 56 57 private static void verifyCodesignResult(List<String> result, Path target, 58 boolean signed) { 59 result.stream().forEachOrdered(TKit::trace); 60 if (signed) { 61 String lookupString = target.toString() + ": valid on disk"; 62 checkString(result, lookupString); 63 lookupString = target.toString() + ": satisfies its Designated Requirement"; 64 checkString(result, lookupString); 65 } else { 66 String lookupString = target.toString() 67 + ": code object is not signed at all"; 68 checkString(result, lookupString); 69 } 70 } 71 72 private static List<String> spctlResult(Path target, String type) { 73 List<String> result = new Executor() 74 .setExecutable("/usr/sbin/spctl") 75 .addArguments("-vvv", "--assess", "--type", type, 76 target.toString()) 77 .executeAndGetOutput(); 78 79 return result; 80 } 81 82 private static void verifySpctlResult(List<String> result, Path target, String type) { 83 result.stream().forEachOrdered(TKit::trace); 84 String lookupString = target.toString() + ": accepted"; 85 checkString(result, lookupString); 86 lookupString = "source=" + DEV_NAME; 87 checkString(result, lookupString); 88 if (type.equals("install")) { 89 lookupString = "origin=" + INSTALLER_CERT; 90 } else { 91 lookupString = "origin=" + APP_CERT; 92 } 93 checkString(result, lookupString); 94 } 95 96 private static List<String> pkgutilResult(Path target) { 97 List<String> result = new Executor() 98 .setExecutable("/usr/sbin/pkgutil") 99 .addArguments("--check-signature", 100 target.toString()) 101 .executeAndGetOutput(); 102 103 return result; 104 } 105 106 private static void verifyPkgutilResult(List<String> result) { 107 result.stream().forEachOrdered(TKit::trace); 108 String lookupString = "Status: signed by a certificate trusted for current user"; 109 checkString(result, lookupString); 110 lookupString = "1. " + INSTALLER_CERT; 111 checkString(result, lookupString); 112 } 113 114 public static void verifyCodesign(Path target, boolean signed) { 115 List<String> result = codesignResult(target, signed); 116 verifyCodesignResult(result, target, signed); 117 } 118 119 public static void verifySpctl(Path target, String type) { 120 List<String> result = spctlResult(target, type); 121 verifySpctlResult(result, target, type); 122 } 123 124 public static void verifyPkgutil(Path target) { 125 List<String> result = pkgutilResult(target); 126 verifyPkgutilResult(result); 127 } 128 129 }