// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CRYPTO_APPLE_UNEXPORTABLE_KEY_APPLE_H_
#define CRYPTO_APPLE_UNEXPORTABLE_KEY_APPLE_H_

#include <memory>

#include "base/compiler_specific.h"

#if defined(__OBJC__)
#import <LocalAuthentication/LocalAuthentication.h>
#endif  // defined(__OBJC__)

#include "crypto/unexportable_key.h"

namespace crypto::apple {

// UnexportableKeyProviderApple is an implementation of the
// UnexportableKeyProvider interface on top of Apple's Secure Enclave. Callers
// must provide a keychain access group when instantiating this class. This
// means that the build must be codesigned for any of this to work.
// https://developer.apple.com/documentation/bundleresources/entitlements/keychain-access-groups?language=objc
//
// Only NIST P-256 elliptic curves are supported.
//
// Unlike Windows keys, Apple platforms will store key metadata locally. Callers
// are responsible for deleting keys when they are no longer needed.
class UnexportableKeyProviderApple : public StatefulUnexportableKeyProvider {
 public:
  explicit UnexportableKeyProviderApple(Config config);
  ~UnexportableKeyProviderApple() override;

#if defined(__OBJC__)
  // Like UnexportableKeyProvider::FromWrappedSigningKeySlowly, but lets you
  // pass an authenticated LAContext to avoid having macOS prompt the user for
  // user verification.
  std::unique_ptr<UnexportableSigningKey> FromWrappedSigningKeySlowly(
      base::span<const uint8_t> wrapped_key,
      LAContext* lacontext);

  // Like UnexportableKeyProvider::GenerateSigningKeySlowly, but lets you pass
  // an authenticated LAContext to avoid having macOS prompt the user for user
  // verification.
  std::unique_ptr<UnexportableSigningKey> GenerateSigningKeySlowly(
      base::span<const SignatureVerifier::SignatureAlgorithm>
          acceptable_algorithms,
      LAContext* lacontext);

  // Like UnexportableKeyProvider::FromWrappedAttestationKeySlowly, but lets you
  // pass an authenticated LAContext to avoid having macOS prompt the user for
  // user verification.
  std::unique_ptr<UnexportableAttestationKey> FromWrappedAttestationKeySlowly(
      base::span<const uint8_t> wrapped_key,
      LAContext* lacontext);

  // Like UnexportableKeyProvider::GenerateAttestationKeySlowly, but lets you
  // pass an authenticated LAContext to avoid having macOS prompt the user for
  // user verification.
  std::unique_ptr<UnexportableAttestationKey> GenerateAttestationKeySlowly(
      base::span<const SignatureVerifier::SignatureAlgorithm>
          acceptable_algorithms,
      LAContext* lacontext);
#endif  // defined(__OBJC__)

  // UnexportableKeyProvider:
  std::optional<SignatureVerifier::SignatureAlgorithm> SelectAlgorithm(
      base::span<const SignatureVerifier::SignatureAlgorithm>
          acceptable_algorithms) override;
  std::unique_ptr<UnexportableSigningKey> GenerateSigningKeySlowly(
      base::span<const SignatureVerifier::SignatureAlgorithm>
          acceptable_algorithms) override;
  std::unique_ptr<UnexportableSigningKey> FromWrappedSigningKeySlowly(
      base::span<const uint8_t> wrapped_key) override;
  std::unique_ptr<UnexportableAttestationKey> GenerateAttestationKeySlowly(
      base::span<const SignatureVerifier::SignatureAlgorithm>
          acceptable_algorithms) override;
  std::unique_ptr<UnexportableAttestationKey> FromWrappedAttestationKeySlowly(
      base::span<const uint8_t> wrapped_key) override;
  StatefulUnexportableKeyProvider* AsStatefulUnexportableKeyProvider()
      LIFETIME_BOUND override;

  // StatefulUnexportableKeyProvider:
  std::optional<std::vector<std::unique_ptr<UnexportableSigningKey>>>
  GetAllKeysSlowly() override;
  std::optional<size_t> DeleteWrappedKeysSlowly(
      base::span<const base::span<const uint8_t>> wrapped_keys) override;
  std::optional<size_t> DeleteKeysSlowly(
      base::span<const UnexportableSigningKey* const> keys) override;
  std::optional<size_t> DeleteAllKeysSlowly() override;

 private:
  struct ObjCStorage;

  const Config::AccessControl access_control_;
  std::unique_ptr<ObjCStorage> objc_storage_;
};

std::unique_ptr<UnexportableKeyProviderApple> GetUnexportableKeyProviderApple(
    UnexportableKeyProvider::Config config);

}  // namespace crypto::apple

#endif  // CRYPTO_APPLE_UNEXPORTABLE_KEY_APPLE_H_
