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

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

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

            
45
    impl ItemValueParseable for PublicKey {
46
1884
        fn from_unparsed(item: UnparsedItem) -> Result<Self, EP> {
47
1884
            Ok(ParsePublicKey::from_unparsed(item)?.key)
48
1884
        }
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
2344
        fn from_args<'s>(args: &mut ArgumentStream<'s>) -> Result<Self, ArgumentError> {
67
            let t;
68
2424
            (t, *args) = (|| {
69
2344
                let whole_line_len = args.whole_line_len();
70
2344
                let options = args.parse_options();
71
2344
                let args = args.clone().into_remaining();
72
2344
                let spc2 = args
73
2344
                    .match_indices(WS)
74
2344
                    .nth(1)
75
2344
                    .map(|(spc2, _)| spc2)
76
2344
                    .unwrap_or_else(|| args.len());
77
2344
                let (t, rest) = args.split_at(spc2);
78
2344
                let t: crate::types::misc::Iso8601TimeSp =
79
2344
                    t.parse().map_err(|_| ArgumentError::Invalid)?;
80
2344
                Ok::<_, AE>((t, ArgumentStream::new(rest, whole_line_len, options)))
81
            })()?;
82
2344
            Ok(t)
83
2344
        }
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
474
        fn from_unparsed(item: UnparsedItem<'_>) -> Result<Self, ErrorProblem> {
94
474
            item.check_no_object()?;
95
474
            item.args_copy()
96
474
                .into_remaining()
97
474
                .parse()
98
474
                .map_err(item.invalid_argument_handler("protocols"))
99
474
        }
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
    impl ItemObjectParseable for Void {
114
2
        fn check_label(_label: &str) -> Result<(), ErrorProblem> {
115
2
            Ok(())
116
2
        }
117

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