Changeset 159310 in webkit


Ignore:
Timestamp:
Nov 14, 2013, 1:44:25 PM (12 years ago)
Author:
ap@apple.com
Message:

Implement raw format for WebCrypto key export
https://bugs.webkit.org/show_bug.cgi?id=124376

Reviewed by Anders Carlsson.

Source/WebCore:

Tests: crypto/subtle/aes-export-key.html

crypto/subtle/hmac-export-key.html

A CryptoKey just exports its native CryptoKeyData, which will also work nicely for
JWK format soon. For spki and pkcs8, we'll need to figure out the best way to
utilize platform library support for ASN.1, but we are not there yet.

  • bindings/js/JSSubtleCryptoCustom.cpp:

(WebCore::JSSubtleCrypto::exportKey):

  • crypto/CryptoKey.h:
  • crypto/SubtleCrypto.idl:
  • crypto/keys/CryptoKeyAES.cpp:

(WebCore::CryptoKeyAES::exportData):

  • crypto/keys/CryptoKeyAES.h:
  • crypto/keys/CryptoKeyHMAC.cpp:

(WebCore::CryptoKeyHMAC::exportData):

  • crypto/keys/CryptoKeyHMAC.h:
  • crypto/keys/CryptoKeyRSA.h:
  • crypto/mac/CryptoKeyRSAMac.cpp:

(WebCore::CryptoKeyRSA::exportData):
Added a dummy implementation for RSA.

LayoutTests:

  • crypto/subtle/aes-export-key-expected.txt: Added.
  • crypto/subtle/aes-export-key.html: Added.
  • crypto/subtle/hmac-export-key-expected.txt: Added.
  • crypto/subtle/hmac-export-key.html: Added.
Location:
trunk
Files:
4 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r159307 r159310  
     12013-11-14  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Implement raw format for WebCrypto key export
     4        https://bugs.webkit.org/show_bug.cgi?id=124376
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * crypto/subtle/aes-export-key-expected.txt: Added.
     9        * crypto/subtle/aes-export-key.html: Added.
     10        * crypto/subtle/hmac-export-key-expected.txt: Added.
     11        * crypto/subtle/hmac-export-key.html: Added.
     12
    1132013-11-14  Bear Travis  <betravis@adobe.com>
    214
  • trunk/Source/WebCore/ChangeLog

    r159308 r159310  
     12013-11-14  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Implement raw format for WebCrypto key export
     4        https://bugs.webkit.org/show_bug.cgi?id=124376
     5
     6        Reviewed by Anders Carlsson.
     7
     8        Tests: crypto/subtle/aes-export-key.html
     9               crypto/subtle/hmac-export-key.html
     10
     11        A CryptoKey just exports its native CryptoKeyData, which will also work nicely for
     12        JWK format soon. For spki and pkcs8, we'll need to figure out the best way to
     13        utilize platform library support for ASN.1, but we are not there yet.
     14
     15        * bindings/js/JSSubtleCryptoCustom.cpp:
     16        (WebCore::JSSubtleCrypto::exportKey):
     17        * crypto/CryptoKey.h:
     18        * crypto/SubtleCrypto.idl:
     19        * crypto/keys/CryptoKeyAES.cpp:
     20        (WebCore::CryptoKeyAES::exportData):
     21        * crypto/keys/CryptoKeyAES.h:
     22        * crypto/keys/CryptoKeyHMAC.cpp:
     23        (WebCore::CryptoKeyHMAC::exportData):
     24        * crypto/keys/CryptoKeyHMAC.h:
     25
     26        * crypto/keys/CryptoKeyRSA.h:
     27        * crypto/mac/CryptoKeyRSAMac.cpp:
     28        (WebCore::CryptoKeyRSA::exportData):
     29        Added a dummy implementation for RSA.
     30
    1312013-11-14  Joseph Pecoraro  <pecoraro@apple.com>
    232
  • trunk/Source/WebCore/bindings/js/JSSubtleCryptoCustom.cpp

    r158943 r159310  
    3333#include "CryptoAlgorithmRegistry.h"
    3434#include "CryptoKeyData.h"
     35#include "CryptoKeyDataOctetSequence.h"
     36#include "CryptoKeyDataRSAComponents.h"
    3537#include "CryptoKeySerializationRaw.h"
    3638#include "Document.h"
     
    507509}
    508510
     511JSValue JSSubtleCrypto::exportKey(JSC::ExecState* exec)
     512{
     513    if (exec->argumentCount() < 2)
     514        return exec->vm().throwException(exec, createNotEnoughArgumentsError(exec));
     515
     516    CryptoKeyFormat keyFormat;
     517    if (!cryptoKeyFormatFromJSValue(exec, exec->argument(0), keyFormat)) {
     518        ASSERT(exec->hadException());
     519        return jsUndefined();
     520    }
     521
     522    RefPtr<CryptoKey> key = toCryptoKey(exec->uncheckedArgument(1));
     523    if (!key)
     524        return throwTypeError(exec);
     525
     526    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
     527    auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     528
     529    if (!key->extractable()) {
     530        m_impl->document()->addConsoleMessage(JSMessageSource, ErrorMessageLevel, "Key is not extractable");
     531        promiseWrapper->reject(nullptr);
     532        return promise;
     533    }
     534
     535    std::unique_ptr<CryptoKeyData> keyData = key->exportData();
     536    if (!keyData) {
     537        // FIXME: Shouldn't happen once all key types implement exportData().
     538        promiseWrapper->reject(nullptr);
     539        return promise;
     540    }
     541
     542    switch (keyFormat) {
     543    case CryptoKeyFormat::Raw:
     544        if (isCryptoKeyDataOctetSequence(*keyData)) {
     545            Vector<unsigned char> result;
     546            result.appendVector(toCryptoKeyDataOctetSequence(*keyData).octetSequence());
     547            promiseWrapper->fulfill(result);
     548        } else {
     549            m_impl->document()->addConsoleMessage(JSMessageSource, ErrorMessageLevel, "Key cannot be exported to raw format");
     550            promiseWrapper->reject(nullptr);
     551        }
     552        break;
     553    default:
     554        throwTypeError(exec, "Unsupported key format");
     555        return jsUndefined();
     556    }
     557
     558    return promise;
     559}
     560
    509561} // namespace WebCore
    510562
  • trunk/Source/WebCore/crypto/CryptoKey.h

    r159213 r159310  
    3939
    4040class CryptoAlgorithmDescriptionBuilder;
     41class CryptoKeyData;
    4142
    4243ENUM_CLASS(CryptoKeyClass) {
     
    6061    bool allows(CryptoKeyUsage usage) const { return usage == (m_usages & usage); }
    6162
     63    virtual std::unique_ptr<CryptoKeyData> exportData() const = 0;
     64
    6265    static Vector<char> randomData(size_t);
    6366
  • trunk/Source/WebCore/crypto/SubtleCrypto.idl

    r159061 r159310  
    3737    [Custom] Promise generateKey(AlgorithmIdentifier algorithm, optional boolean extractable, optional KeyUsage[] keyUsages);
    3838    [Custom] Promise importKey(KeyFormat format, CryptoOperationData keyData, AlgorithmIdentifier? algorithm, optional boolean extractable, optional KeyUsage[] keyUsages);
     39    [Custom] Promise exportKey(KeyFormat format, Key key);
    3940};
  • trunk/Source/WebCore/crypto/keys/CryptoKeyAES.cpp

    r158945 r159310  
    3131#include "CryptoAlgorithmDescriptionBuilder.h"
    3232#include "CryptoAlgorithmRegistry.h"
     33#include "CryptoKeyDataOctetSequence.h"
    3334#include <wtf/text/WTFString.h>
    3435
     
    6364}
    6465
     66std::unique_ptr<CryptoKeyData> CryptoKeyAES::exportData() const
     67{
     68    ASSERT(extractable());
     69    return CryptoKeyDataOctetSequence::create(m_key);
     70}
     71
    6572} // namespace WebCore
    6673
  • trunk/Source/WebCore/crypto/keys/CryptoKeyAES.h

    r159213 r159310  
    4949    const Vector<char>& key() const { return m_key; }
    5050
    51     virtual void buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder&) const OVERRIDE;
    52 
    5351private:
    5452    CryptoKeyAES(CryptoAlgorithmIdentifier, const Vector<char>& key, bool extractable, CryptoKeyUsage);
     53
     54    virtual void buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder&) const OVERRIDE;
     55    virtual std::unique_ptr<CryptoKeyData> exportData() const OVERRIDE;
    5556
    5657    Vector<char> m_key;
  • trunk/Source/WebCore/crypto/keys/CryptoKeyHMAC.cpp

    r158582 r159310  
    3131#include "CryptoAlgorithmDescriptionBuilder.h"
    3232#include "CryptoAlgorithmRegistry.h"
     33#include "CryptoKeyDataOctetSequence.h"
    3334#include <wtf/text/WTFString.h>
    3435
     
    7879}
    7980
     81std::unique_ptr<CryptoKeyData> CryptoKeyHMAC::exportData() const
     82{
     83    ASSERT(extractable());
     84    return CryptoKeyDataOctetSequence::create(m_key);
     85}
     86
    8087} // namespace WebCore
    8188
  • trunk/Source/WebCore/crypto/keys/CryptoKeyHMAC.h

    r159213 r159310  
    4949    const Vector<char>& key() const { return m_key; }
    5050
    51     virtual void buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder&) const OVERRIDE;
    52 
    5351private:
    5452    CryptoKeyHMAC(const Vector<char>& key, CryptoAlgorithmIdentifier hash, bool extractable, CryptoKeyUsage);
     53
     54    virtual void buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder&) const OVERRIDE;
     55    virtual std::unique_ptr<CryptoKeyData> exportData() const OVERRIDE;
    5556
    5657    CryptoAlgorithmIdentifier m_hash;
  • trunk/Source/WebCore/crypto/keys/CryptoKeyRSA.h

    r159213 r159310  
    5959    PlatformRSAKey platformKey() const { return m_platformKey; }
    6060
    61     virtual void buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder&) const OVERRIDE;
    62 
    6361private:
    6462    CryptoKeyRSA(CryptoAlgorithmIdentifier, CryptoKeyType, PlatformRSAKey, bool extractable, CryptoKeyUsage);
     63
     64    virtual void buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder&) const OVERRIDE;
     65    virtual std::unique_ptr<CryptoKeyData> exportData() const OVERRIDE;
    6566
    6667    PlatformRSAKey m_platformKey;
  • trunk/Source/WebCore/crypto/mac/CryptoKeyRSAMac.cpp

    r159211 r159310  
    144144}
    145145
     146std::unique_ptr<CryptoKeyData> CryptoKeyRSA::exportData() const
     147{
     148    // Not implemented yet.
     149    ASSERT(extractable());
     150    return nullptr;
     151}
     152
    146153static bool bigIntegerToUInt32(const Vector<char>& bigInteger, uint32_t& result)
    147154{
Note: See TracChangeset for help on using the changeset viewer.