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

Side by Side Diff: Source/modules/crypto/Key.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.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | 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 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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"
35 #include "core/dom/ExceptionCode.h"
34 #include "modules/crypto/Algorithm.h" 36 #include "modules/crypto/Algorithm.h"
37 #include "public/platform/WebCryptoAlgorithmParams.h"
35 38
36 namespace WebCore { 39 namespace WebCore {
37 40
38 namespace { 41 namespace {
39 42
40 const char* keyTypeToString(WebKit::WebCryptoKeyType type) 43 const char* keyTypeToString(WebKit::WebCryptoKeyType type)
41 { 44 {
42 switch (type) { 45 switch (type) {
43 case WebKit::WebCryptoKeyTypeSecret: 46 case WebKit::WebCryptoKeyTypeSecret:
44 return "secret"; 47 return "secret";
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 83
81 WebKit::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString) 84 WebKit::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
82 { 85 {
83 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) { 86 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
84 if (keyUsageMappings[i].name == usageString) 87 if (keyUsageMappings[i].name == usageString)
85 return keyUsageMappings[i].value; 88 return keyUsageMappings[i].value;
86 } 89 }
87 return 0; 90 return 0;
88 } 91 }
89 92
93 WebKit::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation)
94 {
95 switch (operation) {
96 case Encrypt:
97 return WebKit::WebCryptoKeyUsageEncrypt;
98 case Decrypt:
99 return WebKit::WebCryptoKeyUsageDecrypt;
100 case Sign:
101 return WebKit::WebCryptoKeyUsageSign;
102 case Verify:
103 return WebKit::WebCryptoKeyUsageVerify;
104 case DeriveKey:
105 return WebKit::WebCryptoKeyUsageDeriveKey;
106 case WrapKey:
107 return WebKit::WebCryptoKeyUsageWrapKey;
108 case UnwrapKey:
109 return WebKit::WebCryptoKeyUsageUnwrapKey;
110 case Digest:
111 case GenerateKey:
112 case ImportKey:
113 case NumberOfAlgorithmOperations:
114 break;
115 }
116
117 ASSERT_NOT_REACHED();
118 return 0;
119 }
120
121 bool getHmacHashId(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::WebCrypt oAlgorithmId& hashId)
122 {
123 if (algorithm.hmacParams()) {
124 hashId = algorithm.hmacParams()->hash().id();
125 return true;
126 }
127 if (algorithm.hmacKeyParams()) {
128 hashId = algorithm.hmacKeyParams()->hash().id();
129 return true;
130 }
131 return false;
132 }
133
90 } // namespace 134 } // namespace
91 135
92 Key::~Key() 136 Key::~Key()
93 { 137 {
94 } 138 }
95 139
96 Key::Key(const WebKit::WebCryptoKey& key) 140 Key::Key(const WebKit::WebCryptoKey& key)
97 : m_key(key) 141 : m_key(key)
98 { 142 {
99 ScriptWrappable::init(this); 143 ScriptWrappable::init(this);
(...skipping 24 matching lines...) Expand all
124 { 168 {
125 Vector<String> result; 169 Vector<String> result;
126 for (int i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) { 170 for (int i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
127 WebKit::WebCryptoKeyUsage usage = keyUsageMappings[i].value; 171 WebKit::WebCryptoKeyUsage usage = keyUsageMappings[i].value;
128 if (m_key.usages() & usage) 172 if (m_key.usages() & usage)
129 result.append(keyUsageToString(usage)); 173 result.append(keyUsageToString(usage));
130 } 174 }
131 return result; 175 return result;
132 } 176 }
133 177
178 bool Key::canBeUsedForAlgorithm(const WebKit::WebCryptoAlgorithm& algorithm, Alg orithmOperation op, ExceptionState& es) const
179 {
180 if (!(m_key.usages() & toKeyUsage(op))) {
181 es.throwDOMException(NotSupportedError, "key.usages does not permit this operation");
182 return false;
183 }
184
185 if (m_key.algorithm().id() != algorithm.id()) {
186 es.throwDOMException(NotSupportedError, "key.algorithm does not match th at of operation");
187 return false;
188 }
189
190 // Verify that the algorithm-specific parameters for the key conform to the
191 // algorithm.
192
193 if (m_key.algorithm().id() == WebKit::WebCryptoAlgorithmIdHmac) {
194 WebKit::WebCryptoAlgorithmId keyHash;
195 WebKit::WebCryptoAlgorithmId algorithmHash;
196 if (!getHmacHashId(m_key.algorithm(), keyHash) || !getHmacHashId(algorit hm, algorithmHash) || keyHash != algorithmHash) {
197 es.throwDOMException(NotSupportedError, "key.algorithm does not matc h that of operation (HMAC's hash differs)");
198 return false;
199 }
200 }
201
202 return true;
203 }
204
134 bool Key::parseFormat(const String& formatString, WebKit::WebCryptoKeyFormat& fo rmat) 205 bool Key::parseFormat(const String& formatString, WebKit::WebCryptoKeyFormat& fo rmat)
135 { 206 {
136 // There are few enough values that testing serially is fast enough. 207 // There are few enough values that testing serially is fast enough.
137 if (formatString == "raw") { 208 if (formatString == "raw") {
138 format = WebKit::WebCryptoKeyFormatRaw; 209 format = WebKit::WebCryptoKeyFormatRaw;
139 return true; 210 return true;
140 } 211 }
141 if (formatString == "pkcs8") { 212 if (formatString == "pkcs8") {
142 format = WebKit::WebCryptoKeyFormatPkcs8; 213 format = WebKit::WebCryptoKeyFormatPkcs8;
143 return true; 214 return true;
(...skipping 16 matching lines...) Expand all
160 for (size_t i = 0; i < usages.size(); ++i) { 231 for (size_t i = 0; i < usages.size(); ++i) {
161 WebKit::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]); 232 WebKit::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
162 if (!usage) 233 if (!usage)
163 return false; 234 return false;
164 mask |= usage; 235 mask |= usage;
165 } 236 }
166 return true; 237 return true;
167 } 238 }
168 239
169 } // namespace WebCore 240 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/Key.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698