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 "GCryptUtilities.h" |
30 | |
31 | #include <wtf/Optional.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | Optional<const char*> hashAlgorithmName(CryptoAlgorithmIdentifier identifier) |
36 | { |
37 | switch (identifier) { |
38 | case CryptoAlgorithmIdentifier::SHA_1: |
39 | return "sha1" ; |
40 | case CryptoAlgorithmIdentifier::SHA_224: |
41 | return "sha224" ; |
42 | case CryptoAlgorithmIdentifier::SHA_256: |
43 | return "sha256" ; |
44 | case CryptoAlgorithmIdentifier::SHA_384: |
45 | return "sha384" ; |
46 | case CryptoAlgorithmIdentifier::SHA_512: |
47 | return "sha512" ; |
48 | default: |
49 | return WTF::nullopt; |
50 | } |
51 | } |
52 | |
53 | Optional<int> hmacAlgorithm(CryptoAlgorithmIdentifier identifier) |
54 | { |
55 | switch (identifier) { |
56 | case CryptoAlgorithmIdentifier::SHA_1: |
57 | return GCRY_MAC_HMAC_SHA1; |
58 | case CryptoAlgorithmIdentifier::SHA_224: |
59 | return GCRY_MAC_HMAC_SHA224; |
60 | case CryptoAlgorithmIdentifier::SHA_256: |
61 | return GCRY_MAC_HMAC_SHA256; |
62 | case CryptoAlgorithmIdentifier::SHA_384: |
63 | return GCRY_MAC_HMAC_SHA384; |
64 | case CryptoAlgorithmIdentifier::SHA_512: |
65 | return GCRY_MAC_HMAC_SHA512; |
66 | default: |
67 | return WTF::nullopt; |
68 | } |
69 | } |
70 | |
71 | Optional<int> digestAlgorithm(CryptoAlgorithmIdentifier identifier) |
72 | { |
73 | switch (identifier) { |
74 | case CryptoAlgorithmIdentifier::SHA_1: |
75 | return GCRY_MD_SHA1; |
76 | case CryptoAlgorithmIdentifier::SHA_224: |
77 | return GCRY_MD_SHA224; |
78 | case CryptoAlgorithmIdentifier::SHA_256: |
79 | return GCRY_MD_SHA256; |
80 | case CryptoAlgorithmIdentifier::SHA_384: |
81 | return GCRY_MD_SHA384; |
82 | case CryptoAlgorithmIdentifier::SHA_512: |
83 | return GCRY_MD_SHA512; |
84 | default: |
85 | return WTF::nullopt; |
86 | } |
87 | } |
88 | |
89 | Optional<PAL::CryptoDigest::Algorithm> hashCryptoDigestAlgorithm(CryptoAlgorithmIdentifier identifier) |
90 | { |
91 | switch (identifier) { |
92 | case CryptoAlgorithmIdentifier::SHA_1: |
93 | return PAL::CryptoDigest::Algorithm::SHA_1; |
94 | case CryptoAlgorithmIdentifier::SHA_224: |
95 | return PAL::CryptoDigest::Algorithm::SHA_224; |
96 | case CryptoAlgorithmIdentifier::SHA_256: |
97 | return PAL::CryptoDigest::Algorithm::SHA_256; |
98 | case CryptoAlgorithmIdentifier::SHA_384: |
99 | return PAL::CryptoDigest::Algorithm::SHA_384; |
100 | case CryptoAlgorithmIdentifier::SHA_512: |
101 | return PAL::CryptoDigest::Algorithm::SHA_512; |
102 | default: |
103 | return WTF::nullopt; |
104 | } |
105 | } |
106 | |
107 | Optional<size_t> mpiLength(gcry_mpi_t paramMPI) |
108 | { |
109 | // Retrieve the MPI length for the unsigned format. |
110 | size_t dataLength = 0; |
111 | gcry_error_t error = gcry_mpi_print(GCRYMPI_FMT_USG, nullptr, 0, &dataLength, paramMPI); |
112 | if (error != GPG_ERR_NO_ERROR) { |
113 | PAL::GCrypt::logError(error); |
114 | return WTF::nullopt; |
115 | } |
116 | |
117 | return dataLength; |
118 | } |
119 | |
120 | Optional<size_t> mpiLength(gcry_sexp_t paramSexp) |
121 | { |
122 | // Retrieve the MPI value stored in the s-expression: (name mpi-data) |
123 | PAL::GCrypt::Handle<gcry_mpi_t> paramMPI(gcry_sexp_nth_mpi(paramSexp, 1, GCRYMPI_FMT_USG)); |
124 | if (!paramMPI) |
125 | return WTF::nullopt; |
126 | |
127 | return mpiLength(paramMPI); |
128 | } |
129 | |
130 | Optional<Vector<uint8_t>> mpiData(gcry_mpi_t paramMPI) |
131 | { |
132 | // Retrieve the MPI length. |
133 | auto length = mpiLength(paramMPI); |
134 | if (!length) |
135 | return WTF::nullopt; |
136 | |
137 | // Copy the MPI data into a properly-sized buffer. |
138 | Vector<uint8_t> output(*length); |
139 | gcry_error_t error = gcry_mpi_print(GCRYMPI_FMT_USG, output.data(), output.size(), nullptr, paramMPI); |
140 | if (error != GPG_ERR_NO_ERROR) { |
141 | PAL::GCrypt::logError(error); |
142 | return WTF::nullopt; |
143 | } |
144 | |
145 | return output; |
146 | } |
147 | |
148 | Optional<Vector<uint8_t>> mpiZeroPrefixedData(gcry_mpi_t paramMPI, size_t targetLength) |
149 | { |
150 | // Retrieve the MPI length. Bail if the retrieved length is longer than target length. |
151 | auto length = mpiLength(paramMPI); |
152 | if (!length || *length > targetLength) |
153 | return WTF::nullopt; |
154 | |
155 | // Fill out the output buffer with zeros. Properly determine the zero prefix length, |
156 | // and copy the MPI data into memory area following the prefix (if any). |
157 | Vector<uint8_t> output(targetLength, 0); |
158 | size_t prefixLength = targetLength - *length; |
159 | gcry_error_t error = gcry_mpi_print(GCRYMPI_FMT_USG, output.data() + prefixLength, targetLength, nullptr, paramMPI); |
160 | if (error != GPG_ERR_NO_ERROR) { |
161 | PAL::GCrypt::logError(error); |
162 | return WTF::nullopt; |
163 | } |
164 | |
165 | return output; |
166 | } |
167 | |
168 | Optional<Vector<uint8_t>> mpiData(gcry_sexp_t paramSexp) |
169 | { |
170 | // Retrieve the MPI value stored in the s-expression: (name mpi-data) |
171 | PAL::GCrypt::Handle<gcry_mpi_t> paramMPI(gcry_sexp_nth_mpi(paramSexp, 1, GCRYMPI_FMT_USG)); |
172 | if (!paramMPI) |
173 | return WTF::nullopt; |
174 | |
175 | return mpiData(paramMPI); |
176 | } |
177 | |
178 | Optional<Vector<uint8_t>> mpiZeroPrefixedData(gcry_sexp_t paramSexp, size_t targetLength) |
179 | { |
180 | // Retrieve the MPI value stored in the s-expression: (name mpi-data) |
181 | PAL::GCrypt::Handle<gcry_mpi_t> paramMPI(gcry_sexp_nth_mpi(paramSexp, 1, GCRYMPI_FMT_USG)); |
182 | if (!paramMPI) |
183 | return WTF::nullopt; |
184 | |
185 | return mpiZeroPrefixedData(paramMPI, targetLength); |
186 | } |
187 | |
188 | Optional<Vector<uint8_t>> mpiSignedData(gcry_mpi_t mpi) |
189 | { |
190 | auto data = mpiData(mpi); |
191 | if (!data) |
192 | return WTF::nullopt; |
193 | |
194 | if (data->at(0) & 0x80) |
195 | data->insert(0, 0x00); |
196 | |
197 | return data; |
198 | } |
199 | |
200 | Optional<Vector<uint8_t>> mpiSignedData(gcry_sexp_t paramSexp) |
201 | { |
202 | auto data = mpiData(paramSexp); |
203 | if (!data) |
204 | return WTF::nullopt; |
205 | |
206 | if (data->at(0) & 0x80) |
207 | data->insert(0, 0x00); |
208 | |
209 | return data; |
210 | } |
211 | |
212 | } // namespace WebCore |
213 | |