blob: 08cc431be650144726a9b269ff38cbf3562b3dcd [file] [log] [blame]
abarth@webkit.orgba712d32011-02-11 08:37:36 +00001/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
ap@apple.combe84a7a2013-10-14 23:36:32 +00003 * Copyright (C) 2013 Apple Inc. All rights reserved.
abarth@webkit.orgba712d32011-02-11 08:37:36 +00004 *
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.combdb7a792013-10-14 20:28:55 +000034#include "Document.h"
abarth@webkit.orgba712d32011-02-11 08:37:36 +000035#include "ExceptionCode.h"
jiewen_tan@apple.come0235352016-10-06 21:32:02 +000036#include "SubtleCrypto.h"
jiewen_tan@apple.com99444af2016-07-25 20:35:16 +000037#include "WebKitSubtleCrypto.h"
oliver@apple.comdf606082013-08-05 22:11:50 +000038#include <runtime/ArrayBufferView.h>
abarth@webkit.orgba712d32011-02-11 08:37:36 +000039#include <wtf/CryptographicallyRandomNumber.h>
40
abarth@webkit.orgba712d32011-02-11 08:37:36 +000041namespace WebCore {
42
jiewen_tan@apple.combd3f3c12016-08-15 21:08:25 +000043Crypto::Crypto(ScriptExecutionContext& context)
44 : ContextDestructionObserver(&context)
jiewen_tan@apple.come0235352016-10-06 21:32:02 +000045#if ENABLE(SUBTLE_CRYPTO)
46 , m_subtle(SubtleCrypto::create(context))
47#endif
abarth@webkit.orgba712d32011-02-11 08:37:36 +000048{
49}
50
ap@apple.combdb7a792013-10-14 20:28:55 +000051Crypto::~Crypto()
52{
53}
54
darin@apple.com9f191f72016-10-19 07:00:10 +000055ExceptionOr<void> Crypto::getRandomValues(ArrayBufferView& array)
abarth@webkit.orgba712d32011-02-11 08:37:36 +000056{
darin@apple.com9f191f72016-10-19 07:00:10 +000057 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.orgba712d32011-02-11 08:37:36 +000063}
abarth@webkit.orgba712d32011-02-11 08:37:36 +000064
ap@apple.combe84a7a2013-10-14 23:36:32 +000065#if ENABLE(SUBTLE_CRYPTO)
darin@apple.com9f191f72016-10-19 07:00:10 +000066
jiewen_tan@apple.come0235352016-10-06 21:32:02 +000067SubtleCrypto& Crypto::subtle()
68{
69 return m_subtle;
70}
71
darin@apple.comf412fd12016-10-31 15:54:51 +000072ExceptionOr<WebKitSubtleCrypto&> Crypto::webkitSubtle()
ap@apple.combe84a7a2013-10-14 23:36:32 +000073{
darin@apple.com9f191f72016-10-19 07:00:10 +000074 if (!isMainThread())
75 return Exception { NOT_SUPPORTED_ERR };
jiewen_tan@apple.combd3f3c12016-08-15 21:08:25 +000076
jiewen_tan@apple.com99444af2016-07-25 20:35:16 +000077 if (!m_webkitSubtle)
jiewen_tan@apple.combd3f3c12016-08-15 21:08:25 +000078 m_webkitSubtle = WebKitSubtleCrypto::create(*downcast<Document>(scriptExecutionContext()));
ap@apple.combe84a7a2013-10-14 23:36:32 +000079
darin@apple.comf412fd12016-10-31 15:54:51 +000080 return *m_webkitSubtle;
ap@apple.combe84a7a2013-10-14 23:36:32 +000081}
darin@apple.com9f191f72016-10-19 07:00:10 +000082
ap@apple.combe84a7a2013-10-14 23:36:32 +000083#endif
84
abarth@webkit.orgba712d32011-02-11 08:37:36 +000085}