Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2020 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 com.android.remoteprovisioner; |
| 18 | |
| 19 | import android.app.job.JobParameters; |
| 20 | import android.app.job.JobService; |
Max Bires | 0dbe39c | 2021-02-07 18:39:00 -0800 | [diff] [blame] | 21 | import android.hardware.security.keymint.SecurityLevel; |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | import android.security.remoteprovisioning.AttestationPoolStatus; |
| 25 | import android.security.remoteprovisioning.IRemoteProvisioning; |
| 26 | import android.util.Log; |
| 27 | |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 28 | /** |
| 29 | * A class that extends JobService in order to be scheduled to check the status of the attestation |
| 30 | * key pool at regular intervals. If the job determines that more keys need to be generated and |
| 31 | * signed, it drives that process. |
| 32 | */ |
| 33 | public class PeriodicProvisioner extends JobService { |
| 34 | //TODO(b/176249146): Replace default key amount with a value fetched from the server. |
| 35 | private static final int TOTAL_SIGNED_KEYS = 10; |
| 36 | |
| 37 | private static final String SERVICE = "android.security.remoteprovisioning"; |
| 38 | private static final String TAG = "RemoteProvisioningService"; |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 39 | private ProvisionerThread mProvisionerThread; |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 40 | |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 41 | /** |
| 42 | * Starts the periodic provisioning job, which will occasionally check the attestation key pool |
| 43 | * and provision it as necessary. |
| 44 | */ |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 45 | public boolean onStartJob(JobParameters params) { |
| 46 | Log.d(TAG, "Starting provisioning job"); |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 47 | mProvisionerThread = new ProvisionerThread(params); |
| 48 | mProvisionerThread.start(); |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 52 | /** |
| 53 | * Allows the job to be stopped if need be. |
| 54 | */ |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 55 | public boolean onStopJob(JobParameters params) { |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 56 | mProvisionerThread.stop(); |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | private class ProvisionerThread extends Thread { |
| 61 | private JobParameters mParams; |
| 62 | |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 63 | ProvisionerThread(JobParameters params) { |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 64 | mParams = params; |
| 65 | } |
| 66 | |
| 67 | public void run() { |
| 68 | try { |
| 69 | IRemoteProvisioning binder = |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 70 | IRemoteProvisioning.Stub.asInterface(ServiceManager.getService(SERVICE)); |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 71 | // TODO: Replace expiration date parameter with value fetched from server |
Max Bires | 0dbe39c | 2021-02-07 18:39:00 -0800 | [diff] [blame] | 72 | checkAndProvision(binder, 1, SecurityLevel.TRUSTED_ENVIRONMENT); |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 73 | jobFinished(mParams, false /* wantsReschedule */); |
| 74 | } catch (RemoteException e) { |
| 75 | jobFinished(mParams, true /* wantsReschedule */); |
Max Bires | 0dbe39c | 2021-02-07 18:39:00 -0800 | [diff] [blame] | 76 | Log.e(TAG, "Error on the binder side during provisioning.", e); |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 77 | } catch (InterruptedException e) { |
| 78 | jobFinished(mParams, true /* wantsReschedule */); |
| 79 | Log.e(TAG, "Provisioner thread interrupted.", e); |
| 80 | } |
| 81 | } |
Max Bires | 0dbe39c | 2021-02-07 18:39:00 -0800 | [diff] [blame] | 82 | |
| 83 | private void checkAndProvision(IRemoteProvisioning binder, long expiringBy, int secLevel) |
| 84 | throws InterruptedException, RemoteException { |
Max Bires | feff69a | 2021-02-08 21:28:37 -0800 | [diff] [blame^] | 85 | AttestationPoolStatus pool = binder.getPoolStatus(expiringBy, secLevel); |
Max Bires | 0dbe39c | 2021-02-07 18:39:00 -0800 | [diff] [blame] | 86 | int generated = 0; |
| 87 | while (generated + pool.total - pool.expiring < TOTAL_SIGNED_KEYS) { |
| 88 | generated++; |
| 89 | binder.generateKeyPair(false /* isTestMode */, secLevel); |
| 90 | Thread.sleep(5000); |
| 91 | } |
| 92 | if (generated > 0) { |
| 93 | Log.d(TAG, "Keys generated, moving to provisioning process."); |
| 94 | Provisioner.provisionCerts(generated, secLevel, binder); |
| 95 | } |
| 96 | } |
Max Bires | e24b782 | 2020-12-03 14:49:58 -0800 | [diff] [blame] | 97 | } |
| 98 | } |