xref: /aosp_15_r20/external/tink/testing/cc/keyset_impl.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_TESTING_KEYSET_IMPL_H_
18 #define TINK_TESTING_KEYSET_IMPL_H_
19 
20 #include <grpcpp/grpcpp.h>
21 #include <grpcpp/server_context.h>
22 #include <grpcpp/support/status.h>
23 
24 #include <string>
25 
26 #include "absl/container/flat_hash_map.h"
27 #include "proto/tink.pb.h"
28 #include "proto/testing_api.grpc.pb.h"
29 
30 namespace tink_testing_api {
31 
32 // A Keyset Service.
33 class KeysetImpl final : public Keyset::Service {
34  public:
35   // Returns the key template for the given template name.
36   grpc::Status GetTemplate(grpc::ServerContext* context,
37                            const KeysetTemplateRequest* request,
38                            KeysetTemplateResponse* response) override;
39 
40   // Generates a new keyset with one key from a template.
41   grpc::Status Generate(grpc::ServerContext* context,
42                         const KeysetGenerateRequest* request,
43                         KeysetGenerateResponse* response) override;
44 
45   // Returns a public keyset for a given private keyset.
46   grpc::Status Public(grpc::ServerContext* context,
47                       const KeysetPublicRequest* request,
48                       KeysetPublicResponse* response) override;
49 
50   // Converts a keyset from binary to JSON format.
51   grpc::Status ToJson(grpc::ServerContext* context,
52                       const KeysetToJsonRequest* request,
53                       KeysetToJsonResponse* response) override;
54 
55   // Converts a keyset from JSON to binary format.
56   grpc::Status FromJson(grpc::ServerContext* context,
57                         const KeysetFromJsonRequest* request,
58                         KeysetFromJsonResponse* response) override;
59 
60   // Writes an encrypted keyset.
61   grpc::Status WriteEncrypted(grpc::ServerContext* context,
62                               const KeysetWriteEncryptedRequest* request,
63                               KeysetWriteEncryptedResponse* response) override;
64 
65   // Reads an encrypted keyset.
66   grpc::Status ReadEncrypted(grpc::ServerContext* context,
67                              const KeysetReadEncryptedRequest* request,
68                              KeysetReadEncryptedResponse* response) override;
69   KeysetImpl();
70 
71  private:
72   absl::flat_hash_map<std::string, google::crypto::tink::KeyTemplate>
73       key_templates_;
74 };
75 
76 }  // namespace tink_testing_api
77 
78 #endif  // TINK_TESTING_KEYSET_IMPL_H_
79