aws_lc_rs/rsa/
signature.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0 OR ISC
3
4use std::fmt::{self, Debug, Formatter};
5use std::ops::RangeInclusive;
6
7use crate::aws_lc::{
8    EVP_PKEY_CTX_set_rsa_padding, EVP_PKEY_CTX_set_rsa_pss_saltlen, EVP_PKEY_CTX_set_signature_md,
9    RSA_bits, EVP_PKEY, EVP_PKEY_CTX, RSA_PKCS1_PSS_PADDING, RSA_PSS_SALTLEN_DIGEST,
10};
11
12use crate::digest::{self, match_digest_type, Digest};
13use crate::error::Unspecified;
14use crate::ptr::LcPtr;
15use crate::sealed::Sealed;
16use crate::signature::{ParsedPublicKey, ParsedVerificationAlgorithm, VerificationAlgorithm};
17
18use super::encoding;
19#[cfg(feature = "ring-sig-verify")]
20use untrusted::Input;
21
22#[allow(non_camel_case_types)]
23#[allow(clippy::module_name_repetitions)]
24#[derive(Debug)]
25pub enum RsaPadding {
26    RSA_PKCS1_PADDING,
27    RSA_PKCS1_PSS_PADDING,
28}
29
30/// Parameters for RSA verification.
31pub struct RsaParameters(
32    &'static digest::Algorithm,
33    &'static RsaPadding,
34    RangeInclusive<u32>,
35    &'static RsaVerificationAlgorithmId,
36);
37
38impl RsaParameters {
39    #[inline]
40    pub(crate) fn digest_algorithm(&self) -> &'static digest::Algorithm {
41        self.0
42    }
43
44    #[inline]
45    pub(crate) fn padding(&self) -> &'static RsaPadding {
46        self.1
47    }
48
49    #[inline]
50    pub(crate) fn bit_size_range(&self) -> &RangeInclusive<u32> {
51        &self.2
52    }
53}
54
55impl ParsedVerificationAlgorithm for RsaParameters {
56    fn parsed_verify_sig(
57        &self,
58        public_key: &ParsedPublicKey,
59        msg: &[u8],
60        signature: &[u8],
61    ) -> Result<(), Unspecified> {
62        let evp_pkey = public_key.key();
63        verify_rsa_signature(
64            self.digest_algorithm(),
65            self.padding(),
66            evp_pkey,
67            msg,
68            signature,
69            self.bit_size_range(),
70        )
71    }
72
73    fn parsed_verify_digest_sig(
74        &self,
75        public_key: &ParsedPublicKey,
76        digest: &Digest,
77        signature: &[u8],
78    ) -> Result<(), Unspecified> {
79        let evp_pkey = public_key.key();
80        verify_rsa_digest_signature(
81            self.padding(),
82            evp_pkey,
83            digest,
84            signature,
85            self.bit_size_range(),
86        )
87    }
88}
89
90impl VerificationAlgorithm for RsaParameters {
91    #[cfg(feature = "ring-sig-verify")]
92    fn verify(
93        &self,
94        public_key: Input<'_>,
95        msg: Input<'_>,
96        signature: Input<'_>,
97    ) -> Result<(), Unspecified> {
98        self.verify_sig(
99            public_key.as_slice_less_safe(),
100            msg.as_slice_less_safe(),
101            signature.as_slice_less_safe(),
102        )
103    }
104
105    fn verify_sig(
106        &self,
107        public_key: &[u8],
108        msg: &[u8],
109        signature: &[u8],
110    ) -> Result<(), Unspecified> {
111        let evp_pkey = encoding::rfc8017::decode_public_key_der(public_key)?;
112        verify_rsa_signature(
113            self.digest_algorithm(),
114            self.padding(),
115            &evp_pkey,
116            msg,
117            signature,
118            self.bit_size_range(),
119        )
120    }
121
122    fn verify_digest_sig(
123        &self,
124        public_key: &[u8],
125        digest: &Digest,
126        signature: &[u8],
127    ) -> Result<(), Unspecified> {
128        if self.digest_algorithm() != digest.algorithm() {
129            return Err(Unspecified);
130        }
131        let evp_pkey = encoding::rfc8017::decode_public_key_der(public_key)?;
132        verify_rsa_digest_signature(
133            self.padding(),
134            &evp_pkey,
135            digest,
136            signature,
137            self.bit_size_range(),
138        )
139    }
140}
141
142impl Sealed for RsaParameters {}
143
144impl Debug for RsaParameters {
145    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
146        f.write_str(&format!("{{ {:?} }}", self.3))
147    }
148}
149
150impl RsaParameters {
151    pub(crate) const fn new(
152        digest_alg: &'static digest::Algorithm,
153        padding: &'static RsaPadding,
154        range: RangeInclusive<u32>,
155        verification_alg: &'static RsaVerificationAlgorithmId,
156    ) -> Self {
157        Self(digest_alg, padding, range, verification_alg)
158    }
159
160    /// Parses a DER-encoded `RSAPublicKey` structure (RFC 8017) to determine its size in bits.
161    ///
162    /// # Errors
163    /// `error::Unspecified` on parse error.
164    pub fn public_modulus_len(public_key: &[u8]) -> Result<u32, Unspecified> {
165        let rsa = encoding::rfc8017::decode_public_key_der(public_key)?;
166        Ok(unsafe { RSA_bits(*rsa.as_const().get_rsa()?) })
167    }
168
169    #[must_use]
170    /// Minimum modulus length in bits.
171    pub fn min_modulus_len(&self) -> u32 {
172        *self.2.start()
173    }
174
175    #[must_use]
176    /// Maximum modulus length in bits.
177    pub fn max_modulus_len(&self) -> u32 {
178        *self.2.end()
179    }
180}
181
182#[derive(Debug)]
183#[allow(non_camel_case_types)]
184pub(crate) enum RsaVerificationAlgorithmId {
185    RSA_PKCS1_1024_8192_SHA1_FOR_LEGACY_USE_ONLY,
186    RSA_PKCS1_1024_8192_SHA256_FOR_LEGACY_USE_ONLY,
187    RSA_PKCS1_1024_8192_SHA512_FOR_LEGACY_USE_ONLY,
188    RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY,
189    RSA_PKCS1_2048_8192_SHA256,
190    RSA_PKCS1_2048_8192_SHA384,
191    RSA_PKCS1_2048_8192_SHA512,
192    RSA_PKCS1_3072_8192_SHA384,
193    RSA_PSS_2048_8192_SHA256,
194    RSA_PSS_2048_8192_SHA384,
195    RSA_PSS_2048_8192_SHA512,
196}
197
198#[derive(Debug)]
199#[allow(non_camel_case_types)]
200pub(crate) enum RsaSigningAlgorithmId {
201    RSA_PSS_SHA256,
202    RSA_PSS_SHA384,
203    RSA_PSS_SHA512,
204    RSA_PKCS1_SHA256,
205    RSA_PKCS1_SHA384,
206    RSA_PKCS1_SHA512,
207}
208
209#[allow(clippy::module_name_repetitions)]
210/// Encoding type for an RSA signature
211pub struct RsaSignatureEncoding(
212    &'static digest::Algorithm,
213    &'static RsaPadding,
214    &'static RsaSigningAlgorithmId,
215);
216
217impl RsaSignatureEncoding {
218    pub(crate) const fn new(
219        digest_alg: &'static digest::Algorithm,
220        padding: &'static RsaPadding,
221        sig_alg: &'static RsaSigningAlgorithmId,
222    ) -> Self {
223        Self(digest_alg, padding, sig_alg)
224    }
225
226    #[inline]
227    pub(super) fn digest_algorithm(&self) -> &'static digest::Algorithm {
228        self.0
229    }
230
231    #[inline]
232    pub(super) fn padding(&self) -> &'static RsaPadding {
233        self.1
234    }
235}
236
237impl Sealed for RsaSignatureEncoding {}
238
239/// An RSA signature encoding as described in [RFC 3447 Section 8].
240///
241/// [RFC 3447 Section 8]: https://tools.ietf.org/html/rfc3447#section-8
242#[allow(clippy::module_name_repetitions)]
243pub trait RsaEncoding: 'static + Sync + Sealed + Debug {
244    /// The signature encoding.
245    fn encoding(&'static self) -> &'static RsaSignatureEncoding;
246}
247
248impl RsaEncoding for RsaSignatureEncoding {
249    fn encoding(&'static self) -> &'static RsaSignatureEncoding {
250        self
251    }
252}
253
254impl Debug for RsaSignatureEncoding {
255    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
256        f.write_str(&format!("{{ {:?} }}", self.2))
257    }
258}
259
260#[inline]
261pub(crate) fn configure_rsa_pkcs1_pss_padding(pctx: *mut EVP_PKEY_CTX) -> Result<(), ()> {
262    if 1 != unsafe { EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) } {
263        return Err(());
264    }
265    if 1 != unsafe { EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) } {
266        return Err(());
267    }
268    Ok(())
269}
270
271#[inline]
272pub(crate) fn verify_rsa_signature(
273    algorithm: &'static digest::Algorithm,
274    padding: &'static RsaPadding,
275    public_key: &LcPtr<EVP_PKEY>,
276    msg: &[u8],
277    signature: &[u8],
278    allowed_bit_size: &RangeInclusive<u32>,
279) -> Result<(), Unspecified> {
280    if !allowed_bit_size.contains(&public_key.as_const().key_size_bits().try_into()?) {
281        return Err(Unspecified);
282    }
283
284    let padding_fn = if let RsaPadding::RSA_PKCS1_PSS_PADDING = padding {
285        Some(configure_rsa_pkcs1_pss_padding)
286    } else {
287        None
288    };
289
290    public_key.verify(msg, Some(algorithm), padding_fn, signature)
291}
292
293#[inline]
294pub(crate) fn verify_rsa_digest_signature(
295    padding: &'static RsaPadding,
296    public_key: &LcPtr<EVP_PKEY>,
297    digest: &Digest,
298    signature: &[u8],
299    allowed_bit_size: &RangeInclusive<u32>,
300) -> Result<(), Unspecified> {
301    if !allowed_bit_size.contains(&public_key.as_const().key_size_bits().try_into()?) {
302        return Err(Unspecified);
303    }
304
305    let padding_fn = Some({
306        |pctx: *mut EVP_PKEY_CTX| {
307            let evp_md = match_digest_type(&digest.algorithm().id);
308            if 1 != unsafe { EVP_PKEY_CTX_set_signature_md(pctx, *evp_md) } {
309                return Err(());
310            }
311            if let RsaPadding::RSA_PKCS1_PSS_PADDING = padding {
312                configure_rsa_pkcs1_pss_padding(pctx)
313            } else {
314                Ok(())
315            }
316        }
317    });
318
319    public_key.verify_digest_sig(digest, padding_fn, signature)
320}