UntrustedOmemoIdentityException.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.exceptions;

  18. import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
  19. import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;

  20. /**
  21.  * Exception that gets thrown when we try to en-/decrypt a message for an untrusted contact.
  22.  * This might either be because the user actively untrusted a device, or we receive a message from a contact
  23.  * which contains an identityKey that differs from the one the user trusted.
  24.  */
  25. public class UntrustedOmemoIdentityException extends Exception {

  26.     private static final long serialVersionUID = 1L;
  27.     private final OmemoDevice device;
  28.     private final OmemoFingerprint trustedKey, untrustedKey;

  29.     /**
  30.      * Constructor for when we receive a message with an identityKey different from the one we trusted.
  31.      *
  32.      * @param device device which sent the message.
  33.      * @param fpTrusted fingerprint of the identityKey we previously had and trusted.
  34.      * @param fpUntrusted fingerprint of the new key which is untrusted.
  35.      */
  36.     public UntrustedOmemoIdentityException(OmemoDevice device, OmemoFingerprint fpTrusted, OmemoFingerprint fpUntrusted) {
  37.         super();
  38.         this.device = device;
  39.         this.trustedKey = fpTrusted;
  40.         this.untrustedKey = fpUntrusted;
  41.     }

  42.     /**
  43.      * Constructor for when encryption fails because the user untrusted a recipients device.
  44.      *
  45.      * @param device device the user wants to encrypt for, but which has been marked as untrusted.
  46.      * @param untrustedKey fingerprint of that device.
  47.      */
  48.     public UntrustedOmemoIdentityException(OmemoDevice device, OmemoFingerprint untrustedKey) {
  49.         this(device, null, untrustedKey);
  50.     }

  51.     /**
  52.      * Return the device which sent the message.
  53.      *
  54.      * @return omemoDevice.
  55.      */
  56.     public OmemoDevice getDevice() {
  57.         return device;
  58.     }

  59.     /**
  60.      * Return the fingerprint of the key we expected.
  61.      * This might return null in case this exception got thrown during encryption process.
  62.      *
  63.      * @return the trusted fingerprint.
  64.      */
  65.     public OmemoFingerprint getTrustedFingerprint() {
  66.         return trustedKey;
  67.     }

  68.     /**
  69.      * Return the fingerprint of the unexpected untrusted key.
  70.      *
  71.      * @return the OMEMO fingerprint.
  72.      */
  73.     public OmemoFingerprint getUntrustedFingerprint() {
  74.         return untrustedKey;
  75.     }

  76.     @Override
  77.     public String getMessage() {
  78.         if (trustedKey != null) {
  79.             return "Untrusted OMEMO Identity encountered:\n" +
  80.                     "Fingerprint of trusted key:\n" + trustedKey.blocksOf8Chars() + "\n" +
  81.                     "Fingerprint of untrusted key:\n" + untrustedKey.blocksOf8Chars();
  82.         } else {
  83.             return "Untrusted OMEMO Identity encountered:\n" +
  84.                     "Fingerprint of untrusted key:\n" + untrustedKey.blocksOf8Chars();
  85.         }
  86.     }
  87. }