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