openssl-signature_algorithm-1.0.0/0000755000004100000410000000000013751212122017264 5ustar www-datawww-dataopenssl-signature_algorithm-1.0.0/Gemfile.lock0000644000004100000410000000233313751212122021507 0ustar www-datawww-dataPATH remote: . specs: openssl-signature_algorithm (1.0.0) GEM remote: https://rubygems.org/ specs: appraisal (2.2.0) bundler rake thor (>= 0.14.0) ast (2.4.0) byebug (11.1.1) diff-lcs (1.3) jaro_winkler (1.5.4) parallel (1.19.1) parser (2.7.0.5) ast (~> 2.4.0) rainbow (3.0.0) rake (13.0.1) rexml (3.2.4) rspec (3.9.0) rspec-core (~> 3.9.0) rspec-expectations (~> 3.9.0) rspec-mocks (~> 3.9.0) rspec-core (3.9.1) rspec-support (~> 3.9.1) rspec-expectations (3.9.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-mocks (3.9.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-support (3.9.2) rubocop (0.80.1) jaro_winkler (~> 1.5.1) parallel (~> 1.10) parser (>= 2.7.0.1) rainbow (>= 2.2.2, < 4.0) rexml ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 1.7) ruby-progressbar (1.10.1) thor (1.0.1) unicode-display_width (1.6.1) PLATFORMS ruby DEPENDENCIES appraisal (~> 2.2) byebug (~> 11.0) openssl-signature_algorithm! rake (~> 13.0) rspec (~> 3.0) rubocop (~> 0.80.1) BUNDLED WITH 2.1.4 openssl-signature_algorithm-1.0.0/.travis.yml0000644000004100000410000000072313751212122021377 0ustar www-datawww-data--- dist: bionic language: ruby cache: bundler rvm: - ruby-head - 2.7.1 - 2.6.6 - 2.5.8 - 2.4.10 gemfile: - gemfiles/openssl_head.gemfile - gemfiles/openssl_2_2.gemfile - gemfiles/openssl_2_1.gemfile - gemfiles/openssl_2_0.gemfile - gemfiles/openssl_default.gemfile matrix: fast_finish: true allow_failures: - rvm: ruby-head - gemfile: gemfiles/openssl_head.gemfile before_install: - gem install bundler -v 2.1.4 - rm Gemfile.lock openssl-signature_algorithm-1.0.0/.rspec0000644000004100000410000000010413751212122020374 0ustar www-datawww-data--format documentation --color --require spec_helper --order random openssl-signature_algorithm-1.0.0/README.md0000644000004100000410000000636213751212122020552 0ustar www-datawww-data# OpenSSL::SignatureAlgorithm > ECDSA, RSA-PSS and RSA-PKCS#1 signature algorithms for ruby Sign and verify using signature algorithm wrappers, instead of key objects. Provides `OpenSSL::SignatureAlgorithm::ECDSA`, `OpenSSL::SignatureAlgorithm::RSAPSS` and `OpenSSL::SignatureAlgorithm::RSAPKCS1` ruby object wrappers on top of `OpenSSL::PKey::EC` and `OpenSSL::PKey::RSA`, so that you can reason in terms of the algorithms and do less when signing or verifying signatures. [![Gem](https://img.shields.io/gem/v/openssl-signature_algorithm.svg?style=flat-square&color=informational)](https://rubygems.org/gems/openssl-signature_algorithm) [![Travis](https://img.shields.io/travis/cedarcode/openssl-signature_algorithm/master.svg?style=flat-square)](https://travis-ci.org/cedarcode/openssl-signature_algorithm) ## Installation Add this line to your application's Gemfile: ```ruby gem 'openssl-signature_algorithm' ``` And then execute: $ bundle install Or install it yourself as: $ gem install openssl-signature_algorithm ## Usage ### ECDSA ```ruby to_be_signed = "to-be-signed" # Signer algorithm = OpenSSL::SignatureAlgorithm::ECDSA.new signing_key = algorithm.generate_signing_key signature = algorithm.sign(to_be_signed) # Signer sends verify key to Verifier verify_key_string = signing_key.verify_key.serialize # Verifier verify_key = OpenSSL::SignatureAlgorithm::ECDSA::VerifyKey.deserialize(verify_key_string) algorithm = OpenSSL::SignatureAlgorithm::ECDSA.new algorithm.verify_key = verify_key algorithm.verify(signature, to_be_signed) ``` ### RSA-PSS ```ruby to_be_signed = "to-be-signed" # Signer algorithm = OpenSSL::SignatureAlgorithm::RSAPSS.new signing_key = algorithm.generate_signing_key signature = algorithm.sign(to_be_signed) # Signer sends verify key to Verifier verify_key_string = signing_key.verify_key.serialize # Verifier verify_key = OpenSSL::SignatureAlgorithm::RSAPSS::VerifyKey.deserialize(verify_key_string) algorithm = OpenSSL::SignatureAlgorithm::RSAPSS.new algorithm.verify_key = verify_key algorithm.verify(signature, to_be_signed) ``` ### RSA-PKCS1_v1.5 ```ruby to_be_signed = "to-be-signed" # Signer algorithm = OpenSSL::SignatureAlgorithm::RSAPKCS1.new signing_key = algorithm.generate_signing_key signature = algorithm.sign(to_be_signed) # Signer sends verify key to Verifier verify_key_string = signing_key.verify_key.serialize # Verifier verify_key = OpenSSL::SignatureAlgorithm::RSAPKCS1::VerifyKey.deserialize(verify_key_string) algorithm = OpenSSL::SignatureAlgorithm::RSAPKCS1.new algorithm.verify_key = verify_key algorithm.verify(signature, to_be_signed) ``` ## Development 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. 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). ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/cedarcode/openssl-signature_algorithm. openssl-signature_algorithm-1.0.0/bin/0000755000004100000410000000000013751212122020034 5ustar www-datawww-dataopenssl-signature_algorithm-1.0.0/bin/console0000755000004100000410000000061013751212122021421 0ustar www-datawww-data#!/usr/bin/env ruby # frozen_string_literal: true require "bundler/setup" require "openssl/signature_algorithm" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. # (If you use this, don't forget to add pry to your Gemfile!) # require "pry" # Pry.start require "irb" IRB.start(__FILE__) openssl-signature_algorithm-1.0.0/bin/setup0000755000004100000410000000020313751212122021115 0ustar www-datawww-data#!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' set -vx bundle install # Do any other automated setup that you need to do here openssl-signature_algorithm-1.0.0/gemfiles/0000755000004100000410000000000013751212122021057 5ustar www-datawww-dataopenssl-signature_algorithm-1.0.0/gemfiles/openssl_2_0.gemfile0000644000004100000410000000035713751212122024541 0ustar www-datawww-data# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal", "~> 2.2" gem "byebug", "~> 11.0" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rubocop", "~> 0.80.1" gem "openssl", "~> 2.0.0" gemspec path: "../" openssl-signature_algorithm-1.0.0/gemfiles/.bundle/0000755000004100000410000000000013751212122022406 5ustar www-datawww-dataopenssl-signature_algorithm-1.0.0/gemfiles/.bundle/config0000644000004100000410000000002613751212122023574 0ustar www-datawww-data--- BUNDLE_RETRY: "1" openssl-signature_algorithm-1.0.0/gemfiles/openssl_default.gemfile0000644000004100000410000000032513751212122025600 0ustar www-datawww-data# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal", "~> 2.2" gem "byebug", "~> 11.0" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rubocop", "~> 0.80.1" gemspec path: "../" openssl-signature_algorithm-1.0.0/gemfiles/openssl_2_1.gemfile0000644000004100000410000000035713751212122024542 0ustar www-datawww-data# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal", "~> 2.2" gem "byebug", "~> 11.0" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rubocop", "~> 0.80.1" gem "openssl", "~> 2.1.0" gemspec path: "../" openssl-signature_algorithm-1.0.0/gemfiles/openssl_2_2.gemfile0000644000004100000410000000035713751212122024543 0ustar www-datawww-data# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal", "~> 2.2" gem "byebug", "~> 11.0" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rubocop", "~> 0.80.1" gem "openssl", "~> 2.2.0" gemspec path: "../" openssl-signature_algorithm-1.0.0/gemfiles/openssl_head.gemfile0000644000004100000410000000041313751212122025053 0ustar www-datawww-data# This file was generated by Appraisal source "https://rubygems.org" gem "appraisal", "~> 2.2" gem "byebug", "~> 11.0" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rubocop", "~> 0.80.1" gem "openssl", git: "https://github.com/ruby/openssl" gemspec path: "../" openssl-signature_algorithm-1.0.0/CHANGELOG.md0000644000004100000410000000475113751212122021104 0ustar www-datawww-data# Changelog ## [v1.0.0] - 2020-07-08 ### Added - ECDSA with **secp256k1** curve support added: ```rb OpenSSL::SignatureAlgorithm::ECDSA.new(curve: "secp256k1") ``` - Algorithm **arguments** are now **optional**. The following works: ```rb OpenSSL::SignatureAlgorithm::ECDSA.new # defaults to ECDSA with SHA-256 and NIST P256 curve # Same as OpenSSL::SignatureAlgorithm::ECDSA.new(hash_function: "SHA256", curve: "prime256v1") OpenSSL::SignatureAlgorithm::RSAPSS.new # defaults to SHA-256 # Same as OpenSSL::SignatureAlgorithm::RSAPSS.new(hash_function: "SHA256") OpenSSL::SignatureAlgorithm::RSAPKCS1.new # defaults to SHA-256 # Same as OpenSSL::SignatureAlgorithm::RSAPSS.new(hash_function: "SHA256") ``` ### Changed - Algorithm **instantiation** changed. The positional argument `digest_length` is replaced with keyword arguments, `hash_function:` for RSA algorithms and `hash_function:` and `curve:` for ECDSA. None are required arguments, you get sane defaults if you don't provide any arguments. Code migration examples: ```rb # Change this # OpenSSL::SignatureAlgorithm::ECDSA.new("256") # to OpenSSL::SignatureAlgorithm::ECDSA.new(hash_function: "SHA256") ``` ```rb # Change this # OpenSSL::SignatureAlgorithm::RSAPSS.new("384") # to OpenSSL::SignatureAlgorithm::RSAPSS.new(hash_function: "SHA384") ``` ## [v0.4.0] - 2020-01-31 ### Added - `VerifyKey` serialization and deserialization for easy transmission over the network ## [v0.3.0] - 2020-01-30 ### Added - Support ruby v2.4 (without RSA-PSS) ## [v0.2.0] - 2020-01-29 ### Added - `OpenSSL::SignatureAlgorithm::ECDSA#verify` now supports raw (non DER) signatures ## [v0.1.1] - 2020-01-29 ### Fixed - Fixed error in `OpenSSL::SignatureAlgorithm::RSAPSS#sign` ## [v0.1.0] - 2020-01-29 ### Added - `OpenSSL::SignatureAlgorithm::ECDSA` - `OpenSSL::SignatureAlgorithm::RSAPSS` - `OpenSSL::SignatureAlgorithm::RSAPKCS1` [v1.0.0]: https://github.com/cedarcode/openssl-signature_algorithm/compare/v0.4.0...v1.0.0/ [v0.4.0]: https://github.com/cedarcode/openssl-signature_algorithm/compare/v0.3.0...v0.4.0/ [v0.3.0]: https://github.com/cedarcode/openssl-signature_algorithm/compare/v0.2.0...v0.3.0/ [v0.2.0]: https://github.com/cedarcode/openssl-signature_algorithm/compare/v0.1.1...v0.2.0/ [v0.1.1]: https://github.com/cedarcode/openssl-signature_algorithm/compare/v0.1.0...v0.1.1/ [v0.1.0]: https://github.com/cedarcode/openssl-signature_algorithm/compare/41887c277dc7fa0c884ccf8924cf990ff76784d9...v0.1.0/ openssl-signature_algorithm-1.0.0/.rubocop.yml0000644000004100000410000000466413751212122021550 0ustar www-datawww-dataAllCops: TargetRubyVersion: 2.4 DisabledByDefault: true Exclude: - "gemfiles/**/*" Bundler: Enabled: true Gemspec: Enabled: true Layout: Enabled: true Layout/LineLength: Max: 120 Lint: Enabled: true Naming: Enabled: true Security: Enabled: true Style/BlockComments: Enabled: true Style/CaseEquality: Enabled: true Style/ClassAndModuleChildren: Enabled: true Style/ClassMethods: Enabled: true Style/ClassVars: Enabled: true Style/CommentAnnotation: Enabled: true Style/ConditionalAssignment: Enabled: true Style/DefWithParentheses: Enabled: true Style/Dir: Enabled: true Style/EachForSimpleLoop: Enabled: true Style/EachWithObject: Enabled: true Style/EmptyBlockParameter: Enabled: true Style/EmptyCaseCondition: Enabled: true Style/EmptyElse: Enabled: true Style/EmptyLambdaParameter: Enabled: true Style/EmptyLiteral: Enabled: true Style/EvenOdd: Enabled: true Style/ExpandPathArguments: Enabled: true Style/For: Enabled: true Style/FrozenStringLiteralComment: Enabled: true Style/GlobalVars: Enabled: true Style/HashSyntax: Enabled: true Style/IdenticalConditionalBranches: Enabled: true Style/IfInsideElse: Enabled: true Style/InverseMethods: Enabled: true Style/MethodCallWithoutArgsParentheses: Enabled: true Style/MethodDefParentheses: Enabled: true Style/MultilineMemoization: Enabled: true Style/MutableConstant: Enabled: true Style/NestedParenthesizedCalls: Enabled: true Style/OptionalArguments: Enabled: true Style/ParenthesesAroundCondition: Enabled: true Style/RedundantBegin: Enabled: true Style/RedundantConditional: Enabled: true Style/RedundantException: Enabled: true Style/RedundantFreeze: Enabled: true Style/RedundantInterpolation: Enabled: true Style/RedundantParentheses: Enabled: true Style/RedundantPercentQ: Enabled: true Style/RedundantReturn: Enabled: true Style/RedundantSelf: Enabled: true Style/Semicolon: Enabled: true Style/SingleLineMethods: Enabled: true Style/SpecialGlobalVars: Enabled: true Style/SymbolLiteral: Enabled: true Style/TrailingBodyOnClass: Enabled: true Style/TrailingBodyOnMethodDefinition: Enabled: true Style/TrailingBodyOnModule: Enabled: true Style/TrailingMethodEndStatement: Enabled: true Style/TrivialAccessors: Enabled: true Style/UnpackFirst: Enabled: true Style/YodaCondition: Enabled: true Style/ZeroLengthPredicate: Enabled: true openssl-signature_algorithm-1.0.0/Appraisals0000644000004100000410000000051213751212122021304 0ustar www-datawww-data# frozen_string_literal: true appraise "openssl_head" do gem "openssl", git: "https://github.com/ruby/openssl" end appraise "openssl_2_2" do gem "openssl", "~> 2.2.0" end appraise "openssl_2_1" do gem "openssl", "~> 2.1.0" end appraise "openssl_2_0" do gem "openssl", "~> 2.0.0" end appraise "openssl_default" do end openssl-signature_algorithm-1.0.0/.gitignore0000644000004100000410000000023313751212122021252 0ustar www-datawww-data/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ # rspec failure tracking .rspec_status .byebug_history /gemfiles/*.gemfile.lock openssl-signature_algorithm-1.0.0/openssl-signature_algorithm.gemspec0000644000004100000410000000232113751212122026357 0ustar www-datawww-data# frozen_string_literal: true require_relative 'lib/openssl/signature_algorithm/version' Gem::Specification.new do |spec| spec.name = "openssl-signature_algorithm" spec.version = OpenSSL::SignatureAlgorithm::VERSION spec.authors = ["Gonzalo Rodriguez"] spec.email = ["gonzalo@cedarcode.com"] spec.license = "Apache-2.0" spec.summary = "ECDSA, RSA-PSS and RSA-PKCS#1 algorithms for ruby" spec.description = spec.summary spec.homepage = "https://github.com/cedarcode/openssl-signature_algorithm" spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0") spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md" # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path(__dir__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] end openssl-signature_algorithm-1.0.0/LICENSE0000644000004100000410000002613513751212122020300 0ustar www-datawww-data Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. openssl-signature_algorithm-1.0.0/Rakefile0000644000004100000410000000031713751212122020732 0ustar www-datawww-data# frozen_string_literal: true require "bundler/gem_tasks" require "rspec/core/rake_task" require "rubocop/rake_task" RSpec::Core::RakeTask.new(:spec) RuboCop::RakeTask.new task default: [:rubocop, :spec] openssl-signature_algorithm-1.0.0/lib/0000755000004100000410000000000013751212122020032 5ustar www-datawww-dataopenssl-signature_algorithm-1.0.0/lib/openssl/0000755000004100000410000000000013751212122021515 5ustar www-datawww-dataopenssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/0000755000004100000410000000000013751212122025564 5ustar www-datawww-dataopenssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/version.rb0000644000004100000410000000015213751212122027574 0ustar www-datawww-data# frozen_string_literal: true module OpenSSL module SignatureAlgorithm VERSION = "1.0.0" end end openssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/ecdsa.rb0000644000004100000410000000561113751212122027173 0ustar www-datawww-data# frozen_string_literal: true require "openssl" require "openssl/signature_algorithm/base" module OpenSSL module SignatureAlgorithm class ECDSA < Base BYTE_LENGTH = 8 class SigningKey < OpenSSL::PKey::EC def initialize(*args) super(*args).generate_key end def verify_key VerifyKey.new(group, public_key.to_bn) end end class VerifyKey < OpenSSL::PKey::EC::Point def self.deserialize(pem_string) new(OpenSSL::PKey::EC.new(pem_string).public_key) end def serialize ec_key.to_pem end def ec_key @ec_key ||= begin ec_key = OpenSSL::PKey::EC.new(group) ec_key.public_key = self ec_key end end def verify(*args) ec_key.verify(*args) end end ACCEPTED_PARAMETERS = [ { curve: "prime256v1", hash_function: "SHA256" }, { curve: "secp384r1", hash_function: "SHA384" }, { curve: "secp521r1", hash_function: "SHA512" }, { curve: "secp256k1", hash_function: "SHA256" } ].freeze attr_reader :curve, :hash_function def initialize(curve: nil, hash_function: nil) @curve, @hash_function = pick_parameters(curve, hash_function) end def generate_signing_key @signing_key = SigningKey.new(curve) end def compatible_verify_key?(key) super && key.respond_to?(:group) && key.group.curve_name == curve end private # Borrowed from jwt rubygem. # https://github.com/jwt/ruby-jwt/blob/7a6a3f1dbaff806993156d1dff9c217bb2523ff8/lib/jwt/security_utils.rb#L34-L39 # # Hopefully this will be provided by openssl rubygem in the future. def formatted_signature(signature) n = (verify_key_length.to_f / BYTE_LENGTH).ceil if signature.size == n * 2 r = signature[0..(n - 1)] s = signature[n..-1] OpenSSL::ASN1::Sequence.new([r, s].map { |int| OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(int, 2)) }).to_der else signature end end def verify_key_length verify_key.group.degree end def pick_parameters(curve, hash_function) parameters = ACCEPTED_PARAMETERS.detect do |params| if curve if hash_function params[:curve] == curve && params[:hash_function] == hash_function else params[:curve] == curve end elsif hash_function params[:hash_function] == hash_function else true end end if parameters [parameters[:curve], parameters[:hash_function]] else raise(OpenSSL::SignatureAlgorithm::UnsupportedParameterError, "Unsupported algorithm parameters") end end end end end openssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/rsapss.rb0000644000004100000410000000131013751212122027417 0ustar www-datawww-data# frozen_string_literal: true require "openssl" require "openssl/signature_algorithm/rsa" module OpenSSL module SignatureAlgorithm class RSAPSS < RSA def sign(data) signing_key.sign_pss(hash_function, data, salt_length: :max, mgf1_hash: mgf1_hash_function) end def verify(signature, verification_data) verify_key.verify_pss( hash_function, signature, verification_data, salt_length: :auto, mgf1_hash: mgf1_hash_function ) || raise(OpenSSL::SignatureAlgorithm::SignatureVerificationError, "Signature verification failed") end def mgf1_hash_function hash_function end end end end openssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/base.rb0000644000004100000410000000224013751212122027021 0ustar www-datawww-data# frozen_string_literal: true require "openssl" require "openssl/signature_algorithm/error" module OpenSSL module SignatureAlgorithm class SignatureVerificationError < Error; end class UnsupportedParameterError < Error; end class VerifyKeyError < Error; end class Base attr_reader :signing_key, :verify_key def verify_key=(key) if compatible_verify_key?(key) @verify_key = key else raise(OpenSSL::SignatureAlgorithm::VerifyKeyError, "Incompatible verify key for algorithm") end end def compatible_verify_key?(verify_key) verify_key.respond_to?(:verify) end def sign(data) signing_key.sign(hash_function, data) end def verify(signature, verification_data) formatted_signature = if respond_to?(:formatted_signature, true) formatted_signature(signature) else signature end verify_key.verify(hash_function, formatted_signature, verification_data) || raise(OpenSSL::SignatureAlgorithm::SignatureVerificationError, "Signature verification failed") end end end end openssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/error.rb0000644000004100000410000000017113751212122027241 0ustar www-datawww-data# frozen_string_literal: true module OpenSSL module SignatureAlgorithm class Error < StandardError; end end end openssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/rsa.rb0000644000004100000410000000207113751212122026676 0ustar www-datawww-data# frozen_string_literal: true require "openssl" require "openssl/signature_algorithm/base" module OpenSSL module SignatureAlgorithm class RSA < Base class SigningKey < OpenSSL::PKey::RSA def verify_key VerifyKey.new(public_key.to_pem) end end class VerifyKey < OpenSSL::PKey::RSA class << self alias_method :deserialize, :new end def serialize to_pem end end ACCEPTED_HASH_FUNCTIONS = ["SHA256", "SHA384", "SHA512"].freeze DEFAULT_KEY_SIZE = 2048 attr_reader :hash_function def initialize(hash_function: self.class::ACCEPTED_HASH_FUNCTIONS.first) if self.class::ACCEPTED_HASH_FUNCTIONS.include?(hash_function) @hash_function = hash_function else raise(OpenSSL::SignatureAlgorithm::UnsupportedParameterError, "Unsupported hash function '#{hash_function}'") end end def generate_signing_key(size: DEFAULT_KEY_SIZE) @signing_key = SigningKey.new(size) end end end end openssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm/rsapkcs1.rb0000644000004100000410000000026213751212122027640 0ustar www-datawww-data# frozen_string_literal: true require "openssl" require "openssl/signature_algorithm/rsa" module OpenSSL module SignatureAlgorithm class RSAPKCS1 < RSA end end end openssl-signature_algorithm-1.0.0/lib/openssl/signature_algorithm.rb0000644000004100000410000000046713751212122026120 0ustar www-datawww-data# frozen_string_literal: true require "openssl/signature_algorithm/ecdsa" require "openssl/signature_algorithm/error" require "openssl/signature_algorithm/rsapss" require "openssl/signature_algorithm/rsapkcs1" require "openssl/signature_algorithm/version" module OpenSSL module SignatureAlgorithm end end openssl-signature_algorithm-1.0.0/Gemfile0000644000004100000410000000041113751212122020553 0ustar www-datawww-data# frozen_string_literal: true source "https://rubygems.org" # Specify your gem's dependencies in openssl-signature_algorithm.gemspec gemspec gem "appraisal", "~> 2.2" gem "byebug", "~> 11.0" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rubocop", "~> 0.80.1"