Skip to content

Commit d2237df

Browse files
bdcgoogleandroid code review
authored andcommitted
Merge "Change KeyStore to use Modified UTF-8 to match NativeCrypto"
2 parents 4faeef3 + 2a5b147 commit d2237df

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

keystore/java/android/security/KeyStore.java

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import java.io.InputStream;
2323
import java.io.IOException;
2424
import java.io.OutputStream;
25-
import java.io.UnsupportedEncodingException;
25+
import java.io.UTFDataFormatException;
2626
import java.nio.charset.Charsets;
27+
import java.nio.charset.ModifiedUtf8;
2728
import java.util.ArrayList;
2829

2930
/**
@@ -75,7 +76,7 @@ private byte[] get(byte[] key) {
7576
}
7677

7778
public byte[] get(String key) {
78-
return get(getBytes(key));
79+
return get(getKeyBytes(key));
7980
}
8081

8182
private boolean put(byte[] key, byte[] value) {
@@ -84,7 +85,7 @@ private boolean put(byte[] key, byte[] value) {
8485
}
8586

8687
public boolean put(String key, byte[] value) {
87-
return put(getBytes(key), value);
88+
return put(getKeyBytes(key), value);
8889
}
8990

9091
private boolean delete(byte[] key) {
@@ -93,7 +94,7 @@ private boolean delete(byte[] key) {
9394
}
9495

9596
public boolean delete(String key) {
96-
return delete(getBytes(key));
97+
return delete(getKeyBytes(key));
9798
}
9899

99100
private boolean contains(byte[] key) {
@@ -102,7 +103,7 @@ private boolean contains(byte[] key) {
102103
}
103104

104105
public boolean contains(String key) {
105-
return contains(getBytes(key));
106+
return contains(getKeyBytes(key));
106107
}
107108

108109
public byte[][] saw(byte[] prefix) {
@@ -111,13 +112,13 @@ public byte[][] saw(byte[] prefix) {
111112
}
112113

113114
public String[] saw(String prefix) {
114-
byte[][] values = saw(getBytes(prefix));
115+
byte[][] values = saw(getKeyBytes(prefix));
115116
if (values == null) {
116117
return null;
117118
}
118119
String[] strings = new String[values.length];
119120
for (int i = 0; i < values.length; ++i) {
120-
strings[i] = toString(values[i]);
121+
strings[i] = toKeyString(values[i]);
121122
}
122123
return strings;
123124
}
@@ -133,7 +134,7 @@ private boolean password(byte[] password) {
133134
}
134135

135136
public boolean password(String password) {
136-
return password(getBytes(password));
137+
return password(getPasswordBytes(password));
137138
}
138139

139140
public boolean lock() {
@@ -147,7 +148,7 @@ private boolean unlock(byte[] password) {
147148
}
148149

149150
public boolean unlock(String password) {
150-
return unlock(getBytes(password));
151+
return unlock(getPasswordBytes(password));
151152
}
152153

153154
public boolean isEmpty() {
@@ -161,7 +162,7 @@ private boolean generate(byte[] key) {
161162
}
162163

163164
public boolean generate(String key) {
164-
return generate(getBytes(key));
165+
return generate(getKeyBytes(key));
165166
}
166167

167168
private boolean importKey(byte[] keyName, byte[] key) {
@@ -170,7 +171,7 @@ private boolean importKey(byte[] keyName, byte[] key) {
170171
}
171172

172173
public boolean importKey(String keyName, byte[] key) {
173-
return importKey(getBytes(keyName), key);
174+
return importKey(getKeyBytes(keyName), key);
174175
}
175176

176177
private byte[] getPubkey(byte[] key) {
@@ -179,7 +180,7 @@ private byte[] getPubkey(byte[] key) {
179180
}
180181

181182
public byte[] getPubkey(String key) {
182-
return getPubkey(getBytes(key));
183+
return getPubkey(getKeyBytes(key));
183184
}
184185

185186
private boolean delKey(byte[] key) {
@@ -188,7 +189,7 @@ private boolean delKey(byte[] key) {
188189
}
189190

190191
public boolean delKey(String key) {
191-
return delKey(getBytes(key));
192+
return delKey(getKeyBytes(key));
192193
}
193194

194195
private byte[] sign(byte[] keyName, byte[] data) {
@@ -197,7 +198,7 @@ private byte[] sign(byte[] keyName, byte[] data) {
197198
}
198199

199200
public byte[] sign(String key, byte[] data) {
200-
return sign(getBytes(key), data);
201+
return sign(getKeyBytes(key), data);
201202
}
202203

203204
private boolean verify(byte[] keyName, byte[] data, byte[] signature) {
@@ -206,7 +207,7 @@ private boolean verify(byte[] keyName, byte[] data, byte[] signature) {
206207
}
207208

208209
public boolean verify(String key, byte[] data, byte[] signature) {
209-
return verify(getBytes(key), data, signature);
210+
return verify(getKeyBytes(key), data, signature);
210211
}
211212

212213
private boolean grant(byte[] key, byte[] uid) {
@@ -215,7 +216,7 @@ private boolean grant(byte[] key, byte[] uid) {
215216
}
216217

217218
public boolean grant(String key, int uid) {
218-
return grant(getBytes(key), Integer.toString(uid).getBytes());
219+
return grant(getKeyBytes(key), getUidBytes(uid));
219220
}
220221

221222
private boolean ungrant(byte[] key, byte[] uid) {
@@ -224,7 +225,7 @@ private boolean ungrant(byte[] key, byte[] uid) {
224225
}
225226

226227
public boolean ungrant(String key, int uid) {
227-
return ungrant(getBytes(key), Integer.toString(uid).getBytes());
228+
return ungrant(getKeyBytes(key), getUidBytes(uid));
228229
}
229230

230231
public int getLastError() {
@@ -291,11 +292,34 @@ private ArrayList<byte[]> execute(int code, byte[]... parameters) {
291292
return null;
292293
}
293294

294-
private static byte[] getBytes(String string) {
295-
return string.getBytes(Charsets.UTF_8);
295+
/**
296+
* ModifiedUtf8 is used for key encoding to match the
297+
* implementation of NativeCrypto.ENGINE_load_private_key.
298+
*/
299+
private static byte[] getKeyBytes(String string) {
300+
try {
301+
int utfCount = (int) ModifiedUtf8.countBytes(string, false);
302+
byte[] result = new byte[utfCount];
303+
ModifiedUtf8.encode(result, 0, string);
304+
return result;
305+
} catch (UTFDataFormatException e) {
306+
throw new RuntimeException(e);
307+
}
308+
}
309+
310+
private static String toKeyString(byte[] bytes) {
311+
try {
312+
return ModifiedUtf8.decode(bytes, new char[bytes.length], 0, bytes.length);
313+
} catch (UTFDataFormatException e) {
314+
throw new RuntimeException(e);
315+
}
316+
}
317+
318+
private static byte[] getPasswordBytes(String password) {
319+
return password.getBytes(Charsets.UTF_8);
296320
}
297321

298-
private static String toString(byte[] bytes) {
299-
return new String(bytes, Charsets.UTF_8);
322+
private static byte[] getUidBytes(int uid) {
323+
return Integer.toString(uid).getBytes(Charsets.UTF_8);
300324
}
301325
}

keystore/tests/src/android/security/KeyStoreTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class KeyStoreTest extends ActivityUnitTestCase<Activity> {
3737
private static final String TEST_PASSWD2 = "87654321";
3838
private static final String TEST_KEYNAME = "test-key";
3939
private static final String TEST_KEYNAME1 = "test-key.1";
40-
private static final String TEST_KEYNAME2 = "test-key.2";
40+
private static final String TEST_KEYNAME2 = "test-key\02";
4141
private static final byte[] TEST_KEYVALUE = "test value".getBytes(Charsets.UTF_8);
4242

4343
// "Hello, World" in Chinese

0 commit comments

Comments
 (0)