aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java
blob: 4189aba4f5540e03a19470d78bdb49ff7e431002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
package eu.siacs.conversations.crypto.axolotl;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import org.whispersystems.libaxolotl.AxolotlAddress;
import org.whispersystems.libaxolotl.DuplicateMessageException;
import org.whispersystems.libaxolotl.IdentityKey;
import org.whispersystems.libaxolotl.IdentityKeyPair;
import org.whispersystems.libaxolotl.InvalidKeyException;
import org.whispersystems.libaxolotl.InvalidKeyIdException;
import org.whispersystems.libaxolotl.InvalidMessageException;
import org.whispersystems.libaxolotl.InvalidVersionException;
import org.whispersystems.libaxolotl.LegacyMessageException;
import org.whispersystems.libaxolotl.NoSessionException;
import org.whispersystems.libaxolotl.SessionBuilder;
import org.whispersystems.libaxolotl.SessionCipher;
import org.whispersystems.libaxolotl.UntrustedIdentityException;
import org.whispersystems.libaxolotl.ecc.Curve;
import org.whispersystems.libaxolotl.ecc.ECKeyPair;
import org.whispersystems.libaxolotl.ecc.ECPublicKey;
import org.whispersystems.libaxolotl.protocol.CiphertextMessage;
import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage;
import org.whispersystems.libaxolotl.protocol.WhisperMessage;
import org.whispersystems.libaxolotl.state.AxolotlStore;
import org.whispersystems.libaxolotl.state.PreKeyBundle;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.SessionRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.util.KeyHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.parser.IqParser;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;

public class AxolotlService {

	public static final String PEP_PREFIX = "eu.siacs.conversations.axolotl";
	public static final String PEP_DEVICE_LIST = PEP_PREFIX + ".devicelist";
	public static final String PEP_BUNDLES = PEP_PREFIX + ".bundles";

	public static final String LOGPREFIX = "AxolotlService";

	public static final int NUM_KEYS_TO_PUBLISH = 10;

	private final Account account;
	private final XmppConnectionService mXmppConnectionService;
	private final SQLiteAxolotlStore axolotlStore;
	private final SessionMap sessions;
	private final Map<Jid, Set<Integer>> deviceIds;
	private final Map<String, MessagePacket> messageCache;
	private final FetchStatusMap fetchStatusMap;
	private final SerialSingleThreadExecutor executor;

	public static class SQLiteAxolotlStore implements AxolotlStore {

		public static final String PREKEY_TABLENAME = "prekeys";
		public static final String SIGNED_PREKEY_TABLENAME = "signed_prekeys";
		public static final String SESSION_TABLENAME = "sessions";
		public static final String IDENTITIES_TABLENAME = "identities";
		public static final String ACCOUNT = "account";
		public static final String DEVICE_ID = "device_id";
		public static final String ID = "id";
		public static final String KEY = "key";
		public static final String FINGERPRINT = "fingerprint";
		public static final String NAME = "name";
		public static final String TRUSTED = "trusted";
		public static final String OWN = "ownkey";

		public static final String JSONKEY_REGISTRATION_ID = "axolotl_reg_id";
		public static final String JSONKEY_CURRENT_PREKEY_ID = "axolotl_cur_prekey_id";

		private final Account account;
		private final XmppConnectionService mXmppConnectionService;

		private IdentityKeyPair identityKeyPair;
		private int localRegistrationId;
		private int currentPreKeyId = 0;

		public enum Trust {
			UNDECIDED, // 0
			TRUSTED,
			UNTRUSTED;

			public String toString() {
				switch(this){
					case UNDECIDED:
						return "Trust undecided";
					case TRUSTED:
						return "Trusted";
					case UNTRUSTED:
					default:
						return "Untrusted";
				}
			}

			public static Trust fromBoolean(Boolean trusted) {
				return trusted?TRUSTED:UNTRUSTED;
			}
		};

		private static IdentityKeyPair generateIdentityKeyPair() {
			Log.i(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Generating axolotl IdentityKeyPair...");
			ECKeyPair identityKeyPairKeys = Curve.generateKeyPair();
			IdentityKeyPair ownKey = new IdentityKeyPair(new IdentityKey(identityKeyPairKeys.getPublicKey()),
					identityKeyPairKeys.getPrivateKey());
			return ownKey;
		}

		private static int generateRegistrationId() {
			Log.i(Config.LOGTAG, AxolotlService.LOGPREFIX+" : "+"Generating axolotl registration ID...");
			int reg_id = KeyHelper.generateRegistrationId(true);
			return reg_id;
		}

		public SQLiteAxolotlStore(Account account, XmppConnectionService service) {
			this.account = account;
			this.mXmppConnectionService = service;
			this.localRegistrationId = loadRegistrationId();
			this.currentPreKeyId = loadCurrentPreKeyId();
			for (SignedPreKeyRecord record : loadSignedPreKeys()) {
				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Got Axolotl signed prekey record:" + record.getId());
			}
		}

		public int getCurrentPreKeyId() {
			return currentPreKeyId;
		}

		// --------------------------------------
		// IdentityKeyStore
		// --------------------------------------

		private IdentityKeyPair loadIdentityKeyPair() {
			String ownName = account.getJid().toBareJid().toString();
			IdentityKeyPair ownKey = mXmppConnectionService.databaseBackend.loadOwnIdentityKeyPair(account,
					ownName);

			if (ownKey != null) {
				return ownKey;
			} else {
				Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Could not retrieve axolotl key for account " + ownName);
				ownKey = generateIdentityKeyPair();
				mXmppConnectionService.databaseBackend.storeOwnIdentityKeyPair(account, ownName, ownKey);
			}
			return ownKey;
		}

		private int loadRegistrationId() {
			return loadRegistrationId(false);
		}

		private int loadRegistrationId(boolean regenerate) {
			String regIdString = this.account.getKey(JSONKEY_REGISTRATION_ID);
			int reg_id;
			if (!regenerate && regIdString != null) {
				reg_id = Integer.valueOf(regIdString);
			} else {
				Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Could not retrieve axolotl registration id for account " + account.getJid());
				reg_id = generateRegistrationId();
				boolean success = this.account.setKey(JSONKEY_REGISTRATION_ID, Integer.toString(reg_id));
				if (success) {
					mXmppConnectionService.databaseBackend.updateAccount(account);
				} else {
					Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Failed to write new key to the database!");
				}
			}
			return reg_id;
		}

		private int loadCurrentPreKeyId() {
			String regIdString = this.account.getKey(JSONKEY_CURRENT_PREKEY_ID);
			int reg_id;
			if (regIdString != null) {
				reg_id = Integer.valueOf(regIdString);
			} else {
				Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Could not retrieve current prekey id for account " + account.getJid());
				reg_id = 0;
			}
			return reg_id;
		}

		public void regenerate() {
			mXmppConnectionService.databaseBackend.wipeAxolotlDb(account);
			account.setKey(JSONKEY_CURRENT_PREKEY_ID, Integer.toString(0));
			identityKeyPair = loadIdentityKeyPair();
			localRegistrationId = loadRegistrationId(true);
			currentPreKeyId = 0;
			mXmppConnectionService.updateAccountUi();
		}

		/**
		 * Get the local client's identity key pair.
		 *
		 * @return The local client's persistent identity key pair.
		 */
		@Override
		public IdentityKeyPair getIdentityKeyPair() {
			if(identityKeyPair == null) {
				identityKeyPair = loadIdentityKeyPair();
			}
			return identityKeyPair;
		}

		/**
		 * Return the local client's registration ID.
		 * <p/>
		 * Clients should maintain a registration ID, a random number
		 * between 1 and 16380 that's generated once at install time.
		 *
		 * @return the local client's registration ID.
		 */
		@Override
		public int getLocalRegistrationId() {
			return localRegistrationId;
		}

		/**
		 * Save a remote client's identity key
		 * <p/>
		 * Store a remote client's identity key as trusted.
		 *
		 * @param name        The name of the remote client.
		 * @param identityKey The remote client's identity key.
		 */
		@Override
		public void saveIdentity(String name, IdentityKey identityKey) {
			if(!mXmppConnectionService.databaseBackend.loadIdentityKeys(account, name).contains(identityKey)) {
				mXmppConnectionService.databaseBackend.storeIdentityKey(account, name, identityKey);
			}
		}

		/**
		 * Verify a remote client's identity key.
		 * <p/>
		 * Determine whether a remote client's identity is trusted.  Convention is
		 * that the TextSecure protocol is 'trust on first use.'  This means that
		 * an identity key is considered 'trusted' if there is no entry for the recipient
		 * in the local store, or if it matches the saved key for a recipient in the local
		 * store.  Only if it mismatches an entry in the local store is it considered
		 * 'untrusted.'
		 *
		 * @param name        The name of the remote client.
		 * @param identityKey The identity key to verify.
		 * @return true if trusted, false if untrusted.
		 */
		@Override
		public boolean isTrustedIdentity(String name, IdentityKey identityKey) {
			return true;
		}

		public Trust getFingerprintTrust(String fingerprint) {
			return mXmppConnectionService.databaseBackend.isIdentityKeyTrusted(account, fingerprint);
		}

		public void setFingerprintTrust(String fingerprint, Trust trust) {
			mXmppConnectionService.databaseBackend.setIdentityKeyTrust(account, fingerprint, trust);
		}

		public Set<IdentityKey> getContactUndecidedKeys(String bareJid) {
			return mXmppConnectionService.databaseBackend.loadIdentityKeys(account, bareJid, Trust.UNDECIDED);
		}

		// --------------------------------------
		// SessionStore
		// --------------------------------------

		/**
		 * Returns a copy of the {@link SessionRecord} corresponding to the recipientId + deviceId tuple,
		 * or a new SessionRecord if one does not currently exist.
		 * <p/>
		 * It is important that implementations return a copy of the current durable information.  The
		 * returned SessionRecord may be modified, but those changes should not have an effect on the
		 * durable session state (what is returned by subsequent calls to this method) without the
		 * store method being called here first.
		 *
		 * @param address The name and device ID of the remote client.
		 * @return a copy of the SessionRecord corresponding to the recipientId + deviceId tuple, or
		 * a new SessionRecord if one does not currently exist.
		 */
		@Override
		public SessionRecord loadSession(AxolotlAddress address) {
			SessionRecord session = mXmppConnectionService.databaseBackend.loadSession(this.account, address);
			return (session != null) ? session : new SessionRecord();
		}

		/**
		 * Returns all known devices with active sessions for a recipient
		 *
		 * @param name the name of the client.
		 * @return all known sub-devices with active sessions.
		 */
		@Override
		public List<Integer> getSubDeviceSessions(String name) {
			return mXmppConnectionService.databaseBackend.getSubDeviceSessions(account,
					new AxolotlAddress(name, 0));
		}

		/**
		 * Commit to storage the {@link SessionRecord} for a given recipientId + deviceId tuple.
		 *
		 * @param address the address of the remote client.
		 * @param record  the current SessionRecord for the remote client.
		 */
		@Override
		public void storeSession(AxolotlAddress address, SessionRecord record) {
			mXmppConnectionService.databaseBackend.storeSession(account, address, record);
		}

		/**
		 * Determine whether there is a committed {@link SessionRecord} for a recipientId + deviceId tuple.
		 *
		 * @param address the address of the remote client.
		 * @return true if a {@link SessionRecord} exists, false otherwise.
		 */
		@Override
		public boolean containsSession(AxolotlAddress address) {
			return mXmppConnectionService.databaseBackend.containsSession(account, address);
		}

		/**
		 * Remove a {@link SessionRecord} for a recipientId + deviceId tuple.
		 *
		 * @param address the address of the remote client.
		 */
		@Override
		public void deleteSession(AxolotlAddress address) {
			mXmppConnectionService.databaseBackend.deleteSession(account, address);
		}

		/**
		 * Remove the {@link SessionRecord}s corresponding to all devices of a recipientId.
		 *
		 * @param name the name of the remote client.
		 */
		@Override
		public void deleteAllSessions(String name) {
			mXmppConnectionService.databaseBackend.deleteAllSessions(account,
					new AxolotlAddress(name, 0));
		}

		// --------------------------------------
		// PreKeyStore
		// --------------------------------------

		/**
		 * Load a local PreKeyRecord.
		 *
		 * @param preKeyId the ID of the local PreKeyRecord.
		 * @return the corresponding PreKeyRecord.
		 * @throws InvalidKeyIdException when there is no corresponding PreKeyRecord.
		 */
		@Override
		public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException {
			PreKeyRecord record = mXmppConnectionService.databaseBackend.loadPreKey(account, preKeyId);
			if (record == null) {
				throw new InvalidKeyIdException("No such PreKeyRecord: " + preKeyId);
			}
			return record;
		}

		/**
		 * Store a local PreKeyRecord.
		 *
		 * @param preKeyId the ID of the PreKeyRecord to store.
		 * @param record   the PreKeyRecord.
		 */
		@Override
		public void storePreKey(int preKeyId, PreKeyRecord record) {
			mXmppConnectionService.databaseBackend.storePreKey(account, record);
			currentPreKeyId = preKeyId;
			boolean success = this.account.setKey(JSONKEY_CURRENT_PREKEY_ID, Integer.toString(preKeyId));
			if (success) {
				mXmppConnectionService.databaseBackend.updateAccount(account);
			} else {
				Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Failed to write new prekey id to the database!");
			}
		}

		/**
		 * @param preKeyId A PreKeyRecord ID.
		 * @return true if the store has a record for the preKeyId, otherwise false.
		 */
		@Override
		public boolean containsPreKey(int preKeyId) {
			return mXmppConnectionService.databaseBackend.containsPreKey(account, preKeyId);
		}

		/**
		 * Delete a PreKeyRecord from local storage.
		 *
		 * @param preKeyId The ID of the PreKeyRecord to remove.
		 */
		@Override
		public void removePreKey(int preKeyId) {
			mXmppConnectionService.databaseBackend.deletePreKey(account, preKeyId);
		}

		// --------------------------------------
		// SignedPreKeyStore
		// --------------------------------------

		/**
		 * Load a local SignedPreKeyRecord.
		 *
		 * @param signedPreKeyId the ID of the local SignedPreKeyRecord.
		 * @return the corresponding SignedPreKeyRecord.
		 * @throws InvalidKeyIdException when there is no corresponding SignedPreKeyRecord.
		 */
		@Override
		public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
			SignedPreKeyRecord record = mXmppConnectionService.databaseBackend.loadSignedPreKey(account, signedPreKeyId);
			if (record == null) {
				throw new InvalidKeyIdException("No such SignedPreKeyRecord: " + signedPreKeyId);
			}
			return record;
		}

		/**
		 * Load all local SignedPreKeyRecords.
		 *
		 * @return All stored SignedPreKeyRecords.
		 */
		@Override
		public List<SignedPreKeyRecord> loadSignedPreKeys() {
			return mXmppConnectionService.databaseBackend.loadSignedPreKeys(account);
		}

		/**
		 * Store a local SignedPreKeyRecord.
		 *
		 * @param signedPreKeyId the ID of the SignedPreKeyRecord to store.
		 * @param record         the SignedPreKeyRecord.
		 */
		@Override
		public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
			mXmppConnectionService.databaseBackend.storeSignedPreKey(account, record);
		}

		/**
		 * @param signedPreKeyId A SignedPreKeyRecord ID.
		 * @return true if the store has a record for the signedPreKeyId, otherwise false.
		 */
		@Override
		public boolean containsSignedPreKey(int signedPreKeyId) {
			return mXmppConnectionService.databaseBackend.containsSignedPreKey(account, signedPreKeyId);
		}

		/**
		 * Delete a SignedPreKeyRecord from local storage.
		 *
		 * @param signedPreKeyId The ID of the SignedPreKeyRecord to remove.
		 */
		@Override
		public void removeSignedPreKey(int signedPreKeyId) {
			mXmppConnectionService.databaseBackend.deleteSignedPreKey(account, signedPreKeyId);
		}
	}

	public static class XmppAxolotlSession {
		private final SessionCipher cipher;
		private Integer preKeyId = null;
		private final SQLiteAxolotlStore sqLiteAxolotlStore;
		private final AxolotlAddress remoteAddress;
		private final Account account;
		private String fingerprint = null;

		public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress, String fingerprint) {
			this(account, store, remoteAddress);
			this.fingerprint = fingerprint;
		}

		public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress) {
			this.cipher = new SessionCipher(store, remoteAddress);
			this.remoteAddress = remoteAddress;
			this.sqLiteAxolotlStore = store;
			this.account = account;
		}

		public Integer getPreKeyId() {
			return preKeyId;
		}

		public void resetPreKeyId() {

			preKeyId = null;
		}

		public String getFingerprint() {
			return fingerprint;
		}

		public byte[] processReceiving(XmppAxolotlMessage.XmppAxolotlMessageHeader incomingHeader) {
			byte[] plaintext = null;
			try {
				try {
					PreKeyWhisperMessage message = new PreKeyWhisperMessage(incomingHeader.getContents());
					Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"PreKeyWhisperMessage received, new session ID:" + message.getSignedPreKeyId() + "/" + message.getPreKeyId());
					String fingerprint = message.getIdentityKey().getFingerprint().replaceAll("\\s", "");
					if (this.fingerprint != null && !this.fingerprint.equals(fingerprint)) {
						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Had session with fingerprint "+ this.fingerprint+", received message with fingerprint "+fingerprint);
					} else {
						this.fingerprint = fingerprint;
						plaintext = cipher.decrypt(message);
						if (message.getPreKeyId().isPresent()) {
							preKeyId = message.getPreKeyId().get();
						}
					}
				} catch (InvalidMessageException | InvalidVersionException e) {
					Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"WhisperMessage received");
					WhisperMessage message = new WhisperMessage(incomingHeader.getContents());
					plaintext = cipher.decrypt(message);
				} catch (InvalidKeyException | InvalidKeyIdException | UntrustedIdentityException e) {
					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Error decrypting axolotl header, "+e.getClass().getName()+": " + e.getMessage());
				}
			} catch (LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException  e) {
				Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Error decrypting axolotl header, "+e.getClass().getName()+": " + e.getMessage());
			}
			return plaintext;
		}

		public XmppAxolotlMessage.XmppAxolotlMessageHeader processSending(byte[] outgoingMessage) {
			CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage);
			XmppAxolotlMessage.XmppAxolotlMessageHeader header =
					new XmppAxolotlMessage.XmppAxolotlMessageHeader(remoteAddress.getDeviceId(),
							ciphertextMessage.serialize());
			return header;
		}
	}

	private static class AxolotlAddressMap<T> {
		protected Map<String, Map<Integer, T>> map;
		protected final Object MAP_LOCK = new Object();

		public AxolotlAddressMap() {
			this.map = new HashMap<>();
		}

		public void put(AxolotlAddress address, T value) {
			synchronized (MAP_LOCK) {
				Map<Integer, T> devices = map.get(address.getName());
				if (devices == null) {
					devices = new HashMap<>();
					map.put(address.getName(), devices);
				}
				devices.put(address.getDeviceId(), value);
			}
		}

		public T get(AxolotlAddress address) {
			synchronized (MAP_LOCK) {
				Map<Integer, T> devices = map.get(address.getName());
				if (devices == null) {
					return null;
				}
				return devices.get(address.getDeviceId());
			}
		}

		public Map<Integer, T> getAll(AxolotlAddress address) {
			synchronized (MAP_LOCK) {
				Map<Integer, T> devices = map.get(address.getName());
				if (devices == null) {
					return new HashMap<>();
				}
				return devices;
			}
		}

		public boolean hasAny(AxolotlAddress address) {
			synchronized (MAP_LOCK) {
				Map<Integer, T> devices = map.get(address.getName());
				return devices != null && !devices.isEmpty();
			}
		}

		public void clear() {
			map.clear();
		}

	}

	private static class SessionMap extends AxolotlAddressMap<XmppAxolotlSession> {
		private final XmppConnectionService xmppConnectionService;
		private final Account account;

		public SessionMap(XmppConnectionService service, SQLiteAxolotlStore store, Account account) {
			super();
			this.xmppConnectionService = service;
			this.account = account;
			this.fillMap(store);
		}

		private void fillMap(SQLiteAxolotlStore store) {
			for (Contact contact : account.getRoster().getContacts()) {
				Jid bareJid = contact.getJid().toBareJid();
				if (bareJid == null) {
					continue; // FIXME: handle this?
				}
				String address = bareJid.toString();
				List<Integer> deviceIDs = store.getSubDeviceSessions(address);
				for (Integer deviceId : deviceIDs) {
					AxolotlAddress axolotlAddress = new AxolotlAddress(address, deviceId);
					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Building session for remote address: "+axolotlAddress.toString());
					String fingerprint = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey().getFingerprint().replaceAll("\\s", "");
					this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint));
				}
			}
		}

		@Override
		public void put(AxolotlAddress address, XmppAxolotlSession value) {
			super.put(address, value);
			xmppConnectionService.syncRosterToDisk(account);
		}
	}

	private static enum FetchStatus {
		PENDING,
		SUCCESS,
		ERROR
	}

	private static class FetchStatusMap extends AxolotlAddressMap<FetchStatus> {

	}
	
	public static String getLogprefix(Account account) {
		return LOGPREFIX+" ("+account.getJid().toBareJid().toString()+"): ";
	}

	public AxolotlService(Account account, XmppConnectionService connectionService) {
		this.mXmppConnectionService = connectionService;
		this.account = account;
		this.axolotlStore = new SQLiteAxolotlStore(this.account, this.mXmppConnectionService);
		this.deviceIds = new HashMap<>();
		this.messageCache = new HashMap<>();
		this.sessions = new SessionMap(mXmppConnectionService, axolotlStore, account);
		this.fetchStatusMap = new FetchStatusMap();
		this.executor = new SerialSingleThreadExecutor();
	}

	public IdentityKey getOwnPublicKey() {
		return axolotlStore.getIdentityKeyPair().getPublicKey();
	}

	public Set<IdentityKey> getPendingKeys() {
		return axolotlStore.getContactUndecidedKeys(account.getJid().toBareJid().toString());
	}

	public Set<IdentityKey> getPendingKeys(Contact contact) {
		return axolotlStore.getContactUndecidedKeys(contact.getJid().toBareJid().toString());
	}

	private AxolotlAddress getAddressForJid(Jid jid) {
		return new AxolotlAddress(jid.toString(), 0);
	}

	private Set<XmppAxolotlSession> findOwnSessions() {
		AxolotlAddress ownAddress = getAddressForJid(account.getJid().toBareJid());
		Set<XmppAxolotlSession> ownDeviceSessions = new HashSet<>(this.sessions.getAll(ownAddress).values());
		return ownDeviceSessions;
	}

	private Set<XmppAxolotlSession> findSessionsforContact(Contact contact) {
		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
		Set<XmppAxolotlSession> sessions = new HashSet<>(this.sessions.getAll(contactAddress).values());
		return sessions;
	}

	private boolean hasAny(Contact contact) {
		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
		return sessions.hasAny(contactAddress);
	}

	public void regenerateKeys() {
		axolotlStore.regenerate();
		sessions.clear();
		fetchStatusMap.clear();
		publishBundlesIfNeeded();
		publishOwnDeviceIdIfNeeded();
	}

	public int getOwnDeviceId() {
		return axolotlStore.loadRegistrationId();
	}

	public Set<Integer> getOwnDeviceIds() {
		return this.deviceIds.get(account.getJid().toBareJid());
	}

	public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
		if(deviceIds.contains(getOwnDeviceId())) {
			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Skipping own Device ID:"+ jid + ":"+getOwnDeviceId());
			deviceIds.remove(getOwnDeviceId());
		}
		for(Integer i:deviceIds) {
			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding Device ID:"+ jid + ":"+i);
		}
		this.deviceIds.put(jid, deviceIds);
		publishOwnDeviceIdIfNeeded();
	}

	public void wipeOtherPepDevices() {
		Set<Integer> deviceIds = new HashSet<>();
		deviceIds.add(getOwnDeviceId());
		IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Wiping all other devices from Pep:" + publish);
		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
			@Override
			public void onIqPacketReceived(Account account, IqPacket packet) {
				// TODO: implement this!
			}
		});
	}

	public void publishOwnDeviceIdIfNeeded() {
		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().toBareJid());
		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
			@Override
			public void onIqPacketReceived(Account account, IqPacket packet) {
				Element item = mXmppConnectionService.getIqParser().getItem(packet);
				Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
				if (deviceIds == null) {
					deviceIds = new HashSet<Integer>();
				}
				if (!deviceIds.contains(getOwnDeviceId())) {
					deviceIds.add(getOwnDeviceId());
					IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Own device " + getOwnDeviceId() + " not in PEP devicelist. Publishing: " + publish);
					mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
						@Override
						public void onIqPacketReceived(Account account, IqPacket packet) {
							// TODO: implement this!
						}
					});
				}
			}
		});
	}

	public void publishBundlesIfNeeded() {
		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
			@Override
			public void onIqPacketReceived(Account account, IqPacket packet) {
				PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
				Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
				boolean flush = false;
				if (bundle == null) {
					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received invalid bundle:" + packet);
					bundle = new PreKeyBundle(-1, -1, -1 , null, -1, null, null, null);
					flush = true;
				}
				if (keys == null) {
					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received invalid prekeys:" + packet);
				}
				try {
					boolean changed = false;
					// Validate IdentityKey
					IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
					if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
						changed = true;
					}

					// Validate signedPreKeyRecord + ID
					SignedPreKeyRecord signedPreKeyRecord;
					int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
					try {
						signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
						if ( flush
								||!bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
								|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
							signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
							axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
							changed = true;
						}
					} catch (InvalidKeyIdException e) {
						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
						signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
						axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
						changed = true;
					}

					// Validate PreKeys
					Set<PreKeyRecord> preKeyRecords = new HashSet<>();
					if (keys != null) {
						for (Integer id : keys.keySet()) {
							try {
								PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
								if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
									preKeyRecords.add(preKeyRecord);
								}
							} catch (InvalidKeyIdException ignored) {
							}
						}
					}
					int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
					if (newKeys > 0) {
						List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
								axolotlStore.getCurrentPreKeyId()+1, newKeys);
						preKeyRecords.addAll(newRecords);
						for (PreKeyRecord record : newRecords) {
							axolotlStore.storePreKey(record.getId(), record);
						}
						changed = true;
						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding " + newKeys + " new preKeys to PEP.");
					}


					if(changed) {
						IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
								signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
								preKeyRecords, getOwnDeviceId());
						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+ ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
						mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
							@Override
							public void onIqPacketReceived(Account account, IqPacket packet) {
								// TODO: implement this!
								Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Published bundle, got: " + packet);
							}
						});
					}
				} catch (InvalidKeyException e) {
						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
						return;
				}
			}
		});
	}

	public boolean isContactAxolotlCapable(Contact contact) {

		Jid jid = contact.getJid().toBareJid();
		AxolotlAddress address = new AxolotlAddress(jid.toString(), 0);
		return sessions.hasAny(address) ||
				( deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
	}
	public SQLiteAxolotlStore.Trust getFingerprintTrust(String fingerprint) {
		return axolotlStore.getFingerprintTrust(fingerprint);
	}

	public void setFingerprintTrust(String fingerprint, SQLiteAxolotlStore.Trust trust) {
		axolotlStore.setFingerprintTrust(fingerprint, trust);
	}

	private void buildSessionFromPEP(final Conversation conversation, final AxolotlAddress address, final boolean flushWaitingQueueAfterFetch) {
		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new sesstion for " + address.getDeviceId());

		try {
			IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
					Jid.fromString(address.getName()), address.getDeviceId());
			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Retrieving bundle: " + bundlesPacket);
			mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
				private void finish() {
					AxolotlAddress ownAddress = new AxolotlAddress(conversation.getAccount().getJid().toBareJid().toString(),0);
					AxolotlAddress foreignAddress = new AxolotlAddress(conversation.getJid().toBareJid().toString(),0);
					if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
							&& !fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING)) {
						if (flushWaitingQueueAfterFetch) {
							conversation.findUnsentMessagesWithEncryption(Message.ENCRYPTION_AXOLOTL,
									new Conversation.OnMessageFound() {
										@Override
										public void onMessageFound(Message message) {
											processSending(message);
										}
									});
						}
						mXmppConnectionService.newKeysAvailable();
					}
				}

				@Override
				public void onIqPacketReceived(Account account, IqPacket packet) {
					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received preKey IQ packet, processing...");
					final IqParser parser = mXmppConnectionService.getIqParser();
					final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
					final PreKeyBundle bundle = parser.bundle(packet);
					if (preKeyBundleList.isEmpty() || bundle == null) {
						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"preKey IQ packet invalid: " + packet);
						fetchStatusMap.put(address, FetchStatus.ERROR);
						finish();
						return;
					}
					Random random = new Random();
					final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
					if (preKey == null) {
						//should never happen
						fetchStatusMap.put(address, FetchStatus.ERROR);
						finish();
						return;
					}

					final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
							preKey.getPreKeyId(), preKey.getPreKey(),
							bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
							bundle.getSignedPreKeySignature(), bundle.getIdentityKey());

					axolotlStore.saveIdentity(address.getName(), bundle.getIdentityKey());

					try {
						SessionBuilder builder = new SessionBuilder(axolotlStore, address);
						builder.process(preKeyBundle);
						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
						sessions.put(address, session);
						fetchStatusMap.put(address, FetchStatus.SUCCESS);
					} catch (UntrustedIdentityException|InvalidKeyException e) {
						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Error building session for " + address + ": "
								+ e.getClass().getName() + ", " + e.getMessage());
						fetchStatusMap.put(address, FetchStatus.ERROR);
					}

					finish();
				}
			});
		} catch (InvalidJidException e) {
			Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Got address with invalid jid: " + address.getName());
		}
	}

	public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + conversation.getContact().getJid().toBareJid());
		Jid contactJid = conversation.getContact().getJid().toBareJid();
		Set<AxolotlAddress> addresses = new HashSet<>();
		if(deviceIds.get(contactJid) != null) {
			for(Integer foreignId:this.deviceIds.get(contactJid)) {
				AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
				if(sessions.get(address) == null) {
					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
					if ( identityKey != null ) {
						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Already have session for " +  address.toString() + ", adding to cache...");
						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
						sessions.put(address, session);
					} else {
						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + foreignId);
						addresses.add(new AxolotlAddress(contactJid.toString(), foreignId));
					}
				}
			}
		} else {
			Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
		}
		if(deviceIds.get(account.getJid().toBareJid()) != null) {
			for(Integer ownId:this.deviceIds.get(account.getJid().toBareJid())) {
				AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
				if(sessions.get(address) == null) {
					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
					if ( identityKey != null ) {
						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Already have session for " +  address.toString() + ", adding to cache...");
						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
						sessions.put(address, session);
					} else {
						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
						addresses.add(new AxolotlAddress(account.getJid().toBareJid().toString(), ownId));
					}
				}
			}
		}

		return addresses;
	}

	public boolean createSessionsIfNeeded(final Conversation conversation, final boolean flushWaitingQueueAfterFetch) {
		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
		boolean newSessions = false;
		Set<AxolotlAddress> addresses = findDevicesWithoutSession(conversation);
		for (AxolotlAddress address : addresses) {
			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
			FetchStatus status = fetchStatusMap.get(address);
			if ( status == null || status == FetchStatus.ERROR ) {
					fetchStatusMap.put(address, FetchStatus.PENDING);
					this.buildSessionFromPEP(conversation, address, flushWaitingQueueAfterFetch);
					newSessions = true;
			} else {
				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Already fetching bundle for " +  address.toString());
			}
		}

		return newSessions;
	}

	public boolean hasPendingKeyFetches(Conversation conversation) {
		AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(),0);
		AxolotlAddress foreignAddress = new AxolotlAddress(conversation.getJid().toBareJid().toString(),0);
		return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
				||fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING);

	}

	@Nullable
	public XmppAxolotlMessage encrypt(Message message ){
		final String content;
		if (message.hasFileOnRemoteHost()) {
				content = message.getFileParams().url.toString();
			} else {
				content = message.getBody();
			}
		final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(message.getContact().getJid().toBareJid(),
				getOwnDeviceId(), content);

		if(findSessionsforContact(message.getContact()).isEmpty()) {
			return null;
		}
		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Building axolotl foreign headers...");
		for (XmppAxolotlSession session : findSessionsforContact(message.getContact())) {
			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account)+session.remoteAddress.toString());
			//if(!session.isTrusted()) {
			// TODO: handle this properly
			//              continue;
			//        }
			axolotlMessage.addHeader(session.processSending(axolotlMessage.getInnerKey()));
		}
		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Building axolotl own headers...");
		for (XmppAxolotlSession session : findOwnSessions()) {
			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account)+session.remoteAddress.toString());
			//        if(!session.isTrusted()) {
			// TODO: handle this properly
			//          continue;
			//    }
			axolotlMessage.addHeader(session.processSending(axolotlMessage.getInnerKey()));
		}

		return axolotlMessage;
	}

	private void processSending(final Message message) {
		executor.execute(new Runnable() {
			@Override
			public void run() {
				MessagePacket packet = mXmppConnectionService.getMessageGenerator()
						.generateAxolotlChat(message);
				if (packet == null) {
					mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
					//mXmppConnectionService.updateConversationUi();
				} else {
					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Generated message, caching: " + message.getUuid());
					messageCache.put(message.getUuid(), packet);
					mXmppConnectionService.resendMessage(message);
				}
			}
		});
	}

	public void prepareMessage(final Message message) {
		if (!messageCache.containsKey(message.getUuid())) {
			boolean newSessions = createSessionsIfNeeded(message.getConversation(), true);
			if (!newSessions) {
				this.processSending(message);
			}
		}
	}

	public MessagePacket fetchPacketFromCache(Message message) {
		MessagePacket packet = messageCache.get(message.getUuid());
		if (packet != null) {
			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Cache hit: " + message.getUuid());
			messageCache.remove(message.getUuid());
		} else {
			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Cache miss: " + message.getUuid());
		}
		return packet;
	}

	public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceiving(XmppAxolotlMessage message) {
		XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
		AxolotlAddress senderAddress = new AxolotlAddress(message.getFrom().toString(),
				message.getSenderDeviceId());

		boolean newSession = false;
		XmppAxolotlSession session = sessions.get(senderAddress);
		if (session == null) {
			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Account: "+account.getJid()+" No axolotl session found while parsing received message " + message);
			// TODO: handle this properly
			IdentityKey identityKey = axolotlStore.loadSession(senderAddress).getSessionState().getRemoteIdentityKey();
			if ( identityKey != null ) {
				session = new XmppAxolotlSession(account, axolotlStore, senderAddress, identityKey.getFingerprint().replaceAll("\\s", ""));
			} else {
				session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
			}
			newSession = true;
		}

		for (XmppAxolotlMessage.XmppAxolotlMessageHeader header : message.getHeaders()) {
			if (header.getRecipientDeviceId() == getOwnDeviceId()) {
				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Found axolotl header matching own device ID, processing...");
				byte[] payloadKey = session.processReceiving(header);
				if (payloadKey != null) {
					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Got payload key from axolotl header. Decrypting message...");
					plaintextMessage = message.decrypt(session, payloadKey, session.getFingerprint());
				}
				Integer preKeyId = session.getPreKeyId();
				if (preKeyId != null) {
					publishBundlesIfNeeded();
					session.resetPreKeyId();
				}
				break;
			}
		}

		if (newSession && plaintextMessage != null) {
			sessions.put(senderAddress, session);
		}

		return plaintextMessage;
	}
}