| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 #include "public/platform/WebCryptoAlgorithm.h" | 42 #include "public/platform/WebCryptoAlgorithm.h" |
| 43 #include "wtf/ArrayBufferView.h" | 43 #include "wtf/ArrayBufferView.h" |
| 44 | 44 |
| 45 namespace WebCore { | 45 namespace WebCore { |
| 46 | 46 |
| 47 // FIXME: asynchronous completion of CryptoResult. Need to re-enter the | 47 // FIXME: asynchronous completion of CryptoResult. Need to re-enter the |
| 48 // v8::Context before trying to fulfill the promise, and enable test. | 48 // v8::Context before trying to fulfill the promise, and enable test. |
| 49 | 49 |
| 50 namespace { | 50 namespace { |
| 51 | 51 |
| 52 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data
Buffer, ExceptionState& es) | 52 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data
Buffer, ExceptionState& exceptionState) |
| 53 { | 53 { |
| 54 bool requiresKey = operationType != Digest; | 54 bool requiresKey = operationType != Digest; |
| 55 | 55 |
| 56 // Seems like the generated bindings should take care of these however it | 56 // Seems like the generated bindings should take care of these however it |
| 57 // currently doesn't. See also http://crbugh.com/264520 | 57 // currently doesn't. See also http://crbugh.com/264520 |
| 58 if (requiresKey && !key) { | 58 if (requiresKey && !key) { |
| 59 es.throwTypeError("Invalid key argument"); | 59 exceptionState.throwTypeError("Invalid key argument"); |
| 60 return ScriptPromise(); | 60 return ScriptPromise(); |
| 61 } | 61 } |
| 62 if (operationType == Verify && !signature) { | 62 if (operationType == Verify && !signature) { |
| 63 es.throwTypeError("Invalid signature argument"); | 63 exceptionState.throwTypeError("Invalid signature argument"); |
| 64 return ScriptPromise(); | 64 return ScriptPromise(); |
| 65 } | 65 } |
| 66 if (!dataBuffer) { | 66 if (!dataBuffer) { |
| 67 es.throwTypeError("Invalid dataBuffer argument"); | 67 exceptionState.throwTypeError("Invalid dataBuffer argument"); |
| 68 return ScriptPromise(); | 68 return ScriptPromise(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 blink::WebCryptoAlgorithm algorithm; | 71 blink::WebCryptoAlgorithm algorithm; |
| 72 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) | 72 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, exceptionSta
te)) |
| 73 return ScriptPromise(); | 73 return ScriptPromise(); |
| 74 | 74 |
| 75 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, es)
) | 75 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, exc
eptionState)) |
| 76 return ScriptPromise(); | 76 return ScriptPromise(); |
| 77 | 77 |
| 78 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba
seAddress()); | 78 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba
seAddress()); |
| 79 unsigned dataSize = dataBuffer->byteLength(); | 79 unsigned dataSize = dataBuffer->byteLength(); |
| 80 | 80 |
| 81 ScriptPromise promise = ScriptPromise::createPending(); | 81 ScriptPromise promise = ScriptPromise::createPending(); |
| 82 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); | 82 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); |
| 83 | 83 |
| 84 switch (operationType) { | 84 switch (operationType) { |
| 85 case Encrypt: | 85 case Encrypt: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 105 return promise; | 105 return promise; |
| 106 } | 106 } |
| 107 | 107 |
| 108 } // namespace | 108 } // namespace |
| 109 | 109 |
| 110 SubtleCrypto::SubtleCrypto() | 110 SubtleCrypto::SubtleCrypto() |
| 111 { | 111 { |
| 112 ScriptWrappable::init(this); | 112 ScriptWrappable::init(this); |
| 113 } | 113 } |
| 114 | 114 |
| 115 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& es) | 115 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& exceptionState) |
| 116 { | 116 { |
| 117 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, es); | 117 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, exceptionSt
ate); |
| 118 } | 118 } |
| 119 | 119 |
| 120 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& es) | 120 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& exceptionState) |
| 121 { | 121 { |
| 122 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, es); | 122 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, exceptionSt
ate); |
| 123 } | 123 } |
| 124 | 124 |
| 125 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array
BufferView* data, ExceptionState& es) | 125 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array
BufferView* data, ExceptionState& exceptionState) |
| 126 { | 126 { |
| 127 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, es); | 127 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, exceptionState
); |
| 128 } | 128 } |
| 129 | 129 |
| 130 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& es) | 130 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& excepti
onState) |
| 131 { | 131 { |
| 132 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, es); | 132 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, exce
ptionState); |
| 133 } | 133 } |
| 134 | 134 |
| 135 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi
ew* data, ExceptionState& es) | 135 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi
ew* data, ExceptionState& exceptionState) |
| 136 { | 136 { |
| 137 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, es); | 137 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, exceptionState
); |
| 138 } | 138 } |
| 139 | 139 |
| 140 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
ractable, const Vector<String>& rawKeyUsages, ExceptionState& es) | 140 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
ractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState) |
| 141 { | 141 { |
| 142 blink::WebCryptoKeyUsageMask keyUsages; | 142 blink::WebCryptoKeyUsageMask keyUsages; |
| 143 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) | 143 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState)) |
| 144 return ScriptPromise(); | 144 return ScriptPromise(); |
| 145 | 145 |
| 146 blink::WebCryptoAlgorithm algorithm; | 146 blink::WebCryptoAlgorithm algorithm; |
| 147 if (!normalizeAlgorithm(rawAlgorithm, GenerateKey, algorithm, es)) | 147 if (!normalizeAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState
)) |
| 148 return ScriptPromise(); | 148 return ScriptPromise(); |
| 149 | 149 |
| 150 ScriptPromise promise = ScriptPromise::createPending(); | 150 ScriptPromise promise = ScriptPromise::createPending(); |
| 151 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); | 151 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); |
| 152 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); | 152 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); |
| 153 return promise; | 153 return promise; |
| 154 } | 154 } |
| 155 | 155 |
| 156 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& es) | 156 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& exceptionState) |
| 157 { | 157 { |
| 158 blink::WebCryptoKeyFormat format; | 158 blink::WebCryptoKeyFormat format; |
| 159 if (!Key::parseFormat(rawFormat, format, es)) | 159 if (!Key::parseFormat(rawFormat, format, exceptionState)) |
| 160 return ScriptPromise(); | 160 return ScriptPromise(); |
| 161 | 161 |
| 162 if (!keyData) { | 162 if (!keyData) { |
| 163 es.throwTypeError("Invalid keyData argument"); | 163 exceptionState.throwTypeError("Invalid keyData argument"); |
| 164 return ScriptPromise(); | 164 return ScriptPromise(); |
| 165 } | 165 } |
| 166 | 166 |
| 167 blink::WebCryptoKeyUsageMask keyUsages; | 167 blink::WebCryptoKeyUsageMask keyUsages; |
| 168 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) | 168 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState)) |
| 169 return ScriptPromise(); | 169 return ScriptPromise(); |
| 170 | 170 |
| 171 // The algorithm is optional. | 171 // The algorithm is optional. |
| 172 blink::WebCryptoAlgorithm algorithm; | 172 blink::WebCryptoAlgorithm algorithm; |
| 173 if (!rawAlgorithm.isUndefinedOrNull() && !normalizeAlgorithm(rawAlgorithm, I
mportKey, algorithm, es)) | 173 if (!rawAlgorithm.isUndefinedOrNull() && !normalizeAlgorithm(rawAlgorithm, I
mportKey, algorithm, exceptionState)) |
| 174 return ScriptPromise(); | 174 return ScriptPromise(); |
| 175 | 175 |
| 176 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); | 176 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); |
| 177 | 177 |
| 178 ScriptPromise promise = ScriptPromise::createPending(); | 178 ScriptPromise promise = ScriptPromise::createPending(); |
| 179 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); | 179 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); |
| 180 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat
a->byteLength(), algorithm, extractable, keyUsages, result->result()); | 180 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat
a->byteLength(), algorithm, extractable, keyUsages, result->result()); |
| 181 return promise; | 181 return promise; |
| 182 } | 182 } |
| 183 | 183 |
| 184 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, Excepti
onState& es) | 184 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, Excepti
onState& exceptionState) |
| 185 { | 185 { |
| 186 blink::WebCryptoKeyFormat format; | 186 blink::WebCryptoKeyFormat format; |
| 187 if (!Key::parseFormat(rawFormat, format, es)) | 187 if (!Key::parseFormat(rawFormat, format, exceptionState)) |
| 188 return ScriptPromise(); | 188 return ScriptPromise(); |
| 189 | 189 |
| 190 if (!key) { | 190 if (!key) { |
| 191 es.throwTypeError("Invalid key argument"); | 191 exceptionState.throwTypeError("Invalid key argument"); |
| 192 return ScriptPromise(); | 192 return ScriptPromise(); |
| 193 } | 193 } |
| 194 | 194 |
| 195 if (!key->extractable()) { | 195 if (!key->extractable()) { |
| 196 es.throwDOMException(NotSupportedError, "key is not extractable"); | 196 exceptionState.throwDOMException(NotSupportedError, "key is not extracta
ble"); |
| 197 return ScriptPromise(); | 197 return ScriptPromise(); |
| 198 } | 198 } |
| 199 | 199 |
| 200 ScriptPromise promise = ScriptPromise::createPending(); | 200 ScriptPromise promise = ScriptPromise::createPending(); |
| 201 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); | 201 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise); |
| 202 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->
result()); | 202 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->
result()); |
| 203 return promise; | 203 return promise; |
| 204 } | 204 } |
| 205 | 205 |
| 206 } // namespace WebCore | 206 } // namespace WebCore |
| OLD | NEW |