Changeset 159717 in webkit


Ignore:
Timestamp:
Nov 22, 2013 3:43:54 PM (10 years ago)
Author:
ap@apple.com
Message:

WebCrypto algorithms should check that key algorithm matches
https://bugs.webkit.org/show_bug.cgi?id=123628

Reviewed by Anders Carlsson.

No change in behavior yet, because we have one algorithm per key class.
Will be tested once more algorithms are added.

  • WebCore.xcodeproj/project.pbxproj: Updated for file renames.
  • bindings/js/JSCryptoAlgorithmDictionary.cpp:

(WebCore::createRsaKeyParamsWithHash):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForImportKey):

  • bindings/js/JSCryptoKeySerializationJWK.cpp:

(WebCore::createRSAKeyParametersWithHash):
(WebCore::JSCryptoKeySerializationJWK::reconcileAlgorithm):

  • crypto/CryptoAlgorithmParameters.h:

(WebCore::CryptoAlgorithmParameters::ENUM_CLASS):

  • crypto/parameters/CryptoAlgorithmRsaKeyParamsWithHash.h: Copied from Source/WebCore/crypto/parameters/CryptoAlgorithmRsaSsaKeyParams.h.
  • crypto/parameters/CryptoAlgorithmRsaSsaKeyParams.h: Removed.

Renamed RsaSsaKeyParams to RsaKeyParamsWithHash, because other algorithms (like RSA-OAEP)
are in the same boat. Depending on where the spec goes, we might need to introduce
algorithm specific RSA parameter classes later, but let's reduce copy/pasted code at
least for now.

  • crypto/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp: Moved to the correct directory.
  • crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp: Copied from Source/WebCore/crypto/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp.

(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign): Factored out Mac specific
code, leaving type casting to cross-platform files.
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify): Ditto.

  • crypto/CryptoAlgorithmRegistry.h:

(WebCore::CryptoAlgorithmRegistry::registerAlgorithm):

  • crypto/mac/CryptoAlgorithmRegistryMac.cpp:

(WebCore::CryptoAlgorithmRegistry::platformRegisterAlgorithms):
Reduce copy/pasting in registration code.

  • crypto/algorithms/CryptoAlgorithmAES_CBC.cpp:

(WebCore::CryptoAlgorithmAES_CBC::keyAlgorithmMatches): Check key type and algorithm.
(WebCore::CryptoAlgorithmAES_CBC::encrypt): Cross platform type casting code.
Maybe we'll find a way to autogenerate or eliminate it one day.
(WebCore::CryptoAlgorithmAES_CBC::decrypt): Ditto.

  • crypto/algorithms/CryptoAlgorithmAES_CBC.h:
  • crypto/algorithms/CryptoAlgorithmHMAC.cpp:

(WebCore::CryptoAlgorithmHMAC::keyAlgorithmMatches):
(WebCore::CryptoAlgorithmHMAC::sign):
(WebCore::CryptoAlgorithmHMAC::verify):

  • crypto/algorithms/CryptoAlgorithmHMAC.h:
  • crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp:

(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::keyAlgorithmMatches):
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::sign):
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::verify):
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey):

  • crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h:
  • crypto/mac/CryptoAlgorithmAES_CBCMac.cpp:

(WebCore::CryptoAlgorithmAES_CBC::platformEncrypt):
(WebCore::CryptoAlgorithmAES_CBC::platformDecrypt):

  • crypto/mac/CryptoAlgorithmHMACMac.cpp:

(WebCore::CryptoAlgorithmHMAC::platformSign):
(WebCore::CryptoAlgorithmHMAC::platformVerify):
Same changes for all algorithms that have keys.

Location:
trunk/Source/WebCore
Files:
15 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r159714 r159717  
     12013-11-22  Alexey Proskuryakov  <ap@apple.com>
     2
     3        WebCrypto algorithms should check that key algorithm matches
     4        https://bugs.webkit.org/show_bug.cgi?id=123628
     5
     6        Reviewed by Anders Carlsson.
     7
     8        No change in behavior yet, because we have one algorithm per key class.
     9        Will be tested once more algorithms are added.
     10
     11        * WebCore.xcodeproj/project.pbxproj: Updated for file renames.
     12
     13        * bindings/js/JSCryptoAlgorithmDictionary.cpp:
     14        (WebCore::createRsaKeyParamsWithHash):
     15        (WebCore::JSCryptoAlgorithmDictionary::createParametersForImportKey):
     16        * bindings/js/JSCryptoKeySerializationJWK.cpp:
     17        (WebCore::createRSAKeyParametersWithHash):
     18        (WebCore::JSCryptoKeySerializationJWK::reconcileAlgorithm):
     19        * crypto/CryptoAlgorithmParameters.h:
     20        (WebCore::CryptoAlgorithmParameters::ENUM_CLASS):
     21        * crypto/parameters/CryptoAlgorithmRsaKeyParamsWithHash.h: Copied from Source/WebCore/crypto/parameters/CryptoAlgorithmRsaSsaKeyParams.h.
     22        * crypto/parameters/CryptoAlgorithmRsaSsaKeyParams.h: Removed.
     23        Renamed RsaSsaKeyParams to RsaKeyParamsWithHash, because other algorithms (like RSA-OAEP)
     24        are in the same boat. Depending on where the spec goes, we might need to introduce
     25        algorithm specific RSA parameter classes later, but let's reduce copy/pasted code at
     26        least for now.
     27
     28        * crypto/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp: Moved to the correct directory.
     29        * crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp: Copied from Source/WebCore/crypto/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp.
     30        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign): Factored out Mac specific
     31        code, leaving type casting to cross-platform files.
     32        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify): Ditto.
     33
     34        * crypto/CryptoAlgorithmRegistry.h:
     35        (WebCore::CryptoAlgorithmRegistry::registerAlgorithm):
     36        * crypto/mac/CryptoAlgorithmRegistryMac.cpp:
     37        (WebCore::CryptoAlgorithmRegistry::platformRegisterAlgorithms):
     38        Reduce copy/pasting in registration code.
     39
     40        * crypto/algorithms/CryptoAlgorithmAES_CBC.cpp:
     41        (WebCore::CryptoAlgorithmAES_CBC::keyAlgorithmMatches): Check key type and algorithm.
     42        (WebCore::CryptoAlgorithmAES_CBC::encrypt): Cross platform type casting code.
     43        Maybe we'll find a way to autogenerate or eliminate it one day.
     44        (WebCore::CryptoAlgorithmAES_CBC::decrypt): Ditto.
     45
     46        * crypto/algorithms/CryptoAlgorithmAES_CBC.h:
     47        * crypto/algorithms/CryptoAlgorithmHMAC.cpp:
     48        (WebCore::CryptoAlgorithmHMAC::keyAlgorithmMatches):
     49        (WebCore::CryptoAlgorithmHMAC::sign):
     50        (WebCore::CryptoAlgorithmHMAC::verify):
     51        * crypto/algorithms/CryptoAlgorithmHMAC.h:
     52        * crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp:
     53        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::keyAlgorithmMatches):
     54        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::sign):
     55        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::verify):
     56        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey):
     57        * crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h:
     58        * crypto/mac/CryptoAlgorithmAES_CBCMac.cpp:
     59        (WebCore::CryptoAlgorithmAES_CBC::platformEncrypt):
     60        (WebCore::CryptoAlgorithmAES_CBC::platformDecrypt):
     61        * crypto/mac/CryptoAlgorithmHMACMac.cpp:
     62        (WebCore::CryptoAlgorithmHMAC::platformSign):
     63        (WebCore::CryptoAlgorithmHMAC::platformVerify):
     64        Same changes for all algorithms that have keys.
     65
    1662013-11-22  Brendan Long  <b.long@cablelabs.com>
    267
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r159583 r159717  
    56825682                E1BE512E0CF6C512002EA959 /* XSLTUnicodeSort.h in Headers */ = {isa = PBXBuildFile; fileRef = E1BE512C0CF6C512002EA959 /* XSLTUnicodeSort.h */; };
    56835683                E1C266D818317AB4003F8B33 /* CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1C266D618317AB4003F8B33 /* CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp */; };
    5684                 E1C266DB18319935003F8B33 /* CryptoAlgorithmRsaSsaKeyParams.h in Headers */ = {isa = PBXBuildFile; fileRef = E1C266DA18319935003F8B33 /* CryptoAlgorithmRsaSsaKeyParams.h */; };
     5684                E1C266DB18319935003F8B33 /* CryptoAlgorithmRsaKeyParamsWithHash.h in Headers */ = {isa = PBXBuildFile; fileRef = E1C266DA18319935003F8B33 /* CryptoAlgorithmRsaKeyParamsWithHash.h */; };
    56855685                E1C266DE18319F31003F8B33 /* CryptoKeyDataRSAComponents.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1C266DC18319F31003F8B33 /* CryptoKeyDataRSAComponents.cpp */; };
    56865686                E1C266DF18319F31003F8B33 /* CryptoKeyDataRSAComponents.h in Headers */ = {isa = PBXBuildFile; fileRef = E1C266DD18319F31003F8B33 /* CryptoKeyDataRSAComponents.h */; };
     
    1279012790                E1BE512B0CF6C512002EA959 /* XSLTUnicodeSort.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XSLTUnicodeSort.cpp; sourceTree = "<group>"; };
    1279112791                E1BE512C0CF6C512002EA959 /* XSLTUnicodeSort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XSLTUnicodeSort.h; sourceTree = "<group>"; };
    12792                 E1C266D618317AB4003F8B33 /* CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp; sourceTree = "<group>"; };
    12793                 E1C266DA18319935003F8B33 /* CryptoAlgorithmRsaSsaKeyParams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CryptoAlgorithmRsaSsaKeyParams.h; path = parameters/CryptoAlgorithmRsaSsaKeyParams.h; sourceTree = "<group>"; };
     12792                E1C266D618317AB4003F8B33 /* CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp; path = mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp; sourceTree = "<group>"; };
     12793                E1C266DA18319935003F8B33 /* CryptoAlgorithmRsaKeyParamsWithHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CryptoAlgorithmRsaKeyParamsWithHash.h; path = parameters/CryptoAlgorithmRsaKeyParamsWithHash.h; sourceTree = "<group>"; };
    1279412794                E1C266DC18319F31003F8B33 /* CryptoKeyDataRSAComponents.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CryptoKeyDataRSAComponents.cpp; path = keys/CryptoKeyDataRSAComponents.cpp; sourceTree = "<group>"; };
    1279512795                E1C266DD18319F31003F8B33 /* CryptoKeyDataRSAComponents.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CryptoKeyDataRSAComponents.h; path = keys/CryptoKeyDataRSAComponents.h; sourceTree = "<group>"; };
     
    2043520435                                E1C6571E1816E50300256CDD /* CryptoAlgorithmHmacParams.h */,
    2043620436                                E1BD331D182D8F4200C05D9F /* CryptoAlgorithmRsaKeyGenParams.h */,
    20437                                 E1C266DA18319935003F8B33 /* CryptoAlgorithmRsaSsaKeyParams.h */,
     20437                                E1C266DA18319935003F8B33 /* CryptoAlgorithmRsaKeyParamsWithHash.h */,
     20438                                E1FE136C183FE21D00892F13 /* CryptoAlgorithmRsaOaepParams.h */,
    2043820439                                E1BD331B182D8EE900C05D9F /* CryptoAlgorithmRsaSsaParams.h */,
    2043920440                        );
     
    2375323754                                B2FA3DDF0AB75A6F000E5AC4 /* JSSVGPathSegMovetoRel.h in Headers */,
    2375423755                                0705852117FDC140005F2BCB /* MediaTrackConstraint.h in Headers */,
    23755                                 E1C266DB18319935003F8B33 /* CryptoAlgorithmRsaSsaKeyParams.h in Headers */,
     23756                                E1C266DB18319935003F8B33 /* CryptoAlgorithmRsaKeyParamsWithHash.h in Headers */,
    2375623757                                CD8B5A46180DFF4E008B8E65 /* VideoTrackMediaSource.h in Headers */,
    2375723758                                B2FA3DE10AB75A6F000E5AC4 /* JSSVGPatternElement.h in Headers */,
  • trunk/Source/WebCore/bindings/js/JSCryptoAlgorithmDictionary.cpp

    r159637 r159717  
    3535#include "CryptoAlgorithmRegistry.h"
    3636#include "CryptoAlgorithmRsaKeyGenParams.h"
    37 #include "CryptoAlgorithmRsaSsaKeyParams.h"
     37#include "CryptoAlgorithmRsaKeyParamsWithHash.h"
    3838#include "CryptoAlgorithmRsaSsaParams.h"
    3939#include "ExceptionCode.h"
     
    239239}
    240240
    241 static std::unique_ptr<CryptoAlgorithmParameters> createRsaSsaKeyParams(ExecState*, JSValue)
    242 {
    243     // WebCrypto RSASSA-PKCS1-v1_5 algorithm currently does not take any parameters to importKey.
    244     return std::make_unique<CryptoAlgorithmRsaSsaKeyParams>();
     241static std::unique_ptr<CryptoAlgorithmParameters> createRsaKeyParamsWithHash(ExecState*, JSValue)
     242{
     243    // WebCrypto RSA algorithms currently do not take any parameters to importKey.
     244    return std::make_unique<CryptoAlgorithmRsaKeyParamsWithHash>();
    245245}
    246246
     
    527527        return std::make_unique<CryptoAlgorithmParameters>();
    528528    case CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5:
    529         return createRsaSsaKeyParams(exec, value);
     529        return createRsaKeyParamsWithHash(exec, value);
    530530    case CryptoAlgorithmIdentifier::RSA_PSS:
    531531    case CryptoAlgorithmIdentifier::RSA_OAEP:
  • trunk/Source/WebCore/bindings/js/JSCryptoKeySerializationJWK.cpp

    r159403 r159717  
    3232#include "CryptoAlgorithmHmacParams.h"
    3333#include "CryptoAlgorithmRegistry.h"
    34 #include "CryptoAlgorithmRsaSsaKeyParams.h"
     34#include "CryptoAlgorithmRsaKeyParamsWithHash.h"
    3535#include "CryptoKey.h"
    3636#include "CryptoKeyAES.h"
     
    156156}
    157157
    158 static std::unique_ptr<CryptoAlgorithmParameters> createRSASSAKeyParameters(CryptoAlgorithmIdentifier hashFunction)
    159 {
    160     std::unique_ptr<CryptoAlgorithmRsaSsaKeyParams> rsaSSAParameters = std::make_unique<CryptoAlgorithmRsaSsaKeyParams>();
    161     rsaSSAParameters->hasHash = true;
    162     rsaSSAParameters->hash = hashFunction;
    163     return std::move(rsaSSAParameters);
    164 }
    165 
     158static std::unique_ptr<CryptoAlgorithmParameters> createRSAKeyParametersWithHash(CryptoAlgorithmIdentifier hashFunction)
     159{
     160    std::unique_ptr<CryptoAlgorithmRsaKeyParamsWithHash> rsaKeyParameters = std::make_unique<CryptoAlgorithmRsaKeyParamsWithHash>();
     161    rsaKeyParameters->hasHash = true;
     162    rsaKeyParameters->hash = hashFunction;
     163    return std::move(rsaKeyParameters);
     164}
    166165
    167166bool JSCryptoKeySerializationJWK::reconcileAlgorithm(std::unique_ptr<CryptoAlgorithm>& suggestedAlgorithm, std::unique_ptr<CryptoAlgorithmParameters>& suggestedParameters) const
     
    185184    } else if (m_jwkAlgorithmName == "RS256") {
    186185        algorithm = CryptoAlgorithmRegistry::shared().create(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5);
    187         parameters = createRSASSAKeyParameters(CryptoAlgorithmIdentifier::SHA_256);
     186        parameters = createRSAKeyParametersWithHash(CryptoAlgorithmIdentifier::SHA_256);
    188187    } else if (m_jwkAlgorithmName == "RS384") {
    189188        algorithm = CryptoAlgorithmRegistry::shared().create(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5);
    190         parameters = createRSASSAKeyParameters(CryptoAlgorithmIdentifier::SHA_384);
     189        parameters = createRSAKeyParametersWithHash(CryptoAlgorithmIdentifier::SHA_384);
    191190    } else if (m_jwkAlgorithmName == "RS512") {
    192191        algorithm = CryptoAlgorithmRegistry::shared().create(CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5);
    193         parameters = createRSASSAKeyParameters(CryptoAlgorithmIdentifier::SHA_512);
     192        parameters = createRSAKeyParametersWithHash(CryptoAlgorithmIdentifier::SHA_512);
    194193    } else if (m_jwkAlgorithmName == "A128CBC") {
    195194        algorithm = CryptoAlgorithmRegistry::shared().create(CryptoAlgorithmIdentifier::AES_CBC);
     
    221220        return toCryptoAlgorithmHmacParams(*parameters).hash == toCryptoAlgorithmHmacParams(*suggestedParameters).hash;
    222221    if (algorithm->identifier() == CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5) {
    223         CryptoAlgorithmRsaSsaKeyParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaKeyParams(*parameters);
    224         CryptoAlgorithmRsaSsaKeyParams& suggestedRSASSAParameters = toCryptoAlgorithmRsaSsaKeyParams(*suggestedParameters);
    225         ASSERT(rsaSSAParameters.hasHash);
    226         if (suggestedRSASSAParameters.hasHash)
    227             return suggestedRSASSAParameters.hash == rsaSSAParameters.hash;
    228         suggestedRSASSAParameters.hasHash = true;
    229         suggestedRSASSAParameters.hash = rsaSSAParameters.hash;
     222        CryptoAlgorithmRsaKeyParamsWithHash& rsaKeyParameters = toCryptoAlgorithmRsaKeyParamsWithHash(*parameters);
     223        CryptoAlgorithmRsaKeyParamsWithHash& suggestedRSAKeyParameters = toCryptoAlgorithmRsaKeyParamsWithHash(*suggestedParameters);
     224        ASSERT(rsaKeyParameters.hasHash);
     225        if (suggestedRSAKeyParameters.hasHash)
     226            return suggestedRSAKeyParameters.hash == rsaKeyParameters.hash;
     227        suggestedRSAKeyParameters.hasHash = true;
     228        suggestedRSAKeyParameters.hash = rsaKeyParameters.hash;
    230229    }
    231230
  • trunk/Source/WebCore/crypto/CryptoAlgorithmParameters.h

    r159213 r159717  
    4646        HmacParams,
    4747        RsaKeyGenParams,
    48         RsaSsaKeyParams,
     48        RsaKeyParamsWithHash,
    4949        RsaSsaParams
    5050    };
  • trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.h

    r158366 r159717  
    5858    typedef std::unique_ptr<CryptoAlgorithm> (*CryptoAlgorithmConstructor)();
    5959
     60    template<typename AlgorithmClass> void registerAlgorithm()
     61    {
     62        registerAlgorithm(AlgorithmClass::s_name, AlgorithmClass::s_identifier, AlgorithmClass::create);
     63    }
     64
    6065    void registerAlgorithm(const String& name, CryptoAlgorithmIdentifier, CryptoAlgorithmConstructor);
    6166    HashMap<String, CryptoAlgorithmIdentifier> m_nameToIdentifierMap;
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.cpp

    r159578 r159717  
    2929#if ENABLE(SUBTLE_CRYPTO)
    3030
     31#include "CryptoAlgorithmAesCbcParams.h"
    3132#include "CryptoAlgorithmAesKeyGenParams.h"
    3233#include "CryptoKeyAES.h"
     
    5455{
    5556    return s_identifier;
     57}
     58
     59bool CryptoAlgorithmAES_CBC::keyAlgorithmMatches(const CryptoAlgorithmAesCbcParams&, const CryptoKey& key) const
     60{
     61    if (key.algorithmIdentifier() != s_identifier)
     62        return false;
     63    ASSERT(isCryptoKeyAES(key));
     64
     65    return true;
     66}
     67
     68void CryptoAlgorithmAES_CBC::encrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     69{
     70    const CryptoAlgorithmAesCbcParams& aesCBCParameters = toCryptoAlgorithmAesCbcParams(parameters);
     71
     72    if (!keyAlgorithmMatches(aesCBCParameters, key)) {
     73        ec = NOT_SUPPORTED_ERR;
     74        return;
     75    }
     76
     77    platformEncrypt(aesCBCParameters, toCryptoKeyAES(key), data, std::move(callback), std::move(failureCallback), ec);
     78}
     79
     80void CryptoAlgorithmAES_CBC::decrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     81{
     82    const CryptoAlgorithmAesCbcParams& aesCBCParameters = toCryptoAlgorithmAesCbcParams(parameters);
     83
     84    if (!keyAlgorithmMatches(aesCBCParameters, key)) {
     85        ec = NOT_SUPPORTED_ERR;
     86        return;
     87    }
     88
     89    platformDecrypt(aesCBCParameters, toCryptoKeyAES(key), data, std::move(callback), std::move(failureCallback), ec);
    5690}
    5791
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.h

    r159578 r159717  
    3333namespace WebCore {
    3434
     35class CryptoAlgorithmAesCbcParams;
     36class CryptoKeyAES;
     37
    3538class CryptoAlgorithmAES_CBC FINAL : public CryptoAlgorithm {
    3639public:
     
    5053    CryptoAlgorithmAES_CBC();
    5154    virtual ~CryptoAlgorithmAES_CBC();
     55
     56    bool keyAlgorithmMatches(const CryptoAlgorithmAesCbcParams& algorithmParameters, const CryptoKey&) const;
     57    void platformEncrypt(const CryptoAlgorithmAesCbcParams&, const CryptoKeyAES&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     58    void platformDecrypt(const CryptoAlgorithmAesCbcParams&, const CryptoKeyAES&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
    5259};
    5360
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.cpp

    r159578 r159717  
    5757}
    5858
     59bool CryptoAlgorithmHMAC::keyAlgorithmMatches(const CryptoAlgorithmHmacParams&, const CryptoKey& key) const
     60{
     61    if (key.algorithmIdentifier() != s_identifier)
     62        return false;
     63    ASSERT(isCryptoKeyHMAC(key));
     64
     65    return true;
     66}
     67
     68void CryptoAlgorithmHMAC::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     69{
     70    const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
     71
     72    if (!keyAlgorithmMatches(hmacParameters, key)) {
     73        ec = NOT_SUPPORTED_ERR;
     74        return;
     75    }
     76
     77    platformSign(hmacParameters, toCryptoKeyHMAC(key), data, std::move(callback), std::move(failureCallback), ec);
     78}
     79
     80void CryptoAlgorithmHMAC::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, BoolCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     81{
     82    const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
     83
     84    if (!keyAlgorithmMatches(hmacParameters, key)) {
     85        ec = NOT_SUPPORTED_ERR;
     86        return;
     87    }
     88
     89    platformVerify(hmacParameters, toCryptoKeyHMAC(key), expectedSignature, data, std::move(callback), std::move(failureCallback), ec);
     90}
     91
    5992void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
    6093{
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.h

    r159578 r159717  
    3333namespace WebCore {
    3434
     35class CryptoAlgorithmHmacParams;
     36class CryptoKeyHMAC;
     37
    3538class CryptoAlgorithmHMAC FINAL : public CryptoAlgorithm {
    3639public:
     
    5053    CryptoAlgorithmHMAC();
    5154    virtual ~CryptoAlgorithmHMAC();
     55
     56    bool keyAlgorithmMatches(const CryptoAlgorithmHmacParams& algorithmParameters, const CryptoKey&) const;
     57    void platformSign(const CryptoAlgorithmHmacParams&, const CryptoKeyHMAC&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     58    void platformVerify(const CryptoAlgorithmHmacParams&, const CryptoKeyHMAC&, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback, VoidCallback failureCallback, ExceptionCode&);
    5259};
    5360
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp

    r159578 r159717  
    3030
    3131#include "CryptoAlgorithmRsaKeyGenParams.h"
    32 #include "CryptoAlgorithmRsaSsaKeyParams.h"
     32#include "CryptoAlgorithmRsaKeyParamsWithHash.h"
     33#include "CryptoAlgorithmRsaSsaParams.h"
    3334#include "CryptoKeyDataRSAComponents.h"
    3435#include "CryptoKeyRSA.h"
     36#include "ExceptionCode.h"
    3537
    3638namespace WebCore {
     
    5658}
    5759
     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, std::move(callback), std::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, std::move(callback), std::move(failureCallback), ec);
     95}
     96
    5897void CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
    5998{
     
    69108void CryptoAlgorithmRSASSA_PKCS1_v1_5::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback failureCallback, ExceptionCode&)
    70109{
    71     const CryptoAlgorithmRsaSsaKeyParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaKeyParams(parameters);
     110    const CryptoAlgorithmRsaKeyParamsWithHash& rsaKeyParameters = toCryptoAlgorithmRsaKeyParamsWithHash(parameters);
    72111    const CryptoKeyDataRSAComponents& rsaComponents = toCryptoKeyDataRSAComponents(keyData);
    73112
     
    78117    }
    79118
    80     if (rsaSSAParameters.hasHash)
    81         result->restrictToHash(rsaSSAParameters.hash);
     119    if (rsaKeyParameters.hasHash)
     120        result->restrictToHash(rsaKeyParameters.hash);
    82121
    83122    callback(*result);
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h

    r159578 r159717  
    3333namespace WebCore {
    3434
     35class CryptoAlgorithmRsaSsaParams;
     36class CryptoKeyRSA;
     37
    3538class CryptoAlgorithmRSASSA_PKCS1_v1_5 FINAL : public CryptoAlgorithm {
    3639public:
     
    5053    CryptoAlgorithmRSASSA_PKCS1_v1_5();
    5154    virtual ~CryptoAlgorithmRSASSA_PKCS1_v1_5();
     55
     56    bool keyAlgorithmMatches(const CryptoAlgorithmRsaSsaParams& algorithmParameters, const CryptoKey&) const;
     57    void platformSign(const CryptoAlgorithmRsaSsaParams&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback, VoidCallback failureCallback, ExceptionCode&);
     58    void platformVerify(const CryptoAlgorithmRsaSsaParams&, const CryptoKeyRSA&, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback, VoidCallback failureCallback, ExceptionCode&);
    5259};
    5360
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmAES_CBCMac.cpp

    r159578 r159717  
    8383}
    8484
    85 void CryptoAlgorithmAES_CBC::encrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     85void CryptoAlgorithmAES_CBC::platformEncrypt(const CryptoAlgorithmAesCbcParams& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode&)
    8686{
    87     const CryptoAlgorithmAesCbcParams& aesCBCParameters = toCryptoAlgorithmAesCbcParams(parameters);
    88 
    89     if (!isCryptoKeyAES(key)) {
    90         ec = NOT_SUPPORTED_ERR;
    91         return;
    92     }
    93     const CryptoKeyAES& aesKey = toCryptoKeyAES(key);
    94 
    95     transformAES_CBC(kCCEncrypt, aesCBCParameters, aesKey, data, std::move(callback), std::move(failureCallback));
     87    transformAES_CBC(kCCEncrypt, parameters, key, data, std::move(callback), std::move(failureCallback));
    9688}
    9789
    98 void CryptoAlgorithmAES_CBC::decrypt(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     90void CryptoAlgorithmAES_CBC::platformDecrypt(const CryptoAlgorithmAesCbcParams& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode&)
    9991{
    100     const CryptoAlgorithmAesCbcParams& aesCBCParameters = toCryptoAlgorithmAesCbcParams(parameters);
    101 
    102     if (!isCryptoKeyAES(key)) {
    103         ec = NOT_SUPPORTED_ERR;
    104         return;
    105     }
    106     const CryptoKeyAES& aesKey = toCryptoKeyAES(key);
    107 
    108     transformAES_CBC(kCCDecrypt, aesCBCParameters, aesKey, data, std::move(callback), std::move(failureCallback));
     92    transformAES_CBC(kCCDecrypt, parameters, key, data, std::move(callback), std::move(failureCallback));
    10993}
    11094
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmHMACMac.cpp

    r159578 r159717  
    8989}
    9090
    91 void CryptoAlgorithmHMAC::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback, ExceptionCode& ec)
     91void CryptoAlgorithmHMAC::platformSign(const CryptoAlgorithmHmacParams& parameters, const CryptoKeyHMAC& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback, ExceptionCode& ec)
    9292{
    93     const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
    94 
    95     if (!isCryptoKeyHMAC(key)) {
    96         ec = NOT_SUPPORTED_ERR;
    97         return;
    98     }
    99     const CryptoKeyHMAC& hmacKey = toCryptoKeyHMAC(key);
    100 
    10193    CCHmacAlgorithm algorithm;
    102     if (!getCommonCryptoAlgorithm(hmacParameters.hash, algorithm)) {
     94    if (!getCommonCryptoAlgorithm(parameters.hash, algorithm)) {
    10395        ec = NOT_SUPPORTED_ERR;
    10496        return;
    10597    }
    10698
    107     Vector<uint8_t> signature = calculateSignature(algorithm, hmacKey.key(), data);
     99    Vector<uint8_t> signature = calculateSignature(algorithm, key.key(), data);
    108100
    109101    callback(signature);
    110102}
    111103
    112 void CryptoAlgorithmHMAC::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, BoolCallback callback, VoidCallback, ExceptionCode& ec)
     104void CryptoAlgorithmHMAC::platformVerify(const CryptoAlgorithmHmacParams& parameters, const CryptoKeyHMAC& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, BoolCallback callback, VoidCallback, ExceptionCode& ec)
    113105{
    114     const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
    115 
    116     if (!isCryptoKeyHMAC(key)) {
    117         ec = NOT_SUPPORTED_ERR;
    118         return;
    119     }
    120     const CryptoKeyHMAC& hmacKey = toCryptoKeyHMAC(key);
    121 
    122106    CCHmacAlgorithm algorithm;
    123     if (!getCommonCryptoAlgorithm(hmacParameters.hash, algorithm)) {
     107    if (!getCommonCryptoAlgorithm(parameters.hash, algorithm)) {
    124108        ec = NOT_SUPPORTED_ERR;
    125109        return;
    126110    }
    127111
    128     Vector<uint8_t> signature = calculateSignature(algorithm, hmacKey.key(), data);
     112    Vector<uint8_t> signature = calculateSignature(algorithm, key.key(), data);
    129113
    130114    bool result = signature.size() == expectedSignature.second && !memcmp(signature.data(), expectedSignature.first, signature.size());
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp

    r159636 r159717  
    8989}
    9090
    91 void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     91void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaParams& parameters, const CryptoKeyRSA& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
    9292{
    93     const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters);
    94 
    95     if (!isCryptoKeyRSA(key)) {
    96         ec = NOT_SUPPORTED_ERR;
    97         return;
    98     }
    99     const CryptoKeyRSA& rsaKey = toCryptoKeyRSA(key);
    100 
    10193    CCDigestAlgorithm digestAlgorithm;
    102     if (!getCommonCryptoDigestAlgorithm(rsaSSAParameters.hash, digestAlgorithm)) {
     94    if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm)) {
    10395        ec = NOT_SUPPORTED_ERR;
    10496        return;
    10597    }
    10698
    107     std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(rsaSSAParameters.hash);
     99    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(parameters.hash);
    108100    if (!digest) {
    109101        ec = NOT_SUPPORTED_ERR;
     
    118110    size_t signatureSize = signature.size();
    119111
    120     CCCryptorStatus status = CCRSACryptorSign(rsaKey.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
     112    CCCryptorStatus status = CCRSACryptorSign(key.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
    121113    if (status) {
    122114        failureCallback();
     
    128120}
    129121
    130 void CryptoAlgorithmRSASSA_PKCS1_v1_5::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
     122void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(const CryptoAlgorithmRsaSsaParams& parameters, const CryptoKeyRSA& key, const CryptoOperationData& signature, const CryptoOperationData& data, BoolCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
    131123{
    132     const CryptoAlgorithmRsaSsaParams& rsaSSAParameters = toCryptoAlgorithmRsaSsaParams(parameters);
    133 
    134     if (!isCryptoKeyRSA(key)) {
    135         ec = NOT_SUPPORTED_ERR;
    136         return;
    137     }
    138     const CryptoKeyRSA& rsaKey = toCryptoKeyRSA(key);
    139 
    140124    CCDigestAlgorithm digestAlgorithm;
    141     if (!getCommonCryptoDigestAlgorithm(rsaSSAParameters.hash, digestAlgorithm)) {
     125    if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm)) {
    142126        ec = NOT_SUPPORTED_ERR;
    143127        return;
    144128    }
    145129
    146     std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(rsaSSAParameters.hash);
     130    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(parameters.hash);
    147131    if (!digest) {
    148132        ec = NOT_SUPPORTED_ERR;
     
    154138    Vector<uint8_t> digestData = digest->computeHash();
    155139
    156     CCCryptorStatus status = CCRSACryptorVerify(rsaKey.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.first, signature.second);
     140    CCCryptorStatus status = CCRSACryptorVerify(key.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.first, signature.second);
    157141    if (!status)
    158142        callback(true);
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmRegistryMac.cpp

    r159180 r159717  
    4242void CryptoAlgorithmRegistry::platformRegisterAlgorithms()
    4343{
    44     registerAlgorithm(CryptoAlgorithmAES_CBC::s_name, CryptoAlgorithmAES_CBC::s_identifier, CryptoAlgorithmAES_CBC::create);
    45     registerAlgorithm(CryptoAlgorithmHMAC::s_name, CryptoAlgorithmHMAC::s_identifier, CryptoAlgorithmHMAC::create);
    46     registerAlgorithm(CryptoAlgorithmRSASSA_PKCS1_v1_5::s_name, CryptoAlgorithmRSASSA_PKCS1_v1_5::s_identifier, CryptoAlgorithmRSASSA_PKCS1_v1_5::create);
    47     registerAlgorithm(CryptoAlgorithmSHA1::s_name, CryptoAlgorithmSHA1::s_identifier, CryptoAlgorithmSHA1::create);
    48     registerAlgorithm(CryptoAlgorithmSHA224::s_name, CryptoAlgorithmSHA224::s_identifier, CryptoAlgorithmSHA224::create);
    49     registerAlgorithm(CryptoAlgorithmSHA256::s_name, CryptoAlgorithmSHA256::s_identifier, CryptoAlgorithmSHA256::create);
    50     registerAlgorithm(CryptoAlgorithmSHA384::s_name, CryptoAlgorithmSHA384::s_identifier, CryptoAlgorithmSHA384::create);
    51     registerAlgorithm(CryptoAlgorithmSHA512::s_name, CryptoAlgorithmSHA512::s_identifier, CryptoAlgorithmSHA512::create);
     44    registerAlgorithm<CryptoAlgorithmAES_CBC>();
     45    registerAlgorithm<CryptoAlgorithmHMAC>();
     46    registerAlgorithm<CryptoAlgorithmRSASSA_PKCS1_v1_5>();
     47    registerAlgorithm<CryptoAlgorithmSHA1>();
     48    registerAlgorithm<CryptoAlgorithmSHA224>();
     49    registerAlgorithm<CryptoAlgorithmSHA256>();
     50    registerAlgorithm<CryptoAlgorithmSHA384>();
     51    registerAlgorithm<CryptoAlgorithmSHA512>();
    5252}
    5353
  • trunk/Source/WebCore/crypto/parameters/CryptoAlgorithmRsaKeyParamsWithHash.h

    r159636 r159717  
    2424 */
    2525
    26 #ifndef CryptoAlgorithmRsaSsaKeyParams_h
     26#ifndef CryptoAlgorithmRsaKeyParamsWithHash_h
    2727#define CryptoAlgorithmRsaSsaKey
    2828
     
    3737// It is necessary to support import from JWK, which treats hash function as part of algorithm
    3838// identifier, so we need to remember it to compare with one passed to sign or verify functions.
    39 class CryptoAlgorithmRsaSsaKeyParams FINAL : public CryptoAlgorithmParameters {
     39class CryptoAlgorithmRsaKeyParamsWithHash FINAL : public CryptoAlgorithmParameters {
    4040public:
    41     CryptoAlgorithmRsaSsaKeyParams()
     41    CryptoAlgorithmRsaKeyParamsWithHash()
    4242        : hasHash(false)
    4343    {
     
    4848    CryptoAlgorithmIdentifier hash;
    4949
    50     virtual Class parametersClass() const OVERRIDE { return Class::RsaSsaKeyParams; }
     50    virtual Class parametersClass() const OVERRIDE { return Class::RsaKeyParamsWithHash; }
    5151};
    5252
    53 CRYPTO_ALGORITHM_PARAMETERS_CASTS(RsaSsaKeyParams)
     53CRYPTO_ALGORITHM_PARAMETERS_CASTS(RsaKeyParamsWithHash)
    5454
    5555}
Note: See TracChangeset for help on using the changeset viewer.