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