Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Check the Apple ID password for Hidden: the kept copy here, else proved to Apple on its own connection
Yashwanth Kamireddi0cbdbd1c8696
diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rsindex 1159bc9..077fac8 100644--- a/crates/quince-auth/src/lib.rs+++ b/crates/quince-auth/src/lib.rs@@ -441,6 +441,11 @@ enum Code {/// OAuth headers every idmsa call carries. Deliberately sets no Origin: the HTTP client/// defaults to the icloud.com origin, which is what the verify, trust and phone endpoints/// expect. Only SRP overrides it — see [`with_srp_origin`].+/// Two secrets compared without the time taken saying how much of one matched.+fn same_secret(a: &[u8], b: &[u8]) -> bool {+ a.len() == b.len() && a.iter().zip(b).fold(0u8, |d, (x, y)| d | (x ^ y)) == 0+}+fn base_oauth_headers(client_id: &str,home: &str,@@ -1476,6 +1481,103 @@ impl Client {Ok(())}+ /// Whether `password` is this account's Apple ID password: what the Hidden album asks for,+ /// as icloud.com asks for it before showing Hidden. The password this computer already+ /// keeps (in the keyring, for signing back in) answers it here when it matches — offline,+ /// with no sign-in for Apple to note. Otherwise (none kept, or the kept one out of date) it+ /// is proved to Apple as sign-in proves it (SRP: the password itself is never sent) on a client+ /// of its own with no cookies: the live session is never touched, nothing Apple answers is+ /// kept, and no code is ever asked for — Apple waits to be asked before it sends one.+ pub async fn check_password(&mut self, password: &str) -> Result<bool> {+ // The kept copy is right nearly every time. One that doesn't match may be the old one —+ // the password changed on the phone since — so Apple is asked before "wrong" is said.+ if self+ .session+ .password+ .as_deref()+ .is_some_and(|kept| same_secret(kept.as_bytes(), password.as_bytes()))+ {+ return Ok(true);+ }+ let apple_id = self.session.apple_id.clone().ok_or(Error::Protocol(+ "no Apple ID to check the password against".into(),+ ))?;+ let http = apple_only()+ .timeout(std::time::Duration::from_secs(30))+ .build()+ .map_err(Error::Network)?;+ let headers = with_srp_origin(+ base_oauth_headers(&self.session.client_id(), &self.endpoints.home, None, None),+ &self.endpoints.auth_root,+ );+ let client = srp::Client::new(&apple_id);+ let r = http+ .post(format!("{}/signin/init", self.endpoints.auth))+ .headers(headers.clone())+ .body(+ json!({+ "a": B64.encode(client.a_public_bytes()),+ "accountName": apple_id,+ "protocols": ["s2k", "s2k_fo"],+ })+ .to_string(),+ )+ .send()+ .await?;+ let status = r.status();+ let text = r.text().await.unwrap_or_default();+ if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {+ return Ok(false);+ }+ if !status.is_success() {+ return Err(apple_error("checking your password", status, &text));+ }+ let challenge = Challenge::of(&serde_json::from_str(&text)?)?;+ let proof = client+ .process_challenge(+ password,+ challenge.protocol,+ &challenge.salt,+ &challenge.b_pub,+ challenge.iterations,+ )+ .ok_or_else(|| {+ Error::Protocol("Apple's sign-in challenge failed its safety check".into())+ })?;+ let mut body = json!({+ "accountName": apple_id,+ "c": challenge.c,+ "m1": B64.encode(proof.m1),+ "m2": B64.encode(proof.m2),+ "rememberMe": false,+ "trustTokens": [],+ });+ if let Some(t) = &self.session.trust_token {+ body["trustTokens"] = json!([t]);+ }+ let r = http+ .post(format!("{}/signin/complete", self.endpoints.auth))+ .headers(headers)+ .body(body.to_string())+ .send()+ .await?;+ match r.status() {+ // Accepted — whether Apple would want a code next, its escrow step, or nothing: the+ // password was right, and nothing further is asked.+ s if s.is_success()+ || s == StatusCode::CONFLICT+ || s == StatusCode::PRECONDITION_FAILED =>+ {+ Ok(true)+ }+ StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Ok(false),+ s => {+ let text = r.text().await.unwrap_or_default();+ Err(apple_error("checking your password", s, &text))+ }+ }+ }+/// Apple's escrow step: the SRP password proof again, with an empty account name, sending/// the session key with it — `POST escrow/init` `{a, accountName: "", protocols}`, then/// `POST escrow/complete` `{m1, m2, c, k}` (icloud-photos-sync#1124, rclone#10048). Applediff --git a/crates/quince-auth/tests/auth_flow.rs b/crates/quince-auth/tests/auth_flow.rsindex 5a304bf..986e7e4 100644--- a/crates/quince-auth/tests/auth_flow.rs+++ b/crates/quince-auth/tests/auth_flow.rs@@ -1281,6 +1281,85 @@ async fn a_failed_answer_is_never_nowhere_to_send() {}}+/// The Hidden album's password check, with the password kept from a finished sign-in: the+/// kept one is accepted on this computer, Apple never asked.+#[tokio::test]+async fn the_kept_hidden_password_is_checked_here() {+ let server = MockServer::start().await;+ mount_init(&server, 200).await;+ mount_complete(&server, 200).await;+ mount_trust_and_login(&server).await;+ let dir = tempfile::tempdir().unwrap();+ let mut client = client_for(&server, dir.path());+ client+ .sign_in("[email protected]", "hunter2")+ .await+ .unwrap();+ let before = server.received_requests().await.unwrap().len();+ assert!(client.check_password("hunter2").await.unwrap());+ assert_eq!(+ server.received_requests().await.unwrap().len(),+ before,+ "Apple is never asked"+ );+}++/// One that isn't the kept password is asked of Apple (SRP) — it may be a new one set on the+/// phone. Accepted, with a code wanted or not, is right; refused is wrong. On a connection of+/// its own: the live session's token is not replaced, no cookie of it is sent, and no code is+/// ever asked for.+#[tokio::test]+async fn any_other_password_is_asked_of_apple_and_nothing_else_moves() {+ for (complete, right) in [(409, true), (200, true), (401, false)] {+ let server = MockServer::start().await;+ mount_init(&server, 200).await;+ Mock::given(method("POST"))+ .and(path("/appleauth/auth/signin/complete"))+ .respond_with(+ ResponseTemplate::new(200).insert_header("X-Apple-Session-Token", "live-session"),+ )+ .up_to_n_times(1)+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path("/appleauth/auth/signin/complete"))+ .respond_with(+ ResponseTemplate::new(complete)+ .insert_header("X-Apple-Session-Token", "from-the-check"),+ )+ .mount(&server)+ .await;+ mount_trust_and_login(&server).await;+ let dir = tempfile::tempdir().unwrap();+ let mut client = client_for(&server, dir.path());+ client+ .sign_in("[email protected]", "hunter2")+ .await+ .unwrap();+ let token = client.session().session_token.clone();+ assert_eq!(+ client.check_password("changed-on-the-phone").await.unwrap(),+ right,+ "Apple answered {complete}"+ );+ let reqs = server.received_requests().await.unwrap();+ assert!(+ requests_to(&reqs, "/securitycode").is_empty(),+ "{complete}: no code asked for"+ );+ assert_eq!(+ client.session().session_token,+ token,+ "{complete}: the live session is untouched"+ );+ let check = requests_to(&reqs, "/signin/complete").pop().unwrap();+ assert!(+ check.headers.get("cookie").is_none(),+ "{complete}: no cookie of the session's sent"+ );+ }+}+/// The reply as it has always been, with the numbers at the top: read as before, texted.#[tokio::test]async fn trusted_numbers_at_the_top_are_still_texted() {