7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2021 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for the specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use qrcode::{EcLevel, QrCode, Version, bits::Bits}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | use ruma::serde::Base64; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use vodozemac::Ed25519PublicKey; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | use crate::error::EncodingError; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | pub(crate) const HEADER: &[u8] = b"MATRIX"; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | pub(crate) const VERSION: u8 = 0x2; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | pub(crate) const MAX_MODE: u8 = 0x2; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | pub(crate) const MIN_SECRET_LEN: usize = 8; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | pub(crate) fn to_bytes( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | mode: u8, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | flow_id: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | first_key: Ed25519PublicKey, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | second_key: Ed25519PublicKey, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | shared_secret: &Base64, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | ) -> Result<Vec<u8>, EncodingError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | let flow_id_len: u16 = flow_id.len().try_into()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | let flow_id_len = flow_id_len.to_be_bytes(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | let data = [ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | HEADER, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | &[VERSION], |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | &[mode], |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | flow_id_len.as_ref(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | flow_id.as_bytes(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | first_key.as_bytes(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | second_key.as_bytes(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | shared_secret.as_bytes(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | ] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | .concat(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | Ok(data) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | pub(crate) fn to_qr_code( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | mode: u8, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | flow_id: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | first_key: Ed25519PublicKey, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | second_key: Ed25519PublicKey, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | shared_secret: &Base64, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | ) -> Result<QrCode, EncodingError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | let data = to_bytes(mode, flow_id, first_key, second_key, shared_secret)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | // Mobile clients seem to have trouble decoding the QR code that gets |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | // generated by `QrCode::new()` it seems to add a couple of data segments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | // with different data modes/types. The parsers seem to assume a single |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | // data type and since we start with an ASCII `MATRIX` header the rest of |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | // the data gets treated as a string as well. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | // We make sure that there isn't an ECI bit set and we just push the bytes, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | // this seems to help since the decoder doesn't assume an encoding and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | // treats everything as raw bytes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | let mut bits = Bits::new(Version::Normal(7)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | bits.push_byte_data(&data)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | bits.push_terminator(EcLevel::L)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | Ok(QrCode::with_bits(bits, EcLevel::L)?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | } |