src/net/java/otr4j/session/SessionImpl.java
changeset 933 ca323cff3ac9
parent 928 0ff0059f2ec3
equal deleted inserted replaced
932:2ef1c6096069 933:ca323cff3ac9
    42  * 
    42  * 
    43  * @author George Politis
    43  * @author George Politis
    44  */
    44  */
    45 public class SessionImpl implements Session {
    45 public class SessionImpl implements Session {
    46 
    46 
    47 	/**
       
    48 	 * 
       
    49 	 * @author George Politis
       
    50 	 * 
       
    51 	 */
       
    52 	class TLV {
    47 	class TLV {
    53 		public TLV(int type, byte[] value) {
    48 		public TLV(int type, byte[] value) {
    54 			this.setType(type);
    49 			this.setType(type);
    55 			this.setValue(value);
    50 			this.setValue(value);
    56 		}
    51 		}
    74 		private int type;
    69 		private int type;
    75 		private byte[] value;
    70 		private byte[] value;
    76 	}
    71 	}
    77 
    72 
    78 	private SessionID sessionID;
    73 	private SessionID sessionID;
    79 	private OtrEngineHost listener;
    74 	private OtrEngineHost host;
    80 	private SessionStatus sessionStatus;
    75 	private SessionStatus sessionStatus;
    81 	private AuthContext authContext;
    76 	private AuthContext authContext;
    82 	private SessionKeys[][] sessionKeys;
    77 	private SessionKeys[][] sessionKeys;
    83 	private Vector<byte[]> oldMacKeys;
    78 	private Vector<byte[]> oldMacKeys;
    84 	private static Logger logger = Logger
    79 	private static Logger logger = Logger
    85 			.getLogger(SessionImpl.class.getName());
    80 			.getLogger(SessionImpl.class.getName());
    86 
    81 
    87 	public SessionImpl(SessionID sessionID, OtrEngineHost listener) {
    82 	public SessionImpl(SessionID sessionID, OtrEngineHost listener) {
    88 
    83 
    89 		this.setSessionID(sessionID);
    84 		this.setSessionID(sessionID);
    90 		this.setListener(listener);
    85 		this.setHost(listener);
    91 
    86 
    92 		// client application calls OtrEngine.getSessionStatus()
    87 		// client application calls OtrEngine.getSessionStatus()
    93 		// -> create new session if it does not exist, end up here
    88 		// -> create new session if it does not exist, end up here
    94 		// -> setSessionStatus() fires statusChangedEvent
    89 		// -> setSessionStatus() fires statusChangedEvent
    95 		// -> client application calls OtrEngine.getSessionStatus()
    90 		// -> client application calls OtrEngine.getSessionStatus()
   272 	 */
   267 	 */
   273 	public SessionID getSessionID() {
   268 	public SessionID getSessionID() {
   274 		return sessionID;
   269 		return sessionID;
   275 	}
   270 	}
   276 
   271 
   277 	private void setListener(OtrEngineHost listener) {
   272 	private void setHost(OtrEngineHost host) {
   278 		this.listener = listener;
   273 		this.host = host;
   279 	}
   274 	}
   280 
   275 
   281 	private OtrEngineHost getListener() {
   276 	private OtrEngineHost getHost() {
   282 		return listener;
   277 		return host;
   283 	}
   278 	}
   284 
   279 
   285 	private SessionKeys[][] getSessionKeys() {
   280 	private SessionKeys[][] getSessionKeys() {
   286 		if (sessionKeys == null)
   281 		if (sessionKeys == null)
   287 			sessionKeys = new SessionKeys[2][2];
   282 			sessionKeys = new SessionKeys[2][2];
   318 		try {
   313 		try {
   319 			m = SerializationUtils.toMessage(msgText);
   314 			m = SerializationUtils.toMessage(msgText);
   320 		} catch (IOException e) {
   315 		} catch (IOException e) {
   321 			throw new OtrException(e);
   316 			throw new OtrException(e);
   322 		}
   317 		}
       
   318 		
       
   319 		if (m == null)
       
   320 			return msgText; // Propably null or empty.
   323 
   321 
   324 		switch (m.messageType) {
   322 		switch (m.messageType) {
   325 		case AbstractEncodedMessage.MESSAGE_DATA:
   323 		case AbstractEncodedMessage.MESSAGE_DATA:
   326 			return handleDataMessage((DataMessage) m);
   324 			return handleDataMessage((DataMessage) m);
   327 		case AbstractMessage.MESSAGE_ERROR:
   325 		case AbstractMessage.MESSAGE_ERROR:
   373 		logger.finest(getSessionID().getAccountID()
   371 		logger.finest(getSessionID().getAccountID()
   374 				+ " received an error message from "
   372 				+ " received an error message from "
   375 				+ getSessionID().getUserID() + " throught "
   373 				+ getSessionID().getUserID() + " throught "
   376 				+ getSessionID().getUserID() + ".");
   374 				+ getSessionID().getUserID() + ".");
   377 
   375 
   378 		getListener().showError(this.getSessionID(), errorMessage.error);
   376 		getHost().showError(this.getSessionID(), errorMessage.error);
   379 
   377 
   380 		OtrPolicy policy = getSessionPolicy();
   378 		OtrPolicy policy = getSessionPolicy();
   381 		if (policy.getErrorStartAKE()) {
   379 		if (policy.getErrorStartAKE()) {
   382 			logger.finest("Error message starts AKE.");
   380 			logger.finest("Error message starts AKE.");
   383 			Vector<Integer> versions = new Vector<Integer>();
   381 			Vector<Integer> versions = new Vector<Integer>();
   504 
   502 
   505 			return decryptedMsgContent;
   503 			return decryptedMsgContent;
   506 
   504 
   507 		case FINISHED:
   505 		case FINISHED:
   508 		case PLAINTEXT:
   506 		case PLAINTEXT:
   509 			getListener().showWarning(this.getSessionID(),
   507 			getHost().showWarning(this.getSessionID(),
   510 					"Unreadable encrypted message was received.");
   508 					"Unreadable encrypted message was received.");
   511 
   509 
   512 			injectMessage(new ErrorMessage(AbstractMessage.MESSAGE_ERROR,
   510 			injectMessage(new ErrorMessage(AbstractMessage.MESSAGE_ERROR,
   513 					"You sent me an unreadable encrypted message.."));
   511 					"You sent me an unreadable encrypted message.."));
   514 			break;
   512 			break;
   522 		try {
   520 		try {
   523 			msg = SerializationUtils.toString(m);
   521 			msg = SerializationUtils.toString(m);
   524 		} catch (IOException e) {
   522 		} catch (IOException e) {
   525 			throw new OtrException(e);
   523 			throw new OtrException(e);
   526 		}
   524 		}
   527 		getListener().injectMessage(getSessionID(), msg);
   525 		getHost().injectMessage(getSessionID(), msg);
   528 	}
   526 	}
   529 
   527 
   530 	private String handlePlainTextMessage(PlainTextMessage plainTextMessage)
   528 	private String handlePlainTextMessage(PlainTextMessage plainTextMessage)
   531 			throws OtrException {
   529 			throws OtrException {
   532 		logger.finest(getSessionID().getAccountID()
   530 		logger.finest(getSessionID().getAccountID()
   542 			switch (this.getSessionStatus()) {
   540 			switch (this.getSessionStatus()) {
   543 			case ENCRYPTED:
   541 			case ENCRYPTED:
   544 			case FINISHED:
   542 			case FINISHED:
   545 				// Display the message to the user, but warn him that the
   543 				// Display the message to the user, but warn him that the
   546 				// message was received unencrypted.
   544 				// message was received unencrypted.
   547 				getListener().showWarning(this.getSessionID(),
   545 				getHost().showWarning(this.getSessionID(),
   548 						"The message was received unencrypted.");
   546 						"The message was received unencrypted.");
   549 				return plainTextMessage.cleanText;
   547 				return plainTextMessage.cleanText;
   550 			case PLAINTEXT:
   548 			case PLAINTEXT:
   551 				// Simply display the message to the user. If
   549 				// Simply display the message to the user. If
   552 				// REQUIRE_ENCRYPTION
   550 				// REQUIRE_ENCRYPTION
   553 				// is set, warn him that the message was received
   551 				// is set, warn him that the message was received
   554 				// unencrypted.
   552 				// unencrypted.
   555 				if (policy.getRequireEncryption()) {
   553 				if (policy.getRequireEncryption()) {
   556 					getListener().showWarning(this.getSessionID(),
   554 					getHost().showWarning(this.getSessionID(),
   557 							"The message was received unencrypted.");
   555 							"The message was received unencrypted.");
   558 				}
   556 				}
   559 				return plainTextMessage.cleanText;
   557 				return plainTextMessage.cleanText;
   560 			}
   558 			}
   561 		} else {
   559 		} else {
   565 			case ENCRYPTED:
   563 			case ENCRYPTED:
   566 			case FINISHED:
   564 			case FINISHED:
   567 				// Remove the whitespace tag and display the message to the
   565 				// Remove the whitespace tag and display the message to the
   568 				// user, but warn him that the message was received
   566 				// user, but warn him that the message was received
   569 				// unencrypted.
   567 				// unencrypted.
   570 				getListener().showWarning(this.getSessionID(),
   568 				getHost().showWarning(this.getSessionID(),
   571 						"The message was received unencrypted.");
   569 						"The message was received unencrypted.");
   572 			case PLAINTEXT:
   570 			case PLAINTEXT:
   573 				// Remove the whitespace tag and display the message to the
   571 				// Remove the whitespace tag and display the message to the
   574 				// user. If REQUIRE_ENCRYPTION is set, warn him that the
   572 				// user. If REQUIRE_ENCRYPTION is set, warn him that the
   575 				// message
   573 				// message
   576 				// was received unencrypted.
   574 				// was received unencrypted.
   577 				if (policy.getRequireEncryption())
   575 				if (policy.getRequireEncryption())
   578 					getListener().showWarning(this.getSessionID(),
   576 					getHost().showWarning(this.getSessionID(),
   579 							"The message was received unencrypted.");
   577 							"The message was received unencrypted.");
   580 			}
   578 			}
   581 
   579 
   582 			if (policy.getWhitespaceStartAKE()) {
   580 			if (policy.getWhitespaceStartAKE()) {
   583 				logger.finest("WHITESPACE_START_AKE is set");
   581 				logger.finest("WHITESPACE_START_AKE is set");
   694 			} catch (IOException e) {
   692 			} catch (IOException e) {
   695 				throw new OtrException(e);
   693 				throw new OtrException(e);
   696 			}
   694 			}
   697 		case FINISHED:
   695 		case FINISHED:
   698 			this.lastSentMessage = msgText;
   696 			this.lastSentMessage = msgText;
   699 			getListener()
   697 			getHost()
   700 					.showError(
   698 					.showError(
   701 							sessionID,
   699 							sessionID,
   702 							"Your message to "
   700 							"Your message to "
   703 									+ sessionID.getUserID()
   701 									+ sessionID.getUserID()
   704 									+ " was not sent.  Either end your private conversation, or restart it.");
   702 									+ " was not sent.  Either end your private conversation, or restart it.");
   735 		case ENCRYPTED:
   733 		case ENCRYPTED:
   736 			Vector<TLV> tlvs = new Vector<TLV>();
   734 			Vector<TLV> tlvs = new Vector<TLV>();
   737 			tlvs.add(new TLV(1, null));
   735 			tlvs.add(new TLV(1, null));
   738 
   736 
   739 			String msg = this.transformSending(null, tlvs);
   737 			String msg = this.transformSending(null, tlvs);
   740 			getListener().injectMessage(getSessionID(), msg);
   738 			getHost().injectMessage(getSessionID(), msg);
   741 			this.setSessionStatus(SessionStatus.PLAINTEXT);
   739 			this.setSessionStatus(SessionStatus.PLAINTEXT);
   742 			break;
   740 			break;
   743 		case FINISHED:
   741 		case FINISHED:
   744 			this.setSessionStatus(SessionStatus.PLAINTEXT);
   742 			this.setSessionStatus(SessionStatus.PLAINTEXT);
   745 			break;
   743 			break;
   774 	public void addOtrEngineListener(OtrEngineListener l) {
   772 	public void addOtrEngineListener(OtrEngineListener l) {
   775 		synchronized (listeners) {
   773 		synchronized (listeners) {
   776 			if (!listeners.contains(l))
   774 			if (!listeners.contains(l))
   777 				listeners.add(l);
   775 				listeners.add(l);
   778 		}
   776 		}
   779 
       
   780 	}
   777 	}
   781 
   778 
   782 	public void removeOtrEngineListener(OtrEngineListener l) {
   779 	public void removeOtrEngineListener(OtrEngineListener l) {
   783 		synchronized (listeners) {
   780 		synchronized (listeners) {
   784 			listeners.remove(l);
   781 			listeners.remove(l);
   785 		}
   782 		}
   786 	}
   783 	}
   787 
   784 
   788 	public OtrPolicy getSessionPolicy() {
   785 	public OtrPolicy getSessionPolicy() {
   789 		return getListener().getSessionPolicy(getSessionID());
   786 		return getHost().getSessionPolicy(getSessionID());
   790 	}
   787 	}
   791 
   788 
   792 	public KeyPair getLocalKeyPair() {
   789 	public KeyPair getLocalKeyPair() {
   793 		return getListener().getKeyPair(this.getSessionID());
   790 		return getHost().getKeyPair(this.getSessionID());
   794 	}
   791 	}
   795 }
   792 }