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KAAK;YACvB,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,4CAA4C,CAAC,CAAC,CAAC,CAAC;gBAC1E,MAAM,KAAK,CAAA;YACb,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;IACD,IAAI,CAAC,CAAC;QACJ,MAAM,CAAC,MAAM,CAAA;IACf,CAAC;AACH,CAAC;AAZe,sBAAc,iBAY7B,CAAA;AAED,6BAAoC,OAAe;IACjD,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,WAAM,EAAE,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC,CAAA;IAC7D,MAAM,CAAC,sBAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;SAC/B,UAAU,CAAC,QAAQ,CAAC,CAAA;AACzB,CAAC;AAJe,2BAAmB,sBAIlC,CAAA","sourcesContent":["import { exec } from \"./util\"\nimport { deleteFile } from \"./promisifed-fs\"\nimport { download } from \"./httpRequest\"\nimport { tmpdir } from \"os\"\nimport * as path from \"path\"\nimport { executeFinally, all } from \"./promise\"\nimport { Promise as BluebirdPromise } from \"bluebird\"\nimport { randomBytes } from \"crypto\"\nimport { tsAwaiter } from \"./awaiter\"\n\nconst __awaiter = tsAwaiter\nArray.isArray(__awaiter)\n\nexport interface CodeSigningInfo {\n cscName: string\n cscKeychainName?: string\n}\n\nfunction randomString(): string {\n return randomBytes(8).toString(\"hex\")\n}\n\nexport function generateKeychainName(): string {\n return \"csc-\" + randomString() + \".keychain\"\n}\n\nexport function createKeychain(keychainName: string, cscLink: string, cscKeyPassword: string): Promise<CodeSigningInfo> {\n const appleCertPath = path.join(tmpdir(), randomString() + \".cer\")\n const developerCertPath = path.join(tmpdir(), randomString() + \".p12\")\n\n const keychainPassword = randomString()\n return executeFinally(Promise.all([\n download(\"https://developer.apple.com/certificationauthority/AppleWWDRCA.cer\", appleCertPath),\n download(cscLink, developerCertPath),\n BluebirdPromise.mapSeries([\n [\"create-keychain\", \"-p\", keychainPassword, keychainName],\n [\"unlock-keychain\", \"-p\", keychainPassword, keychainName],\n [\"set-keychain-settings\", \"-t\", \"3600\", \"-u\", keychainName]\n ], it => exec(\"security\", it))\n ])\n .then(() => importCerts(keychainName, appleCertPath, developerCertPath, cscKeyPassword)),\n error => {\n const tasks = [deleteFile(appleCertPath, true), deleteFile(developerCertPath, true)]\n if (error != null) {\n tasks.push(deleteKeychain(keychainName))\n }\n return all(tasks)\n })\n}\n\nasync function importCerts(keychainName: string, appleCertPath: string, developerCertPath: string, cscKeyPassword: string): Promise<CodeSigningInfo> {\n await exec(\"security\", [\"import\", appleCertPath, \"-k\", keychainName, \"-T\", \"/usr/bin/codesign\"])\n await exec(\"security\", [\"import\", developerCertPath, \"-k\", keychainName, \"-T\", \"/usr/bin/codesign\", \"-P\", cscKeyPassword])\n let cscName = await extractCommonName(cscKeyPassword, developerCertPath)\n return {\n cscName: cscName,\n cscKeychainName: keychainName\n }\n}\n\nfunction extractCommonName(password: string, certPath: string): BluebirdPromise<string> {\n return exec(\"openssl\", [\"pkcs12\", \"-nokeys\", \"-nodes\", \"-passin\", \"pass:\" + password, \"-nomacver\", \"-clcerts\", \"-in\", certPath])\n .then(result => {\n const match = result[0].toString().match(/^subject.*\\/CN=([^\\/]+)/m)\n if (match == null || match[1] == null) {\n throw new Error(\"Cannot extract common name from p12\")\n }\n else {\n return match[1]\n }\n })\n}\n\nexport function sign(path: string, options: CodeSigningInfo): BluebirdPromise<any> {\n const args = [\"--deep\", \"--force\", \"--sign\", options.cscName, path]\n if (options.cscKeychainName != null) {\n args.push(\"--keychain\", options.cscKeychainName)\n }\n return exec(\"codesign\", args)\n}\n\nexport function deleteKeychain(keychainName: string, ignoreNotFound: boolean = true): BluebirdPromise<any> {\n const result = exec(\"security\", [\"delete-keychain\", keychainName])\n if (ignoreNotFound) {\n return result.catch(error => {\n if (!error.message.includes(\"The specified keychain could not be found.\")) {\n throw error\n }\n })\n }\n else {\n return result\n }\n}\n\nexport function downloadCertificate(cscLink: string): Promise<string> {\n const certPath = path.join(tmpdir(), randomString() + \".p12\")\n return download(cscLink, certPath)\n .thenReturn(certPath)\n}"]} |