1 /*
   2  * Copyright (c) 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.incubator.jpackage.internal;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.text.MessageFormat;
  31 import java.util.*;
  32 
  33 import static jdk.incubator.jpackage.internal.StandardBundlerParam.*;
  34 import static jdk.incubator.jpackage.internal.MacAppBundler.*;
  35 import static jdk.incubator.jpackage.internal.OverridableResource.createResource;
  36 
  37 public class MacAppStoreBundler extends MacBaseInstallerBundler {
  38 
  39     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  40             "jdk.incubator.jpackage.internal.resources.MacResources");
  41 
  42     private static final String TEMPLATE_BUNDLE_ICON_HIDPI = "java.icns";
  43 
  44     public static final BundlerParamInfo<String> MAC_APP_STORE_APP_SIGNING_KEY =
  45             new StandardBundlerParam<>(
  46             "mac.signing-key-app",
  47             String.class,
  48             params -> {
  49                 String result = MacBaseInstallerBundler.findKey(
  50                         "3rd Party Mac Developer Application: " +
  51                         SIGNING_KEY_USER.fetchFrom(params),
  52                         SIGNING_KEYCHAIN.fetchFrom(params),
  53                         VERBOSE.fetchFrom(params));
  54                 if (result != null) {
  55                     MacCertificate certificate = new MacCertificate(result);
  56 
  57                     if (!certificate.isValid()) {
  58                         Log.error(MessageFormat.format(
  59                                 I18N.getString("error.certificate.expired"),
  60                                 result));
  61                     }
  62                 }
  63 
  64                 return result;
  65             },
  66             (s, p) -> s);
  67 
  68     public static final BundlerParamInfo<String> MAC_APP_STORE_PKG_SIGNING_KEY =
  69             new StandardBundlerParam<>(
  70             "mac.signing-key-pkg",
  71             String.class,
  72             params -> {
  73                 String result = MacBaseInstallerBundler.findKey(
  74                         "3rd Party Mac Developer Installer: " +
  75                         SIGNING_KEY_USER.fetchFrom(params),
  76                         SIGNING_KEYCHAIN.fetchFrom(params),
  77                         VERBOSE.fetchFrom(params));
  78 
  79                 if (result != null) {
  80                     MacCertificate certificate = new MacCertificate(result);
  81 
  82                     if (!certificate.isValid()) {
  83                         Log.error(MessageFormat.format(
  84                                 I18N.getString("error.certificate.expired"),
  85                                 result));
  86                     }
  87                 }
  88 
  89                 return result;
  90             },
  91             (s, p) -> s);
  92 
  93     public static final BundlerParamInfo<String> INSTALLER_SUFFIX =
  94             new StandardBundlerParam<> (
  95             "mac.app-store.installerName.suffix",
  96             String.class,
  97             params -> "-MacAppStore",
  98             (s, p) -> s);
  99 
 100     public File bundle(Map<String, ? super Object> params,
 101             File outdir) throws PackagerException {
 102         Log.verbose(MessageFormat.format(I18N.getString(
 103                 "message.building-bundle"), APP_NAME.fetchFrom(params)));
 104 
 105         IOUtils.writableOutputDir(outdir.toPath());
 106 
 107         // first, load in some overrides
 108         // icns needs @2 versions, so load in the @2 default
 109         params.put(DEFAULT_ICNS_ICON.getID(), TEMPLATE_BUNDLE_ICON_HIDPI);
 110 
 111         // now we create the app
 112         File appImageDir = APP_IMAGE_TEMP_ROOT.fetchFrom(params);
 113         try {
 114             appImageDir.mkdirs();
 115 
 116             try {
 117                 MacAppImageBuilder.addNewKeychain(params);
 118             } catch (InterruptedException e) {
 119                 Log.error(e.getMessage());
 120             }
 121             // first, make sure we don't use the local signing key
 122             params.put(DEVELOPER_ID_APP_SIGNING_KEY.getID(), null);
 123             File appLocation = prepareAppBundle(params);
 124 
 125             String signingIdentity =
 126                     MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(params);
 127             String identifierPrefix =
 128                     BUNDLE_ID_SIGNING_PREFIX.fetchFrom(params);
 129             MacAppImageBuilder.prepareEntitlements(params);
 130 
 131             MacAppImageBuilder.signAppBundle(params, appLocation.toPath(),
 132                     signingIdentity, identifierPrefix,
 133                     MacAppImageBuilder.getConfig_Entitlements(params));
 134             MacAppImageBuilder.restoreKeychainList(params);
 135 
 136             ProcessBuilder pb;
 137 
 138             // create the final pkg file
 139             File finalPKG = new File(outdir, INSTALLER_NAME.fetchFrom(params)
 140                     + INSTALLER_SUFFIX.fetchFrom(params)
 141                     + ".pkg");
 142             outdir.mkdirs();
 143 
 144             String installIdentify =
 145                     MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(params);
 146 
 147             List<String> buildOptions = new ArrayList<>();
 148             buildOptions.add("productbuild");
 149             buildOptions.add("--component");
 150             buildOptions.add(appLocation.toString());
 151             buildOptions.add("/Applications");
 152             buildOptions.add("--sign");
 153             buildOptions.add(installIdentify);
 154             buildOptions.add("--product");
 155             buildOptions.add(appLocation + "/Contents/Info.plist");
 156             String keychainName = SIGNING_KEYCHAIN.fetchFrom(params);
 157             if (keychainName != null && !keychainName.isEmpty()) {
 158                 buildOptions.add("--keychain");
 159                 buildOptions.add(keychainName);
 160             }
 161             buildOptions.add(finalPKG.getAbsolutePath());
 162 
 163             pb = new ProcessBuilder(buildOptions);
 164 
 165             IOUtils.exec(pb);
 166             return finalPKG;
 167         } catch (PackagerException pe) {
 168             throw pe;
 169         } catch (Exception ex) {
 170             Log.verbose(ex);
 171             throw new PackagerException(ex);
 172         }
 173     }
 174 
 175     ///////////////////////////////////////////////////////////////////////
 176     // Implement Bundler
 177     ///////////////////////////////////////////////////////////////////////
 178 
 179     @Override
 180     public String getName() {
 181         return I18N.getString("store.bundler.name");
 182     }
 183 
 184     @Override
 185     public String getID() {
 186         return "mac.appStore";
 187     }
 188 
 189     @Override
 190     public boolean validate(Map<String, ? super Object> params)
 191             throws ConfigException {
 192         try {
 193             Objects.requireNonNull(params);
 194 
 195             // hdiutil is always available so there's no need to test for
 196             // availability.
 197             // run basic validation to ensure requirements are met
 198 
 199             // we are not interested in return code, only possible exception
 200             validateAppImageAndBundeler(params);
 201 
 202             // reject explicitly set to not sign
 203             if (!Optional.ofNullable(MacAppImageBuilder.
 204                     SIGN_BUNDLE.fetchFrom(params)).orElse(Boolean.TRUE)) {
 205                 throw new ConfigException(
 206                         I18N.getString("error.must-sign-app-store"),
 207                         I18N.getString("error.must-sign-app-store.advice"));
 208             }
 209 
 210             // make sure we have settings for signatures
 211             if (MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(params) == null) {
 212                 throw new ConfigException(
 213                         I18N.getString("error.no-app-signing-key"),
 214                         I18N.getString("error.no-app-signing-key.advice"));
 215             }
 216             if (MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(params) == null) {
 217                 throw new ConfigException(
 218                         I18N.getString("error.no-pkg-signing-key"),
 219                         I18N.getString("error.no-pkg-signing-key.advice"));
 220             }
 221 
 222             // things we could check...
 223             // check the icons, make sure it has hidpi icons
 224             // check the category,
 225             // make sure it fits in the list apple has provided
 226             // validate bundle identifier is reverse dns
 227             // check for \a+\.\a+\..
 228 
 229             return true;
 230         } catch (RuntimeException re) {
 231             if (re.getCause() instanceof ConfigException) {
 232                 throw (ConfigException) re.getCause();
 233             } else {
 234                 throw new ConfigException(re);
 235             }
 236         }
 237     }
 238 
 239     @Override
 240     public File execute(Map<String, ? super Object> params,
 241             File outputParentDir) throws PackagerException {
 242         return bundle(params, outputParentDir);
 243     }
 244 
 245     @Override
 246     public boolean supported(boolean runtimeInstaller) {
 247         // return (!runtimeInstaller &&
 248         //         Platform.getPlatform() == Platform.MAC);
 249         return false; // mac-app-store not yet supported
 250     }
 251 
 252     @Override
 253     public boolean isDefault() {
 254         return false;
 255     }
 256 
 257 }