From b42636ec3ec63bef61af264a0a9b09998db8186b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 30 Jul 2015 21:36:28 +0200 Subject: New OpenPgpDecryptionResult, introduce UNSIGNED, UNENCRYPTED results instead of return types, rework constant naming in OpenPgpSignatureResult --- .../openpgp/OpenPgpDecryptionResult.java | 108 +++++++++++++++++++++ .../java/org/openintents/openpgp/OpenPgpError.java | 6 +- .../org/openintents/openpgp/OpenPgpMetadata.java | 6 +- .../openpgp/OpenPgpSignatureResult.java | 54 ++++++----- .../org/openintents/openpgp/util/OpenPgpApi.java | 23 ++--- 5 files changed, 151 insertions(+), 46 deletions(-) create mode 100644 openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java diff --git a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java new file mode 100644 index 0000000..2090b55 --- /dev/null +++ b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpDecryptionResult.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2015 Dominik Schürmann + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openintents.openpgp; + +import android.os.Parcel; +import android.os.Parcelable; + +public class OpenPgpDecryptionResult implements Parcelable { + /** + * Since there might be a case where new versions of the client using the library getting + * 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; + + // content not encrypted + public static final int RESULT_NOT_ENCRYPTED = -1; + // insecure! + public static final int RESULT_INSECURE = 0; + // encrypted + public static final int RESULT_ENCRYPTED = 1; + + int result; + + public int getResult() { + return result; + } + + public void setResult(int result) { + this.result = result; + } + + public OpenPgpDecryptionResult() { + + } + + public OpenPgpDecryptionResult(int result) { + this.result = result; + } + + public OpenPgpDecryptionResult(OpenPgpDecryptionResult b) { + this.result = b.result; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + /** + * NOTE: When adding fields in the process of updating this API, make sure to bump + * {@link #PARCELABLE_VERSION}. + */ + dest.writeInt(PARCELABLE_VERSION); + // Inject a placeholder that will store the parcel size from this point on + // (not including the size itself). + int sizePosition = dest.dataPosition(); + dest.writeInt(0); + int startPosition = dest.dataPosition(); + // version 1 + dest.writeInt(result); + // Go back and write the size + int parcelableSize = dest.dataPosition() - startPosition; + dest.setDataPosition(sizePosition); + dest.writeInt(parcelableSize); + dest.setDataPosition(startPosition + parcelableSize); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpDecryptionResult createFromParcel(final Parcel source) { + source.readInt(); // parcelableVersion + int parcelableSize = source.readInt(); + int startPosition = source.dataPosition(); + + OpenPgpDecryptionResult vr = new OpenPgpDecryptionResult(); + vr.result = source.readInt(); + + // skip over all fields added in future versions of this parcel + source.setDataPosition(startPosition + parcelableSize); + + return vr; + } + + public OpenPgpDecryptionResult[] newArray(final int size) { + return new OpenPgpDecryptionResult[size]; + } + }; + + @Override + public String toString() { + return "\nresult: " + result; + } + +} diff --git a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java index ce8f21f..69c39fd 100644 --- a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java +++ b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java @@ -19,10 +19,6 @@ package org.openintents.openpgp; import android.os.Parcel; import android.os.Parcelable; -/** - * Parcelable versioning has been copied from Dashclock Widget - * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java - */ public class OpenPgpError implements Parcelable { /** * Since there might be a case where new versions of the client using the library getting @@ -97,7 +93,7 @@ public class OpenPgpError implements Parcelable { public static final Creator CREATOR = new Creator() { public OpenPgpError createFromParcel(final Parcel source) { - int parcelableVersion = source.readInt(); + source.readInt(); // parcelableVersion int parcelableSize = source.readInt(); int startPosition = source.dataPosition(); diff --git a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpMetadata.java b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpMetadata.java index d620a57..8b3a3bb 100644 --- a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpMetadata.java +++ b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpMetadata.java @@ -19,10 +19,6 @@ package org.openintents.openpgp; import android.os.Parcel; import android.os.Parcelable; -/** - * Parcelable versioning has been copied from Dashclock Widget - * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java - */ public class OpenPgpMetadata implements Parcelable { /** * Since there might be a case where new versions of the client using the library getting @@ -99,7 +95,7 @@ public class OpenPgpMetadata implements Parcelable { public static final Creator CREATOR = new Creator() { public OpenPgpMetadata createFromParcel(final Parcel source) { - int parcelableVersion = source.readInt(); + source.readInt(); // parcelableVersion int parcelableSize = source.readInt(); int startPosition = source.dataPosition(); diff --git a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java index c2461ab..f188968 100644 --- a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java +++ b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java @@ -23,10 +23,6 @@ import org.openintents.openpgp.util.OpenPgpUtils; import java.util.ArrayList; -/** - * Parcelable versioning has been copied from Dashclock Widget - * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java - */ public class OpenPgpSignatureResult implements Parcelable { /** * Since there might be a case where new versions of the client using the library getting @@ -35,39 +31,47 @@ public class OpenPgpSignatureResult implements Parcelable { */ public static final int PARCELABLE_VERSION = 2; - // error -> invalid signature! - public static final int SIGNATURE_ERROR = 0; - // successfully verified signature, with certified key - public static final int SIGNATURE_SUCCESS_CERTIFIED = 1; + // content not signed + public static final int RESULT_NO_SIGNATURE = -1; + // invalid signature! + public static final int RESULT_INVALID_SIGNATURE = 0; + // successfully verified signature, with confirmed key + public static final int RESULT_VALID_CONFIRMED = 1; // no key was found for this signature verification - public static final int SIGNATURE_KEY_MISSING = 2; - // successfully verified signature, but with uncertified key - public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3; + public static final int RESULT_KEY_MISSING = 2; + // successfully verified signature, but with unconfirmed key + public static final int RESULT_VALID_UNCONFIRMED = 3; // key has been revoked -> invalid signature! - public static final int SIGNATURE_KEY_REVOKED = 4; + public static final int RESULT_INVALID_KEY_REVOKED = 4; // key is expired -> invalid signature! - public static final int SIGNATURE_KEY_EXPIRED = 5; + public static final int RESULT_INVALID_KEY_EXPIRED = 5; // insecure cryptographic algorithms/protocol -> invalid signature! - public static final int INSECURE_CRYPTOGRAPHY = 6; + public static final int RESULT_INVALID_INSECURE = 6; - int status; + int result; boolean signatureOnly; String primaryUserId; ArrayList userIds; long keyId; - public int getStatus() { - return status; + public int getResult() { + return result; } - public void setStatus(int status) { - this.status = status; + public void setResult(int result) { + this.result = result; } + /** + * @deprecated + */ public boolean isSignatureOnly() { return signatureOnly; } + /** + * @deprecated + */ public void setSignatureOnly(boolean signatureOnly) { this.signatureOnly = signatureOnly; } @@ -102,7 +106,7 @@ public class OpenPgpSignatureResult implements Parcelable { public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, boolean signatureOnly, long keyId, ArrayList userIds) { - this.status = signatureStatus; + this.result = signatureStatus; this.signatureOnly = signatureOnly; this.primaryUserId = signatureUserId; this.keyId = keyId; @@ -110,7 +114,7 @@ public class OpenPgpSignatureResult implements Parcelable { } public OpenPgpSignatureResult(OpenPgpSignatureResult b) { - this.status = b.status; + this.result = b.result; this.primaryUserId = b.primaryUserId; this.signatureOnly = b.signatureOnly; this.keyId = b.keyId; @@ -133,7 +137,7 @@ public class OpenPgpSignatureResult implements Parcelable { dest.writeInt(0); int startPosition = dest.dataPosition(); // version 1 - dest.writeInt(status); + dest.writeInt(result); dest.writeByte((byte) (signatureOnly ? 1 : 0)); dest.writeString(primaryUserId); dest.writeLong(keyId); @@ -148,12 +152,12 @@ public class OpenPgpSignatureResult implements Parcelable { public static final Creator CREATOR = new Creator() { public OpenPgpSignatureResult createFromParcel(final Parcel source) { - int parcelableVersion = source.readInt(); + source.readInt(); // parcelableVersion int parcelableSize = source.readInt(); int startPosition = source.dataPosition(); OpenPgpSignatureResult vr = new OpenPgpSignatureResult(); - vr.status = source.readInt(); + vr.result = source.readInt(); vr.signatureOnly = source.readByte() == 1; vr.primaryUserId = source.readString(); vr.keyId = source.readLong(); @@ -173,7 +177,7 @@ public class OpenPgpSignatureResult implements Parcelable { @Override public String toString() { - String out = "\nstatus: " + status; + String out = "\nresult: " + result; out += "\nprimaryUserId: " + primaryUserId; out += "\nuserIds: " + userIds; out += "\nsignatureOnly: " + signatureOnly; 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 25bf72b..39e71ce 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 @@ -49,7 +49,7 @@ public class OpenPgpApi { * - Introduction of ACTION_DECRYPT_METADATA, RESULT_METADATA, EXTRA_ORIGINAL_FILENAME, and OpenPgpMetadata parcel * - Introduction of internal NFC extras: EXTRA_NFC_SIGNED_HASH, EXTRA_NFC_SIG_CREATION_TIMESTAMP * 5: - * - OpenPgpSignatureResult: new consts SIGNATURE_KEY_REVOKED and SIGNATURE_KEY_EXPIRED + * - OpenPgpSignatureResult: new consts RESULT_INVALID_KEY_REVOKED and RESULT_INVALID_KEY_EXPIRED * - OpenPgpSignatureResult: ArrayList userIds * 6: * - Deprecate ACTION_SIGN @@ -66,8 +66,14 @@ public class OpenPgpApi { * - New ACTION_GET_SIGN_KEY_ID * - EXTRA_PASSPHRASE changed from String to char[] * 8: - * - OpenPgpSignatureResult: new const INSECURE_CRYPTOGRAPHY - * - RESULT_TYPES important change: Introduce RESULT_TYPE_SIGNED_AND_ENCRYPTED, remove RESULT_TYPE_UNENCRYPTED_UNSIGNED + * - OpenPgpSignatureResult: + * method getStatus() renamed to getResult() + * constants have been renamed for clarity + * new constants: RESULT_NO_SIGNATURE, RESULT_INVALID_INSECURE + * isSignatureOnly() has been deprecated + * - RESULT_TYPES have been removed + * - new OpenPgpDecryptionResult returned via RESULT_DECRYPTION + * - OpenPgpSignatureResult and OpenPgpDecryptionResult are never null, they are always returned. */ public static final int API_VERSION = 8; @@ -164,7 +170,7 @@ public class OpenPgpApi { * and also signed-only input. * OutputStream is optional, e.g., for verifying detached signatures! *

- * If OpenPgpSignatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_MISSING + * If OpenPgpSignatureResult.getResult() == OpenPgpSignatureResult.RESULT_KEY_MISSING * in addition a PendingIntent is returned via RESULT_INTENT to download missing keys. * On all other status, in addition a PendingIntent is returned via RESULT_INTENT to open * the key view in OpenKeychain. @@ -174,9 +180,9 @@ public class OpenPgpApi { *

* returned extras: * OpenPgpSignatureResult RESULT_SIGNATURE + * OpenPgpDecryptionResult RESULT_DECRYPTION * OpenPgpDecryptMetadata RESULT_METADATA * String RESULT_CHARSET (charset which was specified in the headers of ascii armored input, if any) - * int RESULT_TYPE */ public static final String ACTION_DECRYPT_VERIFY = "org.openintents.openpgp.action.DECRYPT_VERIFY"; @@ -273,16 +279,11 @@ public class OpenPgpApi { // DECRYPT_VERIFY public static final String EXTRA_DETACHED_SIGNATURE = "detached_signature"; public static final String RESULT_SIGNATURE = "signature"; + public static final String RESULT_DECRYPTION = "decryption"; public static final String RESULT_METADATA = "metadata"; // This will be the charset which was specified in the headers of ascii armored input, if any public static final String RESULT_CHARSET = "charset"; - // unencrypted _and_ unsigned content will return an ERROR as there is no OpenPGP data available - public static final String RESULT_TYPE = "type"; - public static final int RESULT_TYPE_ENCRYPTED = 1; - public static final int RESULT_TYPE_SIGNED = 2; - public static final int RESULT_TYPE_SIGNED_AND_ENCRYPTED = 3; - // INTERNAL, should not be used public static final String EXTRA_CALL_UUID1 = "call_uuid1"; public static final String EXTRA_CALL_UUID2 = "call_uuid2"; -- cgit v1.2.3