private void shareKey(Uri dataUri, boolean fingerprintOnly) {
    String content;
    if (fingerprintOnly) {
      byte[] fingerprintBlob = ProviderHelper.getFingerprint(this, dataUri);
      String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob, false);

      content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
    } else {
      // get public keyring as ascii armored string
      long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
      ArrayList<String> keyringArmored =
          ProviderHelper.getKeyRingsAsArmoredString(this, dataUri, new long[] {masterKeyId});

      content = keyringArmored.get(0);

      // Android will fail with android.os.TransactionTooLargeException if key is too big
      // see http://www.lonestarprod.com/?p=34
      if (content.length() >= 86389) {
        Toast.makeText(getApplicationContext(), R.string.key_too_big_for_sharing, Toast.LENGTH_LONG)
            .show();
        return;
      }
    }

    // let user choose application
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, content);
    sendIntent.setType("text/plain");
    startActivity(
        Intent.createChooser(sendIntent, getResources().getText(R.string.action_share_key_with)));
  }
  private void encryptToContact(Uri dataUri) {
    long keyId = ProviderHelper.getMasterKeyId(getActivity(), dataUri);

    long[] encryptionKeyIds = new long[] {keyId};
    Intent intent = new Intent(getActivity(), EncryptActivity.class);
    intent.setAction(EncryptActivity.ACTION_ENCRYPT);
    intent.putExtra(EncryptActivity.EXTRA_ENCRYPTION_KEY_IDS, encryptionKeyIds);
    // used instead of startActivity set actionbar based on callingPackage
    startActivityForResult(intent, 0);
  }
  private void copyToClipboard(Uri dataUri) {
    // get public keyring as ascii armored string
    long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
    ArrayList<String> keyringArmored =
        ProviderHelper.getKeyRingsAsArmoredString(this, dataUri, new long[] {masterKeyId});

    ClipboardReflection.copyToClipboard(this, keyringArmored.get(0));
    Toast.makeText(getApplicationContext(), R.string.key_copied_to_clipboard, Toast.LENGTH_LONG)
        .show();
  }
Example #4
0
 @Override
 public Passphrase getCachedPassphrase(long subKeyId) throws NoSecretKeyException {
   try {
     if (subKeyId != key.symmetric) {
       long masterKeyId = mProviderHelper.getMasterKeyId(subKeyId);
       return getCachedPassphrase(masterKeyId, subKeyId);
     }
     return getCachedPassphrase(key.symmetric, key.symmetric);
   } catch (NotFoundException e) {
     throw new PassphraseCacheInterface.NoSecretKeyException();
   }
 }
  private void updateFromKeyserver(Uri dataUri) {
    long updateKeyId = ProviderHelper.getMasterKeyId(ViewKeyActivity.this, dataUri);

    if (updateKeyId == 0) {
      Log.e(Constants.TAG, "this shouldn't happen. KeyId == 0!");
      return;
    }

    Intent queryIntent = new Intent(this, ImportKeysActivity.class);
    queryIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEYSERVER);
    queryIntent.putExtra(ImportKeysActivity.EXTRA_KEY_ID, updateKeyId);

    // TODO: lookup with onactivityresult!
    startActivityForResult(queryIntent, RESULT_CODE_LOOKUP_KEY);
  }
  /**
   * If an Intent gives a signatureMasterKeyId and/or encryptionMasterKeyIds, preselect those!
   *
   * @param preselectedSignatureKeyId
   * @param preselectedEncryptionKeyIds
   */
  private void preselectKeys(
      long preselectedSignatureKeyId,
      long[] preselectedEncryptionKeyIds,
      ProviderHelper providerHelper) {
    if (preselectedSignatureKeyId != 0) {
      // TODO: don't use bouncy castle objects!
      try {
        PGPSecretKeyRing keyRing =
            providerHelper.getPGPSecretKeyRingWithKeyId(preselectedSignatureKeyId);

        PGPSecretKey masterKey = keyRing.getSecretKey();
        if (masterKey != null) {
          PGPSecretKey signKey = PgpKeyHelper.getFirstSigningSubkey(keyRing);
          if (signKey != null) {
            setSignatureKeyId(masterKey.getKeyID());
          }
        }
      } catch (ProviderHelper.NotFoundException e) {
        Log.e(Constants.TAG, "key not found!", e);
      }
    }

    if (preselectedEncryptionKeyIds != null) {
      Vector<Long> goodIds = new Vector<Long>();
      for (int i = 0; i < preselectedEncryptionKeyIds.length; ++i) {
        // TODO One query per selected key?! wtf
        try {
          long id =
              providerHelper.getMasterKeyId(
                  KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
                      Long.toString(preselectedEncryptionKeyIds[i])));
          goodIds.add(id);
        } catch (ProviderHelper.NotFoundException e) {
          Log.e(Constants.TAG, "key not found!", e);
        }
      }
      if (goodIds.size() > 0) {
        long[] keyIds = new long[goodIds.size()];
        for (int i = 0; i < goodIds.size(); ++i) {
          keyIds[i] = goodIds.get(i);
        }
        setEncryptionKeyIds(keyIds);
      }
    }
  }
  private void loadData(Uri dataUri) {
    if (dataUri.equals(mDataUri)) {
      Log.d(Constants.TAG, "Same URI, no need to load the data again!");
      return;
    }

    mDataUri = dataUri;

    Log.i(Constants.TAG, "mDataUri: " + mDataUri.toString());

    { // label whether secret key is available, and edit button if it is
      final long masterKeyId = ProviderHelper.getMasterKeyId(getActivity(), mDataUri);
      if (ProviderHelper.hasSecretKeyByMasterKeyId(getActivity(), masterKeyId)) {
        // set this attribute. this is a LITTLE unclean, but we have the info available
        // right here, so why not.
        mSecretKey.setTextColor(getResources().getColor(R.color.emphasis));
        mSecretKey.setText(R.string.secret_key_yes);

        // certify button
        // TODO this button MIGHT be useful if the user wants to
        // certify a private key with another...
        // mActionCertify.setVisibility(View.GONE);

        // edit button
        mActionEdit.setVisibility(View.VISIBLE);
        mActionEdit.setOnClickListener(
            new View.OnClickListener() {
              public void onClick(View view) {
                Intent editIntent = new Intent(getActivity(), EditKeyActivity.class);
                editIntent.setData(
                    KeychainContract.KeyRings.buildSecretKeyRingsByMasterKeyIdUri(
                        Long.toString(masterKeyId)));
                editIntent.setAction(EditKeyActivity.ACTION_EDIT_KEY);
                startActivityForResult(editIntent, 0);
              }
            });
      } else {
        mSecretKey.setTextColor(Color.BLACK);
        mSecretKey.setText(getResources().getString(R.string.secret_key_no));

        // certify button
        mActionCertify.setVisibility(View.VISIBLE);
        // edit button
        mActionEdit.setVisibility(View.GONE);
      }

      // TODO see todo note above, doing this here for now
      mActionCertify.setOnClickListener(
          new View.OnClickListener() {
            public void onClick(View view) {
              certifyKey(
                  KeychainContract.KeyRings.buildPublicKeyRingsByMasterKeyIdUri(
                      Long.toString(masterKeyId)));
            }
          });
    }

    mActionEncrypt.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            encryptToContact(mDataUri);
          }
        });

    mUserIdsAdapter = new ViewKeyUserIdsAdapter(getActivity(), null, 0);
    mUserIds.setAdapter(mUserIdsAdapter);

    mKeysAdapter = new ViewKeyKeysAdapter(getActivity(), null, 0);
    mKeys.setAdapter(mKeysAdapter);

    // Prepare the loaders. Either re-connect with an existing ones,
    // or start new ones.
    getActivity().getSupportLoaderManager().initLoader(LOADER_ID_KEYRING, null, this);
    getActivity().getSupportLoaderManager().initLoader(LOADER_ID_USER_IDS, null, this);
    getActivity().getSupportLoaderManager().initLoader(LOADER_ID_KEYS, null, this);
  }