Skip to content

Commit

Permalink
add optional cached sessionKey to OpenPgpDecryptionResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Valodim committed Apr 29, 2016
1 parent 710a0d8 commit f027645
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -73,6 +84,9 @@ public void writeToParcel(Parcel dest, int flags) {
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);
Expand All @@ -82,12 +96,15 @@ public void writeToParcel(Parcel dest, int flags) {

public static final Creator<OpenPgpDecryptionResult> CREATOR = new Creator<OpenPgpDecryptionResult>() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit f027645

Please sign in to comment.