aboutsummaryrefslogtreecommitdiff
path: root/cc/core/keyset_handle.cc
blob: 9a6b70c28a2bd645d9b8b4a865c2defab5922ed9 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright 2017 Google Inc.
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////

#include "tink/keyset_handle.h"

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "tink/aead.h"
#include "tink/insecure_secret_key_access.h"
#include "tink/internal/key_gen_configuration_impl.h"
#include "tink/internal/key_info.h"
#include "tink/internal/key_status_util.h"
#include "tink/internal/mutable_serialization_registry.h"
#include "tink/internal/proto_key_serialization.h"
#include "tink/internal/util.h"
#include "tink/key_gen_configuration.h"
#include "tink/key_status.h"
#include "tink/keyset_reader.h"
#include "tink/keyset_writer.h"
#include "tink/registry.h"
#include "tink/util/errors.h"
#include "tink/util/keyset_util.h"
#include "proto/tink.pb.h"

using google::crypto::tink::EncryptedKeyset;
using google::crypto::tink::KeyData;
using google::crypto::tink::Keyset;
using google::crypto::tink::KeysetInfo;
using google::crypto::tink::KeyStatusType;
using google::crypto::tink::KeyTemplate;
using google::crypto::tink::OutputPrefixType;

namespace crypto {
namespace tink {

namespace {

util::StatusOr<std::unique_ptr<EncryptedKeyset>> Encrypt(
    const Keyset& keyset, const Aead& master_key_aead,
    absl::string_view associated_data) {
  auto encrypt_result =
      master_key_aead.Encrypt(keyset.SerializeAsString(), associated_data);
  if (!encrypt_result.ok()) return encrypt_result.status();
  auto enc_keyset = absl::make_unique<EncryptedKeyset>();
  enc_keyset->set_encrypted_keyset(encrypt_result.value());
  return std::move(enc_keyset);
}

util::StatusOr<std::unique_ptr<Keyset>> Decrypt(
    const EncryptedKeyset& enc_keyset, const Aead& master_key_aead,
    absl::string_view associated_data) {
  auto decrypt_result =
      master_key_aead.Decrypt(enc_keyset.encrypted_keyset(), associated_data);
  if (!decrypt_result.ok()) return decrypt_result.status();
  auto keyset = absl::make_unique<Keyset>();
  if (!keyset->ParseFromString(decrypt_result.value())) {
    return util::Status(
        absl::StatusCode::kInvalidArgument,
        "Could not parse the decrypted data as a Keyset-proto.");
  }
  return std::move(keyset);
}

util::Status ValidateNoSecret(const Keyset& keyset) {
  for (const Keyset::Key& key : keyset.key()) {
    if (key.key_data().key_material_type() == KeyData::UNKNOWN_KEYMATERIAL ||
        key.key_data().key_material_type() == KeyData::SYMMETRIC ||
        key.key_data().key_material_type() == KeyData::ASYMMETRIC_PRIVATE) {
      return util::Status(
          absl::StatusCode::kFailedPrecondition,
          "Cannot create KeysetHandle with secret key material from "
          "potentially unencrypted source.");
    }
  }
  return util::OkStatus();
}

util::StatusOr<internal::ProtoKeySerialization> ToProtoKeySerialization(
    Keyset::Key key) {
  absl::optional<int> id_requirement = absl::nullopt;
  if (key.output_prefix_type() != OutputPrefixType::RAW) {
    id_requirement = key.key_id();
  }

  return internal::ProtoKeySerialization::Create(
      key.key_data().type_url(),
      RestrictedData(key.key_data().value(), InsecureSecretKeyAccess::Get()),
      key.key_data().key_material_type(), key.output_prefix_type(),
      id_requirement);
}

}  // anonymous namespace

util::Status KeysetHandle::ValidateAt(int index) const {
  const Keyset::Key& proto_key = get_keyset().key(index);
  OutputPrefixType output_prefix_type = proto_key.output_prefix_type();
  absl::optional<int> id_requirement = absl::nullopt;
  if (output_prefix_type != OutputPrefixType::RAW) {
    id_requirement = proto_key.key_id();
  }

  if (!internal::IsPrintableAscii(proto_key.key_data().type_url())) {
    return util::Status(absl::StatusCode::kFailedPrecondition,
                        "Non-printable ASCII character in type URL.");
  }

  util::StatusOr<KeyStatus> key_status =
      internal::FromKeyStatusType(proto_key.status());
  if (!key_status.ok()) return key_status.status();

  return util::OkStatus();
}

util::Status KeysetHandle::Validate() const {
  int num_primary = 0;
  const Keyset& keyset = get_keyset();

  for (int i = 0; i < size(); ++i) {
    util::Status status = ValidateAt(i);
    if (!status.ok()) return status;

    Keyset::Key proto_key = keyset.key(i);
    if (proto_key.key_id() == keyset.primary_key_id()) {
      ++num_primary;
      if (proto_key.status() != KeyStatusType::ENABLED) {
        return util::Status(absl::StatusCode::kFailedPrecondition,
                            "Keyset has primary that is not enabled");
      }
    }
  }

  if (num_primary < 1) {
    return util::Status(absl::StatusCode::kFailedPrecondition,
                        "Keyset has no primary");
  }
  if (num_primary > 1) {
    return util::Status(absl::StatusCode::kFailedPrecondition,
                        "Keyset has more than one primary");
  }

  return util::OkStatus();
}

KeysetHandle::Entry KeysetHandle::GetPrimary() const {
  util::Status validation = Validate();
  CHECK_OK(validation);

  const Keyset& keyset = get_keyset();
  for (int i = 0; i < keyset.key_size(); ++i) {
    if (keyset.key(i).key_id() == keyset.primary_key_id()) {
      return (*this)[i];
    }
  }

  // Since keyset handle was validated, it should have a valid primary key.
  internal::LogFatal("Keyset handle should have a valid primary key.");
}

KeysetHandle::Entry KeysetHandle::operator[](int index) const {
  CHECK(index >= 0 && index < size())
      << "Invalid index " << index << " for keyset of size " << size();

  if (!entries_.empty() && entries_.size() > index) {
    return *entries_[index];
  }
  // Since `entries_` has not been populated, the entry must be created on
  // demand from the key proto entry at `index` in `keyset_`. This special
  // case will no longer be necessary after `keyset_` has been removed from the
  // `KeysetHandle` class.
  //
  // TODO(b/277792846): Remove after transition to rely solely on
  // `KeysetHandle::Entry`.
  return CreateEntryAt(index);
}

KeysetHandle::Entry KeysetHandle::CreateEntryAt(int index) const {
  CHECK(index >= 0 && index < size())
      << "Invalid index " << index << " for keyset of size " << size();

  util::Status validation = ValidateAt(index);
  CHECK_OK(validation);

  Keyset keyset = get_keyset();
  util::StatusOr<Entry> entry =
      CreateEntry(keyset.key(index), keyset.primary_key_id());
  // Status should be OK since this keyset handle has been validated.
  CHECK_OK(entry.status());
  return *entry;
}

util::StatusOr<KeysetHandle::Entry> KeysetHandle::CreateEntry(
    const Keyset::Key& proto_key, uint32_t primary_key_id) {
  util::StatusOr<internal::ProtoKeySerialization> serialization =
      ToProtoKeySerialization(proto_key);
  if (!serialization.ok()) {
    return serialization.status();
  }

  util::StatusOr<std::shared_ptr<const Key>> key =
      internal::MutableSerializationRegistry::GlobalInstance()
          .ParseKeyWithLegacyFallback(*serialization,
                                      InsecureSecretKeyAccess::Get());
  if (!key.ok()) {
    return key.status();
  }

  util::StatusOr<KeyStatus> key_status =
      internal::FromKeyStatusType(proto_key.status());
  if (!key_status.ok()) {
    return key_status.status();
  }

  return Entry(*std::move(key), *key_status, proto_key.key_id(),
               proto_key.key_id() == primary_key_id);
}

util::StatusOr<std::unique_ptr<KeysetHandle>> KeysetHandle::Read(
    std::unique_ptr<KeysetReader> reader, const Aead& master_key_aead,
    const absl::flat_hash_map<std::string, std::string>&
        monitoring_annotations) {
  return ReadWithAssociatedData(std::move(reader), master_key_aead,
                                /*associated_data=*/"", monitoring_annotations);
}

util::StatusOr<std::unique_ptr<KeysetHandle>>
KeysetHandle::ReadWithAssociatedData(
    std::unique_ptr<KeysetReader> reader, const Aead& master_key_aead,
    absl::string_view associated_data,
    const absl::flat_hash_map<std::string, std::string>&
        monitoring_annotations) {
  util::StatusOr<std::unique_ptr<EncryptedKeyset>> enc_keyset_result =
      reader->ReadEncrypted();
  if (!enc_keyset_result.ok()) {
    return ToStatusF(absl::StatusCode::kInvalidArgument,
                     "Error reading encrypted keyset data: %s",
                     enc_keyset_result.status().message());
  }

  auto keyset_result =
      Decrypt(*enc_keyset_result.value(), master_key_aead, associated_data);
  if (!keyset_result.ok()) {
    return ToStatusF(absl::StatusCode::kInvalidArgument,
                     "Error decrypting encrypted keyset: %s",
                     keyset_result.status().message());
  }
  util::StatusOr<std::vector<std::shared_ptr<const Entry>>> entries =
      GetEntriesFromKeyset(**keyset_result);
  if (!entries.ok()) {
    return entries.status();
  }
  if (entries->size() != (*keyset_result)->key_size()) {
    return util::Status(absl::StatusCode::kInternal,
                        "Error converting keyset proto into key entries.");
  }
  return absl::WrapUnique(new KeysetHandle(*std::move(keyset_result), *entries,
                                           monitoring_annotations));
}

util::StatusOr<std::unique_ptr<KeysetHandle>> KeysetHandle::ReadNoSecret(
    const std::string& serialized_keyset,
    const absl::flat_hash_map<std::string, std::string>&
        monitoring_annotations) {
  Keyset keyset;
  if (!keyset.ParseFromString(serialized_keyset)) {
    return util::Status(absl::StatusCode::kInvalidArgument,
                        "Could not parse the input string as a Keyset-proto.");
  }
  util::Status validation = ValidateNoSecret(keyset);
  if (!validation.ok()) {
    return validation;
  }
  util::StatusOr<std::vector<std::shared_ptr<const Entry>>> entries =
      GetEntriesFromKeyset(keyset);
  if (!entries.ok()) {
    return entries.status();
  }
  if (entries->size() != keyset.key_size()) {
    return util::Status(absl::StatusCode::kInternal,
                        "Error converting keyset proto into key entries.");
  }
  return absl::WrapUnique(
      new KeysetHandle(std::move(keyset), *entries, monitoring_annotations));
}

util::Status KeysetHandle::Write(KeysetWriter* writer,
                                 const Aead& master_key_aead) const {
  return WriteWithAssociatedData(writer, master_key_aead, "");
}

util::Status KeysetHandle::WriteWithAssociatedData(
    KeysetWriter* writer, const Aead& master_key_aead,
    absl::string_view associated_data) const {
  if (writer == nullptr) {
    return util::Status(absl::StatusCode::kInvalidArgument,
                        "Writer must be non-null");
  }
  auto encrypt_result = Encrypt(get_keyset(), master_key_aead, associated_data);
  if (!encrypt_result.ok()) {
    return ToStatusF(absl::StatusCode::kInvalidArgument,
                     "Encryption of the keyset failed: %s",
                     encrypt_result.status().message());
  }
  return writer->Write(*(encrypt_result.value()));
}

util::Status KeysetHandle::WriteNoSecret(KeysetWriter* writer) const {
  if (writer == nullptr) {
    return util::Status(absl::StatusCode::kInvalidArgument,
                        "Writer must be non-null");
  }

  util::Status validation = ValidateNoSecret(get_keyset());
  if (!validation.ok()) return validation;

  return writer->Write(get_keyset());
}

util::StatusOr<std::unique_ptr<KeysetHandle>> KeysetHandle::GenerateNew(
    const KeyTemplate& key_template, const KeyGenConfiguration& config,
    const absl::flat_hash_map<std::string, std::string>&
        monitoring_annotations) {
  auto handle =
      absl::WrapUnique(new KeysetHandle(Keyset(), monitoring_annotations));
  util::StatusOr<uint32_t> const result =
      handle->AddKey(key_template, /*as_primary=*/true, config);
  if (!result.ok()) {
    return result.status();
  }
  return std::move(handle);
}

util::StatusOr<std::unique_ptr<KeysetHandle>> KeysetHandle::GenerateNew(
    const KeyTemplate& key_template,
    const absl::flat_hash_map<std::string, std::string>&
        monitoring_annotations) {
  KeyGenConfiguration config;
  util::Status status =
      internal::KeyGenConfigurationImpl::SetGlobalRegistryMode(config);
  if (!status.ok()) {
    return status;
  }
  return GenerateNew(key_template, config, monitoring_annotations);
}

util::StatusOr<std::unique_ptr<Keyset::Key>> ExtractPublicKey(
    const Keyset::Key& key) {
  if (key.key_data().key_material_type() != KeyData::ASYMMETRIC_PRIVATE) {
    return util::Status(
        absl::StatusCode::kInvalidArgument,
        "Key material is not of type KeyData::ASYMMETRIC_PRIVATE");
  }
  auto key_data_result = Registry::GetPublicKeyData(key.key_data().type_url(),
                                                    key.key_data().value());
  if (!key_data_result.ok()) return key_data_result.status();
  auto public_key = absl::make_unique<Keyset::Key>(key);
  public_key->mutable_key_data()->Swap(key_data_result.value().get());
  return std::move(public_key);
}

util::StatusOr<std::unique_ptr<KeysetHandle>>
KeysetHandle::GetPublicKeysetHandle() const {
  std::unique_ptr<Keyset> public_keyset(new Keyset());
  for (const Keyset::Key& key : get_keyset().key()) {
    auto public_key_result = ExtractPublicKey(key);
    if (!public_key_result.ok()) return public_key_result.status();
    public_keyset->add_key()->Swap(public_key_result.value().get());
  }
  public_keyset->set_primary_key_id(get_keyset().primary_key_id());
  util::StatusOr<std::vector<std::shared_ptr<const Entry>>> entries =
      GetEntriesFromKeyset(*public_keyset);
  if (!entries.ok()) {
    return entries.status();
  }
  if (entries->size() != public_keyset->key_size()) {
    return util::Status(absl::StatusCode::kInternal,
                        "Error converting keyset proto into key entries.");
  }
  std::unique_ptr<KeysetHandle> handle(
      new KeysetHandle(std::move(public_keyset), *entries));
  return std::move(handle);
}

crypto::tink::util::StatusOr<uint32_t> KeysetHandle::AddToKeyset(
    const google::crypto::tink::KeyTemplate& key_template, bool as_primary,
    const KeyGenConfiguration& config, Keyset* keyset) {
  if (key_template.output_prefix_type() ==
      google::crypto::tink::OutputPrefixType::UNKNOWN_PREFIX) {
    return util::Status(absl::StatusCode::kInvalidArgument,
                        "key template has unknown prefix");
  }

  // Generate new key data.
  util::StatusOr<std::unique_ptr<KeyData>> key_data;
  if (internal::KeyGenConfigurationImpl::IsInGlobalRegistryMode(config)) {
    key_data = Registry::NewKeyData(key_template);
  } else {
    util::StatusOr<const internal::KeyTypeInfoStore*> key_type_info_store =
        internal::KeyGenConfigurationImpl::GetKeyTypeInfoStore(config);
    if (!key_type_info_store.ok()) {
      return key_type_info_store.status();
    }
    util::StatusOr<const internal::KeyTypeInfoStore::Info*> key_type_info =
        (*key_type_info_store)->Get(key_template.type_url());
    if (!key_type_info.ok()) {
      return key_type_info.status();
    }
    key_data = (*key_type_info)->key_factory().NewKeyData(key_template.value());
  }
  if (!key_data.ok()) {
    return key_data.status();
  }

  // Add and fill in new key in `keyset`.
  Keyset::Key* key = keyset->add_key();
  *(key->mutable_key_data()) = *std::move(key_data).value();
  key->set_status(KeyStatusType::ENABLED);
  key->set_output_prefix_type(key_template.output_prefix_type());

  uint32_t key_id = GenerateUnusedKeyId(*keyset);
  key->set_key_id(key_id);
  if (as_primary) {
    keyset->set_primary_key_id(key_id);
  }
  return key_id;
}

crypto::tink::util::StatusOr<uint32_t> KeysetHandle::AddKey(
    const google::crypto::tink::KeyTemplate& key_template, bool as_primary,
    const KeyGenConfiguration& config) {
  util::StatusOr<uint32_t> id =
      AddToKeyset(key_template, as_primary, config, &keyset_);
  if (!id.ok()) {
    return id.status();
  }
  util::StatusOr<const Entry> entry = CreateEntry(
      keyset_.key(keyset_.key_size() - 1), keyset_.primary_key_id());
  if (!entry.ok()) {
    return entry.status();
  }
  entries_.push_back(std::make_shared<const Entry>(*entry));
  return *id;
}

KeysetInfo KeysetHandle::GetKeysetInfo() const {
  return KeysetInfoFromKeyset(get_keyset());
}

util::StatusOr<std::vector<std::shared_ptr<const KeysetHandle::Entry>>>
KeysetHandle::GetEntriesFromKeyset(const Keyset& keyset) {
  std::vector<std::shared_ptr<const Entry>> entries;
  for (const Keyset::Key& key : keyset.key()) {
    util::StatusOr<const Entry> entry =
        CreateEntry(key, keyset.primary_key_id());
    if (!entry.ok()) {
      return entry.status();
    }
    entries.push_back(std::make_shared<const Entry>(*entry));
  }
  return entries;
}

}  // namespace tink
}  // namespace crypto