CryptoAlgorithmRSA_PSSGCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmRSA_PSS.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmRsaPssParams.h"
#include "CryptoKeyRSA.h"
#include "ExceptionCode.h"
#include "GCryptUtilities.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
static std::optional<Vector<uint8_t>> gcryptSign(gcry_sexp_t keySexp, size_t keyLength, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t saltLength)
{
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 pss)(salt-length %u)(hash %s %b))",
saltLength, *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;
}
PAL::GCrypt::Handle<gcry_sexp_t> sSexp(gcry_sexp_find_token(signatureSexp, "s", 0));
if (!sSexp)
return std::nullopt;
auto signature = mpiData(sSexp);
if (!signature || signature->size() != keyLength / 8)
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 saltLength)
{
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(rsa(s %b)))",
signature.size(), signature.data());
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 pss)(salt-length %u)(hash %s %b))",
saltLength, *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 CryptoAlgorithmRSA_PSS::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& rsaKey = downcast<CryptoKeyRSA>(key.get());
auto& rsaParameters = downcast<CryptoAlgorithmRsaPssParams>(*parameters);
auto output = gcryptSign(rsaKey.platformKey(), rsaKey.keySizeInBits(), data, rsaKey.hashAlgorithmIdentifier(), rsaParameters.saltLength);
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 CryptoAlgorithmRSA_PSS::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& rsaKey = downcast<CryptoKeyRSA>(key.get());
auto& rsaParameters = downcast<CryptoAlgorithmRsaPssParams>(*parameters);
auto output = gcryptVerify(rsaKey.platformKey(), signature, data, rsaKey.hashAlgorithmIdentifier(), rsaParameters.saltLength);
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)