b595469ce1da6c5d4797becfa67cade39faf0b55
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos7580.git] / mobicore / rootpa / Code / Android / app / src / com / gd / mobicore / pa / service / DeveloperService.java
1 /*
2 * Copyright (c) 2013 TRUSTONIC LIMITED
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 package com.gd.mobicore.pa.service;
33
34 import android.app.Service;
35 import android.content.Intent;
36 import android.os.IBinder;
37 import android.os.Bundle;
38
39 import java.util.Random;
40
41 import com.gd.mobicore.pa.jni.CommonPAWrapper;
42 import com.gd.mobicore.pa.ifc.RootPAProvisioningIntents;
43 import com.gd.mobicore.pa.ifc.RootPADeveloperIfc;
44 import com.gd.mobicore.pa.ifc.CommandResult;
45 import com.gd.mobicore.pa.ifc.BooleanResult;
46 import com.gd.mobicore.pa.ifc.TrustletContainer;
47 import com.gd.mobicore.pa.ifc.IfcVersion;
48
49 public class DeveloperService extends BaseService {
50
51 private final RootPADeveloperIfc.Stub mBinder = new ServiceIfc();
52 private static final int DEVELOPER_UID_FOR_LOCK=0x22220000;
53 private static final int UUID_LENGTH=16;
54 private static final int EXTERNAL_MEMORY=2;
55 private static final int DEFAULT_MEMORY_TYPE=EXTERNAL_MEMORY;
56 private static final int DEFAULT_NUMBER_OF_INSTANCES=1;
57 private static final int DEFAULT_FLAGS=0;
58 private static final byte[] DEFAULT_PUKHASH={0,0,0,0,0,0,0,0,0,0,
59 0,0,0,0,0,0,0,0,0,0,
60 0,0,0,0,0,0,0,0,0,0,0,0};
61 private class ServiceIfc extends RootPADeveloperIfc.Stub {
62 public ServiceIfc(){
63 super();
64 }
65
66 // note that these values have to be in line with TltInstallationRequestDataType in rootpa.h
67 public static final int REQUEST_DATA_TLT=1;
68 public static final int REQUEST_DATA_KEY=2;
69
70
71 private CommonPAWrapper commonPAWrapper(){
72 return DeveloperService.this.commonPAWrapper();
73 }
74
75 private boolean uuidOk(byte[] uuid){
76 if(uuid==null || uuid.length != UUID_LENGTH){
77 Log.e(TAG,"DeveloperService.Stub.uuidOk NOK");
78 return false;
79 }
80 Log.d(TAG,"DeveloperService.Stub.uuidOk OK");
81 return true;
82 }
83
84 public CommandResult installTrustletOrKey(int spid, byte[] uuid, byte[] trustletBinary, byte[] key, int minTltVersion, byte[] tltPukHash){
85 Log.d(TAG,">>DeveloperService.Stub.installTrustletOrKey");
86 if(tltPukHash==null){
87 tltPukHash=DEFAULT_PUKHASH;
88 }
89
90 if((trustletBinary == null && key == null) || (trustletBinary != null && key != null) || 0==spid || !uuidOk(uuid) ){
91 return new CommandResult(CommandResult.ROOTPA_ERROR_ILLEGAL_ARGUMENT);
92 }
93
94 int tmpSuid=DEVELOPER_UID_FOR_LOCK+new Random().nextInt();
95
96 if(!DeveloperService.this.acquireLock(tmpSuid, false).isOk()){
97 return new CommandResult(CommandResult.ROOTPA_ERROR_LOCK);
98 }
99 doProvisioningLockSuid_=tmpSuid;
100 int err=0;
101 byte[] data=null;
102 int dataType;
103 try{
104 if(trustletBinary != null){
105 data=trustletBinary;
106 dataType=REQUEST_DATA_TLT;
107 }else{
108 data=key;
109 dataType=REQUEST_DATA_KEY;
110 }
111 setupProxy();
112 err=commonPAWrapper().installTrustlet(spid,
113 uuid,
114 dataType,
115 data,
116 minTltVersion,
117 tltPukHash,
118 DEFAULT_MEMORY_TYPE,
119 DEFAULT_NUMBER_OF_INSTANCES,
120 DEFAULT_FLAGS,
121 se_);
122 }catch(Exception e){
123 Log.e(TAG,"CommonPAWrapper().installTrustletOrKey exception: ", e);
124 err=CommandResult.ROOTPA_ERROR_INTERNAL;
125 }
126
127 Log.d(TAG,"<<DeveloperService.Stub.installTrustletOrKey");
128 return new CommandResult(err);
129 }
130
131 public CommandResult installTrustlet(int spid,
132 byte[] uuid,
133 byte[] trustletBinary,
134 int minTltVersion,
135 byte[] tltPukHash,
136 int memoryType,
137 int numberOfInstances,
138 int flags){
139 Log.d(TAG,">>DeveloperService.Stub.installTrustlet");
140 if(tltPukHash==null){
141 tltPukHash=DEFAULT_PUKHASH;
142 }
143
144
145 if(trustletBinary == null || 0==spid || !uuidOk(uuid) || memoryType > EXTERNAL_MEMORY){
146 return new CommandResult(CommandResult.ROOTPA_ERROR_ILLEGAL_ARGUMENT);
147 }
148
149 int tmpSuid=DEVELOPER_UID_FOR_LOCK+new Random().nextInt();
150
151 if(!DeveloperService.this.acquireLock(tmpSuid, false).isOk()){
152 return new CommandResult(CommandResult.ROOTPA_ERROR_LOCK);
153 }
154 doProvisioningLockSuid_=tmpSuid;
155 int err=0;
156 try{
157 setupProxy();
158 err=commonPAWrapper().installTrustlet(spid,
159 uuid,
160 REQUEST_DATA_TLT,
161 trustletBinary,
162 minTltVersion,
163 tltPukHash,
164 memoryType,
165 numberOfInstances,
166 flags,
167 se_);
168 }catch(Exception e){
169 Log.e(TAG,"CommonPAWrapper().installTrustlet exception: ", e);
170 err=CommandResult.ROOTPA_ERROR_INTERNAL;
171 }
172
173 Log.d(TAG,"<<DeveloperService.Stub.installTrustlet");
174 return new CommandResult(err);
175 }
176 }
177
178 @Override
179 public void onCreate() {
180 Log.d(TAG,"Hello, DeveloperService onCreate");
181 super.onCreate();
182 }
183
184 @Override
185 public void onLowMemory() {
186 Log.d(TAG,"DeveloperService onLowMemory");
187 super.onLowMemory();
188 }
189
190 public void onDestroy(){
191 super.onDestroy();
192 Log.d(TAG,"DeveloperService being destroyed");
193 }
194
195 @Override
196 public IBinder onBind(Intent intent){
197 try{
198 se_ = intent.getByteArrayExtra("SE");
199 }catch(Exception e){
200 Log.i(TAG,"DeveloperService something wrong in the given ip "+e );
201 }
202
203 try{
204 Log.setLoggingLevel(intent.getIntExtra("LOG",0));
205 }catch(Exception e){
206 Log.i(TAG,"DeveloperService something wrong in the given logging level "+e );
207 }
208 Log.i(TAG,"DeveloperService binding, IfcVersion: " +IfcVersion.ROOTPA_ANDROID_API_VERSION_MAJOR+"."+IfcVersion.ROOTPA_ANDROID_API_VERSION_MINOR);
209 if(se_!=null) Log.d(TAG,new String(se_));
210 return mBinder;
211
212 }
213
214 @Override
215 public int onStartCommand(Intent i, int flags, int startid){
216 Log.d(TAG,"DeveloperService starting");
217 return START_STICKY;
218 }
219 }
220
221