| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.security; |
| 18 | |
| 19 | import android.content.ActivityNotFoundException; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import java.security.KeyPair; |
| 25 | |
| 26 | /** |
| 27 | * {@hide} |
| 28 | */ |
| 29 | public class Credentials { |
| 30 | private static final String LOGTAG = "Credentials"; |
| Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 31 | |
| 32 | public static final String UNLOCK_ACTION = "android.credentials.UNLOCK"; |
| 33 | |
| 34 | public static final String INSTALL_ACTION = "android.credentials.INSTALL"; |
| 35 | |
| 36 | public static final String SYSTEM_INSTALL_ACTION = "android.credentials.SYSTEM_INSTALL"; |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 37 | |
| 38 | /** Key prefix for CA certificates. */ |
| 39 | public static final String CA_CERTIFICATE = "CACERT_"; |
| 40 | |
| 41 | /** Key prefix for user certificates. */ |
| 42 | public static final String USER_CERTIFICATE = "USRCERT_"; |
| 43 | |
| 44 | /** Key prefix for user private keys. */ |
| 45 | public static final String USER_PRIVATE_KEY = "USRPKEY_"; |
| 46 | |
| 47 | /** Key prefix for VPN. */ |
| 48 | public static final String VPN = "VPN_"; |
| 49 | |
| 50 | /** Key prefix for WIFI. */ |
| 51 | public static final String WIFI = "WIFI_"; |
| 52 | |
| 53 | /** Data type for public keys. */ |
| 54 | public static final String PUBLIC_KEY = "KEY"; |
| 55 | |
| 56 | /** Data type for private keys. */ |
| 57 | public static final String PRIVATE_KEY = "PKEY"; |
| 58 | |
| 59 | /** Data type for certificates. */ |
| 60 | public static final String CERTIFICATE = "CERT"; |
| 61 | |
| 62 | /** Data type for PKCS12. */ |
| 63 | public static final String PKCS12 = "PKCS12"; |
| 64 | |
| Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 65 | private static Credentials singleton; |
| 66 | |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 67 | public static Credentials getInstance() { |
| 68 | if (singleton == null) { |
| 69 | singleton = new Credentials(); |
| 70 | } |
| 71 | return singleton; |
| 72 | } |
| 73 | |
| 74 | public void unlock(Context context) { |
| 75 | try { |
| 76 | Intent intent = new Intent(UNLOCK_ACTION); |
| 77 | context.startActivity(intent); |
| 78 | } catch (ActivityNotFoundException e) { |
| 79 | Log.w(LOGTAG, e.toString()); |
| 80 | } |
| 81 | } |
| 82 | |
| Hung-ying Tyan | c5e630a | 2010-10-08 08:20:16 +0800 | [diff] [blame^] | 83 | private Intent createInstallIntent() { |
| 84 | Intent intent = new Intent(INSTALL_ACTION); |
| 85 | intent.setClassName("com.android.certinstaller", |
| 86 | "com.android.certinstaller.CertInstallerMain"); |
| 87 | return intent; |
| 88 | } |
| 89 | |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 90 | public void install(Context context, KeyPair pair) { |
| 91 | try { |
| Hung-ying Tyan | c5e630a | 2010-10-08 08:20:16 +0800 | [diff] [blame^] | 92 | Intent intent = createInstallIntent(); |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 93 | intent.putExtra(PRIVATE_KEY, pair.getPrivate().getEncoded()); |
| 94 | intent.putExtra(PUBLIC_KEY, pair.getPublic().getEncoded()); |
| 95 | context.startActivity(intent); |
| 96 | } catch (ActivityNotFoundException e) { |
| 97 | Log.w(LOGTAG, e.toString()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public void install(Context context, String type, byte[] value) { |
| 102 | try { |
| Hung-ying Tyan | c5e630a | 2010-10-08 08:20:16 +0800 | [diff] [blame^] | 103 | Intent intent = createInstallIntent(); |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 104 | intent.putExtra(type, value); |
| 105 | context.startActivity(intent); |
| 106 | } catch (ActivityNotFoundException e) { |
| 107 | Log.w(LOGTAG, e.toString()); |
| 108 | } |
| 109 | } |
| Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 110 | |
| 111 | public void installFromSdCard(Context context) { |
| 112 | try { |
| Hung-ying Tyan | c5e630a | 2010-10-08 08:20:16 +0800 | [diff] [blame^] | 113 | context.startActivity(createInstallIntent()); |
| Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 114 | } catch (ActivityNotFoundException e) { |
| 115 | Log.w(LOGTAG, e.toString()); |
| 116 | } |
| 117 | } |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 118 | } |