| 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; |
| Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 23 | import com.android.org.bouncycastle.openssl.PEMReader; |
| 24 | import com.android.org.bouncycastle.openssl.PEMWriter; |
| 25 | import java.io.ByteArrayInputStream; |
| 26 | import java.io.ByteArrayOutputStream; |
| 27 | import java.io.IOException; |
| 28 | import java.io.InputStreamReader; |
| 29 | import java.io.OutputStreamWriter; |
| 30 | import java.io.Reader; |
| 31 | import java.io.Writer; |
| 32 | import java.nio.charset.Charsets; |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 33 | import java.security.KeyPair; |
| Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * {@hide} |
| 39 | */ |
| 40 | public class Credentials { |
| 41 | private static final String LOGTAG = "Credentials"; |
| Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 42 | |
| Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 43 | public static final String INSTALL_ACTION = "android.credentials.INSTALL"; |
| 44 | |
| Brian Carlstrom | 4a9e1a2 | 2011-04-22 15:45:22 -0700 | [diff] [blame] | 45 | public static final String UNLOCK_ACTION = "com.android.credentials.UNLOCK"; |
| 46 | |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 47 | /** Key prefix for CA certificates. */ |
| 48 | public static final String CA_CERTIFICATE = "CACERT_"; |
| 49 | |
| 50 | /** Key prefix for user certificates. */ |
| 51 | public static final String USER_CERTIFICATE = "USRCERT_"; |
| 52 | |
| 53 | /** Key prefix for user private keys. */ |
| 54 | public static final String USER_PRIVATE_KEY = "USRPKEY_"; |
| 55 | |
| 56 | /** Key prefix for VPN. */ |
| 57 | public static final String VPN = "VPN_"; |
| 58 | |
| 59 | /** Key prefix for WIFI. */ |
| 60 | public static final String WIFI = "WIFI_"; |
| 61 | |
| 62 | /** Data type for public keys. */ |
| 63 | public static final String PUBLIC_KEY = "KEY"; |
| 64 | |
| 65 | /** Data type for private keys. */ |
| 66 | public static final String PRIVATE_KEY = "PKEY"; |
| 67 | |
| 68 | /** Data type for certificates. */ |
| 69 | public static final String CERTIFICATE = "CERT"; |
| 70 | |
| 71 | /** Data type for PKCS12. */ |
| 72 | public static final String PKCS12 = "PKCS12"; |
| 73 | |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 74 | // historically used by Android |
| 75 | public static final String EXTENSION_CRT = ".crt"; |
| 76 | public static final String EXTENSION_P12 = ".p12"; |
| 77 | // commonly used on Windows |
| 78 | public static final String EXTENSION_CER = ".cer"; |
| 79 | public static final String EXTENSION_PFX = ".pfx"; |
| 80 | |
| Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Convert objects to a PEM format, which is used for |
| 83 | * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY |
| 84 | * entries. |
| 85 | */ |
| 86 | public static byte[] convertToPem(Object... objects) throws IOException { |
| 87 | ByteArrayOutputStream bao = new ByteArrayOutputStream(); |
| 88 | Writer writer = new OutputStreamWriter(bao, Charsets.US_ASCII); |
| 89 | PEMWriter pw = new PEMWriter(writer); |
| 90 | for (Object o : objects) { |
| 91 | pw.writeObject(o); |
| 92 | } |
| 93 | pw.close(); |
| 94 | return bao.toByteArray(); |
| 95 | } |
| 96 | /** |
| 97 | * Convert objects from PEM format, which is used for |
| 98 | * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY |
| 99 | * entries. |
| 100 | */ |
| 101 | public static List<Object> convertFromPem(byte[] bytes) throws IOException { |
| 102 | ByteArrayInputStream bai = new ByteArrayInputStream(bytes); |
| 103 | Reader reader = new InputStreamReader(bai, Charsets.US_ASCII); |
| 104 | PEMReader pr = new PEMReader(reader); |
| 105 | |
| 106 | List<Object> result = new ArrayList<Object>(); |
| 107 | Object o; |
| 108 | while ((o = pr.readObject()) != null) { |
| 109 | result.add(o); |
| 110 | } |
| 111 | pr.close(); |
| 112 | return result; |
| 113 | } |
| 114 | |
| Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 115 | private static Credentials singleton; |
| 116 | |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 117 | public static Credentials getInstance() { |
| 118 | if (singleton == null) { |
| 119 | singleton = new Credentials(); |
| 120 | } |
| 121 | return singleton; |
| 122 | } |
| 123 | |
| 124 | public void unlock(Context context) { |
| 125 | try { |
| Brian Carlstrom | 4a9e1a2 | 2011-04-22 15:45:22 -0700 | [diff] [blame] | 126 | Intent intent = new Intent(UNLOCK_ACTION); |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 127 | context.startActivity(intent); |
| 128 | } catch (ActivityNotFoundException e) { |
| 129 | Log.w(LOGTAG, e.toString()); |
| 130 | } |
| 131 | } |
| 132 | |
| Hung-ying Tyan | c5e630a | 2010-10-08 08:20:16 +0800 | [diff] [blame] | 133 | private Intent createInstallIntent() { |
| 134 | Intent intent = new Intent(INSTALL_ACTION); |
| 135 | intent.setClassName("com.android.certinstaller", |
| 136 | "com.android.certinstaller.CertInstallerMain"); |
| 137 | return intent; |
| 138 | } |
| 139 | |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 140 | public void install(Context context) { |
| 141 | try { |
| 142 | Intent intent = createInstallIntent(); |
| 143 | context.startActivity(intent); |
| 144 | } catch (ActivityNotFoundException e) { |
| 145 | Log.w(LOGTAG, e.toString()); |
| 146 | } |
| 147 | } |
| 148 | |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 149 | public void install(Context context, KeyPair pair) { |
| 150 | try { |
| Hung-ying Tyan | c5e630a | 2010-10-08 08:20:16 +0800 | [diff] [blame] | 151 | Intent intent = createInstallIntent(); |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 152 | intent.putExtra(PRIVATE_KEY, pair.getPrivate().getEncoded()); |
| 153 | intent.putExtra(PUBLIC_KEY, pair.getPublic().getEncoded()); |
| 154 | context.startActivity(intent); |
| 155 | } catch (ActivityNotFoundException e) { |
| 156 | Log.w(LOGTAG, e.toString()); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public void install(Context context, String type, byte[] value) { |
| 161 | try { |
| Hung-ying Tyan | c5e630a | 2010-10-08 08:20:16 +0800 | [diff] [blame] | 162 | Intent intent = createInstallIntent(); |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 163 | intent.putExtra(type, value); |
| 164 | context.startActivity(intent); |
| 165 | } catch (ActivityNotFoundException e) { |
| 166 | Log.w(LOGTAG, e.toString()); |
| 167 | } |
| 168 | } |
| Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 169 | } |