1package eu.siacs.conversations.crypto;
2
3import android.app.PendingIntent;
4import android.content.Intent;
5
6import org.openintents.openpgp.util.OpenPgpApi;
7
8import java.io.ByteArrayInputStream;
9import java.io.ByteArrayOutputStream;
10import java.io.FileInputStream;
11import java.io.FileOutputStream;
12import java.io.IOException;
13import java.io.InputStream;
14import java.io.OutputStream;
15import java.net.URL;
16import java.util.ArrayDeque;
17import java.util.HashSet;
18import java.util.List;
19
20import eu.siacs.conversations.entities.Conversation;
21import eu.siacs.conversations.entities.DownloadableFile;
22import eu.siacs.conversations.entities.Message;
23import eu.siacs.conversations.http.HttpConnectionManager;
24import eu.siacs.conversations.services.XmppConnectionService;
25
26public class PgpDecryptionService {
27
28 private final XmppConnectionService mXmppConnectionService;
29 private OpenPgpApi openPgpApi = null;
30
31 protected final ArrayDeque<Message> messages = new ArrayDeque();
32 protected final HashSet<Message> pendingNotifications = new HashSet<>();
33 Message currentMessage;
34 private PendingIntent pendingIntent;
35
36
37 public PgpDecryptionService(XmppConnectionService service) {
38 this.mXmppConnectionService = service;
39 }
40
41 public synchronized boolean decrypt(final Message message, boolean notify) {
42 messages.add(message);
43 if (notify && pendingIntent == null) {
44 pendingNotifications.add(message);
45 continueDecryption();
46 return false;
47 } else {
48 continueDecryption();
49 return notify;
50 }
51 }
52
53 public synchronized void decrypt(final List<Message> list) {
54 for(Message message : list) {
55 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
56 messages.add(message);
57 }
58 }
59 continueDecryption();
60 }
61
62 public synchronized void discard(List<Message> discards) {
63 this.messages.removeAll(discards);
64 this.pendingNotifications.removeAll(discards);
65 }
66
67 public synchronized void discard(Message message) {
68 this.messages.remove(message);
69 this.pendingNotifications.remove(message);
70 }
71
72 protected synchronized void decryptNext() {
73 if (pendingIntent == null
74 && getOpenPgpApi() != null
75 && (currentMessage = messages.poll()) != null) {
76 new Thread(new Runnable() {
77 @Override
78 public void run() {
79 executeApi(currentMessage);
80 decryptNext();
81 }
82 }).start();
83 }
84 }
85
86 public synchronized void continueDecryption(boolean resetPending) {
87 if (resetPending) {
88 this.pendingIntent = null;
89 }
90 continueDecryption();
91 }
92
93 public synchronized void continueDecryption() {
94 if (currentMessage == null) {
95 decryptNext();
96 }
97 }
98
99 private synchronized OpenPgpApi getOpenPgpApi() {
100 if (openPgpApi == null) {
101 this.openPgpApi = mXmppConnectionService.getOpenPgpApi();
102 }
103 return this.openPgpApi;
104 }
105
106 private void executeApi(Message message) {
107 Intent params = new Intent();
108 params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
109 if (message.getType() == Message.TYPE_TEXT) {
110 InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
111 final OutputStream os = new ByteArrayOutputStream();
112 Intent result = getOpenPgpApi().executeApi(params, is, os);
113 switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
114 case OpenPgpApi.RESULT_CODE_SUCCESS:
115 try {
116 os.flush();
117 message.setBody(os.toString());
118 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
119 final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
120 if (message.trusted()
121 && message.treatAsDownloadable() != Message.Decision.NEVER
122 && manager.getAutoAcceptFileSize() > 0) {
123 manager.createNewDownloadConnection(message);
124 }
125 } catch (IOException e) {
126 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
127 }
128 mXmppConnectionService.updateMessage(message);
129 break;
130 case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
131 synchronized (PgpDecryptionService.this) {
132 messages.addFirst(message);
133 currentMessage = null;
134 storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
135 }
136 break;
137 case OpenPgpApi.RESULT_CODE_ERROR:
138 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
139 mXmppConnectionService.updateMessage(message);
140 break;
141 }
142 } else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
143 try {
144 final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
145 final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
146 outputFile.getParentFile().mkdirs();
147 outputFile.createNewFile();
148 InputStream is = new FileInputStream(inputFile);
149 OutputStream os = new FileOutputStream(outputFile);
150 Intent result = getOpenPgpApi().executeApi(params, is, os);
151 switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
152 case OpenPgpApi.RESULT_CODE_SUCCESS:
153 URL url = message.getFileParams().url;
154 mXmppConnectionService.getFileBackend().updateFileParams(message,url);
155 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
156 inputFile.delete();
157 mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile);
158 mXmppConnectionService.updateMessage(message);
159 break;
160 case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
161 synchronized (PgpDecryptionService.this) {
162 messages.addFirst(message);
163 currentMessage = null;
164 storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
165 }
166 break;
167 case OpenPgpApi.RESULT_CODE_ERROR:
168 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
169 mXmppConnectionService.updateMessage(message);
170 break;
171 }
172 } catch (final IOException e) {
173 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
174 mXmppConnectionService.updateMessage(message);
175 }
176 }
177 notifyIfPending(message);
178 }
179
180 private synchronized void notifyIfPending(Message message) {
181 if (pendingNotifications.remove(message)) {
182 mXmppConnectionService.getNotificationService().push(message);
183 }
184 }
185
186 private void storePendingIntent(PendingIntent pendingIntent) {
187 this.pendingIntent = pendingIntent;
188 mXmppConnectionService.updateConversationUi();
189 }
190
191 public synchronized boolean hasPendingIntent(Conversation conversation) {
192 if (pendingIntent == null) {
193 return false;
194 } else {
195 for(Message message : messages) {
196 if (message.getConversation() == conversation) {
197 return true;
198 }
199 }
200 return false;
201 }
202 }
203
204 public PendingIntent getPendingIntent() {
205 return pendingIntent;
206 }
207
208 public boolean isConnected() {
209 return getOpenPgpApi() != null;
210 }
211}