Пример #1
0
  @Override
  public void processPacket(Stanza packet) {
    PublicKeyPublish p = (PublicKeyPublish) packet;

    if (p.getType() == IQ.Type.result) {
      byte[] _publicKey = p.getPublicKey();

      if (_publicKey != null) {
        String from = XmppStringUtils.parseBareJid(p.getFrom());
        boolean selfJid = Authenticator.isSelfJID(getContext(), from);

        // is this our key?
        if (selfJid) {
          byte[] bridgeCertData;
          try {
            PersonalKey key = getApplication().getPersonalKey();

            bridgeCertData =
                X509Bridge.createCertificate(_publicKey, key.getAuthKeyPair().getPrivateKey())
                    .getEncoded();
          } catch (Exception e) {
            Log.e(MessageCenterService.TAG, "error decoding key data", e);
            bridgeCertData = null;
          }

          if (bridgeCertData != null) {
            // store key data in AccountManager
            Authenticator.setDefaultPersonalKey(
                getContext(), _publicKey, null, bridgeCertData, null);
            // invalidate cached personal key
            getApplication().invalidatePersonalKey();

            Log.v(MessageCenterService.TAG, "personal key updated.");
          }
        }

        String id = p.getStanzaId();

        // broadcast key update
        Intent i = new Intent(ACTION_PUBLICKEY);
        i.putExtra(EXTRA_PACKET_ID, id);
        i.putExtra(EXTRA_FROM, p.getFrom());
        i.putExtra(EXTRA_TO, p.getTo());
        i.putExtra(EXTRA_PUBLIC_KEY, _publicKey);
        sendBroadcast(i);

        // if we are not syncing and this is not a response for the Syncer
        // save the key immediately
        if (!SyncAdapter.getIQPacketId().equals(id) || !SyncAdapter.isActive(getContext())) {

          // updating server key
          if (XmppStringUtils.parseDomain(from).equals(from)) {
            Log.v("pubkey", "Updating server key for " + from);
            try {
              Keyring.setKey(getContext(), from, _publicKey);
            } catch (Exception e) {
              // TODO warn user
              Log.e(MessageCenterService.TAG, "unable to update user key", e);
            }
          } else {
            try {
              Log.v("pubkey", "Updating key for " + from);
              Keyring.setKey(
                  getContext(), from, _publicKey, selfJid ? MyUsers.Keys.TRUST_VERIFIED : -1);

              // update display name with uid (if empty)
              PGPUserID keyUid = PGP.parseUserId(_publicKey, getConnection().getServiceName());
              if (keyUid != null && keyUid.getName() != null)
                UsersProvider.updateDisplayNameIfEmpty(getContext(), from, keyUid.getName());

              // invalidate cache for this user
              Contact.invalidate(from);
            } catch (Exception e) {
              // TODO warn user
              Log.e(MessageCenterService.TAG, "unable to update user key", e);
            }
          }
        }
      }
    }
  }