apvoxa_apkcheck 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35f3a7bb9bd2f55ab3287fa76fdf9087af8233bed5f4630ff3689f387da3226a
4
+ data.tar.gz: dd29ec387aea4be27289ba06ad04daccd1d491c62dba0ceb8aae242ba2514ed8
5
+ SHA512:
6
+ metadata.gz: 9eeec759da29a813b00c54f407edbfa8cd44723c8eabce3a78d0cb42150fdee5bae96078e352b7de8e67c16681b3044e637e56b51e74fdce3ddffe050dda89d0
7
+ data.tar.gz: 421e27e629160fa857e2714754b0cf03cd0f35e9e66fa826b1b34c49af91e0005850f5f209b654b0ee3c93233dc63cd0000763e9bca51b544baded93d84381d0
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # apvoxa_apkcheck
2
+
3
+ A tiny command-line tool and Ruby library to check an APK before you install it:
4
+ it prints the file SHA-256 and the SHA-256 fingerprint of the v1 signing certificate,
5
+ and can compare either one with a fingerprint you copied from the publisher.
6
+
7
+ ```
8
+ gem install apvoxa_apkcheck
9
+ apkcheck app.apk
10
+ apkcheck app.apk AA:BB:CC:... # exit 0 on match
11
+ ```
12
+
13
+ ## Why
14
+
15
+ Sideloading is safer when you compare the signer of the file you downloaded with the
16
+ fingerprint the publisher documents. Guides:
17
+
18
+ - [Install APK files safely on Android](https://apvoxa.com/en/articles/install-apk-safely-on-android)
19
+ - [Published signing fingerprints](https://apvoxa.com/en/articles/published-signing-fingerprints)
20
+ - More on [apvoxa](https://apvoxa.com/), an Android APK directory.
21
+
22
+ ## Limits
23
+
24
+ Only the legacy v1 (META-INF) signature block is read. APKs signed with v2/v3 only need `apksigner verify --print-certs`.
data/bin/apkcheck ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "apvoxa_apkcheck"
5
+
6
+ path, expected = ARGV
7
+ if path.nil? || path == "-h" || path == "--help"
8
+ warn "usage: apkcheck FILE.apk [EXPECTED_SHA256]"
9
+ warn "prints the file SHA-256 and the v1 signing certificate fingerprints;"
10
+ warn "with EXPECTED_SHA256 it exits 0 if file hash or signer matches, 1 otherwise."
11
+ exit(path.nil? ? 2 : 0)
12
+ end
13
+
14
+ fh = ApvoxaApkcheck.file_sha256(path)
15
+ puts "file sha256 : #{ApvoxaApkcheck.pretty(fh)}"
16
+ signers = ApvoxaApkcheck.signing_fingerprints(path)
17
+ signers.each { |s| puts "signer sha256 : #{ApvoxaApkcheck.pretty(s)}" }
18
+ puts "(no v1 signer block found; use apksigner for v2+)" if signers.empty?
19
+
20
+ if expected
21
+ ok = ApvoxaApkcheck.verify_file(path, expected) || ApvoxaApkcheck.verify_signer(path, expected)
22
+ puts ok ? "MATCH" : "MISMATCH"
23
+ exit(ok ? 0 : 1)
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApvoxaApkcheck
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "openssl"
5
+ require "zip"
6
+
7
+ # Small helpers for checking an APK file before installing it.
8
+ #
9
+ # Background reading:
10
+ # https://apvoxa.com/en/articles/install-apk-safely-on-android
11
+ # https://apvoxa.com/en/articles/published-signing-fingerprints
12
+ module ApvoxaApkcheck
13
+ VERSION_FILE = File.expand_path("apvoxa_apkcheck/version.rb", __dir__)
14
+ require VERSION_FILE
15
+
16
+ # SHA-256 of the whole file, lowercase hex.
17
+ def self.file_sha256(path)
18
+ Digest::SHA256.file(path).hexdigest
19
+ end
20
+
21
+ # Normalise "AA:BB:cc..." or "aabbcc..." to lowercase hex without colons.
22
+ def self.normalize(fp)
23
+ fp.to_s.delete(": \n\t").downcase
24
+ end
25
+
26
+ # True if the file hash equals the expected hash (colons/case ignored).
27
+ def self.verify_file(path, expected)
28
+ file_sha256(path) == normalize(expected)
29
+ end
30
+
31
+ # SHA-256 fingerprints of v1 (JAR) signing certificates found in META-INF.
32
+ # APKs signed only with v2+ schemes have no META-INF block; use apksigner then.
33
+ def self.signing_fingerprints(path)
34
+ out = []
35
+ Zip::File.open(path) do |zip|
36
+ zip.glob("META-INF/*.{RSA,DSA,EC}").each do |entry|
37
+ pkcs7 = OpenSSL::PKCS7.new(entry.get_input_stream.read)
38
+ pkcs7.certificates.each do |cert|
39
+ out << Digest::SHA256.hexdigest(cert.to_der)
40
+ end
41
+ end
42
+ end
43
+ out.uniq
44
+ end
45
+
46
+ # Compare the signing certificate to a published fingerprint.
47
+ def self.verify_signer(path, expected)
48
+ signing_fingerprints(path).include?(normalize(expected))
49
+ end
50
+
51
+ def self.pretty(hex)
52
+ hex.upcase.scan(/../).join(":")
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apvoxa_apkcheck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Apvoxa Guides
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rubyzip
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ description: Command-line helper that prints an APK's file SHA-256 and v1 signing
27
+ certificate fingerprint and compares them with a published value. See https://apvoxa.com/en/articles/published-signing-fingerprints
28
+ for how to find publisher fingerprints.
29
+ executables:
30
+ - apkcheck
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - bin/apkcheck
36
+ - lib/apvoxa_apkcheck.rb
37
+ - lib/apvoxa_apkcheck/version.rb
38
+ homepage: https://apvoxa.com/en/articles/install-apk-safely-on-android
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://apvoxa.com/en/articles/install-apk-safely-on-android
43
+ documentation_uri: https://apvoxa.com/en/articles/published-signing-fingerprints
44
+ source_code_uri: https://apvoxa.com/
45
+ changelog_uri: https://apvoxa.com/en/articles/published-signing-fingerprints
46
+ bug_tracker_uri: https://apvoxa.com/
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.6.7
62
+ specification_version: 4
63
+ summary: Check an APK's SHA-256 and signing certificate fingerprint before installing
64
+ test_files: []