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

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

Issue 23798002: Cleanup: Make Platform::crypto() required. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 3 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 | « no previous file | public/platform/Platform.h » ('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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 namespace WebCore { 44 namespace WebCore {
45 45
46 // FIXME: asynchronous completion of CryptoResult. Need to re-enter the 46 // FIXME: asynchronous completion of CryptoResult. Need to re-enter the
47 // v8::Context before trying to fulfill the promise, and enable test. 47 // v8::Context before trying to fulfill the promise, and enable test.
48 48
49 namespace { 49 namespace {
50 50
51 ScriptObject startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Algo rithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataB uffer, ExceptionState& es) 51 ScriptObject startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Algo rithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataB uffer, ExceptionState& es)
52 { 52 {
53 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto();
54 if (!platformCrypto) {
55 es.throwDOMException(NotSupportedError);
56 return ScriptObject();
57 }
58
59 bool requiresKey = operationType != Digest; 53 bool requiresKey = operationType != Digest;
60 54
61 // Seems like the generated bindings should take care of these however it 55 // Seems like the generated bindings should take care of these however it
62 // currently doesn't. See also http://crbugh.com/264520 56 // currently doesn't. See also http://crbugh.com/264520
63 if (requiresKey && !key) { 57 if (requiresKey && !key) {
64 es.throwTypeError("Invalid key argument"); 58 es.throwTypeError("Invalid key argument");
65 return ScriptObject(); 59 return ScriptObject();
66 } 60 }
67 if (operationType == Verify && !signature) { 61 if (operationType == Verify && !signature) {
68 es.throwTypeError("Invalid signature argument"); 62 es.throwTypeError("Invalid signature argument");
(...skipping 11 matching lines...) Expand all
80 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, es) ) 74 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, es) )
81 return ScriptObject(); 75 return ScriptObject();
82 76
83 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba seAddress()); 77 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba seAddress());
84 size_t dataSize = dataBuffer->byteLength(); 78 size_t dataSize = dataBuffer->byteLength();
85 79
86 RefPtr<CryptoResult> result = CryptoResult::create(); 80 RefPtr<CryptoResult> result = CryptoResult::create();
87 81
88 switch (operationType) { 82 switch (operationType) {
89 case Encrypt: 83 case Encrypt:
90 platformCrypto->encrypt(algorithm, key->key(), data, dataSize, result->r esult()); 84 WebKit::Platform::current()->crypto()->encrypt(algorithm, key->key(), da ta, dataSize, result->result());
91 break; 85 break;
92 case Decrypt: 86 case Decrypt:
93 platformCrypto->decrypt(algorithm, key->key(), data, dataSize, result->r esult()); 87 WebKit::Platform::current()->crypto()->decrypt(algorithm, key->key(), da ta, dataSize, result->result());
94 break; 88 break;
95 case Sign: 89 case Sign:
96 platformCrypto->sign(algorithm, key->key(), data, dataSize, result->resu lt()); 90 WebKit::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
97 break; 91 break;
98 case Verify: 92 case Verify:
99 platformCrypto->verifySignature(algorithm, key->key(), reinterpret_cast< const unsigned char*>(signature->baseAddress()), signature->byteLength(), data, dataSize, result->result()); 93 WebKit::Platform::current()->crypto()->verifySignature(algorithm, key->k ey(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signatur e->byteLength(), data, dataSize, result->result());
100 break; 94 break;
101 case Digest: 95 case Digest:
102 platformCrypto->digest(algorithm, data, dataSize, result->result()); 96 WebKit::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
103 break; 97 break;
104 default: 98 default:
105 ASSERT_NOT_REACHED(); 99 ASSERT_NOT_REACHED();
106 return ScriptObject(); 100 return ScriptObject();
107 } 101 }
108 102
109 return result->promise(); 103 return result->promise();
110 } 104 }
111 105
112 } // namespace 106 } // namespace
(...skipping 23 matching lines...) Expand all
136 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, es); 130 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, es);
137 } 131 }
138 132
139 ScriptObject SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVie w* data, ExceptionState& es) 133 ScriptObject SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVie w* data, ExceptionState& es)
140 { 134 {
141 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, es); 135 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, es);
142 } 136 }
143 137
144 ScriptObject SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extr actable, const Vector<String>& rawKeyUsages, ExceptionState& es) 138 ScriptObject SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extr actable, const Vector<String>& rawKeyUsages, ExceptionState& es)
145 { 139 {
146 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto();
147 if (!platformCrypto) {
148 es.throwDOMException(NotSupportedError);
149 return ScriptObject();
150 }
151
152 WebKit::WebCryptoKeyUsageMask keyUsages; 140 WebKit::WebCryptoKeyUsageMask keyUsages;
153 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) 141 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es))
154 return ScriptObject(); 142 return ScriptObject();
155 143
156 WebKit::WebCryptoAlgorithm algorithm; 144 WebKit::WebCryptoAlgorithm algorithm;
157 if (!normalizeAlgorithm(rawAlgorithm, GenerateKey, algorithm, es)) 145 if (!normalizeAlgorithm(rawAlgorithm, GenerateKey, algorithm, es))
158 return ScriptObject(); 146 return ScriptObject();
159 147
160 RefPtr<CryptoResult> result = CryptoResult::create(); 148 RefPtr<CryptoResult> result = CryptoResult::create();
161 platformCrypto->generateKey(algorithm, extractable, keyUsages, result->resul t()); 149 WebKit::Platform::current()->crypto()->generateKey(algorithm, extractable, k eyUsages, result->result());
162 return result->promise(); 150 return result->promise();
163 } 151 }
164 152
165 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& es) 153 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& es)
166 { 154 {
167 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto();
168 if (!platformCrypto) {
169 es.throwDOMException(NotSupportedError);
170 return ScriptObject();
171 }
172
173 WebKit::WebCryptoKeyFormat format; 155 WebKit::WebCryptoKeyFormat format;
174 if (!Key::parseFormat(rawFormat, format, es)) 156 if (!Key::parseFormat(rawFormat, format, es))
175 return ScriptObject(); 157 return ScriptObject();
176 158
177 if (!keyData) { 159 if (!keyData) {
178 es.throwTypeError("Invalid keyData argument"); 160 es.throwTypeError("Invalid keyData argument");
179 return ScriptObject(); 161 return ScriptObject();
180 } 162 }
181 163
182 WebKit::WebCryptoKeyUsageMask keyUsages; 164 WebKit::WebCryptoKeyUsageMask keyUsages;
183 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) 165 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es))
184 return ScriptObject(); 166 return ScriptObject();
185 167
186 WebKit::WebCryptoAlgorithm algorithm; 168 WebKit::WebCryptoAlgorithm algorithm;
187 if (!normalizeAlgorithm(rawAlgorithm, ImportKey, algorithm, es)) 169 if (!normalizeAlgorithm(rawAlgorithm, ImportKey, algorithm, es))
188 return ScriptObject(); 170 return ScriptObject();
189 171
190 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress()); 172 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress());
191 173
192 RefPtr<CryptoResult> result = CryptoResult::create(); 174 RefPtr<CryptoResult> result = CryptoResult::create();
193 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor ithm, extractable, keyUsages, result->result()); 175 WebKit::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDa ta->byteLength(), algorithm, extractable, keyUsages, result->result());
194 return result->promise(); 176 return result->promise();
195 } 177 }
196 178
197 } // namespace WebCore 179 } // namespace WebCore
OLDNEW
« no previous file with comments | « no previous file | public/platform/Platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698