2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.security;
19 import android.net.LocalSocketAddress;
20 import android.net.LocalSocket;
22 import java.io.InputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
31 public class KeyStore {
32 public static final int NO_ERROR = 1;
33 public static final int LOCKED = 2;
34 public static final int UNINITIALIZED = 3;
35 public static final int SYSTEM_ERROR = 4;
36 public static final int PROTOCOL_ERROR = 5;
37 public static final int PERMISSION_DENIED = 6;
38 public static final int KEY_NOT_FOUND = 7;
39 public static final int VALUE_CORRUPTED = 8;
40 public static final int UNDEFINED_ACTION = 9;
41 public static final int WRONG_PASSWORD = 10;
43 private static final LocalSocketAddress sAddress = new LocalSocketAddress(
44 "keystore", LocalSocketAddress.Namespace.RESERVED);
46 private int mError = NO_ERROR;
50 public static KeyStore getInstance() {
51 return new KeyStore();
59 public byte[] get(byte[] key) {
60 ArrayList<byte[]> values = execute('g', key);
61 return (values == null || values.isEmpty()) ? null : values.get(0);
64 public String get(String key) {
65 byte[] value = get(getBytes(key));
66 return (value == null) ? null : toString(value);
69 public boolean put(byte[] key, byte[] value) {
70 execute('i', key, value);
71 return mError == NO_ERROR;
74 public boolean put(String key, String value) {
75 return put(getBytes(key), getBytes(value));
78 public boolean delete(byte[] key) {
80 return mError == NO_ERROR;
83 public boolean delete(String key) {
84 return delete(getBytes(key));
87 public boolean contains(byte[] key) {
89 return mError == NO_ERROR;
92 public boolean contains(String key) {
93 return contains(getBytes(key));
96 public byte[][] saw(byte[] prefix) {
97 ArrayList<byte[]> values = execute('s', prefix);
98 return (values == null) ? null : values.toArray(new byte[values.size()][]);
101 public String[] saw(String prefix) {
102 byte[][] values = saw(getBytes(prefix));
103 if (values == null) {
106 String[] strings = new String[values.length];
107 for (int i = 0; i < values.length; ++i) {
108 strings[i] = toString(values[i]);
113 public boolean reset() {
115 return mError == NO_ERROR;
118 public boolean password(byte[] oldPassword, byte[] newPassword) {
119 execute('p', oldPassword, newPassword);
120 return mError == NO_ERROR;
123 public boolean password(String oldPassword, String newPassword) {
124 return password(getBytes(oldPassword), getBytes(newPassword));
127 public boolean password(byte[] password) {
128 return password(password, password);
131 public boolean password(String password) {
132 return password(getBytes(password));
135 public boolean lock() {
137 return mError == NO_ERROR;
140 public boolean unlock(byte[] password) {
141 execute('u', password);
142 return mError == NO_ERROR;
145 public boolean unlock(String password) {
146 return unlock(getBytes(password));
149 public int getLastError() {
153 private ArrayList<byte[]> execute(int code, byte[]... parameters) {
154 mError = PROTOCOL_ERROR;
156 for (byte[] parameter : parameters) {
157 if (parameter == null || parameter.length > 65535) {
162 LocalSocket socket = new LocalSocket();
164 socket.connect(sAddress);
166 OutputStream out = socket.getOutputStream();
168 for (byte[] parameter : parameters) {
169 out.write(parameter.length >> 8);
170 out.write(parameter.length);
171 out.write(parameter);
174 socket.shutdownOutput();
176 InputStream in = socket.getInputStream();
177 if ((code = in.read()) != NO_ERROR) {
184 ArrayList<byte[]> values = new ArrayList<byte[]>();
187 if ((i = in.read()) == -1) {
190 if ((j = in.read()) == -1) {
193 byte[] value = new byte[i << 8 | j];
194 for (i = 0; i < value.length; i += j) {
195 if ((j = in.read(value, i, value.length - i)) == -1) {
203 } catch (IOException e) {
208 } catch (IOException e) {}
213 private static byte[] getBytes(String string) {
215 return string.getBytes("UTF-8");
216 } catch (UnsupportedEncodingException e) {
218 throw new RuntimeException(e);
222 private static String toString(byte[] bytes) {
224 return new String(bytes, "UTF-8");
225 } catch (UnsupportedEncodingException e) {
227 throw new RuntimeException(e);