2 * Copyright (C) 2014 Igalia S.L. 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 "CryptoAlgorithmRSA_OAEP.h"
29 #if ENABLE(SUBTLE_CRYPTO)
31 #include "CryptoAlgorithmRsaOaepParams.h"
32 #include "CryptoKeyRSA.h"
33 #include "ExceptionCode.h"
34 #include "GCryptUtilities.h"
35 #include "NotImplemented.h"
36 #include "ScriptExecutionContext.h"
40 static std::optional<Vector<uint8_t>> gcryptEncrypt(CryptoAlgorithmIdentifier hashAlgorithmIdentifier, gcry_sexp_t keySexp, const Vector<uint8_t>& labelVector, const Vector<uint8_t>& plainText)
42 // Embed the plain-text data in a data s-expression using OAEP padding.
43 // Empty label data is properly handled by gcry_sexp_build().
44 PAL::GCrypt::Handle<gcry_sexp_t> dataSexp;
46 auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier);
50 gcry_error_t error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags oaep)(hash-algo %s)(label %b)(value %b))",
51 *shaAlgorithm, labelVector.size(), labelVector.data(), plainText.size(), plainText.data());
52 if (error != GPG_ERR_NO_ERROR) {
53 PAL::GCrypt::logError(error);
58 // Encrypt data with the provided key. The returned s-expression is of this form:
63 PAL::GCrypt::Handle<gcry_sexp_t> cipherSexp;
64 gcry_error_t error = gcry_pk_encrypt(&cipherSexp, dataSexp, keySexp);
65 if (error != GPG_ERR_NO_ERROR) {
66 PAL::GCrypt::logError(error);
70 // Return MPI data of the embedded a integer.
71 PAL::GCrypt::Handle<gcry_sexp_t> aSexp(gcry_sexp_find_token(cipherSexp, "a", 0));
75 return mpiData(aSexp);
78 static std::optional<Vector<uint8_t>> gcryptDecrypt(CryptoAlgorithmIdentifier hashAlgorithmIdentifier, gcry_sexp_t keySexp, const Vector<uint8_t>& labelVector, const Vector<uint8_t>& cipherText)
80 // Embed the cipher-text data in an enc-val s-expression using OAEP padding.
81 // Empty label data is properly handled by gcry_sexp_build().
82 PAL::GCrypt::Handle<gcry_sexp_t> encValSexp;
84 auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier);
88 gcry_error_t error = gcry_sexp_build(&encValSexp, nullptr, "(enc-val(flags oaep)(hash-algo %s)(label %b)(rsa(a %b)))",
89 *shaAlgorithm, labelVector.size(), labelVector.data(), cipherText.size(), cipherText.data());
90 if (error != GPG_ERR_NO_ERROR) {
91 PAL::GCrypt::logError(error);
96 // Decrypt data with the provided key. The returned s-expression is of this form:
100 PAL::GCrypt::Handle<gcry_sexp_t> plainSexp;
101 gcry_error_t error = gcry_pk_decrypt(&plainSexp, encValSexp, keySexp);
102 if (error != GPG_ERR_NO_ERROR) {
103 PAL::GCrypt::logError(error);
107 // Return MPI data of the embedded value integer.
108 PAL::GCrypt::Handle<gcry_sexp_t> valueSexp(gcry_sexp_find_token(plainSexp, "value", 0));
112 return mpiData(valueSexp);
115 void CryptoAlgorithmRSA_OAEP::platformEncrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
119 [parameters = WTFMove(parameters), key = WTFMove(key), plainText = WTFMove(plainText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
120 auto& rsaParameters = downcast<CryptoAlgorithmRsaOaepParams>(*parameters);
121 auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
123 auto output = gcryptEncrypt(rsaKey.hashAlgorithmIdentifier(), rsaKey.platformKey(), rsaParameters.labelVector(), plainText);
125 // We should only dereference callbacks after being back to the Document/Worker threads.
127 [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
128 exceptionCallback(OperationError);
134 // We should only dereference callbacks after being back to the Document/Worker threads.
136 [output = WTFMove(*output), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
143 void CryptoAlgorithmRSA_OAEP::platformDecrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
147 [parameters = WTFMove(parameters), key = WTFMove(key), cipherText = WTFMove(cipherText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
148 auto& rsaParameters = downcast<CryptoAlgorithmRsaOaepParams>(*parameters);
149 auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
151 auto output = gcryptDecrypt(rsaKey.hashAlgorithmIdentifier(), rsaKey.platformKey(), rsaParameters.labelVector(), cipherText);
153 // We should only dereference callbacks after being back to the Document/Worker threads.
155 [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
156 exceptionCallback(OperationError);
162 // We should only dereference callbacks after being back to the Document/Worker threads.
164 [output = WTFMove(*output), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
171 ExceptionOr<void> CryptoAlgorithmRSA_OAEP::platformEncrypt(const CryptoAlgorithmRsaOaepParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&&)
174 return Exception { NOT_SUPPORTED_ERR };
177 ExceptionOr<void> CryptoAlgorithmRSA_OAEP::platformDecrypt(const CryptoAlgorithmRsaOaepParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&&)
180 return Exception { NOT_SUPPORTED_ERR };
183 } // namespace WebCore
185 #endif // ENABLE(SUBTLE_CRYPTO)