After adding the fonctionality for apk publish5
This commit is contained in:
parent
d81f6a3409
commit
bce8fd15d9
@ -1,5 +1,6 @@
|
||||
/**
|
||||
* Parse Android APK metadata and signing certificate fingerprint.
|
||||
* Supports JAR signing (v1) and APK Signature Scheme v2/v3.
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
@ -18,6 +19,10 @@ export type ApkParsedMeta = {
|
||||
signingCertSha256: string | null;
|
||||
};
|
||||
|
||||
const APK_SIG_BLOCK_MAGIC = Buffer.from('APK Sig Block 42', 'utf8');
|
||||
const APK_SIGNATURE_SCHEME_V2_BLOCK_ID = 0x7109871a;
|
||||
const APK_SIGNATURE_SCHEME_V3_BLOCK_ID = 0xf05368c0;
|
||||
|
||||
/** True when file starts with ZIP/APK magic bytes. */
|
||||
export function assertApkZipMagic(filePath: string): void {
|
||||
const fd = fs.openSync(filePath, 'r');
|
||||
@ -36,38 +41,167 @@ function sha256OfDer(der: Buffer): string {
|
||||
return crypto.createHash('sha256').update(der).digest('hex');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract SHA-256 of the first signing certificate found in META-INF (JAR/APK signature).
|
||||
* Returns null when no PKCS#7 cert block can be parsed (e.g. v3-only signing edge cases).
|
||||
*/
|
||||
export function extractSigningCertSha256(filePath: string): string | null {
|
||||
const zip = new AdmZip(filePath);
|
||||
const entries = zip.getEntries().filter((e) => {
|
||||
const name = e.entryName.replace(/\\/g, '/');
|
||||
return (
|
||||
name.startsWith('META-INF/') &&
|
||||
!e.isDirectory &&
|
||||
/\.(RSA|DSA|EC)$/i.test(path.basename(name))
|
||||
);
|
||||
});
|
||||
function readU32LE(buf: Buffer, offset: number): number {
|
||||
return buf.readUInt32LE(offset);
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
try {
|
||||
const der = entry.getData();
|
||||
const asn1 = forge.asn1.fromDer(der.toString('binary'));
|
||||
const p7 = (forge.pkcs7 as unknown as { messageFromAsn1: (a: unknown) => { certificates?: Array<{ toAsn1: () => unknown }> } }).messageFromAsn1(asn1);
|
||||
const certs = p7.certificates || [];
|
||||
if (!certs.length) continue;
|
||||
const asn1Cert = certs[0].toAsn1();
|
||||
const derBytes = Buffer.from(forge.asn1.toDer(asn1Cert as forge.asn1.Asn1).getBytes(), 'binary');
|
||||
return sha256OfDer(derBytes);
|
||||
} catch {
|
||||
// try next entry
|
||||
function readU64LEAsNumber(buf: Buffer, offset: number): number {
|
||||
// APK sizes we care about fit in JS safe integers.
|
||||
const lo = buf.readUInt32LE(offset);
|
||||
const hi = buf.readUInt32LE(offset + 4);
|
||||
return hi * 0x100000000 + lo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate ZIP End of Central Directory and return central-directory offset.
|
||||
*/
|
||||
function findCentralDirectoryOffset(fileBuf: Buffer): number {
|
||||
const maxComment = 0xffff;
|
||||
const minEocd = 22;
|
||||
const start = Math.max(0, fileBuf.length - (minEocd + maxComment));
|
||||
for (let i = fileBuf.length - minEocd; i >= start; i--) {
|
||||
if (fileBuf[i] === 0x50 && fileBuf[i + 1] === 0x4b && fileBuf[i + 2] === 0x05 && fileBuf[i + 3] === 0x06) {
|
||||
const commentLen = fileBuf.readUInt16LE(i + 20);
|
||||
if (i + minEocd + commentLen === fileBuf.length) {
|
||||
return fileBuf.readUInt32LE(i + 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error('ZIP End of Central Directory not found');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract first X.509 certificate DER from an APK Signing Block v2 or v3 value.
|
||||
* Layout: length-prefixed signers → each has length-prefixed signed-data →
|
||||
* digests then certificates (length-prefixed sequence of length-prefixed DER certs).
|
||||
*/
|
||||
function firstCertDerFromV2orV3Block(blockValue: Buffer): Buffer | null {
|
||||
if (blockValue.length < 4) return null;
|
||||
const signersLen = readU32LE(blockValue, 0);
|
||||
if (signersLen <= 0 || 4 + signersLen > blockValue.length) return null;
|
||||
|
||||
let offset = 4;
|
||||
const signersEnd = 4 + signersLen;
|
||||
while (offset + 4 <= signersEnd) {
|
||||
const signerLen = readU32LE(blockValue, offset);
|
||||
offset += 4;
|
||||
if (signerLen <= 0 || offset + signerLen > signersEnd) break;
|
||||
const signer = blockValue.subarray(offset, offset + signerLen);
|
||||
offset += signerLen;
|
||||
|
||||
if (signer.length < 4) continue;
|
||||
const signedDataLen = readU32LE(signer, 0);
|
||||
if (signedDataLen <= 0 || 4 + signedDataLen > signer.length) continue;
|
||||
const signedData = signer.subarray(4, 4 + signedDataLen);
|
||||
|
||||
if (signedData.length < 4) continue;
|
||||
const digestsLen = readU32LE(signedData, 0);
|
||||
let sdOff = 4 + digestsLen;
|
||||
if (digestsLen < 0 || sdOff + 4 > signedData.length) continue;
|
||||
|
||||
const certsLen = readU32LE(signedData, sdOff);
|
||||
sdOff += 4;
|
||||
if (certsLen <= 0 || sdOff + certsLen > signedData.length) continue;
|
||||
|
||||
const certsEnd = sdOff + certsLen;
|
||||
if (sdOff + 4 > certsEnd) continue;
|
||||
const certLen = readU32LE(signedData, sdOff);
|
||||
sdOff += 4;
|
||||
if (certLen <= 0 || sdOff + certLen > certsEnd) continue;
|
||||
return Buffer.from(signedData.subarray(sdOff, sdOff + certLen));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read signing cert from APK Signature Scheme v2/v3 block (before ZIP central directory).
|
||||
*/
|
||||
export function extractSigningCertSha256FromApkSigningBlock(filePath: string): string | null {
|
||||
const fileBuf = fs.readFileSync(filePath);
|
||||
let cdOffset: number;
|
||||
try {
|
||||
cdOffset = findCentralDirectoryOffset(fileBuf);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (cdOffset < 32) return null;
|
||||
|
||||
const magicOffset = cdOffset - 16;
|
||||
if (!fileBuf.subarray(magicOffset, cdOffset).equals(APK_SIG_BLOCK_MAGIC)) return null;
|
||||
|
||||
const blockSizeFooter = readU64LEAsNumber(fileBuf, cdOffset - 24);
|
||||
if (blockSizeFooter < 24) return null;
|
||||
// Header size field sits immediately before the pairs; total block = 8 + blockSizeFooter.
|
||||
const blockStart = cdOffset - blockSizeFooter - 8;
|
||||
if (blockStart < 0) return null;
|
||||
const blockSizeHeader = readU64LEAsNumber(fileBuf, blockStart);
|
||||
if (blockSizeHeader !== blockSizeFooter) return null;
|
||||
|
||||
let pairsOffset = blockStart + 8;
|
||||
const pairsEnd = cdOffset - 24;
|
||||
let v2Value: Buffer | null = null;
|
||||
let v3Value: Buffer | null = null;
|
||||
|
||||
while (pairsOffset + 12 <= pairsEnd) {
|
||||
const pairLen = readU64LEAsNumber(fileBuf, pairsOffset);
|
||||
pairsOffset += 8;
|
||||
if (pairLen < 4 || pairsOffset + pairLen > pairsEnd) break;
|
||||
const id = readU32LE(fileBuf, pairsOffset);
|
||||
const value = fileBuf.subarray(pairsOffset + 4, pairsOffset + pairLen);
|
||||
pairsOffset += pairLen;
|
||||
if (id === APK_SIGNATURE_SCHEME_V2_BLOCK_ID) v2Value = Buffer.from(value);
|
||||
if (id === APK_SIGNATURE_SCHEME_V3_BLOCK_ID) v3Value = Buffer.from(value);
|
||||
}
|
||||
|
||||
// Prefer v3 then v2 (same first signer cert in normal dual-signed APKs).
|
||||
for (const value of [v3Value, v2Value]) {
|
||||
if (!value) continue;
|
||||
const der = firstCertDerFromV2orV3Block(value);
|
||||
if (der && der.length > 0) return sha256OfDer(der);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract SHA-256 of the first signing certificate (JAR v1 META-INF, then APK v2/v3).
|
||||
*/
|
||||
export function extractSigningCertSha256(filePath: string): string | null {
|
||||
try {
|
||||
const zip = new AdmZip(filePath);
|
||||
const entries = zip.getEntries().filter((e) => {
|
||||
const name = e.entryName.replace(/\\/g, '/');
|
||||
return (
|
||||
name.startsWith('META-INF/') &&
|
||||
!e.isDirectory &&
|
||||
/\.(RSA|DSA|EC)$/i.test(path.basename(name))
|
||||
);
|
||||
});
|
||||
|
||||
for (const entry of entries) {
|
||||
try {
|
||||
const der = entry.getData();
|
||||
const asn1 = forge.asn1.fromDer(der.toString('binary'));
|
||||
const p7 = (
|
||||
forge.pkcs7 as unknown as {
|
||||
messageFromAsn1: (a: unknown) => { certificates?: Array<{ toAsn1: () => unknown }> };
|
||||
}
|
||||
).messageFromAsn1(asn1);
|
||||
const certs = p7.certificates || [];
|
||||
if (!certs.length) continue;
|
||||
const asn1Cert = certs[0].toAsn1();
|
||||
const derBytes = Buffer.from(forge.asn1.toDer(asn1Cert as forge.asn1.Asn1).getBytes(), 'binary');
|
||||
return sha256OfDer(derBytes);
|
||||
} catch {
|
||||
// try next entry
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// fall through to v2/v3
|
||||
}
|
||||
|
||||
return extractSigningCertSha256FromApkSigningBlock(filePath);
|
||||
}
|
||||
|
||||
/** Parse packageName / versionCode / versionName and signing cert fingerprint. */
|
||||
export async function parseApkFile(filePath: string): Promise<ApkParsedMeta> {
|
||||
assertApkZipMagic(filePath);
|
||||
|
||||
@ -48,6 +48,8 @@ ANDROID_APK_EXPECTED_PACKAGE=com.prediqo.mobile
|
||||
|
||||
Empty fingerprint skips cert check (dev only).
|
||||
|
||||
The API reads the signing cert from APK Signature Scheme **v2/v3** (and JAR v1 when present). Modern Flutter release APKs often have no `META-INF/*.RSA`.
|
||||
|
||||
## Admin publish flow
|
||||
|
||||
1. Create a release (version name + **versionCode** strictly greater than the current published one)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user