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.service; 18 19 import android.app.Service; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.IBinder; 23 import android.os.RemoteException; 24 import android.os.ServiceManager; 25 import android.security.IGenerateRkpKeyService; 26 import android.security.remoteprovisioning.AttestationPoolStatus; 27 import android.security.remoteprovisioning.ImplInfo; 28 import android.security.remoteprovisioning.IRemoteProvisioning; 29 import android.util.Log; 30 31 import com.android.remoteprovisioner.GeekResponse; 32 import com.android.remoteprovisioner.Provisioner; 33 import com.android.remoteprovisioner.ServerInterface; 34 import com.android.remoteprovisioner.SettingsManager; 35 36 /** 37 * Provides the implementation for IGenerateKeyService.aidl 38 */ 39 public class GenerateRkpKeyService extends Service { 40 private static final int KEY_GENERATION_PAUSE_MS = 1000; 41 private static final String SERVICE = "android.security.remoteprovisioning"; 42 private static final String TAG = "RemoteProvisioningService"; 43 44 @Override onCreate()45 public void onCreate() { 46 super.onCreate(); 47 } 48 49 @Override onBind(Intent intent)50 public IBinder onBind(Intent intent) { 51 return mBinder; 52 } 53 54 private final IGenerateRkpKeyService.Stub mBinder = new IGenerateRkpKeyService.Stub() { 55 @Override 56 public void generateKey(int securityLevel) { 57 try { 58 Log.i(TAG, "generateKey ping for secLevel: " + securityLevel); 59 IRemoteProvisioning binder = 60 IRemoteProvisioning.Stub.asInterface(ServiceManager.getService(SERVICE)); 61 checkAndFillPool(binder, securityLevel); 62 } catch (RemoteException e) { 63 Log.e(TAG, "Remote Exception: ", e); 64 } 65 } 66 67 @Override 68 public void notifyKeyGenerated(int securityLevel) { 69 try { 70 Log.i(TAG, "Notify key generated ping for secLevel: " + securityLevel); 71 IRemoteProvisioning binder = 72 IRemoteProvisioning.Stub.asInterface(ServiceManager.getService(SERVICE)); 73 checkAndFillPool(binder, securityLevel); 74 } catch (RemoteException e) { 75 Log.e(TAG, "Remote Exception: ", e); 76 } 77 } 78 79 private void checkAndFillPool(IRemoteProvisioning binder, int secLevel) 80 throws RemoteException { 81 AttestationPoolStatus pool = 82 binder.getPoolStatus(System.currentTimeMillis(), secLevel); 83 ImplInfo[] implInfos = binder.getImplementationInfo(); 84 int curve = 0; 85 for (int i = 0; i < implInfos.length; i++) { 86 if (implInfos[i].secLevel == secLevel) { 87 curve = implInfos[i].supportedCurve; 88 break; 89 } 90 } 91 // If there are no unassigned keys, go ahead and provision some. If there are no 92 // attested keys at all on the system, this implies that it is a hybrid 93 // rkp/factory-provisioned system that has turned off RKP. In that case, do 94 // not provision. 95 if (pool.unassigned == 0 && pool.attested != 0) { 96 Log.i(TAG, "All signed keys are currently in use, provisioning more."); 97 Context context = getApplicationContext(); 98 int keysToProvision = SettingsManager.getExtraSignedKeysAvailable(context); 99 int existingUnsignedKeys = pool.total - pool.attested; 100 int keysToGenerate = keysToProvision - existingUnsignedKeys; 101 try { 102 for (int i = 0; i < keysToGenerate; i++) { 103 binder.generateKeyPair(false /* isTestMode */, secLevel); 104 Thread.sleep(KEY_GENERATION_PAUSE_MS); 105 } 106 } catch (InterruptedException e) { 107 Log.i(TAG, "Thread interrupted", e); 108 } 109 GeekResponse resp = ServerInterface.fetchGeek(context); 110 if (resp == null) { 111 Log.e(TAG, "Server unavailable"); 112 return; 113 } 114 Provisioner.provisionCerts(keysToProvision, secLevel, resp.getGeekChain(curve), 115 resp.getChallenge(), binder, context); 116 } 117 } 118 }; 119 } 120