CryptoAlgorithmRSA_PSSGCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmRSA_PSS.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmRsaPssParams.h"
#include "CryptoKeyRSA.h"
#include "GCryptUtilities.h"
namespace WebCore {
static std::optional<Vector<uint8_t>> gcryptSign(gcry_sexp_t keySexp, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t saltLength, 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 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;
return mpiZeroPrefixedData(sSexp, keySizeInBytes);
}
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 };
}
ExceptionOr<Vector<uint8_t>> CryptoAlgorithmRSA_PSS::platformSign(const CryptoAlgorithmRsaPssParams& parameters, const CryptoKeyRSA& key, const Vector<uint8_t>& data)
{
RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!(key.keySizeInBits() % 8));
auto output = gcryptSign(key.platformKey(), data, key.hashAlgorithmIdentifier(), parameters.saltLength, key.keySizeInBits() / 8);
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
ExceptionOr<bool> CryptoAlgorithmRSA_PSS::platformVerify(const CryptoAlgorithmRsaPssParams& parameters, const CryptoKeyRSA& key, const Vector<uint8_t>& signature, const Vector<uint8_t>& data)
{
auto output = gcryptVerify(key.platformKey(), signature, data, key.hashAlgorithmIdentifier(), parameters.saltLength);
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
}
#endif // ENABLE(SUBTLE_CRYPTO)