Changeset 160455 in webkit


Ignore:
Timestamp:
Dec 11, 2013 2:13:55 PM (10 years ago)
Author:
ap@apple.com
Message:

WebCrypto keys should support structured clone
https://bugs.webkit.org/show_bug.cgi?id=125590

Reviewed by Oliver Hunt.

Source/WebCore:

Tests: crypto/subtle/aes-postMessage.html

crypto/subtle/hmac-postMessage.html
crypto/subtle/postMessage-worker.html
crypto/subtle/rsa-postMessage.html

  • crypto/CryptoAlgorithmIdentifier.h:

(WebCore::CryptoAlgorithmIdentifier):

  • bindings/js/JSCryptoAlgorithmDictionary.cpp:

(WebCore::JSCryptoAlgorithmDictionary::createParametersForEncrypt):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForDecrypt):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForSign):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForVerify):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForDigest):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForGenerateKey):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForDeriveKey):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForDeriveBits):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForImportKey):
(WebCore::JSCryptoAlgorithmDictionary::createParametersForExportKey):
AES_KW was added to WebCrypto spec editor's draft yesterday night. Now that we're
adding a matching enum for structired clone serialization, it's as good a time as
any to update the order of values to match.

  • bindings/js/SerializedScriptValue.cpp:

(WebCore::countUsages):
(WebCore::CloneSerializer::dumpIfTerminal):
(WebCore::CloneSerializer::write):
(WebCore::CloneDeserializer::read):
(WebCore::CloneDeserializer::readHMACKey):
(WebCore::CloneDeserializer::readAESKey):
(WebCore::CloneDeserializer::readRSAKey):
(WebCore::CloneDeserializer::readCryptoKey):
(WebCore::CloneDeserializer::readTerminal):
Added serialization/deserialization for CryptoKey. This doesn't update version
number, because we don't currently store structured clones in persistent storage -
and even if we did, we wouldn't want to invalidate everything users already stored.

  • crypto/CryptoAlgorithmRegistry.cpp:

(WebCore::CryptoAlgorithmRegistry::shared):
(WebCore::registryMutex):
(WebCore::CryptoAlgorithmRegistry::getIdentifierForName):
(WebCore::CryptoAlgorithmRegistry::nameForIdentifier):
(WebCore::CryptoAlgorithmRegistry::create):
(WebCore::CryptoAlgorithmRegistry::registerAlgorithm):

  • crypto/CryptoKey.idl:

With structured clone, it is now possible to send a Key to a web worker. That's
of no practical use because the crypto API is not exposed in workers, but we
shouldn't crash anyway.

  • crypto/keys/CryptoKeyAES.cpp:

(WebCore::CryptoKeyAES::CryptoKeyAES):
(WebCore::CryptoKeyAES::isValidAESAlgorithm):

  • crypto/keys/CryptoKeyAES.h:

Exposed isValidAESAlgorithm, so that a caller could know whether the constructor
will assert.

  • CMakeLists.txt:
  • GNUmakefile.am:
  • WebCore.vcxproj/WebCore.vcxproj.filters:
  • WebCore.vcxproj/WebCoreCommon.props:

Added crypto/keys to search paths to avoid build breakage.

LayoutTests:

  • crypto/subtle/aes-postMessage-expected.txt: Added.
  • crypto/subtle/aes-postMessage.html: Added.
  • crypto/subtle/hmac-postMessage-expected.txt: Added.
  • crypto/subtle/hmac-postMessage.html: Added.
  • crypto/subtle/postMessage-worker-expected.txt: Added.
  • crypto/subtle/postMessage-worker.html: Added.
  • crypto/subtle/rsa-postMessage-expected.txt: Added.
  • crypto/subtle/rsa-postMessage.html: Added.
  • crypto/subtle/resources/postMessage-worker.js: Added.
Location:
trunk
Files:
9 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r160451 r160455  
     12013-12-11  Alexey Proskuryakov  <ap@apple.com>
     2
     3        WebCrypto keys should support structured clone
     4        https://bugs.webkit.org/show_bug.cgi?id=125590
     5
     6        Reviewed by Oliver Hunt.
     7
     8        * crypto/subtle/aes-postMessage-expected.txt: Added.
     9        * crypto/subtle/aes-postMessage.html: Added.
     10        * crypto/subtle/hmac-postMessage-expected.txt: Added.
     11        * crypto/subtle/hmac-postMessage.html: Added.
     12        * crypto/subtle/postMessage-worker-expected.txt: Added.
     13        * crypto/subtle/postMessage-worker.html: Added.
     14        * crypto/subtle/rsa-postMessage-expected.txt: Added.
     15        * crypto/subtle/rsa-postMessage.html: Added.
     16        * crypto/subtle/resources/postMessage-worker.js: Added.
     17
    1182013-12-11  Bear Travis  <betravis@adobe.com>
    219
  • trunk/Source/WebCore/CMakeLists.txt

    r160336 r160455  
    2525    "${WEBCORE_DIR}/bridge/c"
    2626    "${WEBCORE_DIR}/crypto"
     27    "${WEBCORE_DIR}/crypto/keys"
    2728    "${WEBCORE_DIR}/css"
    2829    "${WEBCORE_DIR}/dom"
  • trunk/Source/WebCore/ChangeLog

    r160451 r160455  
     12013-12-11  Alexey Proskuryakov  <ap@apple.com>
     2
     3        WebCrypto keys should support structured clone
     4        https://bugs.webkit.org/show_bug.cgi?id=125590
     5
     6        Reviewed by Oliver Hunt.
     7
     8        Tests: crypto/subtle/aes-postMessage.html
     9               crypto/subtle/hmac-postMessage.html
     10               crypto/subtle/postMessage-worker.html
     11               crypto/subtle/rsa-postMessage.html
     12
     13        * crypto/CryptoAlgorithmIdentifier.h:
     14        (WebCore::CryptoAlgorithmIdentifier):
     15        * bindings/js/JSCryptoAlgorithmDictionary.cpp:
     16        (WebCore::JSCryptoAlgorithmDictionary::createParametersForEncrypt):
     17        (WebCore::JSCryptoAlgorithmDictionary::createParametersForDecrypt):
     18        (WebCore::JSCryptoAlgorithmDictionary::createParametersForSign):
     19        (WebCore::JSCryptoAlgorithmDictionary::createParametersForVerify):
     20        (WebCore::JSCryptoAlgorithmDictionary::createParametersForDigest):
     21        (WebCore::JSCryptoAlgorithmDictionary::createParametersForGenerateKey):
     22        (WebCore::JSCryptoAlgorithmDictionary::createParametersForDeriveKey):
     23        (WebCore::JSCryptoAlgorithmDictionary::createParametersForDeriveBits):
     24        (WebCore::JSCryptoAlgorithmDictionary::createParametersForImportKey):
     25        (WebCore::JSCryptoAlgorithmDictionary::createParametersForExportKey):
     26        AES_KW was added to WebCrypto spec editor's draft yesterday night. Now that we're
     27        adding a matching enum for structired clone serialization, it's as good a time as
     28        any to update the order of values to match.
     29
     30        * bindings/js/SerializedScriptValue.cpp:
     31        (WebCore::countUsages):
     32        (WebCore::CloneSerializer::dumpIfTerminal):
     33        (WebCore::CloneSerializer::write):
     34        (WebCore::CloneDeserializer::read):
     35        (WebCore::CloneDeserializer::readHMACKey):
     36        (WebCore::CloneDeserializer::readAESKey):
     37        (WebCore::CloneDeserializer::readRSAKey):
     38        (WebCore::CloneDeserializer::readCryptoKey):
     39        (WebCore::CloneDeserializer::readTerminal):
     40        Added serialization/deserialization for CryptoKey. This doesn't update version
     41        number, because we don't currently store structured clones in persistent storage -
     42        and even if we did, we wouldn't want to invalidate everything users already stored.
     43
     44        * crypto/CryptoAlgorithmRegistry.cpp:
     45        (WebCore::CryptoAlgorithmRegistry::shared):
     46        (WebCore::registryMutex):
     47        (WebCore::CryptoAlgorithmRegistry::getIdentifierForName):
     48        (WebCore::CryptoAlgorithmRegistry::nameForIdentifier):
     49        (WebCore::CryptoAlgorithmRegistry::create):
     50        (WebCore::CryptoAlgorithmRegistry::registerAlgorithm):
     51        * crypto/CryptoKey.idl:
     52        With structured clone, it is now possible to send a Key to a web worker. That's
     53        of no practical use because the crypto API is not exposed in workers, but we
     54        shouldn't crash anyway.
     55
     56        * crypto/keys/CryptoKeyAES.cpp:
     57        (WebCore::CryptoKeyAES::CryptoKeyAES):
     58        (WebCore::CryptoKeyAES::isValidAESAlgorithm):
     59        * crypto/keys/CryptoKeyAES.h:
     60        Exposed isValidAESAlgorithm, so that a caller could know whether the constructor
     61        will assert.
     62
     63        * CMakeLists.txt:
     64        * GNUmakefile.am:
     65        * WebCore.vcxproj/WebCore.vcxproj.filters:
     66        * WebCore.vcxproj/WebCoreCommon.props:
     67        Added crypto/keys to search paths to avoid build breakage.
     68
    1692013-12-11  Bear Travis  <betravis@adobe.com>
    270
  • trunk/Source/WebCore/GNUmakefile.am

    r159827 r160455  
    3434        -I$(srcdir)/Source/WebCore/bridge/jsc \
    3535        -I$(srcdir)/Source/WebCore/crypto \
     36        -I$(srcdir)/Source/WebCore/crypto/keys \
    3637        -I$(srcdir)/Source/WebCore/css \
    3738        -I$(srcdir)/Source/WebCore/dom \
  • trunk/Source/WebCore/WebCore.vcxproj/WebCore.vcxproj.filters

    r160338 r160455  
    176176      <UniqueIdentifier>{02334427-19d6-44c8-a5d1-e1a67a9c4997}</UniqueIdentifier>
    177177    </Filter>
     178    <Filter Include="crypto\keys">
     179      <UniqueIdentifier>{edd0faa6-bb8c-4c0e-b28a-064bde7e1eba}</UniqueIdentifier>
     180    </Filter>
    178181    <Filter Include="css">
    179182      <UniqueIdentifier>{353b76ca-c49a-41da-88da-33dc422325ef}</UniqueIdentifier>
  • trunk/Source/WebCore/WebCore.vcxproj/WebCoreCommon.props

    r159827 r160455  
    88  <ItemDefinitionGroup>
    99    <ClCompile>
    10       <AdditionalIncludeDirectories>$(ProjectDir)..;$(ProjectDir)..\Modules\mediacontrols;$(ProjectDir)..\Modules\mediastream;$(ProjectDir)..\Modules\filesystem;$(ProjectDir)..\Modules\geolocation;$(ProjectDir)..\Modules\indexeddb;$(ProjectDir)..\Modules\mediasource;$(ProjectDir)..\Modules\navigatorcontentutils;$(ProjectDir)..\Modules\plugins;$(ProjectDir)..\Modules\speech;$(ProjectDir)..\Modules\proximity;$(ProjectDir)..\Modules\quota;$(ProjectDir)..\Modules\notifications;$(ProjectDir)..\Modules\webdatabase;$(ProjectDir)..\Modules\websockets;$(ProjectDir)..\accessibility;$(ProjectDir)..\accessibility\win;$(ProjectDir)..\bridge;$(ProjectDir)..\bridge\c;$(ProjectDir)..\bridge\jsc;$(ProjectDir)..\css;$(ProjectDir)..\editing;$(ProjectDir)..\fileapi;$(ProjectDir)..\rendering;$(ProjectDir)..\rendering\line;$(ProjectDir)..\rendering\mathml;$(ProjectDir)..\rendering\shapes;$(ProjectDir)..\rendering\style;$(ProjectDir)..\rendering\svg;$(ProjectDir)..\bindings;$(ProjectDir)..\bindings\generic;$(ProjectDir)..\bindings\js;$(ProjectDir)..\bindings\js\specialization;$(ProjectDir)..\dom;$(ProjectDir)..\dom\default;$(ProjectDir)..\history;$(ProjectDir)..\html;$(ProjectDir)..\html\canvas;$(ProjectDir)..\html\forms;$(ProjectDir)..\html\parser;$(ProjectDir)..\html\shadow;$(ProjectDir)..\html\track;$(ProjectDir)..\inspector;$(ProjectDir)..\loader;$(ProjectDir)..\loader\appcache;$(ProjectDir)..\loader\archive;$(ProjectDir)..\loader\archive\cf;$(ProjectDir)..\loader\cache;$(ProjectDir)..\loader\icon;$(ProjectDir)..\mathml;$(ProjectDir)..\page;$(ProjectDir)..\page\animation;$(ProjectDir)..\page\scrolling;$(ProjectDir)..\page\win;$(ProjectDir)..\platform;$(ProjectDir)..\platform\animation;$(ProjectDir)..\platform\mock;$(ProjectDir)..\platform\sql;$(ProjectDir)..\platform\win;$(ProjectDir)..\platform\network;$(ProjectDir)..\platform\network\win;$(ProjectDir)..\platform\cf;$(ProjectDir)..\platform\graphics;$(ProjectDir)..\platform\graphics\ca;$(ProjectDir)..\platform\graphics\cpu\arm\filters;$(ProjectDir)..\platform\graphics\filters;$(ProjectDir)..\platform\graphics\filters\arm;$(ProjectDir)..\platform\graphics\opentype;$(ProjectDir)..\platform\graphics\transforms;$(ProjectDir)..\platform\text;$(ProjectDir)..\platform\text\transcoder;$(ProjectDir)..\platform\graphics\win;$(ProjectDir)..\xml;$(ProjectDir)..\xml\parser;$(ConfigurationBuildDir)\obj$(PlatformArchitecture)\WebCore\DerivedSources;$(ProjectDir)..\plugins;$(ProjectDir)..\plugins\win;$(ProjectDir)..\svg\animation;$(ProjectDir)..\svg\graphics;$(ProjectDir)..\svg\properties;$(ProjectDir)..\svg\graphics\filters;$(ProjectDir)..\svg;$(ProjectDir)..\testing;$(ProjectDir)..\crypto;$(ProjectDir)..\wml;$(ProjectDir)..\storage;$(ProjectDir)..\style;$(ProjectDir)..\websockets;$(ProjectDir)..\workers;$(ConfigurationBuildDir)\include;$(ConfigurationBuildDir)\include\private;$(ConfigurationBuildDir)\include\JavaScriptCore;$(ConfigurationBuildDir)\include\private\JavaScriptCore;$(ProjectDir)..\ForwardingHeaders;$(ProjectDir)..\platform\graphics\gpu;$(ProjectDir)..\platform\graphics\egl;$(ProjectDir)..\platform\graphics\surfaces;$(ProjectDir)..\platform\graphics\surfaces\egl;$(ProjectDir)..\platform\graphics\opengl;$(WebKit_Libraries)\include;$(WebKit_Libraries)\include\private;$(WebKit_Libraries)\include\private\JavaScriptCore;$(WebKit_Libraries)\include\sqlite;$(WebKit_Libraries)\include\JavaScriptCore;$(WebKit_Libraries)\include\zlib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     10      <AdditionalIncludeDirectories>$(ProjectDir)..;$(ProjectDir)..\Modules\mediacontrols;$(ProjectDir)..\Modules\mediastream;$(ProjectDir)..\Modules\filesystem;$(ProjectDir)..\Modules\geolocation;$(ProjectDir)..\Modules\indexeddb;$(ProjectDir)..\Modules\mediasource;$(ProjectDir)..\Modules\navigatorcontentutils;$(ProjectDir)..\Modules\plugins;$(ProjectDir)..\Modules\speech;$(ProjectDir)..\Modules\proximity;$(ProjectDir)..\Modules\quota;$(ProjectDir)..\Modules\notifications;$(ProjectDir)..\Modules\webdatabase;$(ProjectDir)..\Modules\websockets;$(ProjectDir)..\accessibility;$(ProjectDir)..\accessibility\win;$(ProjectDir)..\bridge;$(ProjectDir)..\bridge\c;$(ProjectDir)..\bridge\jsc;$(ProjectDir)..\css;$(ProjectDir)..\editing;$(ProjectDir)..\fileapi;$(ProjectDir)..\rendering;$(ProjectDir)..\rendering\line;$(ProjectDir)..\rendering\mathml;$(ProjectDir)..\rendering\shapes;$(ProjectDir)..\rendering\style;$(ProjectDir)..\rendering\svg;$(ProjectDir)..\bindings;$(ProjectDir)..\bindings\generic;$(ProjectDir)..\bindings\js;$(ProjectDir)..\bindings\js\specialization;$(ProjectDir)..\dom;$(ProjectDir)..\dom\default;$(ProjectDir)..\history;$(ProjectDir)..\html;$(ProjectDir)..\html\canvas;$(ProjectDir)..\html\forms;$(ProjectDir)..\html\parser;$(ProjectDir)..\html\shadow;$(ProjectDir)..\html\track;$(ProjectDir)..\inspector;$(ProjectDir)..\loader;$(ProjectDir)..\loader\appcache;$(ProjectDir)..\loader\archive;$(ProjectDir)..\loader\archive\cf;$(ProjectDir)..\loader\cache;$(ProjectDir)..\loader\icon;$(ProjectDir)..\mathml;$(ProjectDir)..\page;$(ProjectDir)..\page\animation;$(ProjectDir)..\page\scrolling;$(ProjectDir)..\page\win;$(ProjectDir)..\platform;$(ProjectDir)..\platform\animation;$(ProjectDir)..\platform\mock;$(ProjectDir)..\platform\sql;$(ProjectDir)..\platform\win;$(ProjectDir)..\platform\network;$(ProjectDir)..\platform\network\win;$(ProjectDir)..\platform\cf;$(ProjectDir)..\platform\graphics;$(ProjectDir)..\platform\graphics\ca;$(ProjectDir)..\platform\graphics\cpu\arm\filters;$(ProjectDir)..\platform\graphics\filters;$(ProjectDir)..\platform\graphics\filters\arm;$(ProjectDir)..\platform\graphics\opentype;$(ProjectDir)..\platform\graphics\transforms;$(ProjectDir)..\platform\text;$(ProjectDir)..\platform\text\transcoder;$(ProjectDir)..\platform\graphics\win;$(ProjectDir)..\xml;$(ProjectDir)..\xml\parser;$(ConfigurationBuildDir)\obj$(PlatformArchitecture)\WebCore\DerivedSources;$(ProjectDir)..\plugins;$(ProjectDir)..\plugins\win;$(ProjectDir)..\svg\animation;$(ProjectDir)..\svg\graphics;$(ProjectDir)..\svg\properties;$(ProjectDir)..\svg\graphics\filters;$(ProjectDir)..\svg;$(ProjectDir)..\testing;$(ProjectDir)..\crypto;$(ProjectDir)..\crypto\keys;$(ProjectDir)..\wml;$(ProjectDir)..\storage;$(ProjectDir)..\style;$(ProjectDir)..\websockets;$(ProjectDir)..\workers;$(ConfigurationBuildDir)\include;$(ConfigurationBuildDir)\include\private;$(ConfigurationBuildDir)\include\JavaScriptCore;$(ConfigurationBuildDir)\include\private\JavaScriptCore;$(ProjectDir)..\ForwardingHeaders;$(ProjectDir)..\platform\graphics\gpu;$(ProjectDir)..\platform\graphics\egl;$(ProjectDir)..\platform\graphics\surfaces;$(ProjectDir)..\platform\graphics\surfaces\egl;$(ProjectDir)..\platform\graphics\opengl;$(WebKit_Libraries)\include;$(WebKit_Libraries)\include\private;$(WebKit_Libraries)\include\private\JavaScriptCore;$(WebKit_Libraries)\include\sqlite;$(WebKit_Libraries)\include\JavaScriptCore;$(WebKit_Libraries)\include\zlib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    1111      <PreprocessorDefinitions>DISABLE_3D_RENDERING;WEBCORE_CONTEXT_MENUS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    1212      <PrecompiledHeader>Use</PrecompiledHeader>
  • trunk/Source/WebCore/bindings/js/JSCryptoAlgorithmDictionary.cpp

    r159966 r160455  
    318318    case CryptoAlgorithmIdentifier::AES_GCM:
    319319    case CryptoAlgorithmIdentifier::AES_CFB:
    320     case CryptoAlgorithmIdentifier::HMAC:
    321     case CryptoAlgorithmIdentifier::DH:
    322     case CryptoAlgorithmIdentifier::SHA_1:
    323     case CryptoAlgorithmIdentifier::SHA_224:
    324     case CryptoAlgorithmIdentifier::SHA_256:
    325     case CryptoAlgorithmIdentifier::SHA_384:
    326     case CryptoAlgorithmIdentifier::SHA_512:
    327     case CryptoAlgorithmIdentifier::CONCAT:
    328     case CryptoAlgorithmIdentifier::HKDF_CTR:
    329     case CryptoAlgorithmIdentifier::PBKDF2:
    330         setDOMException(exec, NOT_SUPPORTED_ERR);
    331         return nullptr;
    332     case CryptoAlgorithmIdentifier::AES_KW:
    333         return std::make_unique<CryptoAlgorithmParameters>();
     320        setDOMException(exec, NOT_SUPPORTED_ERR);
     321        return nullptr;
     322    case CryptoAlgorithmIdentifier::AES_KW:
     323        return std::make_unique<CryptoAlgorithmParameters>();
     324    case CryptoAlgorithmIdentifier::HMAC:
     325    case CryptoAlgorithmIdentifier::DH:
     326    case CryptoAlgorithmIdentifier::SHA_1:
     327    case CryptoAlgorithmIdentifier::SHA_224:
     328    case CryptoAlgorithmIdentifier::SHA_256:
     329    case CryptoAlgorithmIdentifier::SHA_384:
     330    case CryptoAlgorithmIdentifier::SHA_512:
     331    case CryptoAlgorithmIdentifier::CONCAT:
     332    case CryptoAlgorithmIdentifier::HKDF_CTR:
     333    case CryptoAlgorithmIdentifier::PBKDF2:
     334        setDOMException(exec, NOT_SUPPORTED_ERR);
     335        return nullptr;
    334336    }
    335337}
     
    355357    case CryptoAlgorithmIdentifier::AES_GCM:
    356358    case CryptoAlgorithmIdentifier::AES_CFB:
    357     case CryptoAlgorithmIdentifier::HMAC:
    358     case CryptoAlgorithmIdentifier::DH:
    359     case CryptoAlgorithmIdentifier::SHA_1:
    360     case CryptoAlgorithmIdentifier::SHA_224:
    361     case CryptoAlgorithmIdentifier::SHA_256:
    362     case CryptoAlgorithmIdentifier::SHA_384:
    363     case CryptoAlgorithmIdentifier::SHA_512:
    364     case CryptoAlgorithmIdentifier::CONCAT:
    365     case CryptoAlgorithmIdentifier::HKDF_CTR:
    366     case CryptoAlgorithmIdentifier::PBKDF2:
    367         setDOMException(exec, NOT_SUPPORTED_ERR);
    368         return nullptr;
    369     case CryptoAlgorithmIdentifier::AES_KW:
    370         return std::make_unique<CryptoAlgorithmParameters>();
     359        setDOMException(exec, NOT_SUPPORTED_ERR);
     360        return nullptr;
     361    case CryptoAlgorithmIdentifier::AES_KW:
     362        return std::make_unique<CryptoAlgorithmParameters>();
     363    case CryptoAlgorithmIdentifier::HMAC:
     364    case CryptoAlgorithmIdentifier::DH:
     365    case CryptoAlgorithmIdentifier::SHA_1:
     366    case CryptoAlgorithmIdentifier::SHA_224:
     367    case CryptoAlgorithmIdentifier::SHA_256:
     368    case CryptoAlgorithmIdentifier::SHA_384:
     369    case CryptoAlgorithmIdentifier::SHA_512:
     370    case CryptoAlgorithmIdentifier::CONCAT:
     371    case CryptoAlgorithmIdentifier::HKDF_CTR:
     372    case CryptoAlgorithmIdentifier::PBKDF2:
     373        setDOMException(exec, NOT_SUPPORTED_ERR);
     374        return nullptr;
    371375    }
    372376}
     
    389393    case CryptoAlgorithmIdentifier::AES_GCM:
    390394    case CryptoAlgorithmIdentifier::AES_CFB:
     395    case CryptoAlgorithmIdentifier::AES_KW:
    391396        setDOMException(exec, NOT_SUPPORTED_ERR);
    392397        return nullptr;
     
    402407    case CryptoAlgorithmIdentifier::HKDF_CTR:
    403408    case CryptoAlgorithmIdentifier::PBKDF2:
    404     case CryptoAlgorithmIdentifier::AES_KW:
    405409        setDOMException(exec, NOT_SUPPORTED_ERR);
    406410        return nullptr;
     
    425429    case CryptoAlgorithmIdentifier::AES_GCM:
    426430    case CryptoAlgorithmIdentifier::AES_CFB:
     431    case CryptoAlgorithmIdentifier::AES_KW:
    427432        setDOMException(exec, NOT_SUPPORTED_ERR);
    428433        return nullptr;
     
    438443    case CryptoAlgorithmIdentifier::HKDF_CTR:
    439444    case CryptoAlgorithmIdentifier::PBKDF2:
    440     case CryptoAlgorithmIdentifier::AES_KW:
    441445        setDOMException(exec, NOT_SUPPORTED_ERR);
    442446        return nullptr;
     
    458462    case CryptoAlgorithmIdentifier::AES_GCM:
    459463    case CryptoAlgorithmIdentifier::AES_CFB:
    460     case CryptoAlgorithmIdentifier::HMAC:
    461     case CryptoAlgorithmIdentifier::DH:
    462         setDOMException(exec, NOT_SUPPORTED_ERR);
    463         return nullptr;
    464     case CryptoAlgorithmIdentifier::SHA_1:
    465     case CryptoAlgorithmIdentifier::SHA_224:
    466     case CryptoAlgorithmIdentifier::SHA_256:
    467     case CryptoAlgorithmIdentifier::SHA_384:
    468     case CryptoAlgorithmIdentifier::SHA_512:
    469         return std::make_unique<CryptoAlgorithmParameters>();
    470     case CryptoAlgorithmIdentifier::CONCAT:
    471     case CryptoAlgorithmIdentifier::HKDF_CTR:
    472     case CryptoAlgorithmIdentifier::PBKDF2:
    473     case CryptoAlgorithmIdentifier::AES_KW:
     464    case CryptoAlgorithmIdentifier::AES_KW:
     465    case CryptoAlgorithmIdentifier::HMAC:
     466    case CryptoAlgorithmIdentifier::DH:
     467        setDOMException(exec, NOT_SUPPORTED_ERR);
     468        return nullptr;
     469    case CryptoAlgorithmIdentifier::SHA_1:
     470    case CryptoAlgorithmIdentifier::SHA_224:
     471    case CryptoAlgorithmIdentifier::SHA_256:
     472    case CryptoAlgorithmIdentifier::SHA_384:
     473    case CryptoAlgorithmIdentifier::SHA_512:
     474        return std::make_unique<CryptoAlgorithmParameters>();
     475    case CryptoAlgorithmIdentifier::CONCAT:
     476    case CryptoAlgorithmIdentifier::HKDF_CTR:
     477    case CryptoAlgorithmIdentifier::PBKDF2:
    474478        setDOMException(exec, NOT_SUPPORTED_ERR);
    475479        return nullptr;
     
    494498    case CryptoAlgorithmIdentifier::AES_GCM:
    495499    case CryptoAlgorithmIdentifier::AES_CFB:
     500    case CryptoAlgorithmIdentifier::AES_KW:
    496501        return createAesKeyGenParams(exec, value);
    497502    case CryptoAlgorithmIdentifier::HMAC:
     
    508513        setDOMException(exec, NOT_SUPPORTED_ERR);
    509514        return nullptr;
    510     case CryptoAlgorithmIdentifier::AES_KW:
    511         return createAesKeyGenParams(exec, value);
    512515    }
    513516}
     
    527530    case CryptoAlgorithmIdentifier::AES_GCM:
    528531    case CryptoAlgorithmIdentifier::AES_CFB:
    529     case CryptoAlgorithmIdentifier::HMAC:
    530     case CryptoAlgorithmIdentifier::DH:
    531     case CryptoAlgorithmIdentifier::SHA_1:
    532     case CryptoAlgorithmIdentifier::SHA_224:
    533     case CryptoAlgorithmIdentifier::SHA_256:
    534     case CryptoAlgorithmIdentifier::SHA_384:
    535     case CryptoAlgorithmIdentifier::SHA_512:
    536     case CryptoAlgorithmIdentifier::CONCAT:
    537     case CryptoAlgorithmIdentifier::HKDF_CTR:
    538     case CryptoAlgorithmIdentifier::PBKDF2:
    539     case CryptoAlgorithmIdentifier::AES_KW:
     532    case CryptoAlgorithmIdentifier::AES_KW:
     533    case CryptoAlgorithmIdentifier::HMAC:
     534    case CryptoAlgorithmIdentifier::DH:
     535    case CryptoAlgorithmIdentifier::SHA_1:
     536    case CryptoAlgorithmIdentifier::SHA_224:
     537    case CryptoAlgorithmIdentifier::SHA_256:
     538    case CryptoAlgorithmIdentifier::SHA_384:
     539    case CryptoAlgorithmIdentifier::SHA_512:
     540    case CryptoAlgorithmIdentifier::CONCAT:
     541    case CryptoAlgorithmIdentifier::HKDF_CTR:
     542    case CryptoAlgorithmIdentifier::PBKDF2:
    540543        setDOMException(exec, NOT_SUPPORTED_ERR);
    541544        return nullptr;
     
    557560    case CryptoAlgorithmIdentifier::AES_GCM:
    558561    case CryptoAlgorithmIdentifier::AES_CFB:
    559     case CryptoAlgorithmIdentifier::HMAC:
    560     case CryptoAlgorithmIdentifier::DH:
    561     case CryptoAlgorithmIdentifier::SHA_1:
    562     case CryptoAlgorithmIdentifier::SHA_224:
    563     case CryptoAlgorithmIdentifier::SHA_256:
    564     case CryptoAlgorithmIdentifier::SHA_384:
    565     case CryptoAlgorithmIdentifier::SHA_512:
    566     case CryptoAlgorithmIdentifier::CONCAT:
    567     case CryptoAlgorithmIdentifier::HKDF_CTR:
    568     case CryptoAlgorithmIdentifier::PBKDF2:
    569     case CryptoAlgorithmIdentifier::AES_KW:
     562    case CryptoAlgorithmIdentifier::AES_KW:
     563    case CryptoAlgorithmIdentifier::HMAC:
     564    case CryptoAlgorithmIdentifier::DH:
     565    case CryptoAlgorithmIdentifier::SHA_1:
     566    case CryptoAlgorithmIdentifier::SHA_224:
     567    case CryptoAlgorithmIdentifier::SHA_256:
     568    case CryptoAlgorithmIdentifier::SHA_384:
     569    case CryptoAlgorithmIdentifier::SHA_512:
     570    case CryptoAlgorithmIdentifier::CONCAT:
     571    case CryptoAlgorithmIdentifier::HKDF_CTR:
     572    case CryptoAlgorithmIdentifier::PBKDF2:
    570573        setDOMException(exec, NOT_SUPPORTED_ERR);
    571574        return nullptr;
     
    591594    case CryptoAlgorithmIdentifier::AES_GCM:
    592595    case CryptoAlgorithmIdentifier::AES_CFB:
     596    case CryptoAlgorithmIdentifier::AES_KW:
    593597        return std::make_unique<CryptoAlgorithmParameters>();
    594598    case CryptoAlgorithmIdentifier::HMAC:
     
    606610        setDOMException(exec, NOT_SUPPORTED_ERR);
    607611        return nullptr;
    608     case CryptoAlgorithmIdentifier::AES_KW:
    609         return std::make_unique<CryptoAlgorithmParameters>();
    610612    }
    611613}
     
    625627    case CryptoAlgorithmIdentifier::AES_GCM:
    626628    case CryptoAlgorithmIdentifier::AES_CFB:
    627     case CryptoAlgorithmIdentifier::HMAC:
    628     case CryptoAlgorithmIdentifier::DH:
    629         return std::make_unique<CryptoAlgorithmParameters>();
    630     case CryptoAlgorithmIdentifier::SHA_1:
    631     case CryptoAlgorithmIdentifier::SHA_224:
    632     case CryptoAlgorithmIdentifier::SHA_256:
    633     case CryptoAlgorithmIdentifier::SHA_384:
    634     case CryptoAlgorithmIdentifier::SHA_512:
    635     case CryptoAlgorithmIdentifier::CONCAT:
    636     case CryptoAlgorithmIdentifier::HKDF_CTR:
    637     case CryptoAlgorithmIdentifier::PBKDF2:
    638         setDOMException(exec, NOT_SUPPORTED_ERR);
    639         return nullptr;
    640     case CryptoAlgorithmIdentifier::AES_KW:
    641         return std::make_unique<CryptoAlgorithmParameters>();
     629    case CryptoAlgorithmIdentifier::AES_KW:
     630    case CryptoAlgorithmIdentifier::HMAC:
     631    case CryptoAlgorithmIdentifier::DH:
     632        return std::make_unique<CryptoAlgorithmParameters>();
     633    case CryptoAlgorithmIdentifier::SHA_1:
     634    case CryptoAlgorithmIdentifier::SHA_224:
     635    case CryptoAlgorithmIdentifier::SHA_256:
     636    case CryptoAlgorithmIdentifier::SHA_384:
     637    case CryptoAlgorithmIdentifier::SHA_512:
     638    case CryptoAlgorithmIdentifier::CONCAT:
     639    case CryptoAlgorithmIdentifier::HKDF_CTR:
     640    case CryptoAlgorithmIdentifier::PBKDF2:
     641        setDOMException(exec, NOT_SUPPORTED_ERR);
     642        return nullptr;
    642643    }
    643644}
  • trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp

    r160250 r160455  
    2929
    3030#include "Blob.h"
     31#include "CryptoKeyAES.h"
     32#include "CryptoKeyDataOctetSequence.h"
     33#include "CryptoKeyDataRSAComponents.h"
     34#include "CryptoKeyHMAC.h"
     35#include "CryptoKeyRSA.h"
    3136#include "ExceptionCode.h"
    3237#include "File.h"
     
    3439#include "ImageData.h"
    3540#include "JSBlob.h"
     41#include "JSCryptoKey.h"
    3642#include "JSDOMGlobalObject.h"
    3743#include "JSFile.h"
     
    118124    MapObjectTag = 30,
    119125    NonMapPropertiesTag = 31,
     126#if ENABLE(SUBTLE_CRYPTO)
     127    CryptoKeyTag = 32,
     128#endif
    120129    ErrorTag = 255
    121130};
     
    156165
    157166}
     167
     168#if ENABLE(SUBTLE_CRYPTO)
     169
     170ENUM_CLASS(CryptoKeyClassSubtag) {
     171    HMAC = 0,
     172    AES = 1,
     173    RSA = 2
     174};
     175const uint8_t cryptoKeyClassSubtagMaximumValue = 2;
     176
     177ENUM_CLASS(CryptoKeyAsymmetricTypeSubtag) {
     178    Public = 0,
     179    Private = 1
     180};
     181const uint8_t cryptoKeyAsymmetricTypeSubtagMaximumValue = 1;
     182
     183ENUM_CLASS(CryptoKeyUsageTag) {
     184    Encrypt = 0,
     185    Decrypt = 1,
     186    Sign = 2,
     187    Verify = 3,
     188    DeriveKey = 4,
     189    DeriveBits = 5,
     190    WrapKey = 6,
     191    UnwrapKey = 7
     192};
     193const uint8_t cryptoKeyUsageTagMaximumValue = 7;
     194
     195ENUM_CLASS(CryptoAlgorithmIdentifierTag) {
     196    RSAES_PKCS1_v1_5 = 0,
     197    RSASSA_PKCS1_v1_5 = 1,
     198    RSA_PSS = 2,
     199    RSA_OAEP = 3,
     200    ECDSA = 4,
     201    ECDH = 5,
     202    AES_CTR = 6,
     203    AES_CBC = 7,
     204    AES_CMAC = 8,
     205    AES_GCM = 9,
     206    AES_CFB = 10,
     207    AES_KW = 11,
     208    HMAC = 12,
     209    DH = 13,
     210    SHA_1 = 14,
     211    SHA_224 = 15,
     212    SHA_256 = 16,
     213    SHA_384 = 17,
     214    SHA_512 = 18,
     215    CONCAT = 19,
     216    HKDF_CTR = 20,
     217    PBKDF2 = 21,
     218};
     219const uint8_t cryptoAlgorithmIdentifierTagMaximumValue = 21;
     220
     221static unsigned countUsages(CryptoKeyUsage usages)
     222{
     223    // Fast bit count algorithm for sparse bit maps.
     224    unsigned count = 0;
     225    while (usages) {
     226        usages = usages & (usages - 1);
     227        ++count;
     228    }
     229    return count;
     230}
     231
     232#endif
    158233
    159234/* CurrentVersion tracks the serialization version so that persistent stores
     
    218293 *    | MessagePortReferenceTag <value:uint32_t>
    219294 *    | ArrayBuffer
    220  *    | ArrayBufferViewTag ArrayBufferViewSubtag <byteOffset:uint32_t> <byteLenght:uint32_t> (ArrayBuffer | ObjectReference)
     295 *    | ArrayBufferViewTag ArrayBufferViewSubtag <byteOffset:uint32_t> <byteLength:uint32_t> (ArrayBuffer | ObjectReference)
    221296 *    | ArrayBufferTransferTag <value:uint32_t>
     297 *    | CryptoKeyTag <extractable:int32_t> <usagesCount:uint32_t> <usages:byte{usagesCount}> CryptoKeyClassSubtag (CryptoKeyHMAC | CryptoKeyAES | CryptoKeyRSA)
    222298 *
    223299 * String :-
     
    256332 * ArrayBuffer :-
    257333 *    ArrayBufferTag <length:uint32_t> <contents:byte{length}>
     334 *
     335 * CryptoKeyHMAC :-
     336 *    <keySize:uint32_t> <keyData:byte{keySize}> CryptoAlgorithmIdentifierTag // Algorithm tag inner hash function.
     337 *
     338 * CryptoKeyAES :-
     339 *    CryptoAlgorithmIdentifierTag <keySize:uint32_t> <keyData:byte{keySize}>
     340 *
     341 * CryptoKeyRSA :-
     342 *    CryptoAlgorithmIdentifierTag <isRestrictedToHash:int32_t> CryptoAlgorithmIdentifierTag? CryptoKeyAsymmetricTypeSubtag CryptoKeyRSAPublicComponents CryptoKeyRSAPrivateComponents?
     343 *
     344 * CryptoKeyRSAPublicComponents :-
     345 *    <modulusSize:uint32_t> <modulus:byte{modulusSize}> <exponentSize:uint32_t> <exponent:byte{exponentSize}>
     346 *
     347 * CryptoKeyRSAPrivateComponents :-
     348 *    <privateExponentSize:uint32_t> <privateExponent:byte{privateExponentSize}> <primeCount:uint32_t> FirstPrimeInfo? PrimeInfo{primeCount - 1}
     349 *
     350 * // CRT data could be computed from prime factors. It is only serialized to reuse a code path that's needed for JWK.
     351 * FirstPrimeInfo :-
     352 *    <factorSize:uint32_t> <factor:byte{factorSize}> <crtExponentSize:uint32_t> <crtExponent:byte{crtExponentSize}>
     353 *
     354 * PrimeInfo :-
     355 *    <factorSize:uint32_t> <factor:byte{factorSize}> <crtExponentSize:uint32_t> <crtExponent:byte{crtExponentSize}> <crtCoefficientSize:uint32_t> <crtCoefficient:byte{crtCoefficientSize}>
    258356 */
    259357
     
    725823                return success;
    726824            }
     825#if ENABLE(SUBTLE_CRYPTO)
     826            if (CryptoKey* key = toCryptoKey(obj)) {
     827                write(CryptoKeyTag);
     828                write(key);
     829                return true;
     830            }
     831#endif
    727832
    728833            return false;
     
    742847        writeLittleEndian<uint8_t>(m_buffer, static_cast<uint8_t>(tag));
    743848    }
     849
     850#if ENABLE(SUBTLE_CRYPTO)
     851    void write(CryptoKeyClassSubtag tag)
     852    {
     853        writeLittleEndian<uint8_t>(m_buffer, static_cast<uint8_t>(tag));
     854    }
     855
     856    void write(CryptoKeyAsymmetricTypeSubtag tag)
     857    {
     858        writeLittleEndian<uint8_t>(m_buffer, static_cast<uint8_t>(tag));
     859    }
     860
     861    void write(CryptoKeyUsageTag tag)
     862    {
     863        writeLittleEndian<uint8_t>(m_buffer, static_cast<uint8_t>(tag));
     864    }
     865
     866    void write(CryptoAlgorithmIdentifierTag tag)
     867    {
     868        writeLittleEndian<uint8_t>(m_buffer, static_cast<uint8_t>(tag));
     869    }
     870#endif
    744871
    745872    void write(uint8_t c)
     
    835962    }
    836963
     964    void write(const Vector<uint8_t>& vector)
     965    {
     966        uint32_t size = vector.size();
     967        write(size);
     968        writeLittleEndian(m_buffer, vector.data(), size);
     969    }
     970
    837971    void write(const File* file)
    838972    {
     
    842976        write(file->type());
    843977    }
     978
     979#if ENABLE(SUBTLE_CRYPTO)
     980    void write(CryptoAlgorithmIdentifier algorithm)
     981    {
     982        switch (algorithm) {
     983        case CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5:
     984            write(CryptoAlgorithmIdentifierTag::RSAES_PKCS1_v1_5);
     985            break;
     986        case CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5:
     987            write(CryptoAlgorithmIdentifierTag::RSASSA_PKCS1_v1_5);
     988            break;
     989        case CryptoAlgorithmIdentifier::RSA_PSS:
     990            write(CryptoAlgorithmIdentifierTag::RSA_PSS);
     991            break;
     992        case CryptoAlgorithmIdentifier::RSA_OAEP:
     993            write(CryptoAlgorithmIdentifierTag::RSA_OAEP);
     994            break;
     995        case CryptoAlgorithmIdentifier::ECDSA:
     996            write(CryptoAlgorithmIdentifierTag::ECDSA);
     997            break;
     998        case CryptoAlgorithmIdentifier::ECDH:
     999            write(CryptoAlgorithmIdentifierTag::ECDH);
     1000            break;
     1001        case CryptoAlgorithmIdentifier::AES_CTR:
     1002            write(CryptoAlgorithmIdentifierTag::AES_CTR);
     1003            break;
     1004        case CryptoAlgorithmIdentifier::AES_CBC:
     1005            write(CryptoAlgorithmIdentifierTag::AES_CBC);
     1006            break;
     1007        case CryptoAlgorithmIdentifier::AES_CMAC:
     1008            write(CryptoAlgorithmIdentifierTag::AES_CMAC);
     1009            break;
     1010        case CryptoAlgorithmIdentifier::AES_GCM:
     1011            write(CryptoAlgorithmIdentifierTag::AES_GCM);
     1012            break;
     1013        case CryptoAlgorithmIdentifier::AES_CFB:
     1014            write(CryptoAlgorithmIdentifierTag::AES_CFB);
     1015            break;
     1016        case CryptoAlgorithmIdentifier::AES_KW:
     1017            write(CryptoAlgorithmIdentifierTag::AES_KW);
     1018            break;
     1019        case CryptoAlgorithmIdentifier::HMAC:
     1020            write(CryptoAlgorithmIdentifierTag::HMAC);
     1021            break;
     1022        case CryptoAlgorithmIdentifier::DH:
     1023            write(CryptoAlgorithmIdentifierTag::DH);
     1024            break;
     1025        case CryptoAlgorithmIdentifier::SHA_1:
     1026            write(CryptoAlgorithmIdentifierTag::SHA_1);
     1027            break;
     1028        case CryptoAlgorithmIdentifier::SHA_224:
     1029            write(CryptoAlgorithmIdentifierTag::SHA_224);
     1030            break;
     1031        case CryptoAlgorithmIdentifier::SHA_256:
     1032            write(CryptoAlgorithmIdentifierTag::SHA_256);
     1033            break;
     1034        case CryptoAlgorithmIdentifier::SHA_384:
     1035            write(CryptoAlgorithmIdentifierTag::SHA_384);
     1036            break;
     1037        case CryptoAlgorithmIdentifier::SHA_512:
     1038            write(CryptoAlgorithmIdentifierTag::SHA_512);
     1039            break;
     1040        case CryptoAlgorithmIdentifier::CONCAT:
     1041            write(CryptoAlgorithmIdentifierTag::CONCAT);
     1042            break;
     1043        case CryptoAlgorithmIdentifier::HKDF_CTR:
     1044            write(CryptoAlgorithmIdentifierTag::HKDF_CTR);
     1045            break;
     1046        case CryptoAlgorithmIdentifier::PBKDF2:
     1047            write(CryptoAlgorithmIdentifierTag::PBKDF2);
     1048            break;
     1049        }
     1050    }
     1051
     1052    void write(CryptoKeyDataRSAComponents::Type type)
     1053    {
     1054        switch (type) {
     1055        case CryptoKeyDataRSAComponents::Type::Public:
     1056            write(CryptoKeyAsymmetricTypeSubtag::Public);
     1057            return;
     1058        case CryptoKeyDataRSAComponents::Type::Private:
     1059            write(CryptoKeyAsymmetricTypeSubtag::Private);
     1060            return;
     1061        }
     1062    }
     1063
     1064    void write(const CryptoKeyDataRSAComponents& key)
     1065    {
     1066        write(key.type());
     1067        write(key.modulus());
     1068        write(key.exponent());
     1069        if (key.type() == CryptoKeyDataRSAComponents::Type::Public)
     1070            return;
     1071
     1072        write(key.privateExponent());
     1073
     1074        unsigned primeCount = key.hasAdditionalPrivateKeyParameters() ? key.otherPrimeInfos().size() + 2 : 0;
     1075        write(primeCount);
     1076        if (!primeCount)
     1077            return;
     1078
     1079        write(key.firstPrimeInfo().primeFactor);
     1080        write(key.firstPrimeInfo().factorCRTExponent);
     1081        write(key.secondPrimeInfo().primeFactor);
     1082        write(key.secondPrimeInfo().factorCRTExponent);
     1083        write(key.secondPrimeInfo().factorCRTCoefficient);
     1084        for (unsigned i = 2; i < primeCount; ++i) {
     1085            write(key.otherPrimeInfos()[i].primeFactor);
     1086            write(key.otherPrimeInfos()[i].factorCRTExponent);
     1087            write(key.otherPrimeInfos()[i].factorCRTCoefficient);
     1088        }
     1089    }
     1090
     1091    void write(const CryptoKey* key)
     1092    {
     1093        write(key->extractable());
     1094
     1095        CryptoKeyUsage usages = key->usagesBitmap();
     1096        write(countUsages(usages));
     1097        if (usages & CryptoKeyUsageEncrypt)
     1098            write(CryptoKeyUsageTag::Encrypt);
     1099        if (usages & CryptoKeyUsageDecrypt)
     1100            write(CryptoKeyUsageTag::Decrypt);
     1101        if (usages & CryptoKeyUsageSign)
     1102            write(CryptoKeyUsageTag::Sign);
     1103        if (usages & CryptoKeyUsageVerify)
     1104            write(CryptoKeyUsageTag::Verify);
     1105        if (usages & CryptoKeyUsageDeriveKey)
     1106            write(CryptoKeyUsageTag::DeriveKey);
     1107        if (usages & CryptoKeyUsageDeriveBits)
     1108            write(CryptoKeyUsageTag::DeriveBits);
     1109        if (usages & CryptoKeyUsageWrapKey)
     1110            write(CryptoKeyUsageTag::WrapKey);
     1111        if (usages & CryptoKeyUsageUnwrapKey)
     1112            write(CryptoKeyUsageTag::UnwrapKey);
     1113
     1114        switch (key->keyClass()) {
     1115        case CryptoKeyClass::HMAC:
     1116            write(CryptoKeyClassSubtag::HMAC);
     1117            write(toCryptoKeyHMAC(key)->key());
     1118            write(toCryptoKeyHMAC(key)->hashAlgorithmIdentifier());
     1119            break;
     1120        case CryptoKeyClass::AES:
     1121            write(CryptoKeyClassSubtag::AES);
     1122            write(key->algorithmIdentifier());
     1123            write(toCryptoKeyAES(key)->key());
     1124            break;
     1125        case CryptoKeyClass::RSA:
     1126            write(CryptoKeyClassSubtag::RSA);
     1127            write(key->algorithmIdentifier());
     1128            CryptoAlgorithmIdentifier hash;
     1129            bool isRestrictedToHash = toCryptoKeyRSA(key)->isRestrictedToHash(hash);
     1130            write(isRestrictedToHash);
     1131            if (isRestrictedToHash)
     1132                write(hash);
     1133            write(toCryptoKeyDataRSAComponents(*key->exportData()));
     1134            break;
     1135        }
     1136    }
     1137#endif
    8441138
    8451139    void write(const uint8_t* data, unsigned length)
     
    14541748    }
    14551749
     1750    bool read(Vector<uint8_t>& result)
     1751    {
     1752        ASSERT(result.isEmpty());
     1753        uint32_t size;
     1754        if (!read(size))
     1755            return false;
     1756        if (m_ptr + size > m_end)
     1757            return false;
     1758        result.append(m_ptr, size);
     1759        m_ptr += size;
     1760        return true;
     1761    }
     1762
     1763#if ENABLE(SUBTLE_CRYPTO)
     1764    bool read(CryptoAlgorithmIdentifier& result)
     1765    {
     1766        uint8_t algorithmTag;
     1767        if (!read(algorithmTag))
     1768            return false;
     1769        if (algorithmTag > cryptoAlgorithmIdentifierTagMaximumValue)
     1770            return false;
     1771        switch (static_cast<CryptoAlgorithmIdentifierTag>(algorithmTag)) {
     1772        case CryptoAlgorithmIdentifierTag::RSAES_PKCS1_v1_5:
     1773            result = CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5;
     1774            break;
     1775        case CryptoAlgorithmIdentifierTag::RSASSA_PKCS1_v1_5:
     1776            result = CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5;
     1777            break;
     1778        case CryptoAlgorithmIdentifierTag::RSA_PSS:
     1779            result = CryptoAlgorithmIdentifier::RSA_PSS;
     1780            break;
     1781        case CryptoAlgorithmIdentifierTag::RSA_OAEP:
     1782            result = CryptoAlgorithmIdentifier::RSA_OAEP;
     1783            break;
     1784        case CryptoAlgorithmIdentifierTag::ECDSA:
     1785            result = CryptoAlgorithmIdentifier::ECDSA;
     1786            break;
     1787        case CryptoAlgorithmIdentifierTag::ECDH:
     1788            result = CryptoAlgorithmIdentifier::ECDH;
     1789            break;
     1790        case CryptoAlgorithmIdentifierTag::AES_CTR:
     1791            result = CryptoAlgorithmIdentifier::AES_CTR;
     1792            break;
     1793        case CryptoAlgorithmIdentifierTag::AES_CBC:
     1794            result = CryptoAlgorithmIdentifier::AES_CBC;
     1795            break;
     1796        case CryptoAlgorithmIdentifierTag::AES_CMAC:
     1797            result = CryptoAlgorithmIdentifier::AES_CMAC;
     1798            break;
     1799        case CryptoAlgorithmIdentifierTag::AES_GCM:
     1800            result = CryptoAlgorithmIdentifier::AES_GCM;
     1801            break;
     1802        case CryptoAlgorithmIdentifierTag::AES_CFB:
     1803            result = CryptoAlgorithmIdentifier::AES_CFB;
     1804            break;
     1805        case CryptoAlgorithmIdentifierTag::AES_KW:
     1806            result = CryptoAlgorithmIdentifier::AES_KW;
     1807            break;
     1808        case CryptoAlgorithmIdentifierTag::HMAC:
     1809            result = CryptoAlgorithmIdentifier::HMAC;
     1810            break;
     1811        case CryptoAlgorithmIdentifierTag::DH:
     1812            result = CryptoAlgorithmIdentifier::DH;
     1813            break;
     1814        case CryptoAlgorithmIdentifierTag::SHA_1:
     1815            result = CryptoAlgorithmIdentifier::SHA_1;
     1816            break;
     1817        case CryptoAlgorithmIdentifierTag::SHA_224:
     1818            result = CryptoAlgorithmIdentifier::SHA_224;
     1819            break;
     1820        case CryptoAlgorithmIdentifierTag::SHA_256:
     1821            result = CryptoAlgorithmIdentifier::SHA_256;
     1822            break;
     1823        case CryptoAlgorithmIdentifierTag::SHA_384:
     1824            result = CryptoAlgorithmIdentifier::SHA_384;
     1825            break;
     1826        case CryptoAlgorithmIdentifierTag::SHA_512:
     1827            result = CryptoAlgorithmIdentifier::SHA_512;
     1828            break;
     1829        case CryptoAlgorithmIdentifierTag::CONCAT:
     1830            result = CryptoAlgorithmIdentifier::CONCAT;
     1831            break;
     1832        case CryptoAlgorithmIdentifierTag::HKDF_CTR:
     1833            result = CryptoAlgorithmIdentifier::HKDF_CTR;
     1834            break;
     1835        case CryptoAlgorithmIdentifierTag::PBKDF2:
     1836            result = CryptoAlgorithmIdentifier::PBKDF2;
     1837            break;
     1838        }
     1839        return true;
     1840    }
     1841
     1842    bool read(CryptoKeyClassSubtag& result)
     1843    {
     1844        uint8_t tag;
     1845        if (!read(tag))
     1846            return false;
     1847        if (tag > cryptoKeyClassSubtagMaximumValue)
     1848            return false;
     1849        result = static_cast<CryptoKeyClassSubtag>(tag);
     1850        return true;
     1851    }
     1852
     1853    bool read(CryptoKeyUsageTag& result)
     1854    {
     1855        uint8_t tag;
     1856        if (!read(tag))
     1857            return false;
     1858        if (tag > cryptoKeyUsageTagMaximumValue)
     1859            return false;
     1860        result = static_cast<CryptoKeyUsageTag>(tag);
     1861        return true;
     1862    }
     1863
     1864    bool read(CryptoKeyAsymmetricTypeSubtag& result)
     1865    {
     1866        uint8_t tag;
     1867        if (!read(tag))
     1868            return false;
     1869        if (tag > cryptoKeyAsymmetricTypeSubtagMaximumValue)
     1870            return false;
     1871        result = static_cast<CryptoKeyAsymmetricTypeSubtag>(tag);
     1872        return true;
     1873    }
     1874
     1875    bool readHMACKey(bool extractable, CryptoKeyUsage usages, RefPtr<CryptoKey>& result)
     1876    {
     1877        Vector<uint8_t> keyData;
     1878        if (!read(keyData))
     1879            return false;
     1880        CryptoAlgorithmIdentifier hash;
     1881        if (!read(hash))
     1882            return false;
     1883        result = CryptoKeyHMAC::create(keyData, hash, extractable, usages);
     1884        return true;
     1885    }
     1886
     1887    bool readAESKey(bool extractable, CryptoKeyUsage usages, RefPtr<CryptoKey>& result)
     1888    {
     1889        CryptoAlgorithmIdentifier algorithm;
     1890        if (!read(algorithm))
     1891            return false;
     1892        if (!CryptoKeyAES::isValidAESAlgorithm(algorithm))
     1893            return false;
     1894        Vector<uint8_t> keyData;
     1895        if (!read(keyData))
     1896            return false;
     1897        result = CryptoKeyAES::create(algorithm, keyData, extractable, usages);
     1898        return true;
     1899    }
     1900
     1901    bool readRSAKey(bool extractable, CryptoKeyUsage usages, RefPtr<CryptoKey>& result)
     1902    {
     1903        CryptoAlgorithmIdentifier algorithm;
     1904        if (!read(algorithm))
     1905            return false;
     1906
     1907        int32_t isRestrictedToHash;
     1908        CryptoAlgorithmIdentifier hash;
     1909        if (!read(isRestrictedToHash))
     1910            return false;
     1911        if (isRestrictedToHash && !read(hash))
     1912            return false;
     1913
     1914        CryptoKeyAsymmetricTypeSubtag type;
     1915        if (!read(type))
     1916            return false;
     1917
     1918        Vector<uint8_t> modulus;
     1919        if (!read(modulus))
     1920            return false;
     1921        Vector<uint8_t> exponent;
     1922        if (!read(exponent))
     1923            return false;
     1924
     1925        if (type == CryptoKeyAsymmetricTypeSubtag::Public) {
     1926            auto keyData = CryptoKeyDataRSAComponents::createPublic(modulus, exponent);
     1927            auto key = CryptoKeyRSA::create(algorithm, *keyData, extractable, usages);
     1928            if (isRestrictedToHash)
     1929                key->restrictToHash(hash);
     1930            result = std::move(key);
     1931            return true;
     1932        }
     1933
     1934        Vector<uint8_t> privateExponent;
     1935        if (!read(privateExponent))
     1936            return false;
     1937
     1938        uint32_t primeCount;
     1939        if (!read(primeCount))
     1940            return false;
     1941
     1942        if (!primeCount) {
     1943            auto keyData = CryptoKeyDataRSAComponents::createPrivate(modulus, exponent, privateExponent);
     1944            auto key = CryptoKeyRSA::create(algorithm, *keyData, extractable, usages);
     1945            if (isRestrictedToHash)
     1946                key->restrictToHash(hash);
     1947            result = std::move(key);
     1948            return true;
     1949        }
     1950
     1951        if (primeCount < 2)
     1952            return false;
     1953
     1954        CryptoKeyDataRSAComponents::PrimeInfo firstPrimeInfo;
     1955        CryptoKeyDataRSAComponents::PrimeInfo secondPrimeInfo;
     1956        Vector<CryptoKeyDataRSAComponents::PrimeInfo> otherPrimeInfos(primeCount - 2);
     1957
     1958        if (!read(firstPrimeInfo.primeFactor))
     1959            return false;
     1960        if (!read(firstPrimeInfo.factorCRTExponent))
     1961            return false;
     1962        if (!read(secondPrimeInfo.primeFactor))
     1963            return false;
     1964        if (!read(secondPrimeInfo.factorCRTExponent))
     1965            return false;
     1966        if (!read(secondPrimeInfo.factorCRTCoefficient))
     1967            return false;
     1968        for (unsigned i = 2; i < primeCount; ++i) {
     1969            if (!read(otherPrimeInfos[i].primeFactor))
     1970                return false;
     1971            if (!read(otherPrimeInfos[i].factorCRTExponent))
     1972                return false;
     1973            if (!read(otherPrimeInfos[i].factorCRTCoefficient))
     1974                return false;
     1975        }
     1976
     1977        auto keyData = CryptoKeyDataRSAComponents::createPrivateWithAdditionalData(modulus, exponent, privateExponent, firstPrimeInfo, secondPrimeInfo, otherPrimeInfos);
     1978        auto key = CryptoKeyRSA::create(algorithm, *keyData, extractable, usages);
     1979        if (isRestrictedToHash)
     1980            key->restrictToHash(hash);
     1981        result = std::move(key);
     1982        return true;
     1983    }
     1984
     1985    bool readCryptoKey(JSValue& cryptoKey)
     1986    {
     1987        int32_t extractable;
     1988        if (!read(extractable))
     1989            return false;
     1990
     1991        uint32_t usagesCount;
     1992        if (!read(usagesCount))
     1993            return false;
     1994
     1995        CryptoKeyUsage usages = 0;
     1996        for (uint32_t i = 0; i < usagesCount; ++i) {
     1997            CryptoKeyUsageTag usage;
     1998            if (!read(usage))
     1999                return false;
     2000            switch (usage) {
     2001            case CryptoKeyUsageTag::Encrypt:
     2002                usages |= CryptoKeyUsageEncrypt;
     2003                break;
     2004            case CryptoKeyUsageTag::Decrypt:
     2005                usages |= CryptoKeyUsageDecrypt;
     2006                break;
     2007            case CryptoKeyUsageTag::Sign:
     2008                usages |= CryptoKeyUsageSign;
     2009                break;
     2010            case CryptoKeyUsageTag::Verify:
     2011                usages |= CryptoKeyUsageVerify;
     2012                break;
     2013            case CryptoKeyUsageTag::DeriveKey:
     2014                usages |= CryptoKeyUsageDeriveKey;
     2015                break;
     2016            case CryptoKeyUsageTag::DeriveBits:
     2017                usages |= CryptoKeyUsageDeriveBits;
     2018                break;
     2019            case CryptoKeyUsageTag::WrapKey:
     2020                usages |= CryptoKeyUsageWrapKey;
     2021                break;
     2022            case CryptoKeyUsageTag::UnwrapKey:
     2023                usages |= CryptoKeyUsageUnwrapKey;
     2024                break;
     2025            }
     2026        }
     2027
     2028        CryptoKeyClassSubtag cryptoKeyClass;
     2029        if (!read(cryptoKeyClass))
     2030            return false;
     2031        RefPtr<CryptoKey> result;
     2032        switch (cryptoKeyClass) {
     2033        case CryptoKeyClassSubtag::HMAC:
     2034            if (!readHMACKey(extractable, usages, result))
     2035                return false;
     2036            break;
     2037        case CryptoKeyClassSubtag::AES:
     2038            if (!readAESKey(extractable, usages, result))
     2039                return false;
     2040            break;
     2041        case CryptoKeyClassSubtag::RSA:
     2042            if (!readRSAKey(extractable, usages, result))
     2043                return false;
     2044            break;
     2045        }
     2046        cryptoKey = getJSValue(result.get());
     2047        return true;
     2048    }
     2049#endif
     2050
    14562051    template<class T>
    14572052    JSValue getJSValue(T* nativeObj)
     
    16592254            return arrayBufferView;
    16602255        }
     2256#if ENABLE(SUBTLE_CRYPTO)
     2257        case CryptoKeyTag: {
     2258            JSValue cryptoKey;
     2259            if (!readCryptoKey(cryptoKey)) {
     2260                fail();
     2261                return JSValue();
     2262            }
     2263            m_gcBuffer.append(cryptoKey);
     2264            return cryptoKey;
     2265        }
     2266#endif
    16612267        default:
    16622268            m_ptr--; // Push the tag back
  • trunk/Source/WebCore/crypto/CryptoAlgorithmIdentifier.h

    r159966 r160455  
    4343    AES_GCM,
    4444    AES_CFB,
     45    AES_KW,
    4546    HMAC,
    4647    DH,
     
    5253    CONCAT,
    5354    HKDF_CTR,
    54     PBKDF2,
    55     AES_KW // Not yet standardized.
     55    PBKDF2
    5656};
    5757
  • trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.cpp

    r158396 r160455  
    3636CryptoAlgorithmRegistry& CryptoAlgorithmRegistry::shared()
    3737{
    38     ASSERT(isMainThread());
    3938    DEFINE_STATIC_LOCAL(CryptoAlgorithmRegistry, registry, ());
    4039    return registry;
     40}
     41
     42static Mutex& registryMutex()
     43{
     44    AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
     45    return mutex;
    4146}
    4247
     
    5156        return false;
    5257
    53     auto iter = m_nameToIdentifierMap.find(name.lower());
     58    MutexLocker lock(registryMutex());
     59
     60    auto iter = m_nameToIdentifierMap.find(name.isolatedCopy().lower());
    5461    if (iter == m_nameToIdentifierMap.end())
    5562        return false;
     
    6168String CryptoAlgorithmRegistry::nameForIdentifier(CryptoAlgorithmIdentifier identifier)
    6269{
    63     return m_identifierToNameMap.get(static_cast<unsigned>(identifier));
     70    MutexLocker lock(registryMutex());
     71
     72    return m_identifierToNameMap.get(static_cast<unsigned>(identifier)).isolatedCopy();
    6473}
    6574
    6675std::unique_ptr<CryptoAlgorithm> CryptoAlgorithmRegistry::create(CryptoAlgorithmIdentifier identifier)
    6776{
     77    MutexLocker lock(registryMutex());
     78
    6879    auto iter = m_identifierToConstructorMap.find(static_cast<unsigned>(identifier));
    6980    if (iter == m_identifierToConstructorMap.end())
     
    7687{
    7788    ASSERT(name == name.lower());
     89
     90    MutexLocker lock(registryMutex());
    7891
    7992    bool added = m_nameToIdentifierMap.add(name, identifier).isNewEntry;
  • trunk/Source/WebCore/crypto/CryptoKey.idl

    r159068 r160455  
    4545    GenerateIsReachable=Impl,
    4646    InterfaceName=Key,
     47    JSNoStaticTables,
    4748    NoInterfaceObject,
    4849    SkipVTableValidation
  • trunk/Source/WebCore/crypto/keys/CryptoKeyAES.cpp

    r159966 r160455  
    4040    , m_key(key)
    4141{
    42     ASSERT(algorithm == CryptoAlgorithmIdentifier::AES_CTR
     42    ASSERT(isValidAESAlgorithm(algorithm));
     43}
     44
     45CryptoKeyAES::~CryptoKeyAES()
     46{
     47}
     48
     49bool CryptoKeyAES::isValidAESAlgorithm(CryptoAlgorithmIdentifier algorithm)
     50{
     51    return algorithm == CryptoAlgorithmIdentifier::AES_CTR
    4352        || algorithm == CryptoAlgorithmIdentifier::AES_CBC
    4453        || algorithm == CryptoAlgorithmIdentifier::AES_CMAC
    4554        || algorithm == CryptoAlgorithmIdentifier::AES_GCM
    4655        || algorithm == CryptoAlgorithmIdentifier::AES_CFB
    47         || algorithm == CryptoAlgorithmIdentifier::AES_KW);
    48 }
    49 
    50 CryptoKeyAES::~CryptoKeyAES()
    51 {
     56        || algorithm == CryptoAlgorithmIdentifier::AES_KW;
    5257}
    5358
  • trunk/Source/WebCore/crypto/keys/CryptoKeyAES.h

    r159390 r160455  
    4343    virtual ~CryptoKeyAES();
    4444
     45    static bool isValidAESAlgorithm(CryptoAlgorithmIdentifier);
     46
    4547    static PassRefPtr<CryptoKeyAES> generate(CryptoAlgorithmIdentifier, size_t lengthBits, bool extractable, CryptoKeyUsage);
    4648
Note: See TracChangeset for help on using the changeset viewer.