Home | History | Annotate | Download | only in keystore
      1 /*
      2  * Copyright (C) 2015 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 #ifndef KEYSTORE_OPERATION_H_
     18 #define KEYSTORE_OPERATION_H_
     19 
     20 #include <map>
     21 #include <vector>
     22 
     23 #include <binder/Binder.h>
     24 #include <binder/IBinder.h>
     25 #include <keymasterV4_0/Keymaster.h>
     26 #include <utils/StrongPointer.h>
     27 
     28 #include <keystore/keymaster_types.h>
     29 #include <keystore/keystore_hidl_support.h>
     30 
     31 #include "operation_proto_handler.h"
     32 #include "operation_struct.h"
     33 
     34 namespace keystore {
     35 
     36 using ::android::IBinder;
     37 using ::android::sp;
     38 using keymaster::support::Keymaster;
     39 
     40 /**
     41  * OperationMap handles the translation of uint64_t's and keymaster2_device_t's to opaque binder
     42  * tokens that can be used to reference that operation at a later time by applications. It also does
     43  * LRU tracking for operation pruning and keeps a mapping of clients to operations to allow for
     44  * graceful handling of application death.
     45  */
     46 
     47 class OperationMap {
     48   public:
     49     explicit OperationMap(IBinder::DeathRecipient* deathRecipient);
     50     sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose,
     51                              const sp<Keymaster>& dev, const sp<IBinder>& appToken,
     52                              KeyCharacteristics&& characteristics,
     53                              const hidl_vec<KeyParameter>& params, bool pruneable);
     54     NullOr<const Operation&> getOperation(const sp<IBinder>& token);
     55     NullOr<Operation> removeOperation(const sp<IBinder>& token, bool wasSuccessful);
     56     bool hasPruneableOperation() const;
     57     size_t getOperationCount() const { return mMap.size(); }
     58     size_t getPruneableOperationCount() const;
     59     void setOperationAuthToken(const sp<IBinder>& token, HardwareAuthToken authToken);
     60     void setOperationVerificationToken(const sp<IBinder>& token, VerificationToken authToken);
     61     sp<IBinder> getOldestPruneableOperation();
     62     std::vector<sp<IBinder>> getOperationsForToken(const sp<IBinder>& appToken);
     63 
     64   private:
     65     void updateLru(const sp<IBinder>& token);
     66     void removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken);
     67     std::map<sp<IBinder>, Operation> mMap;
     68     std::vector<sp<IBinder>> mLru;
     69     std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
     70     IBinder::DeathRecipient* mDeathRecipient;
     71 };
     72 
     73 }  // namespace keystore
     74 
     75 #endif
     76