2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CryptoAlgorithmRSASSA_PKCS1_v1_5.h"
29 #if ENABLE(SUBTLE_CRYPTO)
31 #include "CommonCryptoUtilities.h"
32 #include "CryptoDigestAlgorithm.h"
33 #include "CryptoKeyRSA.h"
34 #include "ScriptExecutionContext.h"
38 static ExceptionOr<Vector<uint8_t>> signRSASSA_PKCS1_v1_5(CryptoAlgorithmIdentifier hash, const PlatformRSAKey key, size_t keyLength, const Vector<uint8_t>& data)
40 CCDigestAlgorithm digestAlgorithm;
41 if (!getCommonCryptoDigestAlgorithm(hash, digestAlgorithm))
42 return Exception { OperationError };
44 auto cryptoDigestAlgorithm = WebCore::cryptoDigestAlgorithm(hash);
45 if (!cryptoDigestAlgorithm)
46 return Exception { OperationError };
47 auto digest = PAL::CryptoDigest::create(*cryptoDigestAlgorithm);
49 return Exception { OperationError };
50 digest->addBytes(data.data(), data.size());
51 auto digestData = digest->computeHash();
53 Vector<uint8_t> signature(keyLength / 8); // Per https://tools.ietf.org/html/rfc3447#section-8.2.1
54 size_t signatureSize = signature.size();
56 CCCryptorStatus status = CCRSACryptorSign(key, ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
58 return Exception { OperationError };
60 return WTFMove(signature);
63 static ExceptionOr<bool> verifyRSASSA_PKCS1_v1_5(CryptoAlgorithmIdentifier hash, const PlatformRSAKey key, const Vector<uint8_t>& signature, const Vector<uint8_t>& data)
65 CCDigestAlgorithm digestAlgorithm;
66 if (!getCommonCryptoDigestAlgorithm(hash, digestAlgorithm))
67 return Exception { OperationError };
69 auto cryptoDigestAlgorithm = WebCore::cryptoDigestAlgorithm(hash);
70 if (!cryptoDigestAlgorithm)
71 return Exception { OperationError };
72 auto digest = PAL::CryptoDigest::create(*cryptoDigestAlgorithm);
74 return Exception { OperationError };
75 digest->addBytes(data.data(), data.size());
76 auto digestData = digest->computeHash();
78 auto status = CCRSACryptorVerify(key, ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), signature.size());
81 if (status == kCCNotVerified || status == kCCDecodeError) // <rdar://problem/15464982> CCRSACryptorVerify returns kCCDecodeError instead of kCCNotVerified sometimes
84 return Exception { OperationError };
87 void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
90 workQueue.dispatch([key = WTFMove(key), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
91 auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
92 auto result = signRSASSA_PKCS1_v1_5(rsaKey.hashAlgorithmIdentifier(), rsaKey.platformKey(), rsaKey.keySizeInBits(), data);
93 if (result.hasException()) {
94 // We should only dereference callbacks after being back to the Document/Worker threads.
95 context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
96 exceptionCallback(ec);
101 // We should only dereference callbacks after being back to the Document/Worker threads.
102 context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
109 void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(Ref<CryptoKey>&& key, Vector<uint8_t>&& signature, Vector<uint8_t>&& data, BoolCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
112 workQueue.dispatch([key = WTFMove(key), signature = WTFMove(signature), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
113 auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
114 auto result = verifyRSASSA_PKCS1_v1_5(rsaKey.hashAlgorithmIdentifier(), rsaKey.platformKey(), signature, data);
115 if (result.hasException()) {
116 // We should only dereference callbacks after being back to the Document/Worker threads.
117 context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
118 exceptionCallback(ec);
123 // We should only dereference callbacks after being back to the Document/Worker threads.
124 context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
131 } // namespace WebCore
133 #endif // ENABLE(SUBTLE_CRYPTO)