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

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

Issue 22849026: WebCrypto: Check for HmacKeyParams when seeing if a Key can be used for an Algorithm. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase onto master Created 7 years, 4 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
« no previous file with comments | « Source/modules/crypto/Key.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 23 matching lines...) Expand all
34 #include "V8Key.h" // Must precede ScriptPromiseResolver.h 34 #include "V8Key.h" // Must precede ScriptPromiseResolver.h
35 #include "bindings/v8/ExceptionState.h" 35 #include "bindings/v8/ExceptionState.h"
36 #include "bindings/v8/custom/V8ArrayBufferCustom.h" // Must precede ScriptPromis eResolver.h 36 #include "bindings/v8/custom/V8ArrayBufferCustom.h" // Must precede ScriptPromis eResolver.h
37 #include "bindings/v8/ScriptPromiseResolver.h" 37 #include "bindings/v8/ScriptPromiseResolver.h"
38 #include "core/dom/ExceptionCode.h" 38 #include "core/dom/ExceptionCode.h"
39 #include "modules/crypto/Key.h" 39 #include "modules/crypto/Key.h"
40 #include "modules/crypto/NormalizeAlgorithm.h" 40 #include "modules/crypto/NormalizeAlgorithm.h"
41 #include "public/platform/Platform.h" 41 #include "public/platform/Platform.h"
42 #include "public/platform/WebArrayBuffer.h" 42 #include "public/platform/WebArrayBuffer.h"
43 #include "public/platform/WebCrypto.h" 43 #include "public/platform/WebCrypto.h"
44 #include "public/platform/WebCryptoAlgorithmParams.h" 44 #include "public/platform/WebCryptoAlgorithm.h"
45 #include "wtf/ArrayBufferView.h" 45 #include "wtf/ArrayBufferView.h"
46 46
47 namespace WebCore { 47 namespace WebCore {
48 48
49 // FIXME: asynchronous completion of CryptoResult. Need to re-enter the 49 // FIXME: asynchronous completion of CryptoResult. Need to re-enter the
50 // v8::Context before trying to fulfill the promise, and enable test. 50 // v8::Context before trying to fulfill the promise, and enable test.
51 51
52 namespace { 52 namespace {
53 53
54 class CryptoResult : public WebKit::WebCryptoResultPrivate, public ThreadSafeRef Counted<CryptoResult> { 54 class CryptoResult : public WebKit::WebCryptoResultPrivate, public ThreadSafeRef Counted<CryptoResult> {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 void finish() 110 void finish()
111 { 111 {
112 ASSERT(!m_finished); 112 ASSERT(!m_finished);
113 m_finished = true; 113 m_finished = true;
114 } 114 }
115 115
116 RefPtr<ScriptPromiseResolver> m_promiseResolver; 116 RefPtr<ScriptPromiseResolver> m_promiseResolver;
117 bool m_finished; 117 bool m_finished;
118 }; 118 };
119 119
120 WebKit::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation)
121 {
122 switch (operation) {
123 case Encrypt:
124 return WebKit::WebCryptoKeyUsageEncrypt;
125 case Decrypt:
126 return WebKit::WebCryptoKeyUsageDecrypt;
127 case Sign:
128 return WebKit::WebCryptoKeyUsageSign;
129 case Verify:
130 return WebKit::WebCryptoKeyUsageVerify;
131 case DeriveKey:
132 return WebKit::WebCryptoKeyUsageDeriveKey;
133 case WrapKey:
134 return WebKit::WebCryptoKeyUsageWrapKey;
135 case UnwrapKey:
136 return WebKit::WebCryptoKeyUsageUnwrapKey;
137 case Digest:
138 case GenerateKey:
139 case ImportKey:
140 case NumberOfAlgorithmOperations:
141 break;
142 }
143
144 ASSERT_NOT_REACHED();
145 return 0;
146 }
147
148 bool keyCanBeUsedForAlgorithm(const WebKit::WebCryptoKey& key, const WebKit::Web CryptoAlgorithm& algorithm, AlgorithmOperation op)
149 {
150 if (!(key.usages() & toKeyUsage(op)))
151 return false;
152
153 if (key.algorithm().id() != algorithm.id())
154 return false;
155
156 if (key.algorithm().paramsType() == WebKit::WebCryptoAlgorithmParamsTypeNone )
157 return true;
158
159 // Verify that the algorithm-specific parameters for the key conform to the
160 // algorithm.
161
162 if (key.algorithm().paramsType() == WebKit::WebCryptoAlgorithmParamsTypeHmac Params) {
163 return key.algorithm().hmacParams()->hash().id() == algorithm.hmacParams ()->hash().id();
164 }
165
166 ASSERT_NOT_REACHED();
167 return false;
168 }
169
170 ScriptObject startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Algo rithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataB uffer, ExceptionState& es) 120 ScriptObject startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Algo rithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataB uffer, ExceptionState& es)
171 { 121 {
172 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); 122 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto();
173 if (!platformCrypto) { 123 if (!platformCrypto) {
174 es.throwDOMException(NotSupportedError); 124 es.throwDOMException(NotSupportedError);
175 return ScriptObject(); 125 return ScriptObject();
176 } 126 }
177 127
178 WebKit::WebCryptoAlgorithm algorithm; 128 WebKit::WebCryptoAlgorithm algorithm;
179 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) 129 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es))
180 return ScriptObject(); 130 return ScriptObject();
181 131
182 // All operations other than Digest require a valid Key. 132 // All operations other than Digest require a valid Key.
183 if (operationType != Digest) { 133 if (operationType != Digest) {
184 if (!key) { 134 if (!key) {
185 es.throwTypeError(); 135 es.throwTypeError();
186 return ScriptObject(); 136 return ScriptObject();
187 } 137 }
188 138
189 if (!keyCanBeUsedForAlgorithm(key->key(), algorithm, operationType)) { 139 if (!key->canBeUsedForAlgorithm(algorithm, operationType, es))
190 es.throwDOMException(NotSupportedError);
191 return ScriptObject(); 140 return ScriptObject();
192 }
193 } 141 }
194 142
195 // Only Verify takes a signature. 143 // Only Verify takes a signature.
196 if (operationType == Verify && !signature) { 144 if (operationType == Verify && !signature) {
197 es.throwTypeError(); 145 es.throwTypeError();
198 return ScriptObject(); 146 return ScriptObject();
199 } 147 }
200 148
201 if (!dataBuffer) { 149 if (!dataBuffer) {
202 es.throwTypeError(); 150 es.throwTypeError();
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 return ScriptObject(); 265 return ScriptObject();
318 266
319 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress()); 267 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress());
320 268
321 RefPtr<CryptoResult> result = CryptoResult::create(); 269 RefPtr<CryptoResult> result = CryptoResult::create();
322 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor ithm, extractable, keyUsages, result->result()); 270 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor ithm, extractable, keyUsages, result->result());
323 return result->promise(); 271 return result->promise();
324 } 272 }
325 273
326 } // namespace WebCore 274 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/Key.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698