CryptoAlgorithmRSASSA_PKCS1_v1_5GCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmRSASSA_PKCS1_v1_5.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmRsaSsaParamsDeprecated.h"
#include "CryptoKeyRSA.h"
#include "ExceptionCode.h"
#include "GCryptUtilities.h"
#include "NotImplemented.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
static std::optional<Vector<uint8_t>> gcryptSign(gcry_sexp_t keySexp, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier)
{
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 pkcs1)(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;
}
PAL::GCrypt::Handle<gcry_sexp_t> sSexp(gcry_sexp_find_token(signatureSexp, "s", 0));
if (!sSexp)
return std::nullopt;
return mpiData(sSexp);
}
static std::optional<bool> gcryptVerify(gcry_sexp_t keySexp, const Vector<uint8_t>& signature, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier)
{
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 pkcs1)(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 CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch(
[key = WTFMove(key), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
auto output = gcryptSign(rsaKey.platformKey(), data, rsaKey.hashAlgorithmIdentifier());
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 CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(Ref<CryptoKey>&& key, Vector<uint8_t>&& signature, Vector<uint8_t>&& data, BoolCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch(
[key = WTFMove(key), signature = WTFMove(signature), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
auto output = gcryptVerify(rsaKey.platformKey(), signature, data, rsaKey.hashAlgorithmIdentifier());
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();
});
});
}
ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&&)
{
notImplemented();
return Exception { NOT_SUPPORTED_ERR };
}
ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(const CryptoAlgorithmRsaSsaParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, const CryptoOperationData&, BoolCallback&&, VoidCallback&&)
{
notImplemented();
return Exception { NOT_SUPPORTED_ERR };
}
}
#endif // ENABLE(SUBTLE_CRYPTO)