summaryrefslogtreecommitdiffstats
path: root/keystore/java/android/security/CertTool.java
blob: 88d6e3dcdd6c7517ae347f1856623752cfb0dd7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * 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.Context;
import android.content.Intent;
import android.security.Keystore;
import android.text.TextUtils;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.util.ArrayList;

/**
 * The CertTool class provides the functions to list the certs/keys,
 * generate the certificate request(csr) and store certificates into
 * keystore.
 *
 * {@hide}
 */
public class CertTool {
    static {
        System.loadLibrary("certtool_jni");
    }

    /** Keystore namespace for CA certificates. */
    public static final String CA_CERTIFICATE = "CACERT";

    /** Keystore namespace for user certificates. */
    public static final String USER_CERTIFICATE = "USRCERT";

    /** Keystore namespace for user private keys. */
    public static final String USER_KEY = "USRKEY";

    /** Action string for adding certificates to keystore. */
    public static final String ACTION_ADD_CREDENTIAL =
            "android.security.ADD_CREDENTIAL";

    /** Action string for installing certificates to keystore from sdcard. */
    public static final String ACTION_INSTALL_CERT_FROM_SDCARD =
            "android.security.INSTALL_CERT_FROM_SDCARD";

    /** Dialog title for adding a CA certificate. */
    public static final String TITLE_CA_CERT = "CA Certificate";

    /** Dialog title for adding a user certificate. */
    public static final String TITLE_USER_CERT = "User Certificate";

    /** Dialog title for adding a user private key. */
    public static final String TITLE_PRIVATE_KEY = "Private Key";

    /** Dialog title for adding a PKCS12 keystore. */
    public static final String TITLE_PKCS12_KEYSTORE = "PKCS12 Keystore";

    public static final int INCORRECT_PKCS12_PASSPHRASE = -100;

    /**
     * The builder class for building an add-credential-to-keystore intent.
     */
    public static class AddCredentialIntentBuilder {
        private Intent mIntent;
        private int mCount;

        /**
         * Creates a builder to build a add-credential-to-keystore intent.
         *
         * @param title title of the dialog for adding this credential
         * @param descriptions description strings to show on the dialog
         */
        public AddCredentialIntentBuilder(String title,
                String... descriptions) {
            Intent intent = new Intent(ACTION_ADD_CREDENTIAL);
            intent.putExtra(KEY_TITLE, title);

            int i = 0;
            for (String description : descriptions) {
                intent.putExtra(KEY_DESCRIPTION + (i++), description);
            }
            mIntent = intent;
        }

        /**
         * Adds credential data to the intent.
         *
         * @param namespace the namespace of the keystore to add the credential
         *      data to
         * @param data the credential data
         * @return this builder
         */
        public AddCredentialIntentBuilder addCredential(String namespace,
                byte[] data) {
            mIntent.putExtra(KEY_NAMESPACE + mCount, namespace);
            mIntent.putExtra(KEY_ITEM + mCount, data);
            mCount++;
            return this;
        }

        /** Returns the intent. */
        public Intent build() {
            return mIntent;
        }
    }

    /**
     * Request for adding credential data to keystore.
     */
    public static class AddCredentialRequest {
        private Intent mIntent;

        /**
         * Creates an add-credential-data-to-keystore request.
         *
         * @param intent an add-credential-data-to-keystore intent
         * @see AddCredentialIntentBuilder
         */
        public AddCredentialRequest(Intent intent) {
            mIntent = intent;
        }

        /** Returns the dialog title. */
        public String getTitle() {
            return mIntent.getStringExtra(KEY_TITLE);
        }

        /**
         * Returns the i'th credential data.
         * @return the data or null if not exists
         */
        public byte[] getDataAt(int i) {
            return mIntent.getByteArrayExtra(KEY_ITEM + i);
        }

        /**
         * Returns the namespace of the i'th credential data.
         * @return the namespace string or null if missing
         */
        public String getNamespaceAt(int i) {
            return mIntent.getStringExtra(KEY_NAMESPACE + i);
        }

        /** Returns the descriptions of the credential data. */
        public String[] getDescriptions() {
            ArrayList<String> list = new ArrayList<String>();
            for (int i = 0; ; i++) {
                String s = mIntent.getStringExtra(KEY_DESCRIPTION + i);
                if (s == null) break;
                list.add(s);
            }
            return list.toArray(new String[list.size()]);
        }
    }

    private static final String KEY_TITLE = "typeName";
    private static final String KEY_ITEM = "item";
    private static final String KEY_NAMESPACE = "namespace";
    private static final String KEY_DESCRIPTION = "description";

    private static final String TAG = "CertTool";
    private static final String UNKNOWN = "Unknown";
    private static final String ISSUER_NAME = "Issuer Name:";
    private static final String DISTINCT_NAME = "Distinct Name:";

    private static final String KEYNAME_DELIMITER = "_";
    private static final Keystore sKeystore = Keystore.getInstance();

    private native int getPkcs12Handle(byte[] data, String password);
    private native String getPkcs12Certificate(int handle);
    private native String getPkcs12PrivateKey(int handle);
    private native String popPkcs12CertificateStack(int handle);
    private native void freePkcs12Handle(int handle);
    private native String generateCertificateRequest(int bits, String challenge);
    private native boolean isPkcs12Keystore(byte[] data);
    private native int generateX509Certificate(byte[] data);
    private native boolean isCaCertificate(int handle);
    private native String getIssuerDN(int handle);
    private native String getCertificateDN(int handle);
    private native String getPrivateKeyPEM(int handle);
    private native void freeX509Certificate(int handle);

    private static CertTool sSingleton = null;

    private CertTool() { }

    public static final CertTool getInstance() {
        if (sSingleton == null) {
            sSingleton = new CertTool();
        }
        return sSingleton;
    }

    /**
     * Gets the full key to retrieve the user private key from the keystore.
     * @see #getAllUserCertificateKeys()
     */
    public String getUserPrivateKey(String key) {
        return USER_KEY + KEYNAME_DELIMITER + key;
    }

    /**
     * Gets the full key to retrieve the user certificate from the keystore.
     * @see #getAllUserCertificateKeys()
     */
    public String getUserCertificate(String key) {
        return USER_CERTIFICATE + KEYNAME_DELIMITER + key;
    }

    /**
     * Gets the full key to retrieve the CA certificate from the keystore.
     * @see #getAllCaCertificateKeys()
     */
    public String getCaCertificate(String key) {
        return CA_CERTIFICATE + KEYNAME_DELIMITER + key;
    }

    /**
     * Gets all the keys to the user certificates/private keys stored in the
     * keystore.
     * @see #getUserCertificate(String)
     * @see #getUserPrivateKey(String)
     */
    public String[] getAllUserCertificateKeys() {
        return sKeystore.listKeys(USER_KEY);
    }

    /**
     * Gets all the keys to the CA certificates stored in the keystore.
     * @see #getCaCertificate(String)
     */
    public String[] getAllCaCertificateKeys() {
        return sKeystore.listKeys(CA_CERTIFICATE);
    }

    public String[] getSupportedKeyStrenghs() {
        return new String[] {"High Grade", "Medium Grade"};
    }

    private int getKeyLength(int index) {
        if (index == 0) return 2048;
        return 1024;
    }

    /**
     * Generates a key pair.
     *
     * @param keyStrengthIndex index to the array of supported key strengths;
     *      see {@link #getSupportedKeyStrenghs()}
     * @param challenge the challenge string for generating the pair
     * @param dirName (not used)
     * @return a certificate request from the resulted public key
     */
    public String generateKeyPair(int keyStrengthIndex, String challenge,
            String dirName) {
        return generateCertificateRequest(getKeyLength(keyStrengthIndex),
                challenge);
    }

    private int extractAndStoreKeysFromPkcs12(int handle, String keyname) {
        int ret, i = 0;
        String pemData;

        if ((pemData = getPkcs12Certificate(handle)) != null) {
            if ((ret = sKeystore.put(USER_CERTIFICATE, keyname, pemData)) != 0) {
                return ret;
            }
        }
        if ((pemData = getPkcs12PrivateKey(handle)) != null) {
            if ((ret = sKeystore.put(USER_KEY, keyname, pemData)) != 0) {
                return ret;
            }
        }
        if ((pemData = this.popPkcs12CertificateStack(handle)) != null) {
            if ((ret = sKeystore.put(CA_CERTIFICATE, keyname, pemData)) != 0) {
                return ret;
            }
        }
        return 0;
    }

    /** Adds a PKCS12 keystore to the keystore. */
    public int addPkcs12Keystore(byte[] p12Data, String password,
            String keyname) {
        int handle, ret;
        Log.i("CertTool", "addPkcs12Keystore()");

        if ((handle = getPkcs12Handle(p12Data, password)) == 0) {
            return INCORRECT_PKCS12_PASSPHRASE;
        }
        ret = extractAndStoreKeysFromPkcs12(handle, keyname);
        freePkcs12Handle(handle);
        return ret;
    }

    /**
     * Adds a certificate to the keystore.
     *
     * @param data the certificate data
     * @param context the context to send an add-credential-to-keystore intent
     */
    public synchronized void addCertificate(byte[] data, Context context) {
        int handle;
        Intent intent = null;

        Log.i("CertTool", "addCertificate()");
        if (isPkcs12Keystore(data)) {
            intent = prepareIntent(TITLE_PKCS12_KEYSTORE, UNKNOWN, UNKNOWN)
                    .addCredential(USER_KEY, data).build();
        } else if ((handle = generateX509Certificate(data)) != 0) {
            String issuer = getIssuerDN(handle);
            String distinctName = getCertificateDN(handle);
            String privateKeyPEM = getPrivateKeyPEM(handle);
            if (isCaCertificate(handle)) {
                intent = prepareIntent(TITLE_CA_CERT, issuer, distinctName)
                        .addCredential(CA_CERTIFICATE, data).build();
            } else {
                AddCredentialIntentBuilder builder =
                        prepareIntent(TITLE_USER_CERT, issuer, distinctName)
                        .addCredential(USER_CERTIFICATE, data);
                if (!TextUtils.isEmpty(privateKeyPEM)) {
                    builder.addCredential(USER_KEY, privateKeyPEM.getBytes());
                }
                intent = builder.build();
            }
            freeX509Certificate(handle);
        }
        if (intent != null) {
            context.startActivity(intent);
        } else {
            Log.w("CertTool", "incorrect data for addCertificate()");
        }
    }

    private AddCredentialIntentBuilder prepareIntent(
            String title, String issuer, String distinctName) {
        return new AddCredentialIntentBuilder(title, ISSUER_NAME + issuer,
                DISTINCT_NAME + distinctName);
    }
}