use of net.java.otr4j.OtrPolicy in project Zom-Android by zom.
the class OtrChatManager method transformSending.
public boolean transformSending(Message message, boolean isResponse, byte[] data) {
String localUserId = message.getFrom().getAddress();
String remoteUserId = message.getTo().getAddress();
String body = message.getBody();
SessionID sessionId = getSessionId(localUserId, remoteUserId);
if (mOtrEngine != null && sessionId != null) {
SessionStatus sessionStatus = mOtrEngine.getSessionStatus(sessionId);
if (data != null && sessionStatus != SessionStatus.ENCRYPTED) {
// Cannot send data without OTR, so start a session and drop message.
// Message will be resent by caller when session is encrypted.
startSession(sessionId);
OtrDebugLogger.log("auto-start OTR on data send request");
return false;
}
OtrDebugLogger.log("session status: " + sessionStatus);
try {
OtrPolicy sessionPolicy = getSessionPolicy(sessionId);
if (sessionStatus == SessionStatus.PLAINTEXT && sessionPolicy.getRequireEncryption()) {
startSession(sessionId);
return false;
}
if (sessionStatus != SessionStatus.PLAINTEXT || sessionPolicy.getRequireEncryption()) {
body = mOtrEngine.transformSending(sessionId, body, isResponse, data);
if (!message.getTo().getAddress().contains("/"))
message.setTo(mOtrEngineHost.appendSessionResource(sessionId, message.getTo()));
} else if (sessionStatus == SessionStatus.PLAINTEXT && sessionPolicy.getAllowV2() && sessionPolicy.getSendWhitespaceTag()) {
// Work around asmack not sending whitespace tag for auto discovery
body += " \t \t\t\t\t \t \t \t \t \t \t \t\t \t ";
}
} catch (Exception e) {
OtrDebugLogger.log("error encrypting", e);
return false;
}
}
message.setBody(body);
return true;
}
use of net.java.otr4j.OtrPolicy in project Zom-Android by zom.
the class SessionImpl method transformReceiving.
/*
* (non-Javadoc)
*
* @see
* net.java.otr4j.session.ISession#handleReceivingMessage(java.lang.String)
*/
public String transformReceiving(String msgText, List<TLV> tlvs) throws OtrException, NullPointerException {
OtrPolicy policy = getSessionPolicy();
if (!policy.getAllowV1() && !policy.getAllowV2()) {
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Policy does not allow neither V1 not V2, ignoring message.");
return msgText;
}
try {
msgText = assembler.accumulate(msgText);
} catch (ProtocolException e) {
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "An invalid message fragment was discarded.");
return null;
}
if (msgText == null)
// Not a complete message (yet).
return null;
AbstractMessage m;
try {
m = SerializationUtils.toMessage(msgText);
} catch (IOException e) {
throw new OtrException(e);
}
if (m == null)
// msgText; // Propably null or empty.
return null;
switch(m.messageType) {
case AbstractEncodedMessage.MESSAGE_DATA:
return handleDataMessage((DataMessage) m, tlvs);
case AbstractMessage.MESSAGE_ERROR:
handleErrorMessage((ErrorMessage) m);
return null;
case AbstractMessage.MESSAGE_PLAINTEXT:
return handlePlainTextMessage((PlainTextMessage) m);
case AbstractMessage.MESSAGE_QUERY:
handleQueryMessage((QueryMessage) m);
return null;
case AbstractEncodedMessage.MESSAGE_DH_COMMIT:
case AbstractEncodedMessage.MESSAGE_DHKEY:
case AbstractEncodedMessage.MESSAGE_REVEALSIG:
case AbstractEncodedMessage.MESSAGE_SIGNATURE:
AuthContext auth = this.getAuthContext(false);
auth.handleReceivingMessage(m);
if (auth.getIsSecure()) {
this.setSessionStatus(SessionStatus.ENCRYPTED);
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Gone Secure.");
}
return null;
default:
throw new UnsupportedOperationException("Received an unknown message type.");
}
}
use of net.java.otr4j.OtrPolicy in project Zom-Android by zom.
the class SessionImpl method handlePlainTextMessage.
private String handlePlainTextMessage(PlainTextMessage plainTextMessage) throws OtrException {
// if (DEBUG_ENABLED) Log.d(LOG_TAG,getSessionID().getLocalUserId() + " received a plaintext message from "
// + getSessionID().getRemoteUserId() + " throught "
// + getSessionID().getProtocolName() + ".");
OtrPolicy policy = getSessionPolicy();
List<Integer> versions = plainTextMessage.versions;
if (versions == null || versions.size() < 1) {
// if (DEBUG_ENABLED) Log.d(LOG_TAG,"Received plaintext message without the whitespace tag.");
switch(this.getSessionStatus()) {
case ENCRYPTED:
case FINISHED:
// return "[WARNING UNENCRYPTED: " + plainTextMessage.cleanText + "]";
return plainTextMessage.cleanText;
case PLAINTEXT:
// unencrypted.
if (policy.getRequireEncryption()) {
showError("The message was received unencrypted.");
}
return plainTextMessage.cleanText;
}
} else {
// if (DEBUG_ENABLED) Log.d(LOG_TAG,"Received plaintext message with the whitespace tag.");
switch(this.getSessionStatus()) {
case ENCRYPTED:
case FINISHED:
// Remove the whitespace tag and display the message to the
// user, but warn him that the message was received
// unencrypted.
showError("The message was received unencrypted.");
case PLAINTEXT:
// was received unencrypted.
if (policy.getRequireEncryption())
showError("The message was received unencrypted.");
}
if (policy.getWhitespaceStartAKE()) {
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "WHITESPACE_START_AKE is set");
if (plainTextMessage.versions.contains(2) && policy.getAllowV2()) {
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "V2 tag found.");
getAuthContext(true).respondV2Auth();
} else if (plainTextMessage.versions.contains(1) && policy.getAllowV1()) {
throw new UnsupportedOperationException();
}
}
}
return plainTextMessage.cleanText;
}
use of net.java.otr4j.OtrPolicy in project Zom-Android by zom.
the class SessionImpl method handleErrorMessage.
private void handleErrorMessage(ErrorMessage errorMessage) throws OtrException {
if (DEBUG_ENABLED)
Log.d(LOG_TAG, getSessionID().getLocalUserId() + " received an error message from " + getSessionID().getRemoteUserId() + " throught " + getSessionID().getRemoteUserId() + ".");
OtrPolicy policy = getSessionPolicy();
// Re-negotiate if we got an error and we are encrypted
if (policy.getErrorStartAKE() && getSessionStatus() == SessionStatus.ENCRYPTED) {
showWarning(errorMessage.error + " Initiating encryption.");
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Error message starts AKE.");
doTransmitLastMessage = true;
isLastMessageRetransmit = true;
Vector<Integer> versions = new Vector<Integer>();
if (policy.getAllowV1())
versions.add(1);
if (policy.getAllowV2())
versions.add(2);
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Sending Query");
injectMessage(new QueryMessage(versions));
} else {
showError(errorMessage.error);
}
}
Aggregations