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 | #include "config.h" |
27 | #include "CryptoKeyAES.h" |
28 | |
29 | #if ENABLE(WEB_CRYPTO) |
30 | |
31 | #include "CryptoAlgorithmAesKeyParams.h" |
32 | #include "CryptoAlgorithmRegistry.h" |
33 | #include "ExceptionOr.h" |
34 | #include "JsonWebKey.h" |
35 | #include <wtf/text/Base64.h> |
36 | #include <wtf/text/WTFString.h> |
37 | |
38 | namespace WebCore { |
39 | |
40 | static inline bool lengthIsValid(size_t length) |
41 | { |
42 | return (length == CryptoKeyAES::s_length128) || (length == CryptoKeyAES::s_length192) || (length == CryptoKeyAES::s_length256); |
43 | } |
44 | |
45 | CryptoKeyAES::CryptoKeyAES(CryptoAlgorithmIdentifier algorithm, const Vector<uint8_t>& key, bool , CryptoKeyUsageBitmap usage) |
46 | : CryptoKey(algorithm, CryptoKeyType::Secret, extractable, usage) |
47 | , m_key(key) |
48 | { |
49 | ASSERT(isValidAESAlgorithm(algorithm)); |
50 | } |
51 | |
52 | CryptoKeyAES::CryptoKeyAES(CryptoAlgorithmIdentifier algorithm, Vector<uint8_t>&& key, bool , CryptoKeyUsageBitmap usage) |
53 | : CryptoKey(algorithm, CryptoKeyType::Secret, extractable, usage) |
54 | , m_key(WTFMove(key)) |
55 | { |
56 | ASSERT(isValidAESAlgorithm(algorithm)); |
57 | } |
58 | |
59 | CryptoKeyAES::~CryptoKeyAES() = default; |
60 | |
61 | bool CryptoKeyAES::isValidAESAlgorithm(CryptoAlgorithmIdentifier algorithm) |
62 | { |
63 | return algorithm == CryptoAlgorithmIdentifier::AES_CTR |
64 | || algorithm == CryptoAlgorithmIdentifier::AES_CBC |
65 | || algorithm == CryptoAlgorithmIdentifier::AES_GCM |
66 | || algorithm == CryptoAlgorithmIdentifier::AES_CFB |
67 | || algorithm == CryptoAlgorithmIdentifier::AES_KW; |
68 | } |
69 | |
70 | RefPtr<CryptoKeyAES> CryptoKeyAES::generate(CryptoAlgorithmIdentifier algorithm, size_t lengthBits, bool , CryptoKeyUsageBitmap usages) |
71 | { |
72 | if (!lengthIsValid(lengthBits)) |
73 | return nullptr; |
74 | return adoptRef(new CryptoKeyAES(algorithm, randomData(lengthBits / 8), extractable, usages)); |
75 | } |
76 | |
77 | RefPtr<CryptoKeyAES> CryptoKeyAES::importRaw(CryptoAlgorithmIdentifier algorithm, Vector<uint8_t>&& keyData, bool , CryptoKeyUsageBitmap usages) |
78 | { |
79 | if (!lengthIsValid(keyData.size() * 8)) |
80 | return nullptr; |
81 | return adoptRef(new CryptoKeyAES(algorithm, WTFMove(keyData), extractable, usages)); |
82 | } |
83 | |
84 | RefPtr<CryptoKeyAES> CryptoKeyAES::importJwk(CryptoAlgorithmIdentifier algorithm, JsonWebKey&& keyData, bool , CryptoKeyUsageBitmap usages, CheckAlgCallback&& callback) |
85 | { |
86 | if (keyData.kty != "oct" ) |
87 | return nullptr; |
88 | if (keyData.k.isNull()) |
89 | return nullptr; |
90 | Vector<uint8_t> octetSequence; |
91 | if (!base64URLDecode(keyData.k, octetSequence)) |
92 | return nullptr; |
93 | if (!callback(octetSequence.size() * 8, keyData.alg)) |
94 | return nullptr; |
95 | if (usages && !keyData.use.isNull() && keyData.use != "enc" ) |
96 | return nullptr; |
97 | if (keyData.key_ops && ((keyData.usages & usages) != usages)) |
98 | return nullptr; |
99 | if (keyData.ext && !keyData.ext.value() && extractable) |
100 | return nullptr; |
101 | |
102 | return adoptRef(new CryptoKeyAES(algorithm, WTFMove(octetSequence), extractable, usages)); |
103 | } |
104 | |
105 | JsonWebKey CryptoKeyAES::exportJwk() const |
106 | { |
107 | JsonWebKey result; |
108 | result.kty = "oct" ; |
109 | result.k = base64URLEncode(m_key); |
110 | result.key_ops = usages(); |
111 | result.ext = extractable(); |
112 | return result; |
113 | } |
114 | |
115 | ExceptionOr<size_t> CryptoKeyAES::getKeyLength(const CryptoAlgorithmParameters& parameters) |
116 | { |
117 | auto& aesParameters = downcast<CryptoAlgorithmAesKeyParams>(parameters); |
118 | if (!lengthIsValid(aesParameters.length)) |
119 | return Exception { OperationError }; |
120 | return aesParameters.length; |
121 | } |
122 | |
123 | auto CryptoKeyAES::algorithm() const -> KeyAlgorithm |
124 | { |
125 | CryptoAesKeyAlgorithm result; |
126 | result.name = CryptoAlgorithmRegistry::singleton().name(algorithmIdentifier()); |
127 | result.length = m_key.size() * 8; |
128 | return result; |
129 | } |
130 | |
131 | } // namespace WebCore |
132 | |
133 | #endif // ENABLE(WEB_CRYPTO) |
134 | |