| 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 30 matching lines...) Expand all Loading... |
| 41 #include "public/platform/WebCryptoAlgorithm.h" | 41 #include "public/platform/WebCryptoAlgorithm.h" |
| 42 #include "wtf/ArrayBufferView.h" | 42 #include "wtf/ArrayBufferView.h" |
| 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 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data
Buffer, ExceptionState& es) |
| 52 { | 52 { |
| 53 bool requiresKey = operationType != Digest; | 53 bool requiresKey = operationType != Digest; |
| 54 | 54 |
| 55 // 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 |
| 56 // currently doesn't. See also http://crbugh.com/264520 | 56 // currently doesn't. See also http://crbugh.com/264520 |
| 57 if (requiresKey && !key) { | 57 if (requiresKey && !key) { |
| 58 es.throwTypeError("Invalid key argument"); | 58 es.throwTypeError("Invalid key argument"); |
| 59 return ScriptObject(); | 59 return ScriptPromise(); |
| 60 } | 60 } |
| 61 if (operationType == Verify && !signature) { | 61 if (operationType == Verify && !signature) { |
| 62 es.throwTypeError("Invalid signature argument"); | 62 es.throwTypeError("Invalid signature argument"); |
| 63 return ScriptObject(); | 63 return ScriptPromise(); |
| 64 } | 64 } |
| 65 if (!dataBuffer) { | 65 if (!dataBuffer) { |
| 66 es.throwTypeError("Invalid dataBuffer argument"); | 66 es.throwTypeError("Invalid dataBuffer argument"); |
| 67 return ScriptObject(); | 67 return ScriptPromise(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 WebKit::WebCryptoAlgorithm algorithm; | 70 WebKit::WebCryptoAlgorithm algorithm; |
| 71 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) | 71 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) |
| 72 return ScriptObject(); | 72 return ScriptPromise(); |
| 73 | 73 |
| 74 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, es)
) | 74 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, es)
) |
| 75 return ScriptObject(); | 75 return ScriptPromise(); |
| 76 | 76 |
| 77 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()); |
| 78 size_t dataSize = dataBuffer->byteLength(); | 78 size_t dataSize = dataBuffer->byteLength(); |
| 79 | 79 |
| 80 RefPtr<CryptoResult> result = CryptoResult::create(); | 80 RefPtr<CryptoResult> result = CryptoResult::create(); |
| 81 | 81 |
| 82 switch (operationType) { | 82 switch (operationType) { |
| 83 case Encrypt: | 83 case Encrypt: |
| 84 WebKit::Platform::current()->crypto()->encrypt(algorithm, key->key(), da
ta, dataSize, result->result()); | 84 WebKit::Platform::current()->crypto()->encrypt(algorithm, key->key(), da
ta, dataSize, result->result()); |
| 85 break; | 85 break; |
| 86 case Decrypt: | 86 case Decrypt: |
| 87 WebKit::Platform::current()->crypto()->decrypt(algorithm, key->key(), da
ta, dataSize, result->result()); | 87 WebKit::Platform::current()->crypto()->decrypt(algorithm, key->key(), da
ta, dataSize, result->result()); |
| 88 break; | 88 break; |
| 89 case Sign: | 89 case Sign: |
| 90 WebKit::Platform::current()->crypto()->sign(algorithm, key->key(), data,
dataSize, result->result()); | 90 WebKit::Platform::current()->crypto()->sign(algorithm, key->key(), data,
dataSize, result->result()); |
| 91 break; | 91 break; |
| 92 case Verify: | 92 case Verify: |
| 93 WebKit::Platform::current()->crypto()->verifySignature(algorithm, key->k
ey(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signatur
e->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()); |
| 94 break; | 94 break; |
| 95 case Digest: | 95 case Digest: |
| 96 WebKit::Platform::current()->crypto()->digest(algorithm, data, dataSize,
result->result()); | 96 WebKit::Platform::current()->crypto()->digest(algorithm, data, dataSize,
result->result()); |
| 97 break; | 97 break; |
| 98 default: | 98 default: |
| 99 ASSERT_NOT_REACHED(); | 99 ASSERT_NOT_REACHED(); |
| 100 return ScriptObject(); | 100 return ScriptPromise(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 return result->promise(); | 103 return result->promise(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 } // namespace | 106 } // namespace |
| 107 | 107 |
| 108 SubtleCrypto::SubtleCrypto() | 108 SubtleCrypto::SubtleCrypto() |
| 109 { | 109 { |
| 110 ScriptWrappable::init(this); | 110 ScriptWrappable::init(this); |
| 111 } | 111 } |
| 112 | 112 |
| 113 ScriptObject SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Arr
ayBufferView* data, ExceptionState& es) | 113 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& es) |
| 114 { | 114 { |
| 115 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, es); | 115 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, es); |
| 116 } | 116 } |
| 117 | 117 |
| 118 ScriptObject SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Arr
ayBufferView* data, ExceptionState& es) | 118 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& es) |
| 119 { | 119 { |
| 120 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, es); | 120 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, es); |
| 121 } | 121 } |
| 122 | 122 |
| 123 ScriptObject SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, ArrayB
ufferView* data, ExceptionState& es) | 123 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array
BufferView* data, ExceptionState& es) |
| 124 { | 124 { |
| 125 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, es); | 125 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, es); |
| 126 } | 126 } |
| 127 | 127 |
| 128 ScriptObject SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& es) | 128 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& es) |
| 129 { | 129 { |
| 130 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, es); | 130 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, es); |
| 131 } | 131 } |
| 132 | 132 |
| 133 ScriptObject SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVie
w* data, ExceptionState& es) | 133 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi
ew* data, ExceptionState& es) |
| 134 { | 134 { |
| 135 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, es); | 135 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, es); |
| 136 } | 136 } |
| 137 | 137 |
| 138 ScriptObject SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extr
actable, const Vector<String>& rawKeyUsages, ExceptionState& es) | 138 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
ractable, const Vector<String>& rawKeyUsages, ExceptionState& es) |
| 139 { | 139 { |
| 140 WebKit::WebCryptoKeyUsageMask keyUsages; | 140 WebKit::WebCryptoKeyUsageMask keyUsages; |
| 141 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) | 141 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) |
| 142 return ScriptObject(); | 142 return ScriptPromise(); |
| 143 | 143 |
| 144 WebKit::WebCryptoAlgorithm algorithm; | 144 WebKit::WebCryptoAlgorithm algorithm; |
| 145 if (!normalizeAlgorithm(rawAlgorithm, GenerateKey, algorithm, es)) | 145 if (!normalizeAlgorithm(rawAlgorithm, GenerateKey, algorithm, es)) |
| 146 return ScriptObject(); | 146 return ScriptPromise(); |
| 147 | 147 |
| 148 RefPtr<CryptoResult> result = CryptoResult::create(); | 148 RefPtr<CryptoResult> result = CryptoResult::create(); |
| 149 WebKit::Platform::current()->crypto()->generateKey(algorithm, extractable, k
eyUsages, result->result()); | 149 WebKit::Platform::current()->crypto()->generateKey(algorithm, extractable, k
eyUsages, result->result()); |
| 150 return result->promise(); | 150 return result->promise(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k
eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& es) | 153 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& es) |
| 154 { | 154 { |
| 155 WebKit::WebCryptoKeyFormat format; | 155 WebKit::WebCryptoKeyFormat format; |
| 156 if (!Key::parseFormat(rawFormat, format, es)) | 156 if (!Key::parseFormat(rawFormat, format, es)) |
| 157 return ScriptObject(); | 157 return ScriptPromise(); |
| 158 | 158 |
| 159 if (!keyData) { | 159 if (!keyData) { |
| 160 es.throwTypeError("Invalid keyData argument"); | 160 es.throwTypeError("Invalid keyData argument"); |
| 161 return ScriptObject(); | 161 return ScriptPromise(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 WebKit::WebCryptoKeyUsageMask keyUsages; | 164 WebKit::WebCryptoKeyUsageMask keyUsages; |
| 165 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) | 165 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, es)) |
| 166 return ScriptObject(); | 166 return ScriptPromise(); |
| 167 | 167 |
| 168 WebKit::WebCryptoAlgorithm algorithm; | 168 WebKit::WebCryptoAlgorithm algorithm; |
| 169 if (!normalizeAlgorithm(rawAlgorithm, ImportKey, algorithm, es)) | 169 if (!normalizeAlgorithm(rawAlgorithm, ImportKey, algorithm, es)) |
| 170 return ScriptObject(); | 170 return ScriptPromise(); |
| 171 | 171 |
| 172 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); | 172 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); |
| 173 | 173 |
| 174 RefPtr<CryptoResult> result = CryptoResult::create(); | 174 RefPtr<CryptoResult> result = CryptoResult::create(); |
| 175 WebKit::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDa
ta->byteLength(), algorithm, extractable, keyUsages, result->result()); | 175 WebKit::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDa
ta->byteLength(), algorithm, extractable, keyUsages, result->result()); |
| 176 return result->promise(); | 176 return result->promise(); |
| 177 } | 177 } |
| 178 | 178 |
| 179 ScriptObject SubtleCrypto::exportKey(const String& rawFormat, Key* key, Exceptio
nState& es) | 179 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, Excepti
onState& es) |
| 180 { | 180 { |
| 181 WebKit::WebCryptoKeyFormat format; | 181 WebKit::WebCryptoKeyFormat format; |
| 182 if (!Key::parseFormat(rawFormat, format, es)) | 182 if (!Key::parseFormat(rawFormat, format, es)) |
| 183 return ScriptObject(); | 183 return ScriptPromise(); |
| 184 | 184 |
| 185 if (!key) { | 185 if (!key) { |
| 186 es.throwTypeError("Invalid key argument"); | 186 es.throwTypeError("Invalid key argument"); |
| 187 return ScriptObject(); | 187 return ScriptPromise(); |
| 188 } | 188 } |
| 189 | 189 |
| 190 if (!key->extractable()) { | 190 if (!key->extractable()) { |
| 191 es.throwDOMException(NotSupportedError, "key is not extractable"); | 191 es.throwDOMException(NotSupportedError, "key is not extractable"); |
| 192 return ScriptObject(); | 192 return ScriptPromise(); |
| 193 } | 193 } |
| 194 | 194 |
| 195 RefPtr<CryptoResult> result = CryptoResult::create(); | 195 RefPtr<CryptoResult> result = CryptoResult::create(); |
| 196 WebKit::Platform::current()->crypto()->exportKey(format, key->key(), result-
>result()); | 196 WebKit::Platform::current()->crypto()->exportKey(format, key->key(), result-
>result()); |
| 197 return result->promise(); | 197 return result->promise(); |
| 198 } | 198 } |
| 199 | 199 |
| 200 } // namespace WebCore | 200 } // namespace WebCore |
| OLD | NEW |