safety_net_attestation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '095e5d76aa3845bde2f85d4035a1ec6c87e1eca17c77386e89e093bfcac73746'
4
+ data.tar.gz: 45b6fcf2fda15262cc462c9edb260b940b90ce98cb9458ed8db0042e3e964667
5
+ SHA512:
6
+ metadata.gz: a191389fa782094c8ba8c90cfb929b7e40165d56a745775e4a7c1c078097add9af1b6aa032224fa587e9b287629a6d784e8c8ced13cfcce006a2ca76b1f03223
7
+ data.tar.gz: 932f39dcffb6cc06b51ae993b3d47806c9ccc4b72d1e46475f7cd48c47af9377bc551c8c413240c79d7e5c6a420b960c4e0e3857ef7c80f26e864258ef2d2d73
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,197 @@
1
+ inherit_mode:
2
+ merge:
3
+ - AllowedNames
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.3
7
+ DisabledByDefault: true
8
+ Exclude:
9
+ - "gemfiles/**/*"
10
+ - "vendor/**/*"
11
+
12
+ Bundler:
13
+ Enabled: true
14
+
15
+ Gemspec:
16
+ Enabled: true
17
+
18
+ Layout:
19
+ Enabled: true
20
+
21
+ Lint:
22
+ Enabled: true
23
+
24
+ Metrics/LineLength:
25
+ Max: 120
26
+
27
+ Naming:
28
+ Enabled: true
29
+
30
+ Security:
31
+ Enabled: true
32
+
33
+ Style/BlockComments:
34
+ Enabled: true
35
+
36
+ Style/BracesAroundHashParameters:
37
+ Enabled: true
38
+
39
+ Style/CaseEquality:
40
+ Enabled: true
41
+
42
+ Style/ClassAndModuleChildren:
43
+ Enabled: true
44
+
45
+ Style/ClassMethods:
46
+ Enabled: true
47
+
48
+ Style/ClassVars:
49
+ Enabled: true
50
+
51
+ Style/CommentAnnotation:
52
+ Enabled: true
53
+
54
+ Style/ConditionalAssignment:
55
+ Enabled: true
56
+
57
+ Style/DefWithParentheses:
58
+ Enabled: true
59
+
60
+ Style/Dir:
61
+ Enabled: true
62
+
63
+ Style/EachForSimpleLoop:
64
+ Enabled: true
65
+
66
+ Style/EachWithObject:
67
+ Enabled: true
68
+
69
+ Style/EmptyBlockParameter:
70
+ Enabled: true
71
+
72
+ Style/EmptyCaseCondition:
73
+ Enabled: true
74
+
75
+ Style/EmptyElse:
76
+ Enabled: true
77
+
78
+ Style/EmptyLambdaParameter:
79
+ Enabled: true
80
+
81
+ Style/EmptyLiteral:
82
+ Enabled: true
83
+
84
+ Style/EvenOdd:
85
+ Enabled: true
86
+
87
+ Style/ExpandPathArguments:
88
+ Enabled: true
89
+
90
+ Style/For:
91
+ Enabled: true
92
+
93
+ Style/FrozenStringLiteralComment:
94
+ Enabled: true
95
+
96
+ Style/GlobalVars:
97
+ Enabled: true
98
+
99
+ Style/HashSyntax:
100
+ Enabled: true
101
+
102
+ Style/IdenticalConditionalBranches:
103
+ Enabled: true
104
+
105
+ Style/IfInsideElse:
106
+ Enabled: true
107
+
108
+ Style/InverseMethods:
109
+ Enabled: true
110
+
111
+ Style/MethodCallWithoutArgsParentheses:
112
+ Enabled: true
113
+
114
+ Style/MethodDefParentheses:
115
+ Enabled: true
116
+
117
+ Style/MultilineMemoization:
118
+ Enabled: true
119
+
120
+ Style/MutableConstant:
121
+ Enabled: true
122
+
123
+ Style/NestedParenthesizedCalls:
124
+ Enabled: true
125
+
126
+ Style/OptionalArguments:
127
+ Enabled: true
128
+
129
+ Style/ParenthesesAroundCondition:
130
+ Enabled: true
131
+
132
+ Style/RedundantBegin:
133
+ Enabled: true
134
+
135
+ Style/RedundantConditional:
136
+ Enabled: true
137
+
138
+ Style/RedundantException:
139
+ Enabled: true
140
+
141
+ Style/RedundantFreeze:
142
+ Enabled: true
143
+
144
+ Style/RedundantParentheses:
145
+ Enabled: true
146
+
147
+ Style/RedundantReturn:
148
+ Enabled: true
149
+
150
+ Style/RedundantSelf:
151
+ Enabled: true
152
+
153
+ Style/Semicolon:
154
+ Enabled: true
155
+
156
+ Style/SingleLineMethods:
157
+ Enabled: true
158
+
159
+ Style/SpecialGlobalVars:
160
+ Enabled: true
161
+
162
+ Style/SymbolLiteral:
163
+ Enabled: true
164
+
165
+ Style/StringLiterals:
166
+ Enabled: true
167
+ EnforcedStyle: double_quotes
168
+
169
+ Style/TrailingBodyOnClass:
170
+ Enabled: true
171
+
172
+ Style/TrailingBodyOnMethodDefinition:
173
+ Enabled: true
174
+
175
+ Style/TrailingBodyOnModule:
176
+ Enabled: true
177
+
178
+ Style/TrailingMethodEndStatement:
179
+ Enabled: true
180
+
181
+ Style/TrivialAccessors:
182
+ Enabled: true
183
+
184
+ Style/UnneededInterpolation:
185
+ Enabled: true
186
+
187
+ Style/UnneededPercentQ:
188
+ Enabled: true
189
+
190
+ Style/UnpackFirst:
191
+ Enabled: true
192
+
193
+ Style/YodaCondition:
194
+ Enabled: true
195
+
196
+ Style/ZeroLengthPredicate:
197
+ Enabled: true
@@ -0,0 +1,18 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ - 2.5.7
7
+ - 2.4.9
8
+ - 2.3.8
9
+ script:
10
+ - bin/rspec
11
+ jobs:
12
+ fast_finish: true
13
+ include:
14
+ - rvm: 2.6.5
15
+ name: Rubocop
16
+ script:
17
+ - bundle info rubocop
18
+ - bin/rubocop
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2019-12-28
10
+ ### Added
11
+ - Extracted from [webauthn-ruby](https://github.com/cedarcode/webauthn-ruby) after discussion with the maintainers. Thanks for the feedback @grzuy and @brauliomartinezlm!
12
+
13
+ [Unreleased]: https://github.com/bdewater/fido_metadata/compare/v0.1.0...HEAD
14
+ [0.1.0]: https://github.com/bdewater/fido_metadata/releases/tag/v0.1.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at bartdewater@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in safety_net_attestation.gemspec
6
+ gemspec
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ safety_net_attestation (0.1.0)
5
+ jwt (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ byebug (11.0.1)
12
+ coderay (1.1.2)
13
+ diff-lcs (1.3)
14
+ jaro_winkler (1.5.4)
15
+ jwt (2.2.1)
16
+ method_source (0.9.2)
17
+ parallel (1.19.1)
18
+ parser (2.7.0.0)
19
+ ast (~> 2.4.0)
20
+ pry (0.12.2)
21
+ coderay (~> 1.1.0)
22
+ method_source (~> 0.9.0)
23
+ pry-byebug (3.7.0)
24
+ byebug (~> 11.0)
25
+ pry (~> 0.10)
26
+ rainbow (3.0.0)
27
+ rspec (3.9.0)
28
+ rspec-core (~> 3.9.0)
29
+ rspec-expectations (~> 3.9.0)
30
+ rspec-mocks (~> 3.9.0)
31
+ rspec-core (3.9.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-expectations (3.9.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-mocks (3.9.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.9.0)
39
+ rspec-support (3.9.0)
40
+ rubocop (0.75.0)
41
+ jaro_winkler (~> 1.5.1)
42
+ parallel (~> 1.10)
43
+ parser (>= 2.6)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 1.7)
47
+ ruby-progressbar (1.10.1)
48
+ unicode-display_width (1.6.0)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ bundler
55
+ pry-byebug
56
+ rspec (~> 3.8)
57
+ rubocop (= 0.75.0)
58
+ safety_net_attestation!
59
+
60
+ BUNDLED WITH
61
+ 2.1.2
@@ -0,0 +1,77 @@
1
+ # SafetyNetAttestation
2
+
3
+ A Ruby gem to verify SafetyNet attestation statements from Google Play Services on your server.
4
+
5
+ This gem verifies that the statement:
6
+ - has a valid signature that is trusted using certificates from https://pki.goog/
7
+ - has the correct nonce
8
+ - has been generated recently (default allowed leeway from current time is 60 seconds)
9
+ - has a signing certificate with the correct subject
10
+
11
+ With a valid statement your application can then inspect the information contained about the device integrity, calling
12
+ app, and if applicable any integrity errors and potential solutions (see usage).
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'safety_net_attestation'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle install
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install safety_net_attestation
29
+
30
+ ## Usage
31
+
32
+ Request an attestation statement as described in the [Android developer documentation](https://developer.android.com/training/safetynet/attestation#request-attestation-process) and send the JWS response to your server application.
33
+
34
+ In your server application code, do the following:
35
+
36
+ ```ruby
37
+ require "safety_net_attestation"
38
+
39
+ statement = begin
40
+ SafetyNetAttestation::Statement.new(jws_response).verify(nonce)
41
+ rescue SafetyNetAttestation::Error => e
42
+ # Statement is not valid, you should abort
43
+ end
44
+
45
+ statement.json
46
+ # => {"apkPackageName": "com.package.name.of.requesting.app", "ctsProfileMatch": true, ... }
47
+
48
+ # snake cased convenience methods are available after #verify call succeeded, use these to make your specific checks:
49
+ statement.cts_profile_match?
50
+ # => true
51
+ statement.basic_integrity?
52
+ # => true
53
+ statement.apk_package_name
54
+ # => "com.package.name.of.requesting.app"
55
+ statement.apk_certificate_digest_sha256
56
+ # => ["..."]
57
+ statement.error
58
+ # => nil
59
+ statement.advice
60
+ # => nil
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bdewater/safety_net_attestation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
76
+
77
+ The gem and its authors are unaffiliated with Google.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/safety_net_attestation/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "safety_net_attestation"
7
+ spec.version = SafetyNetAttestation::VERSION
8
+ spec.authors = ["Bart de Water"]
9
+
10
+ spec.summary = "SafetyNet attestation response verification"
11
+ spec.homepage = "https://github.com/bdewater/safety_net_attestation"
12
+ spec.license = "MIT"
13
+
14
+ if spec.respond_to?(:metadata)
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
18
+ end
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.required_ruby_version = ">= 2.3"
30
+
31
+ spec.add_dependency "jwt", "~> 2.0"
32
+ spec.add_development_dependency "bundler"
33
+ spec.add_development_dependency "pry-byebug"
34
+ spec.add_development_dependency "rspec", "~> 3.8"
35
+ spec.add_development_dependency "rubocop", "0.75.0"
36
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "safety_net_attestation"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("bundle", __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("bundle", __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "safety_net_attestation/statement"
4
+ require_relative "safety_net_attestation/version"
5
+
6
+ module SafetyNetAttestation
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SafetyNetAttestation
4
+ class Error < StandardError; end
5
+ class NotVerifiedError < Error; end
6
+ class NonceMismatchError < Error; end
7
+ class TimestampError < Error; end
8
+ class CertificateSubjectError < Error; end
9
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+
5
+ module SafetyNetAttestation
6
+ module FixedLengthSecureCompare
7
+ unless OpenSSL.singleton_class.method_defined?(:fixed_length_secure_compare)
8
+ refine OpenSSL.singleton_class do
9
+ def fixed_length_secure_compare(a, b) # rubocop:disable Naming/UncommunicativeMethodParamName
10
+ raise ArgumentError, "inputs must be of equal length" unless a.bytesize == b.bytesize
11
+
12
+ # borrowed from Rack::Utils
13
+ l = a.unpack("C*")
14
+ r, i = 0, -1
15
+ b.each_byte { |v| r |= v ^ l[i += 1] }
16
+ r == 0
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jwt"
4
+ require "openssl"
5
+ require "time"
6
+ require_relative "errors"
7
+ require_relative "fixed_length_secure_compare"
8
+ require_relative "x5c_key_finder"
9
+
10
+ module SafetyNetAttestation
11
+ class Statement
12
+ GOOGLE_ROOT_CERTIFICATES = Dir.glob(
13
+ File.join(__dir__, "lib", "safety_net_attestation", "certificates", "*.*")
14
+ ).map do |file|
15
+ OpenSSL::X509::Certificate.new(file)
16
+ end.freeze
17
+
18
+ using FixedLengthSecureCompare
19
+
20
+ attr_reader :json
21
+
22
+ def initialize(jws_result)
23
+ @jws_result = jws_result
24
+ end
25
+
26
+ def verify(nonce, timestamp_leeway: 60, trusted_certificates: GOOGLE_ROOT_CERTIFICATES)
27
+ certificates = nil
28
+ response, _ = JWT.decode(@jws_result, nil, true, algorithms: ["ES256", "RS256"]) do |headers|
29
+ certificates = headers["x5c"].map do |encoded|
30
+ OpenSSL::X509::Certificate.new(Base64.strict_decode64(encoded))
31
+ end
32
+
33
+ X5cKeyFinder.from(certificates, trusted_certificates)
34
+ end
35
+
36
+ verify_certificate_subject(certificates.first)
37
+ verify_nonce(response, nonce)
38
+ verify_timestamp(response, timestamp_leeway)
39
+
40
+ @json = response
41
+ self
42
+ end
43
+
44
+ def cts_profile_match?
45
+ raise NotVerifiedError unless json
46
+
47
+ json["ctsProfileMatch"]
48
+ end
49
+
50
+ def basic_integrity?
51
+ raise NotVerifiedError unless json
52
+
53
+ json["basicIntegrity"]
54
+ end
55
+
56
+ def apk_package_name
57
+ raise NotVerifiedError unless json
58
+
59
+ json["apkPackageName"]
60
+ end
61
+
62
+ def apk_certificate_digest_sha256
63
+ raise NotVerifiedError unless json
64
+
65
+ json["apkCertificateDigestSha256"]
66
+ end
67
+
68
+ def error
69
+ raise NotVerifiedError unless json
70
+
71
+ json["error"]
72
+ end
73
+
74
+ def advice
75
+ raise NotVerifiedError unless json
76
+
77
+ json["advice"]&.split(",")
78
+ end
79
+
80
+ private
81
+
82
+ def verify_certificate_subject(certificate)
83
+ common_name = certificate.subject.to_a.assoc("CN")
84
+
85
+ unless common_name[1] == "attest.android.com"
86
+ raise CertificateSubjectError
87
+ end
88
+ end
89
+
90
+ def verify_nonce(response, nonce)
91
+ unless OpenSSL.fixed_length_secure_compare(nonce, response["nonce"])
92
+ raise NonceMismatchError
93
+ end
94
+ end
95
+
96
+ def verify_timestamp(response, leeway)
97
+ now = Time.now.to_f
98
+ response_time = response["timestampMs"] / 1000.0
99
+ unless response_time.between?(now - leeway, now + leeway)
100
+ raise TimestampError, "not within #{leeway}s leeway"
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SafetyNetAttestation
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require_relative "errors"
5
+
6
+ module SafetyNetAttestation
7
+ class SignatureError < Error; end
8
+
9
+ class X5cKeyFinder
10
+ def self.from(x5c_certificates, trusted_certificates)
11
+ store = OpenSSL::X509::Store.new
12
+ trusted_certificates.each { |certificate| store.add_cert(certificate) }
13
+
14
+ signing_certificate, *certificate_chain = x5c_certificates
15
+ store_context = OpenSSL::X509::StoreContext.new(store, signing_certificate, certificate_chain)
16
+
17
+ if store_context.verify
18
+ signing_certificate.public_key
19
+ else
20
+ error = "Certificate verification failed: #{store_context.error_string}."
21
+ error = "#{error} Certificate subject: #{store_context.current_cert.subject}." if store_context.current_cert
22
+
23
+ raise SignatureError, error
24
+ end
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safety_net_attestation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bart de Water
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.75.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.75.0
83
+ description:
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".rspec"
91
+ - ".rubocop.yml"
92
+ - ".travis.yml"
93
+ - CHANGELOG.md
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - README.md
98
+ - Rakefile
99
+ - android_safety_net.gemspec
100
+ - bin/console
101
+ - bin/rspec
102
+ - bin/rubocop
103
+ - bin/setup
104
+ - lib/safety_net_attestation.rb
105
+ - lib/safety_net_attestation/certificates/GSR2.crt
106
+ - lib/safety_net_attestation/certificates/GSR4.crt
107
+ - lib/safety_net_attestation/certificates/GTSR1.crt
108
+ - lib/safety_net_attestation/certificates/GTSR2.crt
109
+ - lib/safety_net_attestation/certificates/GTSR3.crt
110
+ - lib/safety_net_attestation/certificates/GTSR4.crt
111
+ - lib/safety_net_attestation/errors.rb
112
+ - lib/safety_net_attestation/fixed_length_secure_compare.rb
113
+ - lib/safety_net_attestation/statement.rb
114
+ - lib/safety_net_attestation/version.rb
115
+ - lib/safety_net_attestation/x5c_key_finder.rb
116
+ homepage: https://github.com/bdewater/safety_net_attestation
117
+ licenses:
118
+ - MIT
119
+ metadata:
120
+ homepage_uri: https://github.com/bdewater/safety_net_attestation
121
+ source_code_uri: https://github.com/bdewater/safety_net_attestation
122
+ changelog_uri: https://github.com/bdewater/safety_net_attestation/blob/master/CHANGELOG.md
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '2.3'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.1.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: SafetyNet attestation response verification
142
+ test_files: []