| OLD | NEW |
| 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 26 matching lines...) Expand all Loading... |
| 37 #include "core/dom/ExecutionContext.h" | 37 #include "core/dom/ExecutionContext.h" |
| 38 #include "modules/crypto/CryptoHistograms.h" | 38 #include "modules/crypto/CryptoHistograms.h" |
| 39 #include "modules/crypto/CryptoKey.h" | 39 #include "modules/crypto/CryptoKey.h" |
| 40 #include "modules/crypto/CryptoResultImpl.h" | 40 #include "modules/crypto/CryptoResultImpl.h" |
| 41 #include "modules/crypto/NormalizeAlgorithm.h" | 41 #include "modules/crypto/NormalizeAlgorithm.h" |
| 42 #include "platform/JSONValues.h" | 42 #include "platform/JSONValues.h" |
| 43 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
| 44 #include "public/platform/WebCrypto.h" | 44 #include "public/platform/WebCrypto.h" |
| 45 #include "public/platform/WebCryptoAlgorithm.h" | 45 #include "public/platform/WebCryptoAlgorithm.h" |
| 46 | 46 |
| 47 // TODO(eroman): Change the public blink::WebCrypto interface to allow | |
| 48 // transferring ownership of data buffers instead of just taking | |
| 49 // a raw pointer+length. This will avoid an extra copy. | |
| 50 | |
| 51 namespace blink { | 47 namespace blink { |
| 52 | 48 |
| 53 static bool parseAlgorithm(const AlgorithmIdentifier& raw, WebCryptoOperation op
, WebCryptoAlgorithm& algorithm, CryptoResult* result) | 49 static bool parseAlgorithm(const AlgorithmIdentifier& raw, WebCryptoOperation op
, WebCryptoAlgorithm& algorithm, CryptoResult* result) |
| 54 { | 50 { |
| 55 AlgorithmError error; | 51 AlgorithmError error; |
| 56 bool success = normalizeAlgorithm(raw, op, algorithm, &error); | 52 bool success = normalizeAlgorithm(raw, op, algorithm, &error); |
| 57 if (!success) | 53 if (!success) |
| 58 result->completeWithError(error.errorType, error.errorDetails); | 54 result->completeWithError(error.errorType, error.errorDetails); |
| 59 return success; | 55 return success; |
| 60 } | 56 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 88 for (unsigned i = 0; i < value.size(); ++i) | 84 for (unsigned i = 0; i < value.size(); ++i) |
| 89 jsonArray->pushString(value[i]); | 85 jsonArray->pushString(value[i]); |
| 90 destination->setArray(property, jsonArray.release()); | 86 destination->setArray(property, jsonArray.release()); |
| 91 return true; | 87 return true; |
| 92 } | 88 } |
| 93 | 89 |
| 94 // FIXME: At the time of writing this is not a part of the spec. It is based an | 90 // FIXME: At the time of writing this is not a part of the spec. It is based an |
| 95 // an unpublished editor's draft for: | 91 // an unpublished editor's draft for: |
| 96 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=24963 | 92 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=24963 |
| 97 // See http://crbug.com/373917. | 93 // See http://crbug.com/373917. |
| 98 static bool copyJwkDictionaryToJson(const Dictionary& dict, Vector<uint8_t>& jso
nUtf8, CryptoResult* result) | 94 static bool copyJwkDictionaryToJson(const Dictionary& dict, WebVector<uint8_t>&
jsonUtf8, CryptoResult* result) |
| 99 { | 95 { |
| 100 RefPtr<JSONObject> jsonObject = JSONObject::create(); | 96 RefPtr<JSONObject> jsonObject = JSONObject::create(); |
| 101 | 97 |
| 102 if (!copyStringProperty("kty", dict, jsonObject.get())) { | 98 if (!copyStringProperty("kty", dict, jsonObject.get())) { |
| 103 result->completeWithError(WebCryptoErrorTypeData, "The required JWK memb
er \"kty\" was missing"); | 99 result->completeWithError(WebCryptoErrorTypeData, "The required JWK memb
er \"kty\" was missing"); |
| 104 return false; | 100 return false; |
| 105 } | 101 } |
| 106 | 102 |
| 107 copyStringProperty("use", dict, jsonObject.get()); | 103 copyStringProperty("use", dict, jsonObject.get()); |
| 108 copySequenceOfStringProperty("key_ops", dict, jsonObject.get()); | 104 copySequenceOfStringProperty("key_ops", dict, jsonObject.get()); |
| 109 copyStringProperty("alg", dict, jsonObject.get()); | 105 copyStringProperty("alg", dict, jsonObject.get()); |
| 110 | 106 |
| 111 bool ext; | 107 bool ext; |
| 112 if (DictionaryHelper::get(dict, "ext", ext)) | 108 if (DictionaryHelper::get(dict, "ext", ext)) |
| 113 jsonObject->setBoolean("ext", ext); | 109 jsonObject->setBoolean("ext", ext); |
| 114 | 110 |
| 115 const char* const propertyNames[] = { "d", "n", "e", "p", "q", "dp", "dq", "
qi", "k", "crv", "x", "y" }; | 111 const char* const propertyNames[] = { "d", "n", "e", "p", "q", "dp", "dq", "
qi", "k", "crv", "x", "y" }; |
| 116 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(propertyNames); ++i) | 112 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(propertyNames); ++i) |
| 117 copyStringProperty(propertyNames[i], dict, jsonObject.get()); | 113 copyStringProperty(propertyNames[i], dict, jsonObject.get()); |
| 118 | 114 |
| 119 String json = jsonObject->toJSONString(); | 115 String json = jsonObject->toJSONString(); |
| 120 jsonUtf8.clear(); | 116 jsonUtf8 = WebVector<uint8_t>(json.utf8().data(), json.utf8().length()); |
| 121 jsonUtf8.append(json.utf8().data(), json.utf8().length()); | |
| 122 return true; | 117 return true; |
| 123 } | 118 } |
| 124 | 119 |
| 125 static Vector<uint8_t> copyBytes(const DOMArrayPiece& source) | 120 static WebVector<uint8_t> copyBytes(const DOMArrayPiece& source) |
| 126 { | 121 { |
| 127 Vector<uint8_t> result; | 122 return WebVector<uint8_t>(static_cast<uint8_t*>(source.data()), source.byteL
ength()); |
| 128 result.append(reinterpret_cast<const uint8_t*>(source.data()), source.byteLe
ngth()); | |
| 129 return result; | |
| 130 } | 123 } |
| 131 | 124 |
| 132 SubtleCrypto::SubtleCrypto() | 125 SubtleCrypto::SubtleCrypto() |
| 133 { | 126 { |
| 134 } | 127 } |
| 135 | 128 |
| 136 ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const AlgorithmIde
ntifier& rawAlgorithm, CryptoKey* key, const BufferSource& rawData) | 129 ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const AlgorithmIde
ntifier& rawAlgorithm, CryptoKey* key, const BufferSource& rawData) |
| 137 { | 130 { |
| 138 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-encrypt | 131 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-encrypt |
| 139 | 132 |
| 140 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 133 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 141 ScriptPromise promise = result->promise(); | 134 ScriptPromise promise = result->promise(); |
| 142 | 135 |
| 143 if (!canAccessWebCrypto(scriptState, result)) | 136 if (!canAccessWebCrypto(scriptState, result)) |
| 144 return promise; | 137 return promise; |
| 145 | 138 |
| 146 // 14.3.1.2: Let data be the result of getting a copy of the bytes held by | 139 // 14.3.1.2: Let data be the result of getting a copy of the bytes held by |
| 147 // the data parameter passed to the encrypt method. | 140 // the data parameter passed to the encrypt method. |
| 148 Vector<uint8_t> data = copyBytes(rawData); | 141 WebVector<uint8_t> data = copyBytes(rawData); |
| 149 | 142 |
| 150 // 14.3.1.3: Let normalizedAlgorithm be the result of normalizing an | 143 // 14.3.1.3: Let normalizedAlgorithm be the result of normalizing an |
| 151 // algorithm, with alg set to algorithm and op set to "encrypt". | 144 // algorithm, with alg set to algorithm and op set to "encrypt". |
| 152 WebCryptoAlgorithm normalizedAlgorithm; | 145 WebCryptoAlgorithm normalizedAlgorithm; |
| 153 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationEncrypt, normalizedAlgor
ithm, result)) | 146 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationEncrypt, normalizedAlgor
ithm, result)) |
| 154 return promise; | 147 return promise; |
| 155 | 148 |
| 156 // 14.3.1.8: If the name member of normalizedAlgorithm is not equal to the | 149 // 14.3.1.8: If the name member of normalizedAlgorithm is not equal to the |
| 157 // name attribute of the [[algorithm]] internal slot of key then | 150 // name attribute of the [[algorithm]] internal slot of key then |
| 158 // throw an InvalidAccessError. | 151 // throw an InvalidAccessError. |
| 159 // | 152 // |
| 160 // 14.3.1.9: If the [[usages]] internal slot of key does not contain an | 153 // 14.3.1.9: If the [[usages]] internal slot of key does not contain an |
| 161 // entry that is "encrypt", then throw an InvalidAccessError. | 154 // entry that is "encrypt", then throw an InvalidAccessError. |
| 162 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageEncryp
t, result)) | 155 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageEncryp
t, result)) |
| 163 return promise; | 156 return promise; |
| 164 | 157 |
| 165 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); | 158 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); |
| 166 Platform::current()->crypto()->encrypt(normalizedAlgorithm, key->key(), data
.data(), data.size(), result->result()); | 159 Platform::current()->crypto()->encrypt(normalizedAlgorithm, key->key(), std:
:move(data), result->result()); |
| 167 return promise; | 160 return promise; |
| 168 } | 161 } |
| 169 | 162 |
| 170 ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const AlgorithmIde
ntifier& rawAlgorithm, CryptoKey* key, const BufferSource& rawData) | 163 ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const AlgorithmIde
ntifier& rawAlgorithm, CryptoKey* key, const BufferSource& rawData) |
| 171 { | 164 { |
| 172 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-decrypt | 165 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-decrypt |
| 173 | 166 |
| 174 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 167 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 175 ScriptPromise promise = result->promise(); | 168 ScriptPromise promise = result->promise(); |
| 176 | 169 |
| 177 if (!canAccessWebCrypto(scriptState, result)) | 170 if (!canAccessWebCrypto(scriptState, result)) |
| 178 return promise; | 171 return promise; |
| 179 | 172 |
| 180 // 14.3.2.2: Let data be the result of getting a copy of the bytes held by | 173 // 14.3.2.2: Let data be the result of getting a copy of the bytes held by |
| 181 // the data parameter passed to the decrypt method. | 174 // the data parameter passed to the decrypt method. |
| 182 Vector<uint8_t> data = copyBytes(rawData); | 175 WebVector<uint8_t> data = copyBytes(rawData); |
| 183 | 176 |
| 184 // 14.3.2.3: Let normalizedAlgorithm be the result of normalizing an | 177 // 14.3.2.3: Let normalizedAlgorithm be the result of normalizing an |
| 185 // algorithm, with alg set to algorithm and op set to "decrypt". | 178 // algorithm, with alg set to algorithm and op set to "decrypt". |
| 186 WebCryptoAlgorithm normalizedAlgorithm; | 179 WebCryptoAlgorithm normalizedAlgorithm; |
| 187 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationDecrypt, normalizedAlgor
ithm, result)) | 180 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationDecrypt, normalizedAlgor
ithm, result)) |
| 188 return promise; | 181 return promise; |
| 189 | 182 |
| 190 // 14.3.2.8: If the name member of normalizedAlgorithm is not equal to the | 183 // 14.3.2.8: If the name member of normalizedAlgorithm is not equal to the |
| 191 // name attribute of the [[algorithm]] internal slot of key then | 184 // name attribute of the [[algorithm]] internal slot of key then |
| 192 // throw an InvalidAccessError. | 185 // throw an InvalidAccessError. |
| 193 // | 186 // |
| 194 // 14.3.2.9: If the [[usages]] internal slot of key does not contain an | 187 // 14.3.2.9: If the [[usages]] internal slot of key does not contain an |
| 195 // entry that is "decrypt", then throw an InvalidAccessError. | 188 // entry that is "decrypt", then throw an InvalidAccessError. |
| 196 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageDecryp
t, result)) | 189 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageDecryp
t, result)) |
| 197 return promise; | 190 return promise; |
| 198 | 191 |
| 199 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); | 192 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); |
| 200 Platform::current()->crypto()->decrypt(normalizedAlgorithm, key->key(), data
.data(), data.size(), result->result()); | 193 Platform::current()->crypto()->decrypt(normalizedAlgorithm, key->key(), std:
:move(data), result->result()); |
| 201 return promise; | 194 return promise; |
| 202 } | 195 } |
| 203 | 196 |
| 204 ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const AlgorithmIdenti
fier& rawAlgorithm, CryptoKey* key, const BufferSource& rawData) | 197 ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const AlgorithmIdenti
fier& rawAlgorithm, CryptoKey* key, const BufferSource& rawData) |
| 205 { | 198 { |
| 206 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-sign | 199 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-sign |
| 207 | 200 |
| 208 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 201 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 209 ScriptPromise promise = result->promise(); | 202 ScriptPromise promise = result->promise(); |
| 210 | 203 |
| 211 if (!canAccessWebCrypto(scriptState, result)) | 204 if (!canAccessWebCrypto(scriptState, result)) |
| 212 return promise; | 205 return promise; |
| 213 | 206 |
| 214 // 14.3.3.2: Let data be the result of getting a copy of the bytes held by | 207 // 14.3.3.2: Let data be the result of getting a copy of the bytes held by |
| 215 // the data parameter passed to the sign method. | 208 // the data parameter passed to the sign method. |
| 216 Vector<uint8_t> data = copyBytes(rawData); | 209 WebVector<uint8_t> data = copyBytes(rawData); |
| 217 | 210 |
| 218 // 14.3.3.3: Let normalizedAlgorithm be the result of normalizing an | 211 // 14.3.3.3: Let normalizedAlgorithm be the result of normalizing an |
| 219 // algorithm, with alg set to algorithm and op set to "sign". | 212 // algorithm, with alg set to algorithm and op set to "sign". |
| 220 WebCryptoAlgorithm normalizedAlgorithm; | 213 WebCryptoAlgorithm normalizedAlgorithm; |
| 221 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationSign, normalizedAlgorith
m, result)) | 214 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationSign, normalizedAlgorith
m, result)) |
| 222 return promise; | 215 return promise; |
| 223 | 216 |
| 224 // 14.3.3.8: If the name member of normalizedAlgorithm is not equal to the | 217 // 14.3.3.8: If the name member of normalizedAlgorithm is not equal to the |
| 225 // name attribute of the [[algorithm]] internal slot of key then | 218 // name attribute of the [[algorithm]] internal slot of key then |
| 226 // throw an InvalidAccessError. | 219 // throw an InvalidAccessError. |
| 227 // | 220 // |
| 228 // 14.3.3.9: If the [[usages]] internal slot of key does not contain an | 221 // 14.3.3.9: If the [[usages]] internal slot of key does not contain an |
| 229 // entry that is "sign", then throw an InvalidAccessError. | 222 // entry that is "sign", then throw an InvalidAccessError. |
| 230 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageSign,
result)) | 223 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageSign,
result)) |
| 231 return promise; | 224 return promise; |
| 232 | 225 |
| 233 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); | 226 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); |
| 234 Platform::current()->crypto()->sign(normalizedAlgorithm, key->key(), data.da
ta(), data.size(), result->result()); | 227 Platform::current()->crypto()->sign(normalizedAlgorithm, key->key(), std::mo
ve(data), result->result()); |
| 235 return promise; | 228 return promise; |
| 236 } | 229 } |
| 237 | 230 |
| 238 ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Algo
rithmIdentifier& rawAlgorithm, CryptoKey* key, const BufferSource& rawSignature,
const BufferSource& rawData) | 231 ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Algo
rithmIdentifier& rawAlgorithm, CryptoKey* key, const BufferSource& rawSignature,
const BufferSource& rawData) |
| 239 { | 232 { |
| 240 // Method described by: https://w3c.github.io/webcrypto/Overview.html#Subtle
Crypto-method-verify | 233 // Method described by: https://w3c.github.io/webcrypto/Overview.html#Subtle
Crypto-method-verify |
| 241 | 234 |
| 242 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 235 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 243 ScriptPromise promise = result->promise(); | 236 ScriptPromise promise = result->promise(); |
| 244 | 237 |
| 245 if (!canAccessWebCrypto(scriptState, result)) | 238 if (!canAccessWebCrypto(scriptState, result)) |
| 246 return promise; | 239 return promise; |
| 247 | 240 |
| 248 // 14.3.4.2: Let signature be the result of getting a copy of the bytes | 241 // 14.3.4.2: Let signature be the result of getting a copy of the bytes |
| 249 // held by the signature parameter passed to the verify method. | 242 // held by the signature parameter passed to the verify method. |
| 250 Vector<uint8_t> signature = copyBytes(rawSignature); | 243 WebVector<uint8_t> signature = copyBytes(rawSignature); |
| 251 | 244 |
| 252 // 14.3.4.3: Let normalizedAlgorithm be the result of normalizing an | 245 // 14.3.4.3: Let normalizedAlgorithm be the result of normalizing an |
| 253 // algorithm, with alg set to algorithm and op set to "verify". | 246 // algorithm, with alg set to algorithm and op set to "verify". |
| 254 WebCryptoAlgorithm normalizedAlgorithm; | 247 WebCryptoAlgorithm normalizedAlgorithm; |
| 255 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationVerify, normalizedAlgori
thm, result)) | 248 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationVerify, normalizedAlgori
thm, result)) |
| 256 return promise; | 249 return promise; |
| 257 | 250 |
| 258 // 14.3.4.5: Let data be the result of getting a copy of the bytes held by | 251 // 14.3.4.5: Let data be the result of getting a copy of the bytes held by |
| 259 // the data parameter passed to the verify method. | 252 // the data parameter passed to the verify method. |
| 260 Vector<uint8_t> data = copyBytes(rawData); | 253 WebVector<uint8_t> data = copyBytes(rawData); |
| 261 | 254 |
| 262 // 14.3.4.9: If the name member of normalizedAlgorithm is not equal to the | 255 // 14.3.4.9: If the name member of normalizedAlgorithm is not equal to the |
| 263 // name attribute of the [[algorithm]] internal slot of key then t
hrow an | 256 // name attribute of the [[algorithm]] internal slot of key then t
hrow an |
| 264 // InvalidAccessError. | 257 // InvalidAccessError. |
| 265 // | 258 // |
| 266 // 14.3.4.10: If the [[usages]] internal slot of key does not contain an | 259 // 14.3.4.10: If the [[usages]] internal slot of key does not contain an |
| 267 // entry that is "verify", then throw an InvalidAccessError. | 260 // entry that is "verify", then throw an InvalidAccessError. |
| 268 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageVerify
, result)) | 261 if (!key->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyUsageVerify
, result)) |
| 269 return promise; | 262 return promise; |
| 270 | 263 |
| 271 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); | 264 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, key->key()); |
| 272 Platform::current()->crypto()->verifySignature(normalizedAlgorithm, key->key
(), signature.data(), signature.size(), data.data(), data.size(), result->result
()); | 265 Platform::current()->crypto()->verifySignature(normalizedAlgorithm, key->key
(), std::move(signature), std::move(data), result->result()); |
| 273 return promise; | 266 return promise; |
| 274 } | 267 } |
| 275 | 268 |
| 276 ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const AlgorithmIden
tifier& rawAlgorithm, const BufferSource& rawData) | 269 ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const AlgorithmIden
tifier& rawAlgorithm, const BufferSource& rawData) |
| 277 { | 270 { |
| 278 // Method described by: https://w3c.github.io/webcrypto/Overview.html#Subtle
Crypto-method-digest | 271 // Method described by: https://w3c.github.io/webcrypto/Overview.html#Subtle
Crypto-method-digest |
| 279 | 272 |
| 280 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 273 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 281 ScriptPromise promise = result->promise(); | 274 ScriptPromise promise = result->promise(); |
| 282 | 275 |
| 283 if (!canAccessWebCrypto(scriptState, result)) | 276 if (!canAccessWebCrypto(scriptState, result)) |
| 284 return promise; | 277 return promise; |
| 285 | 278 |
| 286 // 14.3.5.2: Let data be the result of getting a copy of the bytes held | 279 // 14.3.5.2: Let data be the result of getting a copy of the bytes held |
| 287 // by the data parameter passed to the digest method. | 280 // by the data parameter passed to the digest method. |
| 288 Vector<uint8_t> data = copyBytes(rawData); | 281 WebVector<uint8_t> data = copyBytes(rawData); |
| 289 | 282 |
| 290 // 14.3.5.3: Let normalizedAlgorithm be the result of normalizing an | 283 // 14.3.5.3: Let normalizedAlgorithm be the result of normalizing an |
| 291 // algorithm, with alg set to algorithm and op set to "digest". | 284 // algorithm, with alg set to algorithm and op set to "digest". |
| 292 WebCryptoAlgorithm normalizedAlgorithm; | 285 WebCryptoAlgorithm normalizedAlgorithm; |
| 293 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationDigest, normalizedAlgori
thm, result)) | 286 if (!parseAlgorithm(rawAlgorithm, WebCryptoOperationDigest, normalizedAlgori
thm, result)) |
| 294 return promise; | 287 return promise; |
| 295 | 288 |
| 296 histogramAlgorithm(scriptState->getExecutionContext(), normalizedAlgorithm); | 289 histogramAlgorithm(scriptState->getExecutionContext(), normalizedAlgorithm); |
| 297 Platform::current()->crypto()->digest(normalizedAlgorithm, data.data(), data
.size(), result->result()); | 290 Platform::current()->crypto()->digest(normalizedAlgorithm, std::move(data),
result->result()); |
| 298 return promise; | 291 return promise; |
| 299 } | 292 } |
| 300 | 293 |
| 301 ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Algorith
mIdentifier& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) | 294 ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Algorith
mIdentifier& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) |
| 302 { | 295 { |
| 303 // Method described by: https://w3c.github.io/webcrypto/Overview.html#Subtle
Crypto-method-generateKey | 296 // Method described by: https://w3c.github.io/webcrypto/Overview.html#Subtle
Crypto-method-generateKey |
| 304 | 297 |
| 305 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 298 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 306 ScriptPromise promise = result->promise(); | 299 ScriptPromise promise = result->promise(); |
| 307 | 300 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 if (rawKeyData.isDictionary()) { | 351 if (rawKeyData.isDictionary()) { |
| 359 if (format != WebCryptoKeyFormatJwk) { | 352 if (format != WebCryptoKeyFormatJwk) { |
| 360 result->completeWithError(WebCryptoErrorTypeType, "Key data must be
a buffer for non-JWK formats"); | 353 result->completeWithError(WebCryptoErrorTypeType, "Key data must be
a buffer for non-JWK formats"); |
| 361 return promise; | 354 return promise; |
| 362 } | 355 } |
| 363 } else if (format == WebCryptoKeyFormatJwk) { | 356 } else if (format == WebCryptoKeyFormatJwk) { |
| 364 result->completeWithError(WebCryptoErrorTypeType, "Key data must be an o
bject for JWK import"); | 357 result->completeWithError(WebCryptoErrorTypeType, "Key data must be an o
bject for JWK import"); |
| 365 return promise; | 358 return promise; |
| 366 } | 359 } |
| 367 | 360 |
| 368 Vector<uint8_t> keyData; | 361 WebVector<uint8_t> keyData; |
| 369 | 362 |
| 370 // TODO(eroman): Match the procedure given in the spec to more | 363 // TODO(eroman): Match the procedure given in the spec to more |
| 371 // easily provide a normative reference. | 364 // easily provide a normative reference. |
| 372 if (rawKeyData.isArrayBuffer()) { | 365 if (rawKeyData.isArrayBuffer()) { |
| 373 keyData = copyBytes(rawKeyData.getAsArrayBuffer()); | 366 keyData = copyBytes(rawKeyData.getAsArrayBuffer()); |
| 374 } else if (rawKeyData.isArrayBufferView()) { | 367 } else if (rawKeyData.isArrayBufferView()) { |
| 375 keyData = copyBytes(rawKeyData.getAsArrayBufferView()); | 368 keyData = copyBytes(rawKeyData.getAsArrayBufferView()); |
| 376 } else if (rawKeyData.isDictionary()) { | 369 } else if (rawKeyData.isDictionary()) { |
| 377 if (!copyJwkDictionaryToJson(rawKeyData.getAsDictionary(), keyData, resu
lt)) | 370 if (!copyJwkDictionaryToJson(rawKeyData.getAsDictionary(), keyData, resu
lt)) |
| 378 return promise; | 371 return promise; |
| 379 } | 372 } |
| 380 histogramAlgorithm(scriptState->getExecutionContext(), normalizedAlgorithm); | 373 histogramAlgorithm(scriptState->getExecutionContext(), normalizedAlgorithm); |
| 381 Platform::current()->crypto()->importKey(format, keyData.data(), keyData.siz
e(), normalizedAlgorithm, extractable, keyUsages, result->result()); | 374 Platform::current()->crypto()->importKey(format, std::move(keyData), normali
zedAlgorithm, extractable, keyUsages, result->result()); |
| 382 return promise; | 375 return promise; |
| 383 } | 376 } |
| 384 | 377 |
| 385 ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& ra
wFormat, CryptoKey* key) | 378 ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& ra
wFormat, CryptoKey* key) |
| 386 { | 379 { |
| 387 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-exportKey | 380 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-exportKey |
| 388 | 381 |
| 389 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 382 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 390 ScriptPromise promise = result->promise(); | 383 ScriptPromise promise = result->promise(); |
| 391 | 384 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 if (!CryptoKey::parseFormat(rawFormat, format, result)) | 466 if (!CryptoKey::parseFormat(rawFormat, format, result)) |
| 474 return promise; | 467 return promise; |
| 475 | 468 |
| 476 WebCryptoKeyUsageMask keyUsages; | 469 WebCryptoKeyUsageMask keyUsages; |
| 477 if (!CryptoKey::parseUsageMask(rawKeyUsages, keyUsages, result)) | 470 if (!CryptoKey::parseUsageMask(rawKeyUsages, keyUsages, result)) |
| 478 return promise; | 471 return promise; |
| 479 | 472 |
| 480 // 14.3.12.2: Let wrappedKey be the result of getting a copy of the bytes | 473 // 14.3.12.2: Let wrappedKey be the result of getting a copy of the bytes |
| 481 // held by the wrappedKey parameter passed to the unwrapKey | 474 // held by the wrappedKey parameter passed to the unwrapKey |
| 482 // method. | 475 // method. |
| 483 Vector<uint8_t> wrappedKey = copyBytes(rawWrappedKey); | 476 WebVector<uint8_t> wrappedKey = copyBytes(rawWrappedKey); |
| 484 | 477 |
| 485 // 14.3.12.3: Let normalizedAlgorithm be the result of normalizing an | 478 // 14.3.12.3: Let normalizedAlgorithm be the result of normalizing an |
| 486 // algorithm, with alg set to algorithm and op set to | 479 // algorithm, with alg set to algorithm and op set to |
| 487 // "unwrapKey". | 480 // "unwrapKey". |
| 488 // | 481 // |
| 489 // 14.3.12.4: If an error occurred, let normalizedAlgorithm be the result | 482 // 14.3.12.4: If an error occurred, let normalizedAlgorithm be the result |
| 490 // of normalizing an algorithm, with alg set to algorithm and op | 483 // of normalizing an algorithm, with alg set to algorithm and op |
| 491 // set to "decrypt". | 484 // set to "decrypt". |
| 492 WebCryptoAlgorithm normalizedAlgorithm; | 485 WebCryptoAlgorithm normalizedAlgorithm; |
| 493 if (!parseAlgorithm(rawUnwrapAlgorithm, WebCryptoOperationUnwrapKey, normali
zedAlgorithm, result)) | 486 if (!parseAlgorithm(rawUnwrapAlgorithm, WebCryptoOperationUnwrapKey, normali
zedAlgorithm, result)) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 509 // InvalidAccessError. | 502 // InvalidAccessError. |
| 510 if (!unwrappingKey->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyU
sageUnwrapKey, result)) | 503 if (!unwrappingKey->canBeUsedForAlgorithm(normalizedAlgorithm, WebCryptoKeyU
sageUnwrapKey, result)) |
| 511 return promise; | 504 return promise; |
| 512 | 505 |
| 513 // NOTE: Step (16) disallows empty usages on secret and private keys. This | 506 // NOTE: Step (16) disallows empty usages on secret and private keys. This |
| 514 // normative requirement is enforced by the platform implementation in the | 507 // normative requirement is enforced by the platform implementation in the |
| 515 // call below. | 508 // call below. |
| 516 | 509 |
| 517 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, unwrappingKey->key()); | 510 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, unwrappingKey->key()); |
| 518 histogramAlgorithm(scriptState->getExecutionContext(), normalizedKeyAlgorith
m); | 511 histogramAlgorithm(scriptState->getExecutionContext(), normalizedKeyAlgorith
m); |
| 519 Platform::current()->crypto()->unwrapKey(format, wrappedKey.data(), wrappedK
ey.size(), unwrappingKey->key(), normalizedAlgorithm, normalizedKeyAlgorithm, ex
tractable, keyUsages, result->result()); | 512 Platform::current()->crypto()->unwrapKey(format, std::move(wrappedKey), unwr
appingKey->key(), normalizedAlgorithm, normalizedKeyAlgorithm, extractable, keyU
sages, result->result()); |
| 520 return promise; | 513 return promise; |
| 521 } | 514 } |
| 522 | 515 |
| 523 ScriptPromise SubtleCrypto::deriveBits(ScriptState* scriptState, const Algorithm
Identifier& rawAlgorithm, CryptoKey* baseKey, unsigned lengthBits) | 516 ScriptPromise SubtleCrypto::deriveBits(ScriptState* scriptState, const Algorithm
Identifier& rawAlgorithm, CryptoKey* baseKey, unsigned lengthBits) |
| 524 { | 517 { |
| 525 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-deriveBits | 518 // Method described by: https://w3c.github.io/webcrypto/Overview.html#dfn-Su
btleCrypto-method-deriveBits |
| 526 | 519 |
| 527 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); | 520 CryptoResultImpl* result = CryptoResultImpl::create(scriptState); |
| 528 ScriptPromise promise = result->promise(); | 521 ScriptPromise promise = result->promise(); |
| 529 | 522 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 // normative requirement is enforced by the platform implementation in the | 597 // normative requirement is enforced by the platform implementation in the |
| 605 // call below. | 598 // call below. |
| 606 | 599 |
| 607 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, baseKey->key()); | 600 histogramAlgorithmAndKey(scriptState->getExecutionContext(), normalizedAlgor
ithm, baseKey->key()); |
| 608 histogramAlgorithm(scriptState->getExecutionContext(), normalizedDerivedKeyA
lgorithm); | 601 histogramAlgorithm(scriptState->getExecutionContext(), normalizedDerivedKeyA
lgorithm); |
| 609 Platform::current()->crypto()->deriveKey(normalizedAlgorithm, baseKey->key()
, normalizedDerivedKeyAlgorithm, keyLengthAlgorithm, extractable, keyUsages, res
ult->result()); | 602 Platform::current()->crypto()->deriveKey(normalizedAlgorithm, baseKey->key()
, normalizedDerivedKeyAlgorithm, keyLengthAlgorithm, extractable, keyUsages, res
ult->result()); |
| 610 return promise; | 603 return promise; |
| 611 } | 604 } |
| 612 | 605 |
| 613 } // namespace blink | 606 } // namespace blink |
| OLD | NEW |