1 /*
2 **
3 ** Copyright 2018, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef KEYSTORE_INCLUDE_KEYSTORE_KEYSTORE_PROMISES_H_
19 #define KEYSTORE_INCLUDE_KEYSTORE_KEYSTORE_PROMISES_H_
20 
21 #include <android/security/keystore/BnKeystoreCertificateChainCallback.h>
22 #include <android/security/keystore/BnKeystoreExportKeyCallback.h>
23 #include <android/security/keystore/BnKeystoreKeyCharacteristicsCallback.h>
24 #include <android/security/keystore/BnKeystoreOperationResultCallback.h>
25 #include <android/security/keystore/BnKeystoreResponseCallback.h>
26 #include <future>
27 
28 namespace keystore {
29 
30 template <typename BnInterface, typename Result>
31 class CallbackPromise : public BnInterface, public std::promise<Result> {
32   public:
onFinished(const Result & result)33     ::android::binder::Status onFinished(const Result& result) override {
34         this->set_value(result);
35         return ::android::binder::Status::ok();
36     }
37 };
38 
39 template <typename BnInterface, typename... Results>
40 class CallbackPromise<BnInterface, std::tuple<Results...>>
41     : public BnInterface, public std::promise<std::tuple<Results...>> {
42   public:
onFinished(const Results &...results)43     ::android::binder::Status onFinished(const Results&... results) override {
44         this->set_value({results...});
45         return ::android::binder::Status::ok();
46     }
47 };
48 
49 using OperationResultPromise =
50     CallbackPromise<::android::security::keystore::BnKeystoreOperationResultCallback,
51                     ::android::security::keymaster::OperationResult>;
52 
53 using KeystoreResponsePromise =
54     CallbackPromise<::android::security::keystore::BnKeystoreResponseCallback,
55                     ::android::security::keystore::KeystoreResponse>;
56 
57 using KeyCharacteristicsPromise =
58     CallbackPromise<::android::security::keystore::BnKeystoreKeyCharacteristicsCallback,
59                     std::tuple<::android::security::keystore::KeystoreResponse,
60                                ::android::security::keymaster::KeyCharacteristics>>;
61 using KeystoreExportPromise =
62     CallbackPromise<::android::security::keystore::BnKeystoreExportKeyCallback,
63                     ::android::security::keymaster::ExportResult>;
64 
65 using KeyCertChainPromise =
66     CallbackPromise<::android::security::keystore::BnKeystoreCertificateChainCallback,
67                     std::tuple<::android::security::keystore::KeystoreResponse,
68                                ::android::security::keymaster::KeymasterCertificateChain>>;
69 
70 }  // namespace keystore
71 
72 #endif  // KEYSTORE_INCLUDE_KEYSTORE_KEYSTORE_PROMISES_H_
73