blob: 00ae66ab0f87e433b35c5af27c9da9e1650265a5 [file] [log] [blame]
Edman Anjos9103e272019-04-15 18:32:47 +02001// Copyright 2019 The Chromium OS Authors. All rights reserved.
Edman Anjos80c7b002018-11-16 15:01:24 +01002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef ARC_KEYMASTER_KEYMASTER_SERVER_H_
6#define ARC_KEYMASTER_KEYMASTER_SERVER_H_
7
Edman Anjosebbef002020-05-15 17:20:20 +02008#include <memory>
Edman Anjos9103e272019-04-15 18:32:47 +02009#include <vector>
10
Edman Anjosebbef002020-05-15 17:20:20 +020011#include <base/location.h>
Edman Anjos9103e272019-04-15 18:32:47 +020012#include <base/macros.h>
Edman Anjos44934ea2020-02-10 16:35:26 +010013#include <base/memory/scoped_refptr.h>
Edman Anjosebbef002020-05-15 17:20:20 +020014#include <base/threading/thread.h>
Edman Anjos9103e272019-04-15 18:32:47 +020015#include <keymaster/android_keymaster.h>
Edman Anjos9103e272019-04-15 18:32:47 +020016#include <mojo/keymaster.mojom.h>
Edman Anjos80c7b002018-11-16 15:01:24 +010017
Edman Anjos44934ea2020-02-10 16:35:26 +010018#include "arc/keymaster/context/arc_keymaster_context.h"
19
Edman Anjos80c7b002018-11-16 15:01:24 +010020namespace arc {
21namespace keymaster {
22
Edman Anjos9103e272019-04-15 18:32:47 +020023// KeymasterServer is a Mojo implementation of the Keymaster 3 HIDL interface.
Edman Anjosebbef002020-05-15 17:20:20 +020024// It fulfills requests using the reference Android Keymaster implementation.
Edman Anjos9103e272019-04-15 18:32:47 +020025class KeymasterServer : public arc::mojom::KeymasterServer {
26 public:
Edman Anjosebbef002020-05-15 17:20:20 +020027 KeymasterServer();
28 // Not copyable nor assignable.
29 KeymasterServer(const KeymasterServer&) = delete;
30 KeymasterServer& operator=(const KeymasterServer&) = delete;
31 ~KeymasterServer() override;
Edman Anjos9103e272019-04-15 18:32:47 +020032
33 void SetSystemVersion(uint32_t osVersion, uint32_t osPatchLevel) override;
34
35 void AddRngEntropy(const std::vector<uint8_t>& data,
Qijiang Fanc013ae32020-04-16 02:18:03 +090036 AddRngEntropyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020037
38 void GetKeyCharacteristics(
39 ::arc::mojom::GetKeyCharacteristicsRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090040 GetKeyCharacteristicsCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020041
42 void GenerateKey(std::vector<mojom::KeyParameterPtr> key_params,
Qijiang Fanc013ae32020-04-16 02:18:03 +090043 GenerateKeyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020044
45 void ImportKey(arc::mojom::ImportKeyRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090046 ImportKeyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020047
48 void ExportKey(arc::mojom::ExportKeyRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090049 ExportKeyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020050
51 void AttestKey(arc::mojom::AttestKeyRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090052 AttestKeyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020053
54 void UpgradeKey(arc::mojom::UpgradeKeyRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090055 UpgradeKeyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020056
57 void DeleteKey(const std::vector<uint8_t>& key_blob,
Qijiang Fanc013ae32020-04-16 02:18:03 +090058 DeleteKeyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020059
Qijiang Fanc013ae32020-04-16 02:18:03 +090060 void DeleteAllKeys(DeleteKeyCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020061
62 void Begin(arc::mojom::BeginRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090063 BeginCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020064
65 void Update(arc::mojom::UpdateRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090066 UpdateCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020067
68 void Finish(arc::mojom::FinishRequestPtr request,
Qijiang Fanc013ae32020-04-16 02:18:03 +090069 FinishCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020070
Qijiang Fanc013ae32020-04-16 02:18:03 +090071 void Abort(uint64_t operationHandle, AbortCallback callback) override;
Edman Anjos9103e272019-04-15 18:32:47 +020072
73 private:
Edman Anjosebbef002020-05-15 17:20:20 +020074 class Backend {
75 public:
76 Backend();
77 // Not copyable nor assignable.
78 Backend(const Backend&) = delete;
79 Backend& operator=(const Backend&) = delete;
80 ~Backend();
Edman Anjos9103e272019-04-15 18:32:47 +020081
Edman Anjosebbef002020-05-15 17:20:20 +020082 context::ArcKeymasterContext* context() { return context_; }
83
84 ::keymaster::AndroidKeymaster* keymaster() { return &keymaster_; }
85
86 private:
87 // Owned by |keymaster_|.
88 context::ArcKeymasterContext* context_;
89 ::keymaster::AndroidKeymaster keymaster_;
90 };
91
92 // Runs the AndroidKeymaster operation |member| with |request| as input in the
93 // background |backend_thread_|.
94 //
95 // The given |callback| is run with the output of the keymaster operation,
96 // after being posted to the original task runner that called this method.
97 template <typename KmMember, typename KmRequest, typename KmResponse>
98 void RunKeymasterRequest(
99 const base::Location& location,
100 KmMember member,
101 std::unique_ptr<KmRequest> request,
102 base::OnceCallback<void(std::unique_ptr<KmResponse>)> callback);
103
104 // Encapsulates all fields that should only be accessed from the background
105 // |backend_thread_|.
106 //
107 // This must be created before |backend_thread_| and outlive it. There are no
108 // other thread safety requirements during construction or destruction.
109 Backend backend_;
110
111 // Thread where Keymaster operations are executed.
112 //
113 // |base::Thread| guarantees that destruction waits until any leftover tasks
114 // are executed, so this must be destroyed before |backend_| is.
115 base::Thread backend_thread_;
Edman Anjos9103e272019-04-15 18:32:47 +0200116};
Edman Anjos80c7b002018-11-16 15:01:24 +0100117
118} // namespace keymaster
119} // namespace arc
120
121#endif // ARC_KEYMASTER_KEYMASTER_SERVER_H_