blob: 4614b530e99590ce859031e963ad14e1edaa15eb [file] [log] [blame]
Chia-chi Yeh44039172009-09-21 11:53:59 +08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package android.security;
18
19import android.net.LocalSocketAddress;
20import android.net.LocalSocket;
21
22import java.io.InputStream;
23import java.io.IOException;
24import java.io.OutputStream;
Nick Kralevich34c47c82010-03-09 13:28:14 -080025import java.io.UnsupportedEncodingException;
Chia-chi Yeh44039172009-09-21 11:53:59 +080026import java.util.ArrayList;
27
28/**
29 * {@hide}
30 */
31public class KeyStore {
Chia-chi Yehd12feb92010-02-06 10:43:22 +080032 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;
Chia-chi Yeh44039172009-09-21 11:53:59 +080042
43 private static final LocalSocketAddress sAddress = new LocalSocketAddress(
44 "keystore", LocalSocketAddress.Namespace.RESERVED);
45
46 private int mError = NO_ERROR;
47
48 private KeyStore() {}
49
50 public static KeyStore getInstance() {
51 return new KeyStore();
52 }
53
54 public int test() {
55 execute('t');
56 return mError;
57 }
58
59 public byte[] get(byte[] key) {
Chia-chi Yehd12feb92010-02-06 10:43:22 +080060 ArrayList<byte[]> values = execute('g', key);
Nick Kralevich34c47c82010-03-09 13:28:14 -080061 return (values == null || values.isEmpty()) ? null : values.get(0);
Chia-chi Yeh44039172009-09-21 11:53:59 +080062 }
63
64 public String get(String key) {
Nick Kralevich34c47c82010-03-09 13:28:14 -080065 byte[] value = get(getBytes(key));
66 return (value == null) ? null : toString(value);
Chia-chi Yeh44039172009-09-21 11:53:59 +080067 }
68
69 public boolean put(byte[] key, byte[] value) {
70 execute('i', key, value);
71 return mError == NO_ERROR;
72 }
73
74 public boolean put(String key, String value) {
Nick Kralevich34c47c82010-03-09 13:28:14 -080075 return put(getBytes(key), getBytes(value));
Chia-chi Yeh44039172009-09-21 11:53:59 +080076 }
77
78 public boolean delete(byte[] key) {
79 execute('d', key);
80 return mError == NO_ERROR;
81 }
82
83 public boolean delete(String key) {
Nick Kralevich34c47c82010-03-09 13:28:14 -080084 return delete(getBytes(key));
Chia-chi Yeh44039172009-09-21 11:53:59 +080085 }
86
87 public boolean contains(byte[] key) {
88 execute('e', key);
89 return mError == NO_ERROR;
90 }
91
92 public boolean contains(String key) {
Nick Kralevich34c47c82010-03-09 13:28:14 -080093 return contains(getBytes(key));
Chia-chi Yeh44039172009-09-21 11:53:59 +080094 }
95
Chia-chi Yeh613fcc82009-09-22 03:04:46 +080096 public byte[][] saw(byte[] prefix) {
Chia-chi Yehd12feb92010-02-06 10:43:22 +080097 ArrayList<byte[]> values = execute('s', prefix);
98 return (values == null) ? null : values.toArray(new byte[values.size()][]);
Chia-chi Yeh44039172009-09-21 11:53:59 +080099 }
100
Chia-chi Yeh613fcc82009-09-22 03:04:46 +0800101 public String[] saw(String prefix) {
Nick Kralevich34c47c82010-03-09 13:28:14 -0800102 byte[][] values = saw(getBytes(prefix));
Chia-chi Yeh44039172009-09-21 11:53:59 +0800103 if (values == null) {
104 return null;
105 }
106 String[] strings = new String[values.length];
107 for (int i = 0; i < values.length; ++i) {
Nick Kralevich34c47c82010-03-09 13:28:14 -0800108 strings[i] = toString(values[i]);
Chia-chi Yeh44039172009-09-21 11:53:59 +0800109 }
110 return strings;
111 }
112
113 public boolean reset() {
114 execute('r');
115 return mError == NO_ERROR;
116 }
117
118 public boolean password(byte[] oldPassword, byte[] newPassword) {
119 execute('p', oldPassword, newPassword);
120 return mError == NO_ERROR;
121 }
122
123 public boolean password(String oldPassword, String newPassword) {
Nick Kralevich34c47c82010-03-09 13:28:14 -0800124 return password(getBytes(oldPassword), getBytes(newPassword));
Chia-chi Yeh44039172009-09-21 11:53:59 +0800125 }
126
127 public boolean password(byte[] password) {
128 return password(password, password);
129 }
130
131 public boolean password(String password) {
Nick Kralevich34c47c82010-03-09 13:28:14 -0800132 return password(getBytes(password));
Chia-chi Yeh44039172009-09-21 11:53:59 +0800133 }
134
135 public boolean lock() {
136 execute('l');
137 return mError == NO_ERROR;
138 }
139
140 public boolean unlock(byte[] password) {
141 execute('u', password);
142 return mError == NO_ERROR;
143 }
144
145 public boolean unlock(String password) {
Nick Kralevich34c47c82010-03-09 13:28:14 -0800146 return unlock(getBytes(password));
Chia-chi Yeh44039172009-09-21 11:53:59 +0800147 }
148
149 public int getLastError() {
150 return mError;
151 }
152
Chia-chi Yehd12feb92010-02-06 10:43:22 +0800153 private ArrayList<byte[]> execute(int code, byte[]... parameters) {
Chia-chi Yeh44039172009-09-21 11:53:59 +0800154 mError = PROTOCOL_ERROR;
155
156 for (byte[] parameter : parameters) {
157 if (parameter == null || parameter.length > 65535) {
158 return null;
159 }
160 }
161
162 LocalSocket socket = new LocalSocket();
163 try {
164 socket.connect(sAddress);
165
166 OutputStream out = socket.getOutputStream();
167 out.write(code);
168 for (byte[] parameter : parameters) {
169 out.write(parameter.length >> 8);
170 out.write(parameter.length);
171 out.write(parameter);
172 }
173 out.flush();
174 socket.shutdownOutput();
175
176 InputStream in = socket.getInputStream();
Chia-chi Yehf1ece5d2009-09-24 13:29:58 +0800177 if ((code = in.read()) != NO_ERROR) {
178 if (code != -1) {
179 mError = code;
180 }
Chia-chi Yeh44039172009-09-21 11:53:59 +0800181 return null;
182 }
183
Chia-chi Yehd12feb92010-02-06 10:43:22 +0800184 ArrayList<byte[]> values = new ArrayList<byte[]>();
Chia-chi Yeh44039172009-09-21 11:53:59 +0800185 while (true) {
186 int i, j;
187 if ((i = in.read()) == -1) {
188 break;
189 }
190 if ((j = in.read()) == -1) {
191 return null;
192 }
Chia-chi Yehd12feb92010-02-06 10:43:22 +0800193 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) {
Chia-chi Yeh44039172009-09-21 11:53:59 +0800196 return null;
197 }
198 }
Chia-chi Yehd12feb92010-02-06 10:43:22 +0800199 values.add(value);
Chia-chi Yeh44039172009-09-21 11:53:59 +0800200 }
Chia-chi Yehf1ece5d2009-09-24 13:29:58 +0800201 mError = NO_ERROR;
Chia-chi Yehd12feb92010-02-06 10:43:22 +0800202 return values;
Chia-chi Yeh44039172009-09-21 11:53:59 +0800203 } catch (IOException e) {
204 // ignore
205 } finally {
206 try {
207 socket.close();
208 } catch (IOException e) {}
209 }
210 return null;
211 }
Nick Kralevich34c47c82010-03-09 13:28:14 -0800212
213 private static byte[] getBytes(String string) {
214 try {
215 return string.getBytes("UTF-8");
216 } catch (UnsupportedEncodingException e) {
217 // will never happen
218 throw new RuntimeException(e);
219 }
220 }
221
222 private static String toString(byte[] bytes) {
223 try {
224 return new String(bytes, "UTF-8");
225 } catch (UnsupportedEncodingException e) {
226 // will never happen
227 throw new RuntimeException(e);
228 }
229 }
Chia-chi Yeh44039172009-09-21 11:53:59 +0800230}