Skip to content

Commit

Permalink
Replaced PEFile.writeDataDirectory() with writeCertificateTable()
Browse files Browse the repository at this point in the history
  • Loading branch information
ebourg committed Mar 6, 2024
1 parent 61cbdd0 commit f20a977
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 53 deletions.
9 changes: 0 additions & 9 deletions jsign-core/src/main/java/net/jsign/pe/DataDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ void check() throws IOException {
}
}

/**
* Fill the data directory with zeros.
*
* @since 2.0
*/
public void erase() throws IOException {
peFile.write(getVirtualAddress(), new byte[getSize()]);
}

/**
* Tells if the data directory is at the end of the file.
*
Expand Down
59 changes: 15 additions & 44 deletions jsign-core/src/main/java/net/jsign/pe/PEFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,18 +641,16 @@ public DataDirectory getDataDirectory(DataDirectoryType type) {
}

/**
* Writes the data directory of the specified type. The data is either appended
* at the end of the file or written over the previous data of the same type if
* there is enough space.
* Writes the certificate table. The data is either appended at the end of the file
* or written over the previous certificate table.
*
* @param type the type of the data directory
* @param data the content of the data directory
* @param data the content of the certificate table
* @throws IOException if an I/O error occurs
*/
public synchronized void writeDataDirectory(DataDirectoryType type, byte[] data) throws IOException {
DataDirectory directory = getDataDirectory(type);
synchronized void writeCertificateTable(byte[] data) throws IOException {
DataDirectory directory = getDataDirectory(DataDirectoryType.CERTIFICATE_TABLE);
if (directory == null) {
throw new IOException("No space allocated in the data directories index for the " + type + " directory");
throw new IOException("No space allocated in the data directories index for the certificate table");
}

if (!directory.exists()) {
Expand All @@ -664,43 +662,16 @@ public synchronized void writeDataDirectory(DataDirectoryType type, byte[] data)
// update the entry in the data directory table
directory.write(offset, data.length);

} else {
if (data.length == directory.getSize()) {
// same size as before, just overwrite
write(directory.getVirtualAddress(), data);

} else if (data.length < directory.getSize() && type != DataDirectoryType.CERTIFICATE_TABLE) {
// the new data is smaller, erase and rewrite in-place
// this doesn't work with the certificate table since it changes the file digest
directory.erase();
write(directory.getVirtualAddress(), data);

// update the size in the data directory table
directory.write(directory.getVirtualAddress(), data.length);
} else if (directory.isTrailing()) {
// the data is at the end of the file, overwrite it
write(directory.getVirtualAddress(), data);
channel.truncate(directory.getVirtualAddress() + data.length); // trim the file if the data shrunk

} else if (directory.isTrailing()) {
// the data is at the end of the file, overwrite it
write(directory.getVirtualAddress(), data);
channel.truncate(directory.getVirtualAddress() + data.length); // trim the file if the data shrunk

// update the size in the data directory table
directory.write(directory.getVirtualAddress(), data.length);
// update the size in the data directory table
directory.write(directory.getVirtualAddress(), data.length);

} else {
if (type == DataDirectoryType.CERTIFICATE_TABLE) {
throw new IOException("The certificate table isn't at the end of the file and can't be moved without invalidating the signature");
}

// the new data is larger, erase and relocate it at the end
directory.erase();

long offset = channel.size();

write(offset, data);

// update the entry in the data directory table
directory.write(offset, data.length);
}
} else {
throw new IOException("The certificate table isn't at the end of the file");
}

updateChecksum();
Expand Down Expand Up @@ -740,7 +711,7 @@ public synchronized List<CMSSignedData> getSignatures() {
public void setSignature(CMSSignedData signature) throws IOException {
if (signature != null) {
CertificateTableEntry entry = new CertificateTableEntry(signature);
writeDataDirectory(DataDirectoryType.CERTIFICATE_TABLE, entry.toBytes());
writeCertificateTable(entry.toBytes());

} else if (getDataDirectory(DataDirectoryType.CERTIFICATE_TABLE).exists()) {
// erase the previous signature
Expand Down

0 comments on commit f20a977

Please sign in to comment.