linzer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6b025e976012c79dc5153ebd4ca98f23df08a55bf0706c6d8427e0374b3cd7f5
4
+ data.tar.gz: b864415aad8cc6c9cbee217a64531378b92edbb62fb58b67a036e5997fec4a30
5
+ SHA512:
6
+ metadata.gz: 9d2eea3036d11c6b7f8969d0f30b71fa3715d62820acde93c7d512689aa7c91d281436f8b2ea91e47b906f62167a4611298d2fad64bcf241b39b18f4405ed7a9
7
+ data.tar.gz: 6f85ffe146b8a52647969575dc80011f5fefdf299bdf082cd4f3b4de0fc605f60f8beaaa1cc4cf04843cf2b3a5f7d6441c7f6b3989d50ed559cca398bbc3d7b6
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-02-18
4
+
5
+ - Initial release
6
+ - It barely passes unit tests to verify signatures with RSASSA-PSS Using SHA-512.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at miguel@miguel.cc. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Miguel Landaeta <miguel@miguel.cc>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Linzer
2
+
3
+ Linzer is a Ruby library for [HTTP Message Signatures (RFC 9421)](https://www.rfc-editor.org/rfc/rfc9421.html).
4
+
5
+ ## Install
6
+
7
+ Add the following line to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem "linzer"
11
+ ```
12
+
13
+ Or just `gem install linzer`.
14
+
15
+ ## Usage
16
+
17
+ TODO: Write usage instructions here
18
+
19
+ For now just take a look at the unit tests.
20
+
21
+ It's still early days, so only signatures RSASSA-PSS Using SHA-512 like ones described in the RFC are supported. I'll be expanding the library to cover more functionality specified in the RFC in subsequent releases.
22
+
23
+
24
+ ## Development
25
+
26
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
27
+
28
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nomadium/linzer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nomadium/linzer/blob/master/CODE_OF_CONDUCT.md).
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
37
+
38
+ ## Code of Conduct
39
+
40
+ Everyone interacting in the Linzer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nomadium/linzer/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linzer
4
+ class Message
5
+ def initialize(request_data)
6
+ @headers = Hash(request_data[:headers])
7
+ @http = Hash(request_data[:http])
8
+ end
9
+
10
+ def empty?
11
+ @headers.empty?
12
+ end
13
+
14
+ def header?(header)
15
+ @headers.key?(header)
16
+ end
17
+
18
+ def [](field_name)
19
+ return @headers[field_name] if !field_name.start_with?("@")
20
+
21
+ case field_name
22
+ when "@method" then @http["method"]
23
+ when "@authority" then @http["host"]
24
+ when "@path" then @http["path"]
25
+ else # XXX: improve this and add support for all fields in the RFC
26
+ raise Error.new "Unknown/unsupported field: \"#{field_name}\""
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linzer
4
+ class Verifier
5
+ def initialize(pubkeys = nil)
6
+ @pubkeys = Hash(pubkeys)
7
+ end
8
+
9
+ attr_reader :pubkeys
10
+
11
+ # XXX: probably all this validation can be moved to the Message class
12
+ def verify(message)
13
+ validate message
14
+
15
+ signature_input = parse_field(message, "signature-input")
16
+ signature = parse_field(message, "signature")
17
+
18
+ # XXX: this is a self-imposed limitation, fix later
19
+ reject_multiple(signature)
20
+
21
+ choosen_signature = signature.keys[0]
22
+ if !signature_input.key?(choosen_signature)
23
+ raise Error.new "Signature \"#{choosen_signature}\" is not found."
24
+ end
25
+
26
+ covered_components = signature_input[choosen_signature].to_a
27
+ signature_parameters = signature_input[choosen_signature].parameters
28
+
29
+ signature_value = signature[choosen_signature].value
30
+ # XXX to-do: have a mechanism to inspect components and parameters
31
+
32
+ check_key_presence signature_parameters
33
+ check_components message, covered_components
34
+
35
+ signature_base = build_signature_base(message, signature_input)
36
+
37
+ # XXX to-do: get rid of this hard-coded SHA512 values, support more algs
38
+ key = pubkeys[signature_parameters["keyid"]]
39
+ if !key.verify_pss("SHA512", signature_value, signature_base, salt_length: :auto, mgf1_hash: "SHA512")
40
+ raise Error.new "Failed to verify message: Invalid signature."
41
+ end
42
+
43
+ true
44
+ end
45
+
46
+ private
47
+
48
+ def validate(message)
49
+ raise Error.new "Message to verify cannot be null" if message.nil?
50
+ raise Error.new "Message to verify cannot be empty" if message.empty?
51
+ raise Error.new "Message signature cannot be incomplete" unless message.header?("signature-input")
52
+ raise Error.new "Message has no signature to verify" unless message.header?("signature")
53
+ end
54
+
55
+ def parse_field(message, field_name)
56
+ Starry.parse_dictionary(message[field_name])
57
+ rescue Starry::ParseError => _
58
+ raise Error.new "Cannot parse \"#{field_name}\" field. Bailing out!"
59
+ end
60
+
61
+ def reject_multiple(hsh)
62
+ msg = "Messages with more than 1 signatures are not supported"
63
+ raise Error.new msg if hsh.keys.size > 1
64
+ end
65
+
66
+ def check_key_presence(parameters)
67
+ msg = "Cannot verify signature. Key not found"
68
+
69
+ key_id = parameters["keyid"]
70
+ raise Error.new msg if key_id.nil?
71
+ msg += ": \"#{key_id}\"" if !key_id.empty?
72
+
73
+ raise Error.new msg unless pubkeys.key?(key_id)
74
+ end
75
+
76
+ def check_components(message, components)
77
+ msg = "Cannot verify signature. Missing component in message: "
78
+ components
79
+ .map(&:value)
80
+ .reject { |component| message[component] }
81
+ .shift
82
+ .tap do |component|
83
+ if component
84
+ msg += "\"#{component}\""
85
+ raise Error.new msg
86
+ end
87
+ end
88
+ end
89
+
90
+ def build_signature_base(message, signature_input)
91
+ signature_base = +""
92
+ signature_params = ""
93
+ signature_input.each do |k, l|
94
+ signature_params = l.to_s
95
+ l.value.each { |c| signature_base << "\"#{c.value}\": #{message[c.value]}\n" }
96
+ end
97
+ signature_base << "\"@signature-params\": #{signature_params}"
98
+ signature_base
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linzer
4
+ VERSION = "0.1.0"
5
+ end
data/lib/linzer.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "starry"
4
+ require "openssl"
5
+
6
+ require_relative "linzer/version"
7
+ require_relative "linzer/message"
8
+ require_relative "linzer/verifier"
9
+
10
+ module Linzer
11
+ class Error < StandardError; end
12
+
13
+ class << self
14
+ def verify(pubkeys, message)
15
+ Linzer::Verifier.new(pubkeys)
16
+ .verify(message)
17
+ end
18
+ end
19
+ end
data/linzer.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/linzer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "linzer"
7
+ spec.version = Linzer::VERSION
8
+ spec.authors = ["Miguel Landaeta"]
9
+ spec.email = %w[miguel@miguel.cc]
10
+
11
+ spec.summary = "An implementation of HTTP Messages Signatures (RFC9421)"
12
+ spec.homepage = "https://github.com/nomadium/linzer"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = spec.homepage + "/blob/master/CHANGELOG.md"
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(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (File.expand_path(f) == __FILE__) ||
25
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_runtime_dependency "ed25519", "~> 1.3", ">= 1.3.0"
33
+ spec.add_runtime_dependency "starry", "~> 0.1"
34
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Miguel Landaeta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ed25519
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.3.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: starry
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.1'
47
+ description:
48
+ email:
49
+ - miguel@miguel.cc
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - ".rspec"
55
+ - ".standard.yml"
56
+ - CHANGELOG.md
57
+ - CODE_OF_CONDUCT.md
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - lib/linzer.rb
62
+ - lib/linzer/message.rb
63
+ - lib/linzer/verifier.rb
64
+ - lib/linzer/version.rb
65
+ - linzer.gemspec
66
+ homepage: https://github.com/nomadium/linzer
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/nomadium/linzer
71
+ source_code_uri: https://github.com/nomadium/linzer
72
+ changelog_uri: https://github.com/nomadium/linzer/blob/master/CHANGELOG.md
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.6.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.4.19
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: An implementation of HTTP Messages Signatures (RFC9421)
92
+ test_files: []