| 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 | #pragma once |
| 29 | |
| 30 | #include "CryptoAlgorithmIdentifier.h" |
| 31 | #include <array> |
| 32 | #include <cstring> |
| 33 | #include <gcrypt.h> |
| 34 | #include <pal/crypto/CryptoDigest.h> |
| 35 | #include <pal/crypto/gcrypt/Handle.h> |
| 36 | #include <pal/crypto/gcrypt/Utilities.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | namespace CryptoConstants { |
| 41 | |
| 42 | static const std::array<uint8_t, 18> s_ecPublicKeyIdentifier { { "1.2.840.10045.2.1" } }; |
| 43 | static const std::array<uint8_t, 13> s_ecDHIdentifier { { "1.3.132.1.12" } }; |
| 44 | |
| 45 | static const std::array<uint8_t, 20> s_secp256r1Identifier { { "1.2.840.10045.3.1.7" } }; |
| 46 | static const std::array<uint8_t, 13> s_secp384r1Identifier { { "1.3.132.0.34" } }; |
| 47 | static const std::array<uint8_t, 13> s_secp521r1Identifier { { "1.3.132.0.35" } }; |
| 48 | |
| 49 | static const std::array<uint8_t, 21> s_rsaEncryptionIdentifier { { "1.2.840.113549.1.1.1" } }; |
| 50 | static const std::array<uint8_t, 21> s_RSAES_OAEPIdentifier { { "1.2.840.113549.1.1.7" } }; |
| 51 | static const std::array<uint8_t, 22> s_RSASSA_PSSIdentifier { { "1.2.840.113549.1.1.10" } }; |
| 52 | |
| 53 | static const std::array<uint8_t, 2> s_asn1NullValue { { 0x05, 0x00 } }; |
| 54 | static const std::array<uint8_t, 1> s_asn1Version0 { { 0x00 } }; |
| 55 | static const std::array<uint8_t, 1> s_asn1Version1 { { 0x01 } }; |
| 56 | |
| 57 | static const std::array<uint8_t, 1> s_ecUncompressedFormatLeadingByte { { 0x04 } }; |
| 58 | |
| 59 | template<size_t N> |
| 60 | static inline bool matches(const void* lhs, size_t size, const std::array<uint8_t, N>& rhs) |
| 61 | { |
| 62 | if (size != rhs.size()) |
| 63 | return false; |
| 64 | |
| 65 | return !std::memcmp(lhs, rhs.data(), rhs.size()); |
| 66 | } |
| 67 | |
| 68 | } // namespace CryptoConstants |
| 69 | |
| 70 | Optional<const char*> hashAlgorithmName(CryptoAlgorithmIdentifier); |
| 71 | |
| 72 | Optional<int> hmacAlgorithm(CryptoAlgorithmIdentifier); |
| 73 | Optional<int> digestAlgorithm(CryptoAlgorithmIdentifier); |
| 74 | Optional<PAL::CryptoDigest::Algorithm> hashCryptoDigestAlgorithm(CryptoAlgorithmIdentifier); |
| 75 | |
| 76 | Optional<size_t> mpiLength(gcry_mpi_t); |
| 77 | Optional<size_t> mpiLength(gcry_sexp_t); |
| 78 | Optional<Vector<uint8_t>> mpiData(gcry_mpi_t); |
| 79 | Optional<Vector<uint8_t>> mpiZeroPrefixedData(gcry_mpi_t, size_t targetLength); |
| 80 | Optional<Vector<uint8_t>> mpiData(gcry_sexp_t); |
| 81 | Optional<Vector<uint8_t>> mpiZeroPrefixedData(gcry_sexp_t, size_t targetLength); |
| 82 | Optional<Vector<uint8_t>> mpiSignedData(gcry_mpi_t); |
| 83 | Optional<Vector<uint8_t>> mpiSignedData(gcry_sexp_t); |
| 84 | |
| 85 | } // namespace WebCore |
| 86 | |