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 * @return omemoDevice. 060 */ 061 public OmemoDevice getDevice() { 062 return device; 063 } 064 065 /** 066 * Return the fingerprint of the key we expected. 067 * This might return null in case this exception got thrown during encryption process. 068 * @return 069 */ 070 public OmemoFingerprint getTrustedFingerprint() { 071 return trustedKey; 072 } 073 074 /** 075 * Return the fingerprint of the unexpected untrusted key. 076 * @return 077 */ 078 public OmemoFingerprint getUntrustedFingerprint() { 079 return untrustedKey; 080 } 081 082 @Override 083 public String toString() { 084 if (trustedKey != null) { 085 return "Untrusted OMEMO Identity encountered:\n" + 086 "Fingerprint of trusted key:\n" + trustedKey.blocksOf8Chars() + "\n" + 087 "Fingerprint of untrusted key:\n" + untrustedKey.blocksOf8Chars(); 088 } else { 089 return "Untrusted OMEMO Identity encountered:\n" + 090 "Fingerprint of untrusted key:\n" + untrustedKey.blocksOf8Chars(); 091 } 092 } 093}