1
//! Implementations of our useful traits, on external and parsing-mode types
2

            
3
use super::*;
4

            
5
/// Types related to RSA keys
6
mod rsa {
7
    use super::*;
8
    use crate::types;
9
    use tor_llcrypto::pk::rsa::PublicKey;
10

            
11
    /// An item which contains an RSA public key as an Object, and no extra arguments
12
    #[derive(Deftly)]
13
    #[derive_deftly(ItemValueParseable)]
14
    #[deftly(netdoc(no_extra_args))]
15
    struct ParsePublicKey {
16
        /// The public key data
17
        #[deftly(netdoc(object))]
18
        key: PublicKey,
19
    }
20

            
21
    impl ItemObjectParseable for PublicKey {
22
1842
        fn check_label(label: &str) -> Result<(), EP> {
23
1842
            match label {
24
1842
                "RSA PUBLIC KEY" => Ok(()),
25
                _ => Err(EP::ObjectIncorrectLabel),
26
            }
27
1842
        }
28
1842
        fn from_bytes(input: &[u8]) -> Result<Self, EP> {
29
1842
            (|| {
30
1842
                let key = PublicKey::from_der(input).ok_or(())?;
31

            
32
1842
                if !key.exponent_is(types::misc::RSA_FIXED_EXPONENT) {
33
                    return Err(());
34
1842
                }
35
1842
                if key.bits() < types::misc::RSA_MIN_BITS {
36
                    return Err(());
37
1842
                }
38

            
39
1842
                Ok(key)
40
            })()
41
1842
            .map_err(|()| EP::ObjectInvalidData)
42
1842
        }
43
    }
44

            
45
    impl ItemValueParseable for PublicKey {
46
1842
        fn from_unparsed(item: UnparsedItem) -> Result<Self, EP> {
47
1842
            Ok(ParsePublicKey::from_unparsed(item)?.key)
48
1842
        }
49
    }
50
}
51

            
52
/// Types related to times
53
pub(crate) mod times {
54
    use super::*;
55
    use crate::types::misc::Iso8601TimeSp;
56

            
57
    /// Date and time in deprecated ISO8601-with-space separate arguments syntax
58
    ///
59
    /// Eg `dir-key-published` in a dir auth key cert.
60
    // TODO this should be
61
    //   #[deprecated = "use the Iso8601TimeSp name instead"]
62
    // but right now we have too much outstanding work in flight to do that.
63
    pub type NdaSystemTimeDeprecatedSyntax = Iso8601TimeSp;
64

            
65
    impl ItemArgumentParseable for NdaSystemTimeDeprecatedSyntax {
66
2292
        fn from_args<'s>(args: &mut ArgumentStream<'s>) -> Result<Self, ArgumentError> {
67
            let t;
68
2367
            (t, *args) = (|| {
69
2292
                let whole_line_len = args.whole_line_len();
70
2292
                let options = args.parse_options();
71
2292
                let args = args.clone().into_remaining();
72
2292
                let spc2 = args
73
2292
                    .match_indices(WS)
74
2292
                    .nth(1)
75
2292
                    .map(|(spc2, _)| spc2)
76
2292
                    .unwrap_or_else(|| args.len());
77
2292
                let (t, rest) = args.split_at(spc2);
78
2292
                let t: crate::types::misc::Iso8601TimeSp =
79
2292
                    t.parse().map_err(|_| ArgumentError::Invalid)?;
80
2292
                Ok::<_, AE>((t, ArgumentStream::new(rest, whole_line_len, options)))
81
            })()?;
82
2292
            Ok(t)
83
2292
        }
84
    }
85
}
86

            
87
/// Protocol versions (from `tor-protover`)
88
pub(crate) mod protovers {
89
    use super::*;
90
    use tor_protover::Protocols;
91

            
92
    impl ItemValueParseable for Protocols {
93
460
        fn from_unparsed(item: UnparsedItem<'_>) -> Result<Self, ErrorProblem> {
94
460
            item.check_no_object()?;
95
460
            item.args_copy()
96
460
                .into_remaining()
97
460
                .parse()
98
460
                .map_err(item.invalid_argument_handler("protocols"))
99
460
        }
100
    }
101
}
102

            
103
/// Implementations on `Void`
104
pub(crate) mod void_impls {
105
    use super::*;
106

            
107
    impl ItemValueParseable for Void {
108
        fn from_unparsed(_item: UnparsedItem<'_>) -> Result<Self, ErrorProblem> {
109
            Err(EP::ItemForbidden)
110
        }
111
    }
112

            
113
    #[cfg(feature = "parse2")]
114
    impl ItemObjectParseable for Void {
115
2
        fn check_label(_label: &str) -> Result<(), ErrorProblem> {
116
2
            Ok(())
117
2
        }
118

            
119
2
        fn from_bytes(_input: &[u8]) -> Result<Self, ErrorProblem> {
120
2
            Err(EP::ObjectUnexpected)
121
2
        }
122
    }
123
}
124

            
125
/// Conversion module for `Vec<u8>` as Object with [`ItemValueParseable`]
126
pub mod raw_data_object {
127
    use super::*;
128

            
129
    /// "Parse" the data
130
921
    pub fn try_from(data: Vec<u8>) -> Result<Vec<u8>, Void> {
131
921
        Ok(data)
132
921
    }
133
}