aboutsummaryrefslogtreecommitdiff
path: root/cc/internal/legacy_proto_key.h
blob: 9372cdf5ad4d5bdaa9767f2f21d4fe7968202a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

#ifndef TINK_INTERNAL_LEGACY_PROTO_KEY_H_
#define TINK_INTERNAL_LEGACY_PROTO_KEY_H_

#include <string>

#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "tink/internal/proto_key_serialization.h"
#include "tink/key.h"
#include "tink/parameters.h"
#include "tink/secret_key_access_token.h"
#include "tink/util/statusor.h"
#include "proto/tink.pb.h"

namespace crypto {
namespace tink {
namespace internal {

// Parameters returned by `LegacyProtoKey::GetParameters()` that cannot be used
// to create other LegacyProtoKey instances.
class UnusableLegacyProtoParameters : public Parameters {
 public:
  // Copyable and movable.
  UnusableLegacyProtoParameters(const UnusableLegacyProtoParameters& other) =
      default;
  UnusableLegacyProtoParameters& operator=(
      const UnusableLegacyProtoParameters& other) = default;
  UnusableLegacyProtoParameters(UnusableLegacyProtoParameters&& other) =
      default;
  UnusableLegacyProtoParameters& operator=(
      UnusableLegacyProtoParameters&& other) = default;

  explicit UnusableLegacyProtoParameters(
      absl::string_view type_url,
      google::crypto::tink::OutputPrefixType output_prefix_type)
      : type_url_(type_url), output_prefix_type_(output_prefix_type) {}

  bool HasIdRequirement() const override {
    return output_prefix_type_ != google::crypto::tink::OutputPrefixType::RAW;
  }

  bool operator==(const Parameters& other) const override;

 private:
  std::string type_url_;
  google::crypto::tink::OutputPrefixType output_prefix_type_;
};

// Key type for legacy proto keys.
class LegacyProtoKey : public Key {
 public:
  // Copyable and movable.
  LegacyProtoKey(const LegacyProtoKey& other) = default;
  LegacyProtoKey& operator=(const LegacyProtoKey& other) = default;
  LegacyProtoKey(LegacyProtoKey&& other) = default;
  LegacyProtoKey& operator=(LegacyProtoKey&& other) = default;

  // Creates `LegacyProtoKey` object from `serialization`.  Requires `token` if
  // the key material type is either SYMMETRIC or ASYMMETRIC_PRIVATE.
  static util::StatusOr<LegacyProtoKey> Create(
      ProtoKeySerialization serialization,
      absl::optional<SecretKeyAccessToken> token);

  const Parameters& GetParameters() const override {
    return unusable_proto_parameters_;
  }

  absl::optional<int> GetIdRequirement() const override {
    return serialization_.IdRequirement();
  }

  bool operator==(const Key& other) const override;

  // Returns `ProtoKeySerialization` pointer for this object.  Requires `token`
  // if the key material type is either SYMMETRIC or ASYMMETRIC_PRIVATE.
  util::StatusOr<const ProtoKeySerialization*> Serialization(
      absl::optional<SecretKeyAccessToken> token) const;

 private:
  explicit LegacyProtoKey(ProtoKeySerialization serialization)
      : serialization_(serialization),
        unusable_proto_parameters_(serialization.TypeUrl(),
                                   serialization.GetOutputPrefixType()) {}

  ProtoKeySerialization serialization_;
  UnusableLegacyProtoParameters unusable_proto_parameters_;
};

}  // namespace internal
}  // namespace tink
}  // namespace crypto

#endif  // TINK_INTERNAL_LEGACY_PROTO_KEY_H_