1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "CryptoAlgorithmIdentifier.h"
29#include "CryptoKeyFormat.h"
30#include "CryptoKeyPair.h"
31#include "CryptoKeyUsage.h"
32#include "ExceptionOr.h"
33#include "JsonWebKey.h"
34#include "SubtleCrypto.h"
35#include <wtf/Function.h>
36#include <wtf/ThreadSafeRefCounted.h>
37#include <wtf/Variant.h>
38#include <wtf/Vector.h>
39#include <wtf/WorkQueue.h>
40
41#if ENABLE(WEB_CRYPTO)
42
43namespace WebCore {
44
45class CryptoAlgorithmParameters;
46class CryptoKey;
47class ScriptExecutionContext;
48
49using KeyData = Variant<Vector<uint8_t>, JsonWebKey>;
50using KeyOrKeyPair = Variant<RefPtr<CryptoKey>, CryptoKeyPair>;
51
52class CryptoAlgorithm : public ThreadSafeRefCounted<CryptoAlgorithm> {
53public:
54 virtual ~CryptoAlgorithm() = default;
55
56 virtual CryptoAlgorithmIdentifier identifier() const = 0;
57
58 using BoolCallback = WTF::Function<void(bool)>;
59 using KeyCallback = WTF::Function<void(CryptoKey&)>;
60 using KeyOrKeyPairCallback = WTF::Function<void(KeyOrKeyPair&&)>;
61 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=169395
62 using VectorCallback = WTF::Function<void(const Vector<uint8_t>&)>;
63 using VoidCallback = WTF::Function<void()>;
64 using ExceptionCallback = WTF::Function<void(ExceptionCode)>;
65 using KeyDataCallback = WTF::Function<void(CryptoKeyFormat, KeyData&&)>;
66
67 virtual void encrypt(const CryptoAlgorithmParameters&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
68 virtual void decrypt(const CryptoAlgorithmParameters&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
69 virtual void sign(const CryptoAlgorithmParameters&, Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
70 virtual void verify(const CryptoAlgorithmParameters&, Ref<CryptoKey>&&, Vector<uint8_t>&& signature, Vector<uint8_t>&&, BoolCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
71 virtual void digest(Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
72 virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&);
73 virtual void deriveBits(const CryptoAlgorithmParameters&, Ref<CryptoKey>&&, size_t length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&);
74 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=169262
75 virtual void importKey(CryptoKeyFormat, KeyData&&, const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&);
76 virtual void exportKey(CryptoKeyFormat, Ref<CryptoKey>&&, KeyDataCallback&&, ExceptionCallback&&);
77 virtual void wrapKey(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&);
78 virtual void unwrapKey(Ref<CryptoKey>&&, Vector<uint8_t>&&, VectorCallback&&, ExceptionCallback&&);
79 virtual ExceptionOr<size_t> getKeyLength(const CryptoAlgorithmParameters&);
80
81 static void dispatchOperationInWorkQueue(WorkQueue&, ScriptExecutionContext&, VectorCallback&&, ExceptionCallback&&, WTF::Function<ExceptionOr<Vector<uint8_t>>()>&&);
82 static void dispatchOperationInWorkQueue(WorkQueue&, ScriptExecutionContext&, BoolCallback&&, ExceptionCallback&&, WTF::Function<ExceptionOr<bool>()>&&);
83};
84
85} // namespace WebCore
86
87#endif // ENABLE(WEB_CRYPTO)
88