Skip to content

Commit d79c577

Browse files
authored
spki: use Result for Dyn*AlgorithmIdentifier traits (#974)
There is a possibility that creating the AlgorithmIdentifier results in the Error. Change corresponding methods to return Result<AlgorithmIdentifier>. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent 94cc741 commit d79c577

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

spki/src/traits.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{AlgorithmIdentifier, Error, Result, SubjectPublicKeyInfoRef};
55
#[cfg(feature = "alloc")]
66
use {
77
crate::AlgorithmIdentifierOwned,
8-
der::{Any, Document},
8+
der::{Any, Document, EncodeValue, Tagged},
99
};
1010

1111
#[cfg(feature = "pem")]
@@ -114,20 +114,24 @@ pub trait AssociatedAlgorithmIdentifier {
114114
#[cfg(feature = "alloc")]
115115
pub trait DynAssociatedAlgorithmIdentifier {
116116
/// `AlgorithmIdentifier` for this structure.
117-
fn algorithm_identifier(&self) -> AlgorithmIdentifierOwned;
117+
fn algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned>;
118118
}
119119

120120
#[cfg(feature = "alloc")]
121121
impl<T> DynAssociatedAlgorithmIdentifier for T
122122
where
123123
T: AssociatedAlgorithmIdentifier,
124-
T::Params: Into<Any>,
124+
T::Params: Tagged + EncodeValue,
125125
{
126-
fn algorithm_identifier(&self) -> AlgorithmIdentifierOwned {
127-
AlgorithmIdentifierOwned {
126+
fn algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned> {
127+
Ok(AlgorithmIdentifierOwned {
128128
oid: T::ALGORITHM_IDENTIFIER.oid,
129-
parameters: T::ALGORITHM_IDENTIFIER.parameters.map(Into::into),
130-
}
129+
parameters: T::ALGORITHM_IDENTIFIER
130+
.parameters
131+
.as_ref()
132+
.map(Any::encode_from)
133+
.transpose()?,
134+
})
131135
}
132136
}
133137

@@ -150,19 +154,23 @@ pub trait SignatureAlgorithmIdentifier {
150154
#[cfg(feature = "alloc")]
151155
pub trait DynSignatureAlgorithmIdentifier {
152156
/// `AlgorithmIdentifier` for the corresponding singature system.
153-
fn signature_algorithm_identifier(&self) -> AlgorithmIdentifierOwned;
157+
fn signature_algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned>;
154158
}
155159

156160
#[cfg(feature = "alloc")]
157161
impl<T> DynSignatureAlgorithmIdentifier for T
158162
where
159163
T: SignatureAlgorithmIdentifier,
160-
T::Params: Into<Any>,
164+
T::Params: Tagged + EncodeValue,
161165
{
162-
fn signature_algorithm_identifier(&self) -> AlgorithmIdentifierOwned {
163-
AlgorithmIdentifierOwned {
166+
fn signature_algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned> {
167+
Ok(AlgorithmIdentifierOwned {
164168
oid: T::SIGNATURE_ALGORITHM_IDENTIFIER.oid,
165-
parameters: T::SIGNATURE_ALGORITHM_IDENTIFIER.parameters.map(Into::into),
166-
}
169+
parameters: T::SIGNATURE_ALGORITHM_IDENTIFIER
170+
.parameters
171+
.as_ref()
172+
.map(Any::encode_from)
173+
.transpose()?,
174+
})
167175
}
168176
}

0 commit comments

Comments
 (0)