summaryrefslogtreecommitdiffstats
path: root/third_party/rust/origin-trial-token/lib.rs
blob: 8872095a1d0675e08b3a7d0e23987de9d7fc814b (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Implements a simple processor for
//! https://github.com/chromium/chromium/blob/d7da0240cae77824d1eda25745c4022757499131/third_party/blink/public/common/origin_trials/origin_trials_token_structure.md
//!
//! This crate intentionally leaves the cryptography to the caller. See the
//! tools/ directory for example usages.


/// Latest version as documented.
pub const LATEST_VERSION: u8 = 3;

#[repr(C)]
pub struct RawToken {
    version: u8,
    signature: [u8; 64],
    payload_length: [u8; 4],
    /// Payload is an slice of payload_length bytes, and has to be verified
    /// before returning it to the caller.
    payload: [u8; 0],
}

#[derive(Debug)]
pub enum TokenValidationError {
    BufferTooSmall,
    MismatchedPayloadSize { expected: usize, actual: usize },
    InvalidSignature,
    UnknownVersion,
    UnsupportedThirdPartyToken,
    UnexpectedUsageInNonThirdPartyToken,
    MalformedPayload(serde_json::Error),
}

impl RawToken {
    const HEADER_SIZE: usize = std::mem::size_of::<Self>();

    #[inline]
    pub fn version(&self) -> u8 {
        self.version
    }

    #[inline]
    pub fn signature(&self) -> &[u8; 64] {
        &self.signature
    }

    #[inline]
    pub fn payload_length(&self) -> usize {
        u32::from_be_bytes(self.payload_length) as usize
    }

    #[inline]
    pub fn as_buffer(&self) -> &[u8] {
        let buffer_size = Self::HEADER_SIZE + self.payload_length();
        unsafe { std::slice::from_raw_parts(self as *const _ as *const u8, buffer_size) }
    }

    #[inline]
    pub fn payload(&self) -> &[u8] {
        let len = self.payload_length();
        unsafe { std::slice::from_raw_parts(self.payload.as_ptr(), len) }
    }

    /// Returns a RawToken from a raw buffer.
    pub fn from_buffer<'a>(buffer: &'a [u8]) -> Result<&'a Self, TokenValidationError> {
        if buffer.len() <= Self::HEADER_SIZE {
            return Err(TokenValidationError::BufferTooSmall);
        }
        assert_eq!(
            std::mem::align_of::<Self>(),
            1,
            "RawToken is a view over the buffer"
        );
        let raw_token = unsafe { &*(buffer.as_ptr() as *const Self) };
        let payload = &buffer[Self::HEADER_SIZE..];
        let expected = raw_token.payload_length();
        let actual = payload.len();
        if expected != actual {
            return Err(TokenValidationError::MismatchedPayloadSize { expected, actual });
        }
        Ok(raw_token)
    }

    /// The data to verify the signature in this raw token.
    fn signature_data(&self) -> Vec<u8> {
        Self::raw_signature_data(self.version, self.payload())
    }

    /// The data to sign or verify given a payload and a version.
    fn raw_signature_data(version: u8, payload: &[u8]) -> Vec<u8> {
        let mut data = Vec::with_capacity(payload.len() + 5);
        data.push(version);
        data.extend((payload.len() as u32).to_be_bytes());
        data.extend(payload);
        data
    }

    /// Verify the signature of this raw token.
    pub fn verify(&self, verify_signature: impl FnOnce(&[u8; 64], &[u8]) -> bool) -> bool {
        let signature_data = self.signature_data();
        verify_signature(&self.signature, &signature_data)
    }
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum Usage {
    #[serde(rename = "")]
    None,
    Subset,
}

impl Usage {
    fn is_none(&self) -> bool {
        *self == Self::None
    }
}

impl Default for Usage {
    fn default() -> Self {
        Self::None
    }
}

fn is_false(t: &bool) -> bool {
    *t == false
}

/// An already decoded and maybe-verified token.
#[derive(serde::Deserialize, serde::Serialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Token {
    pub origin: String,
    pub feature: String,
    pub expiry: u64, // Timestamp. Seconds since epoch.
    #[serde(default, skip_serializing_if = "is_false")]
    pub is_subdomain: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub is_third_party: bool,
    #[serde(default, skip_serializing_if = "Usage::is_none")]
    pub usage: Usage,
}

impl Token {
    #[inline]
    pub fn origin(&self) -> &str {
        &self.origin
    }

    #[inline]
    pub fn feature(&self) -> &str {
        &self.feature
    }

    #[inline]
    pub fn expiry_since_unix_epoch(&self) -> std::time::Duration {
        std::time::Duration::from_secs(self.expiry)
    }

    #[inline]
    pub fn expiry_time(&self) -> Option<std::time::SystemTime> {
        std::time::UNIX_EPOCH.checked_add(self.expiry_since_unix_epoch())
    }

    #[inline]
    pub fn is_expired(&self) -> bool {
        let now_duration = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("System time before epoch?");
        now_duration >= self.expiry_since_unix_epoch()
    }

    /// Most high-level function: For a given buffer, tries to parse it and
    /// verify it as a token.
    pub fn from_buffer(
        buffer: &[u8],
        verify_signature: impl FnOnce(&[u8; 64], &[u8]) -> bool,
    ) -> Result<Self, TokenValidationError> {
        Self::from_raw_token(RawToken::from_buffer(buffer)?, verify_signature)
    }

    /// Validates a RawToken's signature and converts the token if valid.
    pub fn from_raw_token(
        token: &RawToken,
        verify_signature: impl FnOnce(&[u8; 64], &[u8]) -> bool,
    ) -> Result<Self, TokenValidationError> {
        if !token.verify(verify_signature) {
            return Err(TokenValidationError::InvalidSignature);
        }
        Self::from_raw_token_unverified(token)
    }

    /// Converts the token from a raw token, without verifying first.
    pub fn from_raw_token_unverified(token: &RawToken) -> Result<Self, TokenValidationError> {
        Self::from_payload(token.version, token.payload())
    }

    /// Converts the token from a raw payload, version pair.
    pub fn from_payload(version: u8, payload: &[u8]) -> Result<Self, TokenValidationError> {
        if version != 2 && version != 3 {
            assert_ne!(version, LATEST_VERSION);
            return Err(TokenValidationError::UnknownVersion);
        }

        let token: Token = match serde_json::from_slice(payload) {
            Ok(t) => t,
            Err(e) => return Err(TokenValidationError::MalformedPayload(e)),
        };

        // Third-party tokens are not supported in version 2.
        if token.is_third_party {
            if version == 2 {
                return Err(TokenValidationError::UnsupportedThirdPartyToken);
            }
        } else if !token.usage.is_none() {
            return Err(TokenValidationError::UnexpectedUsageInNonThirdPartyToken);
        }

        Ok(token)
    }

    /// Converts the token to a raw payload.
    pub fn to_payload(&self) -> Vec<u8> {
        serde_json::to_string(self)
            .expect("Should always be able to turn a token into a payload")
            .into_bytes()
    }

    /// Converts the token to the data that should be signed.
    pub fn to_signature_data(&self) -> Vec<u8> {
        RawToken::raw_signature_data(LATEST_VERSION, &self.to_payload())
    }

    /// Turns the token into a fully signed token.
    pub fn to_signed_token(&self, sign: impl FnOnce(&[u8]) -> [u8; 64]) -> Vec<u8> {
        self.to_signed_token_with_payload(sign, &self.to_payload())
    }

    /// DO NOT EXPOSE: This is intended for testing only. We need to test with
    /// the original payload so that the tokens match, but we assert
    /// that self.to_payload() and payload are equivalent.
    fn to_signed_token_with_payload(
        &self,
        sign: impl FnOnce(&[u8]) -> [u8; 64],
        payload: &[u8],
    ) -> Vec<u8> {
        let signature_data_with_payload = RawToken::raw_signature_data(LATEST_VERSION, &payload);
        let signature = sign(&signature_data_with_payload);

        let mut buffer = Vec::with_capacity(1 + signature.len() + 4 + payload.len());
        buffer.push(LATEST_VERSION);
        buffer.extend(signature);
        buffer.extend((payload.len() as u32).to_be_bytes());
        buffer.extend(payload);

        if cfg!(debug_assertions) {
            let token = Self::from_buffer(&buffer, |_, _| true).expect("Creating malformed token?");
            assert_eq!(self, &token, "Token differs after deserialization?");
        }

        buffer
    }
}

#[cfg(test)]
mod tests;