OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google 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 are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "modules/crypto/NormalizeAlgorithm.h" |
| 33 |
| 34 #include "bindings/v8/Dictionary.h" |
| 35 #include "core/dom/ExceptionCode.h" |
| 36 #include "public/platform/WebCryptoAlgorithm.h" |
| 37 #include "public/platform/WebCryptoAlgorithmParams.h" |
| 38 #include "wtf/ArrayBuffer.h" |
| 39 #include "wtf/ArrayBufferView.h" |
| 40 #include "wtf/HashMap.h" |
| 41 #include "wtf/Uint8Array.h" |
| 42 #include "wtf/Vector.h" |
| 43 #include "wtf/text/StringHash.h" |
| 44 |
| 45 namespace WebCore { |
| 46 |
| 47 namespace { |
| 48 |
| 49 struct AlgorithmNameMapping { |
| 50 const char* const algorithmName; |
| 51 WebKit::WebCryptoAlgorithmId algorithmId; |
| 52 }; |
| 53 |
| 54 // Indicates that the algorithm doesn't support the specified operation. |
| 55 const int UnsupportedOp = -1; |
| 56 |
| 57 // Either UnsupportedOp, or a value from WebKit::WebCryptoAlgorithmParamsType |
| 58 typedef int AlgorithmParamsForOperation; |
| 59 |
| 60 struct OperationParamsMapping { |
| 61 WebKit::WebCryptoAlgorithmId algorithmId; |
| 62 AlgorithmOperation operation; |
| 63 AlgorithmParamsForOperation params; |
| 64 }; |
| 65 |
| 66 const AlgorithmNameMapping algorithmNameMappings[] = { |
| 67 {"AES-CBC", WebKit::WebCryptoAlgorithmIdAesCbc}, |
| 68 {"SHA-1", WebKit::WebCryptoAlgorithmIdSha1}, |
| 69 {"SHA-224", WebKit::WebCryptoAlgorithmIdSha224}, |
| 70 {"SHA-256", WebKit::WebCryptoAlgorithmIdSha256}, |
| 71 {"SHA-384", WebKit::WebCryptoAlgorithmIdSha384}, |
| 72 {"SHA-512", WebKit::WebCryptoAlgorithmIdSha512}, |
| 73 }; |
| 74 |
| 75 // What operations each algorithm supports, and what parameters it expects. |
| 76 const OperationParamsMapping operationParamsMappings[] = { |
| 77 // AES-CBC (section 18.10.) |
| 78 {WebKit::WebCryptoAlgorithmIdAesCbc, Decrypt, WebKit::WebCryptoAlgorithmPara
msTypeAesCbcParams}, |
| 79 {WebKit::WebCryptoAlgorithmIdAesCbc, Encrypt, WebKit::WebCryptoAlgorithmPara
msTypeAesCbcParams}, |
| 80 {WebKit::WebCryptoAlgorithmIdAesCbc, GenerateKey, WebKit::WebCryptoAlgorithm
ParamsTypeAesKeyGenParams}, |
| 81 |
| 82 // SHA-1 (section 18.16.) |
| 83 {WebKit::WebCryptoAlgorithmIdSha1, Digest, WebKit::WebCryptoAlgorithmParamsT
ypeNone}, |
| 84 |
| 85 // SHA-224 (section 18.16.) |
| 86 {WebKit::WebCryptoAlgorithmIdSha224, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, |
| 87 |
| 88 // SHA-256 (section 18.16.) |
| 89 {WebKit::WebCryptoAlgorithmIdSha256, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, |
| 90 |
| 91 // SHA-384 (section 18.16.) |
| 92 {WebKit::WebCryptoAlgorithmIdSha384, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, |
| 93 |
| 94 // SHA-512 (section 18.16.) |
| 95 {WebKit::WebCryptoAlgorithmIdSha512, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, |
| 96 }; |
| 97 |
| 98 // This structure describes an algorithm and its supported operations. |
| 99 struct AlgorithmInfo { |
| 100 AlgorithmInfo() |
| 101 : algorithmName(0) |
| 102 { |
| 103 for (size_t i = 0; i < WTF_ARRAY_LENGTH(paramsForOperation); ++i) |
| 104 paramsForOperation[i] = UnsupportedOp; |
| 105 } |
| 106 |
| 107 WebKit::WebCryptoAlgorithmId algorithmId; |
| 108 const char* algorithmName; |
| 109 AlgorithmParamsForOperation paramsForOperation[NumberOfAlgorithmOperations]; |
| 110 }; |
| 111 |
| 112 // AlgorithmRegistry enumerates each of the different algorithms and its |
| 113 // parameters. This describes the same information as the static tables above, |
| 114 // but in a more convenient runtime form. |
| 115 class AlgorithmRegistry { |
| 116 public: |
| 117 static const AlgorithmInfo* lookupAlgorithmByName(const String& algorithmNam
e); |
| 118 |
| 119 private: |
| 120 AlgorithmRegistry(); |
| 121 |
| 122 // Algorithm name to ID. |
| 123 typedef HashMap<String, WebKit::WebCryptoAlgorithmId, CaseFoldingHash> Algor
ithmNameToIdMap; |
| 124 AlgorithmNameToIdMap m_algorithmNameToId; |
| 125 |
| 126 // Algorithm ID to information. |
| 127 AlgorithmInfo m_algorithms[WebKit::NumberOfWebCryptoAlgorithmId]; |
| 128 }; |
| 129 |
| 130 const AlgorithmInfo* AlgorithmRegistry::lookupAlgorithmByName(const String& algo
rithmName) |
| 131 { |
| 132 DEFINE_STATIC_LOCAL(AlgorithmRegistry, registry, ()); |
| 133 |
| 134 AlgorithmNameToIdMap::const_iterator it = registry.m_algorithmNameToId.find(
algorithmName); |
| 135 if (it == registry.m_algorithmNameToId.end()) |
| 136 return 0; |
| 137 return ®istry.m_algorithms[it->value]; |
| 138 } |
| 139 |
| 140 AlgorithmRegistry::AlgorithmRegistry() |
| 141 { |
| 142 for (size_t i = 0; i < WTF_ARRAY_LENGTH(algorithmNameMappings); ++i) { |
| 143 const AlgorithmNameMapping& mapping = algorithmNameMappings[i]; |
| 144 m_algorithmNameToId.add(mapping.algorithmName, mapping.algorithmId); |
| 145 m_algorithms[mapping.algorithmId].algorithmName = mapping.algorithmName; |
| 146 } |
| 147 |
| 148 for (size_t i = 0; i < WTF_ARRAY_LENGTH(operationParamsMappings); ++i) { |
| 149 const OperationParamsMapping& mapping = operationParamsMappings[i]; |
| 150 m_algorithms[mapping.algorithmId].paramsForOperation[mapping.operation]
= mapping.params; |
| 151 } |
| 152 } |
| 153 |
| 154 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseAesCbcParams(const Dictionary&
raw) |
| 155 { |
| 156 RefPtr<ArrayBufferView> iv; |
| 157 if (!raw.get("iv", iv) || !iv) |
| 158 return nullptr; |
| 159 |
| 160 if (iv->byteLength() != 16) |
| 161 return nullptr; |
| 162 |
| 163 return adoptPtr(new WebKit::WebCryptoAesCbcParams(static_cast<unsigned char*
>(iv->baseAddress()), iv->byteLength())); |
| 164 } |
| 165 |
| 166 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseAesKeyGenParams(const Dictiona
ry& raw) |
| 167 { |
| 168 int32_t length; |
| 169 if (!raw.get("length", length)) |
| 170 return nullptr; |
| 171 if (length < 0 || length > 0xFFFF) |
| 172 return nullptr; |
| 173 return adoptPtr(new WebKit::WebCryptoAesKeyGenParams(length)); |
| 174 } |
| 175 |
| 176 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseAlgorithmParams(const Dictiona
ry& raw, WebKit::WebCryptoAlgorithmParamsType type) |
| 177 { |
| 178 switch (type) { |
| 179 case WebKit::WebCryptoAlgorithmParamsTypeNone: |
| 180 return nullptr; |
| 181 case WebKit::WebCryptoAlgorithmParamsTypeAesCbcParams: |
| 182 return parseAesCbcParams(raw); |
| 183 case WebKit::WebCryptoAlgorithmParamsTypeAesKeyGenParams: |
| 184 return parseAesKeyGenParams(raw); |
| 185 } |
| 186 ASSERT_NOT_REACHED(); |
| 187 return nullptr; |
| 188 } |
| 189 |
| 190 } // namespace |
| 191 |
| 192 // FIXME: Throw the correct exception types! |
| 193 // This implementation corresponds with: |
| 194 // http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules |
| 195 bool normalizeAlgorithm(const Dictionary& raw, AlgorithmOperation op, WebKit::We
bCryptoAlgorithm& algorithm, ExceptionCode& ec) |
| 196 { |
| 197 String algorithmName; |
| 198 if (!raw.get("name", algorithmName)) { |
| 199 ec = NOT_SUPPORTED_ERR; |
| 200 return false; |
| 201 } |
| 202 |
| 203 if (!algorithmName.containsOnlyASCII()) { |
| 204 ec = SYNTAX_ERR; |
| 205 return false; |
| 206 } |
| 207 |
| 208 const AlgorithmInfo* info = AlgorithmRegistry::lookupAlgorithmByName(algorit
hmName); |
| 209 if (!info) { |
| 210 ec = NOT_SUPPORTED_ERR; |
| 211 return false; |
| 212 } |
| 213 |
| 214 if (info->paramsForOperation[op] == UnsupportedOp) { |
| 215 ec = NOT_SUPPORTED_ERR; |
| 216 return false; |
| 217 } |
| 218 |
| 219 WebKit::WebCryptoAlgorithmParamsType paramsType = static_cast<WebKit::WebCry
ptoAlgorithmParamsType>(info->paramsForOperation[op]); |
| 220 OwnPtr<WebKit::WebCryptoAlgorithmParams> params = parseAlgorithmParams(raw,
paramsType); |
| 221 |
| 222 if (!params && paramsType != WebKit::WebCryptoAlgorithmParamsTypeNone) { |
| 223 ec = NOT_SUPPORTED_ERR; |
| 224 return false; |
| 225 } |
| 226 |
| 227 algorithm = WebKit::WebCryptoAlgorithm(info->algorithmId, info->algorithmNam
e, params.release()); |
| 228 return true; |
| 229 } |
| 230 |
| 231 } // namespace WebCore |
OLD | NEW |