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 "CryptoAlgorithmRsaSsaParamsDeprecated.h"
33 #include "CryptoDigest.h"
34 #include "CryptoKeyRSA.h"
35 #include "ExceptionCode.h"
39 inline bool getCryptoDigestAlgorithm(CryptoAlgorithmIdentifier hashFunction, CryptoDigest::Algorithm& algorithm)
41 switch (hashFunction) {
42 case CryptoAlgorithmIdentifier::SHA_1:
43 algorithm = CryptoDigest::Algorithm::SHA_1;
45 case CryptoAlgorithmIdentifier::SHA_224:
46 algorithm = CryptoDigest::Algorithm::SHA_224;
48 case CryptoAlgorithmIdentifier::SHA_256:
49 algorithm = CryptoDigest::Algorithm::SHA_256;
51 case CryptoAlgorithmIdentifier::SHA_384:
52 algorithm = CryptoDigest::Algorithm::SHA_384;
54 case CryptoAlgorithmIdentifier::SHA_512:
55 algorithm = CryptoDigest::Algorithm::SHA_512;
62 void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaParamsDeprecated& parameters, const CryptoKeyRSA& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode& ec)
64 CCDigestAlgorithm digestAlgorithm;
65 if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm)) {
66 ec = NOT_SUPPORTED_ERR;
70 CryptoDigest::Algorithm cryptoDigestAlgorithm;
71 if (!getCryptoDigestAlgorithm(parameters.hash, cryptoDigestAlgorithm)) {
72 ec = NOT_SUPPORTED_ERR;
76 std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(cryptoDigestAlgorithm);
78 ec = NOT_SUPPORTED_ERR;
82 digest->addBytes(data.first, data.second);
84 Vector<uint8_t> digestData = digest->computeHash();
86 Vector<uint8_t> signature(512);
87 size_t signatureSize = signature.size();
89 CCCryptorStatus status = CCRSACryptorSign(key.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
95 signature.resize(signatureSize);
99 void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(const CryptoAlgorithmRsaSsaParamsDeprecated& parameters, const CryptoKeyRSA& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback&& callback, VoidCallback&& failureCallback, ExceptionCode& ec)
101 CCDigestAlgorithm digestAlgorithm;
102 if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm)) {
103 ec = NOT_SUPPORTED_ERR;
107 CryptoDigest::Algorithm cryptoDigestAlgorithm;
108 if (!getCryptoDigestAlgorithm(parameters.hash, cryptoDigestAlgorithm)) {
109 ec = NOT_SUPPORTED_ERR;
113 std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(cryptoDigestAlgorithm);
115 ec = NOT_SUPPORTED_ERR;
119 digest->addBytes(data.first, data.second);
121 Vector<uint8_t> digestData = digest->computeHash();
123 CCCryptorStatus status = CCRSACryptorVerify(key.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.first, signature.second);
126 else if (status == kCCNotVerified || status == kCCDecodeError) // <rdar://problem/15464982> CCRSACryptorVerify returns kCCDecodeError instead of kCCNotVerified sometimes
132 } // namespace WebCore
134 #endif // ENABLE(SUBTLE_CRYPTO)