summaryrefslogtreecommitdiffstats
path: root/keystore/java/android/security/KeyStore.java
blob: f49c4294fb9e29bd99794856715aef9c3fff8acf (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.security;

import android.net.LocalSocketAddress;
import android.net.LocalSocket;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UTFDataFormatException;
import java.nio.charset.Charsets;
import java.nio.charset.ModifiedUtf8;
import java.util.ArrayList;

/**
 * @hide This should not be made public in its present form because it
 * assumes that private and secret key bytes are available and would
 * preclude the use of hardware crypto.
 */
public class KeyStore {

    // ResponseCodes
    public static final int NO_ERROR = 1;
    public static final int LOCKED = 2;
    public static final int UNINITIALIZED = 3;
    public static final int SYSTEM_ERROR = 4;
    public static final int PROTOCOL_ERROR = 5;
    public static final int PERMISSION_DENIED = 6;
    public static final int KEY_NOT_FOUND = 7;
    public static final int VALUE_CORRUPTED = 8;
    public static final int UNDEFINED_ACTION = 9;
    public static final int WRONG_PASSWORD = 10;

    // States
    public enum State { UNLOCKED, LOCKED, UNINITIALIZED };

    private static final LocalSocketAddress sAddress = new LocalSocketAddress(
            "keystore", LocalSocketAddress.Namespace.RESERVED);

    private int mError = NO_ERROR;

    private KeyStore() {}

    public static KeyStore getInstance() {
        return new KeyStore();
    }

    public State state() {
        execute('t');
        switch (mError) {
            case NO_ERROR: return State.UNLOCKED;
            case LOCKED: return State.LOCKED;
            case UNINITIALIZED: return State.UNINITIALIZED;
            default: throw new AssertionError(mError);
        }
    }

    private byte[] get(byte[] key) {
        ArrayList<byte[]> values = execute('g', key);
        return (values == null || values.isEmpty()) ? null : values.get(0);
    }

    public byte[] get(String key) {
        return get(getKeyBytes(key));
    }

    private boolean put(byte[] key, byte[] value) {
        execute('i', key, value);
        return mError == NO_ERROR;
    }

    public boolean put(String key, byte[] value) {
        return put(getKeyBytes(key), value);
    }

    private boolean delete(byte[] key) {
        execute('d', key);
        return mError == NO_ERROR;
    }

    public boolean delete(String key) {
        return delete(getKeyBytes(key));
    }

    private boolean contains(byte[] key) {
        execute('e', key);
        return mError == NO_ERROR;
    }

    public boolean contains(String key) {
        return contains(getKeyBytes(key));
    }

    public byte[][] saw(byte[] prefix) {
        ArrayList<byte[]> values = execute('s', prefix);
        return (values == null) ? null : values.toArray(new byte[values.size()][]);
    }

    public String[] saw(String prefix) {
        byte[][] values = saw(getKeyBytes(prefix));
        if (values == null) {
            return null;
        }
        String[] strings = new String[values.length];
        for (int i = 0; i < values.length; ++i) {
            strings[i] = toKeyString(values[i]);
        }
        return strings;
    }

    public boolean reset() {
        execute('r');
        return mError == NO_ERROR;
    }

    private boolean password(byte[] password) {
        execute('p', password);
        return mError == NO_ERROR;
    }

    public boolean password(String password) {
        return password(getPasswordBytes(password));
    }

    public boolean lock() {
        execute('l');
        return mError == NO_ERROR;
    }

    private boolean unlock(byte[] password) {
        execute('u', password);
        return mError == NO_ERROR;
    }

    public boolean unlock(String password) {
        return unlock(getPasswordBytes(password));
    }

    public boolean isEmpty() {
        execute('z');
        return mError == KEY_NOT_FOUND;
    }

    private boolean generate(byte[] key) {
        execute('a', key);
        return mError == NO_ERROR;
    }

    public boolean generate(String key) {
        return generate(getKeyBytes(key));
    }

    private boolean importKey(byte[] keyName, byte[] key) {
        execute('m', keyName, key);
        return mError == NO_ERROR;
    }

    public boolean importKey(String keyName, byte[] key) {
        return importKey(getKeyBytes(keyName), key);
    }

    private byte[] getPubkey(byte[] key) {
        ArrayList<byte[]> values = execute('b', key);
        return (values == null || values.isEmpty()) ? null : values.get(0);
    }

    public byte[] getPubkey(String key) {
        return getPubkey(getKeyBytes(key));
    }

    private boolean delKey(byte[] key) {
        execute('k', key);
        return mError == NO_ERROR;
    }

    public boolean delKey(String key) {
        return delKey(getKeyBytes(key));
    }

    private byte[] sign(byte[] keyName, byte[] data) {
        final ArrayList<byte[]> values = execute('n', keyName, data);
        return (values == null || values.isEmpty()) ? null : values.get(0);
    }

    public byte[] sign(String key, byte[] data) {
        return sign(getKeyBytes(key), data);
    }

    private boolean verify(byte[] keyName, byte[] data, byte[] signature) {
        execute('v', keyName, data, signature);
        return mError == NO_ERROR;
    }

    public boolean verify(String key, byte[] data, byte[] signature) {
        return verify(getKeyBytes(key), data, signature);
    }

    private boolean grant(byte[] key, byte[] uid) {
        execute('x', key, uid);
        return mError == NO_ERROR;
    }

    public boolean grant(String key, int uid) {
         return grant(getKeyBytes(key), getUidBytes(uid));
    }

    private boolean ungrant(byte[] key, byte[] uid) {
        execute('y', key, uid);
        return mError == NO_ERROR;
    }

    public boolean ungrant(String key, int uid) {
        return ungrant(getKeyBytes(key), getUidBytes(uid));
    }

    public int getLastError() {
        return mError;
    }

    private ArrayList<byte[]> execute(int code, byte[]... parameters) {
        mError = PROTOCOL_ERROR;

        for (byte[] parameter : parameters) {
            if (parameter == null || parameter.length > 65535) {
                return null;
            }
        }

        LocalSocket socket = new LocalSocket();
        try {
            socket.connect(sAddress);

            OutputStream out = socket.getOutputStream();
            out.write(code);
            for (byte[] parameter : parameters) {
                out.write(parameter.length >> 8);
                out.write(parameter.length);
                out.write(parameter);
            }
            out.flush();
            socket.shutdownOutput();

            InputStream in = socket.getInputStream();
            if ((code = in.read()) != NO_ERROR) {
                if (code != -1) {
                    mError = code;
                }
                return null;
            }

            ArrayList<byte[]> values = new ArrayList<byte[]>();
            while (true) {
                int i, j;
                if ((i = in.read()) == -1) {
                    break;
                }
                if ((j = in.read()) == -1) {
                    return null;
                }
                byte[] value = new byte[i << 8 | j];
                for (i = 0; i < value.length; i += j) {
                    if ((j = in.read(value, i, value.length - i)) == -1) {
                        return null;
                    }
                }
                values.add(value);
            }
            mError = NO_ERROR;
            return values;
        } catch (IOException e) {
            // ignore
        } finally {
            try {
                socket.close();
            } catch (IOException e) {}
        }
        return null;
    }

    /**
     * ModifiedUtf8 is used for key encoding to match the
     * implementation of NativeCrypto.ENGINE_load_private_key.
     */
    private static byte[] getKeyBytes(String string) {
        try {
            int utfCount = (int) ModifiedUtf8.countBytes(string, false);
            byte[] result = new byte[utfCount];
            ModifiedUtf8.encode(result, 0, string);
            return result;
        } catch (UTFDataFormatException e) {
            throw new RuntimeException(e);
        }
    }

    private static String toKeyString(byte[] bytes) {
        try {
            return ModifiedUtf8.decode(bytes, new char[bytes.length], 0, bytes.length);
        } catch (UTFDataFormatException e) {
            throw new RuntimeException(e);
        }
    }

    private static byte[] getPasswordBytes(String password) {
        return password.getBytes(Charsets.UTF_8);
    }

    private static byte[] getUidBytes(int uid) {
        return Integer.toString(uid).getBytes(Charsets.UTF_8);
    }
}