mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-27 11:12:15 -05:00
Fixes for decrypt and sign
This commit is contained in:
parent
1421046c6d
commit
94a81dd8ae
@ -74,12 +74,12 @@
|
|||||||
android:text="Encrypt and Sign" />
|
android:text="Encrypt and Sign" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/crypto_provider_demo_decrypt"
|
android:id="@+id/crypto_provider_demo_decrypt_and_verify"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:onClick="decryptAndVerifyOnClick"
|
android:onClick="decryptAndVerifyOnClick"
|
||||||
android:text="Decrypt" />
|
android:text="Decrypt and Verify" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
@ -73,4 +73,14 @@ public class CryptoSignatureResult implements Parcelable {
|
|||||||
return new CryptoSignatureResult[size];
|
return new CryptoSignatureResult[size];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String out = new String();
|
||||||
|
out += "signature: " + signature;
|
||||||
|
out += "\nsignatureSuccess: " + signatureSuccess;
|
||||||
|
out += "\nsignatureUnknown: " + signatureUnknown;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -102,8 +102,10 @@ public class CryptoProviderDemoActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
mMessage.setText(new String(outputBytes) + "\n\n" + signatureResult.toString());
|
mMessage.setText(new String(outputBytes));
|
||||||
|
Toast.makeText(CryptoProviderDemoActivity.this,
|
||||||
|
"signature result:\n" + signatureResult.toString(), Toast.LENGTH_LONG)
|
||||||
|
.show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -73,4 +73,14 @@ public class CryptoSignatureResult implements Parcelable {
|
|||||||
return new CryptoSignatureResult[size];
|
return new CryptoSignatureResult[size];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String out = new String();
|
||||||
|
out += "signature: " + signature;
|
||||||
|
out += "\nsignatureSuccess: " + signatureSuccess;
|
||||||
|
out += "\nsignatureUnknown: " + signatureUnknown;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -351,21 +351,38 @@ public class CryptoService extends Service {
|
|||||||
private synchronized void decryptAndVerifySafe(byte[] inputBytes, ICryptoCallback callback,
|
private synchronized void decryptAndVerifySafe(byte[] inputBytes, ICryptoCallback callback,
|
||||||
AppSettings appSettings) throws RemoteException {
|
AppSettings appSettings) throws RemoteException {
|
||||||
try {
|
try {
|
||||||
// build InputData and write into OutputStream
|
// TODO: this is not really needed
|
||||||
InputStream inputStream = new ByteArrayInputStream(inputBytes);
|
// checked if it is text with BEGIN and END tags
|
||||||
long inputLength = inputBytes.length;
|
|
||||||
InputData inputData = new InputData(inputStream, inputLength);
|
|
||||||
OutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
String message = new String(inputBytes);
|
String message = new String(inputBytes);
|
||||||
Log.d(Constants.TAG, "in: " + message);
|
Log.d(Constants.TAG, "in: " + message);
|
||||||
|
|
||||||
// checked if signed only
|
|
||||||
boolean signedOnly = false;
|
boolean signedOnly = false;
|
||||||
Matcher matcher = PgpMain.PGP_SIGNED_MESSAGE.matcher(message);
|
Matcher matcher = PgpMain.PGP_MESSAGE.matcher(message);
|
||||||
|
if (matcher.matches()) {
|
||||||
|
Log.d(Constants.TAG, "PGP_MESSAGE matched");
|
||||||
|
message = matcher.group(1);
|
||||||
|
// replace non breakable spaces
|
||||||
|
message = message.replaceAll("\\xa0", " ");
|
||||||
|
|
||||||
|
// overwrite inputBytes
|
||||||
|
inputBytes = message.getBytes();
|
||||||
|
} else {
|
||||||
|
matcher = PgpMain.PGP_SIGNED_MESSAGE.matcher(message);
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
signedOnly = true;
|
signedOnly = true;
|
||||||
|
Log.d(Constants.TAG, "PGP_SIGNED_MESSAGE matched");
|
||||||
|
message = matcher.group(1);
|
||||||
|
// replace non breakable spaces
|
||||||
|
message = message.replaceAll("\\xa0", " ");
|
||||||
|
|
||||||
|
// overwrite inputBytes
|
||||||
|
inputBytes = message.getBytes();
|
||||||
|
} else {
|
||||||
|
Log.d(Constants.TAG, "Nothing matched! Binary?");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// END TODO
|
||||||
|
|
||||||
|
Log.d(Constants.TAG, "in: " + new String(inputBytes));
|
||||||
|
|
||||||
// TODO: This allows to decrypt messages with ALL secret keys, not only the one for the
|
// TODO: This allows to decrypt messages with ALL secret keys, not only the one for the
|
||||||
// app, Fix this?
|
// app, Fix this?
|
||||||
@ -374,26 +391,37 @@ public class CryptoService extends Service {
|
|||||||
// throw new PgpMain.PgpGeneralException(getString(R.string.error_noSecretKeyFound));
|
// throw new PgpMain.PgpGeneralException(getString(R.string.error_noSecretKeyFound));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// TODO: duplicates functions from DecryptActivity!
|
String passphrase = null;
|
||||||
boolean assumeSymmetricEncryption = false;
|
boolean assumeSymmetricEncryption = false;
|
||||||
|
if (!signedOnly) {
|
||||||
|
// BEGIN Get key
|
||||||
|
// TODO: this input stream is consumed after PgpMain.getDecryptionKeyId()... do it
|
||||||
|
// better!
|
||||||
|
InputStream inputStream2 = new ByteArrayInputStream(inputBytes);
|
||||||
|
|
||||||
|
// TODO: duplicates functions from DecryptActivity!
|
||||||
|
// TODO: we need activity to input symmetric passphrase
|
||||||
long secretKeyId;
|
long secretKeyId;
|
||||||
try {
|
try {
|
||||||
if (inputStream.markSupported()) {
|
if (inputStream2.markSupported()) {
|
||||||
inputStream.mark(200); // should probably set this to the max size of two pgpF
|
inputStream2.mark(200); // should probably set this to the max size of two
|
||||||
// objects, if it even needs to be anything other than 0.
|
// pgpF
|
||||||
|
// objects, if it even needs to be anything other
|
||||||
|
// than
|
||||||
|
// 0.
|
||||||
}
|
}
|
||||||
secretKeyId = PgpMain.getDecryptionKeyId(this, inputStream);
|
secretKeyId = PgpMain.getDecryptionKeyId(this, inputStream2);
|
||||||
if (secretKeyId == Id.key.none) {
|
if (secretKeyId == Id.key.none) {
|
||||||
throw new PgpMain.PgpGeneralException(
|
throw new PgpMain.PgpGeneralException(
|
||||||
getString(R.string.error_noSecretKeyFound));
|
getString(R.string.error_noSecretKeyFound));
|
||||||
}
|
}
|
||||||
assumeSymmetricEncryption = false;
|
assumeSymmetricEncryption = false;
|
||||||
} catch (PgpMain.NoAsymmetricEncryptionException e) {
|
} catch (PgpMain.NoAsymmetricEncryptionException e) {
|
||||||
if (inputStream.markSupported()) {
|
if (inputStream2.markSupported()) {
|
||||||
inputStream.reset();
|
inputStream2.reset();
|
||||||
}
|
}
|
||||||
secretKeyId = Id.key.symmetric;
|
secretKeyId = Id.key.symmetric;
|
||||||
if (!PgpMain.hasSymmetricEncryption(this, inputStream)) {
|
if (!PgpMain.hasSymmetricEncryption(this, inputStream2)) {
|
||||||
throw new PgpMain.PgpGeneralException(
|
throw new PgpMain.PgpGeneralException(
|
||||||
getString(R.string.error_noKnownEncryptionFound));
|
getString(R.string.error_noKnownEncryptionFound));
|
||||||
}
|
}
|
||||||
@ -402,12 +430,20 @@ public class CryptoService extends Service {
|
|||||||
|
|
||||||
Log.d(Constants.TAG, "secretKeyId " + secretKeyId);
|
Log.d(Constants.TAG, "secretKeyId " + secretKeyId);
|
||||||
|
|
||||||
String passphrase = getCachedPassphrase(secretKeyId);
|
passphrase = getCachedPassphrase(secretKeyId);
|
||||||
if (passphrase == null) {
|
if (passphrase == null) {
|
||||||
callback.onError(new CryptoError(CryptoError.ID_NO_OR_WRONG_PASSPHRASE,
|
callback.onError(new CryptoError(CryptoError.ID_NO_OR_WRONG_PASSPHRASE,
|
||||||
"No or wrong passphrase!"));
|
"No or wrong passphrase!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// build InputData and write into OutputStream
|
||||||
|
InputStream inputStream = new ByteArrayInputStream(inputBytes);
|
||||||
|
long inputLength = inputBytes.length;
|
||||||
|
InputData inputData = new InputData(inputStream, inputLength);
|
||||||
|
|
||||||
|
OutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
Bundle outputBundle;
|
Bundle outputBundle;
|
||||||
if (signedOnly) {
|
if (signedOnly) {
|
||||||
@ -416,7 +452,7 @@ public class CryptoService extends Service {
|
|||||||
} else {
|
} else {
|
||||||
// TODO: assume symmetric: callback to enter symmetric pass
|
// TODO: assume symmetric: callback to enter symmetric pass
|
||||||
outputBundle = PgpMain.decryptAndVerify(this, null, inputData, outputStream,
|
outputBundle = PgpMain.decryptAndVerify(this, null, inputData, outputStream,
|
||||||
passphrase, false);
|
passphrase, assumeSymmetricEncryption);
|
||||||
}
|
}
|
||||||
|
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
|
Loading…
Reference in New Issue
Block a user