| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +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 | |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 19 | import java.io.ByteArrayInputStream; |
| 20 | import java.io.IOException; |
| 21 | import java.security.cert.Certificate; |
| 22 | import java.security.cert.CertificateException; |
| 23 | import java.security.KeyStoreException; |
| 24 | import java.security.NoSuchAlgorithmException; |
| 25 | import java.security.UnrecoverableKeyException; |
| 26 | |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.security.Keystore; |
| 30 | import android.text.TextUtils; |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 31 | import android.util.Log; |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * The CertTool class provides the functions to list the certs/keys, |
| 35 | * generate the certificate request(csr) and store the certificate into |
| 36 | * keystore. |
| 37 | * |
| 38 | * {@hide} |
| 39 | */ |
| 40 | public class CertTool { |
| Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 41 | static { |
| 42 | System.loadLibrary("certtool_jni"); |
| 43 | } |
| 44 | |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 45 | public static final String ACTION_ADD_CREDENTIAL = |
| 46 | "android.security.ADD_CREDENTIAL"; |
| 47 | public static final String KEY_TYPE_NAME = "typeName"; |
| 48 | public static final String KEY_ITEM = "item"; |
| 49 | public static final String KEY_NAMESPACE = "namespace"; |
| 50 | public static final String KEY_DESCRIPTION = "description"; |
| 51 | |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 52 | public static final String TITLE_CA_CERT = "CA Certificate"; |
| 53 | public static final String TITLE_USER_CERT = "User Certificate"; |
| 54 | public static final String TITLE_PKCS12_KEYSTORE = "PKCS12 Keystore"; |
| 55 | public static final String TITLE_PRIVATE_KEY = "Private Key"; |
| Chung-yih Wang | 22726cf | 2009-07-22 05:31:48 +0800 | [diff] [blame^] | 56 | public static final int INCORRECT_PKCS12_PASSPHRASE = -100; |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 57 | |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 58 | private static final String TAG = "CertTool"; |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 59 | private static final String UNKNOWN = "Unknown"; |
| 60 | private static final String ISSUER_NAME = "Issuer Name:"; |
| 61 | private static final String DISTINCT_NAME = "Distinct Name:"; |
| 62 | |
| 63 | private static final String CA_CERTIFICATE = "CACERT"; |
| 64 | private static final String USER_CERTIFICATE = "USRCERT"; |
| 65 | private static final String USER_KEY = "USRKEY"; |
| 66 | |
| Chung-yih Wang | fa927c0 | 2009-07-02 19:11:11 +0800 | [diff] [blame] | 67 | private static final String KEYNAME_DELIMITER = "_"; |
| Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 68 | private static final Keystore sKeystore = Keystore.getInstance(); |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 69 | |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 70 | private native int getPkcs12Handle(byte[] data, String password); |
| 71 | private native String getPkcs12Certificate(int handle); |
| 72 | private native String getPkcs12PrivateKey(int handle); |
| 73 | private native String popPkcs12CertificateStack(int handle); |
| 74 | private native void freePkcs12Handle(int handle); |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 75 | private native String generateCertificateRequest(int bits, String subject); |
| 76 | private native boolean isPkcs12Keystore(byte[] data); |
| 77 | private native int generateX509Certificate(byte[] data); |
| 78 | private native boolean isCaCertificate(int handle); |
| 79 | private native String getIssuerDN(int handle); |
| 80 | private native String getCertificateDN(int handle); |
| 81 | private native String getPrivateKeyPEM(int handle); |
| 82 | private native void freeX509Certificate(int handle); |
| 83 | |
| Chung-yih Wang | bf20b99 | 2009-07-02 23:42:12 +0800 | [diff] [blame] | 84 | private static CertTool singleton = null; |
| 85 | |
| Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 86 | private CertTool() { } |
| 87 | |
| Chung-yih Wang | bf20b99 | 2009-07-02 23:42:12 +0800 | [diff] [blame] | 88 | public static final CertTool getInstance() { |
| 89 | if (singleton == null) { |
| 90 | singleton = new CertTool(); |
| 91 | } |
| 92 | return singleton; |
| 93 | } |
| 94 | |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 95 | public String getUserPrivateKey(String key) { |
| 96 | return USER_KEY + KEYNAME_DELIMITER + key; |
| 97 | } |
| 98 | |
| 99 | public String getUserCertificate(String key) { |
| 100 | return USER_CERTIFICATE + KEYNAME_DELIMITER + key; |
| 101 | } |
| 102 | |
| 103 | public String getCaCertificate(String key) { |
| 104 | return CA_CERTIFICATE + KEYNAME_DELIMITER + key; |
| 105 | } |
| 106 | |
| 107 | public String[] getAllUserCertificateKeys() { |
| Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 108 | return sKeystore.listKeys(USER_KEY); |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public String[] getAllCaCertificateKeys() { |
| Chung-yih Wang | 699ca3f | 2009-07-04 22:19:51 +0800 | [diff] [blame] | 112 | return sKeystore.listKeys(CA_CERTIFICATE); |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public String[] getSupportedKeyStrenghs() { |
| 116 | return new String[] {"High Grade", "Medium Grade"}; |
| 117 | } |
| 118 | |
| 119 | private int getKeyLength(int index) { |
| 120 | if (index == 0) return 2048; |
| 121 | return 1024; |
| 122 | } |
| 123 | |
| 124 | public String generateKeyPair(int keyStrengthIndex, String challenge, |
| 125 | String dirName) { |
| 126 | return generateCertificateRequest(getKeyLength(keyStrengthIndex), |
| 127 | dirName); |
| 128 | } |
| 129 | |
| 130 | private Intent prepareIntent(String title, byte[] data, String namespace, |
| 131 | String issuer, String distinctName) { |
| 132 | Intent intent = new Intent(ACTION_ADD_CREDENTIAL); |
| 133 | intent.putExtra(KEY_TYPE_NAME, title); |
| 134 | intent.putExtra(KEY_ITEM + "0", data); |
| 135 | intent.putExtra(KEY_NAMESPACE + "0", namespace); |
| 136 | intent.putExtra(KEY_DESCRIPTION + "0", ISSUER_NAME + issuer); |
| 137 | intent.putExtra(KEY_DESCRIPTION + "1", DISTINCT_NAME + distinctName); |
| 138 | return intent; |
| 139 | } |
| 140 | |
| 141 | private void addExtraIntentInfo(Intent intent, String namespace, |
| 142 | String data) { |
| 143 | intent.putExtra(KEY_ITEM + "1", data); |
| 144 | intent.putExtra(KEY_NAMESPACE + "1", namespace); |
| 145 | } |
| 146 | |
| Chung-yih Wang | 22726cf | 2009-07-22 05:31:48 +0800 | [diff] [blame^] | 147 | private int extractAndStoreKeysFromPkcs12(int handle, String keyname) { |
| 148 | int ret, i = 0; |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 149 | String pemData; |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 150 | |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 151 | if ((pemData = getPkcs12Certificate(handle)) != null) { |
| Chung-yih Wang | 22726cf | 2009-07-22 05:31:48 +0800 | [diff] [blame^] | 152 | if ((ret = sKeystore.put(USER_CERTIFICATE, keyname, pemData)) != 0) { |
| 153 | return ret; |
| 154 | } |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 155 | } |
| 156 | if ((pemData = getPkcs12PrivateKey(handle)) != null) { |
| Chung-yih Wang | 22726cf | 2009-07-22 05:31:48 +0800 | [diff] [blame^] | 157 | if ((ret = sKeystore.put(USER_KEY, keyname, pemData)) != 0) { |
| 158 | return ret; |
| 159 | } |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 160 | } |
| 161 | while ((pemData = this.popPkcs12CertificateStack(handle)) != null) { |
| 162 | if (i++ > 0) { |
| Chung-yih Wang | 22726cf | 2009-07-22 05:31:48 +0800 | [diff] [blame^] | 163 | if ((ret = sKeystore.put(CA_CERTIFICATE, keyname + i, pemData)) != 0) { |
| 164 | return ret; |
| 165 | } |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 166 | } else { |
| Chung-yih Wang | 22726cf | 2009-07-22 05:31:48 +0800 | [diff] [blame^] | 167 | if ((ret = sKeystore.put(CA_CERTIFICATE, keyname, pemData)) != 0) { |
| 168 | return ret; |
| 169 | } |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 170 | } |
| 171 | } |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| Chung-yih Wang | 22726cf | 2009-07-22 05:31:48 +0800 | [diff] [blame^] | 175 | public int addPkcs12Keystore(byte[] p12Data, String password, |
| 176 | String keyname) { |
| 177 | int handle, ret; |
| 178 | Log.i("CertTool", "addPkcs12Keystore()"); |
| 179 | |
| 180 | if ((handle = getPkcs12Handle(p12Data, password)) == 0) { |
| 181 | return INCORRECT_PKCS12_PASSPHRASE; |
| 182 | } |
| 183 | ret = extractAndStoreKeysFromPkcs12(handle, keyname); |
| 184 | freePkcs12Handle(handle); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 188 | public synchronized void addCertificate(byte[] data, Context context) { |
| 189 | int handle; |
| 190 | Intent intent = null; |
| 191 | |
| Chung-yih Wang | c9c119e | 2009-07-16 19:54:33 +0800 | [diff] [blame] | 192 | Log.i("CertTool", "addCertificate()"); |
| Chung-yih Wang | eec1182 | 2009-07-02 00:22:04 +0800 | [diff] [blame] | 193 | if (isPkcs12Keystore(data)) { |
| 194 | intent = prepareIntent(TITLE_PKCS12_KEYSTORE, data, USER_KEY, |
| 195 | UNKNOWN, UNKNOWN); |
| 196 | } else if ((handle = generateX509Certificate(data)) != 0) { |
| 197 | String issuer = getIssuerDN(handle); |
| 198 | String distinctName = getCertificateDN(handle); |
| 199 | String privateKeyPEM = getPrivateKeyPEM(handle); |
| 200 | if (isCaCertificate(handle)) { |
| 201 | intent = prepareIntent(TITLE_CA_CERT, data, CA_CERTIFICATE, |
| 202 | issuer, distinctName); |
| 203 | } else { |
| 204 | intent = prepareIntent(TITLE_USER_CERT, data, USER_CERTIFICATE, |
| 205 | issuer, distinctName); |
| 206 | if (!TextUtils.isEmpty(privateKeyPEM)) { |
| 207 | addExtraIntentInfo(intent, USER_KEY, privateKeyPEM); |
| 208 | } |
| 209 | } |
| 210 | freeX509Certificate(handle); |
| 211 | } |
| 212 | if (intent != null) context.startActivity(intent); |
| 213 | } |
| 214 | } |