Skip to content

Commit

Permalink
Wip: Implement result parsing for decrypt, inline-detach and inline-v…
Browse files Browse the repository at this point in the history
…erify
  • Loading branch information
vanitasvitae committed Jan 12, 2023
1 parent 63f7255 commit e72d4d4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
4 changes: 2 additions & 2 deletions external-sop/src/main/java/sop/external/ExternalSOP.java
Expand Up @@ -98,12 +98,12 @@ public DetachedVerify detachedVerify() {

@Override
public InlineVerify inlineVerify() {
return new InlineVerifyExternal(binaryName, properties);
return new InlineVerifyExternal(binaryName, properties, tempDirProvider);
}

@Override
public InlineDetach inlineDetach() {
return new InlineDetachExternal(binaryName, properties);
return new InlineDetachExternal(binaryName, properties, tempDirProvider);
}

@Override
Expand Down
Expand Up @@ -163,7 +163,7 @@ public DecryptionResult writeTo(OutputStream outputStream) throws IOException {
reader.close();
}

return new DecryptionResult(sessionKey, verifications); // TODO
return new DecryptionResult(sessionKey, verifications);
}
};
} catch (IOException e) {
Expand Down
Expand Up @@ -10,6 +10,9 @@
import sop.external.ExternalSOP;
import sop.operation.InlineDetach;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -19,10 +22,12 @@

public class InlineDetachExternal implements InlineDetach {

private final ExternalSOP.TempDirProvider tempDirProvider;
private final List<String> commandList = new ArrayList<>();
private final List<String> envList;

public InlineDetachExternal(String binary, Properties environment) {
public InlineDetachExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) {
this.tempDirProvider = tempDirProvider;
commandList.add(binary);
commandList.add("inline-detach");
envList = ExternalSOP.propertiesToEnv(environment);
Expand All @@ -36,6 +41,12 @@ public InlineDetach noArmor() {

@Override
public ReadyWithResult<Signatures> message(InputStream messageInputStream) throws IOException, SOPGPException.BadData {
File tempDir = tempDirProvider.provideTempDirectory();

File signaturesOut = new File(tempDir, "signatures");
signaturesOut.delete();
commandList.add("--signatures-out=" + signaturesOut.getAbsolutePath());

String[] command = commandList.toArray(new String[0]);
String[] env = envList.toArray(new String[0]);

Expand Down Expand Up @@ -65,7 +76,21 @@ public Signatures writeTo(OutputStream outputStream) throws IOException {

ExternalSOP.finish(process);

return null; // TODO
FileInputStream signaturesOutIn = new FileInputStream(signaturesOut);
ByteArrayOutputStream signaturesBuffer = new ByteArrayOutputStream();
while ((r = signaturesOutIn.read(buf)) > 0) {
signaturesBuffer.write(buf, 0, r);
}
signaturesOutIn.close();
signaturesOut.delete();

final byte[] sigBytes = signaturesBuffer.toByteArray();
return new Signatures() {
@Override
public void writeTo(OutputStream signatureOutputStream) throws IOException {
signatureOutputStream.write(sigBytes);
}
};
}
};
} catch (IOException e) {
Expand Down
Expand Up @@ -11,8 +11,12 @@
import sop.operation.InlineVerify;
import sop.util.UTCUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -21,12 +25,14 @@

public class InlineVerifyExternal implements InlineVerify {

private final ExternalSOP.TempDirProvider tempDirProvider;
private final List<String> commandList = new ArrayList<>();
private final List<String> envList;

private int certCounter = 0;

public InlineVerifyExternal(String binary, Properties environment) {
public InlineVerifyExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) {
this.tempDirProvider = tempDirProvider;
commandList.add(binary);
commandList.add("inline-verify");
envList = ExternalSOP.propertiesToEnv(environment);
Expand Down Expand Up @@ -54,6 +60,12 @@ public InlineVerify cert(InputStream cert) throws SOPGPException.BadData, IOExce

@Override
public ReadyWithResult<List<Verification>> data(InputStream data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
File tempDir = tempDirProvider.provideTempDirectory();

File verificationsOut = new File(tempDir, "verifications-out");
verificationsOut.delete();
commandList.add("--verifications-out=" + verificationsOut.getAbsolutePath());

String[] command = commandList.toArray(new String[0]);
String[] env = envList.toArray(new String[0]);

Expand Down Expand Up @@ -84,7 +96,15 @@ public List<Verification> writeTo(OutputStream outputStream) throws IOException,

ExternalSOP.finish(process);

return null; // TODO
FileInputStream verificationsOutIn = new FileInputStream(verificationsOut);
BufferedReader reader = new BufferedReader(new InputStreamReader(verificationsOutIn));
List<Verification> verificationList = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
verificationList.add(Verification.fromString(line.trim()));
}

return verificationList;
}
};
} catch (IOException e) {
Expand Down

0 comments on commit e72d4d4

Please sign in to comment.