001/**
002 *
003 * Copyright 2017 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.omemo.exceptions;
018
019import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
020import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
021
022/**
023 * Exception that gets thrown when we try to en-/decrypt a message for an untrusted contact.
024 * This might either be because the user actively untrusted a device, or we receive a message from a contact
025 * which contains an identityKey that differs from the one the user trusted.
026 */
027public class UntrustedOmemoIdentityException extends Exception {
028
029    private static final long serialVersionUID = 1L;
030    private final OmemoDevice device;
031    private final OmemoFingerprint trustedKey, untrustedKey;
032
033    /**
034     * Constructor for when we receive a message with an identityKey different from the one we trusted.
035     *
036     * @param device device which sent the message.
037     * @param fpTrusted fingerprint of the identityKey we previously had and trusted.
038     * @param fpUntrusted fingerprint of the new key which is untrusted.
039     */
040    public UntrustedOmemoIdentityException(OmemoDevice device, OmemoFingerprint fpTrusted, OmemoFingerprint fpUntrusted) {
041        super();
042        this.device = device;
043        this.trustedKey = fpTrusted;
044        this.untrustedKey = fpUntrusted;
045    }
046
047    /**
048     * Constructor for when encryption fails because the user untrusted a recipients device.
049     *
050     * @param device device the user wants to encrypt for, but which has been marked as untrusted.
051     * @param untrustedKey fingerprint of that device.
052     */
053    public UntrustedOmemoIdentityException(OmemoDevice device, OmemoFingerprint untrustedKey) {
054        this(device, null, untrustedKey);
055    }
056
057    /**
058     * Return the device which sent the message.
059     *
060     * @return omemoDevice.
061     */
062    public OmemoDevice getDevice() {
063        return device;
064    }
065
066    /**
067     * Return the fingerprint of the key we expected.
068     * This might return null in case this exception got thrown during encryption process.
069     *
070     * @return the trusted fingerprint.
071     */
072    public OmemoFingerprint getTrustedFingerprint() {
073        return trustedKey;
074    }
075
076    /**
077     * Return the fingerprint of the unexpected untrusted key.
078     *
079     * @return the OMEMO fingerprint.
080     */
081    public OmemoFingerprint getUntrustedFingerprint() {
082        return untrustedKey;
083    }
084
085    @Override
086    public String getMessage() {
087        if (trustedKey != null) {
088            return "Untrusted OMEMO Identity encountered:\n" +
089                    "Fingerprint of trusted key:\n" + trustedKey.blocksOf8Chars() + "\n" +
090                    "Fingerprint of untrusted key:\n" + untrustedKey.blocksOf8Chars();
091        } else {
092            return "Untrusted OMEMO Identity encountered:\n" +
093                    "Fingerprint of untrusted key:\n" + untrustedKey.blocksOf8Chars();
094        }
095    }
096}