blob: 2d81f9ca70f8628e56a4028309c1bbe3362112ae [file] [log] [blame]
Chad Brubaker40a1a9b2015-02-20 14:08:13 -08001/*
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
Shawn Willden0329a822017-12-04 13:55:14 -070020#include <map>
21#include <vector>
22
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080023#include <binder/Binder.h>
24#include <binder/IBinder.h>
Shawn Willdeneedcfe92018-01-18 15:35:46 -070025#include <keymasterV4_0/Keymaster.h>
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080026#include <utils/StrongPointer.h>
Shawn Willden0329a822017-12-04 13:55:14 -070027
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070028#include <keystore/keymaster_types.h>
Max Bires33aac2d2018-02-23 10:53:10 -080029#include <keystore/keystore_hidl_support.h>
30
31#include "operation_proto_handler.h"
32#include "operation_struct.h"
Shawn Willden0329a822017-12-04 13:55:14 -070033
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010034namespace keystore {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080035
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010036using ::android::IBinder;
37using ::android::sp;
Shawn Willdeneedcfe92018-01-18 15:35:46 -070038using keymaster::support::Keymaster;
Chad Brubakerad6514a2015-04-09 14:00:26 -070039
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080040/**
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010041 * 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.
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080045 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010046
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080047class OperationMap {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010048 public:
Shawn Willdenda6dcc32017-12-03 14:56:05 -070049 explicit OperationMap(IBinder::DeathRecipient* deathRecipient);
50 sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose,
Shawn Willden0329a822017-12-04 13:55:14 -070051 const sp<Keymaster>& dev, const sp<IBinder>& appToken,
Max Bires33aac2d2018-02-23 10:53:10 -080052 KeyCharacteristics&& characteristics,
53 const hidl_vec<KeyParameter>& params, bool pruneable);
Shawn Willdenda6dcc32017-12-03 14:56:05 -070054 NullOr<const Operation&> getOperation(const sp<IBinder>& token);
Max Bires33aac2d2018-02-23 10:53:10 -080055 NullOr<Operation> removeOperation(const sp<IBinder>& token, bool wasSuccessful);
Shawn Willdenda6dcc32017-12-03 14:56:05 -070056 bool hasPruneableOperation() const;
57 size_t getOperationCount() const { return mMap.size(); }
58 size_t getPruneableOperationCount() const;
59 bool setOperationAuthToken(const sp<IBinder>& token, HardwareAuthToken authToken);
60 sp<IBinder> getOldestPruneableOperation();
61 std::vector<sp<IBinder>> getOperationsForToken(const sp<IBinder>& appToken);
62
63 private:
64 void updateLru(const sp<IBinder>& token);
65 void removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken);
66 std::map<sp<IBinder>, Operation> mMap;
67 std::vector<sp<IBinder>> mLru;
68 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
69 IBinder::DeathRecipient* mDeathRecipient;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080070};
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010071
72} // namespace keystore
73
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080074#endif