1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

try to do more work on auth signing (untested)

This commit is contained in:
Ubuntu 2022-10-13 18:21:09 +00:00
parent 9d5299ccc1
commit 321e06f1e4
4 changed files with 101 additions and 56 deletions

29
Cargo.lock generated
View file

@ -165,9 +165,11 @@ dependencies = [
"anyhow",
"azalea-buf",
"base64",
"byteorder",
"log",
"num-bigint 0.4.3",
"reqwest",
"ring",
"serde",
"serde_json",
"sp-core",
@ -2284,6 +2286,21 @@ dependencies = [
"quick-error",
]
[[package]]
name = "ring"
version = "0.16.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
dependencies = [
"cc",
"libc",
"once_cell",
"spin",
"untrusted",
"web-sys",
"winapi",
]
[[package]]
name = "rsa_public_encrypt_pkcs1"
version = "0.4.0"
@ -2765,6 +2782,12 @@ dependencies = [
"wasmi",
]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "ss58-registry"
version = "1.31.0"
@ -3195,6 +3218,12 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "url"
version = "2.2.2"

View file

@ -11,9 +11,11 @@ version = "0.1.0"
anyhow = "1.0.65"
azalea-buf = { path = "../azalea-buf", version = "^0.1.0" }
base64 = "0.13.0"
byteorder = "1.4.3"
log = "0.4.17"
num-bigint = "0.4.3"
reqwest = {version = "0.11.12", features = ["json"]}
ring = "0.16.20"
serde = {version = "1.0.145", features = ["derive"]}
serde_json = "1.0.86"
sp-core = "6.0.0"

View file

@ -9,6 +9,7 @@ use std::{
use anyhow::anyhow;
use azalea_buf::McBufWritable;
use byteorder::WriteBytesExt;
use num_bigint::BigUint;
use reqwest::Url;
use serde::Deserialize;
@ -195,19 +196,19 @@ async fn auth_with_xbox_live(
"TokenType": "JWT"
});
let payload = auth_json.to_string();
let signature = sign(
"https://user.auth.xboxlive.com/user/authenticate",
"",
&payload,
)?;
// let signature = sign(
// "https://user.auth.xboxlive.com/user/authenticate",
// "",
// &payload,
// )?;
println!("auth_json: {:#?}", auth_json);
let res = client
.post("https://user.auth.xboxlive.com/user/authenticate")
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("x-xbl-contract-version", "2")
.header("Cache-Control", "no-store, must-revalidate, no-cache")
.header("Signature", base64::encode(signature))
.header("x-xbl-contract-version", "1")
// .header("Cache-Control", "no-store, must-revalidate, no-cache")
// .header("Signature", base64::encode(signature))
.body(payload)
.send()
.await?
@ -306,61 +307,67 @@ async fn get_profile(
Ok(res)
}
// from https://github.com/PrismarineJS/prismarine-auth/blob/master/src/TokenManagers/XboxTokenManager.js#L112
fn sign(url: &str, authorization_token: &str, payload: &str) -> anyhow::Result<Vec<u8>> {
// const windowsTimestamp = (BigInt((Date.now() / 1000) | 0) + 11644473600n) * 10000000n
// // Only the /uri?and-query-string
// const pathAndQuery = new URL(url).pathname
// // from https://github.com/PrismarineJS/prismarine-auth/blob/master/src/TokenManagers/XboxTokenManager.js#L112
// fn sign(url: &str, authorization_token: &str, payload: &str) -> anyhow::Result<Vec<u8>> {
// // const windowsTimestamp = (BigInt((Date.now() / 1000) | 0) + 11644473600n) * 10000000n
// // // Only the /uri?and-query-string
// // const pathAndQuery = new URL(url).pathname
// // Allocate the buffer for signature, TS, path, tokens and payload and NUL termination
// const allocSize = /* sig */ 5 + /* ts */ 9 + /* POST */ 5 + pathAndQuery.length + 1 + authorizationToken.length + 1 + payload.length + 1
// const buf = SmartBuffer.fromSize(allocSize)
// buf.writeInt32BE(1) // Policy Version
// buf.writeUInt8(0)
// buf.writeBigUInt64BE(windowsTimestamp)
// buf.writeUInt8(0) // null term
// buf.writeStringNT('POST')
// buf.writeStringNT(pathAndQuery)
// buf.writeStringNT(authorizationToken)
// buf.writeStringNT(payload)
// // // Allocate the buffer for signature, TS, path, tokens and payload and NUL termination
// // const allocSize = /* sig */ 5 + /* ts */ 9 + /* POST */ 5 + pathAndQuery.length + 1 + authorizationToken.length + 1 + payload.length + 1
// // const buf = SmartBuffer.fromSize(allocSize)
// // buf.writeInt32BE(1) // Policy Version
// // buf.writeUInt8(0)
// // buf.writeBigUInt64BE(windowsTimestamp)
// // buf.writeUInt8(0) // null term
// // buf.writeStringNT('POST')
// // buf.writeStringNT(pathAndQuery)
// // buf.writeStringNT(authorizationToken)
// // buf.writeStringNT(payload)
let windows_timestamp =
BigUint::from((SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() + 11644473600))
* 10000000;
let path_and_query = Url::parse(url)?.path();
let mut buf = Vec::new();
1u32.write_into(&mut buf)?; // policy version
0u8.write_into(&mut buf)?;
windows_timestamp.write_into(&mut buf)?;
0u8.write_into(&mut buf)?; // null term
"POST".write_into(&mut buf)?;
path_and_query.write_into(&mut buf)?;
authorization_token.write_into(&mut buf)?;
payload.write_into(&mut buf)?;
// let windows_timestamp: BigUint =
// BigUint::from(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() + 11644473600)
// * 10000000;
// let path_and_query = Url::parse(url)?.path();
// let mut buf = Vec::new();
// buf.write_u32(1); // policy version
// buf.write_u8(0);
// buf.write_u64(windows_timestamp.try_into().unwrap());
// buf.write_u8(0); // null term
// buf.extend_from_slice("POST\0".as_bytes());
// const keyPair = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' })
// buf.extend_from_slice(path_and_query.as_bytes());
// buf.write_u8(0);
// // Get the signature from the payload
// const signature = crypto.sign('SHA256', buf.toBuffer(), { key: this.key.privateKey, dsaEncoding: 'ieee-p1363' })
// buf.extend_from_slice(authorization_token.as_bytes());
// buf.write_u8(0);
// const header = SmartBuffer.fromSize(signature.length + 12)
// header.writeInt32BE(1) // Policy Version
// header.writeBigUInt64BE(windowsTimestamp)
// header.writeBuffer(signature) // Add signature at end of header
// buf.extend_from_slice(payload.as_bytes());
// buf.write_u8(0);
// return header.toBuffer()
// // const keyPair = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' })
let key_pair = EcKeyPair::generate(&EcParams::by_curve_nid(Nid::X9_62_PRIME256V1)?)?;
// // // Get the signature from the payload
// // const signature = crypto.sign('SHA256', buf.toBuffer(), { key: this.key.privateKey, dsaEncoding: 'ieee-p1363' })
let signature =
key_pair
.private_key()
.sign(MessageDigest::sha256(), &buf, &mut BigNumContext::new()?)?;
// // const header = SmartBuffer.fromSize(signature.length + 12)
// // header.writeInt32BE(1) // Policy Version
// // header.writeBigUInt64BE(windowsTimestamp)
// // header.writeBuffer(signature) // Add signature at end of header
let mut header = Vec::new();
1u32.write_into(&mut header)?; // policy version
windows_timestamp.write_into(&mut header)?;
signature.write_into(&mut header)?;
// // return header.toBuffer()
Ok(header)
}
// let key_pair = ring::signature::EcdsaKeyPair::
// let signature =
// key_pair
// .private_key()
// .sign(MessageDigest::sha256(), &buf, &mut BigNumContext::new()?)?;
// let mut header = Vec::new();
// 1u32.write_into(&mut header)?; // policy version
// windows_timestamp.write_into(&mut header)?;
// signature.write_into(&mut header)?;
// Ok(header)
// }

View file

@ -110,6 +110,13 @@ impl McBufWritable for String {
}
}
impl McBufWritable for &str {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
write_utf_with_len(buf, self, MAX_STRING_LENGTH.into())
}
}
impl McBufWritable for u32 {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
i32::write_into(&(*self as i32), buf)