Fixes for detached signatures

This commit is contained in:
Dominik Schürmann
2015-01-26 17:33:40 +01:00
parent 34a7728cec
commit fc786280fd
2 changed files with 36 additions and 11 deletions

View File

@@ -428,6 +428,7 @@ public class PgpSignEncrypt extends BaseOperation {
BCPGOutputStream bcpgOut; BCPGOutputStream bcpgOut;
ByteArrayOutputStream detachedByteOut = null; ByteArrayOutputStream detachedByteOut = null;
ArmoredOutputStream detachedArmorOut = null;
BCPGOutputStream detachedBcpgOut = null; BCPGOutputStream detachedBcpgOut = null;
try { try {
@@ -535,7 +536,12 @@ public class PgpSignEncrypt extends BaseOperation {
detachedByteOut = new ByteArrayOutputStream(); detachedByteOut = new ByteArrayOutputStream();
OutputStream detachedOut = detachedByteOut; OutputStream detachedOut = detachedByteOut;
if (mEnableAsciiArmorOutput) { if (mEnableAsciiArmorOutput) {
detachedOut = new ArmoredOutputStream(detachedOut); detachedArmorOut = new ArmoredOutputStream(detachedOut);
if (mVersionHeader != null) {
detachedArmorOut.setHeader("Version", mVersionHeader);
}
detachedOut = detachedArmorOut;
} }
detachedBcpgOut = new BCPGOutputStream(detachedOut); detachedBcpgOut = new BCPGOutputStream(detachedOut);
@@ -614,27 +620,38 @@ public class PgpSignEncrypt extends BaseOperation {
// Note that the checked key here is the master key, not the signing key // Note that the checked key here is the master key, not the signing key
// (although these are always the same on Yubikeys) // (although these are always the same on Yubikeys)
result.setNfcData(mSignatureSubKeyId, e.hashToSign, e.hashAlgo, e.creationTimestamp, mSignaturePassphrase); result.setNfcData(mSignatureSubKeyId, e.hashToSign, e.hashAlgo, e.creationTimestamp, mSignaturePassphrase);
Log.d(Constants.TAG, "e.hashToSign"+ Hex.toHexString(e.hashToSign)); Log.d(Constants.TAG, "e.hashToSign" + Hex.toHexString(e.hashToSign));
return result; return result;
} }
} }
// closing outputs // closing outputs
// NOTE: closing needs to be done in the correct order! // NOTE: closing needs to be done in the correct order!
// TODO: closing bcpgOut and pOut??? if (encryptionOut != null) {
if (enableEncryption) { if (compressGen != null) {
if (enableCompression) {
compressGen.close(); compressGen.close();
} }
encryptionOut.close(); encryptionOut.close();
} }
if (mEnableAsciiArmorOutput) { // Note: Closing ArmoredOutputStream does not close the underlying stream
if (armorOut != null) {
armorOut.close(); armorOut.close();
} }
// Note: Closing ArmoredOutputStream does not close the underlying stream
out.close(); if (detachedArmorOut != null) {
mOutStream.close(); detachedArmorOut.close();
}
// Also closes detachedBcpgOut
if (detachedByteOut != null) {
detachedByteOut.close();
}
if (out != null) {
out.close();
}
if (mOutStream != null) {
mOutStream.close();
}
} catch (SignatureException e) { } catch (SignatureException e) {
log.add(LogType.MSG_SE_ERROR_SIG, indent); log.add(LogType.MSG_SE_ERROR_SIG, indent);

View File

@@ -244,7 +244,12 @@ public class OpenPgpService extends RemoteService {
// Get Input- and OutputStream from ParcelFileDescriptor // Get Input- and OutputStream from ParcelFileDescriptor
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input); InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
OutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(output); OutputStream os = null;
if (cleartextSign) {
// output stream only needed for cleartext signatures,
// detached signatures are returned as extra
os = new ParcelFileDescriptor.AutoCloseOutputStream(output);
}
try { try {
long inputLength = is.available(); long inputLength = is.available();
InputData inputData = new InputData(is, inputLength); InputData inputData = new InputData(is, inputLength);
@@ -325,7 +330,9 @@ public class OpenPgpService extends RemoteService {
} }
} finally { } finally {
is.close(); is.close();
os.close(); if (os != null) {
os.close();
}
} }
} catch (Exception e) { } catch (Exception e) {
Log.d(Constants.TAG, "signImpl", e); Log.d(Constants.TAG, "signImpl", e);
@@ -720,6 +727,7 @@ public class OpenPgpService extends RemoteService {
return signImpl(data, input, output, accSettings, true); return signImpl(data, input, output, accSettings, true);
} else if (OpenPgpApi.ACTION_SIGN.equals(action)) { } else if (OpenPgpApi.ACTION_SIGN.equals(action)) {
// DEPRECATED: same as ACTION_CLEARTEXT_SIGN // DEPRECATED: same as ACTION_CLEARTEXT_SIGN
Log.w(Constants.TAG, "You are using a deprecated API call, please use ACTION_CLEARTEXT_SIGN instead of ACTION_SIGN!");
return signImpl(data, input, output, accSettings, true); return signImpl(data, input, output, accSettings, true);
} else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(action)) { } else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(action)) {
return signImpl(data, input, output, accSettings, false); return signImpl(data, input, output, accSettings, false);