aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/InputDataOperation.java
blob: 030c0a285475400e4480195c7f0e9cb0e7f36457 (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
/*
 * Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.operations;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;

import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.codec.DecodeMonitor;
import org.apache.james.mime4j.dom.FieldParser;
import org.apache.james.mime4j.dom.field.ContentDispositionField;
import org.apache.james.mime4j.field.DefaultFieldParser;
import org.apache.james.mime4j.parser.AbstractContentHandler;
import org.apache.james.mime4j.parser.MimeStreamParser;
import org.apache.james.mime4j.stream.BodyDescriptor;
import org.apache.james.mime4j.stream.Field;
import org.apache.james.mime4j.stream.MimeConfig;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.operations.results.InputDataResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyOperation;
import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider;
import org.sufficientlysecure.keychain.service.InputDataParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.util.Log;


/** This operation deals with input data, trying to determine its type as it goes. */
public class InputDataOperation extends BaseOperation<InputDataParcel> {

    final private byte[] buf = new byte[256];

    public InputDataOperation(Context context, ProviderHelper providerHelper, Progressable progressable) {
        super(context, providerHelper, progressable);
    }

    @NonNull
    @Override
    public InputDataResult execute(InputDataParcel input,
                                     CryptoInputParcel cryptoInput) {

        final OperationLog log = new OperationLog();

        log.add(LogType.MSG_MIME_PARSING, 0);

        Uri currentUri;

        PgpDecryptVerifyInputParcel decryptInput = input.getDecryptInput();
        if (decryptInput != null) {

            PgpDecryptVerifyOperation op =
                    new PgpDecryptVerifyOperation(mContext, mProviderHelper, mProgressable);

            decryptInput.setInputUri(input.getInputUri());

            currentUri = TemporaryStorageProvider.createFile(mContext);
            decryptInput.setOutputUri(currentUri);

            DecryptVerifyResult result = op.execute(decryptInput, cryptoInput);
            if (result.isPending()) {
                return new InputDataResult(log, result);
            }

        } else {
            currentUri = input.getInputUri();
        }

        // If we aren't supposed to attempt mime decode, we are done here
        if (!input.getMimeDecode()) {

            ArrayList<Uri> uris = new ArrayList<>();
            uris.add(currentUri);
            return new InputDataResult(InputDataResult.RESULT_OK, log, uris);

        }

        try {
            InputStream in = mContext.getContentResolver().openInputStream(currentUri);

            MimeStreamParser parser = new MimeStreamParser((MimeConfig) null);

            final ArrayList<Uri> outputUris = new ArrayList<>();

            parser.setContentDecoding(true);
            parser.setRecurse();
            parser.setContentHandler(new AbstractContentHandler() {
                String mFilename;

                @Override
                public void startHeader() throws MimeException {
                    mFilename = null;
                }

                @Override
                public void field(Field field) throws MimeException {
                    field = DefaultFieldParser.getParser().parse(field, DecodeMonitor.SILENT);
                    if (field instanceof ContentDispositionField) {
                        mFilename = ((ContentDispositionField) field).getFilename();
                    }
                }

                @Override
                public void body(BodyDescriptor bd, InputStream is) throws MimeException, IOException {

                    // log.add(LogType.MSG_MIME_PART, 0, bd.getMimeType());

                    Uri uri = TemporaryStorageProvider.createFile(mContext, mFilename, bd.getMimeType());
                    OutputStream out = mContext.getContentResolver().openOutputStream(uri, "w");

                    if (out == null) {
                        Log.e(Constants.TAG, "error!");
                        return;
                    }

                    int len;
                    while ( (len = is.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }

                    out.close();
                    outputUris.add(uri);

                }
            });

            parser.parse(in);

            log.add(LogType.MSG_MIME_PARSING_SUCCESS, 1);

            return new InputDataResult(InputDataResult.RESULT_OK, log, outputUris);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return new InputDataResult(InputDataResult.RESULT_ERROR, log, null);
        } catch (MimeException e) {
            e.printStackTrace();
            return new InputDataResult(InputDataResult.RESULT_ERROR, log, null);
        } catch (IOException e) {
            e.printStackTrace();
            return new InputDataResult(InputDataResult.RESULT_ERROR, log, null);
        }

    }

}