1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! Functionality for encoding the middle document of an onion service descriptor.
//!
//! NOTE: `HsDescMiddle` is a private helper for building hidden service descriptors, and is
//! not meant to be used directly. Hidden services will use `HsDescBuilder` to build and encode
//! hidden service descriptors.

use crate::build::NetdocEncoder;
use crate::doc::hsdesc::build::ClientAuth;
use crate::doc::hsdesc::desc_enc::{
    build_descriptor_cookie_key, HS_DESC_CLIENT_ID_LEN, HS_DESC_ENC_NONCE_LEN, HS_DESC_IV_LEN,
};
use crate::doc::hsdesc::middle::{AuthClient, HsMiddleKwd, HS_DESC_AUTH_TYPE};
use crate::NetdocBuilder;

use tor_bytes::EncodeError;
use tor_hscrypto::Subcredential;
use tor_llcrypto::pk::curve25519::{EphemeralSecret, PublicKey};
use tor_llcrypto::util::ct::CtByteArray;

use base64ct::{Base64, Encoding};
use rand::{CryptoRng, Rng, RngCore};

/// The representation of the middle document of an onion service descriptor.
///
/// The plaintext format of this document is described in section 2.5.1.2. of rend-spec-v3.
#[derive(Debug)]
pub(super) struct HsDescMiddle<'a> {
    /// Client authorization parameters, if client authentication is enabled. If set to `None`,
    /// client authentication is disabled.
    pub(super) client_auth: Option<&'a ClientAuth<'a>>,
    /// The "subcredential" of the onion service.
    pub(super) subcredential: Subcredential,
    /// The (encrypted) inner document of the onion service descriptor.
    ///
    /// The `encrypted` field is created by encrypting a
    /// [`build::inner::HsDescInner`](super::inner::HsDescInner)
    /// inner document as described in sections
    /// 2.5.2.1. and 2.5.2.2. of rend-spec-v3.
    pub(super) encrypted: Vec<u8>,
}

impl<'a> NetdocBuilder for HsDescMiddle<'a> {
    fn build_sign<R: RngCore + CryptoRng>(self, rng: &mut R) -> Result<String, EncodeError> {
        use cipher::{KeyIvInit, StreamCipher};
        use tor_llcrypto::cipher::aes::Aes256Ctr as Cipher;
        use HsMiddleKwd::*;

        let HsDescMiddle {
            client_auth,
            subcredential,
            encrypted,
        } = self;

        let mut encoder = NetdocEncoder::new();

        let (ephemeral_key, auth_clients): (_, Box<dyn std::iter::Iterator<Item = AuthClient>>) =
            match client_auth {
                Some(client_auth) if client_auth.auth_clients.is_empty() => {
                    return Err(tor_error::bad_api_usage!(
                        "client authentication is enabled, but there are no authorized clients"
                    )
                    .into());
                }
                Some(client_auth) => {
                    // Client auth is enabled.
                    let auth_clients = client_auth.auth_clients.iter().map(|client| {
                        let (client_id, cookie_key) = build_descriptor_cookie_key(
                            client_auth.ephemeral_key.secret.as_ref(),
                            client,
                            &subcredential,
                        );

                        // Encrypt the descriptor cookie with the public key of the client.
                        let mut encrypted_cookie = client_auth.descriptor_cookie;
                        let iv = rng.gen::<[u8; HS_DESC_IV_LEN]>();
                        let mut cipher = Cipher::new(&cookie_key.into(), &iv.into());
                        cipher.apply_keystream(&mut encrypted_cookie);

                        AuthClient {
                            client_id,
                            iv,
                            encrypted_cookie,
                        }
                    });

                    (*client_auth.ephemeral_key.public, Box::new(auth_clients))
                }
                None => {
                    // Generate a single client-auth line filled with random values for client-id,
                    // iv, and encrypted-cookie.
                    let dummy_auth_client = AuthClient {
                        client_id: CtByteArray::from(rng.gen::<[u8; HS_DESC_CLIENT_ID_LEN]>()),
                        iv: rng.gen::<[u8; HS_DESC_IV_LEN]>(),
                        encrypted_cookie: rng.gen::<[u8; HS_DESC_ENC_NONCE_LEN]>(),
                    };

                    // As per section 2.5.1.2. of rend-spec-v3, if client auth is disabled, we need to
                    // generate some fake data for the desc-auth-ephemeral-key and auth-client fields.
                    let secret = EphemeralSecret::new(rng);
                    let dummy_ephemeral_key = PublicKey::from(&secret);

                    (
                        dummy_ephemeral_key,
                        Box::new(std::iter::once(dummy_auth_client)),
                    )
                }
            };

        encoder.item(DESC_AUTH_TYPE).arg(&HS_DESC_AUTH_TYPE);
        encoder
            .item(DESC_AUTH_EPHEMERAL_KEY)
            .arg(&Base64::encode_string(ephemeral_key.as_bytes()));

        for auth_client in auth_clients {
            encoder
                .item(AUTH_CLIENT)
                .arg(&Base64::encode_string(&*auth_client.client_id))
                .arg(&Base64::encode_string(&auth_client.iv))
                .arg(&Base64::encode_string(&auth_client.encrypted_cookie));
        }

        encoder.item(ENCRYPTED).object("MESSAGE", encrypted);
        encoder.finish().map_err(|e| e.into())
    }
}

#[cfg(test)]
mod test {
    // @@ begin test lint list maintained by maint/add_warning @@
    #![allow(clippy::bool_assert_comparison)]
    #![allow(clippy::clone_on_copy)]
    #![allow(clippy::dbg_macro)]
    #![allow(clippy::print_stderr)]
    #![allow(clippy::print_stdout)]
    #![allow(clippy::single_char_pattern)]
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::unchecked_duration_subtraction)]
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->

    use super::*;
    use crate::doc::hsdesc::build::test::{create_curve25519_pk, expect_bug};
    use crate::doc::hsdesc::build::ClientAuth;
    use crate::doc::hsdesc::test_data::TEST_SUBCREDENTIAL;
    use tor_basic_utils::test_rng::Config;
    use tor_hscrypto::pk::HsSvcDescEncKeypair;
    use tor_llcrypto::pk::curve25519;
    use tor_llcrypto::util::rand_compat::RngCompatExt;

    // Some dummy bytes, not actually encrypted.
    const TEST_ENCRYPTED_VALUE: &[u8] = &[1, 2, 3, 4];

    #[test]
    fn middle_hsdesc_encoding_no_client_auth() {
        let hs_desc = HsDescMiddle {
            client_auth: None,
            subcredential: TEST_SUBCREDENTIAL.into(),
            encrypted: TEST_ENCRYPTED_VALUE.into(),
        }
        .build_sign(&mut Config::Deterministic.into_rng())
        .unwrap();

        assert_eq!(
            hs_desc,
            r#"desc-auth-type x25519
desc-auth-ephemeral-key XI/a9NGh/7ClaFcKqtdI9DoP8da5ovwPDdgCHUr3xX0=
auth-client F+Z6EDfG7oc= 7EIXRtlSozVtGAs6+mNujQ== pNtSIyiCahSvUVg+7s71Ow==
encrypted
-----BEGIN MESSAGE-----
AQIDBA==
-----END MESSAGE-----
"#
        );
    }

    #[test]
    fn middle_hsdesc_encoding_with_bad_client_auth() {
        let mut rng = Config::Deterministic.into_rng().rng_compat();
        let secret = curve25519::StaticSecret::new(&mut rng);
        let public = curve25519::PublicKey::from(&secret).into();

        let client_auth = ClientAuth {
            ephemeral_key: HsSvcDescEncKeypair {
                public,
                secret: secret.into(),
            },
            auth_clients: &[],
            descriptor_cookie: rand::Rng::gen::<[u8; HS_DESC_ENC_NONCE_LEN]>(&mut rng),
        };

        let err = HsDescMiddle {
            client_auth: Some(&client_auth),
            subcredential: TEST_SUBCREDENTIAL.into(),
            encrypted: TEST_ENCRYPTED_VALUE.into(),
        }
        .build_sign(&mut rng)
        .unwrap_err();

        assert!(expect_bug(err)
            .contains("client authentication is enabled, but there are no authorized clients"));
    }

    #[test]
    fn middle_hsdesc_encoding_client_auth() {
        let mut rng = Config::Deterministic.into_rng().rng_compat();
        // 2 authorized clients
        let auth_clients = vec![
            create_curve25519_pk(&mut rng),
            create_curve25519_pk(&mut rng),
        ];

        let secret = curve25519::StaticSecret::new(&mut rng);
        let public = curve25519::PublicKey::from(&secret).into();

        let client_auth = ClientAuth {
            ephemeral_key: HsSvcDescEncKeypair {
                public,
                secret: secret.into(),
            },
            auth_clients: &auth_clients,
            descriptor_cookie: rand::Rng::gen::<[u8; HS_DESC_ENC_NONCE_LEN]>(&mut rng),
        };

        let hs_desc = HsDescMiddle {
            client_auth: Some(&client_auth),
            subcredential: TEST_SUBCREDENTIAL.into(),
            encrypted: TEST_ENCRYPTED_VALUE.into(),
        }
        .build_sign(&mut Config::Deterministic.into_rng())
        .unwrap();

        assert_eq!(
            hs_desc,
            r#"desc-auth-type x25519
desc-auth-ephemeral-key 9Upi9XNWyqx3ZwHeQ5r3+Dh116k+C4yHeE9BcM68HDc=
auth-client pxfSbhBMPw0= F+Z6EDfG7ofsQhdG2VKjNQ== fEursUD9Bj5Q9mFP8sIddA==
auth-client DV7nt+CDOno= bRgLOvpjbo2k21IjKIJqFA== 2yVT+Lpm/WL4JAU64zlGpQ==
encrypted
-----BEGIN MESSAGE-----
AQIDBA==
-----END MESSAGE-----
"#
        );
    }
}