• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/v8/ScriptPromiseResolverWithContext.h"
35 #include "bindings/v8/ScriptState.h"
36 #include "core/dom/ContextLifecycleObserver.h"
37 #include "core/dom/DOMError.h"
38 #include "core/dom/DOMException.h"
39 #include "core/dom/ExecutionContext.h"
40 #include "modules/crypto/Key.h"
41 #include "modules/crypto/NormalizeAlgorithm.h"
42 #include "public/platform/Platform.h"
43 #include "public/platform/WebArrayBuffer.h"
44 #include "public/platform/WebCryptoAlgorithm.h"
45 #include "wtf/ArrayBufferView.h"
46 
47 namespace WebCore {
48 
49 class CryptoResultImpl::WeakResolver : public ScriptPromiseResolverWithContext {
50 public:
create(ScriptState * scriptState,CryptoResultImpl * result)51     static WeakPtr<ScriptPromiseResolverWithContext> create(ScriptState* scriptState, CryptoResultImpl* result)
52     {
53         RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState, result));
54         p->suspendIfNeeded();
55         p->keepAliveWhilePending();
56         return p->m_weakPtrFactory.createWeakPtr();
57     }
58 
~WeakResolver()59     virtual ~WeakResolver()
60     {
61         m_result->cancel();
62     }
63 
64 private:
WeakResolver(ScriptState * scriptState,CryptoResultImpl * result)65     WeakResolver(ScriptState* scriptState, CryptoResultImpl* result)
66         : ScriptPromiseResolverWithContext(scriptState)
67         , m_weakPtrFactory(this)
68         , m_result(result) { }
69     WeakPtrFactory<ScriptPromiseResolverWithContext> m_weakPtrFactory;
70     RefPtr<CryptoResultImpl> m_result;
71 };
72 
webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType)73 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType)
74 {
75     switch (errorType) {
76     case blink::WebCryptoErrorTypeNotSupported:
77         return NotSupportedError;
78     case blink::WebCryptoErrorTypeSyntax:
79         return SyntaxError;
80     case blink::WebCryptoErrorTypeInvalidState:
81         return InvalidStateError;
82     case blink::WebCryptoErrorTypeInvalidAccess:
83         return InvalidAccessError;
84     case blink::WebCryptoErrorTypeUnknown:
85         return UnknownError;
86     case blink::WebCryptoErrorTypeData:
87         return DataError;
88     case blink::WebCryptoErrorTypeOperation:
89         return OperationError;
90     case blink::WebCryptoErrorTypeType:
91         // FIXME: This should construct a TypeError instead. For now do
92         //        something to facilitate refactor, but this will need to be
93         //        revisited.
94         return DataError;
95     }
96 
97     ASSERT_NOT_REACHED();
98     return 0;
99 }
100 
~CryptoResultImpl()101 CryptoResultImpl::~CryptoResultImpl()
102 {
103 }
104 
create(ScriptState * scriptState)105 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState)
106 {
107     return adoptRef(new CryptoResultImpl(scriptState));
108 }
109 
completeWithError(blink::WebCryptoErrorType errorType,const blink::WebString & errorDetails)110 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
111 {
112     if (m_resolver)
113         m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(errorType), errorDetails));
114 }
115 
completeWithBuffer(const blink::WebArrayBuffer & buffer)116 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer)
117 {
118     if (m_resolver)
119         m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
120 }
121 
completeWithJson(const char * utf8Data,unsigned length)122 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
123 {
124     if (m_resolver) {
125         ScriptPromiseResolverWithContext* resolver = m_resolver.get();
126         ScriptState* scriptState = resolver->scriptState();
127         ScriptState::Scope scope(scriptState);
128 
129         v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState->isolate(), utf8Data, v8::String::kInternalizedString, length);
130 
131         v8::TryCatch exceptionCatcher;
132         v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString);
133         if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) {
134             ASSERT_NOT_REACHED();
135             resolver->reject(DOMException::create(OperationError, "Failed inflating JWK JSON to object"));
136         } else {
137             resolver->resolve(jsonDictionary);
138         }
139     }
140 }
141 
completeWithBoolean(bool b)142 void CryptoResultImpl::completeWithBoolean(bool b)
143 {
144     if (m_resolver)
145         m_resolver->resolve(b);
146 }
147 
completeWithKey(const blink::WebCryptoKey & key)148 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key)
149 {
150     if (m_resolver)
151         m_resolver->resolve(Key::create(key));
152 }
153 
completeWithKeyPair(const blink::WebCryptoKey & publicKey,const blink::WebCryptoKey & privateKey)154 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
155 {
156     if (m_resolver) {
157         ScriptState* scriptState = m_resolver->scriptState();
158         ScriptState::Scope scope(scriptState);
159 
160         // FIXME: Use Dictionary instead, to limit amount of direct v8 access used from WebCore.
161         v8::Handle<v8::Object> keyPair = v8::Object::New(scriptState->isolate());
162 
163         v8::Handle<v8::Value> publicKeyValue = toV8NoInline(Key::create(publicKey), scriptState->context()->Global(), scriptState->isolate());
164         v8::Handle<v8::Value> privateKeyValue = toV8NoInline(Key::create(privateKey), scriptState->context()->Global(), scriptState->isolate());
165 
166         keyPair->Set(v8::String::NewFromUtf8(scriptState->isolate(), "publicKey"), publicKeyValue);
167         keyPair->Set(v8::String::NewFromUtf8(scriptState->isolate(), "privateKey"), privateKeyValue);
168 
169         m_resolver->resolve(v8::Handle<v8::Value>(keyPair));
170     }
171 }
172 
cancelled() const173 bool CryptoResultImpl::cancelled() const
174 {
175     return acquireLoad(&m_cancelled);
176 }
177 
cancel()178 void CryptoResultImpl::cancel()
179 {
180     releaseStore(&m_cancelled, 1);
181 }
182 
CryptoResultImpl(ScriptState * scriptState)183 CryptoResultImpl::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 
promise()191 ScriptPromise CryptoResultImpl::promise()
192 {
193     return m_resolver ? m_resolver->promise() : ScriptPromise();
194 }
195 
196 } // namespace WebCore
197