in jsign-core/src/main/java/net/jsign/AuthenticodeSigner.java [317:359]
public void sign(Signable file) throws Exception {
if (file instanceof PEFile) {
PEFile pefile = (PEFile) file;
// pad the file on a 8 byte boundary (signtool refuses to sign files not properly padded)
// todo only if there was no previous certificate table
pefile.pad(8);
if (replace) {
DataDirectory certificateTable = pefile.getDataDirectory(DataDirectoryType.CERTIFICATE_TABLE);
if (certificateTable != null && !certificateTable.isTrailing()) {
// erase the previous signature
certificateTable.erase();
certificateTable.write(0, 0);
}
}
} else if (file instanceof MSIFile) {
MSIFile msi = (MSIFile) file;
if (!replace && msi.hasExtendedSignature()) {
throw new UnsupportedOperationException("The file has an extended signature which isn't supported by Jsign, it can't be signed without replacing the existing signature");
}
}
CMSSignedData sigData = createSignedData(file);
if (!replace) {
List<CMSSignedData> signatures = file.getSignatures();
if (!signatures.isEmpty()) {
// append the nested signature
sigData = addNestedSignature(signatures.get(0), sigData);
}
}
file.setSignature(sigData);
file.save();
if (file instanceof Closeable) {
((Closeable) file).close();
}
}