CryptoAlgorithmECDSAGCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmECDSA.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmEcdsaParams.h"
#include "CryptoKeyEC.h"
#include "ExceptionCode.h"
#include "GCryptUtilities.h"
#include "ScriptExecutionContext.h"
#include <pal/crypto/CryptoDigest.h>
namespace WebCore {
static bool extractECDSASignatureInteger(Vector<uint8_t>& signature, gcry_sexp_t signatureSexp, const char* integerName, size_t keySizeInBytes)
{
PAL::GCrypt::Handle<gcry_sexp_t> integerSexp(gcry_sexp_find_token(signatureSexp, integerName, 0));
if (!integerSexp)
return false;
auto integerData = mpiData(integerSexp);
if (!integerData)
return false;
size_t dataSize = integerData->size();
if (dataSize >= keySizeInBytes) {
signature.append(&integerData->at(dataSize - keySizeInBytes), keySizeInBytes);
} else {
for (size_t paddingSize = keySizeInBytes - dataSize; paddingSize > 0; --paddingSize)
signature.uncheckedAppend(0x00);
signature.appendVector(*integerData);
}
return true;
}
static std::optional<Vector<uint8_t>> gcryptSign(gcry_sexp_t keySexp, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t keySizeInBytes)
{
Vector<uint8_t> dataHash;
{
auto digestAlgorithm = hashCryptoDigestAlgorithm(hashAlgorithmIdentifier);
if (!digestAlgorithm)
return std::nullopt;
auto digest = PAL::CryptoDigest::create(*digestAlgorithm);
if (!digest)
return std::nullopt;
digest->addBytes(data.data(), data.size());
dataHash = digest->computeHash();
}
PAL::GCrypt::Handle<gcry_sexp_t> dataSexp;
{
auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier);
if (!shaAlgorithm)
return std::nullopt;
gcry_error_t error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags raw)(hash %s %b))",
*shaAlgorithm, dataHash.size(), dataHash.data());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
}
PAL::GCrypt::Handle<gcry_sexp_t> signatureSexp;
gcry_error_t error = gcry_pk_sign(&signatureSexp, dataSexp, keySexp);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
Vector<uint8_t> signature;
signature.reserveInitialCapacity(keySizeInBytes * 2);
if (!extractECDSASignatureInteger(signature, signatureSexp, "r", keySizeInBytes)
|| !extractECDSASignatureInteger(signature, signatureSexp, "s", keySizeInBytes))
return std::nullopt;
return signature;
}
static std::optional<bool> gcryptVerify(gcry_sexp_t keySexp, const Vector<uint8_t>& signature, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t keySizeInBytes)
{
if (signature.size() != keySizeInBytes * 2)
return false;
Vector<uint8_t> dataHash;
{
auto digestAlgorithm = hashCryptoDigestAlgorithm(hashAlgorithmIdentifier);
if (!digestAlgorithm)
return std::nullopt;
auto digest = PAL::CryptoDigest::create(*digestAlgorithm);
if (!digest)
return std::nullopt;
digest->addBytes(data.data(), data.size());
dataHash = digest->computeHash();
}
PAL::GCrypt::Handle<gcry_sexp_t> signatureSexp;
gcry_error_t error = gcry_sexp_build(&signatureSexp, nullptr, "(sig-val(ecdsa(r %b)(s %b)))",
keySizeInBytes, signature.data(), keySizeInBytes, signature.data() + keySizeInBytes);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
PAL::GCrypt::Handle<gcry_sexp_t> dataSexp;
{
auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier);
if (!shaAlgorithm)
return std::nullopt;
error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags raw)(hash %s %b))",
*shaAlgorithm, dataHash.size(), dataHash.data());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
}
error = gcry_pk_verify(signatureSexp, dataSexp, keySexp);
return { error == GPG_ERR_NO_ERROR };
}
void CryptoAlgorithmECDSA::platformSign(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch(
[parameters = WTFMove(parameters), key = WTFMove(key), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& ecKey = downcast<CryptoKeyEC>(key.get());
auto& ecParameters = downcast<CryptoAlgorithmEcdsaParams>(*parameters);
auto output = gcryptSign(ecKey.platformKey(), data, ecParameters.hashIdentifier, ecKey.keySizeInBits() / 8);
if (!output) {
context.postTask(
[callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
exceptionCallback(OperationError);
context.deref();
});
return;
}
context.postTask(
[output = WTFMove(*output), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
callback(output);
context.deref();
});
});
}
void CryptoAlgorithmECDSA::platformVerify(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& signature, Vector<uint8_t>&& data, BoolCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch(
[parameters = WTFMove(parameters), key = WTFMove(key), signature = WTFMove(signature), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& ecKey = downcast<CryptoKeyEC>(key.get());
auto& ecParameters = downcast<CryptoAlgorithmEcdsaParams>(*parameters);
auto output = gcryptVerify(ecKey.platformKey(), signature, data, ecParameters.hashIdentifier, ecKey.keySizeInBits() / 8);
if (!output) {
context.postTask(
[callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
exceptionCallback(OperationError);
context.deref();
});
return;
}
context.postTask(
[output = WTFMove(*output), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
callback(output);
context.deref();
});
});
}
}
#endif // ENABLE(SUBTLE_CRYPTO)