71 lines
2.4 KiB
C++
71 lines
2.4 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// Copyright 2019 Google LLC.
|
|
//
|
|
// This software is licensed under the terms defined in the Widevine Master
|
|
// License Agreement. For a copy of this agreement, please contact
|
|
// widevine-licensing@google.com.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "common/signer_public_key.h"
|
|
|
|
#include "absl/memory/memory.h"
|
|
#include "common/ec_key.h"
|
|
#include "common/rsa_key.h"
|
|
|
|
namespace widevine {
|
|
|
|
// SignerPublicKeyImpl is a generic implementation of SignerPublicKey. The
|
|
// initialization details are in the SignerPublicKey factory method.
|
|
template <typename T>
|
|
class SignerPublicKeyImpl : public SignerPublicKey {
|
|
public:
|
|
explicit SignerPublicKeyImpl(std::unique_ptr<T> signer_public_key)
|
|
: signer_public_key_(std::move(signer_public_key)) {}
|
|
~SignerPublicKeyImpl() override {}
|
|
SignerPublicKeyImpl(const SignerPublicKeyImpl&) = delete;
|
|
SignerPublicKeyImpl& operator=(const SignerPublicKeyImpl&) = delete;
|
|
|
|
bool VerifySignature(const std::string& message, HashAlgorithm hash_algorithm,
|
|
const std::string& signature) const override {
|
|
if (!signer_public_key_->VerifySignature(message, hash_algorithm,
|
|
signature)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
std::unique_ptr<T> signer_public_key_;
|
|
};
|
|
|
|
std::unique_ptr<SignerPublicKey> SignerPublicKey::Create(
|
|
const std::string& signer_public_key, DrmCertificate::Algorithm algorithm) {
|
|
switch (algorithm) {
|
|
case DrmCertificate::RSA: {
|
|
std::unique_ptr<RsaPublicKey> public_key(
|
|
RsaPublicKey::Create(signer_public_key));
|
|
if (public_key == nullptr) {
|
|
return nullptr;
|
|
}
|
|
return absl::make_unique<SignerPublicKeyImpl<RsaPublicKey>>(
|
|
std::move(public_key));
|
|
}
|
|
// All supported ECC curves are specified here.
|
|
case DrmCertificate::ECC_SECP256R1:
|
|
case DrmCertificate::ECC_SECP384R1:
|
|
case DrmCertificate::ECC_SECP521R1: {
|
|
std::unique_ptr<ECPublicKey> public_key =
|
|
ECPublicKey::Create(signer_public_key);
|
|
if (public_key == nullptr) {
|
|
return nullptr;
|
|
}
|
|
return absl::make_unique<SignerPublicKeyImpl<ECPublicKey>>(
|
|
std::move(public_key));
|
|
}
|
|
default:
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
} // namespace widevine
|