blob: e0865a40a34032577983907f08c92515643c4608 [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
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070020#include <list>
Shawn Willden0329a822017-12-04 13:55:14 -070021#include <map>
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070022#include <memory>
23#include <mutex>
24#include <optional>
Shawn Willden0329a822017-12-04 13:55:14 -070025#include <vector>
26
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080027#include <binder/Binder.h>
28#include <binder/IBinder.h>
Shawn Willdeneedcfe92018-01-18 15:35:46 -070029#include <keymasterV4_0/Keymaster.h>
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080030#include <utils/StrongPointer.h>
Shawn Willden0329a822017-12-04 13:55:14 -070031
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070032#include <keystore/keymaster_types.h>
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070033#include <keystore/keystore_concurrency.h>
Max Bires33aac2d2018-02-23 10:53:10 -080034#include <keystore/keystore_hidl_support.h>
35
36#include "operation_proto_handler.h"
37#include "operation_struct.h"
Shawn Willden0329a822017-12-04 13:55:14 -070038
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010039namespace keystore {
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080040
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010041using ::android::IBinder;
42using ::android::sp;
Shawn Willdeneedcfe92018-01-18 15:35:46 -070043using keymaster::support::Keymaster;
Chad Brubakerad6514a2015-04-09 14:00:26 -070044
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080045/**
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010046 * OperationMap handles the translation of uint64_t's and keymaster2_device_t's to opaque binder
47 * tokens that can be used to reference that operation at a later time by applications. It also does
48 * LRU tracking for operation pruning and keeps a mapping of clients to operations to allow for
49 * graceful handling of application death.
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080050 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010051
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080052class OperationMap {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010053 public:
Shawn Willdenda6dcc32017-12-03 14:56:05 -070054 explicit OperationMap(IBinder::DeathRecipient* deathRecipient);
55 sp<IBinder> addOperation(uint64_t handle, uint64_t keyid, KeyPurpose purpose,
Shawn Willden0329a822017-12-04 13:55:14 -070056 const sp<Keymaster>& dev, const sp<IBinder>& appToken,
Max Bires33aac2d2018-02-23 10:53:10 -080057 KeyCharacteristics&& characteristics,
58 const hidl_vec<KeyParameter>& params, bool pruneable);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070059 std::shared_ptr<Operation> getOperation(const sp<IBinder>& token);
60 std::shared_ptr<Operation> removeOperation(const sp<IBinder>& token, bool wasSuccessful);
Shawn Willdenda6dcc32017-12-03 14:56:05 -070061 size_t getOperationCount() const { return mMap.size(); }
Shawn Willdenda6dcc32017-12-03 14:56:05 -070062 sp<IBinder> getOldestPruneableOperation();
63 std::vector<sp<IBinder>> getOperationsForToken(const sp<IBinder>& appToken);
64
65 private:
66 void updateLru(const sp<IBinder>& token);
67 void removeOperationTracking(const sp<IBinder>& token, const sp<IBinder>& appToken);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070068
69 std::map<sp<IBinder>, std::shared_ptr<Operation>> mMap;
70 std::list<sp<IBinder>> mLru;
Shawn Willdenda6dcc32017-12-03 14:56:05 -070071 std::map<sp<IBinder>, std::vector<sp<IBinder>>> mAppTokenMap;
72 IBinder::DeathRecipient* mDeathRecipient;
Max Bires091ed1b2018-12-05 12:13:30 -080073 OperationProtoHandler operationUploader;
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080074};
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010075
76} // namespace keystore
77
Chad Brubaker40a1a9b2015-02-20 14:08:13 -080078#endif