implement upload of keyring after certification, check that option by default

This commit is contained in:
Vincent Breitmoser 2014-10-26 01:50:48 +02:00
parent 108b35cb50
commit 510ef40f55
7 changed files with 57 additions and 17 deletions

View File

@ -2,6 +2,9 @@ package org.sufficientlysecure.keychain.operations;
import android.content.Context;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.keyimport.HkpKeyserver;
import org.sufficientlysecure.keychain.keyimport.Keyserver.AddKeyException;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
@ -17,6 +20,7 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult.LogTyp
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.operations.results.SaveKeyringResult;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.Log;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@ -36,7 +40,7 @@ public class CertifyOperation extends BaseOperation {
super(context, providerHelper, progressable, cancelled);
}
public CertifyResult certify(CertifyActionsParcel parcel) {
public CertifyResult certify(CertifyActionsParcel parcel, String keyServerUri) {
OperationLog log = new OperationLog();
log.add(LogType.MSG_CRT, 0);
@ -72,7 +76,7 @@ public class CertifyOperation extends BaseOperation {
log.add(LogType.MSG_CRT_CERTIFYING, 1);
int certifyOk = 0, certifyError = 0;
int certifyOk = 0, certifyError = 0, uploadOk = 0, uploadError = 0;
// Work through all requested certifications
for (CertifyAction action : parcel.mCertifyActions) {
@ -118,13 +122,20 @@ public class CertifyOperation extends BaseOperation {
return new CertifyResult(CertifyResult.RESULT_CANCELLED, log);
}
HkpKeyserver keyServer = null;
ImportExportOperation importExportOperation = null;
if (keyServerUri != null) {
keyServer = new HkpKeyserver(keyServerUri);
importExportOperation = new ImportExportOperation(mContext, mProviderHelper, mProgressable);
}
// Write all certified keys into the database
for (UncachedKeyRing certifiedKey : certifiedKeys) {
// Check if we were cancelled
if (checkCancelled()) {
log.add(LogType.MSG_OPERATION_CANCELLED, 0);
return new CertifyResult(CertifyResult.RESULT_CANCELLED, log, certifyOk, certifyError);
return new CertifyResult(CertifyResult.RESULT_CANCELLED, log, certifyOk, certifyError, uploadOk, uploadError);
}
log.add(LogType.MSG_CRT_SAVE, 2,
@ -133,6 +144,17 @@ public class CertifyOperation extends BaseOperation {
mProviderHelper.clearLog();
SaveKeyringResult result = mProviderHelper.savePublicKeyRing(certifiedKey);
if (importExportOperation != null) {
// TODO use subresult, get rid of try/catch!
try {
importExportOperation.uploadKeyRingToServer(keyServer, certifiedKey);
uploadOk += 1;
} catch (AddKeyException e) {
Log.e(Constants.TAG, "error uploading key", e);
uploadError += 1;
}
}
if (result.success()) {
certifyOk += 1;
} else {
@ -145,11 +167,11 @@ public class CertifyOperation extends BaseOperation {
if (certifyOk == 0) {
log.add(LogType.MSG_CRT_ERROR_NOTHING, 0);
return new CertifyResult(CertifyResult.RESULT_ERROR, log, certifyOk, certifyError);
return new CertifyResult(CertifyResult.RESULT_ERROR, log, certifyOk, certifyError, uploadOk, uploadError);
}
log.add(LogType.MSG_CRT_SUCCESS, 0);
return new CertifyResult(CertifyResult.RESULT_OK, log, certifyOk, certifyError);
return new CertifyResult(CertifyResult.RESULT_OK, log, certifyOk, certifyError, uploadOk, uploadError);
}

View File

@ -94,6 +94,10 @@ public class ImportExportOperation extends BaseOperation {
}
public void uploadKeyRingToServer(HkpKeyserver server, CanonicalizedPublicKeyRing keyring) throws AddKeyException {
uploadKeyRingToServer(server, keyring.getUncachedKeyRing());
}
public void uploadKeyRingToServer(HkpKeyserver server, UncachedKeyRing keyring) throws AddKeyException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ArmoredOutputStream aos = null;
try {

View File

@ -36,16 +36,18 @@ import org.sufficientlysecure.keychain.ui.LogDisplayFragment;
public class CertifyResult extends OperationResult {
int mCertifyOk, mCertifyError;
int mCertifyOk, mCertifyError, mUploadOk, mUploadError;
public CertifyResult(int result, OperationLog log) {
super(result, log);
}
public CertifyResult(int result, OperationLog log, int certifyOk, int certifyError) {
public CertifyResult(int result, OperationLog log, int certifyOk, int certifyError, int uploadOk, int uploadError) {
this(result, log);
mCertifyOk = certifyOk;
mCertifyError = certifyError;
mUploadOk = uploadOk;
mUploadError = uploadError;
}
/** Construct from a parcel - trivial because we have no extra data. */
@ -53,6 +55,8 @@ public class CertifyResult extends OperationResult {
super(source);
mCertifyOk = source.readInt();
mCertifyError = source.readInt();
mUploadOk = source.readInt();
mUploadError = source.readInt();
}
@Override
@ -60,6 +64,8 @@ public class CertifyResult extends OperationResult {
super.writeToParcel(dest, flags);
dest.writeInt(mCertifyOk);
dest.writeInt(mCertifyError);
dest.writeInt(mUploadOk);
dest.writeInt(mUploadError);
}
public static Creator<CertifyResult> CREATOR = new Creator<CertifyResult>() {

View File

@ -121,6 +121,10 @@ public class UncachedKeyRing {
return mRing.getEncoded();
}
public void encode(OutputStream out) throws IOException {
mRing.encode(out);
}
public byte[] getFingerprint() {
return mRing.getPublicKey().getFingerprint();
}

View File

@ -239,10 +239,11 @@ public class KeychainIntentService extends IntentService implements Progressable
// Input
CertifyActionsParcel parcel = data.getParcelable(CERTIFY_PARCEL);
String keyServerUri = data.getString(UPLOAD_KEY_SERVER);
// Operation
CertifyOperation op = new CertifyOperation(this, providerHelper, this, mActionCanceled);
CertifyResult result = op.certify(parcel);
CertifyResult result = op.certify(parcel, keyServerUri);
// Result
sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result);

View File

@ -61,6 +61,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.widget.CertifyKeySpinner;
import org.sufficientlysecure.keychain.ui.widget.KeySpinner;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Preferences;
import java.util.ArrayList;
@ -174,6 +175,11 @@ public class MultiCertifyKeyFragment extends LoaderFragment
}
});
// If this is a debug build, don't upload by default
if (Constants.DEBUG) {
mUploadKeyCheckbox.setChecked(false);
}
return root;
}
@ -354,6 +360,10 @@ public class MultiCertifyKeyFragment extends LoaderFragment
Bundle data = new Bundle();
data.putParcelable(KeychainIntentService.CERTIFY_PARCEL, parcel);
if (mUploadKeyCheckbox.isChecked()) {
String keyserver = Preferences.getPreferences(getActivity()).getPreferredKeyserver();
data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, keyserver);
}
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
// Message is received after signing is done in KeychainIntentService
@ -371,15 +381,8 @@ public class MultiCertifyKeyFragment extends LoaderFragment
Intent intent = new Intent();
intent.putExtra(CertifyResult.EXTRA_RESULT, result);
mActivity.setResult(Activity.RESULT_OK, intent);
mActivity.finish();
// check if we need to send the key to the server or not
if (mUploadKeyCheckbox.isChecked()) {
// upload the newly signed key to the keyserver
// TODO implement
// uploadKey();
} else {
mActivity.finish();
}
}
}
};

View File

@ -61,7 +61,7 @@
android:id="@+id/sign_key_upload_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:checked="true"
android:text="@string/label_send_key"
android:paddingTop="12dp"
android:paddingBottom="12dp"/>