Skip to content

Commit

Permalink
timestamp.synology.com requires the full Content-Disposition head to …
Browse files Browse the repository at this point in the history
…be sent, including the filename section, e.g. Content-Disposition: form-data; name="file"; filename="ALLCAT.dat.asc"

// this shit took me 4 hours :(
  • Loading branch information
rednoah committed Nov 7, 2015
1 parent 1a95163 commit 75e31b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Example
<package dir="app" includes="**/*.sh" filemode="755" />
<package dir="app" excludes="**/*.sh" />

<codesign keyid="D545C93D" pubring="gpg/pubring.gpg" secring="gpg/secring.gpg" password="" />
<codesign keyid="D545C93D" secring="gpg/secring.gpg" password="" />
</syno:spk>

</target>
Expand Down
14 changes: 12 additions & 2 deletions src/net/filebot/ant/spk/CodeSignTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.UUID;

import net.filebot.ant.spk.openpgp.OpenPGPSignature;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -85,7 +89,7 @@ public void execute() {
}
}

asciiArmoredSignatureFile = signature.generate(true);
asciiArmoredSignatureFile = signature.generate();
} catch (IOException | SignatureException | PGPException e) {
throw new BuildException("Failed to compute PGP signature: " + e.getMessage());
}
Expand All @@ -96,16 +100,22 @@ public void execute() {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost httpPost = new HttpPost(timestamp);

HttpEntity pastData = MultipartEntityBuilder.create().addBinaryBody("file", asciiArmoredSignatureFile).build();
// timestamp.synology.com requires the full Content-Disposition head to be sent, including the filename section, e.g. Content-Disposition: form-data; name="file"; filename="ALLCAT.dat.asc"
HttpEntity pastData = MultipartEntityBuilder.create().addBinaryBody("file", asciiArmoredSignatureFile, ContentType.DEFAULT_BINARY, UUID.randomUUID().toString()).build();
httpPost.setEntity(pastData);

HttpResponse response = httpClient.execute(httpPost);
Files.copy(response.getEntity().getContent(), token.toPath(), StandardCopyOption.REPLACE_EXISTING);
dumpSignature(Files.readAllBytes(token.toPath()));
} catch (IOException e) {
throw new BuildException("Failed to retrieve signature: " + e.getMessage());
}
}

protected void dumpSignature(byte[] bytes) {
log(StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString());
}

protected Resource[] getTarOrderCatResources() {
TreeMap<String, Resource> sortedCats = new TreeMap<String, Resource>();
cats.forEach(fs -> {
Expand Down
17 changes: 7 additions & 10 deletions src/net/filebot/ant/spk/openpgp/OpenPGPSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.security.SignatureException;

Expand Down Expand Up @@ -47,17 +46,15 @@ public void update(byte[] buffer, int offset, int length) throws SignatureExcept
signature.update(buffer, offset, length);
}

public void generate(OutputStream output, boolean asciiArmor) throws IOException, SignatureException, PGPException {
if (asciiArmor) {
output = new ArmoredOutputStream(output);
public byte[] generate() throws IOException, SignatureException, PGPException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);

// make sure to call close() on these streams, because they will want to write some extra data at the end of the file
try (BCPGOutputStream out = new BCPGOutputStream(new ArmoredOutputStream(buffer))) {
signature.generate().encode(out);
}
signature.generate().encode(new BCPGOutputStream(output));
}

public byte[] generate(boolean asciiArmor) throws IOException, SignatureException, PGPException {
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
generate(out, asciiArmor);
return out.toByteArray();
return buffer.toByteArray();
}

public static OpenPGPSignature createSignatureGenerator(String keyId, File secring, char[] password) throws FileNotFoundException, IOException, PGPException {
Expand Down

0 comments on commit 75e31b6

Please sign in to comment.