"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.sign = undefined;

var _bluebirdLst;

function _load_bluebirdLst() {
    return _bluebirdLst = require("bluebird-lst");
}

let sign = exports.sign = (() => {
    var _ref = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* (options) {
        let hashes = options.options.signingHashAlgorithms;
        // msi does not support dual-signing
        if (options.path.endsWith(".msi")) {
            hashes = [hashes != null && !(hashes.indexOf("sha1") !== -1) ? "sha256" : "sha1"];
        } else if (options.path.endsWith(".appx")) {
            hashes = ["sha256"];
        } else if (hashes == null) {
            hashes = ["sha1", "sha256"];
        } else {
            hashes = Array.isArray(hashes) ? hashes : [hashes];
        }
        const executor = (0, (_platformPackager || _load_platformPackager()).resolveFunction)(options.options.sign) || doSign;
        let isNest = false;
        for (const hash of hashes) {
            const taskConfiguration = Object.assign({}, options, { hash, isNest });
            yield executor(Object.assign({}, taskConfiguration, { computeSignToolArgs: function (isWin) {
                    return computeSignToolArgs(taskConfiguration, isWin);
                } }));
            isNest = true;
            if (taskConfiguration.resultOutputPath != null) {
                yield (0, (_fsExtraP || _load_fsExtraP()).rename)(taskConfiguration.resultOutputPath, options.path);
            }
        }
    });

    return function sign(_x) {
        return _ref.apply(this, arguments);
    };
})();

let doSign = (() => {
    var _ref2 = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* (configuration) {
        const toolInfo = yield getToolPath();
        yield (0, (_builderUtil || _load_builderUtil()).exec)(toolInfo.path, configuration.computeSignToolArgs(process.platform === "win32"), {
            // https://github.com/electron-userland/electron-builder/pull/1944
            timeout: parseInt(process.env.SIGNTOOL_TIMEOUT, 10) || 10 * 60 * 1000,
            env: toolInfo.env || process.env
        });
    });

    return function doSign(_x2) {
        return _ref2.apply(this, arguments);
    };
})();
// on windows be aware of http://stackoverflow.com/a/32640183/1910191


let getToolPath = (() => {
    var _ref3 = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
        if ((0, (_flags || _load_flags()).isUseSystemSigncode)()) {
            return { path: "osslsigncode" };
        }
        const result = process.env.SIGNTOOL_PATH;
        if (result) {
            return { path: result };
        }
        const vendorPath = yield getSignVendorPath();
        if (process.platform === "win32") {
            // use modern signtool on Windows Server 2012 R2 to be able to sign AppX
            if (isOldWin6()) {
                return { path: _path.join(vendorPath, "windows-6", "signtool.exe") };
            } else {
                return { path: _path.join(vendorPath, "windows-10", process.arch, "signtool.exe") };
            }
        } else if (process.platform === "darwin") {
            let suffix = null;
            try {
                if (yield (0, (_builderUtil || _load_builderUtil()).isMacOsSierra)()) {
                    const toolDirPath = _path.join(vendorPath, process.platform, "10.12");
                    return {
                        path: _path.join(toolDirPath, "osslsigncode"),
                        env: (0, (_bundledTool || _load_bundledTool()).computeToolEnv)([_path.join(toolDirPath, "lib")])
                    };
                } else if ((_isCi || _load_isCi()).default) {
                    // not clear for what we do this instead of using version detection
                    suffix = "ci";
                }
            } catch (e) {
                (0, (_builderUtil || _load_builderUtil()).warn)(`${e.stack || e}`);
            }
            return { path: _path.join(vendorPath, process.platform, `${suffix == null ? "" : `${suffix}/`}osslsigncode`) };
        } else {
            return { path: _path.join(vendorPath, process.platform, "osslsigncode") };
        }
    });

    return function getToolPath() {
        return _ref3.apply(this, arguments);
    };
})();
//# sourceMappingURL=windowsCodeSign.js.map


exports.getSignVendorPath = getSignVendorPath;
exports.isOldWin6 = isOldWin6;

var _builderUtil;

function _load_builderUtil() {
    return _builderUtil = require("builder-util");
}

var _binDownload;

function _load_binDownload() {
    return _binDownload = require("builder-util/out/binDownload");
}

var _bundledTool;

function _load_bundledTool() {
    return _bundledTool = require("builder-util/out/bundledTool");
}

var _fsExtraP;

function _load_fsExtraP() {
    return _fsExtraP = require("fs-extra-p");
}

var _isCi;

function _load_isCi() {
    return _isCi = _interopRequireDefault(require("is-ci"));
}

var _os;

function _load_os() {
    return _os = _interopRequireWildcard(require("os"));
}

var _path = _interopRequireWildcard(require("path"));

var _platformPackager;

function _load_platformPackager() {
    return _platformPackager = require("./platformPackager");
}

var _flags;

function _load_flags() {
    return _flags = require("./util/flags");
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function getSignVendorPath() {
    //noinspection SpellCheckingInspection
    return (0, (_binDownload || _load_binDownload()).getBinFromGithub)("winCodeSign", "1.9.0", "cyhO9Mv5MTP2o9dwk/+qs0KvuO9CbDhjEJXA2ujpvhcsk5zmc+zY9iqiWXVzOuibTLYNC3qZiuFlJrrCT2kldw==");
}
function computeSignToolArgs(options, isWin) {
    const outputPath = isWin ? options.path : getOutputPath(options.path, options.hash);
    if (!isWin) {
        options.resultOutputPath = outputPath;
    }
    const args = isWin ? ["sign"] : ["-in", options.path, "-out", outputPath];
    if (process.env.ELECTRON_BUILDER_OFFLINE !== "true") {
        const timestampingServiceUrl = options.options.timeStampServer || "http://timestamp.verisign.com/scripts/timstamp.dll";
        if (isWin) {
            args.push(options.isNest || options.hash === "sha256" ? "/tr" : "/t", options.isNest || options.hash === "sha256" ? options.options.rfc3161TimeStampServer || "http://timestamp.comodoca.com/rfc3161" : timestampingServiceUrl);
        } else {
            args.push("-t", timestampingServiceUrl);
        }
    }
    const certificateFile = options.cert;
    if (certificateFile == null) {
        const subjectName = options.options.certificateSubjectName;
        if (process.platform !== "win32") {
            throw new Error(`${subjectName == null ? "certificateSha1" : "certificateSubjectName"} supported only on Windows`);
        }
        if (subjectName == null) {
            args.push("/sha1", options.options.certificateSha1);
        } else {
            args.push("/n", subjectName);
        }
    } else {
        const certExtension = _path.extname(certificateFile);
        if (certExtension === ".p12" || certExtension === ".pfx") {
            args.push(isWin ? "/f" : "-pkcs12", certificateFile);
        } else {
            throw new Error(`Please specify pkcs12 (.p12/.pfx) file, ${certificateFile} is not correct`);
        }
    }
    if (!isWin || options.hash !== "sha1") {
        args.push(isWin ? "/fd" : "-h", options.hash);
        if (isWin && process.env.ELECTRON_BUILDER_OFFLINE !== "true") {
            args.push("/td", "sha256");
        }
    }
    if (options.name) {
        args.push(isWin ? "/d" : "-n", options.name);
    }
    if (options.site) {
        args.push(isWin ? "/du" : "-i", options.site);
    }
    // msi does not support dual-signing
    if (options.isNest) {
        args.push(isWin ? "/as" : "-nest");
    }
    if (options.password) {
        args.push(isWin ? "/p" : "-pass", options.password);
    }
    if (options.options.additionalCertificateFile) {
        args.push(isWin ? "/ac" : "-ac", options.options.additionalCertificateFile);
    }
    if (isWin) {
        // must be last argument
        args.push(options.path);
    }
    return args;
}
function getOutputPath(inputPath, hash) {
    const extension = _path.extname(inputPath);
    return _path.join(_path.dirname(inputPath), `${_path.basename(inputPath, extension)}-signed-${hash}${extension}`);
}
/** @internal */
function isOldWin6() {
    const winVersion = (_os || _load_os()).release();
    return winVersion.startsWith("6.") && !winVersion.startsWith("6.3");
}