diff options
author | Teddy Wing | 2018-11-07 21:16:35 +0100 |
---|---|---|
committer | Teddy Wing | 2018-11-07 21:16:35 +0100 |
commit | d4b1a74ddbd5f9f629541affb6b43d34d8cbd925 (patch) | |
tree | 281b768ebaee51149b4bc0501188f6fce3af6443 | |
parent | 2e575e48be0ff909656a5e309f4a727d18555f49 (diff) | |
download | dome-key-web-d4b1a74ddbd5f9f629541affb6b43d34d8cbd925.tar.bz2 |
AquaticPrime::plist(): Fix signature
When I updated the plist function tests, I discovered that the `data`
field for "Signature" was incorrect.
Turns out that the 'plist' crate takes it upon itself to base64 encode
the input Vec it gets:
https://github.com/ebarnard/rust-plist/blob/v0.3.0/src/xml/writer.rs#L171-L174
This meant I was double-base64 encoding the signature. To fix this, I
removed base64 encoding from the `sign()` method, and return the `[u8]`
array directly.
I thought this would be the end of it, but I ran into another problem
where my tests failed. It turns out that the 'plist' crate base64
encodes using the `base64::MIME` config, which wraps the base64-encoded
string to 76 characters and uses CRLF line endings. But my XML string
uses plain LF line endings, so I ended up with useless CRs in the
`<data>` tag.
To solve this, I ended up having to fork the 'plist' crate and change it
to use the `base64::STANDARD` config, which doesn't line wrap the base64
string. For now the fork is a local copy. I'll publish it when I'm ready
to publish the rest.
-rw-r--r-- | license-generator/Cargo.lock | 4 | ||||
-rw-r--r-- | license-generator/aquatic-prime/Cargo.toml | 2 | ||||
-rw-r--r-- | license-generator/aquatic-prime/src/lib.rs | 45 |
3 files changed, 31 insertions, 20 deletions
diff --git a/license-generator/Cargo.lock b/license-generator/Cargo.lock index 4dcc754..cee7dd9 100644 --- a/license-generator/Cargo.lock +++ b/license-generator/Cargo.lock @@ -5,7 +5,7 @@ dependencies = [ "base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)", - "plist 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "plist 0.3.0", "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -154,7 +154,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "plist" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -275,7 +274,6 @@ dependencies = [ "checksum openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "5e1309181cdcbdb51bc3b6bedb33dfac2a83b3d585033d3f6d9e22e8c1928613" "checksum openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)" = "278c1ad40a89aa1e741a1eed089a2f60b18fab8089c3139b542140fc7d674106" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" -"checksum plist 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0c7316832d9ac5da02786bdc89a3faf0ca07070212b388766e969078fd593edc" "checksum proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "3d7b7eaaa90b4a90a932a9ea6666c95a389e424eff347f0f793979289429feee" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "63b5829244f52738cfee93b3a165c1911388675be000c888d2fae620dee8fa5b" diff --git a/license-generator/aquatic-prime/Cargo.toml b/license-generator/aquatic-prime/Cargo.toml index 2247930..a8628d6 100644 --- a/license-generator/aquatic-prime/Cargo.toml +++ b/license-generator/aquatic-prime/Cargo.toml @@ -6,6 +6,6 @@ version = "0.0.1" base64 = "0.10.0" error-chain = "0.12.0" openssl = "0.10.15" -plist = "0.3.0" +plist = { path = "../../../rust-plist", branch = "data-without-base64-mime" } serde = "1.0.80" serde_derive = "1.0.80" diff --git a/license-generator/aquatic-prime/src/lib.rs b/license-generator/aquatic-prime/src/lib.rs index 1230119..2ded9a1 100644 --- a/license-generator/aquatic-prime/src/lib.rs +++ b/license-generator/aquatic-prime/src/lib.rs @@ -50,7 +50,7 @@ struct AquaticPrime<'a> { } impl<'a> AquaticPrime<'a> { - fn sign(&self, input_data: HashMap<String, String>) -> Result<String> { + fn sign(&self, input_data: HashMap<String, String>) -> Result<[u8; 128]> { let mut input_data: Vec<(String, String)> = input_data .into_iter() .collect(); @@ -96,7 +96,7 @@ impl<'a> AquaticPrime<'a> { Padding::PKCS1, ).chain_err(|| "failed to encrypt input")?; - Ok(base64::encode(&signature[..])) + Ok(signature) } fn plist<T: Serialize>(&self, input_data: T) -> Result<String> { @@ -121,7 +121,7 @@ impl<'a> AquaticPrime<'a> { let signature = self.sign(data)?; plist_dict.insert( "Signature".to_owned(), - Plist::Data(signature.into_bytes()) + Plist::Data(signature.to_vec()) ); // Generate plist XML string @@ -176,7 +176,7 @@ mod tests { qD7a48WFqbzC3powTk6x42b+WpH6boe+u7LW4AXo2ZqGPasVlr1/lUWVHvt5J0OI9oR7\ vmzdXHbbQD7RPXp0ezttrKBFHxNNCbJHMr0="; - assert_eq!(signature.unwrap(), expected); + assert_eq!(base64::encode(&signature.unwrap()[..]), expected); let mut license_data = HashMap::new(); @@ -193,7 +193,7 @@ mod tests { cztBoUJFu8mB45MHE0jmmbRw3qK6FJz9Py2gi1XvGOgH3GW713OCvQBE7vfBj4ZriP0+\ FS18nLfrtM6Xp0mAd1la4DD4oh7d35dlYTY="; - assert_eq!(signature.unwrap(), expected); + assert_eq!(base64::encode(&signature.unwrap()[..]), expected); } #[test] @@ -205,9 +205,7 @@ mod tests { license_data.insert("Name", "Üsér Diacriticà"); license_data.insert("lowercase key", "Keys should be sorted case-insensitive"); - aquatic_prime.plist(license_data); - -r#"<?xml version="1.0" encoding="UTF-8"?> + let expected = r#"<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> @@ -216,15 +214,16 @@ r#"<?xml version="1.0" encoding="UTF-8"?> <key>Name</key> <string>Üsér Diacriticà</string> <key>Signature</key> - <data> - RIhF/3CgyXzPg2wCQ5LShf6W9khtqPcqUDLAHcAZdOIcoeR7PoOHi15423kxq5jOh1lm - cztBoUJFu8mB45MHE0jmmbRw3qK6FJz9Py2gi1XvGOgH3GW713OCvQBE7vfBj4ZriP0+ - FS18nLfrtM6Xp0mAd1la4DD4oh7d35dlYTY= - </data> + <data>RIhF/3CgyXzPg2wCQ5LShf6W9khtqPcqUDLAHcAZdOIcoeR7PoOHi15423kxq5jOh1lmcztBoUJFu8mB45MHE0jmmbRw3qK6FJz9Py2gi1XvGOgH3GW713OCvQBE7vfBj4ZriP0+FS18nLfrtM6Xp0mAd1la4DD4oh7d35dlYTY=</data> <key>lowercase key</key> <string>Keys should be sorted case-insensitive</string> </dict> </plist>"#; + + assert_eq!( + aquatic_prime.plist(license_data).unwrap(), + expected + ); } #[test] @@ -235,15 +234,29 @@ r#"<?xml version="1.0" encoding="UTF-8"?> struct LicenseData<'a> { name: &'a str, email: &'a str, - // signature: Vec<u8>, }; let license_data = LicenseData { name: "User", email: "user@example.com", - // signature: vec![], }; - aquatic_prime.plist(license_data); + let expected = r#"<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>Signature</key> + <data>djFsFgYkt/ESgOnR+dpOFBTqpgWCG9aZggOYG/zv3uOEMf39Zwt5m7L+ulFjmZvfvUR/twuwKRfPWLGjoDHPQqwbBED3PcIP4asBeRbt28y6425tah4KV5SVnzVmZgAwCjkeuOEO5WPljiPXkvbUEVqNaEm79moMkHO9nYKdnP0=</data> + <key>email</key> + <string>user@example.com</string> + <key>name</key> + <string>User</string> +</dict> +</plist>"#; + + assert_eq!( + aquatic_prime.plist(license_data).unwrap(), + expected + ); } } |