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 synchronized (message) {
108 Intent params = new Intent();
109 params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
110 if (message.getType() == Message.TYPE_TEXT) {
111 InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
112 final OutputStream os = new ByteArrayOutputStream();
113 Intent result = getOpenPgpApi().executeApi(params, is, os);
114 switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
115 case OpenPgpApi.RESULT_CODE_SUCCESS:
116 try {
117 os.flush();
118 final String body = os.toString();
119 if (body == null) {
120 throw new IOException("body was null");
121 }
122 message.setBody(body);
123 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
124 final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
125 if (message.trusted()
126 && message.treatAsDownloadable() != Message.Decision.NEVER
127 && manager.getAutoAcceptFileSize() > 0) {
128 manager.createNewDownloadConnection(message);
129 }
130 } catch (IOException e) {
131 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
132 }
133 mXmppConnectionService.updateMessage(message);
134 break;
135 case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
136 synchronized (PgpDecryptionService.this) {
137 messages.addFirst(message);
138 currentMessage = null;
139 storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
140 }
141 break;
142 case OpenPgpApi.RESULT_CODE_ERROR:
143 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
144 mXmppConnectionService.updateMessage(message);
145 break;
146 }
147 } else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
148 try {
149 final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
150 final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
151 outputFile.getParentFile().mkdirs();
152 outputFile.createNewFile();
153 InputStream is = new FileInputStream(inputFile);
154 OutputStream os = new FileOutputStream(outputFile);
155 Intent result = getOpenPgpApi().executeApi(params, is, os);
156 switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
157 case OpenPgpApi.RESULT_CODE_SUCCESS:
158 URL url = message.getFileParams().url;
159 mXmppConnectionService.getFileBackend().updateFileParams(message, url);
160 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
161 inputFile.delete();
162 mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile);
163 mXmppConnectionService.updateMessage(message);
164 break;
165 case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
166 synchronized (PgpDecryptionService.this) {
167 messages.addFirst(message);
168 currentMessage = null;
169 storePendingIntent((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
170 }
171 break;
172 case OpenPgpApi.RESULT_CODE_ERROR:
173 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
174 mXmppConnectionService.updateMessage(message);
175 break;
176 }
177 } catch (final IOException e) {
178 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
179 mXmppConnectionService.updateMessage(message);
180 }
181 }
182 }
183 notifyIfPending(message);
184 }
185
186 private synchronized void notifyIfPending(Message message) {
187 if (pendingNotifications.remove(message)) {
188 mXmppConnectionService.getNotificationService().push(message);
189 }
190 }
191
192 private void storePendingIntent(PendingIntent pendingIntent) {
193 this.pendingIntent = pendingIntent;
194 mXmppConnectionService.updateConversationUi();
195 }
196
197 public synchronized boolean hasPendingIntent(Conversation conversation) {
198 if (pendingIntent == null) {
199 return false;
200 } else {
201 for(Message message : messages) {
202 if (message.getConversation() == conversation) {
203 return true;
204 }
205 }
206 return false;
207 }
208 }
209
210 public PendingIntent getPendingIntent() {
211 return pendingIntent;
212 }
213
214 public boolean isConnected() {
215 return getOpenPgpApi() != null;
216 }
217}