CryptoAlgorithmRSAES_PKCS1_v1_5GCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmRSAES_PKCS1_v1_5.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoKeyRSA.h"
#include "ExceptionCode.h"
#include "GCryptUtilities.h"
#include "NotImplemented.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
static std::optional<Vector<uint8_t>> gcryptEncrypt(gcry_sexp_t keySexp, Vector<uint8_t>&& plainText)
{
PAL::GCrypt::Handle<gcry_sexp_t> dataSexp;
gcry_error_t error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags pkcs1)(value %b))",
plainText.size(), plainText.data());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
PAL::GCrypt::Handle<gcry_sexp_t> cipherSexp;
error = gcry_pk_encrypt(&cipherSexp, dataSexp, keySexp);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
PAL::GCrypt::Handle<gcry_sexp_t> aSexp(gcry_sexp_find_token(cipherSexp, "a", 0));
if (!aSexp)
return std::nullopt;
return mpiData(aSexp);
}
static std::optional<Vector<uint8_t>> gcryptDecrypt(gcry_sexp_t keySexp, Vector<uint8_t>&& cipherText)
{
PAL::GCrypt::Handle<gcry_sexp_t> encValSexp;
gcry_error_t error = gcry_sexp_build(&encValSexp, nullptr, "(enc-val(flags pkcs1)(rsa(a %b)))",
cipherText.size(), cipherText.data());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
PAL::GCrypt::Handle<gcry_sexp_t> plainSexp;
error = gcry_pk_decrypt(&plainSexp, encValSexp, keySexp);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
PAL::GCrypt::Handle<gcry_sexp_t> valueSexp(gcry_sexp_find_token(plainSexp, "value", 0));
if (!valueSexp)
return std::nullopt;
return mpiData(valueSexp);
}
void CryptoAlgorithmRSAES_PKCS1_v1_5::platformEncrypt(Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch(
[key = WTFMove(key), plainText = WTFMove(plainText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
auto output = gcryptEncrypt(rsaKey.platformKey(), WTFMove(plainText));
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) mutable {
callback(WTFMove(output));
context.deref();
});
});
}
void CryptoAlgorithmRSAES_PKCS1_v1_5::platformDecrypt(Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch(
[key = WTFMove(key), cipherText = WTFMove(cipherText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
auto output = gcryptDecrypt(rsaKey.platformKey(), WTFMove(cipherText));
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) mutable {
callback(WTFMove(output));
context.deref();
});
});
}
ExceptionOr<void> CryptoAlgorithmRSAES_PKCS1_v1_5::platformEncrypt(const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&&)
{
notImplemented();
return Exception { NOT_SUPPORTED_ERR };
}
ExceptionOr<void> CryptoAlgorithmRSAES_PKCS1_v1_5::platformDecrypt(const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&&)
{
notImplemented();
return Exception { NOT_SUPPORTED_ERR };
}
}
#endif // ENABLE(SUBTLE_CRYPTO)