void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(const CryptoAlgorithmRsaSsaParams& parameters, const CryptoKeyRSA& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback&& callback, VoidCallback&& failureCallback, ExceptionCode& ec) { CCDigestAlgorithm digestAlgorithm; if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm)) { ec = NOT_SUPPORTED_ERR; return; } std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(parameters.hash); if (!digest) { ec = NOT_SUPPORTED_ERR; return; } digest->addBytes(data.first, data.second); Vector<uint8_t> digestData = digest->computeHash(); CCCryptorStatus status = CCRSACryptorVerify(key.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.first, signature.second); if (!status) callback(true); else if (status == kCCNotVerified || status == kCCDecodeError) // <rdar://problem/15464982> CCRSACryptorVerify returns kCCDecodeError instead of kCCNotVerified sometimes callback(false); else failureCallback(); }
void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaParams& parameters, const CryptoKeyRSA& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode& ec) { CCDigestAlgorithm digestAlgorithm; if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm)) { ec = NOT_SUPPORTED_ERR; return; } std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(parameters.hash); if (!digest) { ec = NOT_SUPPORTED_ERR; return; } digest->addBytes(data.first, data.second); Vector<uint8_t> digestData = digest->computeHash(); Vector<uint8_t> signature(512); size_t signatureSize = signature.size(); CCCryptorStatus status = CCRSACryptorSign(key.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize); if (status) { failureCallback(); return; } signature.resize(signatureSize); callback(signature); }
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(result.get(), nullptr); return { }; }
ExceptionOr<void> CryptoAlgorithmSHA1::digest(const CryptoAlgorithmParametersDeprecated&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) { auto digest = CryptoDigest::create(CryptoDigest::Algorithm::SHA_1); if (!digest) { failureCallback(); return { }; } digest->addBytes(data.first, data.second); callback(digest->computeHash()); return { }; }
ExceptionOr<void> CryptoAlgorithmRSA_OAEP::platformDecrypt(const CryptoAlgorithmRsaOaepParamsDeprecated& parameters, const CryptoKeyRSA& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback) { ASSERT(parameters.hasLabel || parameters.label.isEmpty()); auto result = decryptRSA_OAEP(parameters.hash, parameters.label, key.platformKey(), key.keySizeInBits(), data.first, data.second); if (result.hasException()) { failureCallback(); return { }; } callback(result.releaseReturnValue()); return { }; }
void CryptoAlgorithmSHA512::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&) { std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoDigest::Algorithm::SHA_512); if (!digest) { failureCallback(); return; } digest->addBytes(data.first, data.second); callback(digest->computeHash()); }
void CryptoAlgorithmAES_KW::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&) { const CryptoAlgorithmAesKeyGenParams& aesParameters = downcast<CryptoAlgorithmAesKeyGenParams>(parameters); RefPtr<CryptoKeyAES> result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_KW, aesParameters.length, extractable, usages); if (!result) { failureCallback(); return; } callback(result.get(), nullptr); }
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 { }; }
void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&) { const CryptoAlgorithmHmacKeyParams& hmacParameters = downcast<CryptoAlgorithmHmacKeyParams>(parameters); RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::generate(hmacParameters.hasLength ? hmacParameters.length : 0, hmacParameters.hash, extractable, usages); if (!result) { failureCallback(); return; } callback(result.get(), nullptr); }
void CryptoAlgorithmRSA_OAEP::platformDecrypt(const CryptoAlgorithmRsaOaepParams& parameters, const CryptoKeyRSA& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode& ec) { CCDigestAlgorithm digestAlgorithm; if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm)) { ec = NOT_SUPPORTED_ERR; return; } Vector<uint8_t> plainText(1024); size_t plainTextLength = plainText.size(); CCCryptorStatus status = CCRSACryptorDecrypt(key.platformKey(), ccOAEPPadding, data.first, data.second, plainText.data(), &plainTextLength, parameters.label.data(), parameters.label.size(), digestAlgorithm); if (status) { failureCallback(); return; } plainText.resize(plainTextLength); callback(plainText); }
// FIXME: We should use WorkQueue here instead of dispatch_async once WebKitSubtleCrypto is deprecated. // https://bugs.webkit.org/show_bug.cgi?id=164943 void CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier algorithm, CryptoAlgorithmIdentifier hash, bool hasHash, unsigned modulusLength, const Vector<uint8_t>& publicExponent, bool extractable, CryptoKeyUsageBitmap usage, KeyPairCallback&& callback, VoidCallback&& failureCallback, ScriptExecutionContext* context) { uint32_t e; if (!bigIntegerToUInt32(publicExponent, e)) { // Adding support is tracked as <rdar://problem/15444034>. WTFLogAlways("Public exponent is too big, not supported"); failureCallback(); return; } // We only use the callback functions when back on the main/worker thread, but captured variables are copied on a secondary thread too. KeyPairCallback* localCallback = new KeyPairCallback(WTFMove(callback)); VoidCallback* localFailureCallback = new VoidCallback(WTFMove(failureCallback)); context->ref(); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ ASSERT(context); CCRSACryptorRef ccPublicKey; CCRSACryptorRef ccPrivateKey; CCCryptorStatus status = CCRSACryptorGeneratePair(modulusLength, e, &ccPublicKey, &ccPrivateKey); if (status) { WTFLogAlways("Could not generate a key pair, status %d", status); context->postTask([localCallback, localFailureCallback](ScriptExecutionContext& context) { (*localFailureCallback)(); delete localCallback; delete localFailureCallback; context.deref(); }); return; } context->postTask([algorithm, hash, hasHash, extractable, usage, localCallback, localFailureCallback, ccPublicKey, ccPrivateKey](ScriptExecutionContext& context) { auto publicKey = CryptoKeyRSA::create(algorithm, hash, hasHash, CryptoKeyType::Public, ccPublicKey, true, usage); auto privateKey = CryptoKeyRSA::create(algorithm, hash, hasHash, CryptoKeyType::Private, ccPrivateKey, extractable, usage); (*localCallback)(CryptoKeyPair { WTFMove(publicKey), WTFMove(privateKey) }); delete localCallback; delete localFailureCallback; context.deref(); }); });