summaryrefslogtreecommitdiff
path: root/tests/src/com/android/keychain/tests/BasicKeyChainServiceTest.java
blob: 4144b2da9ea7b0cf51432400efa31a455a9a0f53 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Copyright (C) 2019 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 com.android.keychain.tests;

import static android.os.Process.WIFI_UID;

import static com.google.common.truth.Truth.assertThat;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.ConditionVariable;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.platform.test.annotations.LargeTest;
import android.security.Credentials;
import android.security.IKeyChainService;
import android.security.KeyChain;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.security.keystore.ParcelableKeyGenParameterSpec;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import com.android.keychain.tests.support.IKeyChainServiceTestSupport;
import java.io.IOException;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import libcore.java.security.TestKeyStore;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class BasicKeyChainServiceTest {
    private static final String TAG = "BasicKeyChainServiceTest";
    private static final String ALIAS_1 = "client";
    private static final String ALIAS_IMPORTED = "imported";
    private static final String ALIAS_GENERATED = "generated";
    public static final byte[] DUMMY_CHALLENGE = {'a', 'b', 'c'};
    private static final String ALIAS_NON_EXISTING = "nonexisting";

    private Context mContext;

    private final ConditionVariable mSupportServiceAvailable = new ConditionVariable(false);
    private IKeyChainServiceTestSupport mTestSupportService;
    private boolean mIsSupportServiceBound;

    private ServiceConnection mSupportConnection =
            new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.d(TAG, "test support service connected!");
                    mTestSupportService = IKeyChainServiceTestSupport.Stub.asInterface(service);
                    mSupportServiceAvailable.open();
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    mSupportServiceAvailable.close();
                    mTestSupportService = null;
                }
            };

    private final ConditionVariable mKeyChainAvailable = new ConditionVariable(false);
    private IKeyChainService mKeyChainService;
    private boolean mIsKeyChainServiceBound;

    private ServiceConnection mServiceConnection =
            new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.d(TAG, "KeyChain service connected!");
                    mKeyChainService = IKeyChainService.Stub.asInterface(service);
                    mKeyChainAvailable.open();
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    mKeyChainAvailable.close();
                    mKeyChainService = null;
                }
            };

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getTargetContext();
        bindTestSupportService();
        assertThat(mIsSupportServiceBound).isTrue();
        bindKeyChainService();
        assertThat(mIsKeyChainServiceBound).isTrue();

        waitForSupportService();
        waitForKeyChainService();
    }

    @After
    public void tearDown() {
        // Clean up keys that might have been left over
        try {
            removeKeyPair(ALIAS_IMPORTED);
            removeKeyPair(ALIAS_GENERATED);
        } catch (RemoteException e) {
            // Nothing to do here but warn that clean-up was not successful.
            Log.w(TAG, "Failed cleaning up installed keys", e);
        }
        unbindTestSupportService();
        assertThat(mIsSupportServiceBound).isFalse();
        unbindKeyChainService();
        assertThat(mIsKeyChainServiceBound).isFalse();
    }

    @Test
    public void testCanAccessKeyAfterGettingGrant()
            throws RemoteException, IOException, CertificateException {
        Log.d(TAG, "Testing access to imported key after getting grant.");
        assertThat(mTestSupportService.keystoreReset()).isTrue();
        installFirstKey();
        assertThat(mKeyChainService.requestPrivateKey(ALIAS_1)).isNull();
        mTestSupportService.grantAppPermission(Process.myUid(), ALIAS_1);
        assertThat(mKeyChainService.requestPrivateKey(ALIAS_1)).isNotNull();
    }

    @Test
    public void testInstallAndRemoveKeyPair()
            throws RemoteException, IOException, CertificateException {
        Log.d(TAG, "Testing importing key.");
        assertThat(mTestSupportService.keystoreReset()).isTrue();
        // No key installed, all should fail.
        assertThat(mKeyChainService.requestPrivateKey(ALIAS_IMPORTED)).isNull();
        assertThat(mKeyChainService.getCertificate(ALIAS_IMPORTED)).isNull();
        assertThat(mKeyChainService.getCaCertificates(ALIAS_IMPORTED)).isNull();

        PrivateKeyEntry privateKeyEntry =
                TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA");
        assertThat(mTestSupportService.installKeyPair(privateKeyEntry.getPrivateKey().getEncoded(),
                    privateKeyEntry.getCertificate().getEncoded(),
                    Credentials.convertToPem(privateKeyEntry.getCertificateChain()),
                    ALIAS_IMPORTED)).isTrue();

        // No grant, all should still fail.
        assertThat(mKeyChainService.requestPrivateKey(ALIAS_IMPORTED)).isNull();
        assertThat(mKeyChainService.getCertificate(ALIAS_IMPORTED)).isNull();
        assertThat(mKeyChainService.getCaCertificates(ALIAS_IMPORTED)).isNull();
        // Grant access
        mTestSupportService.grantAppPermission(Process.myUid(), ALIAS_IMPORTED);
        // Has grant, all should succeed.
        assertThat(mKeyChainService.requestPrivateKey(ALIAS_IMPORTED)).isNotNull();
        assertThat(mKeyChainService.getCertificate(ALIAS_IMPORTED)).isNotNull();
        assertThat(mKeyChainService.getCaCertificates(ALIAS_IMPORTED)).isNotNull();
        // Finally, test removal.
        assertThat(mTestSupportService.removeKeyPair(ALIAS_IMPORTED)).isTrue();
    }

    @Test
    public void testUserSelectability() throws RemoteException, IOException, CertificateException {
        Log.d(TAG, "Testing user-selectability of a key.");
        assertThat(mTestSupportService.keystoreReset()).isTrue();
        PrivateKeyEntry privateKeyEntry =
                TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA");
        assertThat(mTestSupportService.installKeyPair(privateKeyEntry.getPrivateKey().getEncoded(),
                privateKeyEntry.getCertificate().getEncoded(),
                Credentials.convertToPem(privateKeyEntry.getCertificateChain()),
                ALIAS_IMPORTED)).isTrue();

        assertThat(mKeyChainService.isUserSelectable(ALIAS_IMPORTED)).isFalse();
        mTestSupportService.setUserSelectable(ALIAS_IMPORTED, true);
        assertThat(mKeyChainService.isUserSelectable(ALIAS_IMPORTED)).isTrue();
        mTestSupportService.setUserSelectable(ALIAS_IMPORTED, false);
        assertThat(mKeyChainService.isUserSelectable(ALIAS_IMPORTED)).isFalse();

        // Remove key
        assertThat(mTestSupportService.removeKeyPair(ALIAS_IMPORTED)).isTrue();
    }

    @Test
    public void testGenerateKeyPairErrorsOnBadUid() throws RemoteException {
        KeyGenParameterSpec specBadUid =
                new KeyGenParameterSpec.Builder(buildRsaKeySpec(ALIAS_GENERATED))
                .setUid(WIFI_UID)
                .build();
        ParcelableKeyGenParameterSpec parcelableSpec =
                new ParcelableKeyGenParameterSpec(specBadUid);
        assertThat(mTestSupportService.generateKeyPair("RSA", parcelableSpec)).isEqualTo(
                KeyChain.KEY_GEN_MISSING_ALIAS);
    }

    @Test
    public void testGenerateKeyPairErrorsOnSuperflousAttestationChallenge() throws RemoteException {
        KeyGenParameterSpec specWithChallenge =
                new KeyGenParameterSpec.Builder(buildRsaKeySpec(ALIAS_GENERATED))
                        .setAttestationChallenge(DUMMY_CHALLENGE)
                        .build();
        ParcelableKeyGenParameterSpec parcelableSpec =
                new ParcelableKeyGenParameterSpec(specWithChallenge);
        assertThat(mTestSupportService.generateKeyPair("RSA", parcelableSpec)).isEqualTo(
                KeyChain.KEY_GEN_SUPERFLUOUS_ATTESTATION_CHALLENGE);
    }

    @Test
    public void testGenerateKeyPairErrorsOnInvalidAlgorithm() throws RemoteException {
        ParcelableKeyGenParameterSpec parcelableSpec = new ParcelableKeyGenParameterSpec(
                buildRsaKeySpec(ALIAS_GENERATED));
        assertThat(mTestSupportService.generateKeyPair("BADBAD", parcelableSpec)).isEqualTo(
                KeyChain.KEY_GEN_NO_SUCH_ALGORITHM);
    }

    @Test
    public void testGenerateKeyPairErrorsOnInvalidAlgorithmParameters() throws RemoteException {
        ParcelableKeyGenParameterSpec parcelableSpec = new ParcelableKeyGenParameterSpec(
                buildRsaKeySpec(ALIAS_GENERATED));
        // RSA key parameters do not make sense for Elliptic Curve
        assertThat(mTestSupportService.generateKeyPair("EC", parcelableSpec)).isEqualTo(
                KeyChain.KEY_GEN_INVALID_ALGORITHM_PARAMETERS);
    }

    @Test
    public void testGenerateKeyPairSucceeds() throws RemoteException {
        generateRsaKey(ALIAS_GENERATED);
        // Test that there are no grants by default
        assertThat(mKeyChainService.requestPrivateKey(ALIAS_GENERATED)).isNull();
        // And is not user-selectable by default
        assertThat(mKeyChainService.isUserSelectable(ALIAS_GENERATED)).isFalse();
        // But after granting access, it can be used.
        mTestSupportService.grantAppPermission(Process.myUid(), ALIAS_GENERATED);
        assertThat(mKeyChainService.requestPrivateKey(ALIAS_GENERATED)).isNotNull();
    }

    @Test
    public void testAttestKeyFailsOnMissingChallenge() throws RemoteException {
        generateRsaKey(ALIAS_GENERATED);
        assertThat(mTestSupportService.attestKey(ALIAS_GENERATED, null, new int[]{}
                )).isEqualTo(KeyChain.KEY_ATTESTATION_MISSING_CHALLENGE);
    }

    @Test
    public void testAttestKeyFailsOnNonExistentKey() throws RemoteException {
        assertThat(mTestSupportService.attestKey(ALIAS_NON_EXISTING, DUMMY_CHALLENGE, new int[]{}
                )).isEqualTo(KeyChain.KEY_ATTESTATION_FAILURE);
    }

    @Test
    public void testAttestKeySucceedsOnGeneratedKey() throws RemoteException {
        generateRsaKey(ALIAS_GENERATED);
        assertThat(mTestSupportService.attestKey(ALIAS_GENERATED, DUMMY_CHALLENGE,
                new int[]{})).isEqualTo(KeyChain.KEY_ATTESTATION_SUCCESS);
    }

    @Test
    public void testSetKeyPairCertificate() throws RemoteException {
        generateRsaKey(ALIAS_GENERATED);
        final byte[] userCert = new byte[] {'a', 'b', 'c'};
        final byte[] certChain = new byte[] {'d', 'e', 'f'};

        assertThat(mTestSupportService.setKeyPairCertificate(ALIAS_GENERATED, userCert,
                certChain)).isTrue();
        mTestSupportService.grantAppPermission(Process.myUid(), ALIAS_GENERATED);

        assertThat(mKeyChainService.getCertificate(ALIAS_GENERATED)).isEqualTo(userCert);
        assertThat(mKeyChainService.getCaCertificates(ALIAS_GENERATED)).isEqualTo(certChain);

        final byte[] newUserCert = new byte[] {'x', 'y', 'z'};
        assertThat(mTestSupportService.setKeyPairCertificate(ALIAS_GENERATED, newUserCert,
                null)).isTrue();
        assertThat(mKeyChainService.getCertificate(ALIAS_GENERATED)).isEqualTo(newUserCert);
        assertThat(mKeyChainService.getCaCertificates(ALIAS_GENERATED)).isNull();
    }

    void generateRsaKey(String alias) throws RemoteException {
        ParcelableKeyGenParameterSpec parcelableSpec = new ParcelableKeyGenParameterSpec(
                buildRsaKeySpec(alias));
        assertThat(mTestSupportService.generateKeyPair("RSA", parcelableSpec)).isEqualTo(
                KeyChain.KEY_GEN_SUCCESS);
    }

    void removeKeyPair(String alias) throws RemoteException {
        assertThat(mTestSupportService.removeKeyPair(alias)).isTrue();
    }

    void bindTestSupportService() {
        Intent serviceIntent = new Intent(mContext, IKeyChainServiceTestSupport.class);
        serviceIntent.setComponent(
                new ComponentName(
                        "com.android.keychain.tests.support",
                        "com.android.keychain.tests.support.KeyChainServiceTestSupport"));
        Log.d(TAG, String.format("Binding intent: %s", serviceIntent));
        mIsSupportServiceBound =
                mContext.bindService(serviceIntent, mSupportConnection, Context.BIND_AUTO_CREATE);
        Log.d(TAG, String.format("Support service binding result: %b", mIsSupportServiceBound));
    }

    void unbindTestSupportService() {
        if (mIsSupportServiceBound) {
            mContext.unbindService(mSupportConnection);
            mIsSupportServiceBound = false;
        }
    }

    void bindKeyChainService() {
        Context appContext = mContext.getApplicationContext();
        Intent intent = new Intent(IKeyChainService.class.getName());
        ComponentName comp = intent.resolveSystemService(appContext.getPackageManager(), 0);
        intent.setComponent(comp);

        Log.d(TAG, String.format("Binding to KeyChain: %s", intent));
        mIsKeyChainServiceBound =
                appContext.bindServiceAsUser(
                        intent,
                        mServiceConnection,
                        Context.BIND_AUTO_CREATE,
                        Process.myUserHandle());
        Log.d(TAG, String.format("KeyChain service binding result: %b", mIsKeyChainServiceBound));
    }

    void unbindKeyChainService() {
        if (mIsKeyChainServiceBound) {
            mContext.getApplicationContext().unbindService(mServiceConnection);
            mIsKeyChainServiceBound = false;
        }
    }

    void installFirstKey() throws RemoteException, IOException, CertificateException {
        String intermediate = "-intermediate";
        String root = "-root";

        String alias1PrivateKey = Credentials.USER_PRIVATE_KEY + ALIAS_1;
        String alias1ClientCert = Credentials.USER_CERTIFICATE + ALIAS_1;
        String alias1IntermediateCert = (Credentials.CA_CERTIFICATE + ALIAS_1 + intermediate);
        String alias1RootCert = (Credentials.CA_CERTIFICATE + ALIAS_1 + root);
        PrivateKeyEntry privateKeyEntry =
                TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA");
        Certificate intermediate1 = privateKeyEntry.getCertificateChain()[1];
        Certificate root1 = TestKeyStore.getClientCertificate().getRootCertificate("RSA");

        assertThat(
                mTestSupportService.keystoreImportKey(
                    alias1PrivateKey, privateKeyEntry.getPrivateKey().getEncoded()))
            .isTrue();
        assertThat(
                mTestSupportService.keystorePut(
                    alias1ClientCert,
                    Credentials.convertToPem(privateKeyEntry.getCertificate())))
            .isTrue();
        assertThat(
                mTestSupportService.keystorePut(
                    alias1IntermediateCert, Credentials.convertToPem(intermediate1)))
            .isTrue();
        assertThat(
                mTestSupportService.keystorePut(alias1RootCert, Credentials.convertToPem(root1)))
            .isTrue();
    }

    void waitForSupportService() {
        Log.d(TAG, "Waiting for support service.");
        assertThat(mSupportServiceAvailable.block(10000)).isTrue();;
        assertThat(mTestSupportService).isNotNull();
    }

    void waitForKeyChainService() {
        Log.d(TAG, "Waiting for keychain service.");
        assertThat(mKeyChainAvailable.block(10000)).isTrue();;
        assertThat(mKeyChainService).isNotNull();
    }

    private KeyGenParameterSpec buildRsaKeySpec(String alias) {
        return new KeyGenParameterSpec.Builder(
                alias,
                KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
                .setKeySize(2048)
                .setDigests(KeyProperties.DIGEST_SHA256)
                .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PSS,
                        KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
                .setIsStrongBoxBacked(false)
                .build();
    }
}