From f027645214ff41a54e15cc46058ce9f1867cad5f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 29 Apr 2016 15:46:03 +0200 Subject: add optional cached sessionKey to OpenPgpDecryptionResult --- .../openpgp/OpenPgpDecryptionResult.java | 39 ++++++++++++++++------ .../org/openintents/openpgp/util/OpenPgpApi.java | 2 ++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java index 2090b55..513b502 100644 --- a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java +++ b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java @@ -25,7 +25,7 @@ public class OpenPgpDecryptionResult implements Parcelable { * old versions of the protocol (and thus old versions of this class), we need a versioning * system for the parcels sent between the clients and the providers. */ - public static final int PARCELABLE_VERSION = 1; + public static final int PARCELABLE_VERSION = 2; // content not encrypted public static final int RESULT_NOT_ENCRYPTED = -1; @@ -34,26 +34,37 @@ public class OpenPgpDecryptionResult implements Parcelable { // encrypted public static final int RESULT_ENCRYPTED = 1; - int result; + public final int result; + public final byte[] sessionKey; + public final byte[] decryptedSessionKey; public int getResult() { return result; } - public void setResult(int result) { + public OpenPgpDecryptionResult(int result) { this.result = result; + this.sessionKey = null; + this.decryptedSessionKey = null; } - public OpenPgpDecryptionResult() { - - } - - public OpenPgpDecryptionResult(int result) { + public OpenPgpDecryptionResult(int result, byte[] sessionKey, byte[] decryptedSessionKey) { this.result = result; + if ((sessionKey == null) != (decryptedSessionKey == null)) { + throw new AssertionError("sessionkey must be null iff decryptedSessionKey is null"); + } + this.sessionKey = sessionKey; + this.decryptedSessionKey = decryptedSessionKey; } public OpenPgpDecryptionResult(OpenPgpDecryptionResult b) { this.result = b.result; + this.sessionKey = b.sessionKey; + this.decryptedSessionKey = b.decryptedSessionKey; + } + + public boolean hasDecryptedSessionKey() { + return sessionKey != null; } public int describeContents() { @@ -73,6 +84,9 @@ public class OpenPgpDecryptionResult implements Parcelable { int startPosition = dest.dataPosition(); // version 1 dest.writeInt(result); + // version 2 + dest.writeByteArray(sessionKey); + dest.writeByteArray(decryptedSessionKey); // Go back and write the size int parcelableSize = dest.dataPosition() - startPosition; dest.setDataPosition(sizePosition); @@ -82,12 +96,15 @@ public class OpenPgpDecryptionResult implements Parcelable { public static final Creator CREATOR = new Creator() { public OpenPgpDecryptionResult createFromParcel(final Parcel source) { - source.readInt(); // parcelableVersion + int version = source.readInt(); // parcelableVersion int parcelableSize = source.readInt(); int startPosition = source.dataPosition(); - OpenPgpDecryptionResult vr = new OpenPgpDecryptionResult(); - vr.result = source.readInt(); + int result = source.readInt(); + byte[] sessionKey = version > 1 ? source.createByteArray() : null; + byte[] decryptedSessionKey = version > 1 ? source.createByteArray() : null; + + OpenPgpDecryptionResult vr = new OpenPgpDecryptionResult(result, sessionKey, decryptedSessionKey); // skip over all fields added in future versions of this parcel source.setDataPosition(startPosition + parcelableSize); diff --git a/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index fe019ab..5fb2382 100644 --- a/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -261,6 +261,8 @@ public class OpenPgpApi { // DECRYPT_VERIFY public static final String EXTRA_DETACHED_SIGNATURE = "detached_signature"; + public static final String EXTRA_DECRYPTION_RESULT_WRAPPER = "decryption_result_wrapper"; + public static final String EXTRA_DECRYPTION_RESULT = "decryption_result"; public static final String RESULT_SIGNATURE = "signature"; public static final String RESULT_DECRYPTION = "decryption"; public static final String RESULT_METADATA = "metadata"; -- cgit v1.2.3