Changeset 213624 in webkit


Ignore:
Timestamp:
Mar 8, 2017 8:04:29 PM (7 years ago)
Author:
jiewen_tan@apple.com
Message:

[WebCrypto] Implement ECDH DeriveBits operation
https://bugs.webkit.org/show_bug.cgi?id=169319
<rdar://problem/23789585>

Reviewed by Brent Fulgham.

Source/WebCore:

This patch implements DeriveBits operation of ECDH according to the spec:
https://www.w3.org/TR/WebCryptoAPI/#ecdh-operations.

Tests: crypto/subtle/derive-bits-malformed-parameters.html

crypto/subtle/ecdh-derive-bits-malformed-parametrs.html
crypto/subtle/ecdh-generate-key-derive-bits.html
crypto/subtle/ecdh-import-key-derive-bits-custom-length.html
crypto/subtle/ecdh-import-key-derive-bits-null-length.html
crypto/workers/subtle/ecdh-import-key-derive-bits.html

  • CMakeLists.txt:
  • DerivedSources.make:
  • PlatformGTK.cmake:
  • PlatformMac.cmake:
  • WebCore.xcodeproj/project.pbxproj:
  • bindings/js/JSSubtleCryptoCustom.cpp:

(WebCore::normalizeCryptoAlgorithmParameters):
(WebCore::jsSubtleCryptoFunctionDeriveKeyPromise):
(WebCore::jsSubtleCryptoFunctionDeriveBitsPromise):
(WebCore::JSSubtleCrypto::generateKey):
Reorder a bit of the functions.

  • crypto/CommonCryptoUtilities.h:
  • crypto/CryptoAlgorithm.cpp:

(WebCore::CryptoAlgorithm::deriveBits):

  • crypto/CryptoAlgorithm.h:
  • crypto/CryptoAlgorithmParameters.h:
  • crypto/algorithms/CryptoAlgorithmECDH.cpp:

(WebCore::CryptoAlgorithmECDH::deriveBits):

  • crypto/algorithms/CryptoAlgorithmECDH.h:
  • crypto/gnutls/CryptoAlgorithmECDHGnuTLS.cpp: Added.

(WebCore::CryptoAlgorithmECDH::platformDeriveBits):

  • crypto/keys/CryptoKeyEC.h:
  • crypto/mac/CryptoAlgorithmECDHMac.cpp: Added.

(WebCore::CryptoAlgorithmECDH::platformDeriveBits):

  • crypto/parameters/CryptoAlgorithmEcdhKeyDeriveParams.h: Added.
  • crypto/parameters/EcdhKeyDeriveParams.idl: Added.

LayoutTests:

Refine some comments.

  • crypto/subtle/derive-bits-malformed-parameters-expected.txt: Renamed from LayoutTests/crypto/subtle/deriveBits-malformed-parameters-expected.txt.
  • crypto/subtle/derive-bits-malformed-parameters.html: Added.
  • crypto/subtle/deriveBits-malformed-parameters.html: Removed.
  • crypto/subtle/ecdh-derive-bits-malformed-parametrs-expected.txt: Added.
  • crypto/subtle/ecdh-derive-bits-malformed-parametrs.html: Added.
  • crypto/subtle/ecdh-generate-key-derive-bits-expected.txt: Added.
  • crypto/subtle/ecdh-generate-key-derive-bits.html: Added.
  • crypto/subtle/ecdh-import-key-derive-bits-custom-length-expected.txt: Added.
  • crypto/subtle/ecdh-import-key-derive-bits-custom-length.html: Added.
  • crypto/subtle/ecdh-import-key-derive-bits-null-length-expected.txt: Added.
  • crypto/subtle/ecdh-import-key-derive-bits-null-length.html: Added.
  • crypto/workers/subtle/ecdh-import-key-derive-bits-expected.txt: Added.
  • crypto/workers/subtle/ecdh-import-key-derive-bits.html: Added.
  • crypto/workers/subtle/resources/ecdh-import-key-derive-bits.js: Added.
Location:
trunk
Files:
12 added
1 deleted
16 edited
4 copied
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r213623 r213624  
     12017-03-08  Jiewen Tan  <jiewen_tan@apple.com>
     2
     3        [WebCrypto] Implement ECDH DeriveBits operation
     4        https://bugs.webkit.org/show_bug.cgi?id=169319
     5        <rdar://problem/23789585>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        * TestExpectations:
     10        Refine some comments.
     11        * crypto/subtle/derive-bits-malformed-parameters-expected.txt: Renamed from LayoutTests/crypto/subtle/deriveBits-malformed-parameters-expected.txt.
     12        * crypto/subtle/derive-bits-malformed-parameters.html: Added.
     13        * crypto/subtle/deriveBits-malformed-parameters.html: Removed.
     14        * crypto/subtle/ecdh-derive-bits-malformed-parametrs-expected.txt: Added.
     15        * crypto/subtle/ecdh-derive-bits-malformed-parametrs.html: Added.
     16        * crypto/subtle/ecdh-generate-key-derive-bits-expected.txt: Added.
     17        * crypto/subtle/ecdh-generate-key-derive-bits.html: Added.
     18        * crypto/subtle/ecdh-import-key-derive-bits-custom-length-expected.txt: Added.
     19        * crypto/subtle/ecdh-import-key-derive-bits-custom-length.html: Added.
     20        * crypto/subtle/ecdh-import-key-derive-bits-null-length-expected.txt: Added.
     21        * crypto/subtle/ecdh-import-key-derive-bits-null-length.html: Added.
     22        * crypto/workers/subtle/ecdh-import-key-derive-bits-expected.txt: Added.
     23        * crypto/workers/subtle/ecdh-import-key-derive-bits.html: Added.
     24        * crypto/workers/subtle/resources/ecdh-import-key-derive-bits.js: Added.
     25
    1262017-03-08  John Wilander  <wilander@apple.com>
    227
  • trunk/LayoutTests/TestExpectations

    r213604 r213624  
    960960workers/bomb-with-v8.html [ Skip ]
    961961
    962 # WebCryptoAPI tests that take too long to complete, need to scale down. webkit.org/b/159638
     962# WebCryptoAPI tests that take too long to complete
    963963imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/hkdf.worker.html [ Skip ]
    964964imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/pbkdf2.worker.html [ Skip ]
  • trunk/LayoutTests/crypto/subtle/derive-bits-malformed-parameters-expected.txt

    r213623 r213624  
    77PASS crypto.subtle.deriveBits(1) rejected promise  with TypeError: Not enough arguments.
    88PASS crypto.subtle.deriveBits(1, 2) rejected promise  with TypeError: Not enough arguments.
    9 PASS crypto.subtle.deriveBits("ECDH", 2, 3) rejected promise  with NotSupportedError (DOM Exception 9): The operation is not supported..
     9PASS crypto.subtle.deriveBits({ name:"ECDH", public:wrongKey }, wrongKey, 128) rejected promise  with InvalidAccessError (DOM Exception 15): CryptoKey doesn't match AlgorithmIdentifier.
     10PASS crypto.subtle.deriveBits({ name:"ECDH", public:wrongKey }, wrongKey, 128) rejected promise  with InvalidAccessError (DOM Exception 15): CryptoKey doesn't support bits derivation.
    1011PASS successfullyParsed is true
    1112
  • trunk/Source/WebCore/CMakeLists.txt

    r213598 r213624  
    345345    crypto/parameters/AesKeyGenParams.idl
    346346    crypto/parameters/EcKeyParams.idl
     347    crypto/parameters/EcdhKeyDeriveParams.idl
    347348    crypto/parameters/HmacKeyParams.idl
    348349    crypto/parameters/RsaHashedImportParams.idl
  • trunk/Source/WebCore/ChangeLog

    r213623 r213624  
     12017-03-08  Jiewen Tan  <jiewen_tan@apple.com>
     2
     3        [WebCrypto] Implement ECDH DeriveBits operation
     4        https://bugs.webkit.org/show_bug.cgi?id=169319
     5        <rdar://problem/23789585>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        This patch implements DeriveBits operation of ECDH according to the spec:
     10        https://www.w3.org/TR/WebCryptoAPI/#ecdh-operations.
     11
     12        Tests: crypto/subtle/derive-bits-malformed-parameters.html
     13               crypto/subtle/ecdh-derive-bits-malformed-parametrs.html
     14               crypto/subtle/ecdh-generate-key-derive-bits.html
     15               crypto/subtle/ecdh-import-key-derive-bits-custom-length.html
     16               crypto/subtle/ecdh-import-key-derive-bits-null-length.html
     17               crypto/workers/subtle/ecdh-import-key-derive-bits.html
     18
     19        * CMakeLists.txt:
     20        * DerivedSources.make:
     21        * PlatformGTK.cmake:
     22        * PlatformMac.cmake:
     23        * WebCore.xcodeproj/project.pbxproj:
     24        * bindings/js/JSSubtleCryptoCustom.cpp:
     25        (WebCore::normalizeCryptoAlgorithmParameters):
     26        (WebCore::jsSubtleCryptoFunctionDeriveKeyPromise):
     27        (WebCore::jsSubtleCryptoFunctionDeriveBitsPromise):
     28        (WebCore::JSSubtleCrypto::generateKey):
     29        Reorder a bit of the functions.
     30        * crypto/CommonCryptoUtilities.h:
     31        * crypto/CryptoAlgorithm.cpp:
     32        (WebCore::CryptoAlgorithm::deriveBits):
     33        * crypto/CryptoAlgorithm.h:
     34        * crypto/CryptoAlgorithmParameters.h:
     35        * crypto/algorithms/CryptoAlgorithmECDH.cpp:
     36        (WebCore::CryptoAlgorithmECDH::deriveBits):
     37        * crypto/algorithms/CryptoAlgorithmECDH.h:
     38        * crypto/gnutls/CryptoAlgorithmECDHGnuTLS.cpp: Added.
     39        (WebCore::CryptoAlgorithmECDH::platformDeriveBits):
     40        * crypto/keys/CryptoKeyEC.h:
     41        * crypto/mac/CryptoAlgorithmECDHMac.cpp: Added.
     42        (WebCore::CryptoAlgorithmECDH::platformDeriveBits):
     43        * crypto/parameters/CryptoAlgorithmEcdhKeyDeriveParams.h: Added.
     44        * crypto/parameters/EcdhKeyDeriveParams.idl: Added.
     45
    1462017-03-08  John Wilander  <wilander@apple.com>
    247
  • trunk/Source/WebCore/DerivedSources.make

    r213598 r213624  
    277277    $(WebCore)/crypto/parameters/AesKeyGenParams.idl \
    278278    $(WebCore)/crypto/parameters/EcKeyParams.idl \
     279    $(WebCore)/crypto/parameters/EcdhKeyDeriveParams.idl \
    279280    $(WebCore)/crypto/parameters/HmacKeyParams.idl \
    280281    $(WebCore)/crypto/parameters/RsaHashedImportParams.idl \
  • trunk/Source/WebCore/PlatformGTK.cmake

    r213489 r213624  
    404404        crypto/gnutls/CryptoAlgorithmAES_GCMGnuTLS.cpp
    405405        crypto/gnutls/CryptoAlgorithmAES_KWGnuTLS.cpp
     406        crypto/gnutls/CryptoAlgorithmECDHGnuTLS.cpp
    406407        crypto/gnutls/CryptoAlgorithmRSAES_PKCS1_v1_5GnuTLS.cpp
    407408        crypto/gnutls/CryptoAlgorithmRSASSA_PKCS1_v1_5GnuTLS.cpp
  • trunk/Source/WebCore/PlatformMac.cmake

    r213541 r213624  
    207207    crypto/mac/CryptoAlgorithmAES_GCMMac.cpp
    208208    crypto/mac/CryptoAlgorithmAES_KWMac.cpp
     209    crypto/mac/CryptoAlgorithmECDHMac.cpp
    209210    crypto/mac/CryptoAlgorithmHMACMac.cpp
    210211    crypto/mac/CryptoAlgorithmRSAES_PKCS1_v1_5Mac.cpp
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r213598 r213624  
    23792379                5750A9861E6A216800705C4A /* CryptoAlgorithmECDH.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5750A9841E6A216800705C4A /* CryptoAlgorithmECDH.cpp */; };
    23802380                5750A9871E6A216800705C4A /* CryptoAlgorithmECDH.h in Headers */ = {isa = PBXBuildFile; fileRef = 5750A9851E6A216800705C4A /* CryptoAlgorithmECDH.h */; };
     2381                5768142A1E6F99C100E77754 /* CryptoAlgorithmEcdhKeyDeriveParams.h in Headers */ = {isa = PBXBuildFile; fileRef = 576814291E6F99C100E77754 /* CryptoAlgorithmEcdhKeyDeriveParams.h */; };
     2382                576814351E6FE3E800E77754 /* CryptoAlgorithmECDHMac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 576814341E6FE3E800E77754 /* CryptoAlgorithmECDHMac.cpp */; };
     2383                576814401E709FA100E77754 /* JSEcdhKeyDeriveParams.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5768143D1E709C3600E77754 /* JSEcdhKeyDeriveParams.cpp */; };
     2384                576814411E709FA400E77754 /* JSEcdhKeyDeriveParams.h in Headers */ = {isa = PBXBuildFile; fileRef = 5768143E1E709C3600E77754 /* JSEcdhKeyDeriveParams.h */; };
    23812385                5768E4341DB7524500D0A4F7 /* JSRsaHashedKeyGenParams.h in Headers */ = {isa = PBXBuildFile; fileRef = 5768E4331DB7524500D0A4F7 /* JSRsaHashedKeyGenParams.h */; };
    23822386                5768E4361DB7527400D0A4F7 /* JSRsaHashedKeyGenParams.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5768E4351DB7527300D0A4F7 /* JSRsaHashedKeyGenParams.cpp */; };
     
    98089812                5750A9841E6A216800705C4A /* CryptoAlgorithmECDH.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoAlgorithmECDH.cpp; sourceTree = "<group>"; };
    98099813                5750A9851E6A216800705C4A /* CryptoAlgorithmECDH.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoAlgorithmECDH.h; sourceTree = "<group>"; };
     9814                576814281E6F98AD00E77754 /* EcdhKeyDeriveParams.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = EcdhKeyDeriveParams.idl; sourceTree = "<group>"; };
     9815                576814291E6F99C100E77754 /* CryptoAlgorithmEcdhKeyDeriveParams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoAlgorithmEcdhKeyDeriveParams.h; sourceTree = "<group>"; };
     9816                576814341E6FE3E800E77754 /* CryptoAlgorithmECDHMac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoAlgorithmECDHMac.cpp; sourceTree = "<group>"; };
     9817                5768143D1E709C3600E77754 /* JSEcdhKeyDeriveParams.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSEcdhKeyDeriveParams.cpp; sourceTree = "<group>"; };
     9818                5768143E1E709C3600E77754 /* JSEcdhKeyDeriveParams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSEcdhKeyDeriveParams.h; sourceTree = "<group>"; };
    98109819                5768E4331DB7524500D0A4F7 /* JSRsaHashedKeyGenParams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSRsaHashedKeyGenParams.h; sourceTree = "<group>"; };
    98119820                5768E4351DB7527300D0A4F7 /* JSRsaHashedKeyGenParams.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSRsaHashedKeyGenParams.cpp; sourceTree = "<group>"; };
     
    2359923608                                57B5F80D1E5D2F2D00F34F90 /* CryptoAlgorithmAES_GCMMac.cpp */,
    2360023609                                E1FE137C184D270200892F13 /* CryptoAlgorithmAES_KWMac.cpp */,
     23610                                576814341E6FE3E800E77754 /* CryptoAlgorithmECDHMac.cpp */,
    2360123611                                E125F8371822F1EB00D84CD9 /* CryptoAlgorithmHMACMac.cpp */,
    2360223612                                E1BB84AC1822CA7400525043 /* CryptoAlgorithmRegistryMac.cpp */,
     
    2368023690                                E19AC3F61824E5D100349426 /* CryptoAlgorithmAesKeyGenParamsDeprecated.h */,
    2368123691                                5750A97D1E6A13EF00705C4A /* CryptoAlgorithmEcKeyParams.h */,
     23692                                576814291E6F99C100E77754 /* CryptoAlgorithmEcdhKeyDeriveParams.h */,
    2368223693                                577483181DB4491F00716EF9 /* CryptoAlgorithmHmacKeyParams.h */,
    2368323694                                E19DA29B18189ADD00088BC8 /* CryptoAlgorithmHmacKeyParamsDeprecated.h */,
     
    2369223703                                E1BD331B182D8EE900C05D9F /* CryptoAlgorithmRsaSsaParamsDeprecated.h */,
    2369323704                                5750A97C1E6A12B400705C4A /* EcKeyParams.idl */,
     23705                                576814281E6F98AD00E77754 /* EcdhKeyDeriveParams.idl */,
    2369423706                                577483171DB1FE8900716EF9 /* HmacKeyParams.idl */,
    2369523707                                57E2336C1DCD437000F28D01 /* RsaHashedImportParams.idl */,
     
    2386823880                                5750A97F1E6A150800705C4A /* JSEcKeyParams.cpp */,
    2386923881                                5750A9801E6A150800705C4A /* JSEcKeyParams.h */,
     23882                                5768143D1E709C3600E77754 /* JSEcdhKeyDeriveParams.cpp */,
     23883                                5768143E1E709C3600E77754 /* JSEcdhKeyDeriveParams.h */,
    2387023884                                57E233681DCAB24300F28D01 /* JSHmacKeyParams.cpp */,
    2387123885                                57E233661DCAB21C00F28D01 /* JSHmacKeyParams.h */,
     
    2674226756                                7C6579F41E00856600E3A27A /* JSApplePayShippingMethod.h in Headers */,
    2674326757                                1AE96A931D1A0DDD00B86768 /* JSApplePayShippingMethodSelectedEvent.h in Headers */,
     26758                                576814411E709FA400E77754 /* JSEcdhKeyDeriveParams.h in Headers */,
    2674426759                                65DF31DB09D1C123000BE325 /* JSAttr.h in Headers */,
    2674526760                                FDA15E9E12B03EE1003A583A /* JSAudioBuffer.h in Headers */,
     
    2847028485                                BC5EB5E50E81BF6D00B25965 /* StyleSurroundData.h in Headers */,
    2847128486                                BC5EB8100E81F2CE00B25965 /* StyleTransformData.h in Headers */,
     28487                                5768142A1E6F99C100E77754 /* CryptoAlgorithmEcdhKeyDeriveParams.h in Headers */,
    2847228488                                E4DEAA1817A93DC3000E0430 /* StyleTreeResolver.h in Headers */,
    2847328489                                E42E76DC1C7AF77600E3614D /* StyleUpdate.h in Headers */,
     
    3040730423                                D66817FA166FE6D700FA07B4 /* HTMLTemplateElement.cpp in Sources */,
    3040830424                                A81369D7097374F600D74463 /* HTMLTextAreaElement.cpp in Sources */,
     30425                                576814401E709FA100E77754 /* JSEcdhKeyDeriveParams.cpp in Sources */,
    3040930426                                9BC6C21C13CCC97B008E0337 /* HTMLTextFormControlElement.cpp in Sources */,
    3041030427                                830519951BB0F11000F3772E /* HTMLTimeElement.cpp in Sources */,
     
    3060830625                                BC124EFF0C26447A009E2349 /* JSBarProp.cpp in Sources */,
    3060930626                                BC946346107A934B00857193 /* JSBeforeLoadEvent.cpp in Sources */,
     30627                                576814351E6FE3E800E77754 /* CryptoAlgorithmECDHMac.cpp in Sources */,
    3061030628                                70F546E8B8B5D7DC54EE144E /* JSBeforeUnloadEvent.cpp in Sources */,
    3061130629                                FDF09DC81399B62200688E5B /* JSBiquadFilterNode.cpp in Sources */,
  • trunk/Source/WebCore/bindings/js/JSSubtleCryptoCustom.cpp

    r213560 r213624  
    4040#include "JSDOMWrapper.h"
    4141#include "JSEcKeyParams.h"
     42#include "JSEcdhKeyDeriveParams.h"
    4243#include "JSHmacKeyParams.h"
    4344#include "JSJsonWebKey.h"
     
    161162            break;
    162163        case Operations::DeriveKey:
    163         case Operations::DeriveBits:
    164164            throwNotSupportedError(state, scope);
    165165            return nullptr;
     166        case Operations::DeriveBits:
     167            switch (*identifier) {
     168            case CryptoAlgorithmIdentifier::ECDH: {
     169                // Remove this hack once https://bugs.webkit.org/show_bug.cgi?id=169333 is fixed.
     170                JSValue nameValue = value.getObject()->get(&state, Identifier::fromString(&state, "name"));
     171                JSValue publicValue = value.getObject()->get(&state, Identifier::fromString(&state, "public"));
     172                JSObject* newValue = constructEmptyObject(&state);
     173                newValue->putDirect(vm, Identifier::fromString(&vm, "name"), nameValue);
     174                newValue->putDirect(vm, Identifier::fromString(&vm, "publicKey"), publicValue);
     175
     176                auto params = convertDictionary<CryptoAlgorithmEcdhKeyDeriveParams>(state, newValue);
     177                RETURN_IF_EXCEPTION(scope, nullptr);
     178                result = std::make_unique<CryptoAlgorithmEcdhKeyDeriveParams>(params);
     179                break;
     180            }
     181            default:
     182                throwNotSupportedError(state, scope);
     183                return nullptr;
     184            }
     185            break;
    166186        case Operations::GenerateKey:
    167187            switch (*identifier) {
     
    661681}
    662682
    663 static void jsSubtleCryptoFunctionDeriveKeyPromise(ExecState& state, Ref<DeferredPromise>&& promise)
    664 {
    665     VM& vm = state.vm();
    666     auto scope = DECLARE_THROW_SCOPE(vm);
    667 
    668     if (UNLIKELY(state.argumentCount() < 5)) {
    669         promise->reject<IDLAny>(createNotEnoughArgumentsError(&state));
    670         return;
    671     }
    672 
    673     auto params = normalizeCryptoAlgorithmParameters(state, state.uncheckedArgument(0), Operations::DeriveKey);
    674     RETURN_IF_EXCEPTION(scope, void());
    675 
    676     // We should always return a NOT_SUPPORTED_ERR since we currently don't support any algorithms that has deriveKey operation.
    677     ASSERT_NOT_REACHED();
    678 }
    679 
    680 static void jsSubtleCryptoFunctionDeriveBitsPromise(ExecState& state, Ref<DeferredPromise>&& promise)
    681 {
    682     VM& vm = state.vm();
    683     auto scope = DECLARE_THROW_SCOPE(vm);
    684 
    685     if (UNLIKELY(state.argumentCount() < 3)) {
    686         promise->reject<IDLAny>(createNotEnoughArgumentsError(&state));
    687         return;
    688     }
    689 
    690     auto params = normalizeCryptoAlgorithmParameters(state, state.uncheckedArgument(0), Operations::DeriveBits);
    691     RETURN_IF_EXCEPTION(scope, void());
    692 
    693     // We should always return a NOT_SUPPORTED_ERR since we currently don't support any algorithms that has deriveBits operation.
    694     ASSERT_NOT_REACHED();
    695 }
    696 
    697683static void jsSubtleCryptoFunctionGenerateKeyPromise(ExecState& state, Ref<DeferredPromise>&& promise)
    698684{
     
    748734}
    749735
     736static void jsSubtleCryptoFunctionDeriveKeyPromise(ExecState& state, Ref<DeferredPromise>&& promise)
     737{
     738    VM& vm = state.vm();
     739    auto scope = DECLARE_THROW_SCOPE(vm);
     740
     741    if (UNLIKELY(state.argumentCount() < 5)) {
     742        promise->reject<IDLAny>(createNotEnoughArgumentsError(&state));
     743        return;
     744    }
     745
     746    auto params = normalizeCryptoAlgorithmParameters(state, state.uncheckedArgument(0), Operations::DeriveKey);
     747    RETURN_IF_EXCEPTION(scope, void());
     748
     749    // We should always return a NOT_SUPPORTED_ERR since we currently don't support any algorithms that has deriveKey operation.
     750    ASSERT_NOT_REACHED();
     751}
     752
     753static void jsSubtleCryptoFunctionDeriveBitsPromise(ExecState& state, Ref<DeferredPromise>&& promise)
     754{
     755    VM& vm = state.vm();
     756    auto scope = DECLARE_THROW_SCOPE(vm);
     757
     758    if (UNLIKELY(state.argumentCount() < 3)) {
     759        promise->reject<IDLAny>(createNotEnoughArgumentsError(&state));
     760        return;
     761    }
     762
     763    auto params = normalizeCryptoAlgorithmParameters(state, state.uncheckedArgument(0), Operations::DeriveBits);
     764    RETURN_IF_EXCEPTION(scope, void());
     765
     766    auto baseKey = toCryptoKey(state, state.uncheckedArgument(1));
     767    RETURN_IF_EXCEPTION(scope, void());
     768
     769    auto length = convert<IDLUnsignedLong>(state, state.uncheckedArgument(2), IntegerConversionConfiguration::Normal);
     770    RETURN_IF_EXCEPTION(scope, void());
     771
     772    if (params->identifier != baseKey->algorithmIdentifier()) {
     773        promise->reject(INVALID_ACCESS_ERR, ASCIILiteral("CryptoKey doesn't match AlgorithmIdentifier"));
     774        return;
     775    }
     776
     777    if (!baseKey->allows(CryptoKeyUsageDeriveBits)) {
     778        promise->reject(INVALID_ACCESS_ERR, ASCIILiteral("CryptoKey doesn't support bits derivation"));
     779        return;
     780    }
     781
     782    auto algorithm = CryptoAlgorithmRegistry::singleton().create(params->identifier);
     783    if (UNLIKELY(!algorithm)) {
     784        throwNotSupportedError(state, scope);
     785        return;
     786    }
     787
     788    auto callback = [capturedPromise = promise.copyRef()](const Vector<uint8_t>& derivedKey) mutable {
     789        fulfillPromiseWithArrayBuffer(WTFMove(capturedPromise), derivedKey.data(), derivedKey.size());
     790        return;
     791    };
     792    auto exceptionCallback = [capturedPromise = WTFMove(promise)](ExceptionCode ec) mutable {
     793        rejectWithException(WTFMove(capturedPromise), ec);
     794    };
     795
     796    auto subtle = jsDynamicDowncast<JSSubtleCrypto*>(vm, state.thisValue());
     797    ASSERT(subtle);
     798    algorithm->deriveBits(WTFMove(params), baseKey.releaseNonNull(), length, WTFMove(callback), WTFMove(exceptionCallback), *scriptExecutionContextFromExecState(&state), subtle->wrapped().workQueue());
     799}
     800
    750801static void jsSubtleCryptoFunctionImportKeyPromise(ExecState& state, Ref<DeferredPromise>&& promise)
    751802{
     
    10991150}
    11001151
     1152
     1153JSValue JSSubtleCrypto::generateKey(ExecState& state)
     1154{
     1155    return callPromiseFunction<jsSubtleCryptoFunctionGenerateKeyPromise, PromiseExecutionScope::WindowOrWorker>(state);
     1156}
     1157
    11011158JSValue JSSubtleCrypto::deriveKey(ExecState& state)
    11021159{
     
    11091166}
    11101167
    1111 JSValue JSSubtleCrypto::generateKey(ExecState& state)
    1112 {
    1113     return callPromiseFunction<jsSubtleCryptoFunctionGenerateKeyPromise, PromiseExecutionScope::WindowOrWorker>(state);
    1114 }
    1115 
    11161168JSValue JSSubtleCrypto::importKey(ExecState& state)
    11171169{
  • trunk/Source/WebCore/crypto/CommonCryptoUtilities.h

    r213560 r213624  
    108108extern "C" CCCryptorStatus CCECCryptorCreateFromData(size_t keySize, uint8_t *qX, size_t qXLength, uint8_t *qY, size_t qYLength, CCECCryptorRef *ref);
    109109extern "C" CCCryptorStatus CCECCryptorGetKeyComponents(CCECCryptorRef ecKey, size_t *keySize, uint8_t *qX, size_t *qXLength, uint8_t *qY, size_t *qYLength, uint8_t *d, size_t *dLength);
    110 
     110extern "C" CCCryptorStatus CCECCryptorComputeSharedSecret(CCECCryptorRef privateKey, CCECCryptorRef publicKey, void *out, size_t *outLen);
    111111
    112112#if !USE(APPLE_INTERNAL_SDK)
  • trunk/Source/WebCore/crypto/CryptoAlgorithm.cpp

    r209200 r213624  
    5959
    6060void CryptoAlgorithm::generateKey(const CryptoAlgorithmParameters&, bool, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&)
     61{
     62    exceptionCallback(NOT_SUPPORTED_ERR);
     63}
     64
     65void CryptoAlgorithm::deriveBits(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, unsigned long, VectorCallback&&, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&, WorkQueue&)
    6166{
    6267    exceptionCallback(NOT_SUPPORTED_ERR);
  • trunk/Source/WebCore/crypto/CryptoAlgorithm.h

    r213560 r213624  
    7373    virtual void digest(Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
    7474    virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&);
     75    virtual void deriveBits(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, unsigned long length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
    7576    // FIXME: https://bugs.webkit.org/show_bug.cgi?id=169262
    7677    virtual void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&);
  • trunk/Source/WebCore/crypto/CryptoAlgorithmParameters.h

    r213489 r213624  
    4242        AesKeyGenParams,
    4343        EcKeyParams,
     44        EcdhKeyDeriveParams,
    4445        HmacKeyParams,
    4546        RsaHashedKeyGenParams,
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.cpp

    r213560 r213624  
    3030
    3131#include "CryptoAlgorithmEcKeyParams.h"
     32#include "CryptoAlgorithmEcdhKeyDeriveParams.h"
    3233#include "CryptoKeyEC.h"
    3334#include "ExceptionCode.h"
     
    6465    pair.privateKey->setUsagesBitmap(pair.privateKey->usagesBitmap() & (CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits));
    6566    callback(WTFMove(pair));
     67}
     68
     69void CryptoAlgorithmECDH::deriveBits(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& baseKey, unsigned long length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
     70{
     71    // We only accept length that is a multiple of 8.
     72    if (length % 8) {
     73        exceptionCallback(OperationError);
     74        return;
     75    }
     76
     77    ASSERT(parameters);
     78    auto& ecParameters = downcast<CryptoAlgorithmEcdhKeyDeriveParams>(*parameters);
     79
     80    if (baseKey->type() != CryptoKey::Type::Private) {
     81        exceptionCallback(INVALID_ACCESS_ERR);
     82        return;
     83    }
     84    ASSERT(ecParameters.publicKey);
     85    if (ecParameters.publicKey->type() != CryptoKey::Type::Public) {
     86        exceptionCallback(INVALID_ACCESS_ERR);
     87        return;
     88    }
     89    if (baseKey->algorithmIdentifier() != ecParameters.publicKey->algorithmIdentifier()) {
     90        exceptionCallback(INVALID_ACCESS_ERR);
     91        return;
     92    }
     93    auto& ecBaseKey = downcast<CryptoKeyEC>(baseKey.get());
     94    auto& ecPublicKey = downcast<CryptoKeyEC>(*(ecParameters.publicKey.get()));
     95    if (ecBaseKey.namedCurve() != ecPublicKey.namedCurve()) {
     96        exceptionCallback(INVALID_ACCESS_ERR);
     97        return;
     98    }
     99
     100    auto unifiedCallback = [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](std::optional<Vector<uint8_t>>&& derivedKey, unsigned long length) mutable {
     101        if (!derivedKey) {
     102            exceptionCallback(OperationError);
     103            return;
     104        }
     105        if (!length) {
     106            callback(WTFMove(*derivedKey));
     107            return;
     108        }
     109        if (length / 8 > (*derivedKey).size()) {
     110            exceptionCallback(OperationError);
     111            return;
     112        }
     113        (*derivedKey).shrink(length / 8);
     114        callback(WTFMove(*derivedKey));
     115    };
     116    platformDeriveBits(WTFMove(baseKey), ecParameters.publicKey.releaseNonNull(), length, WTFMove(unifiedCallback), context, workQueue);
    66117}
    67118
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.h

    r213560 r213624  
    4343
    4444    void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final;
     45    void deriveBits(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, unsigned long length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final;
    4546    void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final;
    4647    void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final;
     48
     49    using Callback = Function<void(std::optional<Vector<uint8_t>>&&, unsigned long)>;
     50    void platformDeriveBits(Ref<CryptoKey>&& baseKey, Ref<CryptoKey>&& publicKey, unsigned long length, Callback&&, ScriptExecutionContext&, WorkQueue&);
    4751};
    4852
  • trunk/Source/WebCore/crypto/gnutls/CryptoAlgorithmECDHGnuTLS.cpp

    r213623 r213624  
    2424 */
    2525
    26 #pragma once
    27 
    28 #include "CryptoAlgorithm.h"
     26#include "config.h"
     27#include "CryptoAlgorithmECDH.h"
    2928
    3029#if ENABLE(SUBTLE_CRYPTO)
    3130
     31#include "NotImplemented.h"
     32
    3233namespace WebCore {
    3334
    34 class CryptoAlgorithmECDH final : public CryptoAlgorithm {
    35 public:
    36     static constexpr const char* s_name = "ECDH";
    37     static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::ECDH;
    38     static Ref<CryptoAlgorithm> create();
    39 
    40 private:
    41     CryptoAlgorithmECDH() = default;
    42     CryptoAlgorithmIdentifier identifier() const final;
    43 
    44     void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final;
    45     void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final;
    46     void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final;
    47 };
     35void CryptoAlgorithmECDH::platformDeriveBits(Ref<CryptoKey>&&, Ref<CryptoKey>&&, unsigned long, Callback&&, ScriptExecutionContext&, WorkQueue&)
     36{
     37    notImplemented();
     38}
    4839
    4940} // namespace WebCore
  • trunk/Source/WebCore/crypto/keys/CryptoKeyEC.h

    r213560 r213624  
    8585
    8686    size_t keySizeInBits() const;
     87    NamedCurve namedCurve() const { return m_curve; }
     88    PlatformECKey platformKey() { return m_platformKey; }
    8789
    8890private:
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmECDHMac.cpp

    r213623 r213624  
    2424 */
    2525
    26 #pragma once
    27 
    28 #include "CryptoAlgorithm.h"
     26#include "config.h"
     27#include "CryptoAlgorithmECDH.h"
    2928
    3029#if ENABLE(SUBTLE_CRYPTO)
    3130
     31#include "CommonCryptoUtilities.h"
     32#include "CryptoKeyEC.h"
     33#include "ScriptExecutionContext.h"
     34
    3235namespace WebCore {
    3336
    34 class CryptoAlgorithmECDH final : public CryptoAlgorithm {
    35 public:
    36     static constexpr const char* s_name = "ECDH";
    37     static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::ECDH;
    38     static Ref<CryptoAlgorithm> create();
     37void CryptoAlgorithmECDH::platformDeriveBits(Ref<CryptoKey>&& baseKey, Ref<CryptoKey>&& publicKey, unsigned long length, Callback&& callback, ScriptExecutionContext& context, WorkQueue& workQueue)
     38{
     39    context.ref();
     40    workQueue.dispatch([baseKey = WTFMove(baseKey), publicKey = WTFMove(publicKey), length, callback = WTFMove(callback), &context]() mutable {
     41        auto& ecBaseKey = downcast<CryptoKeyEC>(baseKey.get());
     42        auto& ecPublicKey = downcast<CryptoKeyEC>(publicKey.get());
    3943
    40 private:
    41     CryptoAlgorithmECDH() = default;
    42     CryptoAlgorithmIdentifier identifier() const final;
     44        std::optional<Vector<uint8_t>> result = std::nullopt;
     45        Vector<uint8_t> derivedKey(ecBaseKey.keySizeInBits() / 8); // Per https://tools.ietf.org/html/rfc6090#section-4.
     46        size_t size = derivedKey.size();
     47        if (!CCECCryptorComputeSharedSecret(ecBaseKey.platformKey(), ecPublicKey.platformKey(), derivedKey.data(), &size))
     48            result = WTFMove(derivedKey);
    4349
    44     void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final;
    45     void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final;
    46     void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final;
    47 };
     50        context.postTask([result = WTFMove(result), length, callback = WTFMove(callback)](ScriptExecutionContext& context) mutable {
     51            callback(WTFMove(result), length);
     52            context.deref();
     53        });
     54    });
     55}
    4856
    49 } // namespace WebCore
     57}
    5058
    5159#endif // ENABLE(SUBTLE_CRYPTO)
  • trunk/Source/WebCore/crypto/parameters/CryptoAlgorithmEcdhKeyDeriveParams.h

    r213623 r213624  
    2626#pragma once
    2727
    28 #include "CryptoAlgorithm.h"
     28#include "CryptoAlgorithmParameters.h"
     29#include <wtf/RefCounted.h>
    2930
    3031#if ENABLE(SUBTLE_CRYPTO)
     
    3233namespace WebCore {
    3334
    34 class CryptoAlgorithmECDH final : public CryptoAlgorithm {
     35class CryptoKey;
     36
     37class CryptoAlgorithmEcdhKeyDeriveParams final : public CryptoAlgorithmParameters {
    3538public:
    36     static constexpr const char* s_name = "ECDH";
    37     static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::ECDH;
    38     static Ref<CryptoAlgorithm> create();
     39    RefPtr<CryptoKey> publicKey;
    3940
    40 private:
    41     CryptoAlgorithmECDH() = default;
    42     CryptoAlgorithmIdentifier identifier() const final;
    43 
    44     void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final;
    45     void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final;
    46     void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final;
     41    Class parametersClass() const final { return Class::EcdhKeyDeriveParams; }
    4742};
    4843
    4944} // namespace WebCore
    5045
     46SPECIALIZE_TYPE_TRAITS_CRYPTO_ALGORITHM_PARAMETERS(EcdhKeyDeriveParams)
     47
    5148#endif // ENABLE(SUBTLE_CRYPTO)
  • trunk/Source/WebCore/crypto/parameters/EcdhKeyDeriveParams.idl

    r213623 r213624  
    2424 */
    2525
    26 #pragma once
    27 
    28 #include "CryptoAlgorithm.h"
    29 
    30 #if ENABLE(SUBTLE_CRYPTO)
    31 
    32 namespace WebCore {
    33 
    34 class CryptoAlgorithmECDH final : public CryptoAlgorithm {
    35 public:
    36     static constexpr const char* s_name = "ECDH";
    37     static constexpr CryptoAlgorithmIdentifier s_identifier = CryptoAlgorithmIdentifier::ECDH;
    38     static Ref<CryptoAlgorithm> create();
    39 
    40 private:
    41     CryptoAlgorithmECDH() = default;
    42     CryptoAlgorithmIdentifier identifier() const final;
    43 
    44     void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final;
    45     void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final;
    46     void exportKey(SubtleCrypto::KeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&) final;
     26[
     27    Conditional=SUBTLE_CRYPTO,
     28    ImplementedAs=CryptoAlgorithmEcdhKeyDeriveParams,
     29] dictionary EcdhKeyDeriveParams : CryptoAlgorithmParameters {
     30    // We should rename this to public once https://bugs.webkit.org/show_bug.cgi?id=169333 is fixed.
     31    // The peer's EC public key.
     32    required CryptoKey publicKey;
    4733};
    48 
    49 } // namespace WebCore
    50 
    51 #endif // ENABLE(SUBTLE_CRYPTO)
Note: See TracChangeset for help on using the changeset viewer.