1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "modules/crypto/CryptoResultImpl.h"
33
34#include "bindings/core/v8/Dictionary.h"
35#include "bindings/core/v8/ScriptPromiseResolver.h"
36#include "bindings/core/v8/ScriptState.h"
37#include "core/dom/ContextLifecycleObserver.h"
38#include "core/dom/DOMError.h"
39#include "core/dom/DOMException.h"
40#include "core/dom/ExecutionContext.h"
41#include "modules/crypto/CryptoKey.h"
42#include "modules/crypto/NormalizeAlgorithm.h"
43#include "public/platform/Platform.h"
44#include "public/platform/WebArrayBuffer.h"
45#include "public/platform/WebCryptoAlgorithm.h"
46#include "wtf/ArrayBufferView.h"
47
48namespace blink {
49
50class CryptoResultImpl::WeakResolver : public ScriptPromiseResolver {
51public:
52    static WeakPtr<ScriptPromiseResolver> create(ScriptState* scriptState, CryptoResultImpl* result)
53    {
54        RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState, result));
55        p->suspendIfNeeded();
56        p->keepAliveWhilePending();
57        return p->m_weakPtrFactory.createWeakPtr();
58    }
59
60    virtual ~WeakResolver()
61    {
62        m_result->cancel();
63    }
64
65private:
66    WeakResolver(ScriptState* scriptState, CryptoResultImpl* result)
67        : ScriptPromiseResolver(scriptState)
68        , m_weakPtrFactory(this)
69        , m_result(result) { }
70    WeakPtrFactory<ScriptPromiseResolver> m_weakPtrFactory;
71    RefPtr<CryptoResultImpl> m_result;
72};
73
74ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType)
75{
76    switch (errorType) {
77    case WebCryptoErrorTypeNotSupported:
78        return NotSupportedError;
79    case WebCryptoErrorTypeSyntax:
80        return SyntaxError;
81    case WebCryptoErrorTypeInvalidState:
82        return InvalidStateError;
83    case WebCryptoErrorTypeInvalidAccess:
84        return InvalidAccessError;
85    case WebCryptoErrorTypeUnknown:
86        return UnknownError;
87    case WebCryptoErrorTypeData:
88        return DataError;
89    case WebCryptoErrorTypeOperation:
90        return OperationError;
91    case WebCryptoErrorTypeType:
92        // FIXME: This should construct a TypeError instead. For now do
93        //        something to facilitate refactor, but this will need to be
94        //        revisited.
95        return DataError;
96    }
97
98    ASSERT_NOT_REACHED();
99    return 0;
100}
101
102CryptoResultImpl::~CryptoResultImpl()
103{
104}
105
106PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState)
107{
108    return adoptRef(new CryptoResultImpl(scriptState));
109}
110
111void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const WebString& errorDetails)
112{
113    if (m_resolver)
114        m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(errorType), errorDetails));
115}
116
117void CryptoResultImpl::completeWithBuffer(const WebArrayBuffer& buffer)
118{
119    if (m_resolver)
120        m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
121}
122
123void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
124{
125    if (m_resolver) {
126        ScriptPromiseResolver* resolver = m_resolver.get();
127        ScriptState* scriptState = resolver->scriptState();
128        ScriptState::Scope scope(scriptState);
129
130        v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState->isolate(), utf8Data, v8::String::kInternalizedString, length);
131
132        v8::TryCatch exceptionCatcher;
133        v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString);
134        if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) {
135            ASSERT_NOT_REACHED();
136            resolver->reject(DOMException::create(OperationError, "Failed inflating JWK JSON to object"));
137        } else {
138            resolver->resolve(jsonDictionary);
139        }
140    }
141}
142
143void CryptoResultImpl::completeWithBoolean(bool b)
144{
145    if (m_resolver)
146        m_resolver->resolve(b);
147}
148
149void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
150{
151    if (m_resolver)
152        m_resolver->resolve(CryptoKey::create(key));
153}
154
155void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
156{
157    if (m_resolver) {
158        ScriptState* scriptState = m_resolver->scriptState();
159        ScriptState::Scope scope(scriptState);
160
161        Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate());
162
163        v8::Handle<v8::Value> publicKeyValue = toV8NoInline(CryptoKey::create(publicKey), scriptState->context()->Global(), scriptState->isolate());
164        v8::Handle<v8::Value> privateKeyValue = toV8NoInline(CryptoKey::create(privateKey), scriptState->context()->Global(), scriptState->isolate());
165
166        keyPair.set("publicKey", publicKeyValue);
167        keyPair.set("privateKey", privateKeyValue);
168
169        m_resolver->resolve(keyPair.v8Value());
170    }
171}
172
173bool CryptoResultImpl::cancelled() const
174{
175    return acquireLoad(&m_cancelled);
176}
177
178void CryptoResultImpl::cancel()
179{
180    releaseStore(&m_cancelled, 1);
181}
182
183CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
184    : m_cancelled(0)
185{
186    // Creating the WeakResolver may return nullptr if active dom objects have
187    // been stopped. And in the process set m_cancelled to 1.
188    m_resolver = WeakResolver::create(scriptState, this);
189}
190
191ScriptPromise CryptoResultImpl::promise()
192{
193    return m_resolver ? m_resolver->promise() : ScriptPromise();
194}
195
196} // namespace blink
197