M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions
+190
View File
@@ -0,0 +1,190 @@
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.
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.
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
@@ -0,0 +1,118 @@
/*
* 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.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.security.KeyPair;
/**
* {@hide}
*/
public class Credentials {
private static final String LOGTAG = "Credentials";
public static final String UNLOCK_ACTION = "android.credentials.UNLOCK";
public static final String INSTALL_ACTION = "android.credentials.INSTALL";
public static final String SYSTEM_INSTALL_ACTION = "android.credentials.SYSTEM_INSTALL";
/** Key prefix for CA certificates. */
public static final String CA_CERTIFICATE = "CACERT_";
/** Key prefix for user certificates. */
public static final String USER_CERTIFICATE = "USRCERT_";
/** Key prefix for user private keys. */
public static final String USER_PRIVATE_KEY = "USRPKEY_";
/** Key prefix for VPN. */
public static final String VPN = "VPN_";
/** Key prefix for WIFI. */
public static final String WIFI = "WIFI_";
/** Data type for public keys. */
public static final String PUBLIC_KEY = "KEY";
/** Data type for private keys. */
public static final String PRIVATE_KEY = "PKEY";
/** Data type for certificates. */
public static final String CERTIFICATE = "CERT";
/** Data type for PKCS12. */
public static final String PKCS12 = "PKCS12";
private static Credentials singleton;
public static Credentials getInstance() {
if (singleton == null) {
singleton = new Credentials();
}
return singleton;
}
public void unlock(Context context) {
try {
Intent intent = new Intent(UNLOCK_ACTION);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(LOGTAG, e.toString());
}
}
private Intent createInstallIntent() {
Intent intent = new Intent(INSTALL_ACTION);
intent.setClassName("com.android.certinstaller",
"com.android.certinstaller.CertInstallerMain");
return intent;
}
public void install(Context context, KeyPair pair) {
try {
Intent intent = createInstallIntent();
intent.putExtra(PRIVATE_KEY, pair.getPrivate().getEncoded());
intent.putExtra(PUBLIC_KEY, pair.getPublic().getEncoded());
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(LOGTAG, e.toString());
}
}
public void install(Context context, String type, byte[] value) {
try {
Intent intent = createInstallIntent();
intent.putExtra(type, value);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(LOGTAG, e.toString());
}
}
public void installFromSdCard(Context context) {
try {
context.startActivity(createInstallIntent());
} catch (ActivityNotFoundException e) {
Log.w(LOGTAG, e.toString());
}
}
}
@@ -0,0 +1,230 @@
/*
* 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.UnsupportedEncodingException;
import java.util.ArrayList;
/**
* {@hide}
*/
public class KeyStore {
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;
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 int test() {
execute('t');
return mError;
}
public byte[] get(byte[] key) {
ArrayList<byte[]> values = execute('g', key);
return (values == null || values.isEmpty()) ? null : values.get(0);
}
public String get(String key) {
byte[] value = get(getBytes(key));
return (value == null) ? null : toString(value);
}
public boolean put(byte[] key, byte[] value) {
execute('i', key, value);
return mError == NO_ERROR;
}
public boolean put(String key, String value) {
return put(getBytes(key), getBytes(value));
}
public boolean delete(byte[] key) {
execute('d', key);
return mError == NO_ERROR;
}
public boolean delete(String key) {
return delete(getBytes(key));
}
public boolean contains(byte[] key) {
execute('e', key);
return mError == NO_ERROR;
}
public boolean contains(String key) {
return contains(getBytes(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(getBytes(prefix));
if (values == null) {
return null;
}
String[] strings = new String[values.length];
for (int i = 0; i < values.length; ++i) {
strings[i] = toString(values[i]);
}
return strings;
}
public boolean reset() {
execute('r');
return mError == NO_ERROR;
}
public boolean password(byte[] oldPassword, byte[] newPassword) {
execute('p', oldPassword, newPassword);
return mError == NO_ERROR;
}
public boolean password(String oldPassword, String newPassword) {
return password(getBytes(oldPassword), getBytes(newPassword));
}
public boolean password(byte[] password) {
return password(password, password);
}
public boolean password(String password) {
return password(getBytes(password));
}
public boolean lock() {
execute('l');
return mError == NO_ERROR;
}
public boolean unlock(byte[] password) {
execute('u', password);
return mError == NO_ERROR;
}
public boolean unlock(String password) {
return unlock(getBytes(password));
}
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;
}
private static byte[] getBytes(String string) {
try {
return string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// will never happen
throw new RuntimeException(e);
}
}
private static String toString(byte[] bytes) {
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
// will never happen
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,139 @@
/*
* Copyright (C) 2010 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.os.Environment;
import android.os.FileUtils;
import android.os.Process;
import org.apache.harmony.luni.util.InputStreamHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
*@hide
*/
public class SystemKeyStore {
private static final String SYSTEM_KEYSTORE_DIRECTORY = "misc/systemkeys";
private static final String KEY_FILE_EXTENSION = ".sks";
private static SystemKeyStore mInstance = new SystemKeyStore();
private SystemKeyStore() { }
public static SystemKeyStore getInstance() {
return mInstance;
}
public static String toHexString(byte[] keyData) {
if (keyData == null) {
return null;
}
int keyLen = keyData.length;
int expectedStringLen = keyData.length * 2;
StringBuilder sb = new StringBuilder(expectedStringLen);
for (int i = 0; i < keyData.length; i++) {
String hexStr = Integer.toString(keyData[i] & 0x00FF, 16);
if (hexStr.length() == 1) {
hexStr = "0" + hexStr;
}
sb.append(hexStr);
}
return sb.toString();
}
public String generateNewKeyHexString(int numBits, String algName, String keyName)
throws NoSuchAlgorithmException {
return toHexString(generateNewKey(numBits, algName, keyName));
}
public byte[] generateNewKey(int numBits, String algName, String keyName)
throws NoSuchAlgorithmException {
// Check if key with similar name exists. If so, return null.
File keyFile = getKeyFile(keyName);
if (keyFile.exists()) {
throw new IllegalArgumentException();
}
KeyGenerator skg = KeyGenerator.getInstance(algName);
SecureRandom srng = SecureRandom.getInstance("SHA1PRNG");
skg.init(numBits, srng);
SecretKey sk = skg.generateKey();
byte[] retKey = sk.getEncoded();
try {
// Store the key
if (!keyFile.createNewFile()) {
throw new IllegalArgumentException();
}
FileOutputStream fos = new FileOutputStream(keyFile);
fos.write(retKey);
fos.flush();
FileUtils.sync(fos);
fos.close();
FileUtils.setPermissions(keyFile.getName(), (FileUtils.S_IRUSR | FileUtils.S_IWUSR),
-1, -1);
} catch (IOException ioe) {
return null;
}
return retKey;
}
private File getKeyFile(String keyName) {
File sysKeystoreDir = new File(Environment.getDataDirectory(),
SYSTEM_KEYSTORE_DIRECTORY);
File keyFile = new File(sysKeystoreDir, keyName + KEY_FILE_EXTENSION);
return keyFile;
}
public String retrieveKeyHexString(String keyName) throws IOException {
return toHexString(retrieveKey(keyName));
}
public byte[] retrieveKey(String keyName) throws IOException {
File keyFile = getKeyFile(keyName);
if (!keyFile.exists()) {
return null;
}
FileInputStream fis = new FileInputStream(keyFile);
return InputStreamHelper.readFullyAndClose(fis);
}
public void deleteKey(String keyName) {
// Get the file first.
File keyFile = getKeyFile(keyName);
if (!keyFile.exists()) {
throw new IllegalArgumentException();
}
keyFile.delete();
}
}
+15
View File
@@ -0,0 +1,15 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
# We only want this apk build for tests.
LOCAL_MODULE_TAGS := tests
LOCAL_CERTIFICATE := platform
LOCAL_JAVA_LIBRARIES := android.test.runner
# Include all test java files.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := KeyStoreTests
include $(BUILD_PACKAGE)
+29
View File
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.security.tests"
android:sharedUserId="android.uid.system">
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name=".KeyStoreTestRunner"
android:targetPackage="android.security.tests"
android:label="KeyStore Tests">
</instrumentation>
</manifest>
+146
View File
@@ -0,0 +1,146 @@
/*
* 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.tests;
import android.app.Activity;
import android.security.KeyStore;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;
/**
* Junit / Instrumentation test case for KeyStore class
*
* Running the test suite:
*
* adb shell am instrument -w android.security.tests/.KeyStoreTestRunner
*/
@MediumTest
public class KeyStoreTest extends ActivityUnitTestCase<Activity> {
private static final String TEST_PASSWD = "12345678";
private static final String TEST_EMPTY_PASSWD = "";
private static final String TEST_SHORT_PASSWD = "short";
private static final String TEST_PASSWD2 = "87654321";
private static final String TEST_KEYNAME = "testkey";
private static final String TEST_KEYNAME1 = "testkey1";
private static final String TEST_KEYNAME2 = "testkey2";
private static final String TEST_KEYVALUE = "test value";
// "Hello, World" in Chinese
private static final String TEST_I18N = "\u4F60\u597D, \u4E16\u754C";
private KeyStore mKeyStore = null;
public KeyStoreTest() {
super(Activity.class);
}
@Override
protected void setUp() throws Exception {
mKeyStore = KeyStore.getInstance();
if (mKeyStore.test() != KeyStore.UNINITIALIZED) mKeyStore.reset();
assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test());
super.setUp();
}
@Override
protected void tearDown() throws Exception {
mKeyStore.reset();
super.tearDown();
}
public void testTest() throws Exception {
assertEquals(KeyStore.UNINITIALIZED, mKeyStore.test());
}
public void testPassword() throws Exception {
//assertFalse(mKeyStore.password(TEST_EMPTY_PASSWD));
//assertFalse(mKeyStore.password(TEST_SHORT_PASSWD));
assertTrue(mKeyStore.password(TEST_PASSWD));
assertEquals(KeyStore.NO_ERROR, mKeyStore.test());
assertFalse(mKeyStore.password(TEST_PASSWD2, TEST_PASSWD2));
//assertFalse(mKeyStore.password(TEST_PASSWD, TEST_SHORT_PASSWD));
assertTrue(mKeyStore.password(TEST_PASSWD, TEST_PASSWD2));
}
public void testPut() throws Exception {
assertFalse(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
assertFalse(mKeyStore.contains(TEST_KEYNAME));
mKeyStore.password(TEST_PASSWD);
assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE));
}
public void testI18n() throws Exception {
assertFalse(mKeyStore.put(TEST_I18N, TEST_I18N));
assertFalse(mKeyStore.contains(TEST_I18N));
mKeyStore.password(TEST_I18N);
assertTrue(mKeyStore.put(TEST_I18N, TEST_I18N));
assertTrue(mKeyStore.contains(TEST_I18N));
}
public void testDelete() throws Exception {
assertTrue(mKeyStore.delete(TEST_KEYNAME));
mKeyStore.password(TEST_PASSWD);
assertTrue(mKeyStore.delete(TEST_KEYNAME));
mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE);
assertTrue(mKeyStore.delete(TEST_KEYNAME));
}
public void testContains() throws Exception {
assertFalse(mKeyStore.contains(TEST_KEYNAME));
mKeyStore.password(TEST_PASSWD);
assertFalse(mKeyStore.contains(TEST_KEYNAME));
mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE);
assertTrue(mKeyStore.contains(TEST_KEYNAME));
}
public void testSaw() throws Exception {
String[] results = mKeyStore.saw(TEST_KEYNAME);
assertEquals(0, results.length);
mKeyStore.password(TEST_PASSWD);
mKeyStore.put(TEST_KEYNAME1, TEST_KEYVALUE);
mKeyStore.put(TEST_KEYNAME2, TEST_KEYVALUE);
results = mKeyStore.saw(TEST_KEYNAME);
assertEquals(2, results.length);
}
public void testLock() throws Exception {
assertFalse(mKeyStore.lock());
mKeyStore.password(TEST_PASSWD);
assertEquals(KeyStore.NO_ERROR, mKeyStore.test());
assertTrue(mKeyStore.lock());
assertEquals(KeyStore.LOCKED, mKeyStore.test());
}
public void testUnlock() throws Exception {
mKeyStore.password(TEST_PASSWD);
assertEquals(KeyStore.NO_ERROR, mKeyStore.test());
mKeyStore.lock();
assertFalse(mKeyStore.unlock(TEST_PASSWD2));
assertTrue(mKeyStore.unlock(TEST_PASSWD));
}
}
@@ -0,0 +1,48 @@
/*
* 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.tests;
import junit.framework.TestSuite;
import android.test.InstrumentationTestRunner;
import android.test.InstrumentationTestSuite;
/**
* Instrumentation Test Runner for all KeyStore unit tests.
*
* Running all tests:
*
* runtest keystore-unit
* or
* adb shell am instrument -w android.security.tests/.KeyStoreTestRunner
*/
public class KeyStoreTestRunner extends InstrumentationTestRunner {
@Override
public TestSuite getAllTests() {
TestSuite suite = new InstrumentationTestSuite(this);
suite.addTestSuite(android.security.tests.KeyStoreTest.class);
suite.addTestSuite(android.security.tests.SystemKeyStoreTest.class);
return suite;
}
@Override
public ClassLoader getLoader() {
return KeyStoreTestRunner.class.getClassLoader();
}
}
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2010 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.tests;
import android.app.Activity;
import android.security.SystemKeyStore;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.MediumTest;
/**
* Junit / Instrumentation test case for KeyStore class
*
* Running the test suite:
*
* adb shell am instrument -w android.security.tests/.KeyStoreTestRunner
*/
@MediumTest
public class SystemKeyStoreTest extends ActivityUnitTestCase<Activity> {
private static final String keyName = "TestKey";
private static final String keyName2 = "TestKey2";
private SystemKeyStore mSysKeyStore = null;
public SystemKeyStoreTest() {
super(Activity.class);
}
@Override
protected void setUp() throws Exception {
mSysKeyStore = SystemKeyStore.getInstance();
try {
mSysKeyStore.deleteKey(keyName);
mSysKeyStore.deleteKey(keyName2);
} catch (Exception e) { }
super.setUp();
}
@Override
protected void tearDown() throws Exception {
try {
mSysKeyStore.deleteKey(keyName);
mSysKeyStore.deleteKey(keyName2);
} catch (Exception e) { }
super.tearDown();
}
public void testBasicAccess() throws Exception {
try {
byte[] newKey = mSysKeyStore.generateNewKey(128, "AES", keyName);
assertNotNull(newKey);
byte[] recKey = mSysKeyStore.retrieveKey(keyName);
assertEquals(newKey.length, recKey.length);
for (int i = 0; i < newKey.length; i++) {
assertEquals(newKey[i], recKey[i]);
}
mSysKeyStore.deleteKey(keyName);
byte[] nullKey = mSysKeyStore.retrieveKey(keyName);
assertNull(nullKey);
String newKeyStr = mSysKeyStore.generateNewKeyHexString(128, "AES", keyName2);
assertNotNull(newKeyStr);
String recKeyStr = mSysKeyStore.retrieveKeyHexString(keyName2);
assertEquals(newKeyStr, recKeyStr);
mSysKeyStore.deleteKey(keyName2);
String nullKey2 = mSysKeyStore.retrieveKeyHexString(keyName2);
assertNull(nullKey2);
} catch (Exception e) {
fail();
}
}
}