From 305766955b08770954b459b14ff396d32ad39494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 19 May 2021 16:10:48 +0200 Subject: [PATCH] matrix-sdk: Add a crate to generate and parse QR codes This patch adds types and methods to parse QR codes defined in the [spec]. It supports parsing the QR format from an image or from a byte string, converting back to an image and bytestring is possible as well. [spec]: https://spec.matrix.org/unstable/client-server-api/#qr-code-format --- Cargo.toml | 1 + matrix_qrcode/Cargo.toml | 18 ++ matrix_qrcode/data/self-no-master.png | Bin 0 -> 1775 bytes matrix_qrcode/data/self-verification.png | Bin 0 -> 1467 bytes matrix_qrcode/data/verification.png | Bin 0 -> 4277 bytes matrix_qrcode/src/error.rs | 47 ++++ matrix_qrcode/src/lib.rs | 204 +++++++++++++++ matrix_qrcode/src/types.rs | 315 +++++++++++++++++++++++ matrix_qrcode/src/utils.rs | 100 +++++++ 9 files changed, 685 insertions(+) create mode 100644 matrix_qrcode/Cargo.toml create mode 100644 matrix_qrcode/data/self-no-master.png create mode 100644 matrix_qrcode/data/self-verification.png create mode 100644 matrix_qrcode/data/verification.png create mode 100644 matrix_qrcode/src/error.rs create mode 100644 matrix_qrcode/src/lib.rs create mode 100644 matrix_qrcode/src/types.rs create mode 100644 matrix_qrcode/src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index 050a6c8c..07953e68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "matrix_sdk", + "matrix_qrcode", "matrix_sdk_base", "matrix_sdk_test", "matrix_sdk_test_macros", diff --git a/matrix_qrcode/Cargo.toml b/matrix_qrcode/Cargo.toml new file mode 100644 index 00000000..f9ab6f34 --- /dev/null +++ b/matrix_qrcode/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "matrix_qrcode" +version = "0.1.0" +authors = ["Damir Jelić "] +edition = "2018" + +[features] +default = ["decode_image"] +decode_image = ["image", "rqrr", "qrcode/image", "qrcode/svg"] + +[dependencies] +base64 = "0.13.0" +byteorder = "1.4.3" +image = { version = "0.23.14", optional = true } +qrcode = { version = "0.12.0", default-features = false } +rqrr = { version = "0.3.2" , optional = true } +ruma-identifiers = "0.19.1" +thiserror = "1.0.24" diff --git a/matrix_qrcode/data/self-no-master.png b/matrix_qrcode/data/self-no-master.png new file mode 100644 index 0000000000000000000000000000000000000000..1bffd19eef81114da9e9cce4af8cfb7fae37a4e9 GIT binary patch literal 1775 zcma)-dpOg39LGJaq{LFE<5H@VT$W1?c~()wgXjFMD}rOogO-2HD@v{Wm=pij;;6aj*8dO}KgnXV zJ#*ML3yrSb`p0gre4l4F9;?m%=-ch@z>gbSI^G#$hbFvJU}V@ zey1?=2JJpsosCo}aV;R1wMZ@v&ZR04HAUCOPi9%4t08OSU13&%nK!M~x>ZY@&EHFP z`WMY#OyaLvG(3gLJcP-(BtNs0WKpw+vvXm2g!@n)B`9gN*NuSDmeU0{pVQiK!X@cg zlA{kWQ6yRU#;#8qy>*9#x`poM$jTtupwmi6hL(b@btzUsC8J=_|l#?;;XG!G6}11ibDOyv8DLC zfj8#zS9ML49QUou-!+}j+L=4;HPDMIFz_uSX&{%Y4>-tJ)cM+&Pc>6<~y?a8r_Y(f#x@mcwhEh}d+=QXac$Zh(sJx65+ran zPjs;Gdweu^Rx|Z#NeM0Q@T+dz?$k2j5dn=v>7@@Z2-tpx1XJCV*Y8>U&6J)*-f$w5 z{u-cIpG{acLzbxQN&}8Ri`Ole9!-XUoz^+70%j|@5ONtOfN|wz{2A}xri=M+ZufJ} z37e%+O5sFMCvo)F;8#wm$I$u6#BM!Cv4R0u>v_7_6sHC$R6fp;Sl42v6Gl zpM&iUR)*?NMtxkH=li?5U#OIB8MOLJOH zb8J5~d_tLe=qn$T#-AOW2~q5ZuU(S}7<-bJ@LFVYn6^GxK?lJ!YO<@4CoU!wPxZo0 zFFY3Z$#drpPBrXQo05p7s8Q&Wl>!vp8dY~MGz>`{QWtq5^X1Q4xxL7Eu*D*=I8 zXkYmj9Vk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G$%5ipK}Z|4D1wwhmQf4{?h$B+AF)J!Omrh z$ziBYy`6sQ(n1H&qk1+J6sq7E{PC#n>GErYhz6^9{?AghoA_v~26HDaYkeEr5I!MB Jiw++9`fuI4&2j($ literal 0 HcmV?d00001 diff --git a/matrix_qrcode/data/verification.png b/matrix_qrcode/data/verification.png new file mode 100644 index 0000000000000000000000000000000000000000..e4a918ecbda618e9a085a53500f52a3edc9176d2 GIT binary patch literal 4277 zcmb7IjX#t7`=8lrn`py)I|?%~^AMfm^x$D^bjZ^@>@+9kDIJuo^UYHwo3)eSWJHv( z-D-{ug~YYKiB)Z-q-uOGyHtr zH4*v<5D27s*uyOVusz>z*cRZ~S(SYnFpzVk!xR{BrNAQDAP_S4up5z*93m+h|L@xa z+uS6Zo1Ye5cJy2euur%{yjnZ=@LEshbIqI+7nhZ@t6%PJX1btcMrmIVW!~msI;tA; z_zHGOJq=M7Zw^Mg9rS~!uDamdZu&#)jUT6BhvEKOx@vAbsu`<#Cjp*@n9jR1aSWA) z02_b4LMHK;vWiptsct-`3%hkH4_S>N)Qmkv1G^&2;$*ua7HsmJ#B+xr5yTWay*zJF zbHiDT*%UY{&mrfXdn3MZrunipY0$+)dXaMpKlxJ%6s5YPi`rv7+`&Ni9IOm{Xvs!I zJhH{UJVgxH%1_ATKBFShZcwRu+8@LUL^G9j*P-B2-dm;TAp_Bf$%N3}uql*?B(<#~ zA~uW>Kf8=KL>}y@l2DONz~P{Oc;1Zpb1e6?QiQ(p)t^W~=3=bW`0yy_C>-%xShQ1n zgr7R*;h=LbO~RK3C(am7x(1=J`fDXJM3?6IQJr;P^6Eay~PC`5%3w zJM~XDU(**n;W}ZIFfSw)(tE2TEtas)u!$@eUC<~0C8h~~L^041$^g2_}*|UAZFgU8B~EZkGs@x52cnm(BgYkApV0OnZJ=irfgp(&sxGr(aG)1v(7NV5*6vt^Fr%{M%XtXJA3d^ zR%tcrK^F^#=TwO4DJIY1(`SczQ**c(dKZx7c!E7yHAsLbww8YFyPe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eC z0jNP^tS*`biXMB9A|8UDFk%`7BiH`i%n(yr!Fv6xBe-2 z`Ul~UG3m>~QPb|T8r5o)}Wk$5?P*NNF;sz@%43+3%QrDx}$up(?fjen+z9ezx3g@zirg7vq!A0f6`MpO7X>B;%yL`KLNqSz^yZJR$7zlo*m()7td7-bWXW@sa%iDkSoVu&hI zJeS)UW^GDv5H(L0u2a&w8ud+dJv<=|PRryhRmke-%Y}WXB_frSO9^CB)jd^!kOc(i z*^hN*yYTktFsMIK;l|U!D&nZx`I7x2gY^lO=*sUy{EZXD3bl$^J}%#M_^Zhfz{Dni zN1x~>cWK2cW;1=gPIZu_Ik4Em7*obLKHyRNqgKk5Nlyx=>oD&MHO>$nj32;)6i4J@VI2 z=9w(IO@tD4`@6=aBK`a=c+g=)k=b8YIn&LMmJNIecHwgZ5bl=bc+x=jF=8=2@OE!H zWOW|8_uiYk!hd|$?;CuIV>JRYqj|N38DyO!g@>ydGvKyIaT|K?@?Fq9dQw5^oZM9@ zGWyqNYreJ}diRGwVsXokEaxd7O>3u6BB&d37Kl(W*4Bsj)w6N?+UM+(4*C+Zv_URn zRV7pFgB@cXpNNqU%EwrP9i|ZYTX3`q%2NG^1N-%UwjBpW z3(;R?*9W(ReGBYwFaQQQRn<((S^#MnGC9tjze|Dw8ISM-E-WYN1LE@;TF zcDuUp#?>)1>;gw+35(+t0|%LjCUWvP#aW`G$?G&Jq+G z*He;rI`&W}){hv})4KT_MnQ7+(DLpBR Result<(), DecodingError> { +//! use image; +//! +//! let image = image::open("/path/to/my/image.png").unwrap(); +//! let result = QrVerification::from_image(image)?; +//! # Ok(()) +//! # } +//! ``` + +#![deny( + missing_debug_implementations, + dead_code, + trivial_casts, + trivial_numeric_casts, + unused_extern_crates, + unused_import_braces, + unused_qualifications +)] + +mod error; +mod types; +mod utils; + +pub use error::{DecodingError, EncodingError}; +#[cfg(feature = "decode_image")] +#[cfg_attr(feature = "docs", doc(cfg(decode_image)))] +pub use image; +pub use qrcode; +#[cfg(feature = "decode_image")] +#[cfg_attr(feature = "docs", doc(cfg(decode_image)))] +pub use rqrr; +pub use types::{ + QrVerification, SelfVerificationData, SelfVerificationNoMasterKey, VerificationData, +}; + +#[cfg(test)] +mod test { + #[cfg(feature = "decode_image")] + use std::{convert::TryFrom, io::Cursor}; + + #[cfg(feature = "decode_image")] + use image::{ImageFormat, Luma}; + #[cfg(feature = "decode_image")] + use qrcode::QrCode; + + #[cfg(feature = "decode_image")] + use crate::utils::decode_qr; + use crate::{DecodingError, QrVerification}; + + #[cfg(feature = "decode_image")] + static VERIFICATION: &[u8; 4277] = include_bytes!("../data/verification.png"); + #[cfg(feature = "decode_image")] + static SELF_VERIFICATION: &[u8; 1467] = include_bytes!("../data/self-verification.png"); + #[cfg(feature = "decode_image")] + static SELF_NO_MASTER: &[u8; 1775] = include_bytes!("../data/self-no-master.png"); + + #[test] + #[cfg(feature = "decode_image")] + fn decode_qr_test() { + let image = Cursor::new(VERIFICATION); + let image = image::load(image, ImageFormat::Png).unwrap().to_luma8(); + decode_qr(image).expect("Couldn't decode the QR code"); + } + + #[test] + #[cfg(feature = "decode_image")] + fn decode_test() { + let image = Cursor::new(VERIFICATION); + let image = image::load(image, ImageFormat::Png).unwrap().to_luma8(); + let result = QrVerification::try_from(image).unwrap(); + + assert!(matches!(result, QrVerification::Verification(_))); + } + + #[test] + #[cfg(feature = "decode_image")] + fn decode_encode_cycle() { + let image = Cursor::new(VERIFICATION); + let image = image::load(image, ImageFormat::Png).unwrap(); + let result = QrVerification::from_image(image).unwrap(); + + assert!(matches!(result, QrVerification::Verification(_))); + + let encoded = result.to_qr_code().unwrap(); + let image = encoded.render::>().build(); + let second_result = QrVerification::try_from(image).unwrap(); + + assert_eq!(result, second_result); + + let bytes = result.to_bytes().unwrap(); + let third_result = QrVerification::from_bytes(bytes).unwrap(); + + assert_eq!(result, third_result); + } + + #[test] + #[cfg(feature = "decode_image")] + fn decode_encode_cycle_self() { + let image = Cursor::new(SELF_VERIFICATION); + let image = image::load(image, ImageFormat::Png).unwrap(); + let result = QrVerification::try_from(image).unwrap(); + + assert!(matches!(result, QrVerification::SelfVerification(_))); + + let encoded = result.to_qr_code().unwrap(); + let image = encoded.render::>().build(); + let second_result = QrVerification::from_luma(image).unwrap(); + + assert_eq!(result, second_result); + + let bytes = result.to_bytes().unwrap(); + let third_result = QrVerification::from_bytes(bytes).unwrap(); + + assert_eq!(result, third_result); + } + + #[test] + #[cfg(feature = "decode_image")] + fn decode_encode_cycle_self_no_master() { + let image = Cursor::new(SELF_NO_MASTER); + let image = image::load(image, ImageFormat::Png).unwrap(); + let result = QrVerification::from_image(image).unwrap(); + + assert!(matches!(result, QrVerification::SelfVerificationNoMasterKey(_))); + + let encoded = result.to_qr_code().unwrap(); + let image = encoded.render::>().build(); + let second_result = QrVerification::try_from(image).unwrap(); + + assert_eq!(result, second_result); + + let bytes = result.to_bytes().unwrap(); + let third_result = QrVerification::try_from(bytes).unwrap(); + + assert_eq!(result, third_result); + } + + #[test] + #[cfg(feature = "decode_image")] + fn decode_invalid_qr() { + let qr = QrCode::new(b"NonMatrixCode").expect("Can't build a simple QR code"); + let image = qr.render::>().build(); + let result = QrVerification::try_from(image); + assert!(matches!(result, Err(DecodingError::Header))) + } + + #[test] + fn decode_invalid_header() { + let data = b"NonMatrixCode"; + let result = QrVerification::from_bytes(data); + assert!(matches!(result, Err(DecodingError::Header))) + } + + #[test] + fn decode_invalid_mode() { + let data = b"MATRIX\x02\x03"; + let result = QrVerification::from_bytes(data); + assert!(matches!(result, Err(DecodingError::Mode(3)))) + } + + #[test] + fn decode_invalid_version() { + let data = b"MATRIX\x01\x03"; + let result = QrVerification::from_bytes(data); + assert!(matches!(result, Err(DecodingError::Version(1)))) + } + + #[test] + fn decode_missing_data() { + let data = b"MATRIX\x02\x02"; + let result = QrVerification::from_bytes(data); + assert!(matches!(result, Err(DecodingError::Read(_)))) + } + + #[test] + fn decode_short_secret() { + let data = b"MATRIX\x02\x02\x00\x07FLOW_IDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBSECRET"; + let result = QrVerification::from_bytes(data); + assert!(matches!(result, Err(DecodingError::SharedSecret(_)))) + } +} diff --git a/matrix_qrcode/src/types.rs b/matrix_qrcode/src/types.rs new file mode 100644 index 00000000..efac59f5 --- /dev/null +++ b/matrix_qrcode/src/types.rs @@ -0,0 +1,315 @@ +// Copyright 2021 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::{ + convert::TryFrom, + io::{Cursor, Read}, +}; + +use byteorder::{BigEndian, ReadBytesExt}; +#[cfg(feature = "decode_image")] +use image::{DynamicImage, ImageBuffer, Luma}; +use qrcode::QrCode; +use ruma_identifiers::EventId; + +#[cfg(feature = "decode_image")] +#[cfg_attr(feature = "docs", doc(cfg(decode_image)))] +use crate::utils::decode_qr; +use crate::{ + error::{DecodingError, EncodingError}, + utils::{base_64_encode, to_bytes, to_qr_code, HEADER, MAX_MODE, MIN_SECRET_LEN, VERSION}, +}; + +#[derive(Clone, Debug, PartialEq)] +pub enum QrVerification { + Verification(VerificationData), + SelfVerification(SelfVerificationData), + SelfVerificationNoMasterKey(SelfVerificationNoMasterKey), +} + +#[cfg(feature = "decode_image")] +#[cfg_attr(feature = "docs", doc(cfg(decode_image)))] +impl TryFrom for QrVerification { + type Error = DecodingError; + + fn try_from(image: DynamicImage) -> Result { + Self::from_image(image) + } +} + +#[cfg(feature = "decode_image")] +#[cfg_attr(feature = "docs", doc(cfg(decode_image)))] +impl TryFrom, Vec>> for QrVerification { + type Error = DecodingError; + + fn try_from(image: ImageBuffer, Vec>) -> Result { + Self::from_luma(image) + } +} + +impl TryFrom> for QrVerification { + type Error = DecodingError; + + fn try_from(value: Vec) -> Result { + Self::from_bytes(value) + } +} + +impl QrVerification { + #[cfg(feature = "decode_image")] + #[cfg_attr(feature = "docs", doc(cfg(decode_image)))] + pub fn from_image(image: DynamicImage) -> Result { + let image = image.to_luma8(); + Self::decode(image) + } + + #[cfg(feature = "decode_image")] + #[cfg_attr(feature = "docs", doc(cfg(decode_image)))] + pub fn from_luma(image: ImageBuffer, Vec>) -> Result { + Self::decode(image) + } + + pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result { + Self::decode_bytes(bytes) + } + + pub fn to_qr_code(&self) -> Result { + match self { + QrVerification::Verification(v) => v.to_qr_code(), + QrVerification::SelfVerification(v) => v.to_qr_code(), + QrVerification::SelfVerificationNoMasterKey(v) => v.to_qr_code(), + } + } + + pub fn to_bytes(&self) -> Result, EncodingError> { + match self { + QrVerification::Verification(v) => v.to_bytes(), + QrVerification::SelfVerification(v) => v.to_bytes(), + QrVerification::SelfVerificationNoMasterKey(v) => v.to_bytes(), + } + } + + fn decode_bytes(bytes: impl AsRef<[u8]>) -> Result { + let mut decoded = Cursor::new(bytes); + + let mut header = [0u8; 6]; + let mut first_key = [0u8; 32]; + let mut second_key = [0u8; 32]; + + decoded.read_exact(&mut header)?; + let version = decoded.read_u8()?; + let mode = decoded.read_u8()?; + + if header != HEADER { + return Err(DecodingError::Header); + } else if version != VERSION { + return Err(DecodingError::Version(version)); + } else if mode > MAX_MODE { + return Err(DecodingError::Mode(mode)); + } + + let flow_id_len = decoded.read_u16::()?; + let mut flow_id = vec![0; flow_id_len.into()]; + + decoded.read_exact(&mut flow_id)?; + decoded.read_exact(&mut first_key)?; + decoded.read_exact(&mut second_key)?; + + let mut shared_secret = Vec::new(); + + decoded.read_to_end(&mut shared_secret)?; + + if shared_secret.len() < MIN_SECRET_LEN { + return Err(DecodingError::SharedSecret(shared_secret.len())); + } + + QrVerification::new(mode, flow_id, first_key, second_key, shared_secret) + } + + #[cfg(feature = "decode_image")] + #[cfg_attr(feature = "docs", doc(cfg(decode_image)))] + fn decode(image: ImageBuffer, Vec>) -> Result { + let decoded = decode_qr(image)?; + Self::decode_bytes(decoded) + } + + fn new( + mode: u8, + flow_id: Vec, + first_key: [u8; 32], + second_key: [u8; 32], + shared_secret: Vec, + ) -> Result { + let first_key = base_64_encode(&first_key); + let second_key = base_64_encode(&second_key); + let flow_id = String::from_utf8(flow_id)?; + let shared_secret = base_64_encode(&shared_secret); + + match mode { + VerificationData::QR_MODE => { + let event_id = EventId::try_from(flow_id)?; + Ok(VerificationData::new(event_id, first_key, second_key, shared_secret).into()) + } + SelfVerificationData::QR_MODE => { + Ok(SelfVerificationData::new(flow_id, first_key, second_key, shared_secret).into()) + } + SelfVerificationNoMasterKey::QR_MODE => { + Ok(SelfVerificationNoMasterKey::new(flow_id, first_key, second_key, shared_secret) + .into()) + } + m => Err(DecodingError::Mode(m)), + } + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct VerificationData { + event_id: EventId, + first_master_key: String, + second_master_key: String, + shared_secret: String, +} + +impl VerificationData { + const QR_MODE: u8 = 0x00; + + pub fn to_bytes(&self) -> Result, EncodingError> { + to_bytes( + Self::QR_MODE, + &self.event_id.as_str(), + &self.first_master_key, + &self.second_master_key, + &self.shared_secret, + ) + } + + pub fn to_qr_code(&self) -> Result { + to_qr_code( + Self::QR_MODE, + self.event_id.as_str(), + &self.first_master_key, + &self.second_master_key, + &self.shared_secret, + ) + } + + pub fn new( + event_id: EventId, + first_key: String, + second_key: String, + shared_secret: String, + ) -> Self { + Self { event_id, first_master_key: first_key, second_master_key: second_key, shared_secret } + } +} + +impl From for QrVerification { + fn from(data: VerificationData) -> Self { + Self::Verification(data) + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct SelfVerificationData { + transaction_id: String, + master_key: String, + device_key: String, + shared_secret: String, +} + +impl SelfVerificationData { + const QR_MODE: u8 = 0x01; + + pub fn to_bytes(&self) -> Result, EncodingError> { + to_bytes( + Self::QR_MODE, + &self.transaction_id, + &self.master_key, + &self.device_key, + &self.shared_secret, + ) + } + + pub fn to_qr_code(&self) -> Result { + to_qr_code( + Self::QR_MODE, + &self.transaction_id, + &self.master_key, + &self.device_key, + &self.shared_secret, + ) + } + + pub fn new( + transaction_id: String, + master_key: String, + device_key: String, + shared_secret: String, + ) -> Self { + Self { transaction_id, master_key, device_key, shared_secret } + } +} + +impl From for QrVerification { + fn from(data: SelfVerificationData) -> Self { + Self::SelfVerification(data) + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct SelfVerificationNoMasterKey { + transaction_id: String, + device_key: String, + master_key: String, + shared_secret: String, +} + +impl SelfVerificationNoMasterKey { + const QR_MODE: u8 = 0x02; + + pub fn to_bytes(&self) -> Result, EncodingError> { + to_bytes( + Self::QR_MODE, + &self.transaction_id, + &self.device_key, + &self.master_key, + &self.shared_secret, + ) + } + + pub fn to_qr_code(&self) -> Result { + to_qr_code( + Self::QR_MODE, + &self.transaction_id, + &self.device_key, + &self.master_key, + &self.shared_secret, + ) + } + + pub fn new( + transaction_id: String, + device_key: String, + master_key: String, + shared_secret: String, + ) -> Self { + Self { transaction_id, device_key, master_key, shared_secret } + } +} + +impl From for QrVerification { + fn from(data: SelfVerificationNoMasterKey) -> Self { + Self::SelfVerificationNoMasterKey(data) + } +} diff --git a/matrix_qrcode/src/utils.rs b/matrix_qrcode/src/utils.rs new file mode 100644 index 00000000..6a30ff76 --- /dev/null +++ b/matrix_qrcode/src/utils.rs @@ -0,0 +1,100 @@ +// Copyright 2021 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::convert::TryInto; + +use base64::{decode_config, encode_config, STANDARD_NO_PAD}; +#[cfg(feature = "decode_image")] +use image::{ImageBuffer, Luma}; +use qrcode::QrCode; + +#[cfg(feature = "decode_image")] +use crate::error::DecodingError; +use crate::error::EncodingError; + +pub(crate) const HEADER: &[u8] = b"MATRIX"; +pub(crate) const VERSION: u8 = 0x2; +pub(crate) const MAX_MODE: u8 = 0x2; +pub(crate) const MIN_SECRET_LEN: usize = 8; + +pub(crate) fn base_64_encode(data: &[u8]) -> String { + encode_config(data, STANDARD_NO_PAD) +} + +pub(crate) fn base64_decode(data: &str) -> Result, base64::DecodeError> { + decode_config(data, STANDARD_NO_PAD) +} + +pub(crate) fn to_bytes( + mode: u8, + flow_id: &str, + first_key: &str, + second_key: &str, + shared_secret: &str, +) -> Result, EncodingError> { + let flow_id_len: u16 = flow_id.len().try_into()?; + let flow_id_len = flow_id_len.to_be_bytes(); + + let first_key = base64_decode(first_key)?; + let second_key = base64_decode(second_key)?; + let shared_secret = base64_decode(shared_secret)?; + + let data = [ + HEADER, + &[VERSION], + &[mode], + flow_id_len.as_ref(), + flow_id.as_bytes(), + &first_key, + &second_key, + &shared_secret, + ] + .concat(); + + Ok(data) +} + +pub(crate) fn to_qr_code( + mode: u8, + flow_id: &str, + first_key: &str, + second_key: &str, + shared_secret: &str, +) -> Result { + let data = to_bytes(mode, flow_id, first_key, second_key, shared_secret)?; + Ok(QrCode::new(data)?) +} + +#[cfg(feature = "decode_image")] +pub(crate) fn decode_qr(image: ImageBuffer, Vec>) -> Result, DecodingError> { + let mut image = rqrr::PreparedImage::prepare(image); + let grids = image.detect_grids(); + + let mut error = None; + + for grid in grids { + let mut decoded = Vec::new(); + + match grid.decode_to(&mut decoded) { + Ok(_) => { + if decoded.starts_with(HEADER) { + return Ok(decoded); + } + } + Err(e) => error = Some(e), + } + } + + Err(error.map(|e| e.into()).unwrap_or_else(|| DecodingError::Header)) +}