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

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

Issue 267133002: [webcrypto] Allow both ArrayBuffer and ArrayBufferView as inputs to crypto.subtle methods. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 7 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 28 matching lines...) Expand all
39 #include "public/platform/WebCrypto.h" 39 #include "public/platform/WebCrypto.h"
40 #include "public/platform/WebCryptoAlgorithm.h" 40 #include "public/platform/WebCryptoAlgorithm.h"
41 #include "wtf/ArrayBufferView.h" 41 #include "wtf/ArrayBufferView.h"
42 42
43 namespace WebCore { 43 namespace WebCore {
44 44
45 namespace { 45 namespace {
46 46
47 // Seems like the generated bindings should take care of these however it 47 // Seems like the generated bindings should take care of these however it
48 // currently doesn't. See also http://crbug.com/264520 48 // currently doesn't. See also http://crbug.com/264520
49 template <typename T> 49 bool ensureNotNull(const SubtleCrypto::Bytes& x, const char* paramName, CryptoRe sult* result)
50 bool ensureNotNull(T* x, const char* paramName, CryptoResult* result)
51 { 50 {
52 if (!x) { 51 if (x.isNull) {
53 String message = String("Invalid ") + paramName + String(" argument"); 52 String message = String("Invalid ") + paramName + String(" argument");
54 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin g(message)); 53 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin g(message));
55 return false; 54 return false;
56 } 55 }
57 return true; 56 return true;
58 } 57 }
59 58
60 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data Buffer) 59 bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result)
60 {
61 if (!key) {
62 String message = String("Invalid ") + paramName + String(" argument");
63 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin g(message));
64 return false;
65 }
66 return true;
67 }
68
69 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg orithmOperation operationType, const SubtleCrypto::Bytes& signature, const Subtl eCrypto::Bytes& dataBuffer)
61 { 70 {
62 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); 71 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
63 ScriptPromise promise = result->promise(); 72 ScriptPromise promise = result->promise();
64 73
65 bool requiresKey = operationType != Digest; 74 bool requiresKey = operationType != Digest;
66 75
67 if (requiresKey && !ensureNotNull(key, "key", result.get())) 76 if (requiresKey && !ensureNotNull(key, "key", result.get()))
68 return promise; 77 return promise;
69 if (operationType == Verify && !ensureNotNull(signature, "signature", result .get())) 78 if (operationType == Verify && !ensureNotNull(signature, "signature", result .get()))
70 return promise; 79 return promise;
71 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) 80 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get()))
72 return promise; 81 return promise;
73 82
74 blink::WebCryptoAlgorithm algorithm; 83 blink::WebCryptoAlgorithm algorithm;
75 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get())) 84 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get()))
76 return promise; 85 return promise;
77 86
78 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res ult.get())) 87 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res ult.get()))
79 return promise; 88 return promise;
80 89
81 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba seAddress()); 90 const unsigned char* data = dataBuffer.bytes;
82 unsigned dataSize = dataBuffer->byteLength(); 91 unsigned dataSize = dataBuffer.size;
83 92
84 switch (operationType) { 93 switch (operationType) {
85 case Encrypt: 94 case Encrypt:
86 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), dat a, dataSize, result->result()); 95 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), dat a, dataSize, result->result());
87 break; 96 break;
88 case Decrypt: 97 case Decrypt:
89 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), dat a, dataSize, result->result()); 98 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), dat a, dataSize, result->result());
90 break; 99 break;
91 case Sign: 100 case Sign:
92 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result()); 101 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
93 break; 102 break;
94 case Verify: 103 case Verify:
95 blink::Platform::current()->crypto()->verifySignature(algorithm, key->ke y(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signature ->byteLength(), data, dataSize, result->result()); 104 blink::Platform::current()->crypto()->verifySignature(algorithm, key->ke y(), signature.bytes, signature.size, data, dataSize, result->result());
96 break; 105 break;
97 case Digest: 106 case Digest:
98 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result()); 107 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
99 break; 108 break;
100 default: 109 default:
101 ASSERT_NOT_REACHED(); 110 ASSERT_NOT_REACHED();
102 return ScriptPromise(); 111 return ScriptPromise();
103 } 112 }
104 113
105 return promise; 114 return promise;
106 } 115 }
107 116
108 } // namespace 117 } // namespace
109 118
119 SubtleCrypto::Bytes::Bytes()
120 {
121 initNull();
122 }
123
124 SubtleCrypto::Bytes::Bytes(ArrayBuffer* buffer)
125 {
126 if (buffer) {
127 bytes = static_cast<unsigned char*>(buffer->data());
128 size = buffer->byteLength();
129 isNull = false;
130 } else {
131 initNull();
132 }
133 }
134
135 SubtleCrypto::Bytes::Bytes(ArrayBufferView* buffer)
136 {
137 if (buffer) {
138 bytes = static_cast<unsigned char*>(buffer->baseAddress());
139 size = buffer->byteLength();
140 isNull = false;
141 } else {
142 initNull();
143 }
144 }
145
146 void SubtleCrypto::Bytes::initNull()
147 {
148 bytes = 0;
149 size = 0;
150 isNull = true;
151 }
152
110 SubtleCrypto::SubtleCrypto() 153 SubtleCrypto::SubtleCrypto()
111 { 154 {
112 ScriptWrappable::init(this); 155 ScriptWrappable::init(this);
113 } 156 }
114 157
115 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar rayBufferView* data) 158 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, co nst Bytes& data)
116 { 159 {
117 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data); 160 return startCryptoOperation(rawAlgorithm, key, Encrypt, Bytes(), data);
118 } 161 }
119 162
120 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar rayBufferView* data) 163 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, co nst Bytes& data)
121 { 164 {
122 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data); 165 return startCryptoOperation(rawAlgorithm, key, Decrypt, Bytes(), data);
123 } 166 }
124 167
125 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array BufferView* data) 168 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, const Bytes& data)
126 { 169 {
127 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data); 170 return startCryptoOperation(rawAlgorithm, key, Sign, Bytes(), data);
128 } 171 }
129 172
130 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* signature, ArrayBufferView* data) 173 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, const Bytes& signature, const Bytes& data)
131 { 174 {
132 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data); 175 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data);
133 } 176 }
134 177
135 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi ew* data) 178 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, const Bytes& data)
136 { 179 {
137 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data); 180 return startCryptoOperation(rawAlgorithm, 0, Digest, Bytes(), data);
138 } 181 }
139 182
140 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext ractable, const Vector<String>& rawKeyUsages) 183 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext ractable, const Vector<String>& rawKeyUsages)
141 { 184 {
142 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); 185 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
143 ScriptPromise promise = result->promise(); 186 ScriptPromise promise = result->promise();
144 187
145 blink::WebCryptoKeyUsageMask keyUsages; 188 blink::WebCryptoKeyUsageMask keyUsages;
146 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) 189 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get()))
147 return promise; 190 return promise;
148 191
149 blink::WebCryptoAlgorithm algorithm; 192 blink::WebCryptoAlgorithm algorithm;
150 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) 193 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get()))
151 return promise; 194 return promise;
152 195
153 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke yUsages, result->result()); 196 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke yUsages, result->result());
154 return promise; 197 return promise;
155 } 198 }
156 199
157 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) 200 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, const Bytes& keyD ata, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& raw KeyUsages)
158 { 201 {
159 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); 202 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
160 ScriptPromise promise = result->promise(); 203 ScriptPromise promise = result->promise();
161 204
162 if (!ensureNotNull(keyData, "keyData", result.get())) 205 if (!ensureNotNull(keyData, "keyData", result.get()))
163 return promise; 206 return promise;
164 207
165 blink::WebCryptoKeyFormat format; 208 blink::WebCryptoKeyFormat format;
166 if (!Key::parseFormat(rawFormat, format, result.get())) 209 if (!Key::parseFormat(rawFormat, format, result.get()))
167 return promise; 210 return promise;
168 211
169 blink::WebCryptoKeyUsageMask keyUsages; 212 blink::WebCryptoKeyUsageMask keyUsages;
170 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) 213 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get()))
171 return promise; 214 return promise;
172 215
173 blink::WebCryptoAlgorithm algorithm; 216 blink::WebCryptoAlgorithm algorithm;
174 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) 217 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get()))
175 return promise; 218 return promise;
176 219
177 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress()); 220 blink::Platform::current()->crypto()->importKey(format, keyData.bytes, keyDa ta.size, algorithm, extractable, keyUsages, result->result());
178
179 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat a->byteLength(), algorithm, extractable, keyUsages, result->result());
180 return promise; 221 return promise;
181 } 222 }
182 223
183 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key) 224 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key)
184 { 225 {
185 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); 226 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
186 ScriptPromise promise = result->promise(); 227 ScriptPromise promise = result->promise();
187 228
188 if (!ensureNotNull(key, "key", result.get())) 229 if (!ensureNotNull(key, "key", result.get()))
189 return promise; 230 return promise;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 return promise; 266 return promise;
226 } 267 }
227 268
228 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get() )) 269 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get() ))
229 return promise; 270 return promise;
230 271
231 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe y->key(), wrapAlgorithm, result->result()); 272 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe y->key(), wrapAlgorithm, result->result());
232 return promise; 273 return promise;
233 } 274 }
234 275
235 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView* wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dict ionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKey Usages) 276 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, const Bytes& wrap pedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictiona ry& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsag es)
236 { 277 {
237 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); 278 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
238 ScriptPromise promise = result->promise(); 279 ScriptPromise promise = result->promise();
239 280
240 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) 281 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get()))
241 return promise; 282 return promise;
242 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) 283 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get()))
243 return promise; 284 return promise;
244 285
245 blink::WebCryptoKeyFormat format; 286 blink::WebCryptoKeyFormat format;
246 if (!Key::parseFormat(rawFormat, format, result.get())) 287 if (!Key::parseFormat(rawFormat, format, result.get()))
247 return promise; 288 return promise;
248 289
249 blink::WebCryptoKeyUsageMask keyUsages; 290 blink::WebCryptoKeyUsageMask keyUsages;
250 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) 291 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get()))
251 return promise; 292 return promise;
252 293
253 blink::WebCryptoAlgorithm unwrapAlgorithm; 294 blink::WebCryptoAlgorithm unwrapAlgorithm;
254 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.g et())) 295 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.g et()))
255 return promise; 296 return promise;
256 297
257 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; 298 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
258 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit hm, result.get())) 299 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit hm, result.get()))
259 return promise; 300 return promise;
260 301
261 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result .get())) 302 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result .get()))
262 return promise; 303 return promise;
263 304
264 const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrap pedKey->baseAddress()); 305 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes, wr appedKey.size, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, ext ractable, keyUsages, result->result());
265 unsigned wrappedKeyDataSize = wrappedKey->byteLength();
266
267 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrap pedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, ex tractable, keyUsages, result->result());
268 return promise; 306 return promise;
269 } 307 }
270 308
271 } // namespace WebCore 309 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698