Changeset 209092 in webkit


Ignore:
Timestamp:
Nov 29, 2016 2:52:03 PM (7 years ago)
Author:
jiewen_tan@apple.com
Message:

Update SubtleCrypto::sign to match the latest spec
https://bugs.webkit.org/show_bug.cgi?id=164740
<rdar://problem/29257864>

Reviewed by Brent Fulgham.

LayoutTests/imported/w3c:

  • WebCryptoAPI/idlharness-expected.txt:

Source/WebCore:

This patch does following few things:

  1. It updates the SubtleCrypto::sign method to match the latest spec: https://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-sign. It also refers to the latest Editor's Draft to a certain degree: https://w3c.github.io/webcrypto/Overview.html#SubtleCrypto-method-sign.
  2. It implements sign operations of the following algorithms: HMAC, and RSASSA-PKCS1-V1_5.
  3. It also replaces promise.copyRef() with WTFMove(promise) for all the capture lists of exception callbacks in promise functions.

Tests: crypto/subtle/hmac-import-key-sign-sha1.html

crypto/subtle/hmac-import-key-sign-sha224.html
crypto/subtle/hmac-import-key-sign-sha256.html
crypto/subtle/hmac-import-key-sign-sha384.html
crypto/subtle/hmac-import-key-sign-sha512.html
crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha1.html
crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha224.html
crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha256.html
crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha384.html
crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha512.html
crypto/subtle/sign-malformed-parameters.html
crypto/workers/subtle/hmac-import-key-sign.html
crypto/workers/subtle/rsassa-pkcs1-v1_5-import-key-sign.html

  • bindings/js/JSSubtleCryptoCustom.cpp:

(WebCore::normalizeCryptoAlgorithmParameters):
(WebCore::jsSubtleCryptoFunctionDecryptPromise):
Fix a minor bug.
(WebCore::jsSubtleCryptoFunctionEncryptPromise):
(WebCore::jsSubtleCryptoFunctionDecryptPromise):
(WebCore::jsSubtleCryptoFunctionSignPromise):
(WebCore::jsSubtleCryptoFunctionGenerateKeyPromise):
(WebCore::jsSubtleCryptoFunctionImportKeyPromise):
(WebCore::jsSubtleCryptoFunctionExportKeyPromise):
(WebCore::JSSubtleCrypto::sign):

  • crypto/CryptoAlgorithm.cpp:

(WebCore::CryptoAlgorithm::sign):

  • crypto/CryptoAlgorithm.h:
  • crypto/SubtleCrypto.idl:
  • crypto/algorithms/CryptoAlgorithmHMAC.cpp:

(WebCore::CryptoAlgorithmHMAC::sign):

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

(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::sign):

  • crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h:
  • crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp:

(WebCore::CryptoAlgorithmHMAC::platformSign):

  • crypto/gnutls/CryptoAlgorithmHMACGnuTLS.cpp:

(WebCore::CryptoAlgorithmHMAC::platformSign):

  • crypto/gnutls/CryptoAlgorithmRSASSA_PKCS1_v1_5GnuTLS.cpp:

(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign):

  • crypto/mac/CryptoAlgorithmHMACMac.cpp:

(WebCore::commonCryptoHMACAlgorithm):
(WebCore::calculateSignature):
Remove a null guardance since the depending bug is resolved.
(WebCore::CryptoAlgorithmHMAC::platformSign):
(WebCore::CryptoAlgorithmHMAC::platformVerify):

  • crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp:

(WebCore::signRSASSA_PKCS1_v1_5):
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign):

LayoutTests:

  • crypto/subtle/hmac-import-key-sign-sha1-expected.txt: Added.
  • crypto/subtle/hmac-import-key-sign-sha1.html: Added.
  • crypto/subtle/hmac-import-key-sign-sha224-expected.txt: Added.
  • crypto/subtle/hmac-import-key-sign-sha224.html: Added.
  • crypto/subtle/hmac-import-key-sign-sha256-expected.txt: Added.
  • crypto/subtle/hmac-import-key-sign-sha256.html: Added.
  • crypto/subtle/hmac-import-key-sign-sha384-expected.txt: Added.
  • crypto/subtle/hmac-import-key-sign-sha384.html: Added.
  • crypto/subtle/hmac-import-key-sign-sha512-expected.txt: Added.
  • crypto/subtle/hmac-import-key-sign-sha512.html: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha1-expected.txt: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha1.html: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha224-expected.txt: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha224.html: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha256-expected.txt: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha256.html: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha384-expected.txt: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha384.html: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha512-expected.txt: Added.
  • crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha512.html: Added.
  • crypto/subtle/sign-malformed-parameters-expected.txt: Added.
  • crypto/subtle/sign-malformed-parameters.html: Added.
  • crypto/workers/subtle/hmac-import-key-sign-expected.txt: Added.
  • crypto/workers/subtle/hmac-import-key-sign.html: Added.
  • crypto/workers/subtle/resources/hmac-import-key-sign.js: Added.
  • crypto/workers/subtle/resources/rsassa-pkcs1-v1_5-import-key-sign.js: Added.
  • crypto/workers/subtle/rsassa-pkcs1-v1_5-import-key-sign-expected.txt: Added.
  • crypto/workers/subtle/rsassa-pkcs1-v1_5-import-key-sign.html: Added.
Location:
trunk
Files:
28 added
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r209091 r209092  
     12016-11-22  Jiewen Tan  <jiewen_tan@apple.com>
     2
     3        Update SubtleCrypto::sign to match the latest spec
     4        https://bugs.webkit.org/show_bug.cgi?id=164740
     5        <rdar://problem/29257864>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        * crypto/subtle/hmac-import-key-sign-sha1-expected.txt: Added.
     10        * crypto/subtle/hmac-import-key-sign-sha1.html: Added.
     11        * crypto/subtle/hmac-import-key-sign-sha224-expected.txt: Added.
     12        * crypto/subtle/hmac-import-key-sign-sha224.html: Added.
     13        * crypto/subtle/hmac-import-key-sign-sha256-expected.txt: Added.
     14        * crypto/subtle/hmac-import-key-sign-sha256.html: Added.
     15        * crypto/subtle/hmac-import-key-sign-sha384-expected.txt: Added.
     16        * crypto/subtle/hmac-import-key-sign-sha384.html: Added.
     17        * crypto/subtle/hmac-import-key-sign-sha512-expected.txt: Added.
     18        * crypto/subtle/hmac-import-key-sign-sha512.html: Added.
     19        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha1-expected.txt: Added.
     20        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha1.html: Added.
     21        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha224-expected.txt: Added.
     22        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha224.html: Added.
     23        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha256-expected.txt: Added.
     24        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha256.html: Added.
     25        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha384-expected.txt: Added.
     26        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha384.html: Added.
     27        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha512-expected.txt: Added.
     28        * crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha512.html: Added.
     29        * crypto/subtle/sign-malformed-parameters-expected.txt: Added.
     30        * crypto/subtle/sign-malformed-parameters.html: Added.
     31        * crypto/workers/subtle/hmac-import-key-sign-expected.txt: Added.
     32        * crypto/workers/subtle/hmac-import-key-sign.html: Added.
     33        * crypto/workers/subtle/resources/hmac-import-key-sign.js: Added.
     34        * crypto/workers/subtle/resources/rsassa-pkcs1-v1_5-import-key-sign.js: Added.
     35        * crypto/workers/subtle/rsassa-pkcs1-v1_5-import-key-sign-expected.txt: Added.
     36        * crypto/workers/subtle/rsassa-pkcs1-v1_5-import-key-sign.html: Added.
     37
    1382016-11-29  Chris Dumez  <cdumez@apple.com>
    239
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r208918 r209092  
     12016-11-22  Jiewen Tan  <jiewen_tan@apple.com>
     2
     3        Update SubtleCrypto::sign to match the latest spec
     4        https://bugs.webkit.org/show_bug.cgi?id=164740
     5        <rdar://problem/29257864>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        * WebCryptoAPI/idlharness-expected.txt:
     10
    1112016-11-18  Jiewen Tan  <jiewen_tan@apple.com>
    212
  • trunk/LayoutTests/imported/w3c/WebCryptoAPI/idlharness-expected.txt

    r208918 r209092  
    4848PASS SubtleCrypto interface: crypto.subtle must inherit property "decrypt" with the proper type (1)
    4949PASS SubtleCrypto interface: calling decrypt(AlgorithmIdentifier,CryptoKey,BufferSource) on crypto.subtle with too few arguments must throw TypeError
    50 FAIL SubtleCrypto interface: crypto.subtle must inherit property "sign" with the proper type (2) assert_inherits: property "sign" not found in prototype chain
    51 FAIL SubtleCrypto interface: calling sign(AlgorithmIdentifier,CryptoKey,BufferSource) on crypto.subtle with too few arguments must throw TypeError assert_inherits: property "sign" not found in prototype chain
     50PASS SubtleCrypto interface: crypto.subtle must inherit property "sign" with the proper type (2)
     51PASS SubtleCrypto interface: calling sign(AlgorithmIdentifier,CryptoKey,BufferSource) on crypto.subtle with too few arguments must throw TypeError
    5252FAIL SubtleCrypto interface: crypto.subtle must inherit property "verify" with the proper type (3) assert_inherits: property "verify" not found in prototype chain
    5353FAIL SubtleCrypto interface: calling verify(AlgorithmIdentifier,CryptoKey,BufferSource,BufferSource) on crypto.subtle with too few arguments must throw TypeError assert_inherits: property "verify" not found in prototype chain
  • trunk/Source/WebCore/ChangeLog

    r209091 r209092  
     12016-11-22  Jiewen Tan  <jiewen_tan@apple.com>
     2
     3        Update SubtleCrypto::sign to match the latest spec
     4        https://bugs.webkit.org/show_bug.cgi?id=164740
     5        <rdar://problem/29257864>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        This patch does following few things:
     10        1. It updates the SubtleCrypto::sign method to match the latest spec:
     11           https://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-sign.
     12           It also refers to the latest Editor's Draft to a certain degree:
     13           https://w3c.github.io/webcrypto/Overview.html#SubtleCrypto-method-sign.
     14        2. It implements sign operations of the following algorithms: HMAC, and
     15           RSASSA-PKCS1-V1_5.
     16        3. It also replaces promise.copyRef() with WTFMove(promise) for all the
     17           capture lists of exception callbacks in promise functions.
     18
     19        Tests: crypto/subtle/hmac-import-key-sign-sha1.html
     20               crypto/subtle/hmac-import-key-sign-sha224.html
     21               crypto/subtle/hmac-import-key-sign-sha256.html
     22               crypto/subtle/hmac-import-key-sign-sha384.html
     23               crypto/subtle/hmac-import-key-sign-sha512.html
     24               crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha1.html
     25               crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha224.html
     26               crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha256.html
     27               crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha384.html
     28               crypto/subtle/rsassa-pkcs1-v1_5-import-key-sign-sha512.html
     29               crypto/subtle/sign-malformed-parameters.html
     30               crypto/workers/subtle/hmac-import-key-sign.html
     31               crypto/workers/subtle/rsassa-pkcs1-v1_5-import-key-sign.html
     32
     33        * bindings/js/JSSubtleCryptoCustom.cpp:
     34        (WebCore::normalizeCryptoAlgorithmParameters):
     35        (WebCore::jsSubtleCryptoFunctionDecryptPromise):
     36        Fix a minor bug.
     37        (WebCore::jsSubtleCryptoFunctionEncryptPromise):
     38        (WebCore::jsSubtleCryptoFunctionDecryptPromise):
     39        (WebCore::jsSubtleCryptoFunctionSignPromise):
     40        (WebCore::jsSubtleCryptoFunctionGenerateKeyPromise):
     41        (WebCore::jsSubtleCryptoFunctionImportKeyPromise):
     42        (WebCore::jsSubtleCryptoFunctionExportKeyPromise):
     43        (WebCore::JSSubtleCrypto::sign):
     44        * crypto/CryptoAlgorithm.cpp:
     45        (WebCore::CryptoAlgorithm::sign):
     46        * crypto/CryptoAlgorithm.h:
     47        * crypto/SubtleCrypto.idl:
     48        * crypto/algorithms/CryptoAlgorithmHMAC.cpp:
     49        (WebCore::CryptoAlgorithmHMAC::sign):
     50        * crypto/algorithms/CryptoAlgorithmHMAC.h:
     51        * crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp:
     52        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::sign):
     53        * crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h:
     54        * crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp:
     55        (WebCore::CryptoAlgorithmHMAC::platformSign):
     56        * crypto/gnutls/CryptoAlgorithmHMACGnuTLS.cpp:
     57        (WebCore::CryptoAlgorithmHMAC::platformSign):
     58        * crypto/gnutls/CryptoAlgorithmRSASSA_PKCS1_v1_5GnuTLS.cpp:
     59        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign):
     60        * crypto/mac/CryptoAlgorithmHMACMac.cpp:
     61        (WebCore::commonCryptoHMACAlgorithm):
     62        (WebCore::calculateSignature):
     63        Remove a null guardance since the depending bug is resolved.
     64        (WebCore::CryptoAlgorithmHMAC::platformSign):
     65        (WebCore::CryptoAlgorithmHMAC::platformVerify):
     66        * crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp:
     67        (WebCore::signRSASSA_PKCS1_v1_5):
     68        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign):
     69
    1702016-11-29  Chris Dumez  <cdumez@apple.com>
    271
  • trunk/Source/WebCore/bindings/js/JSSubtleCryptoCustom.cpp

    r209077 r209092  
    5555    Encrypt,
    5656    Decrypt,
     57    Sign,
    5758    Digest,
    5859    GenerateKey,
     
    113114                break;
    114115            }
     116            default:
     117                setDOMException(&state, NOT_SUPPORTED_ERR);
     118                return nullptr;
     119            }
     120            break;
     121        case Operations::Sign:
     122            switch (*identifier) {
     123            case CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5:
     124            case CryptoAlgorithmIdentifier::HMAC:
     125                result = std::make_unique<CryptoAlgorithmParameters>(params);
     126                break;
    115127            default:
    116128                setDOMException(&state, NOT_SUPPORTED_ERR);
     
    453465        return;
    454466    };
    455     auto exceptionCallback = [capturedPromise = promise.copyRef()](ExceptionCode ec) mutable {
     467    auto exceptionCallback = [capturedPromise = WTFMove(promise)](ExceptionCode ec) mutable {
    456468        rejectWithException(WTFMove(capturedPromise), ec);
    457469    };
     
    494506    RETURN_IF_EXCEPTION(scope, void());
    495507
    496     auto callback = [capturedPromise = promise.copyRef()](const Vector<uint8_t>& cipherText) mutable {
    497         fulfillPromiseWithArrayBuffer(WTFMove(capturedPromise), cipherText.data(), cipherText.size());
    498         return;
    499     };
    500     auto exceptionCallback = [capturedPromise = promise.copyRef()](ExceptionCode ec) mutable {
     508    auto callback = [capturedPromise = promise.copyRef()](const Vector<uint8_t>& plainText) mutable {
     509        fulfillPromiseWithArrayBuffer(WTFMove(capturedPromise), plainText.data(), plainText.size());
     510        return;
     511    };
     512    auto exceptionCallback = [capturedPromise = WTFMove(promise)](ExceptionCode ec) mutable {
    501513        rejectWithException(WTFMove(capturedPromise), ec);
    502514    };
     
    505517    ASSERT(subtle);
    506518    algorithm->decrypt(WTFMove(params), key.releaseNonNull(), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), *scriptExecutionContextFromExecState(&state), subtle->wrapped().workQueue());
     519}
     520
     521static void jsSubtleCryptoFunctionSignPromise(ExecState& state, Ref<DeferredPromise>&& promise)
     522{
     523    VM& vm = state.vm();
     524    auto scope = DECLARE_THROW_SCOPE(vm);
     525
     526    if (UNLIKELY(state.argumentCount() < 3)) {
     527        promise->reject<JSValue>(createNotEnoughArgumentsError(&state));
     528        return;
     529    }
     530
     531    auto params = normalizeCryptoAlgorithmParameters(state, state.uncheckedArgument(0), Operations::Sign);
     532    RETURN_IF_EXCEPTION(scope, void());
     533
     534    auto key = toCryptoKey(state, state.uncheckedArgument(1));
     535    RETURN_IF_EXCEPTION(scope, void());
     536
     537    auto data = toVector(state, state.uncheckedArgument(2));
     538    RETURN_IF_EXCEPTION(scope, void());
     539
     540    if (params->identifier != key->algorithmIdentifier()) {
     541        promise->reject(INVALID_ACCESS_ERR, ASCIILiteral("CryptoKey doesn't match AlgorithmIdentifier"));
     542        return;
     543    }
     544
     545    if (!key->allows(CryptoKeyUsageSign)) {
     546        promise->reject(INVALID_ACCESS_ERR, ASCIILiteral("CryptoKey doesn't support signing"));
     547        return;
     548    }
     549
     550    auto algorithm = createAlgorithm(state, key->algorithmIdentifier());
     551    RETURN_IF_EXCEPTION(scope, void());
     552
     553    auto callback = [capturedPromise = promise.copyRef()](const Vector<uint8_t>& signature) mutable {
     554        fulfillPromiseWithArrayBuffer(WTFMove(capturedPromise), signature.data(), signature.size());
     555        return;
     556    };
     557    auto exceptionCallback = [capturedPromise = WTFMove(promise)](ExceptionCode ec) mutable {
     558        rejectWithException(WTFMove(capturedPromise), ec);
     559    };
     560
     561    JSSubtleCrypto* subtle = jsDynamicDowncast<JSSubtleCrypto*>(state.thisValue());
     562    ASSERT(subtle);
     563    algorithm->sign(key.releaseNonNull(), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), *scriptExecutionContextFromExecState(&state), subtle->wrapped().workQueue());
    507564}
    508565
     
    546603        }
    547604    };
    548     auto exceptionCallback = [capturedPromise =  promise.copyRef()](ExceptionCode ec) mutable {
     605    auto exceptionCallback = [capturedPromise = WTFMove(promise)](ExceptionCode ec) mutable {
    549606        rejectWithException(WTFMove(capturedPromise), ec);
    550607    };
     
    591648        capturedPromise->resolve(key);
    592649    };
    593     auto exceptionCallback = [capturedPromise =  promise.copyRef()](ExceptionCode ec) mutable {
     650    auto exceptionCallback = [capturedPromise = WTFMove(promise)](ExceptionCode ec) mutable {
    594651        rejectWithException(WTFMove(capturedPromise), ec);
    595652    };
     
    658715        ASSERT_NOT_REACHED();
    659716    };
    660     auto exceptionCallback = [capturedPromise =  promise.copyRef()](ExceptionCode ec) mutable {
     717    auto exceptionCallback = [capturedPromise = WTFMove(promise)](ExceptionCode ec) mutable {
    661718        rejectWithException(WTFMove(capturedPromise), ec);
    662719    };
     
    678735}
    679736
     737JSValue JSSubtleCrypto::sign(ExecState& state)
     738{
     739    return callPromiseFunction<jsSubtleCryptoFunctionSignPromise, PromiseExecutionScope::WindowOrWorker>(state);
     740}
     741
    680742JSValue JSSubtleCrypto::generateKey(ExecState& state)
    681743{
  • trunk/Source/WebCore/crypto/CryptoAlgorithm.cpp

    r209077 r209092  
    3939
    4040void CryptoAlgorithm::decrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&, WorkQueue&)
     41{
     42    exceptionCallback(NOT_SUPPORTED_ERR);
     43}
     44
     45void CryptoAlgorithm::sign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&, WorkQueue&)
    4146{
    4247    exceptionCallback(NOT_SUPPORTED_ERR);
  • trunk/Source/WebCore/crypto/CryptoAlgorithm.h

    r209077 r209092  
    6767    virtual void encrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
    6868    virtual void decrypt(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
     69    virtual void sign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
    6970    virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&);
    7071    virtual void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&);
  • trunk/Source/WebCore/crypto/SubtleCrypto.idl

    r208918 r209092  
    3434    [Custom] Promise<any> encrypt(AlgorithmIdentifier algorithm, CryptoKey key, BufferSource data);
    3535    [Custom] Promise<any> decrypt(AlgorithmIdentifier algorithm, CryptoKey key, BufferSource data);
     36    [Custom] Promise<any> sign(AlgorithmIdentifier algorithm, CryptoKey key, BufferSource data);
    3637    // FIXME: Should this return a Promise<(CryptoKey or CryptoKeyPair)>?
    3738    [Custom] Promise<any> generateKey(AlgorithmIdentifier algorithm, boolean extractable, sequence<CryptoKeyUsage> keyUsages);
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.cpp

    r209077 r209092  
    7070}
    7171
     72void CryptoAlgorithmHMAC::sign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
     73{
     74    platformSign(WTFMove(key), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue);
     75}
     76
    7277void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&)
    7378{
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.h

    r209077 r209092  
    4545    CryptoAlgorithmIdentifier identifier() const final;
    4646
     47    void sign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final;
    4748    void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final;
    4849    void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final;
     
    5657
    5758    bool keyAlgorithmMatches(const CryptoAlgorithmHmacParamsDeprecated& algorithmParameters, const CryptoKey&) const;
     59    void platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
    5860    ExceptionOr<void> platformSign(const CryptoAlgorithmHmacParamsDeprecated&, const CryptoKeyHMAC&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback);
    5961    ExceptionOr<void> platformVerify(const CryptoAlgorithmHmacParamsDeprecated&, const CryptoKeyHMAC&, const CryptoOperationData& signature, const CryptoOperationData&, BoolCallback&&, VoidCallback&& failureCallback);
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.cpp

    r209077 r209092  
    6969}
    7070
     71void CryptoAlgorithmRSASSA_PKCS1_v1_5::sign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
     72{
     73    if (key->type() != CryptoKeyType::Private) {
     74        exceptionCallback(INVALID_ACCESS_ERR);
     75        return;
     76    }
     77    platformSign(WTFMove(key), WTFMove(data), WTFMove(callback), WTFMove(exceptionCallback), context, workQueue);
     78}
     79
    7180void CryptoAlgorithmRSASSA_PKCS1_v1_5::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context)
    7281{
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmRSASSA_PKCS1_v1_5.h

    r209077 r209092  
    4545    CryptoAlgorithmIdentifier identifier() const final;
    4646
     47    void sign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final;
    4748    void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final;
    4849    void importKey(SubtleCrypto::KeyFormat, KeyData&&, const std::unique_ptr<CryptoAlgorithmParameters>&&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&) final;
     
    5556
    5657    bool keyAlgorithmMatches(const CryptoAlgorithmRsaSsaParamsDeprecated& algorithmParameters, const CryptoKey&) const;
     58    void platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
    5759    ExceptionOr<void> platformSign(const CryptoAlgorithmRsaSsaParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&& failureCallback);
    5860    ExceptionOr<void> platformVerify(const CryptoAlgorithmRsaSsaParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData& signature, const CryptoOperationData&, BoolCallback&&, VoidCallback&& failureCallback);
  • trunk/Source/WebCore/crypto/gcrypt/CryptoAlgorithmHMACGCrypt.cpp

    r208985 r209092  
    3333#include "CryptoKeyHMAC.h"
    3434#include "ExceptionCode.h"
     35#include "NotImplemented.h"
    3536#include <gcrypt.h>
    3637#include <wtf/CryptographicUtilities.h>
     
    9798}
    9899
     100void CryptoAlgorithmHMAC::platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&)
     101{
     102    notImplemented();
     103}
     104
    99105ExceptionOr<void> CryptoAlgorithmHMAC::platformSign(const CryptoAlgorithmHmacParamsDeprecated& parameters, const CryptoKeyHMAC& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback)
    100106{
  • trunk/Source/WebCore/crypto/gnutls/CryptoAlgorithmHMACGnuTLS.cpp

    r208669 r209092  
    3232#include "CryptoKeyHMAC.h"
    3333#include "ExceptionCode.h"
     34#include "NotImplemented.h"
    3435#include <gnutls/gnutls.h>
    3536#include <gnutls/crypto.h>
     
    6970}
    7071
     72void CryptoAlgorithmHMAC::platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&)
     73{
     74    notImplemented();
     75}
     76
    7177ExceptionOr<void> CryptoAlgorithmHMAC::platformSign(const CryptoAlgorithmHmacParamsDeprecated& parameters, const CryptoKeyHMAC& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&&)
    7278{
  • trunk/Source/WebCore/crypto/gnutls/CryptoAlgorithmRSASSA_PKCS1_v1_5GnuTLS.cpp

    r208669 r209092  
    3636namespace WebCore {
    3737
     38void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&)
     39{
     40    notImplemented();
     41}
     42
    3843ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaParamsDeprecated&, const CryptoKeyRSA&, const CryptoOperationData&, VectorCallback&&, VoidCallback&&)
    3944{
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmHMACMac.cpp

    r208985 r209092  
    3232#include "CryptoKeyHMAC.h"
    3333#include "ExceptionCode.h"
     34#include "ScriptExecutionContext.h"
    3435#include <CommonCrypto/CommonHMAC.h>
    3536#include <wtf/CryptographicUtilities.h>
     
    5556}
    5657
    57 static Vector<uint8_t> calculateSignature(CCHmacAlgorithm algorithm, const Vector<uint8_t>& key, const CryptoOperationData& data)
     58// FIXME: We should change data to Vector<uint8_t> type once WebKitSubtleCrypto is deprecated.
     59// https://bugs.webkit.org/show_bug.cgi?id=164939
     60static Vector<uint8_t> calculateSignature(CCHmacAlgorithm algorithm, const Vector<uint8_t>& key, const uint8_t* data, size_t dataLength)
    5861{
    5962    size_t digestLength;
     
    8083
    8184    Vector<uint8_t> result(digestLength);
    82     const void* keyData = key.data() ? key.data() : reinterpret_cast<const uint8_t*>(""); // <rdar://problem/15467425> HMAC crashes when key pointer is null.
    83     CCHmac(algorithm, keyData, key.size(), data.first, data.second, result.data());
     85    CCHmac(algorithm, key.data(), key.size(), data, dataLength, result.data());
    8486    return result;
     87}
     88
     89void CryptoAlgorithmHMAC::platformSign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
     90{
     91    context.ref();
     92    workQueue.dispatch([key = WTFMove(key), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
     93        auto& hmacKey = downcast<CryptoKeyHMAC>(key.get());
     94        auto algorithm = commonCryptoHMACAlgorithm(hmacKey.hashAlgorithmIdentifier());
     95        if (!algorithm) {
     96            // We should only dereference callbacks after being back to the Document/Worker threads.
     97            context.postTask([exceptionCallback = WTFMove(exceptionCallback), callback = WTFMove(callback)](ScriptExecutionContext& context) {
     98                exceptionCallback(OperationError);
     99                context.deref();
     100            });
     101            return;
     102        }
     103        auto result = calculateSignature(*algorithm, hmacKey.key(), data.data(), data.size());
     104        // We should only dereference callbacks after being back to the Document/Worker threads.
     105        context.postTask([callback = WTFMove(callback), result = WTFMove(result), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
     106            callback(result);
     107            context.deref();
     108        });
     109    });
     110
    85111}
    86112
     
    90116    if (!algorithm)
    91117        return Exception { NOT_SUPPORTED_ERR };
    92     callback(calculateSignature(*algorithm, key.key(), data));
     118    callback(calculateSignature(*algorithm, key.key(), data.first, data.second));
    93119    return { };
    94120}
     
    100126        return Exception { NOT_SUPPORTED_ERR };
    101127
    102     auto signature = calculateSignature(*algorithm, key.key(), data);
     128    auto signature = calculateSignature(*algorithm, key.key(), data.first, data.second);
    103129
    104130    // Using a constant time comparison to prevent timing attacks.
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp

    r208985 r209092  
    3434#include "CryptoKeyRSA.h"
    3535#include "ExceptionCode.h"
     36#include "ScriptExecutionContext.h"
    3637
    3738namespace WebCore {
     
    5556}
    5657
     58// FIXME: We should change data to Vector<uint8_t> type once WebKitSubtleCrypto is deprecated.
     59// https://bugs.webkit.org/show_bug.cgi?id=164939
     60static ExceptionOr<Vector<uint8_t>> signRSASSA_PKCS1_v1_5(CryptoAlgorithmIdentifier hash, const PlatformRSAKey key, size_t keyLength, const uint8_t* data, size_t dataLength)
     61{
     62    CCDigestAlgorithm digestAlgorithm;
     63    if (!getCommonCryptoDigestAlgorithm(hash, digestAlgorithm))
     64        return Exception { OperationError };
     65
     66    auto cryptoDigestAlgorithm = WebCore::cryptoDigestAlgorithm(hash);
     67    if (!cryptoDigestAlgorithm)
     68        return Exception { OperationError };
     69    auto digest = CryptoDigest::create(*cryptoDigestAlgorithm);
     70    if (!digest)
     71        return Exception { OperationError };
     72    digest->addBytes(data, dataLength);
     73    auto digestData = digest->computeHash();
     74
     75    Vector<uint8_t> signature(keyLength / 8); // Per https://tools.ietf.org/html/rfc3447#section-8.2.1
     76    size_t signatureSize = signature.size();
     77
     78    CCCryptorStatus status = CCRSACryptorSign(key, ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
     79    if (status)
     80        return Exception { OperationError };
     81
     82    return WTFMove(signature);
     83}
     84
     85void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(Ref<CryptoKey>&& key, Vector<uint8_t>&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
     86{
     87    context.ref();
     88    workQueue.dispatch([key = WTFMove(key), data = WTFMove(data), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
     89        auto& rsaKey = downcast<CryptoKeyRSA>(key.get());
     90        auto result = signRSASSA_PKCS1_v1_5(rsaKey.hashAlgorithmIdentifier(), rsaKey.platformKey(), rsaKey.keySizeInBits(), data.data(), data.size());
     91        if (result.hasException()) {
     92            // We should only dereference callbacks after being back to the Document/Worker threads.
     93            context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
     94                exceptionCallback(ec);
     95                context.deref();
     96            });
     97            return;
     98        }
     99        // We should only dereference callbacks after being back to the Document/Worker threads.
     100        context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
     101            callback(result);
     102            context.deref();
     103        });
     104    });
     105}
     106
    57107ExceptionOr<void> CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaParamsDeprecated& parameters, const CryptoKeyRSA& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback)
    58108{
    59     CCDigestAlgorithm digestAlgorithm;
    60     if (!getCommonCryptoDigestAlgorithm(parameters.hash, digestAlgorithm))
    61         return Exception { NOT_SUPPORTED_ERR };
    62 
    63     auto cryptoDigestAlgorithm = WebCore::cryptoDigestAlgorithm(parameters.hash);
    64     if (!cryptoDigestAlgorithm)
    65         return Exception { NOT_SUPPORTED_ERR };
    66 
    67     auto digest = CryptoDigest::create(*cryptoDigestAlgorithm);
    68     if (!digest)
    69         return Exception { NOT_SUPPORTED_ERR };
    70 
    71     digest->addBytes(data.first, data.second);
    72 
    73     auto digestData = digest->computeHash();
    74 
    75     Vector<uint8_t> signature(512);
    76     size_t signatureSize = signature.size();
    77 
    78     CCCryptorStatus status = CCRSACryptorSign(key.platformKey(), ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
    79     if (status) {
     109    auto result = signRSASSA_PKCS1_v1_5(parameters.hash, key.platformKey(), key.keySizeInBits(), data.first, data.second);
     110    if (result.hasException()) {
    80111        failureCallback();
    81112        return { };
    82113    }
    83 
    84     signature.resize(signatureSize);
    85     callback(signature);
     114    callback(result.releaseReturnValue());
    86115    return { };
    87116}
Note: See TracChangeset for help on using the changeset viewer.