diff options
Diffstat (limited to 'Source/WebCore/crypto/algorithms')
22 files changed, 2555 insertions, 0 deletions
diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.cpp new file mode 100644 index 000000000..513970448 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.cpp @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmAES_CBC.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "CryptoAlgorithmAesCbcParams.h" +#include "CryptoAlgorithmAesCbcParamsDeprecated.h" +#include "CryptoAlgorithmAesKeyGenParams.h" +#include "CryptoAlgorithmAesKeyGenParamsDeprecated.h" +#include "CryptoKeyAES.h" +#include "CryptoKeyDataOctetSequence.h" +#include "ExceptionCode.h" + +namespace WebCore { + +static const char* const ALG128 = "A128CBC"; +static const char* const ALG192 = "A192CBC"; +static const char* const ALG256 = "A256CBC"; +static const size_t IVSIZE = 16; + +static inline bool usagesAreInvalidForCryptoAlgorithmAES_CBC(CryptoKeyUsageBitmap usages) +{ + return usages & (CryptoKeyUsageSign | CryptoKeyUsageVerify | CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits); +} + +Ref<CryptoAlgorithm> CryptoAlgorithmAES_CBC::create() +{ + return adoptRef(*new CryptoAlgorithmAES_CBC); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmAES_CBC::identifier() const +{ + return s_identifier; +} + +bool CryptoAlgorithmAES_CBC::keyAlgorithmMatches(const CryptoAlgorithmAesCbcParamsDeprecated&, const CryptoKey& key) const +{ + if (key.algorithmIdentifier() != s_identifier) + return false; + ASSERT(is<CryptoKeyAES>(key)); + return true; +} + +void CryptoAlgorithmAES_CBC::encrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + ASSERT(parameters); + auto& aesParameters = downcast<CryptoAlgorithmAesCbcParams>(*parameters); + if (aesParameters.ivVector().size() != IVSIZE) { + exceptionCallback(OperationError); + return; + } + platformEncrypt(WTFMove(parameters), WTFMove(key), WTFMove(plainText), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmAES_CBC::decrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + ASSERT(parameters); + auto& aesParameters = downcast<CryptoAlgorithmAesCbcParams>(*parameters); + if (aesParameters.ivVector().size() != IVSIZE) { + exceptionCallback(OperationError); + return; + } + platformDecrypt(WTFMove(parameters), WTFMove(key), WTFMove(cipherText), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&) +{ + const auto& aesParameters = downcast<CryptoAlgorithmAesKeyGenParams>(parameters); + + if (usagesAreInvalidForCryptoAlgorithmAES_CBC(usages)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + auto result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_CBC, aesParameters.length, extractable, usages); + if (!result) { + exceptionCallback(OperationError); + return; + } + + callback(WTFMove(result)); +} + +void CryptoAlgorithmAES_CBC::importKey(SubtleCrypto::KeyFormat format, KeyData&& data, const std::unique_ptr<CryptoAlgorithmParameters>&& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + ASSERT(parameters); + if (usagesAreInvalidForCryptoAlgorithmAES_CBC(usages)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + RefPtr<CryptoKeyAES> result; + switch (format) { + case SubtleCrypto::KeyFormat::Raw: + result = CryptoKeyAES::importRaw(parameters->identifier, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + case SubtleCrypto::KeyFormat::Jwk: { + auto checkAlgCallback = [](size_t length, const String& alg) -> bool { + switch (length) { + case CryptoKeyAES::s_length128: + return alg.isNull() || alg == ALG128; + case CryptoKeyAES::s_length192: + return alg.isNull() || alg == ALG192; + case CryptoKeyAES::s_length256: + return alg.isNull() || alg == ALG256; + } + return false; + }; + result = CryptoKeyAES::importJwk(parameters->identifier, WTFMove(WTF::get<JsonWebKey>(data)), extractable, usages, WTFMove(checkAlgCallback)); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + if (!result) { + exceptionCallback(DataError); + return; + } + + callback(*result); +} + +void CryptoAlgorithmAES_CBC::exportKey(SubtleCrypto::KeyFormat format, Ref<CryptoKey>&& key, KeyDataCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + const auto& aesKey = downcast<CryptoKeyAES>(key.get()); + + if (aesKey.key().isEmpty()) { + exceptionCallback(OperationError); + return; + } + + KeyData result; + switch (format) { + case SubtleCrypto::KeyFormat::Raw: + result = Vector<uint8_t>(aesKey.key()); + break; + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey jwk = aesKey.exportJwk(); + switch (aesKey.key().size() * 8) { + case CryptoKeyAES::s_length128: + jwk.alg = String(ALG128); + break; + case CryptoKeyAES::s_length192: + jwk.alg = String(ALG192); + break; + case CryptoKeyAES::s_length256: + jwk.alg = String(ALG256); + break; + default: + ASSERT_NOT_REACHED(); + } + result = WTFMove(jwk); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + + callback(format, WTFMove(result)); +} + +ExceptionOr<void> CryptoAlgorithmAES_CBC::encrypt(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto& aesCBCParameters = downcast<CryptoAlgorithmAesCbcParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(aesCBCParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformEncrypt(aesCBCParameters, downcast<CryptoKeyAES>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmAES_CBC::decrypt(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto& aesCBCParameters = downcast<CryptoAlgorithmAesCbcParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(aesCBCParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformDecrypt(aesCBCParameters, downcast<CryptoKeyAES>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParametersDeprecated& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ScriptExecutionContext&) +{ + auto& aesParameters = downcast<CryptoAlgorithmAesKeyGenParamsDeprecated>(parameters); + + auto result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_CBC, aesParameters.length, extractable, usages); + if (!result) { + failureCallback(); + return { }; + } + + callback(WTFMove(result)); + return { }; +} + +ExceptionOr<void> CryptoAlgorithmAES_CBC::importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsageBitmap usage, KeyCallback&& callback, VoidCallback&&) +{ + if (!is<CryptoKeyDataOctetSequence>(keyData)) + return Exception { NOT_SUPPORTED_ERR }; + auto& keyDataOctetSequence = downcast<CryptoKeyDataOctetSequence>(keyData); + callback(CryptoKeyAES::create(CryptoAlgorithmIdentifier::AES_CBC, keyDataOctetSequence.octetSequence(), extractable, usage)); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.h new file mode 100644 index 000000000..e56e48e5f --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmAesCbcParamsDeprecated; +class CryptoKeyAES; + +class CryptoAlgorithmAES_CBC final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "AES-CBC"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::AES_CBC; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmAES_CBC() = default; + CryptoAlgorithmIdentifier identifier() const final; + + void encrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void decrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final; + void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final; + void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final; + + ExceptionOr<void> encrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> decrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> generateKey(const CryptoAlgorithmParametersDeprecated&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext&) final; + ExceptionOr<void> importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, VoidCallback&& failureCallback) final; + + bool keyAlgorithmMatches(const CryptoAlgorithmAesCbcParamsDeprecated& algorithmParameters, const CryptoKey&) const; + void platformEncrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + void platformDecrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + ExceptionOr<void> platformEncrypt(const CryptoAlgorithmAesCbcParamsDeprecated&, const CryptoKeyAES&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); + ExceptionOr<void> platformDecrypt(const CryptoAlgorithmAesCbcParamsDeprecated&, const CryptoKeyAES&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_KW.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_KW.cpp new file mode 100644 index 000000000..8b60efdef --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_KW.cpp @@ -0,0 +1,212 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmAES_KW.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "CryptoAlgorithmAesKeyGenParams.h" +#include "CryptoAlgorithmAesKeyGenParamsDeprecated.h" +#include "CryptoKeyAES.h" +#include "CryptoKeyDataOctetSequence.h" +#include "ExceptionCode.h" +#include <wtf/Variant.h> + +namespace WebCore { + +static const char* const ALG128 = "A128KW"; +static const char* const ALG192 = "A192KW"; +static const char* const ALG256 = "A256KW"; + +static inline bool usagesAreInvalidForCryptoAlgorithmAES_KW(CryptoKeyUsageBitmap usages) +{ + return usages & (CryptoKeyUsageSign | CryptoKeyUsageVerify | CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits | CryptoKeyUsageEncrypt | CryptoKeyUsageDecrypt); +} + +Ref<CryptoAlgorithm> CryptoAlgorithmAES_KW::create() +{ + return adoptRef(*new CryptoAlgorithmAES_KW); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmAES_KW::identifier() const +{ + return s_identifier; +} + +bool CryptoAlgorithmAES_KW::keyAlgorithmMatches(const CryptoKey& key) const +{ + if (key.algorithmIdentifier() != s_identifier) + return false; + ASSERT(is<CryptoKeyAES>(key)); + return true; +} + +void CryptoAlgorithmAES_KW::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&) +{ + if (usagesAreInvalidForCryptoAlgorithmAES_KW(usages)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + auto result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_KW, downcast<CryptoAlgorithmAesKeyGenParams>(parameters).length, extractable, usages); + if (!result) { + exceptionCallback(OperationError); + return; + } + + callback(WTFMove(result)); +} + +void CryptoAlgorithmAES_KW::importKey(SubtleCrypto::KeyFormat format, KeyData&& data, const std::unique_ptr<CryptoAlgorithmParameters>&& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + ASSERT(parameters); + if (usagesAreInvalidForCryptoAlgorithmAES_KW(usages)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + RefPtr<CryptoKeyAES> result; + switch (format) { + case SubtleCrypto::KeyFormat::Raw: + result = CryptoKeyAES::importRaw(parameters->identifier, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + case SubtleCrypto::KeyFormat::Jwk: { + result = CryptoKeyAES::importJwk(parameters->identifier, WTFMove(WTF::get<JsonWebKey>(data)), extractable, usages, [](size_t length, const String& alg) -> bool { + switch (length) { + case CryptoKeyAES::s_length128: + return alg.isNull() || alg == ALG128; + case CryptoKeyAES::s_length192: + return alg.isNull() || alg == ALG192; + case CryptoKeyAES::s_length256: + return alg.isNull() || alg == ALG256; + } + return false; + }); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + if (!result) { + exceptionCallback(DataError); + return; + } + + callback(*result); +} + +void CryptoAlgorithmAES_KW::exportKey(SubtleCrypto::KeyFormat format, Ref<CryptoKey>&& key, KeyDataCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + const auto& aesKey = downcast<CryptoKeyAES>(key.get()); + + if (aesKey.key().isEmpty()) { + exceptionCallback(OperationError); + return; + } + + KeyData result; + switch (format) { + case SubtleCrypto::KeyFormat::Raw: + result = Vector<uint8_t>(aesKey.key()); + break; + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey jwk = aesKey.exportJwk(); + switch (aesKey.key().size() * 8) { + case CryptoKeyAES::s_length128: + jwk.alg = String(ALG128); + break; + case CryptoKeyAES::s_length192: + jwk.alg = String(ALG192); + break; + case CryptoKeyAES::s_length256: + jwk.alg = String(ALG256); + break; + default: + ASSERT_NOT_REACHED(); + } + result = WTFMove(jwk); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + + callback(format, WTFMove(result)); +} + +void CryptoAlgorithmAES_KW::wrapKey(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + if (data.size() % 8) { + exceptionCallback(OperationError); + return; + } + platformWrapKey(WTFMove(key), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback)); +} + +void CryptoAlgorithmAES_KW::unwrapKey(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + platformUnwrapKey(WTFMove(key), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback)); +} + +ExceptionOr<void> CryptoAlgorithmAES_KW::encryptForWrapKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + if (!keyAlgorithmMatches(key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformEncrypt(downcast<CryptoKeyAES>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmAES_KW::decryptForUnwrapKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + if (!keyAlgorithmMatches(key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformDecrypt(downcast<CryptoKeyAES>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmAES_KW::generateKey(const CryptoAlgorithmParametersDeprecated& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ScriptExecutionContext&) +{ + auto result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_KW, downcast<CryptoAlgorithmAesKeyGenParamsDeprecated>(parameters).length, extractable, usages); + if (!result) { + failureCallback(); + return { }; + } + + callback(WTFMove(result)); + return { }; +} + +ExceptionOr<void> CryptoAlgorithmAES_KW::importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsageBitmap usage, KeyCallback&& callback, VoidCallback&&) +{ + if (!is<CryptoKeyDataOctetSequence>(keyData)) + return Exception { NOT_SUPPORTED_ERR }; + callback(CryptoKeyAES::create(CryptoAlgorithmIdentifier::AES_KW, downcast<CryptoKeyDataOctetSequence>(keyData).octetSequence(), extractable, usage)); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_KW.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_KW.h new file mode 100644 index 000000000..00294e391 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_KW.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoKeyAES; + +class CryptoAlgorithmAES_KW final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "AES-KW"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::AES_KW; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmAES_KW() = default; + CryptoAlgorithmIdentifier identifier() const final; + + void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final; + void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final; + void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final; + void wrapKey(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&) final; + void unwrapKey(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&) final; + + ExceptionOr<void> encryptForWrapKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> decryptForUnwrapKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> generateKey(const CryptoAlgorithmParametersDeprecated&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext&) final; + ExceptionOr<void> importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, VoidCallback&& failureCallback) final; + + bool keyAlgorithmMatches(const CryptoKey&) const; + void platformWrapKey(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&); + void platformUnwrapKey(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&); + ExceptionOr<void> platformEncrypt(const CryptoKeyAES&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); + ExceptionOr<void> platformDecrypt(const CryptoKeyAES&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.cpp new file mode 100644 index 000000000..be064a370 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.cpp @@ -0,0 +1,238 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmHMAC.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "CryptoAlgorithmHmacKeyParams.h" +#include "CryptoAlgorithmHmacKeyParamsDeprecated.h" +#include "CryptoAlgorithmHmacParamsDeprecated.h" +#include "CryptoKeyDataOctetSequence.h" +#include "CryptoKeyHMAC.h" +#include "ExceptionCode.h" +#include <wtf/Variant.h> + +namespace WebCore { + +static const char* const ALG1 = "HS1"; +static const char* const ALG224 = "HS224"; +static const char* const ALG256 = "HS256"; +static const char* const ALG384 = "HS384"; +static const char* const ALG512 = "HS512"; + +static inline bool usagesAreInvalidForCryptoAlgorithmHMAC(CryptoKeyUsageBitmap usages) +{ + return usages & (CryptoKeyUsageEncrypt | CryptoKeyUsageDecrypt | CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits | CryptoKeyUsageWrapKey | CryptoKeyUsageUnwrapKey); +} + +Ref<CryptoAlgorithm> CryptoAlgorithmHMAC::create() +{ + return adoptRef(*new CryptoAlgorithmHMAC); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmHMAC::identifier() const +{ + return s_identifier; +} + +bool CryptoAlgorithmHMAC::keyAlgorithmMatches(const CryptoAlgorithmHmacParamsDeprecated& parameters, const CryptoKey& key) const +{ + if (key.algorithmIdentifier() != s_identifier) + return false; + + if (downcast<CryptoKeyHMAC>(key).hashAlgorithmIdentifier() != parameters.hash) + return false; + + return true; +} + +void CryptoAlgorithmHMAC::sign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + platformSign(WTFMove(key), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmHMAC::verify(Ref<CryptoKey>&& key, Vector<uint8_t>&& signature, Vector<uint8_t>&& data, BoolCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + platformVerify(WTFMove(key), WTFMove(signature), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&) +{ + const auto& hmacParameters = downcast<CryptoAlgorithmHmacKeyParams>(parameters); + + if (usagesAreInvalidForCryptoAlgorithmHMAC(usages)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + if (hmacParameters.length && !hmacParameters.length.value()) { + exceptionCallback(OperationError); + return; + } + + auto result = CryptoKeyHMAC::generate(hmacParameters.length.value_or(0), hmacParameters.hashIdentifier, extractable, usages); + if (!result) { + exceptionCallback(OperationError); + return; + } + + callback(WTFMove(result)); +} + +void CryptoAlgorithmHMAC::importKey(SubtleCrypto::KeyFormat format, KeyData&& data, const std::unique_ptr<CryptoAlgorithmParameters>&& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + ASSERT(parameters); + const auto& hmacParameters = downcast<CryptoAlgorithmHmacKeyParams>(*parameters); + + if (usagesAreInvalidForCryptoAlgorithmHMAC(usages)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + RefPtr<CryptoKeyHMAC> result; + switch (format) { + case SubtleCrypto::KeyFormat::Raw: + result = CryptoKeyHMAC::importRaw(hmacParameters.length.value_or(0), hmacParameters.hashIdentifier, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + case SubtleCrypto::KeyFormat::Jwk: { + auto checkAlgCallback = [](CryptoAlgorithmIdentifier hash, const String& alg) -> bool { + switch (hash) { + case CryptoAlgorithmIdentifier::SHA_1: + return alg.isNull() || alg == ALG1; + case CryptoAlgorithmIdentifier::SHA_224: + return alg.isNull() || alg == ALG224; + case CryptoAlgorithmIdentifier::SHA_256: + return alg.isNull() || alg == ALG256; + case CryptoAlgorithmIdentifier::SHA_384: + return alg.isNull() || alg == ALG384; + case CryptoAlgorithmIdentifier::SHA_512: + return alg.isNull() || alg == ALG512; + default: + return false; + } + return false; + }; + result = CryptoKeyHMAC::importJwk(hmacParameters.length.value_or(0), hmacParameters.hashIdentifier, WTFMove(WTF::get<JsonWebKey>(data)), extractable, usages, WTFMove(checkAlgCallback)); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + if (!result) { + exceptionCallback(DataError); + return; + } + + callback(*result); +} + +void CryptoAlgorithmHMAC::exportKey(SubtleCrypto::KeyFormat format, Ref<CryptoKey>&& key, KeyDataCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + const auto& hmacKey = downcast<CryptoKeyHMAC>(key.get()); + + if (hmacKey.key().isEmpty()) { + exceptionCallback(OperationError); + return; + } + + KeyData result; + switch (format) { + case SubtleCrypto::KeyFormat::Raw: + result = Vector<uint8_t>(hmacKey.key()); + break; + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey jwk = hmacKey.exportJwk(); + switch (hmacKey.hashAlgorithmIdentifier()) { + case CryptoAlgorithmIdentifier::SHA_1: + jwk.alg = String(ALG1); + break; + case CryptoAlgorithmIdentifier::SHA_224: + jwk.alg = String(ALG224); + break; + case CryptoAlgorithmIdentifier::SHA_256: + jwk.alg = String(ALG256); + break; + case CryptoAlgorithmIdentifier::SHA_384: + jwk.alg = String(ALG384); + break; + case CryptoAlgorithmIdentifier::SHA_512: + jwk.alg = String(ALG512); + break; + default: + ASSERT_NOT_REACHED(); + } + result = WTFMove(jwk); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + + callback(format, WTFMove(result)); +} + +ExceptionOr<void> CryptoAlgorithmHMAC::sign(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto& hmacParameters = downcast<CryptoAlgorithmHmacParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(hmacParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformSign(hmacParameters, downcast<CryptoKeyHMAC>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmHMAC::verify(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, BoolCallback&& callback, VoidCallback&& failureCallback) +{ + auto& hmacParameters = downcast<CryptoAlgorithmHmacParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(hmacParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformVerify(hmacParameters, downcast<CryptoKeyHMAC>(key), expectedSignature, data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParametersDeprecated& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ScriptExecutionContext&) +{ + auto& hmacParameters = downcast<CryptoAlgorithmHmacKeyParamsDeprecated>(parameters); + auto result = CryptoKeyHMAC::generate(hmacParameters.hasLength ? hmacParameters.length : 0, hmacParameters.hash, extractable, usages); + if (!result) { + failureCallback(); + return { }; + } + callback(WTFMove(result)); + return { }; +} + +ExceptionOr<void> CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsageBitmap usage, KeyCallback&& callback, VoidCallback&&) +{ + if (!is<CryptoKeyDataOctetSequence>(keyData)) + return Exception { NOT_SUPPORTED_ERR }; + callback(CryptoKeyHMAC::create(downcast<CryptoKeyDataOctetSequence>(keyData).octetSequence(), downcast<CryptoAlgorithmHmacParamsDeprecated>(parameters).hash, extractable, usage)); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.h new file mode 100644 index 000000000..3ad69c92e --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmHmacParamsDeprecated; +class CryptoKeyHMAC; + +class CryptoAlgorithmHMAC final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "HMAC"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::HMAC; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmHMAC() = default; + CryptoAlgorithmIdentifier identifier() const final; + + void sign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void verify(Ref<CryptoKey>&&, Vector<uint8_t>&& signature, Vector<uint8_t>&&, BoolCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final; + void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final; + void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final; + + // The following will be deprecated. + ExceptionOr<void> sign(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> verify(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData&, BoolCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> generateKey(const CryptoAlgorithmParametersDeprecated&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext&) final; + ExceptionOr<void> importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, VoidCallback&& failureCallback) final; + + bool keyAlgorithmMatches(const CryptoAlgorithmHmacParamsDeprecated& algorithmParameters, const CryptoKey&) const; + void platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + void platformVerify(Ref<CryptoKey>&&, Vector<uint8_t>&& signature, Vector<uint8_t>&&, BoolCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + ExceptionOr<void> platformSign(const CryptoAlgorithmHmacParamsDeprecated&, const CryptoKeyHMAC&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); + ExceptionOr<void> platformVerify(const CryptoAlgorithmHmacParamsDeprecated&, const CryptoKeyHMAC&, const CryptoOperationData& signature, const CryptoOperationData&, BoolCallback&&, VoidCallback&& failureCallback); +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSAES_PKCS1_v1_5.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSAES_PKCS1_v1_5.cpp new file mode 100644 index 000000000..8a8b4f2f8 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSAES_PKCS1_v1_5.cpp @@ -0,0 +1,234 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmRSAES_PKCS1_v1_5.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "CryptoAlgorithmRsaKeyGenParams.h" +#include "CryptoAlgorithmRsaKeyGenParamsDeprecated.h" +#include "CryptoAlgorithmRsaKeyParamsWithHashDeprecated.h" +#include "CryptoKeyDataRSAComponents.h" +#include "CryptoKeyPair.h" +#include "CryptoKeyRSA.h" +#include "ExceptionCode.h" +#include <wtf/Variant.h> + +namespace WebCore { + +static const char* const ALG = "RSA1_5"; + +Ref<CryptoAlgorithm> CryptoAlgorithmRSAES_PKCS1_v1_5::create() +{ + return adoptRef(*new CryptoAlgorithmRSAES_PKCS1_v1_5); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmRSAES_PKCS1_v1_5::identifier() const +{ + return s_identifier; +} + +bool CryptoAlgorithmRSAES_PKCS1_v1_5::keyAlgorithmMatches(const CryptoKey& key) const +{ + if (key.algorithmIdentifier() != s_identifier) + return false; + ASSERT(is<CryptoKeyRSA>(key)); + + return true; +} + +void CryptoAlgorithmRSAES_PKCS1_v1_5::encrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + if (key->type() != CryptoKeyType::Public) { + exceptionCallback(INVALID_ACCESS_ERR); + return; + } + platformEncrypt(WTFMove(key), WTFMove(plainText), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmRSAES_PKCS1_v1_5::decrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + if (key->type() != CryptoKeyType::Private) { + exceptionCallback(INVALID_ACCESS_ERR); + return; + } + platformDecrypt(WTFMove(key), WTFMove(cipherText), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmRSAES_PKCS1_v1_5::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context) +{ + const auto& rsaParameters = downcast<CryptoAlgorithmRsaKeyGenParams>(parameters); + + if (usages & (CryptoKeyUsageSign | CryptoKeyUsageVerify | CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits | CryptoKeyUsageWrapKey | CryptoKeyUsageUnwrapKey)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + auto keyPairCallback = [capturedCallback = WTFMove(callback)](CryptoKeyPair&& pair) { + pair.publicKey->setUsagesBitmap(pair.publicKey->usagesBitmap() & CryptoKeyUsageEncrypt); + pair.privateKey->setUsagesBitmap(pair.privateKey->usagesBitmap() & CryptoKeyUsageDecrypt); + capturedCallback(WTFMove(pair)); + }; + auto failureCallback = [capturedCallback = WTFMove(exceptionCallback)]() { + capturedCallback(OperationError); + }; + // Notice: CryptoAlgorithmIdentifier::SHA_1 is just a placeholder. It should not have any effect. + CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5, CryptoAlgorithmIdentifier::SHA_1, false, rsaParameters.modulusLength, rsaParameters.publicExponentVector(), extractable, usages, WTFMove(keyPairCallback), WTFMove(failureCallback), &context); +} + +void CryptoAlgorithmRSAES_PKCS1_v1_5::importKey(SubtleCrypto::KeyFormat format, KeyData&& data, const std::unique_ptr<CryptoAlgorithmParameters>&& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + ASSERT(parameters); + RefPtr<CryptoKeyRSA> result; + switch (format) { + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey key = WTFMove(WTF::get<JsonWebKey>(data)); + if (usages && ((!key.d.isNull() && (usages ^ CryptoKeyUsageDecrypt)) || (key.d.isNull() && (usages ^ CryptoKeyUsageEncrypt)))) { + exceptionCallback(SYNTAX_ERR); + return; + } + if (usages && !key.use.isNull() && key.use != "enc") { + exceptionCallback(DataError); + return; + } + if (!key.alg.isNull() && key.alg != ALG) { + exceptionCallback(DataError); + return; + } + result = CryptoKeyRSA::importJwk(parameters->identifier, std::nullopt, WTFMove(key), extractable, usages); + break; + } + case SubtleCrypto::KeyFormat::Spki: { + if (usages && (usages ^ CryptoKeyUsageEncrypt)) { + exceptionCallback(SYNTAX_ERR); + return; + } + result = CryptoKeyRSA::importSpki(parameters->identifier, std::nullopt, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + } + case SubtleCrypto::KeyFormat::Pkcs8: { + if (usages && (usages ^ CryptoKeyUsageDecrypt)) { + exceptionCallback(SYNTAX_ERR); + return; + } + result = CryptoKeyRSA::importPkcs8(parameters->identifier, std::nullopt, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + if (!result) { + exceptionCallback(DataError); + return; + } + + callback(*result); +} + +void CryptoAlgorithmRSAES_PKCS1_v1_5::exportKey(SubtleCrypto::KeyFormat format, Ref<CryptoKey>&& key, KeyDataCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + const auto& rsaKey = downcast<CryptoKeyRSA>(key.get()); + + if (!rsaKey.keySizeInBits()) { + exceptionCallback(OperationError); + return; + } + + KeyData result; + switch (format) { + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey jwk = rsaKey.exportJwk(); + jwk.alg = String(ALG); + result = WTFMove(jwk); + break; + } + case SubtleCrypto::KeyFormat::Spki: { + auto spki = rsaKey.exportSpki(); + if (spki.hasException()) { + exceptionCallback(spki.releaseException().code()); + return; + } + result = spki.releaseReturnValue(); + break; + } + case SubtleCrypto::KeyFormat::Pkcs8: { + auto pkcs8 = rsaKey.exportPkcs8(); + if (pkcs8.hasException()) { + exceptionCallback(pkcs8.releaseException().code()); + return; + } + result = pkcs8.releaseReturnValue(); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + + callback(format, WTFMove(result)); +} + +ExceptionOr<void> CryptoAlgorithmRSAES_PKCS1_v1_5::encrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + if (!keyAlgorithmMatches(key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformEncrypt(downcast<CryptoKeyRSA>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmRSAES_PKCS1_v1_5::decrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + if (!keyAlgorithmMatches(key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformDecrypt(downcast<CryptoKeyRSA>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmRSAES_PKCS1_v1_5::generateKey(const CryptoAlgorithmParametersDeprecated& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ScriptExecutionContext& context) +{ + auto& rsaParameters = downcast<CryptoAlgorithmRsaKeyGenParamsDeprecated>(parameters); + auto keyPairCallback = [capturedCallback = WTFMove(callback)](CryptoKeyPair&& pair) { + capturedCallback(WTFMove(pair)); + }; + CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5, rsaParameters.hash, rsaParameters.hasHash, rsaParameters.modulusLength, rsaParameters.publicExponent, extractable, usages, WTFMove(keyPairCallback), WTFMove(failureCallback), &context); + return { }; +} + +ExceptionOr<void> CryptoAlgorithmRSAES_PKCS1_v1_5::importKey(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsageBitmap usage, KeyCallback&& callback, VoidCallback&& failureCallback) +{ + auto& rsaParameters = downcast<CryptoAlgorithmRsaKeyParamsWithHashDeprecated>(parameters); + auto& rsaComponents = downcast<CryptoKeyDataRSAComponents>(keyData); + auto result = CryptoKeyRSA::create(CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5, rsaParameters.hash, rsaParameters.hasHash, rsaComponents, extractable, usage); + if (!result) { + failureCallback(); + return { }; + } + callback(*result); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSAES_PKCS1_v1_5.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSAES_PKCS1_v1_5.h new file mode 100644 index 000000000..a988f947c --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSAES_PKCS1_v1_5.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmRSAESParams; +class CryptoKeyRSA; + +class CryptoAlgorithmRSAES_PKCS1_v1_5 final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "RSAES-PKCS1-v1_5"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmRSAES_PKCS1_v1_5() = default; + CryptoAlgorithmIdentifier identifier() const final; + + void encrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void decrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final; + void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final; + void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final; + + ExceptionOr<void> encrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> decrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> generateKey(const CryptoAlgorithmParametersDeprecated&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext&) final; + ExceptionOr<void> importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, VoidCallback&& failureCallback) final; + + bool keyAlgorithmMatches(const CryptoKey&) const; + void platformEncrypt(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + void platformDecrypt(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + ExceptionOr<void> platformEncrypt(const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); + ExceptionOr<void> platformDecrypt(const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp new file mode 100644 index 000000000..e3e9ed053 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp @@ -0,0 +1,289 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmRSASSA_PKCS1_v1_5.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "CryptoAlgorithmRsaHashedImportParams.h" +#include "CryptoAlgorithmRsaHashedKeyGenParams.h" +#include "CryptoAlgorithmRsaKeyGenParamsDeprecated.h" +#include "CryptoAlgorithmRsaKeyParamsWithHashDeprecated.h" +#include "CryptoAlgorithmRsaSsaParamsDeprecated.h" +#include "CryptoKeyDataRSAComponents.h" +#include "CryptoKeyPair.h" +#include "CryptoKeyRSA.h" +#include "ExceptionCode.h" +#include <wtf/Variant.h> + +namespace WebCore { + +static const char* const ALG1 = "RS1"; +static const char* const ALG224 = "RS224"; +static const char* const ALG256 = "RS256"; +static const char* const ALG384 = "RS384"; +static const char* const ALG512 = "RS512"; + +Ref<CryptoAlgorithm> CryptoAlgorithmRSASSA_PKCS1_v1_5::create() +{ + return adoptRef(*new CryptoAlgorithmRSASSA_PKCS1_v1_5); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmRSASSA_PKCS1_v1_5::identifier() const +{ + return s_identifier; +} + +bool CryptoAlgorithmRSASSA_PKCS1_v1_5::keyAlgorithmMatches(const CryptoAlgorithmRsaSsaParamsDeprecated& algorithmParameters, const CryptoKey& key) const +{ + if (key.algorithmIdentifier() != s_identifier) + return false; + + CryptoAlgorithmIdentifier keyHash; + if (downcast<CryptoKeyRSA>(key).isRestrictedToHash(keyHash) && keyHash != algorithmParameters.hash) + return false; + + return true; +} + +void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + if (key->type() != CryptoKeyType::Private) { + exceptionCallback(INVALID_ACCESS_ERR); + return; + } + platformSign(WTFMove(key), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(Ref<CryptoKey>&& key, Vector<uint8_t>&& signature, Vector<uint8_t>&& data, BoolCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + if (key->type() != CryptoKeyType::Public) { + exceptionCallback(INVALID_ACCESS_ERR); + return; + } + platformVerify(WTFMove(key), WTFMove(signature), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context) +{ + const auto& rsaParameters = downcast<CryptoAlgorithmRsaHashedKeyGenParams>(parameters); + + if (usages & (CryptoKeyUsageDecrypt | CryptoKeyUsageEncrypt | CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits | CryptoKeyUsageWrapKey | CryptoKeyUsageUnwrapKey)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + auto keyPairCallback = [capturedCallback = WTFMove(callback)](CryptoKeyPair&& pair) { + pair.publicKey->setUsagesBitmap(pair.publicKey->usagesBitmap() & CryptoKeyUsageVerify); + pair.privateKey->setUsagesBitmap(pair.privateKey->usagesBitmap() & CryptoKeyUsageSign); + capturedCallback(WTFMove(pair)); + }; + auto failureCallback = [capturedCallback = WTFMove(exceptionCallback)]() { + capturedCallback(OperationError); + }; + CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaParameters.hashIdentifier, true, rsaParameters.modulusLength, rsaParameters.publicExponentVector(), extractable, usages, WTFMove(keyPairCallback), WTFMove(failureCallback), &context); +} + +void CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey(SubtleCrypto::KeyFormat format, KeyData&& data, const std::unique_ptr<CryptoAlgorithmParameters>&& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + ASSERT(parameters); + const auto& rsaParameters = downcast<CryptoAlgorithmRsaHashedImportParams>(*parameters); + + RefPtr<CryptoKeyRSA> result; + switch (format) { + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey key = WTFMove(WTF::get<JsonWebKey>(data)); + + if (usages && ((!key.d.isNull() && (usages ^ CryptoKeyUsageSign)) || (key.d.isNull() && (usages ^ CryptoKeyUsageVerify)))) { + exceptionCallback(SYNTAX_ERR); + return; + } + if (usages && !key.use.isNull() && key.use != "sig") { + exceptionCallback(DataError); + return; + } + + bool isMatched = false; + switch (rsaParameters.hashIdentifier) { + case CryptoAlgorithmIdentifier::SHA_1: + isMatched = key.alg.isNull() || key.alg == ALG1; + break; + case CryptoAlgorithmIdentifier::SHA_224: + isMatched = key.alg.isNull() || key.alg == ALG224; + break; + case CryptoAlgorithmIdentifier::SHA_256: + isMatched = key.alg.isNull() || key.alg == ALG256; + break; + case CryptoAlgorithmIdentifier::SHA_384: + isMatched = key.alg.isNull() || key.alg == ALG384; + break; + case CryptoAlgorithmIdentifier::SHA_512: + isMatched = key.alg.isNull() || key.alg == ALG512; + break; + default: + break; + } + if (!isMatched) { + exceptionCallback(DataError); + return; + } + + result = CryptoKeyRSA::importJwk(rsaParameters.identifier, rsaParameters.hashIdentifier, WTFMove(key), extractable, usages); + break; + } + case SubtleCrypto::KeyFormat::Spki: { + if (usages && (usages ^ CryptoKeyUsageVerify)) { + exceptionCallback(SYNTAX_ERR); + return; + } + // FIXME: <webkit.org/b/165436> + result = CryptoKeyRSA::importSpki(rsaParameters.identifier, rsaParameters.hashIdentifier, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + } + case SubtleCrypto::KeyFormat::Pkcs8: { + if (usages && (usages ^ CryptoKeyUsageSign)) { + exceptionCallback(SYNTAX_ERR); + return; + } + // FIXME: <webkit.org/b/165436> + result = CryptoKeyRSA::importPkcs8(parameters->identifier, rsaParameters.hashIdentifier, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + if (!result) { + exceptionCallback(DataError); + return; + } + + callback(*result); +} + +void CryptoAlgorithmRSASSA_PKCS1_v1_5::exportKey(SubtleCrypto::KeyFormat format, Ref<CryptoKey>&& key, KeyDataCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + const auto& rsaKey = downcast<CryptoKeyRSA>(key.get()); + + if (!rsaKey.keySizeInBits()) { + exceptionCallback(OperationError); + return; + } + + KeyData result; + switch (format) { + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey jwk = rsaKey.exportJwk(); + switch (rsaKey.hashAlgorithmIdentifier()) { + case CryptoAlgorithmIdentifier::SHA_1: + jwk.alg = String(ALG1); + break; + case CryptoAlgorithmIdentifier::SHA_224: + jwk.alg = String(ALG224); + break; + case CryptoAlgorithmIdentifier::SHA_256: + jwk.alg = String(ALG256); + break; + case CryptoAlgorithmIdentifier::SHA_384: + jwk.alg = String(ALG384); + break; + case CryptoAlgorithmIdentifier::SHA_512: + jwk.alg = String(ALG512); + break; + default: + ASSERT_NOT_REACHED(); + } + result = WTFMove(jwk); + break; + } + case SubtleCrypto::KeyFormat::Spki: { + auto spki = rsaKey.exportSpki(); + if (spki.hasException()) { + exceptionCallback(spki.releaseException().code()); + return; + } + result = spki.releaseReturnValue(); + break; + } + case SubtleCrypto::KeyFormat::Pkcs8: { + auto pkcs8 = rsaKey.exportPkcs8(); + if (pkcs8.hasException()) { + exceptionCallback(pkcs8.releaseException().code()); + return; + } + result = pkcs8.releaseReturnValue(); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + + callback(format, WTFMove(result)); +} + +ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto& rsaSSAParameters = downcast<CryptoAlgorithmRsaSsaParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(rsaSSAParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformSign(rsaSSAParameters, downcast<CryptoKeyRSA>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback&& callback, VoidCallback&& failureCallback) +{ + auto& rsaSSAParameters = downcast<CryptoAlgorithmRsaSsaParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(rsaSSAParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformVerify(rsaSSAParameters, downcast<CryptoKeyRSA>(key), signature, data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey(const CryptoAlgorithmParametersDeprecated& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ScriptExecutionContext& context) +{ + auto& rsaParameters = downcast<CryptoAlgorithmRsaKeyGenParamsDeprecated>(parameters); + auto keyPairCallback = [capturedCallback = WTFMove(callback)](CryptoKeyPair&& pair) { + capturedCallback(WTFMove(pair)); + }; + CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaParameters.hash, rsaParameters.hasHash, rsaParameters.modulusLength, rsaParameters.publicExponent, extractable, usages, WTFMove(keyPairCallback), WTFMove(failureCallback), &context); + return { }; +} + +ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsageBitmap usage, KeyCallback&& callback, VoidCallback&& failureCallback) +{ + auto& rsaKeyParameters = downcast<CryptoAlgorithmRsaKeyParamsWithHashDeprecated>(parameters); + auto& rsaComponents = downcast<CryptoKeyDataRSAComponents>(keyData); + auto result = CryptoKeyRSA::create(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaKeyParameters.hash, rsaKeyParameters.hasHash, rsaComponents, extractable, usage); + if (!result) { + failureCallback(); + return { }; + } + callback(*result); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h new file mode 100644 index 000000000..01d8693b5 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmRsaSsaParamsDeprecated; +class CryptoKeyRSA; + +class CryptoAlgorithmRSASSA_PKCS1_v1_5 final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "RSASSA-PKCS1-v1_5"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmRSASSA_PKCS1_v1_5() = default; + CryptoAlgorithmIdentifier identifier() const final; + + void sign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void verify(Ref<CryptoKey>&&, Vector<uint8_t>&& signature, Vector<uint8_t>&&, BoolCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final; + void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final; + void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final; + + ExceptionOr<void> sign(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> verify(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData& signature, const CryptoOperationData&, BoolCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> generateKey(const CryptoAlgorithmParametersDeprecated&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext&) final; + ExceptionOr<void> importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, VoidCallback&& failureCallback) final; + + bool keyAlgorithmMatches(const CryptoAlgorithmRsaSsaParamsDeprecated& algorithmParameters, const CryptoKey&) const; + void platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + void platformVerify(Ref<CryptoKey>&&, Vector<uint8_t>&& signature, Vector<uint8_t>&&, BoolCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + ExceptionOr<void> platformSign(const CryptoAlgorithmRsaSsaParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); + ExceptionOr<void> platformVerify(const CryptoAlgorithmRsaSsaParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData& signature, const CryptoOperationData&, BoolCallback&&, VoidCallback&& failureCallback); +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSA_OAEP.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSA_OAEP.cpp new file mode 100644 index 000000000..df4d30aa8 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSA_OAEP.cpp @@ -0,0 +1,305 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmRSA_OAEP.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "CryptoAlgorithmRsaHashedImportParams.h" +#include "CryptoAlgorithmRsaHashedKeyGenParams.h" +#include "CryptoAlgorithmRsaKeyGenParamsDeprecated.h" +#include "CryptoAlgorithmRsaKeyParamsWithHashDeprecated.h" +#include "CryptoAlgorithmRsaOaepParamsDeprecated.h" +#include "CryptoKeyDataRSAComponents.h" +#include "CryptoKeyPair.h" +#include "CryptoKeyRSA.h" +#include "ExceptionCode.h" +#include <wtf/Variant.h> + +namespace WebCore { + +static const char* const ALG1 = "RSA-OAEP"; +static const char* const ALG224 = "RSA-OAEP-224"; +static const char* const ALG256 = "RSA-OAEP-256"; +static const char* const ALG384 = "RSA-OAEP-384"; +static const char* const ALG512 = "RSA-OAEP-512"; + +Ref<CryptoAlgorithm> CryptoAlgorithmRSA_OAEP::create() +{ + return adoptRef(*new CryptoAlgorithmRSA_OAEP); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmRSA_OAEP::identifier() const +{ + return s_identifier; +} + +bool CryptoAlgorithmRSA_OAEP::keyAlgorithmMatches(const CryptoAlgorithmRsaOaepParamsDeprecated& algorithmParameters, const CryptoKey& key) const +{ + if (key.algorithmIdentifier() != s_identifier) + return false; + + CryptoAlgorithmIdentifier keyHash; + if (downcast<CryptoKeyRSA>(key).isRestrictedToHash(keyHash) && keyHash != algorithmParameters.hash) + return false; + + return true; +} + +void CryptoAlgorithmRSA_OAEP::encrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + ASSERT(parameters); + if (key->type() != CryptoKeyType::Public) { + exceptionCallback(INVALID_ACCESS_ERR); + return; + } + platformEncrypt(WTFMove(parameters), WTFMove(key), WTFMove(plainText), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmRSA_OAEP::decrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + ASSERT(parameters); + if (key->type() != CryptoKeyType::Private) { + exceptionCallback(INVALID_ACCESS_ERR); + return; + } + platformDecrypt(WTFMove(parameters), WTFMove(key), WTFMove(cipherText), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue); +} + +void CryptoAlgorithmRSA_OAEP::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context) +{ + const auto& rsaParameters = downcast<CryptoAlgorithmRsaHashedKeyGenParams>(parameters); + + if (usages & (CryptoKeyUsageSign | CryptoKeyUsageVerify | CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits)) { + exceptionCallback(SYNTAX_ERR); + return; + } + + auto keyPairCallback = [capturedCallback = WTFMove(callback)](CryptoKeyPair&& pair) { + pair.publicKey->setUsagesBitmap(pair.publicKey->usagesBitmap() & (CryptoKeyUsageEncrypt | CryptoKeyUsageWrapKey)); + pair.privateKey->setUsagesBitmap(pair.privateKey->usagesBitmap() & (CryptoKeyUsageDecrypt | CryptoKeyUsageUnwrapKey)); + capturedCallback(WTFMove(pair)); + }; + auto failureCallback = [capturedCallback = WTFMove(exceptionCallback)]() { + capturedCallback(OperationError); + }; + CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSA_OAEP, rsaParameters.hashIdentifier, true, rsaParameters.modulusLength, rsaParameters.publicExponentVector(), extractable, usages, WTFMove(keyPairCallback), WTFMove(failureCallback), &context); +} + +void CryptoAlgorithmRSA_OAEP::importKey(SubtleCrypto::KeyFormat format, KeyData&& data, const std::unique_ptr<CryptoAlgorithmParameters>&& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + ASSERT(parameters); + const auto& rsaParameters = downcast<CryptoAlgorithmRsaHashedImportParams>(*parameters); + + RefPtr<CryptoKeyRSA> result; + switch (format) { + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey key = WTFMove(WTF::get<JsonWebKey>(data)); + + bool isUsagesAllowed = false; + if (!key.d.isNull()) { + isUsagesAllowed = isUsagesAllowed || !(usages ^ CryptoKeyUsageDecrypt); + isUsagesAllowed = isUsagesAllowed || !(usages ^ CryptoKeyUsageUnwrapKey); + isUsagesAllowed = isUsagesAllowed || !(usages ^ (CryptoKeyUsageDecrypt | CryptoKeyUsageUnwrapKey)); + } else { + isUsagesAllowed = isUsagesAllowed || !(usages ^ CryptoKeyUsageEncrypt); + isUsagesAllowed = isUsagesAllowed || !(usages ^ CryptoKeyUsageWrapKey); + isUsagesAllowed = isUsagesAllowed || !(usages ^ (CryptoKeyUsageEncrypt | CryptoKeyUsageWrapKey)); + } + isUsagesAllowed = isUsagesAllowed || !usages; + if (!isUsagesAllowed) { + exceptionCallback(SYNTAX_ERR); + return; + } + + if (usages && !key.use.isNull() && key.use != "enc") { + exceptionCallback(DataError); + return; + } + + bool isMatched = false; + switch (rsaParameters.hashIdentifier) { + case CryptoAlgorithmIdentifier::SHA_1: + isMatched = key.alg.isNull() || key.alg == ALG1; + break; + case CryptoAlgorithmIdentifier::SHA_224: + isMatched = key.alg.isNull() || key.alg == ALG224; + break; + case CryptoAlgorithmIdentifier::SHA_256: + isMatched = key.alg.isNull() || key.alg == ALG256; + break; + case CryptoAlgorithmIdentifier::SHA_384: + isMatched = key.alg.isNull() || key.alg == ALG384; + break; + case CryptoAlgorithmIdentifier::SHA_512: + isMatched = key.alg.isNull() || key.alg == ALG512; + break; + default: + break; + } + if (!isMatched) { + exceptionCallback(DataError); + return; + } + + result = CryptoKeyRSA::importJwk(rsaParameters.identifier, rsaParameters.hashIdentifier, WTFMove(key), extractable, usages); + break; + } + case SubtleCrypto::KeyFormat::Spki: { + if (usages && (usages ^ CryptoKeyUsageEncrypt) && (usages ^ CryptoKeyUsageWrapKey) && (usages ^ (CryptoKeyUsageEncrypt | CryptoKeyUsageWrapKey))) { + exceptionCallback(SYNTAX_ERR); + return; + } + // FIXME: <webkit.org/b/165436> + result = CryptoKeyRSA::importSpki(rsaParameters.identifier, rsaParameters.hashIdentifier, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + } + case SubtleCrypto::KeyFormat::Pkcs8: { + if (usages && (usages ^ CryptoKeyUsageDecrypt) && (usages ^ CryptoKeyUsageUnwrapKey) && (usages ^ (CryptoKeyUsageDecrypt | CryptoKeyUsageUnwrapKey))) { + exceptionCallback(SYNTAX_ERR); + return; + } + // FIXME: <webkit.org/b/165436> + result = CryptoKeyRSA::importPkcs8(parameters->identifier, rsaParameters.hashIdentifier, WTFMove(WTF::get<Vector<uint8_t>>(data)), extractable, usages); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + if (!result) { + exceptionCallback(DataError); + return; + } + + callback(*result); +} + +void CryptoAlgorithmRSA_OAEP::exportKey(SubtleCrypto::KeyFormat format, Ref<CryptoKey>&& key, KeyDataCallback&& callback, ExceptionCallback&& exceptionCallback) +{ + const auto& rsaKey = downcast<CryptoKeyRSA>(key.get()); + + if (!rsaKey.keySizeInBits()) { + exceptionCallback(OperationError); + return; + } + + KeyData result; + switch (format) { + case SubtleCrypto::KeyFormat::Jwk: { + JsonWebKey jwk = rsaKey.exportJwk(); + switch (rsaKey.hashAlgorithmIdentifier()) { + case CryptoAlgorithmIdentifier::SHA_1: + jwk.alg = String(ALG1); + break; + case CryptoAlgorithmIdentifier::SHA_224: + jwk.alg = String(ALG224); + break; + case CryptoAlgorithmIdentifier::SHA_256: + jwk.alg = String(ALG256); + break; + case CryptoAlgorithmIdentifier::SHA_384: + jwk.alg = String(ALG384); + break; + case CryptoAlgorithmIdentifier::SHA_512: + jwk.alg = String(ALG512); + break; + default: + ASSERT_NOT_REACHED(); + } + result = WTFMove(jwk); + break; + } + case SubtleCrypto::KeyFormat::Spki: { + // FIXME: <webkit.org/b/165437> + auto spki = rsaKey.exportSpki(); + if (spki.hasException()) { + exceptionCallback(spki.releaseException().code()); + return; + } + result = spki.releaseReturnValue(); + break; + } + case SubtleCrypto::KeyFormat::Pkcs8: { + // FIXME: <webkit.org/b/165437> + auto pkcs8 = rsaKey.exportPkcs8(); + if (pkcs8.hasException()) { + exceptionCallback(pkcs8.releaseException().code()); + return; + } + result = pkcs8.releaseReturnValue(); + break; + } + default: + exceptionCallback(NOT_SUPPORTED_ERR); + return; + } + + callback(format, WTFMove(result)); +} + +ExceptionOr<void> CryptoAlgorithmRSA_OAEP::encrypt(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto& rsaOAEPParameters = downcast<CryptoAlgorithmRsaOaepParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(rsaOAEPParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformEncrypt(rsaOAEPParameters, downcast<CryptoKeyRSA>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmRSA_OAEP::decrypt(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto& rsaOAEPParameters = downcast<CryptoAlgorithmRsaOaepParamsDeprecated>(parameters); + if (!keyAlgorithmMatches(rsaOAEPParameters, key)) + return Exception { NOT_SUPPORTED_ERR }; + return platformDecrypt(rsaOAEPParameters, downcast<CryptoKeyRSA>(key), data, WTFMove(callback), WTFMove(failureCallback)); +} + +ExceptionOr<void> CryptoAlgorithmRSA_OAEP::generateKey(const CryptoAlgorithmParametersDeprecated& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ScriptExecutionContext& context) +{ + auto& rsaParameters = downcast<CryptoAlgorithmRsaKeyGenParamsDeprecated>(parameters); + auto keyPairCallback = [capturedCallback = WTFMove(callback)](CryptoKeyPair&& pair) { + capturedCallback(WTFMove(pair)); + }; + CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSA_OAEP, rsaParameters.hash, rsaParameters.hasHash, rsaParameters.modulusLength, rsaParameters.publicExponent, extractable, usages, WTFMove(keyPairCallback), WTFMove(failureCallback), &context); + return { }; +} + +ExceptionOr<void> CryptoAlgorithmRSA_OAEP::importKey(const CryptoAlgorithmParametersDeprecated& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsageBitmap usage, KeyCallback&& callback, VoidCallback&& failureCallback) +{ + auto& rsaKeyParameters = downcast<CryptoAlgorithmRsaKeyParamsWithHashDeprecated>(parameters); + auto& rsaComponents = downcast<CryptoKeyDataRSAComponents>(keyData); + auto result = CryptoKeyRSA::create(CryptoAlgorithmIdentifier::RSA_OAEP, rsaKeyParameters.hash, rsaKeyParameters.hasHash, rsaComponents, extractable, usage); + if (!result) { + failureCallback(); + return { }; + } + callback(*result); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSA_OAEP.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSA_OAEP.h new file mode 100644 index 000000000..4aea6a4b8 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSA_OAEP.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmRsaOaepParamsDeprecated; +class CryptoKeyRSA; + +class CryptoAlgorithmRSA_OAEP final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "RSA-OAEP"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::RSA_OAEP; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmRSA_OAEP() = default; + CryptoAlgorithmIdentifier identifier() const final; + + void encrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void decrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final; + void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final; + void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final; + + ExceptionOr<void> encrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> decrypt(const CryptoAlgorithmParametersDeprecated&, const CryptoKey&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; + ExceptionOr<void> generateKey(const CryptoAlgorithmParametersDeprecated&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, VoidCallback&& failureCallback, ScriptExecutionContext&) final; + ExceptionOr<void> importKey(const CryptoAlgorithmParametersDeprecated&, const CryptoKeyData&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, VoidCallback&& failureCallback) final; + + bool keyAlgorithmMatches(const CryptoAlgorithmRsaOaepParamsDeprecated& algorithmParameters, const CryptoKey&) const; + void platformEncrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + void platformDecrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + ExceptionOr<void> platformEncrypt(const CryptoAlgorithmRsaOaepParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); + ExceptionOr<void> platformDecrypt(const CryptoAlgorithmRsaOaepParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback); +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.cpp new file mode 100644 index 000000000..addf84d2b --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmSHA1.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "ExceptionCode.h" +#include "ScriptExecutionContext.h" +#include <pal/crypto/CryptoDigest.h> + +namespace WebCore { + +Ref<CryptoAlgorithm> CryptoAlgorithmSHA1::create() +{ + return adoptRef(*new CryptoAlgorithmSHA1); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmSHA1::identifier() const +{ + return s_identifier; +} + +void CryptoAlgorithmSHA1::digest(Vector<uint8_t>&& message, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_1); + if (!digest) { + exceptionCallback(OperationError); + return; + } + + context.ref(); + workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), &context]() mutable { + digest->addBytes(message.data(), message.size()); + auto result = digest->computeHash(); + context.postTask([callback = WTFMove(callback), result = WTFMove(result)](ScriptExecutionContext& context) { + callback(result); + context.deref(); + }); + }); +} + +ExceptionOr<void> CryptoAlgorithmSHA1::digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_1); + if (!digest) { + failureCallback(); + return { }; + } + digest->addBytes(data.first, data.second); + callback(digest->computeHash()); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.h new file mode 100644 index 000000000..f355b49d5 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmSHA1 final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "SHA-1"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::SHA_1; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmSHA1() = default; + CryptoAlgorithmIdentifier identifier() const final; + void digest(Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + ExceptionOr<void> digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.cpp new file mode 100644 index 000000000..634c5a3d9 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmSHA224.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "ExceptionCode.h" +#include "ScriptExecutionContext.h" +#include <pal/crypto/CryptoDigest.h> + +namespace WebCore { + +Ref<CryptoAlgorithm> CryptoAlgorithmSHA224::create() +{ + return adoptRef(*new CryptoAlgorithmSHA224); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmSHA224::identifier() const +{ + return s_identifier; +} + +void CryptoAlgorithmSHA224::digest(Vector<uint8_t>&& message, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_224); + if (!digest) { + exceptionCallback(OperationError); + return; + } + + context.ref(); + workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), &context]() mutable { + digest->addBytes(message.data(), message.size()); + auto result = digest->computeHash(); + context.postTask([callback = WTFMove(callback), result = WTFMove(result)](ScriptExecutionContext& context) { + callback(result); + context.deref(); + }); + }); +} + +ExceptionOr<void> CryptoAlgorithmSHA224::digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_224); + if (!digest) { + failureCallback(); + return { }; + } + digest->addBytes(data.first, data.second); + callback(digest->computeHash()); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.h new file mode 100644 index 000000000..a5b1f6383 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmSHA224 final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "SHA-224"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::SHA_224; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmSHA224() = default; + CryptoAlgorithmIdentifier identifier() const final; + void digest(Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + ExceptionOr<void> digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.cpp new file mode 100644 index 000000000..0ea0506b9 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmSHA256.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "ExceptionCode.h" +#include "ScriptExecutionContext.h" +#include <pal/crypto/CryptoDigest.h> + +namespace WebCore { + +Ref<CryptoAlgorithm> CryptoAlgorithmSHA256::create() +{ + return adoptRef(*new CryptoAlgorithmSHA256); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmSHA256::identifier() const +{ + return s_identifier; +} + +void CryptoAlgorithmSHA256::digest(Vector<uint8_t>&& message, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256); + if (!digest) { + exceptionCallback(OperationError); + return; + } + + context.ref(); + workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), &context]() mutable { + digest->addBytes(message.data(), message.size()); + auto result = digest->computeHash(); + context.postTask([callback = WTFMove(callback), result = WTFMove(result)](ScriptExecutionContext& context) { + callback(result); + context.deref(); + }); + }); +} + +ExceptionOr<void> CryptoAlgorithmSHA256::digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256); + if (!digest) { + failureCallback(); + return { }; + } + digest->addBytes(data.first, data.second); + callback(digest->computeHash()); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.h new file mode 100644 index 000000000..c2acc82f6 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmSHA256 final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "SHA-256"; + static const CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::SHA_256; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmSHA256() = default; + CryptoAlgorithmIdentifier identifier() const final; + void digest(Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + ExceptionOr<void> digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.cpp new file mode 100644 index 000000000..88f313629 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmSHA384.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "ExceptionCode.h" +#include "ScriptExecutionContext.h" +#include <pal/crypto/CryptoDigest.h> + +namespace WebCore { + +Ref<CryptoAlgorithm> CryptoAlgorithmSHA384::create() +{ + return adoptRef(*new CryptoAlgorithmSHA384); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmSHA384::identifier() const +{ + return s_identifier; +} + +void CryptoAlgorithmSHA384::digest(Vector<uint8_t>&& message, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_384); + if (!digest) { + exceptionCallback(OperationError); + return; + } + + context.ref(); + workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), &context]() mutable { + digest->addBytes(message.data(), message.size()); + auto result = digest->computeHash(); + context.postTask([callback = WTFMove(callback), result = WTFMove(result)](ScriptExecutionContext& context) { + callback(result); + context.deref(); + }); + }); +} + +ExceptionOr<void> CryptoAlgorithmSHA384::digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_384); + if (!digest) { + failureCallback(); + return { }; + } + digest->addBytes(data.first, data.second); + callback(digest->computeHash()); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.h new file mode 100644 index 000000000..4f7bab449 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmSHA384 final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "SHA-384"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::SHA_384; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmSHA384() = default; + CryptoAlgorithmIdentifier identifier() const final; + void digest(Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + ExceptionOr<void> digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.cpp new file mode 100644 index 000000000..0103f0953 --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CryptoAlgorithmSHA512.h" + +#if ENABLE(SUBTLE_CRYPTO) + +#include "ExceptionCode.h" +#include "ScriptExecutionContext.h" +#include <pal/crypto/CryptoDigest.h> + +namespace WebCore { + +Ref<CryptoAlgorithm> CryptoAlgorithmSHA512::create() +{ + return adoptRef(*new CryptoAlgorithmSHA512); +} + +CryptoAlgorithmIdentifier CryptoAlgorithmSHA512::identifier() const +{ + return s_identifier; +} + +void CryptoAlgorithmSHA512::digest(Vector<uint8_t>&& message, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_512); + if (!digest) { + exceptionCallback(OperationError); + return; + } + + context.ref(); + workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), &context]() mutable { + digest->addBytes(message.data(), message.size()); + auto result = digest->computeHash(); + context.postTask([callback = WTFMove(callback), result = WTFMove(result)](ScriptExecutionContext& context) { + callback(result); + context.deref(); + }); + }); +} + +ExceptionOr<void> CryptoAlgorithmSHA512::digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) +{ + auto digest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_512); + if (!digest) { + failureCallback(); + return { }; + } + digest->addBytes(data.first, data.second); + callback(digest->computeHash()); + return { }; +} + +} + +#endif // ENABLE(SUBTLE_CRYPTO) diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.h new file mode 100644 index 000000000..b53971e6f --- /dev/null +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CryptoAlgorithm.h" + +#if ENABLE(SUBTLE_CRYPTO) + +namespace WebCore { + +class CryptoAlgorithmSHA512 final : public CryptoAlgorithm { +public: + static constexpr const char* s_name = "SHA-512"; + static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::SHA_512; + static Ref<CryptoAlgorithm> create(); + +private: + CryptoAlgorithmSHA512() = default; + CryptoAlgorithmIdentifier identifier() const final; + void digest(Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + ExceptionOr<void> digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback) final; +}; + +} // namespace WebCore + +#endif // ENABLE(SUBTLE_CRYPTO) |