1/* 2 * Copyright (C) 2013 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 24 */ 25 26#include "config.h" 27#include "CryptoAlgorithmRSASSA_PKCS1_v1_5.h" 28 29#if ENABLE(SUBTLE_CRYPTO) 30 31#include "CryptoAlgorithmRsaKeyGenParams.h" 32#include "CryptoAlgorithmRsaKeyParamsWithHash.h" 33#include "CryptoAlgorithmRsaSsaParams.h" 34#include "CryptoKeyDataRSAComponents.h" 35#include "CryptoKeyRSA.h" 36#include "ExceptionCode.h" 37 38namespace WebCore { 39 40const char* const CryptoAlgorithmRSASSA_PKCS1_v1_5::s_name = "RSASSA-PKCS1-v1_5"; 41 42CryptoAlgorithmRSASSA_PKCS1_v1_5::CryptoAlgorithmRSASSA_PKCS1_v1_5() 43{ 44} 45 46CryptoAlgorithmRSASSA_PKCS1_v1_5::~CryptoAlgorithmRSASSA_PKCS1_v1_5() 47{ 48} 49 50std::unique_ptr<CryptoAlgorithm> CryptoAlgorithmRSASSA_PKCS1_v1_5::create() 51{ 52 return std::unique_ptr<CryptoAlgorithm>(new CryptoAlgorithmRSASSA_PKCS1_v1_5); 53} 54 55CryptoAlgorithmIdentifier CryptoAlgorithmRSASSA_PKCS1_v1_5::identifier() const 56{ 57 return s_identifier; 58} 59 60bool CryptoAlgorithmRSASSA_PKCS1_v1_5::keyAlgorithmMatches(const CryptoAlgorithmRsaSsaParams& algorithmParameters, const CryptoKey& key) const 61{ 62 if (key.algorithmIdentifier() != s_identifier) 63 return false; 64 ASSERT(isCryptoKeyRSA(key)); 65 66 CryptoAlgorithmIdentifier keyHash; 67 if (toCryptoKeyRSA(key).isRestrictedToHash(keyHash) && keyHash != algorithmParameters.hash) 68 return false; 69 70 return true; 71} 72 73void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec) 74{ 75 const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters); 76 77 if (!keyAlgorithmMatches(rsaSSAParameters, key)) { 78 ec = NOT_SUPPORTED_ERR; 79 return; 80 } 81 82 platformSign(rsaSSAParameters, toCryptoKeyRSA(key), data, WTF::move(callback), WTF::move(failureCallback), ec); 83} 84 85void CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback callback, VoidCallback failureCallback, ExceptionCode& ec) 86{ 87 const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters); 88 89 if (!keyAlgorithmMatches(rsaSSAParameters, key)) { 90 ec = NOT_SUPPORTED_ERR; 91 return; 92 } 93 94 platformVerify(rsaSSAParameters, toCryptoKeyRSA(key), signature, data, WTF::move(callback), WTF::move(failureCallback), ec); 95} 96 97void CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&) 98{ 99 const CryptoAlgorithmRsaKeyGenParams& rsaParameters = toCryptoAlgorithmRsaKeyGenParams(parameters); 100 101 auto keyPairCallback = [callback](CryptoKeyPair& pair) { 102 callback(nullptr, &pair); 103 }; 104 105 CryptoKeyRSA::generatePair(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaParameters.modulusLength, rsaParameters.publicExponent, extractable, usages, WTF::move(keyPairCallback), WTF::move(failureCallback)); 106} 107 108void CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback failureCallback, ExceptionCode&) 109{ 110 const CryptoAlgorithmRsaKeyParamsWithHash& rsaKeyParameters = toCryptoAlgorithmRsaKeyParamsWithHash(parameters); 111 const CryptoKeyDataRSAComponents& rsaComponents = toCryptoKeyDataRSAComponents(keyData); 112 113 RefPtr<CryptoKeyRSA> result = CryptoKeyRSA::create(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5, rsaComponents, extractable, usage); 114 if (!result) { 115 failureCallback(); 116 return; 117 } 118 119 if (rsaKeyParameters.hasHash) 120 result->restrictToHash(rsaKeyParameters.hash); 121 122 callback(*result); 123} 124 125} 126 127#endif // ENABLE(SUBTLE_CRYPTO) 128