1 /*
2  * Copyright (C) 2022 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.rkpdapp.service;
18 
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Binder;
23 import android.os.IBinder;
24 import android.os.Process;
25 import android.os.RemoteException;
26 import android.util.Log;
27 
28 import com.android.rkpdapp.IGetRegistrationCallback;
29 import com.android.rkpdapp.IRegistration;
30 import com.android.rkpdapp.IRemoteProvisioning;
31 import com.android.rkpdapp.ThreadPool;
32 import com.android.rkpdapp.database.ProvisionedKeyDao;
33 import com.android.rkpdapp.database.RkpdDatabase;
34 import com.android.rkpdapp.interfaces.ServerInterface;
35 import com.android.rkpdapp.interfaces.ServiceManagerInterface;
36 import com.android.rkpdapp.interfaces.SystemInterface;
37 import com.android.rkpdapp.metrics.RkpdClientOperation;
38 import com.android.rkpdapp.provisioner.Provisioner;
39 import com.android.rkpdapp.utils.Settings;
40 
41 /** Provides the implementation for IRemoteProvisioning.aidl */
42 public class RemoteProvisioningService extends Service {
43     public static final String TAG = "com.android.rkpdapp";
44     private static final boolean IS_ASYNC = false;
45     private final IRemoteProvisioning.Stub mBinder = new RemoteProvisioningBinder();
46 
47     @Override
onCreate()48     public void onCreate() {
49         super.onCreate();
50     }
51 
52     @Override
onBind(Intent intent)53     public IBinder onBind(Intent intent) {
54         return mBinder;
55     }
56 
57     final class RemoteProvisioningBinder extends IRemoteProvisioning.Stub {
58         @Override
getRegistration(int callerUid, String irpcName, IGetRegistrationCallback callback)59         public void getRegistration(int callerUid, String irpcName,
60                 IGetRegistrationCallback callback) {
61             final Context context = getApplicationContext();
62             RkpdClientOperation metric = RkpdClientOperation.getRegistration(callerUid, irpcName);
63             try (metric) {
64                 if (Settings.getDefaultUrl().isEmpty() || Settings.getUrl(context).isEmpty()) {
65                     callback.onError("RKP is disabled. System configured with no default URL.");
66                     metric.setResult(RkpdClientOperation.Result.RKP_UNSUPPORTED);
67                     return;
68                 }
69 
70                 // Check that only system process and self can bind.
71                 if (Binder.getCallingUid() != Process.SYSTEM_UID
72                         && Binder.getCallingUid() != Process.myUid()) {
73                     callback.onError(
74                             "Only system server and self are allowed to call RKP service.");
75                     return;
76                 }
77 
78                 SystemInterface systemInterface;
79                 try {
80                     systemInterface = ServiceManagerInterface.getInstance(irpcName);
81                 } catch (IllegalArgumentException e) {
82                     Log.e(TAG, "Error getting HAL '" + irpcName + "'", e);
83                     callback.onError("Invalid HAL name: " + irpcName);
84                     metric.setResult(RkpdClientOperation.Result.ERROR_INVALID_HAL);
85                     return;
86                 }
87 
88                 ProvisionedKeyDao dao = RkpdDatabase.getDatabase(context).provisionedKeyDao();
89                 Provisioner provisioner = new Provisioner(context, dao, IS_ASYNC);
90                 IRegistration.Stub registration = new RegistrationBinder(context, callerUid,
91                         systemInterface, dao, new ServerInterface(context, IS_ASYNC), provisioner,
92                         ThreadPool.EXECUTOR);
93                 metric.setResult(RkpdClientOperation.Result.SUCCESS);
94                 callback.onSuccess(registration);
95             } catch (RemoteException e) {
96                 Log.e(TAG, "Error notifying callback binder", e);
97                 metric.setResult(RkpdClientOperation.Result.ERROR_INTERNAL);
98                 throw e.rethrowAsRuntimeException();
99             }
100         }
101 
102         @Override
cancelGetRegistration(IGetRegistrationCallback callback)103         public void cancelGetRegistration(IGetRegistrationCallback callback) {
104             // Not actually supported on this end of the transaction, because we always
105             // complete, and there's no way to win the race.
106             Log.i(TAG, "cancelGetRegistration");
107         }
108     }
109 }
110