blob: f1b89cbce41d2c8320bfb8310c82a65ad53f6d6f [file]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sharing/vapid_key_manager.h"
#include "chrome/browser/sharing/sharing_sync_preference.h"
#include "crypto/ec_private_key.h"
VapidKeyManager::VapidKeyManager(SharingSyncPreference* sharing_sync_preference)
: sharing_sync_preference_(sharing_sync_preference) {}
VapidKeyManager::~VapidKeyManager() = default;
crypto::ECPrivateKey* VapidKeyManager::GetOrCreateKey() {
base::Optional<std::vector<uint8_t>> stored_key =
sharing_sync_preference_->GetVapidKey();
if (stored_key) {
vapid_key_ = crypto::ECPrivateKey::CreateFromPrivateKeyInfo(*stored_key);
return vapid_key_.get();
}
vapid_key_ = crypto::ECPrivateKey::Create();
if (!vapid_key_)
return nullptr;
std::vector<uint8_t> key;
if (!vapid_key_->ExportPrivateKey(&key)) {
LOG(ERROR) << "Could not export vapid key";
vapid_key_.reset();
return nullptr;
}
sharing_sync_preference_->SetVapidKey(key);
return vapid_key_.get();
}