Changeset 158387 in webkit


Ignore:
Timestamp:
Oct 31, 2013, 12:31:43 PM (12 years ago)
Author:
ap@apple.com
Message:

[WebCrypto] Add SHA-1
https://bugs.webkit.org/show_bug.cgi?id=123582

Reviewed by Anders Carlsson.

Source/WebCore:

Tests: security/crypto-subtle-arguments.html

security/crypto-subtle-sha1.html

  • WebCore.xcodeproj/project.pbxproj: Added new files.
  • bindings/js/JSSubtleCryptoCustom.cpp:

(WebCore::createAlgorithmFromJSValue):
(WebCore::sequenceOfCryptoOperationDataFromJSValue):
(WebCore::JSSubtleCrypto::digest):

  • crypto/SubtleCrypto.idl:

Added bindings for crypto.digest.

  • crypto/algorithms: Added.
  • crypto/algorithms/CryptoAlgorithmSHA1.cpp: Added.
  • crypto/algorithms/CryptoAlgorithmSHA1.h: Added.
  • crypto/mac/CryptoAlgorithmRegistryMac.cpp:

(WebCore::CryptoAlgorithmRegistry::platformRegisterAlgorithms): Register SHA-1.

  • crypto/mac/CryptoAlgorithmSHA1Mac.cpp: Added.

(WebCore::CryptoAlgorithmSHA1::digest): Performs the work synchronously, because
otherwise we'd have to copy the data first, which is crazy for something as simple
as hashing. We can change to a dispatch queue later if we find that it's actually
better to copy and do the work asynchronously.

LayoutTests:

  • security/resources/common.js: Added from Blink.

(importTestKeys):
(asciiToArrayBuffer):
(printRejectedResult):
(printAcceptedResult):
(failAndFinishJSTest):

  • security/crypto-subtle-arguments-expected.txt: Added.
  • security/crypto-subtle-arguments.html: Added.

Added tests for how bindings treat crazy arguments. Heavily based on a test from Blink.

  • security/crypto-subtle-sha1-expected.txt: Added.
  • security/crypto-subtle-sha1.html: Added.

Test that SHA-1 works.

Location:
trunk
Files:
9 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r158384 r158387  
     12013-10-31  Alexey Proskuryakov  <ap@apple.com>
     2
     3        [WebCrypto] Add SHA-1
     4        https://bugs.webkit.org/show_bug.cgi?id=123582
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * security/resources/common.js: Added from Blink.
     9        (importTestKeys):
     10        (asciiToArrayBuffer):
     11        (printRejectedResult):
     12        (printAcceptedResult):
     13        (failAndFinishJSTest):
     14
     15        * security/crypto-subtle-arguments-expected.txt: Added.
     16        * security/crypto-subtle-arguments.html: Added.
     17        Added tests for how bindings treat crazy arguments. Heavily based on a test from Blink.
     18
     19        * security/crypto-subtle-sha1-expected.txt: Added.
     20        * security/crypto-subtle-sha1.html: Added.
     21        Test that SHA-1 works.
     22
     23        * TestExpectations: Skip these tests everywhere for now.
     24
    1252013-10-31  Filip Pizlo  <fpizlo@apple.com>
    226
  • trunk/LayoutTests/TestExpectations

    r158362 r158387  
    7676webkit.org/b/122679 security/crypto-subtle-gc-2.html [ Skip ]
    7777webkit.org/b/122679 security/crypto-subtle-gc-3.html [ Skip ]
     78webkit.org/b/122679 security/crypto-subtle-sha1.html [ Skip ]
     79webkit.org/b/122679 security/crypto-subtle-arguments.html [ Skip ]
    7880
    7981webkit.org/b/123555 [ Debug ] media/media-fragments/TC0054.html [ Crash ]
  • trunk/Source/WebCore/ChangeLog

    r158386 r158387  
     12013-10-31  Alexey Proskuryakov  <ap@apple.com>
     2
     3        [WebCrypto] Add SHA-1
     4        https://bugs.webkit.org/show_bug.cgi?id=123582
     5
     6        Reviewed by Anders Carlsson.
     7
     8        Tests: security/crypto-subtle-arguments.html
     9               security/crypto-subtle-sha1.html
     10
     11        * WebCore.xcodeproj/project.pbxproj: Added new files.
     12
     13        * bindings/js/JSSubtleCryptoCustom.cpp:
     14        (WebCore::createAlgorithmFromJSValue):
     15        (WebCore::sequenceOfCryptoOperationDataFromJSValue):
     16        (WebCore::JSSubtleCrypto::digest):
     17        * crypto/SubtleCrypto.idl:
     18        Added bindings for crypto.digest.
     19
     20        * crypto/algorithms: Added.
     21        * crypto/algorithms/CryptoAlgorithmSHA1.cpp: Added.
     22        * crypto/algorithms/CryptoAlgorithmSHA1.h: Added.
     23        * crypto/mac/CryptoAlgorithmRegistryMac.cpp:
     24        (WebCore::CryptoAlgorithmRegistry::platformRegisterAlgorithms): Register SHA-1.
     25
     26        * crypto/mac/CryptoAlgorithmSHA1Mac.cpp: Added.
     27        (WebCore::CryptoAlgorithmSHA1::digest): Performs the work synchronously, because
     28        otherwise we'd have to copy the data first, which is crazy for something as simple
     29        as hashing. We can change to a dispatch queue later if we find that it's actually
     30        better to copy and do the work asynchronously.
     31
    1322013-10-31  Sudarsana Nagineni  <sudarsana.nagineni@intel.com>
    233
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r158367 r158387  
    55035503                E11C9DB00EB3699500E409DB /* ScriptExecutionContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E11C9DAF0EB3699500E409DB /* ScriptExecutionContext.cpp */; };
    55045504                E124748410AA161D00B79493 /* AuthenticationClient.h in Headers */ = {isa = PBXBuildFile; fileRef = E124748310AA161D00B79493 /* AuthenticationClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
     5505                E125F82B1822CFEC00D84CD9 /* CryptoAlgorithmSHA1.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E125F8291822CFEC00D84CD9 /* CryptoAlgorithmSHA1.cpp */; };
     5506                E125F82C1822CFEC00D84CD9 /* CryptoAlgorithmSHA1.h in Headers */ = {isa = PBXBuildFile; fileRef = E125F82A1822CFEC00D84CD9 /* CryptoAlgorithmSHA1.h */; };
     5507                E125F82E1822CFFF00D84CD9 /* CryptoAlgorithmSHA1Mac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E125F82D1822CFFF00D84CD9 /* CryptoAlgorithmSHA1Mac.cpp */; };
    55055508                E12719C70EEEC16800F61213 /* NavigatorBase.h in Headers */ = {isa = PBXBuildFile; fileRef = E12719C60EEEC16800F61213 /* NavigatorBase.h */; };
    55065509                E12719CA0EEEC21300F61213 /* NavigatorBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E12719C90EEEC21300F61213 /* NavigatorBase.cpp */; };
     
    1250712510                E11C9DAF0EB3699500E409DB /* ScriptExecutionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScriptExecutionContext.cpp; sourceTree = "<group>"; };
    1250812511                E124748310AA161D00B79493 /* AuthenticationClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AuthenticationClient.h; sourceTree = "<group>"; };
     12512                E125F8291822CFEC00D84CD9 /* CryptoAlgorithmSHA1.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoAlgorithmSHA1.cpp; sourceTree = "<group>"; };
     12513                E125F82A1822CFEC00D84CD9 /* CryptoAlgorithmSHA1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoAlgorithmSHA1.h; sourceTree = "<group>"; };
     12514                E125F82D1822CFFF00D84CD9 /* CryptoAlgorithmSHA1Mac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CryptoAlgorithmSHA1Mac.cpp; path = mac/CryptoAlgorithmSHA1Mac.cpp; sourceTree = "<group>"; };
    1250912515                E12719C60EEEC16800F61213 /* NavigatorBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NavigatorBase.h; sourceTree = "<group>"; };
    1251012516                E12719C90EEEC21300F61213 /* NavigatorBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NavigatorBase.cpp; sourceTree = "<group>"; };
     
    2013320139                        children = (
    2013420140                                E1BB84AC1822CA7400525043 /* CryptoAlgorithmRegistryMac.cpp */,
     20141                                E125F82D1822CFFF00D84CD9 /* CryptoAlgorithmSHA1Mac.cpp */,
    2013520142                        );
    2013620143                        name = mac;
     
    2014020147                        isa = PBXGroup;
    2014120148                        children = (
     20149                                E125F8291822CFEC00D84CD9 /* CryptoAlgorithmSHA1.cpp */,
     20150                                E125F82A1822CFEC00D84CD9 /* CryptoAlgorithmSHA1.h */,
    2014220151                        );
    2014320152                        path = algorithms;
     
    2360523614                                4E1959220A39DABA00220FE5 /* MediaFeatureNames.h in Headers */,
    2360623615                                07A6D1EC1491137700051D0C /* MediaFragmentURIParser.h in Headers */,
     23616                                E125F82C1822CFEC00D84CD9 /* CryptoAlgorithmSHA1.h in Headers */,
    2360723617                                A8EA800E0A19516E00A8EF5F /* MediaList.h in Headers */,
    2360823618                                E44613E40CD681A200FADA75 /* MediaPlayer.h in Headers */,
     
    2566725677                                85183B470AA6926100F19FA3 /* DOMHTMLParagraphElement.mm in Sources */,
    2566825678                                85ECBEF80AA7626900544F0B /* DOMHTMLParamElement.mm in Sources */,
     25679                                E125F82B1822CFEC00D84CD9 /* CryptoAlgorithmSHA1.cpp in Sources */,
    2566925680                                85183B490AA6926100F19FA3 /* DOMHTMLPreElement.mm in Sources */,
    2567025681                                A4226E961163D73A008B8397 /* DOMHTMLProgressElement.mm in Sources */,
     
    2605826069                                A81369D7097374F600D74463 /* HTMLTextAreaElement.cpp in Sources */,
    2605926070                                9BC6C21C13CCC97B008E0337 /* HTMLTextFormControlElement.cpp in Sources */,
     26071                                E125F82E1822CFFF00D84CD9 /* CryptoAlgorithmSHA1Mac.cpp in Sources */,
    2606026072                                A871DC290A15205700B12A68 /* HTMLTitleElement.cpp in Sources */,
    2606126073                                977B3877122883E900B81FF8 /* HTMLTokenizer.cpp in Sources */,
  • trunk/Source/WebCore/bindings/js/JSSubtleCryptoCustom.cpp

    r157430 r158387  
    2929#if ENABLE(SUBTLE_CRYPTO)
    3030
     31#include "CryptoAlgorithm.h"
     32#include "CryptoAlgorithmParameters.h"
     33#include "CryptoAlgorithmRegistry.h"
     34#include "Document.h"
     35#include "ExceptionCode.h"
     36#include "JSCryptoAlgorithmDictionary.h"
     37#include "JSDOMPromise.h"
     38#include <runtime/Error.h>
     39
     40using namespace JSC;
     41
    3142namespace WebCore {
    3243
     44static std::unique_ptr<CryptoAlgorithm> createAlgorithmFromJSValue(ExecState* exec, JSValue value)
     45{
     46    CryptoAlgorithmIdentifier algorithmIdentifier;
     47    if (!JSCryptoAlgorithmDictionary::getAlgorithmIdentifier(exec, value, algorithmIdentifier)) {
     48        ASSERT(exec->hadException());
     49        return nullptr;
     50    }
    3351
    34 
     52    std::unique_ptr<CryptoAlgorithm> result(CryptoAlgorithmRegistry::shared().create(algorithmIdentifier));
     53    if (!result)
     54        setDOMException(exec, NOT_SUPPORTED_ERR);
     55    return result;
    3556}
    3657
     58static bool sequenceOfCryptoOperationDataFromJSValue(ExecState* exec, JSValue value, Vector<CryptoOperationData>& result)
     59{
     60    unsigned sequenceLength;
     61    JSObject* sequence = toJSSequence(exec, value, sequenceLength);
     62    if (!sequence) {
     63        ASSERT(exec->hadException());
     64        return false;
     65    }
     66
     67    for (unsigned i = 0; i < sequenceLength; ++i) {
     68        JSValue item = sequence->get(exec, i);
     69        if (ArrayBuffer* buffer = toArrayBuffer(item))
     70            result.append(std::make_pair(static_cast<char*>(buffer->data()), buffer->byteLength()));
     71        else if (RefPtr<ArrayBufferView> bufferView = toArrayBufferView(item))
     72            result.append(std::make_pair(static_cast<char*>(bufferView->baseAddress()), bufferView->byteLength()));
     73        else {
     74            throwTypeError(exec, "Only ArrayBuffer and ArrayBufferView objects can be part of CryptoOperationData sequence");
     75            return false;
     76        }
     77    }
     78    return true;
     79}
     80
     81JSValue JSSubtleCrypto::digest(ExecState* exec)
     82{
     83    if (exec->argumentCount() < 2)
     84        return exec->vm().throwException(exec, createNotEnoughArgumentsError(exec));
     85
     86    std::unique_ptr<CryptoAlgorithm> algorithm = createAlgorithmFromJSValue(exec, exec->uncheckedArgument(0));
     87    if (!algorithm) {
     88        ASSERT(exec->hadException());
     89        return jsUndefined();
     90    }
     91
     92    std::unique_ptr<CryptoAlgorithmParameters> parameters = JSCryptoAlgorithmDictionary::createParametersForDigest(exec, algorithm->identifier(), exec->uncheckedArgument(0));
     93    if (!parameters) {
     94        ASSERT(exec->hadException());
     95        return jsUndefined();
     96    }
     97
     98    Vector<CryptoOperationData> data;
     99    if (!sequenceOfCryptoOperationDataFromJSValue(exec, exec->uncheckedArgument(1), data)) {
     100        ASSERT(exec->hadException());
     101        return jsUndefined();
     102    }
     103
     104    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
     105    std::unique_ptr<PromiseWrapper> promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     106
     107    ExceptionCode ec = 0;
     108    algorithm->digest(*parameters, data, std::move(promiseWrapper), ec);
     109    if (ec) {
     110        setDOMException(exec, ec);
     111        return jsUndefined();
     112    }
     113
     114    return promise;
     115}
     116
     117} // namespace WebCore
     118
    37119#endif
  • trunk/Source/WebCore/crypto/SubtleCrypto.idl

    r157430 r158387  
    3131    OperationsNotDeletable
    3232] interface SubtleCrypto {
     33    [Custom] Promise digest(AlgorithmIdentifier algorithm, sequence<CryptoOperationData> data);
    3334};
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmRegistryMac.cpp

    r158363 r158387  
    3030
    3131//#include "CryptoAlgorithmHMAC.h"
    32 //#include "CryptoAlgorithmSHA1.h"
     32#include "CryptoAlgorithmSHA1.h"
    3333
    3434namespace WebCore {
     
    3636void CryptoAlgorithmRegistry::platformRegisterAlgorithms()
    3737{
    38 //    registerAlgorithm(CryptoAlgorithmHMAC::m_name, CryptoAlgorithmHMAC::m_identifier, CryptoAlgorithmHMAC::create);
    39 //    registerAlgorithm(CryptoAlgorithmSHA1::m_name, CryptoAlgorithmSHA1::m_identifier, CryptoAlgorithmSHA1::create);
     38//    registerAlgorithm(CryptoAlgorithmHMAC::s_name, CryptoAlgorithmHMAC::s_identifier, CryptoAlgorithmHMAC::create);
     39    registerAlgorithm(CryptoAlgorithmSHA1::s_name, CryptoAlgorithmSHA1::s_identifier, CryptoAlgorithmSHA1::create);
    4040}
    4141
Note: See TracChangeset for help on using the changeset viewer.