blob: 7937c3b00e354070d14a20d183cc62f41c04bcc5 [file]
// Copyright 2026 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
//
// https://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 THIRD_PARTY_CREDENTIO_CLAIM_LOCAL_SIGNER_H_
#define THIRD_PARTY_CREDENTIO_CLAIM_LOCAL_SIGNER_H_
#include <cstddef>
#include <optional>
#include "absl/base/nullability.h"
#include "absl/functional/any_invocable.h"
#include "absl/log/die_if_null.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "claim/signer.h"
#include "cose/timestamp_creator.h"
#include "crypto/crypto_write_handler.h"
#include "crypto/hash.h"
namespace credentio {
// A signer to sign a Claim according to
// https://spec.c2pa.org/specifications/specifications/2.4/specs/C2PA_Specification.html#_signing_a_claim.
class LocalClaimSigner : public ClaimSigner {
public:
explicit LocalClaimSigner(CryptoWriteHandler* absl_nonnull crypto_writer,
const HasherFactory* absl_nonnull hasher_factory,
std::optional<size_t> pad_to_size = std::nullopt)
: crypto_writer_(*ABSL_DIE_IF_NULL(crypto_writer)),
hasher_factory_(*ABSL_DIE_IF_NULL(hasher_factory)),
timestamp_creator_(&crypto_writer_, &hasher_factory_),
pad_to_size_(pad_to_size) {}
// Signs the serialized CBOR of the claim and returns the serialized
// COSE_Sign1_Tagged structure that will be written into the C2PA Claim
// Signature box. For more details, see
// https://spec.c2pa.org/specifications/specifications/2.4/specs/C2PA_Specification.html#_signing_a_claim.
// It guarantees that the size of the result is fixed and is a function of
// `max_signature_bytes_size()` and `max_timestamp_byte_size()` of the
// CryptoWriteHandler rather than of the actual signature and timestamp sizes.
void Sign(absl::string_view claim, Options options,
absl::AnyInvocable<void(absl::StatusOr<SignResult>) &&> cb)
const override;
private:
CryptoWriteHandler& crypto_writer_;
const HasherFactory& hasher_factory_;
TimestampCreator timestamp_creator_;
std::optional<size_t> pad_to_size_;
};
} // namespace credentio
#endif // THIRD_PARTY_CREDENTIO_CLAIM_LOCAL_SIGNER_H_