2 * Copyright (c) 2013 TRUSTONIC LIMITED
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
32 package com
.gd
.mobicore
.pa
.service
;
34 import android
.app
.Service
;
35 import android
.content
.Intent
;
36 import android
.os
.IBinder
;
37 import android
.os
.Bundle
;
39 import java
.util
.Random
;
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
;
49 public class DeveloperService
extends BaseService
{
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
{
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;
71 private CommonPAWrapper
commonPAWrapper(){
72 return DeveloperService
.this.commonPAWrapper();
75 private boolean uuidOk(byte[] uuid
){
76 if(uuid
==null || uuid
.length
!= UUID_LENGTH
){
77 Log
.e(TAG
,"DeveloperService.Stub.uuidOk NOK");
80 Log
.d(TAG
,"DeveloperService.Stub.uuidOk OK");
84 public CommandResult
installTrustletOrKey(int spid
, byte[] uuid
, byte[] trustletBinary
, byte[] key
, int minTltVersion
, byte[] tltPukHash
){
85 Log
.d(TAG
,">>DeveloperService.Stub.installTrustletOrKey");
87 tltPukHash
=DEFAULT_PUKHASH
;
90 if((trustletBinary
== null && key
== null) || (trustletBinary
!= null && key
!= null) || 0==spid
|| !uuidOk(uuid
) ){
91 return new CommandResult(CommandResult
.ROOTPA_ERROR_ILLEGAL_ARGUMENT
);
94 int tmpSuid
=DEVELOPER_UID_FOR_LOCK
+new Random().nextInt();
96 if(!DeveloperService
.this.acquireLock(tmpSuid
, false).isOk()){
97 return new CommandResult(CommandResult
.ROOTPA_ERROR_LOCK
);
99 doProvisioningLockSuid_
=tmpSuid
;
104 if(trustletBinary
!= null){
106 dataType
=REQUEST_DATA_TLT
;
109 dataType
=REQUEST_DATA_KEY
;
112 err
=commonPAWrapper().installTrustlet(spid
,
119 DEFAULT_NUMBER_OF_INSTANCES
,
123 Log
.e(TAG
,"CommonPAWrapper().installTrustletOrKey exception: ", e
);
124 err
=CommandResult
.ROOTPA_ERROR_INTERNAL
;
127 Log
.d(TAG
,"<<DeveloperService.Stub.installTrustletOrKey");
128 return new CommandResult(err
);
131 public CommandResult
installTrustlet(int spid
,
133 byte[] trustletBinary
,
137 int numberOfInstances
,
139 Log
.d(TAG
,">>DeveloperService.Stub.installTrustlet");
140 if(tltPukHash
==null){
141 tltPukHash
=DEFAULT_PUKHASH
;
145 if(trustletBinary
== null || 0==spid
|| !uuidOk(uuid
) || memoryType
> EXTERNAL_MEMORY
){
146 return new CommandResult(CommandResult
.ROOTPA_ERROR_ILLEGAL_ARGUMENT
);
149 int tmpSuid
=DEVELOPER_UID_FOR_LOCK
+new Random().nextInt();
151 if(!DeveloperService
.this.acquireLock(tmpSuid
, false).isOk()){
152 return new CommandResult(CommandResult
.ROOTPA_ERROR_LOCK
);
154 doProvisioningLockSuid_
=tmpSuid
;
158 err
=commonPAWrapper().installTrustlet(spid
,
169 Log
.e(TAG
,"CommonPAWrapper().installTrustlet exception: ", e
);
170 err
=CommandResult
.ROOTPA_ERROR_INTERNAL
;
173 Log
.d(TAG
,"<<DeveloperService.Stub.installTrustlet");
174 return new CommandResult(err
);
179 public void onCreate() {
180 Log
.d(TAG
,"Hello, DeveloperService onCreate");
185 public void onLowMemory() {
186 Log
.d(TAG
,"DeveloperService onLowMemory");
190 public void onDestroy(){
192 Log
.d(TAG
,"DeveloperService being destroyed");
196 public IBinder
onBind(Intent intent
){
198 se_
= intent
.getByteArrayExtra("SE");
200 Log
.i(TAG
,"DeveloperService something wrong in the given ip "+e
);
204 Log
.setLoggingLevel(intent
.getIntExtra("LOG",0));
206 Log
.i(TAG
,"DeveloperService something wrong in the given logging level "+e
);
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_
));
215 public int onStartCommand(Intent i
, int flags
, int startid
){
216 Log
.d(TAG
,"DeveloperService starting");