Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: Source/modules/crypto/Key.cpp

Issue 179353002: [webcrypto] Add the KeyAlgorithm interface. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 15 matching lines...) Expand all
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "modules/crypto/Key.h" 32 #include "modules/crypto/Key.h"
33 33
34 #include "bindings/v8/ExceptionState.h" 34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h" 35 #include "core/dom/ExceptionCode.h"
36 #include "modules/crypto/Algorithm.h" 36 #include "modules/crypto/KeyAlgorithm.h"
37 #include "platform/CryptoResult.h" 37 #include "platform/CryptoResult.h"
38 #include "public/platform/WebCryptoAlgorithmParams.h" 38 #include "public/platform/WebCryptoAlgorithmParams.h"
39 #include "public/platform/WebString.h" 39 #include "public/platform/WebString.h"
40 40
41 namespace WebCore { 41 namespace WebCore {
42 42
43 DEFINE_GC_INFO(Key); 43 DEFINE_GC_INFO(Key);
44 44
45 namespace { 45 namespace {
46 46
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 case Digest: 115 case Digest:
116 case GenerateKey: 116 case GenerateKey:
117 case ImportKey: 117 case ImportKey:
118 break; 118 break;
119 } 119 }
120 120
121 ASSERT_NOT_REACHED(); 121 ASSERT_NOT_REACHED();
122 return 0; 122 return 0;
123 } 123 }
124 124
125 bool getHmacHashId(const blink::WebCryptoAlgorithm& algorithm, blink::WebCryptoA lgorithmId& hashId)
126 {
127 if (algorithm.hmacParams()) {
128 hashId = algorithm.hmacParams()->hash().id();
129 return true;
130 }
131 if (algorithm.hmacKeyParams()) {
132 hashId = algorithm.hmacKeyParams()->hash().id();
133 return true;
134 }
135 return false;
136 }
137
138 } // namespace 125 } // namespace
139 126
140 Key::~Key() 127 Key::~Key()
141 { 128 {
142 } 129 }
143 130
144 Key::Key(const blink::WebCryptoKey& key) 131 Key::Key(const blink::WebCryptoKey& key)
145 : m_key(key) 132 : m_key(key)
146 { 133 {
147 ScriptWrappable::init(this); 134 ScriptWrappable::init(this);
148 } 135 }
149 136
150 String Key::type() const 137 String Key::type() const
151 { 138 {
152 return keyTypeToString(m_key.type()); 139 return keyTypeToString(m_key.type());
153 } 140 }
154 141
155 bool Key::extractable() const 142 bool Key::extractable() const
156 { 143 {
157 return m_key.extractable(); 144 return m_key.extractable();
158 } 145 }
159 146
160 Algorithm* Key::algorithm() 147 KeyAlgorithm* Key::algorithm()
161 { 148 {
162 if (!m_algorithm) 149 if (!m_algorithm)
163 m_algorithm = Algorithm::create(m_key.algorithm()); 150 m_algorithm = KeyAlgorithm::create(m_key.algorithm());
164 return m_algorithm.get(); 151 return m_algorithm.get();
165 } 152 }
166 153
167 // FIXME: This creates a new javascript array each time. What should happen 154 // FIXME: This creates a new javascript array each time. What should happen
168 // instead is return the same (immutable) array. (Javascript callers can 155 // instead is return the same (immutable) array. (Javascript callers can
169 // distinguish this by doing an == test on the arrays and seeing they are 156 // distinguish this by doing an == test on the arrays and seeing they are
170 // different). 157 // different).
171 Vector<String> Key::usages() const 158 Vector<String> Key::usages() const
172 { 159 {
173 Vector<String> result; 160 Vector<String> result;
(...skipping 10 matching lines...) Expand all
184 if (!(m_key.usages() & toKeyUsage(op))) { 171 if (!(m_key.usages() & toKeyUsage(op))) {
185 result->completeWithError("key.usages does not permit this operation"); 172 result->completeWithError("key.usages does not permit this operation");
186 return false; 173 return false;
187 } 174 }
188 175
189 if (m_key.algorithm().id() != algorithm.id()) { 176 if (m_key.algorithm().id() != algorithm.id()) {
190 result->completeWithError("key.algorithm does not match that of operatio n"); 177 result->completeWithError("key.algorithm does not match that of operatio n");
191 return false; 178 return false;
192 } 179 }
193 180
194 // Verify that the algorithm-specific parameters for the key conform to the
195 // algorithm.
196 // FIXME: This is incomplete and not future proof. Operational parameters
197 // should be enumerated when defining new parameters.
198
199 if (m_key.algorithm().id() == blink::WebCryptoAlgorithmIdHmac) {
200 blink::WebCryptoAlgorithmId keyHash;
201 blink::WebCryptoAlgorithmId algorithmHash;
202 if (!getHmacHashId(m_key.algorithm(), keyHash) || !getHmacHashId(algorit hm, algorithmHash) || keyHash != algorithmHash) {
203 result->completeWithError("key.algorithm does not match that of oper ation (HMAC's hash differs)");
204 return false;
205 }
206 }
207
208 return true; 181 return true;
209 } 182 }
210 183
211 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& for mat, CryptoResult* result) 184 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& for mat, CryptoResult* result)
212 { 185 {
213 // There are few enough values that testing serially is fast enough. 186 // There are few enough values that testing serially is fast enough.
214 if (formatString == "raw") { 187 if (formatString == "raw") {
215 format = blink::WebCryptoKeyFormatRaw; 188 format = blink::WebCryptoKeyFormatRaw;
216 return true; 189 return true;
217 } 190 }
(...skipping 27 matching lines...) Expand all
245 } 218 }
246 return true; 219 return true;
247 } 220 }
248 221
249 void Key::trace(Visitor* visitor) 222 void Key::trace(Visitor* visitor)
250 { 223 {
251 visitor->trace(m_algorithm); 224 visitor->trace(m_algorithm);
252 } 225 }
253 226
254 } // namespace WebCore 227 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698