UNPKG

3.94 kBJavaScriptView Raw
1"use strict";
2const util_1 = require("./util");
3const promisifed_fs_1 = require("./promisifed-fs");
4const httpRequest_1 = require("./httpRequest");
5const os_1 = require("os");
6const path = require("path");
7const promise_1 = require("./promise");
8const bluebird_1 = require("bluebird");
9const crypto_1 = require("crypto");
10const awaiter_1 = require("./awaiter");
11const __awaiter = awaiter_1.tsAwaiter;
12Array.isArray(__awaiter);
13function randomString() {
14 return crypto_1.randomBytes(8).toString("hex");
15}
16function generateKeychainName() {
17 return "csc-" + randomString() + ".keychain";
18}
19exports.generateKeychainName = generateKeychainName;
20function createKeychain(keychainName, cscLink, cscKeyPassword) {
21 const appleCertPath = path.join(os_1.tmpdir(), randomString() + ".cer");
22 const developerCertPath = path.join(os_1.tmpdir(), randomString() + ".p12");
23 const keychainPassword = randomString();
24 return promise_1.executeFinally(Promise.all([
25 httpRequest_1.download("https://developer.apple.com/certificationauthority/AppleWWDRCA.cer", appleCertPath),
26 httpRequest_1.download(cscLink, developerCertPath),
27 bluebird_1.Promise.mapSeries([
28 ["create-keychain", "-p", keychainPassword, keychainName],
29 ["unlock-keychain", "-p", keychainPassword, keychainName],
30 ["set-keychain-settings", "-t", "3600", "-u", keychainName]
31 ], it => util_1.exec("security", it))
32 ])
33 .then(() => importCerts(keychainName, appleCertPath, developerCertPath, cscKeyPassword)), error => {
34 const tasks = [promisifed_fs_1.deleteFile(appleCertPath, true), promisifed_fs_1.deleteFile(developerCertPath, true)];
35 if (error != null) {
36 tasks.push(deleteKeychain(keychainName));
37 }
38 return promise_1.all(tasks);
39 });
40}
41exports.createKeychain = createKeychain;
42function importCerts(keychainName, appleCertPath, developerCertPath, cscKeyPassword) {
43 return __awaiter(this, void 0, void 0, function* () {
44 yield util_1.exec("security", ["import", appleCertPath, "-k", keychainName, "-T", "/usr/bin/codesign"]);
45 yield util_1.exec("security", ["import", developerCertPath, "-k", keychainName, "-T", "/usr/bin/codesign", "-P", cscKeyPassword]);
46 let cscName = yield extractCommonName(cscKeyPassword, developerCertPath);
47 return {
48 cscName: cscName,
49 cscKeychainName: keychainName
50 };
51 });
52}
53function extractCommonName(password, certPath) {
54 return util_1.exec("openssl", ["pkcs12", "-nokeys", "-nodes", "-passin", "pass:" + password, "-nomacver", "-clcerts", "-in", certPath])
55 .then(result => {
56 const match = result[0].toString().match(/^subject.*\/CN=([^\/]+)/m);
57 if (match == null || match[1] == null) {
58 throw new Error("Cannot extract common name from p12");
59 }
60 else {
61 return match[1];
62 }
63 });
64}
65function sign(path, options) {
66 const args = ["--deep", "--force", "--sign", options.cscName, path];
67 if (options.cscKeychainName != null) {
68 args.push("--keychain", options.cscKeychainName);
69 }
70 return util_1.exec("codesign", args);
71}
72exports.sign = sign;
73function deleteKeychain(keychainName, ignoreNotFound = true) {
74 const result = util_1.exec("security", ["delete-keychain", keychainName]);
75 if (ignoreNotFound) {
76 return result.catch(error => {
77 if (!error.message.includes("The specified keychain could not be found.")) {
78 throw error;
79 }
80 });
81 }
82 else {
83 return result;
84 }
85}
86exports.deleteKeychain = deleteKeychain;
87function downloadCertificate(cscLink) {
88 const certPath = path.join(os_1.tmpdir(), randomString() + ".p12");
89 return httpRequest_1.download(cscLink, certPath)
90 .thenReturn(certPath);
91}
92exports.downloadCertificate = downloadCertificate;
93//# sourceMappingURL=codeSign.js.map
\No newline at end of file