-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joaquim Rocha <joaquim.rocha@microsoft.com>
- Loading branch information
1 parent
5cf3f0d
commit 3b2546a
Showing
4 changed files
with
287 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
const { execSync } = require('child_process'); | ||
const path = require('path'); | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
|
||
const SIGN_JSON_TEMPLATE = { | ||
Version: '1.0.0', | ||
DriEmail: [`${process.env.HEADLAMP_WINDOWS_SIGN_EMAIL}`], | ||
GroupId: null, | ||
CorrelationVector: null, | ||
SignBatches: [], | ||
}; | ||
|
||
const POLICY_JSON = { | ||
Version: '1.0.0', | ||
Intent: '', | ||
ContentType: '', | ||
ContentOrigin: '', | ||
ProductState: '', | ||
Audience: '', | ||
}; | ||
|
||
const AUTH_JSON = { | ||
Version: '1.0.0', | ||
AuthenticationType: 'AAD_CERT', | ||
ClientId: `${process.env.HEADLAMP_WINDOWS_CLIENT_ID}`, | ||
AuthCert: { | ||
SubjectName: `CN=${process.env.HEADLAMP_WINDOWS_CLIENT_ID}.microsoft.com`, | ||
StoreLocation: 'LocalMachine', | ||
StoreName: 'My', | ||
SendX5c: 'true', | ||
}, | ||
RequestSigningCert: { | ||
SubjectName: `CN=${process.env.HEADLAMP_WINDOWS_CLIENT_ID}`, | ||
StoreLocation: 'LocalMachine', | ||
StoreName: 'My', | ||
}, | ||
}; | ||
|
||
function getFileList(rootDir) { | ||
let files = {}; | ||
let dirs = ['.']; | ||
while (dirs.length > 0) { | ||
const dirName = dirs.shift(); | ||
const curDir = path.join(rootDir, dirName); | ||
|
||
fs.readdirSync(curDir).forEach(file => { | ||
if (['node_modules', '.git'].includes(file)) { | ||
return; | ||
} | ||
const filepath = path.resolve(rootDir, dirName, file); | ||
const stat = fs.statSync(filepath); | ||
if (stat.isDirectory() && !files[file]) { | ||
dirs.push(path.join(dirName, file)); | ||
files[file] = []; | ||
} else { | ||
if (!files[dirName]) { | ||
files[dirName] = []; | ||
} | ||
|
||
files[dirName].push(file); | ||
} | ||
}); | ||
} | ||
return files; | ||
} | ||
|
||
function createJson(pathToSign, fileName = 'test_SignInput.json') { | ||
let rootDir = pathToSign; | ||
let files = {}; | ||
|
||
// Check if we are signing one single file or all files in a directory | ||
const stat = fs.statSync(pathToSign); | ||
if (stat.isFile()) { | ||
rootDir = path.dirname(pathToSign); | ||
files = { '.': [path.basename(pathToSign)] }; | ||
} else { | ||
files = getFileList(pathToSign); | ||
} | ||
|
||
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', pathToSign, stat, rootDir, files); | ||
|
||
const filesJson = (dir, files) => { | ||
return { | ||
SourceLocationType: 'UNC', | ||
SourceRootDirectory: path.resolve(rootDir, dir), | ||
SignRequestFiles: files.map(f => ({ | ||
SourceLocation: f, | ||
SourceHash: '', | ||
HashType: null, | ||
Name: f, | ||
})), | ||
SigningInfo: { | ||
Operations: [ | ||
{ | ||
KeyCode: 'CP-401337-Apple', | ||
|
||
OperationCode: 'MacAppNotarize', | ||
|
||
Parameters: { | ||
BundleId: 'com.microsoft.Headlamp', // this parameter enables the hardening flag during signing. | ||
}, | ||
|
||
ToolName: 'sign', | ||
|
||
ToolVersion: '1.0', | ||
}, | ||
], | ||
}, | ||
}; | ||
}; | ||
|
||
SIGN_JSON_TEMPLATE.SignBatches = Object.keys(files) | ||
.map(dir => filesJson(dir, files[dir])) | ||
.filter(f => f.SignRequestFiles.length > 0); | ||
|
||
console.log('>>>>>>>>SIGN', JSON.stringify(SIGN_JSON_TEMPLATE, undefined, 2)); | ||
|
||
const filePath = path.join(os.tmpdir(), fileName); | ||
fs.writeFileSync(filePath, JSON.stringify(SIGN_JSON_TEMPLATE, undefined, 2)); | ||
|
||
return filePath; | ||
} | ||
|
||
/** | ||
* Signs the given file, or all files in a given directory if that's what's passed to it. | ||
* @param esrpTool - The path to the ESRP tool. | ||
* @param pathToSign - A path to a file or directory. | ||
*/ | ||
function sign(esrpTool, pathToSign) { | ||
const absPathToSign = path.resolve(pathToSign); | ||
const signJsonBase = path.basename(absPathToSign).split('.')[0]; | ||
const signInputJson = createJson(absPathToSign, `${signJsonBase}-SignInput.json`); | ||
console.log('>>>>>>>>>>>>>>>>>>>>>>>||', absPathToSign); | ||
|
||
const policyJson = path.resolve(os.tmpdir(), 'Policy.json'); | ||
fs.writeFileSync(policyJson, JSON.stringify(POLICY_JSON, undefined, 2)); | ||
const authJson = path.resolve(os.tmpdir(), 'Auth.json'); | ||
fs.writeFileSync(authJson, JSON.stringify(AUTH_JSON, undefined, 2)); | ||
console.log('>>>>', `${esrpTool} Sign -a ${authJson} -p ${policyJson} -i ${signInputJson}`); | ||
console.log( | ||
'EXEC', | ||
execSync( | ||
`${esrpTool} Sign -l Verbose -a ${authJson} -p ${policyJson} -i ${signInputJson}`, | ||
(error, stdout, stderr) => { | ||
if (error) { | ||
console.log(`error: ${error.message}`); | ||
return; | ||
} | ||
if (stderr) { | ||
console.log(`stderr: ${stderr}`); | ||
return; | ||
} | ||
console.log(`stdout: ${stdout}`); | ||
} | ||
) | ||
); | ||
} | ||
|
||
// module.exports = { | ||
// sign, | ||
// }; | ||
|
||
if (require.main === module) { | ||
console.log('>>>>>>>>>>', process.argv.slice(2)); | ||
sign(process.env.ESRP_PATH, process.argv[2]); | ||
process.exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters