/**
   * 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);
      }
    }
  }
Ejemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public static Vector<PGPSecretKey> getCertificationKeys(PGPSecretKeyRing keyRing) {
    Vector<PGPSecretKey> signingKeys = new Vector<PGPSecretKey>();

    for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(keyRing.getSecretKeys())) {
      if (isCertificationKey(key)) {
        signingKeys.add(key);
      }
    }

    return signingKeys;
  }
Ejemplo n.º 3
0
  @SuppressWarnings("unchecked")
  public static PGPSecretKey getMasterKey(PGPSecretKeyRing keyRing) {
    if (keyRing == null) {
      return null;
    }
    for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(keyRing.getSecretKeys())) {
      if (key.isMasterKey()) {
        return key;
      }
    }

    return null;
  }
Ejemplo n.º 4
0
  @SuppressWarnings("unchecked")
  public static PGPSecretKey getKeyNum(PGPSecretKeyRing keyRing, long num) {
    long cnt = 0;
    if (keyRing == null) {
      return null;
    }
    for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(keyRing.getSecretKeys())) {
      if (cnt == num) {
        return key;
      }
      cnt++;
    }

    return null;
  }
Ejemplo n.º 5
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_key);

    mActionBar = getSupportActionBar();
    mActionBar.setDisplayShowTitleEnabled(true);

    // set actionbar without home button if called from another app
    if (getCallingPackage() != null && getCallingPackage().equals(Constants.PACKAGE_NAME)) {
      mActionBar.setDisplayHomeAsUpEnabled(true);
      mActionBar.setHomeButtonEnabled(true);
    } else {
      mActionBar.setDisplayHomeAsUpEnabled(false);
      mActionBar.setHomeButtonEnabled(false);
    }

    // find views
    mChangePassPhrase = (Button) findViewById(R.id.edit_key_btn_change_pass_phrase);
    mNoPassphrase = (CheckBox) findViewById(R.id.edit_key_no_passphrase);

    mUserIds = new Vector<String>();
    mKeys = new Vector<PGPSecretKey>();
    mKeysUsages = new Vector<Integer>();

    // Catch Intents opened from other apps
    mIntent = getIntent();

    // Handle intents
    Bundle extras = mIntent.getExtras();
    if (ACTION_CREATE_KEY.equals(mIntent.getAction())) {
      mActionBar.setTitle(R.string.title_createKey);

      mCurrentPassPhrase = "";

      if (extras != null) {
        // if userId is given, prefill the fields
        if (extras.containsKey(EXTRA_USER_IDS)) {
          Log.d(Constants.TAG, "UserIds are given!");
          mUserIds.add(extras.getString(EXTRA_USER_IDS));
        }

        // if no passphrase is given
        if (extras.containsKey(EXTRA_NO_PASSPHRASE)) {
          boolean noPassphrase = extras.getBoolean(EXTRA_NO_PASSPHRASE);
          if (noPassphrase) {
            // check "no passphrase" checkbox and remove button
            mNoPassphrase.setChecked(true);
            mChangePassPhrase.setVisibility(View.GONE);
          }
        }

        // generate key
        if (extras.containsKey(EXTRA_GENERATE_DEFAULT_KEYS)) {
          boolean generateDefaultKeys = extras.getBoolean(EXTRA_GENERATE_DEFAULT_KEYS);
          if (generateDefaultKeys) {

            // build layout in handler after generating keys not directly in onCreate
            mBuildLayout = false;

            // Send all information needed to service generate keys in other thread
            Intent intent = new Intent(this, ApgService.class);
            intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_GENERATE_DEFAULT_RSA_KEYS);

            // fill values for this action
            Bundle data = new Bundle();
            data.putString(ApgService.SYMMETRIC_PASSPHRASE, mCurrentPassPhrase);

            intent.putExtra(ApgService.EXTRA_DATA, data);

            // show progress dialog
            mGeneratingDialog =
                ProgressDialogFragment.newInstance(
                    R.string.progress_generating, ProgressDialog.STYLE_SPINNER);

            // Message is received after generating is done in ApgService
            ApgHandler saveHandler =
                new ApgHandler(this, mGeneratingDialog) {
                  public void handleMessage(Message message) {
                    // handle messages by standard ApgHandler first
                    super.handleMessage(message);

                    if (message.arg1 == ApgHandler.MESSAGE_OKAY) {
                      // get new key from data bundle returned from service
                      Bundle data = message.getData();
                      PGPSecretKeyRing masterKeyRing =
                          PGPConversionHelper.BytesToPGPSecretKeyRing(
                              data.getByteArray(ApgService.RESULT_NEW_KEY));
                      PGPSecretKeyRing subKeyRing =
                          PGPConversionHelper.BytesToPGPSecretKeyRing(
                              data.getByteArray(ApgService.RESULT_NEW_KEY2));

                      // add master key
                      Iterator<PGPSecretKey> masterIt = masterKeyRing.getSecretKeys();
                      mKeys.add(masterIt.next());
                      mKeysUsages.add(Id.choice.usage.sign_only);

                      // add sub key
                      Iterator<PGPSecretKey> subIt = subKeyRing.getSecretKeys();
                      subIt.next(); // masterkey
                      mKeys.add(subIt.next());
                      mKeysUsages.add(Id.choice.usage.encrypt_only);

                      buildLayout();
                    }
                  };
                };

            // Create a new Messenger for the communication back
            Messenger messenger = new Messenger(saveHandler);
            intent.putExtra(ApgService.EXTRA_MESSENGER, messenger);

            mGeneratingDialog.show(getSupportFragmentManager(), "dialog");

            // start service with intent
            startService(intent);
          }
        }
      }
    } else if (ACTION_EDIT_KEY.equals(mIntent.getAction())) {
      mActionBar.setTitle(R.string.title_editKey);

      mCurrentPassPhrase = PGPMain.getEditPassPhrase();
      if (mCurrentPassPhrase == null) {
        mCurrentPassPhrase = "";
      }

      if (mCurrentPassPhrase.equals("")) {
        // check "no passphrase" checkbox and remove button
        mNoPassphrase.setChecked(true);
        mChangePassPhrase.setVisibility(View.GONE);
      }

      if (extras != null) {

        if (extras.containsKey(EXTRA_KEY_ID)) {
          long keyId = mIntent.getExtras().getLong(EXTRA_KEY_ID);

          if (keyId != 0) {
            PGPSecretKey masterKey = null;
            mKeyRing = PGPMain.getSecretKeyRing(keyId);
            if (mKeyRing != null) {
              masterKey = PGPHelper.getMasterKey(mKeyRing);
              for (PGPSecretKey key :
                  new IterableIterator<PGPSecretKey>(mKeyRing.getSecretKeys())) {
                mKeys.add(key);
                mKeysUsages.add(-1); // get usage when view is created
              }
            }
            if (masterKey != null) {
              for (String userId : new IterableIterator<String>(masterKey.getUserIDs())) {
                mUserIds.add(userId);
              }
            }
          }
        }
      }
    }

    mChangePassPhrase.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            showDialog(Id.dialog.new_pass_phrase);
          }
        });

    // disable passphrase when no passphrase checkobox is checked!
    mNoPassphrase.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
              // remove passphrase
              mNewPassPhrase = null;

              mChangePassPhrase.setVisibility(View.GONE);
            } else {
              mChangePassPhrase.setVisibility(View.VISIBLE);
            }
          }
        });

    if (mBuildLayout) {
      buildLayout();
    }
  }