abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
ap@apple.com | be84a7a | 2013-10-14 23:36:32 +0000 | [diff] [blame] | 3 | * Copyright (C) 2013 Apple Inc. All rights reserved. |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of Google, Inc. ("Google") nor the names of |
| 15 | * its contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | #include "config.h" |
| 32 | #include "Crypto.h" |
| 33 | |
ap@apple.com | bdb7a79 | 2013-10-14 20:28:55 +0000 | [diff] [blame] | 34 | #include "Document.h" |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 35 | #include "ExceptionCode.h" |
jiewen_tan@apple.com | e023535 | 2016-10-06 21:32:02 +0000 | [diff] [blame] | 36 | #include "SubtleCrypto.h" |
jiewen_tan@apple.com | 99444af | 2016-07-25 20:35:16 +0000 | [diff] [blame] | 37 | #include "WebKitSubtleCrypto.h" |
oliver@apple.com | df60608 | 2013-08-05 22:11:50 +0000 | [diff] [blame] | 38 | #include <runtime/ArrayBufferView.h> |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 39 | #include <wtf/CryptographicallyRandomNumber.h> |
| 40 | |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 41 | namespace WebCore { |
| 42 | |
jiewen_tan@apple.com | bd3f3c1 | 2016-08-15 21:08:25 +0000 | [diff] [blame] | 43 | Crypto::Crypto(ScriptExecutionContext& context) |
| 44 | : ContextDestructionObserver(&context) |
jiewen_tan@apple.com | e023535 | 2016-10-06 21:32:02 +0000 | [diff] [blame] | 45 | #if ENABLE(SUBTLE_CRYPTO) |
| 46 | , m_subtle(SubtleCrypto::create(context)) |
| 47 | #endif |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
ap@apple.com | bdb7a79 | 2013-10-14 20:28:55 +0000 | [diff] [blame] | 51 | Crypto::~Crypto() |
| 52 | { |
| 53 | } |
| 54 | |
darin@apple.com | 9f191f7 | 2016-10-19 07:00:10 +0000 | [diff] [blame] | 55 | ExceptionOr<void> Crypto::getRandomValues(ArrayBufferView& array) |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 56 | { |
darin@apple.com | 9f191f7 | 2016-10-19 07:00:10 +0000 | [diff] [blame] | 57 | if (!isInt(array.getType())) |
| 58 | return Exception { TYPE_MISMATCH_ERR }; |
| 59 | if (array.byteLength() > 65536) |
| 60 | return Exception { QUOTA_EXCEEDED_ERR }; |
| 61 | cryptographicallyRandomValues(array.baseAddress(), array.byteLength()); |
| 62 | return { }; |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 63 | } |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 64 | |
ap@apple.com | be84a7a | 2013-10-14 23:36:32 +0000 | [diff] [blame] | 65 | #if ENABLE(SUBTLE_CRYPTO) |
darin@apple.com | 9f191f7 | 2016-10-19 07:00:10 +0000 | [diff] [blame] | 66 | |
jiewen_tan@apple.com | e023535 | 2016-10-06 21:32:02 +0000 | [diff] [blame] | 67 | SubtleCrypto& Crypto::subtle() |
| 68 | { |
| 69 | return m_subtle; |
| 70 | } |
| 71 | |
darin@apple.com | f412fd1 | 2016-10-31 15:54:51 +0000 | [diff] [blame] | 72 | ExceptionOr<WebKitSubtleCrypto&> Crypto::webkitSubtle() |
ap@apple.com | be84a7a | 2013-10-14 23:36:32 +0000 | [diff] [blame] | 73 | { |
darin@apple.com | 9f191f7 | 2016-10-19 07:00:10 +0000 | [diff] [blame] | 74 | if (!isMainThread()) |
| 75 | return Exception { NOT_SUPPORTED_ERR }; |
jiewen_tan@apple.com | bd3f3c1 | 2016-08-15 21:08:25 +0000 | [diff] [blame] | 76 | |
jiewen_tan@apple.com | 99444af | 2016-07-25 20:35:16 +0000 | [diff] [blame] | 77 | if (!m_webkitSubtle) |
jiewen_tan@apple.com | bd3f3c1 | 2016-08-15 21:08:25 +0000 | [diff] [blame] | 78 | m_webkitSubtle = WebKitSubtleCrypto::create(*downcast<Document>(scriptExecutionContext())); |
ap@apple.com | be84a7a | 2013-10-14 23:36:32 +0000 | [diff] [blame] | 79 | |
darin@apple.com | f412fd1 | 2016-10-31 15:54:51 +0000 | [diff] [blame] | 80 | return *m_webkitSubtle; |
ap@apple.com | be84a7a | 2013-10-14 23:36:32 +0000 | [diff] [blame] | 81 | } |
darin@apple.com | 9f191f7 | 2016-10-19 07:00:10 +0000 | [diff] [blame] | 82 | |
ap@apple.com | be84a7a | 2013-10-14 23:36:32 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
abarth@webkit.org | ba712d3 | 2011-02-11 08:37:36 +0000 | [diff] [blame] | 85 | } |