Skip to main content

sec1/
parameters.rs

1use der::{
2    DecodeValue, EncodeValue, FixedTag, Header, Length, Reader, Tag, Writer,
3    asn1::{AnyRef, ObjectIdentifier},
4};
5
6/// Elliptic curve parameters as described in
7/// [RFC5480 Section 2.1.1](https://datatracker.ietf.org/doc/html/rfc5480#section-2.1.1):
8///
9/// ```text
10/// ECParameters ::= CHOICE {
11///   namedCurve         OBJECT IDENTIFIER
12///   -- implicitCurve   NULL
13///   -- specifiedCurve  SpecifiedECDomain
14/// }
15///   -- implicitCurve and specifiedCurve MUST NOT be used in PKIX.
16///   -- Details for SpecifiedECDomain can be found in [X9.62].
17///   -- Any future additions to this CHOICE should be coordinated
18///   -- with ANSI X9.
19/// ```
20#[derive(Copy, Clone, Debug, Eq, PartialEq)]
21pub enum EcParameters {
22    /// Elliptic curve named by a particular OID.
23    ///
24    /// > namedCurve identifies all the required values for a particular
25    /// > set of elliptic curve domain parameters to be represented by an
26    /// > object identifier.
27    NamedCurve(ObjectIdentifier),
28}
29
30impl<'a> DecodeValue<'a> for EcParameters {
31    type Error = der::Error;
32
33    fn decode_value<R: Reader<'a>>(decoder: &mut R, header: Header) -> der::Result<Self> {
34        ObjectIdentifier::decode_value(decoder, header).map(Self::NamedCurve)
35    }
36}
37
38impl EncodeValue for EcParameters {
39    fn value_len(&self) -> der::Result<Length> {
40        match self {
41            Self::NamedCurve(oid) => oid.value_len(),
42        }
43    }
44
45    fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
46        match self {
47            Self::NamedCurve(oid) => oid.encode_value(writer),
48        }
49    }
50}
51
52impl EcParameters {
53    /// Obtain the `namedCurve` OID.
54    pub fn named_curve(self) -> Option<ObjectIdentifier> {
55        match self {
56            Self::NamedCurve(oid) => Some(oid),
57        }
58    }
59}
60
61impl<'a> From<&'a EcParameters> for AnyRef<'a> {
62    fn from(params: &'a EcParameters) -> AnyRef<'a> {
63        match params {
64            EcParameters::NamedCurve(oid) => oid.into(),
65        }
66    }
67}
68
69impl From<ObjectIdentifier> for EcParameters {
70    fn from(oid: ObjectIdentifier) -> EcParameters {
71        EcParameters::NamedCurve(oid)
72    }
73}
74
75impl FixedTag for EcParameters {
76    const TAG: Tag = Tag::ObjectIdentifier;
77}