| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2017 Metrological Group B.V. |
| 4 | * Copyright (C) 2017 Igalia S.L. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 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 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "CryptoAlgorithmECDSA.h" |
| 30 | |
| 31 | #if ENABLE(WEB_CRYPTO) |
| 32 | |
| 33 | #include "CryptoAlgorithmEcdsaParams.h" |
| 34 | #include "CryptoKeyEC.h" |
| 35 | #include "GCryptUtilities.h" |
| 36 | #include <pal/crypto/CryptoDigest.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | static bool (Vector<uint8_t>& signature, gcry_sexp_t signatureSexp, const char* integerName, size_t keySizeInBytes) |
| 41 | { |
| 42 | // Retrieve byte data of the specified integer. |
| 43 | PAL::GCrypt::Handle<gcry_sexp_t> integerSexp(gcry_sexp_find_token(signatureSexp, integerName, 0)); |
| 44 | if (!integerSexp) |
| 45 | return false; |
| 46 | |
| 47 | auto integerData = mpiData(integerSexp); |
| 48 | if (!integerData) |
| 49 | return false; |
| 50 | |
| 51 | size_t dataSize = integerData->size(); |
| 52 | if (dataSize >= keySizeInBytes) { |
| 53 | // Append the last `keySizeInBytes` bytes of the data Vector, if available. |
| 54 | signature.append(&integerData->at(dataSize - keySizeInBytes), keySizeInBytes); |
| 55 | } else { |
| 56 | // If not, prefix the binary data with zero bytes. |
| 57 | for (size_t paddingSize = keySizeInBytes - dataSize; paddingSize > 0; --paddingSize) |
| 58 | signature.uncheckedAppend(0x00); |
| 59 | signature.appendVector(*integerData); |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | static Optional<Vector<uint8_t>> gcryptSign(gcry_sexp_t keySexp, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t keySizeInBytes) |
| 66 | { |
| 67 | // Perform digest operation with the specified algorithm on the given data. |
| 68 | Vector<uint8_t> dataHash; |
| 69 | { |
| 70 | auto digestAlgorithm = hashCryptoDigestAlgorithm(hashAlgorithmIdentifier); |
| 71 | if (!digestAlgorithm) |
| 72 | return WTF::nullopt; |
| 73 | |
| 74 | auto digest = PAL::CryptoDigest::create(*digestAlgorithm); |
| 75 | if (!digest) |
| 76 | return WTF::nullopt; |
| 77 | |
| 78 | digest->addBytes(data.data(), data.size()); |
| 79 | dataHash = digest->computeHash(); |
| 80 | } |
| 81 | |
| 82 | // Construct the data s-expression that contains raw hashed data. |
| 83 | PAL::GCrypt::Handle<gcry_sexp_t> dataSexp; |
| 84 | { |
| 85 | auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier); |
| 86 | if (!shaAlgorithm) |
| 87 | return WTF::nullopt; |
| 88 | |
| 89 | gcry_error_t error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags raw)(hash %s %b))" , |
| 90 | *shaAlgorithm, dataHash.size(), dataHash.data()); |
| 91 | if (error != GPG_ERR_NO_ERROR) { |
| 92 | PAL::GCrypt::logError(error); |
| 93 | return WTF::nullopt; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Perform the PK signing, retrieving a sig-val s-expression of the following form: |
| 98 | // (sig-val |
| 99 | // (dsa |
| 100 | // (r r-mpi) |
| 101 | // (s s-mpi))) |
| 102 | PAL::GCrypt::Handle<gcry_sexp_t> signatureSexp; |
| 103 | gcry_error_t error = gcry_pk_sign(&signatureSexp, dataSexp, keySexp); |
| 104 | if (error != GPG_ERR_NO_ERROR) { |
| 105 | PAL::GCrypt::logError(error); |
| 106 | return WTF::nullopt; |
| 107 | } |
| 108 | |
| 109 | // Retrieve MPI data of the resulting r and s integers. They are concatenated into |
| 110 | // a single buffer, properly accounting for integers that don't match the key in size. |
| 111 | Vector<uint8_t> signature; |
| 112 | signature.reserveInitialCapacity(keySizeInBytes * 2); |
| 113 | |
| 114 | if (!extractECDSASignatureInteger(signature, signatureSexp, "r" , keySizeInBytes) |
| 115 | || !extractECDSASignatureInteger(signature, signatureSexp, "s" , keySizeInBytes)) |
| 116 | return WTF::nullopt; |
| 117 | |
| 118 | return signature; |
| 119 | } |
| 120 | |
| 121 | static Optional<bool> gcryptVerify(gcry_sexp_t keySexp, const Vector<uint8_t>& signature, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t keySizeInBytes) |
| 122 | { |
| 123 | // Bail if the signature size isn't double the key size (i.e. concatenated r and s components). |
| 124 | if (signature.size() != keySizeInBytes * 2) |
| 125 | return false; |
| 126 | |
| 127 | // Perform digest operation with the specified algorithm on the given data. |
| 128 | Vector<uint8_t> dataHash; |
| 129 | { |
| 130 | auto digestAlgorithm = hashCryptoDigestAlgorithm(hashAlgorithmIdentifier); |
| 131 | if (!digestAlgorithm) |
| 132 | return WTF::nullopt; |
| 133 | |
| 134 | auto digest = PAL::CryptoDigest::create(*digestAlgorithm); |
| 135 | if (!digest) |
| 136 | return WTF::nullopt; |
| 137 | |
| 138 | digest->addBytes(data.data(), data.size()); |
| 139 | dataHash = digest->computeHash(); |
| 140 | } |
| 141 | |
| 142 | // Construct the sig-val s-expression, extracting the r and s components from the signature vector. |
| 143 | PAL::GCrypt::Handle<gcry_sexp_t> signatureSexp; |
| 144 | gcry_error_t error = gcry_sexp_build(&signatureSexp, nullptr, "(sig-val(ecdsa(r %b)(s %b)))" , |
| 145 | keySizeInBytes, signature.data(), keySizeInBytes, signature.data() + keySizeInBytes); |
| 146 | if (error != GPG_ERR_NO_ERROR) { |
| 147 | PAL::GCrypt::logError(error); |
| 148 | return WTF::nullopt; |
| 149 | } |
| 150 | |
| 151 | // Construct the data s-expression that contains raw hashed data. |
| 152 | PAL::GCrypt::Handle<gcry_sexp_t> dataSexp; |
| 153 | { |
| 154 | auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier); |
| 155 | if (!shaAlgorithm) |
| 156 | return WTF::nullopt; |
| 157 | |
| 158 | error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags raw)(hash %s %b))" , |
| 159 | *shaAlgorithm, dataHash.size(), dataHash.data()); |
| 160 | if (error != GPG_ERR_NO_ERROR) { |
| 161 | PAL::GCrypt::logError(error); |
| 162 | return WTF::nullopt; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Perform the PK verification. We report success if there's no error returned, or |
| 167 | // a failure in any other case. OperationError should not be returned at this point, |
| 168 | // avoiding spilling information about the exact cause of verification failure. |
| 169 | error = gcry_pk_verify(signatureSexp, dataSexp, keySexp); |
| 170 | return { error == GPG_ERR_NO_ERROR }; |
| 171 | } |
| 172 | |
| 173 | ExceptionOr<Vector<uint8_t>> CryptoAlgorithmECDSA::platformSign(const CryptoAlgorithmEcdsaParams& parameters, const CryptoKeyEC& key, const Vector<uint8_t>& data) |
| 174 | { |
| 175 | auto output = gcryptSign(key.platformKey(), data, parameters.hashIdentifier, (key.keySizeInBits() + 7) / 8); |
| 176 | if (!output) |
| 177 | return Exception { OperationError }; |
| 178 | return WTFMove(*output); |
| 179 | } |
| 180 | |
| 181 | ExceptionOr<bool> CryptoAlgorithmECDSA::platformVerify(const CryptoAlgorithmEcdsaParams& parameters, const CryptoKeyEC& key, const Vector<uint8_t>& signature, const Vector<uint8_t>& data) |
| 182 | { |
| 183 | auto output = gcryptVerify(key.platformKey(), signature, data, parameters.hashIdentifier, (key.keySizeInBits() + 7)/ 8); |
| 184 | if (!output) |
| 185 | return Exception { OperationError }; |
| 186 | return WTFMove(*output); |
| 187 | } |
| 188 | |
| 189 | } // namespace WebCore |
| 190 | |
| 191 | #endif // ENABLE(WEB_CRYPTO) |
| 192 | |