2015-04-15 04:10:53 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
|
|
|
|
* Copyright (C) 2015 Vincent Breitmoser <v.breitmoser@mugenguild.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-03-21 21:36:10 -04:00
|
|
|
|
2015-04-15 04:10:53 -04:00
|
|
|
package org.sufficientlysecure.keychain.ui;
|
2015-03-21 21:36:10 -04:00
|
|
|
|
2015-03-21 22:34:34 -04:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
2015-03-21 21:36:10 -04:00
|
|
|
import android.content.Intent;
|
2015-03-21 22:34:34 -04:00
|
|
|
import android.database.Cursor;
|
2015-03-21 21:36:10 -04:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Message;
|
|
|
|
import android.os.Messenger;
|
|
|
|
import android.support.v4.app.Fragment;
|
2015-03-21 22:34:34 -04:00
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.CursorLoader;
|
|
|
|
import android.support.v4.content.Loader;
|
2015-03-21 21:36:10 -04:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.ViewGroup;
|
2015-03-21 22:34:34 -04:00
|
|
|
import android.widget.Button;
|
2015-03-21 21:36:10 -04:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.spongycastle.util.encoders.Hex;
|
|
|
|
import org.sufficientlysecure.keychain.R;
|
|
|
|
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
|
|
|
|
import org.sufficientlysecure.keychain.operations.results.PromoteKeyResult;
|
2015-03-21 22:34:34 -04:00
|
|
|
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
|
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;
|
2015-03-21 21:36:10 -04:00
|
|
|
import org.sufficientlysecure.keychain.service.KeychainIntentService;
|
2015-03-24 08:19:43 -04:00
|
|
|
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
|
2015-05-16 18:35:10 -04:00
|
|
|
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
|
|
|
|
2015-03-21 21:36:10 -04:00
|
|
|
|
2015-04-15 04:02:41 -04:00
|
|
|
public class ViewKeyYubiKeyFragment extends Fragment
|
2015-03-21 22:34:34 -04:00
|
|
|
implements LoaderCallbacks<Cursor> {
|
2015-03-21 21:36:10 -04:00
|
|
|
|
2015-05-16 17:59:04 -04:00
|
|
|
public static final String ARG_MASTER_KEY_ID = "master_key_id";
|
2015-03-21 21:36:10 -04:00
|
|
|
public static final String ARG_FINGERPRINT = "fingerprint";
|
|
|
|
public static final String ARG_USER_ID = "user_id";
|
2015-03-21 22:56:58 -04:00
|
|
|
public static final String ARG_CARD_AID = "aid";
|
2015-05-16 17:59:04 -04:00
|
|
|
|
2015-03-21 22:34:34 -04:00
|
|
|
private byte[][] mFingerprints;
|
2015-03-21 21:36:10 -04:00
|
|
|
private String mUserId;
|
2015-03-21 22:56:58 -04:00
|
|
|
private byte[] mCardAid;
|
2015-03-21 22:34:34 -04:00
|
|
|
private long mMasterKeyId;
|
|
|
|
private Button vButton;
|
|
|
|
private TextView vStatus;
|
2015-03-21 21:36:10 -04:00
|
|
|
|
2015-05-16 17:59:04 -04:00
|
|
|
public static ViewKeyYubiKeyFragment newInstance(long masterKeyId,
|
|
|
|
byte[] fingerprints, String userId, byte[] aid) {
|
2015-04-15 04:02:41 -04:00
|
|
|
ViewKeyYubiKeyFragment frag = new ViewKeyYubiKeyFragment();
|
2015-03-21 21:36:10 -04:00
|
|
|
|
|
|
|
Bundle args = new Bundle();
|
2015-05-16 17:59:04 -04:00
|
|
|
args.putLong(ARG_MASTER_KEY_ID, masterKeyId);
|
2015-03-21 21:36:10 -04:00
|
|
|
args.putByteArray(ARG_FINGERPRINT, fingerprints);
|
|
|
|
args.putString(ARG_USER_ID, userId);
|
2015-03-21 22:56:58 -04:00
|
|
|
args.putByteArray(ARG_CARD_AID, aid);
|
2015-03-21 21:36:10 -04:00
|
|
|
frag.setArguments(args);
|
|
|
|
|
|
|
|
return frag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
Bundle args = getArguments();
|
2015-03-21 22:34:34 -04:00
|
|
|
ByteBuffer buf = ByteBuffer.wrap(args.getByteArray(ARG_FINGERPRINT));
|
2015-05-16 18:59:50 -04:00
|
|
|
mFingerprints = new byte[buf.remaining()/20][];
|
2015-03-21 22:34:34 -04:00
|
|
|
for (int i = 0; i < mFingerprints.length; i++) {
|
|
|
|
mFingerprints[i] = new byte[20];
|
|
|
|
buf.get(mFingerprints[i]);
|
|
|
|
}
|
2015-03-21 21:36:10 -04:00
|
|
|
mUserId = args.getString(ARG_USER_ID);
|
2015-03-21 22:56:58 -04:00
|
|
|
mCardAid = args.getByteArray(ARG_CARD_AID);
|
2015-03-21 21:36:10 -04:00
|
|
|
|
2015-05-16 17:59:04 -04:00
|
|
|
mMasterKeyId = args.getLong(ARG_MASTER_KEY_ID);
|
2015-03-21 22:34:34 -04:00
|
|
|
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
|
|
|
|
2015-03-21 21:36:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.view_key_yubikey, null);
|
|
|
|
|
|
|
|
TextView vSerNo = (TextView) view.findViewById(R.id.yubikey_serno);
|
|
|
|
TextView vUserId = (TextView) view.findViewById(R.id.yubikey_userid);
|
|
|
|
|
2015-03-21 22:56:58 -04:00
|
|
|
String serno = Hex.toHexString(mCardAid, 10, 4);
|
2015-03-21 22:34:34 -04:00
|
|
|
vSerNo.setText(getString(R.string.yubikey_serno, serno));
|
2015-03-21 21:36:10 -04:00
|
|
|
|
|
|
|
if (!mUserId.isEmpty()) {
|
2015-03-21 22:34:34 -04:00
|
|
|
vUserId.setText(getString(R.string.yubikey_key_holder, mUserId));
|
2015-03-21 21:36:10 -04:00
|
|
|
} else {
|
2015-05-21 04:31:46 -04:00
|
|
|
vUserId.setText(getString(R.string.yubikey_key_holder_not_set));
|
2015-03-21 21:36:10 -04:00
|
|
|
}
|
|
|
|
|
2015-03-21 22:34:34 -04:00
|
|
|
vButton = (Button) view.findViewById(R.id.button_bind);
|
|
|
|
vButton.setOnClickListener(new OnClickListener() {
|
2015-03-21 21:36:10 -04:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
promoteToSecretKey();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-21 22:34:34 -04:00
|
|
|
vStatus = (TextView) view.findViewById(R.id.yubikey_status);
|
2015-03-21 21:36:10 -04:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void promoteToSecretKey() {
|
|
|
|
|
2015-03-24 08:19:43 -04:00
|
|
|
ServiceProgressHandler saveHandler = new ServiceProgressHandler(getActivity()) {
|
2015-05-28 07:32:34 -04:00
|
|
|
@Override
|
2015-03-21 21:36:10 -04:00
|
|
|
public void handleMessage(Message message) {
|
|
|
|
// handle messages by standard KeychainIntentServiceHandler first
|
|
|
|
super.handleMessage(message);
|
|
|
|
|
|
|
|
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
|
|
|
|
// get returned data bundle
|
|
|
|
Bundle returnData = message.getData();
|
|
|
|
|
|
|
|
PromoteKeyResult result =
|
|
|
|
returnData.getParcelable(DecryptVerifyResult.EXTRA_RESULT);
|
|
|
|
|
|
|
|
result.createNotify(getActivity()).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Send all information needed to service to decrypt in other thread
|
|
|
|
Intent intent = new Intent(getActivity(), KeychainIntentService.class);
|
|
|
|
|
|
|
|
// fill values for this action
|
|
|
|
|
|
|
|
intent.setAction(KeychainIntentService.ACTION_PROMOTE_KEYRING);
|
|
|
|
|
|
|
|
Bundle data = new Bundle();
|
2015-03-21 22:34:34 -04:00
|
|
|
data.putLong(KeychainIntentService.PROMOTE_MASTER_KEY_ID, mMasterKeyId);
|
2015-03-21 22:56:58 -04:00
|
|
|
data.putByteArray(KeychainIntentService.PROMOTE_CARD_AID, mCardAid);
|
2015-05-16 18:35:10 -04:00
|
|
|
long[] subKeyIds = new long[mFingerprints.length];
|
|
|
|
for (int i = 0; i < subKeyIds.length; i++) {
|
|
|
|
subKeyIds[i] = KeyFormattingUtils.getKeyIdFromFingerprint(mFingerprints[i]);
|
|
|
|
}
|
|
|
|
data.putLongArray(KeychainIntentService.PROMOTE_SUBKEY_IDS, subKeyIds);
|
2015-03-21 21:36:10 -04:00
|
|
|
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
|
|
|
|
|
|
|
|
// Create a new Messenger for the communication back
|
|
|
|
Messenger messenger = new Messenger(saveHandler);
|
|
|
|
intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
|
|
|
|
|
|
|
|
// start service with intent
|
|
|
|
getActivity().startService(intent);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-21 22:34:34 -04:00
|
|
|
public static final String[] PROJECTION = new String[]{
|
|
|
|
Keys._ID,
|
|
|
|
Keys.KEY_ID,
|
|
|
|
Keys.RANK,
|
|
|
|
Keys.HAS_SECRET,
|
|
|
|
Keys.FINGERPRINT
|
|
|
|
};
|
2015-05-16 17:59:04 -04:00
|
|
|
// private static final int INDEX_KEY_ID = 1;
|
|
|
|
// private static final int INDEX_RANK = 2;
|
2015-03-21 22:34:34 -04:00
|
|
|
private static final int INDEX_HAS_SECRET = 3;
|
|
|
|
private static final int INDEX_FINGERPRINT = 4;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
return new CursorLoader(getActivity(), Keys.buildKeysUri(mMasterKeyId),
|
|
|
|
PROJECTION, null, null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
|
|
|
if (!data.moveToFirst()) {
|
|
|
|
// wut?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean allBound = true;
|
|
|
|
boolean noneBound = true;
|
|
|
|
|
|
|
|
do {
|
|
|
|
SecretKeyType keyType = SecretKeyType.fromNum(data.getInt(INDEX_HAS_SECRET));
|
|
|
|
byte[] fingerprint = data.getBlob(INDEX_FINGERPRINT);
|
|
|
|
Integer index = naiveIndexOf(mFingerprints, fingerprint);
|
|
|
|
if (index == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (keyType == SecretKeyType.DIVERT_TO_CARD) {
|
|
|
|
noneBound = false;
|
|
|
|
} else {
|
|
|
|
allBound = false;
|
|
|
|
}
|
|
|
|
} while (data.moveToNext());
|
|
|
|
|
|
|
|
if (allBound) {
|
|
|
|
vButton.setVisibility(View.GONE);
|
2015-03-21 22:56:58 -04:00
|
|
|
vStatus.setText(R.string.yubikey_status_bound);
|
2015-03-21 22:34:34 -04:00
|
|
|
} else {
|
|
|
|
vButton.setVisibility(View.VISIBLE);
|
2015-03-22 01:36:27 -04:00
|
|
|
vStatus.setText(noneBound
|
|
|
|
? R.string.yubikey_status_unbound
|
|
|
|
: R.string.yubikey_status_partly);
|
2015-03-21 22:34:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-16 18:35:10 -04:00
|
|
|
static private Integer naiveIndexOf(byte[][] haystack, byte[] needle) {
|
2015-03-21 22:34:34 -04:00
|
|
|
for (int i = 0; i < haystack.length; i++) {
|
|
|
|
if (Arrays.equals(needle, haystack[i])) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
|
|
|
|
|
|
|
}
|
2015-03-21 21:36:10 -04:00
|
|
|
}
|