aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/thialfihar/android/apg/ApgService.java
blob: ec744448b74f72eed6a2540bbd4cfe04c3988b2b (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
package org.thialfihar.android.apg;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class ApgService extends Service {
    static String TAG = "ApgService";

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "bound");
        return mBinder;
    }

    private enum error {
        ARGUMENTS_MISSING,
        APG_FAILURE
    }

    private final IApgService.Stub mBinder = new IApgService.Stub() {

        public boolean encrypt_with_passphrase(Bundle pArgs, Bundle pReturn) {
            ArrayList<String> errors = new ArrayList<String>();
            ArrayList<String> warnings = new ArrayList<String>();

            pReturn.putStringArrayList("ERRORS", errors);
            pReturn.putStringArrayList("WARNINGS", warnings);

            String msg = pArgs.getString("MSG");
            pArgs.remove("MSG");

            String passphrase = pArgs.getString("SYM_KEY");
            pArgs.remove("SYM_KEY");

            if (msg == null) {
                errors.add("Message to encrypt (MSG) missing");
            }

            if (passphrase == null) {
                errors.add("Symmetric key (SYM_KEY) missing");
            }

            if (!pArgs.isEmpty()) {
                Iterator<String> _iter = pArgs.keySet().iterator();
                while (_iter.hasNext()) {
                    warnings.add("Unknown key: " + _iter.next());
                }
            }

            if (errors.size() != 0) {
                pReturn.putInt("ERROR", error.ARGUMENTS_MISSING.ordinal());
                return false;
            }

            Preferences _mPreferences = Preferences.getPreferences(getBaseContext(), true);
            InputStream _inStream = new ByteArrayInputStream(msg.getBytes());
            InputData _in = new InputData(_inStream, 9999);
            OutputStream _out = new ByteArrayOutputStream();
            long _enc_keys[] = {};

            Apg.initialize(getApplicationContext());
            try {
                Apg.encrypt(getApplicationContext(), // context
                        _in, // input stream
                        _out, // output stream
                        true, // armored
                        _enc_keys, // encryption keys
                        0, // signature key
                        null, // signature passphrase
                        null, // progress
                        _mPreferences.getDefaultEncryptionAlgorithm(), // encryption
                        _mPreferences.getDefaultHashAlgorithm(), // hash
                        Id.choice.compression.none, // compression
                        false, // mPreferences.getForceV3Signatures(),
                        passphrase // passPhrase
                        );
            } catch (Exception e) {
                Log.d(TAG, "Exception in encrypt");
                errors.add("Internal failure in APG when encrypting: " + e.getMessage());

                pReturn.putInt("ERROR", error.APG_FAILURE.ordinal());
                return false;
            }

            Log.d(TAG, "Encrypted");
            pReturn.putString("RESULT", _out.toString());
            return true;
        }

        public boolean decrypt_with_passphrase(Bundle pArgs, Bundle pReturn) {
            ArrayList<String> errors = new ArrayList<String>();
            ArrayList<String> warnings = new ArrayList<String>();

            pReturn.putStringArrayList("ERRORS", errors);
            pReturn.putStringArrayList("WARNINGS", warnings);

            String encrypted_msg = pArgs.getString("MSG");
            pArgs.remove("MSG");

            String passphrase = pArgs.getString("SYM_KEY");
            pArgs.remove("SYM_KEY");

            if (encrypted_msg == null) {
                errors.add("Message to decrypt (MSG) missing");
            }

            if (passphrase == null) {
                errors.add("Symmetric key (SYM_KEY) missing");
            }

            if (!pArgs.isEmpty()) {
                Iterator<String> iter = pArgs.keySet().iterator();
                while (iter.hasNext()) {
                    warnings.add("Unknown key: " + iter.next());
                }
            }

            if (errors.size() != 0) {
                pReturn.putStringArrayList("ERROR", errors);
                pReturn.putInt("ERROR", error.ARGUMENTS_MISSING.ordinal());
                return false;
            }

            InputStream inStream = new ByteArrayInputStream(encrypted_msg.getBytes());
            InputData in = new InputData(inStream, 9999); // XXX what size in
            // second parameter?
            OutputStream out = new ByteArrayOutputStream();
            try {
                Apg.decrypt(getApplicationContext(), in, out, passphrase, null, // progress
                        true // symmetric
                        );
            } catch (Exception e) {
                Log.d(TAG, "Exception in decrypt");
                errors.add("Internal failure in APG when decrypting: " + e.getMessage());

                pReturn.putInt("ERROR", error.APG_FAILURE.ordinal());
                pReturn.putStringArrayList("ERROR", errors);
                return false;
            }

            pReturn.putString("RESULT", out.toString());
            return true;
        }
    };
}