| 1 | /* |
| 2 | * Copyright (C) 2017 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 "ResourceCryptographicDigest.h" |
| 28 | |
| 29 | #include "ParsingUtilities.h" |
| 30 | #include <pal/crypto/CryptoDigest.h> |
| 31 | #include <wtf/Optional.h> |
| 32 | #include <wtf/text/Base64.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | template<typename CharacterType> |
| 37 | static bool parseHashAlgorithmAdvancingPosition(const CharacterType*& position, const CharacterType* end, ResourceCryptographicDigest::Algorithm& algorithm) |
| 38 | { |
| 39 | // FIXME: This would be much cleaner with a lookup table of pairs of label / algorithm enum values, but I can't |
| 40 | // figure out how to keep the labels compiletime strings for skipExactlyIgnoringASCIICase. |
| 41 | |
| 42 | if (skipExactlyIgnoringASCIICase(position, end, "sha256" )) { |
| 43 | algorithm = ResourceCryptographicDigest::Algorithm::SHA256; |
| 44 | return true; |
| 45 | } |
| 46 | if (skipExactlyIgnoringASCIICase(position, end, "sha384" )) { |
| 47 | algorithm = ResourceCryptographicDigest::Algorithm::SHA384; |
| 48 | return true; |
| 49 | } |
| 50 | if (skipExactlyIgnoringASCIICase(position, end, "sha512" )) { |
| 51 | algorithm = ResourceCryptographicDigest::Algorithm::SHA512; |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | template<typename CharacterType> |
| 59 | static Optional<ResourceCryptographicDigest> parseCryptographicDigestImpl(const CharacterType*& position, const CharacterType* end) |
| 60 | { |
| 61 | if (position == end) |
| 62 | return WTF::nullopt; |
| 63 | |
| 64 | ResourceCryptographicDigest::Algorithm algorithm; |
| 65 | if (!parseHashAlgorithmAdvancingPosition(position, end, algorithm)) |
| 66 | return WTF::nullopt; |
| 67 | |
| 68 | if (!skipExactly<CharacterType>(position, end, '-')) |
| 69 | return WTF::nullopt; |
| 70 | |
| 71 | const CharacterType* beginHashValue = position; |
| 72 | skipWhile<CharacterType, isBase64OrBase64URLCharacter>(position, end); |
| 73 | skipExactly<CharacterType>(position, end, '='); |
| 74 | skipExactly<CharacterType>(position, end, '='); |
| 75 | |
| 76 | if (position == beginHashValue) |
| 77 | return WTF::nullopt; |
| 78 | |
| 79 | Vector<uint8_t> digest; |
| 80 | StringView hashValue(beginHashValue, position - beginHashValue); |
| 81 | if (!base64Decode(hashValue, digest, Base64ValidatePadding)) { |
| 82 | if (!base64URLDecode(hashValue, digest)) |
| 83 | return WTF::nullopt; |
| 84 | } |
| 85 | |
| 86 | return ResourceCryptographicDigest { algorithm, WTFMove(digest) }; |
| 87 | } |
| 88 | |
| 89 | Optional<ResourceCryptographicDigest> parseCryptographicDigest(const UChar*& begin, const UChar* end) |
| 90 | { |
| 91 | return parseCryptographicDigestImpl(begin, end); |
| 92 | } |
| 93 | |
| 94 | Optional<ResourceCryptographicDigest> parseCryptographicDigest(const LChar*& begin, const LChar* end) |
| 95 | { |
| 96 | return parseCryptographicDigestImpl(begin, end); |
| 97 | } |
| 98 | |
| 99 | template<typename CharacterType> |
| 100 | static Optional<EncodedResourceCryptographicDigest> parseEncodedCryptographicDigestImpl(const CharacterType*& position, const CharacterType* end) |
| 101 | { |
| 102 | if (position == end) |
| 103 | return WTF::nullopt; |
| 104 | |
| 105 | EncodedResourceCryptographicDigest::Algorithm algorithm; |
| 106 | if (!parseHashAlgorithmAdvancingPosition(position, end, algorithm)) |
| 107 | return WTF::nullopt; |
| 108 | |
| 109 | if (!skipExactly<CharacterType>(position, end, '-')) |
| 110 | return WTF::nullopt; |
| 111 | |
| 112 | const CharacterType* beginHashValue = position; |
| 113 | skipWhile<CharacterType, isBase64OrBase64URLCharacter>(position, end); |
| 114 | skipExactly<CharacterType>(position, end, '='); |
| 115 | skipExactly<CharacterType>(position, end, '='); |
| 116 | |
| 117 | if (position == beginHashValue) |
| 118 | return WTF::nullopt; |
| 119 | |
| 120 | return EncodedResourceCryptographicDigest { algorithm, String(beginHashValue, position - beginHashValue) }; |
| 121 | } |
| 122 | |
| 123 | Optional<EncodedResourceCryptographicDigest> parseEncodedCryptographicDigest(const UChar*& begin, const UChar* end) |
| 124 | { |
| 125 | return parseEncodedCryptographicDigestImpl(begin, end); |
| 126 | } |
| 127 | |
| 128 | Optional<EncodedResourceCryptographicDigest> parseEncodedCryptographicDigest(const LChar*& begin, const LChar* end) |
| 129 | { |
| 130 | return parseEncodedCryptographicDigestImpl(begin, end); |
| 131 | } |
| 132 | |
| 133 | Optional<ResourceCryptographicDigest> decodeEncodedResourceCryptographicDigest(const EncodedResourceCryptographicDigest& encodedDigest) |
| 134 | { |
| 135 | Vector<uint8_t> digest; |
| 136 | if (!base64Decode(encodedDigest.digest, digest, Base64ValidatePadding)) { |
| 137 | if (!base64URLDecode(encodedDigest.digest, digest)) |
| 138 | return WTF::nullopt; |
| 139 | } |
| 140 | |
| 141 | return ResourceCryptographicDigest { encodedDigest.algorithm, WTFMove(digest) }; |
| 142 | } |
| 143 | |
| 144 | static PAL::CryptoDigest::Algorithm toCryptoDigestAlgorithm(ResourceCryptographicDigest::Algorithm algorithm) |
| 145 | { |
| 146 | switch (algorithm) { |
| 147 | case ResourceCryptographicDigest::Algorithm::SHA256: |
| 148 | return PAL::CryptoDigest::Algorithm::SHA_256; |
| 149 | case ResourceCryptographicDigest::Algorithm::SHA384: |
| 150 | return PAL::CryptoDigest::Algorithm::SHA_384; |
| 151 | case ResourceCryptographicDigest::Algorithm::SHA512: |
| 152 | return PAL::CryptoDigest::Algorithm::SHA_512; |
| 153 | } |
| 154 | ASSERT_NOT_REACHED(); |
| 155 | return PAL::CryptoDigest::Algorithm::SHA_512; |
| 156 | } |
| 157 | |
| 158 | ResourceCryptographicDigest cryptographicDigestForBytes(ResourceCryptographicDigest::Algorithm algorithm, const void* bytes, size_t length) |
| 159 | { |
| 160 | auto cryptoDigest = PAL::CryptoDigest::create(toCryptoDigestAlgorithm(algorithm)); |
| 161 | cryptoDigest->addBytes(bytes, length); |
| 162 | return { algorithm, cryptoDigest->computeHash() }; |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |