@@ -25,11 +25,16 @@ class SecurityLevelInterceptor(
2525 private val original : IKeystoreSecurityLevel , private val level : Int
2626) : BinderInterceptor() {
2727 companion object {
28- private val generateKeyTransaction = getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " generateKey" )
29- private val deleteKeyTransaction = getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " deleteKey" )
30- private val createOperationTransaction = getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " createOperation" )
31- private val importWrappedKeyTransaction = getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " importWrappedKey" )
32- private val importKeyTransaction = getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " importKey" )
28+ private val createOperationTransaction =
29+ getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " createOperation" ) // 1
30+ private val generateKeyTransaction =
31+ getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " generateKey" ) // 2
32+ private val importKeyTransaction =
33+ getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " importKey" ) // 3
34+ private val importWrappedKeyTransaction =
35+ getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " importWrappedKey" ) // 4
36+ private val deleteKeyTransaction =
37+ getTransactCode(IKeystoreSecurityLevel .Stub ::class .java, " deleteKey" ) // 6
3338
3439 }
3540 override fun onPreTransact (
@@ -114,23 +119,30 @@ class SecurityLevelInterceptor(
114119 val params = data.createTypedArray(KeyParameter .CREATOR ) ? : return Skip
115120 val kgp = CertHack .KeyGenParameters (params)
116121
117- val info = Cache .getInfoByNspace(keyDescriptor.nspace)
118- if (info == null || (info.key.uid != callingUid)) {
119- Logger .e(" key not found or uid mismatch" )
120- return Skip
121- }
122122 if (keyDescriptor.domain != 4 ) throw IllegalArgumentException (" unsupported domain ${keyDescriptor.domain} " )
123123 kgp.purpose.any { it != 2 /* sign */ && it != 7 /* attest */ } ||
124124 throw IllegalArgumentException (" unsupported purpose ${kgp.purpose} " )
125- kgp.digest.any { it != 4 } ||
126- throw IllegalArgumentException (" unsupported digest ${kgp.digest} " )
127125 val algorithm = when (kgp.algorithm) {
128- Algorithm .EC -> " SHA256withECDSA "
129- Algorithm .RSA -> " SHA256withRSA "
126+ Algorithm .EC -> " ECDSA "
127+ Algorithm .RSA -> " RSA "
130128 else -> throw IllegalArgumentException (" unsupported algorithm ${kgp.algorithm} " )
131129 }
132130
133- val op = KeyStoreOperation (info.keyPair.private, algorithm)
131+ val infos = Cache .getInfoByNspace(callingUid, keyDescriptor.nspace)
132+ if (infos.isEmpty()) {
133+ Logger .e(" key not found" )
134+ return Skip
135+ }
136+ infos.filter { it.response.metadata.key.alias == keyDescriptor.alias }.let {
137+ it.forEach {
138+ Logger .d(" found key alias=${it.key.alias} uid=${it.key.uid} actual=${it.response.metadata.key.alias} " )
139+ Logger .d(" createOperation" , it.chain.first().toString())
140+ }
141+ }
142+ Logger .d(" found keys number: ${infos.size} " )
143+ val info = infos.first { it.response.metadata.key.alias == keyDescriptor.alias && it.keyPair.private.algorithm == algorithm }
144+ Logger .d(" createOperation" , info.chain.first().toString())
145+ val op = KeyStoreOperation (info.keyPair.private, " SHA256with$algorithm " )
134146 val parcel = Parcel .obtain()
135147 parcel.writeNoException()
136148 val createOperationResponse = CreateOperationResponse ().apply {
@@ -151,27 +163,32 @@ class SecurityLevelInterceptor(
151163 var isAborted = false
152164
153165 constructor (privateKey: PrivateKey , algorithm: String ) {
166+ Logger .d(" KeyStoreOperation using algorithm $algorithm , privateKey=${privateKey.algorithm} " )
154167 signature = Signature .getInstance(algorithm)
155168 signature.initSign(privateKey)
156169 }
157170
158171 override fun updateAad (aadInput : ByteArray? ) {
159172 // do nothing for now
173+ Logger .d(" updateAad called, ignored" )
160174 }
161175
162176 override fun update (input : ByteArray ): ByteArray? {
163177 if (isAborted) throw IllegalStateException (" operation aborted" )
178+ Logger .d(" update called with ${input.size} bytes" )
164179 signature.update(input)
165180 return null
166181 }
167182
168183 override fun finish (input : ByteArray? , signature : ByteArray? ): ByteArray? {
169184 if (isAborted) throw IllegalStateException (" operation aborted" )
185+ Logger .d(" finish called with ${input?.size ? : 0 } bytes" )
170186 this .signature.update(input)
171187 return this .signature.sign()
172188 }
173189
174190 override fun abort () {
191+ Logger .d(" abort called" )
175192 isAborted = true
176193 }
177194 }
0 commit comments