ssbx 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: 0cda81615e31774e4279c4454b0a5b1c9d77bc331568acc612471f7f56dbc586
4
+ data.tar.gz: e5d8453d32656c2018cbc83208701952b9a96b9e3d058689dd2948d295a0f426
5
+ SHA512:
6
+ metadata.gz: 496d3f33a15db8e5ad3080a2651ffb1b25f40d631add12a58ae4365196dffb9936bb71d03ea3e462757b35a17a9e4e11031b4cfb7f15914981879fd78d0e77e6
7
+ data.tar.gz: bf54c85290eeb62e4ab9e4bb3e230a783fbe168ac76756dbbdfdc2f61a8b6277c67cab970ffb67c19ef66875c63987cea54c7225ccba5ea3894ec4abecbb5475
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,4 @@
1
+ image: ruby:2.2
2
+ job:
3
+ script:
4
+ - rake test
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:gitlab) {|repo_name| "https://gitlab.com/basking2/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ssbx.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ssbx (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 2.0)
16
+ rake (~> 10.0)
17
+ ssbx!
18
+
19
+ BUNDLED WITH
20
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Sam Baskinger
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,39 @@
1
+ # Ssbx
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'ssbx'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ssbx
18
+
19
+ ## Usage
20
+
21
+ Edit `~/.ssbx.yaml` to contain...
22
+
23
+ ```yaml
24
+ user: You
25
+ pass: A secret password
26
+ ```
27
+
28
+ Then use `ssbx` to view or edit that file.
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ 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).
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require 'rdoc/task'
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ RDoc::Task.new do |rdoc|
11
+ rdoc.main = "README.rdoc"
12
+ rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb")
13
+ end
14
+
15
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ssbx"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/exe/ssbx ADDED
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "ssbx"
4
+ require 'optparse'
5
+ require 'yaml'
6
+ require 'pp'
7
+
8
+ CONFIG = {
9
+ 'file' => 'ssbx.enc',
10
+ 'user' => 'anonymous',
11
+ 'pass' => 'password',
12
+ }
13
+
14
+ default_config = File.join(ENV['HOME'], '.ssbx.yaml')
15
+ if File.file? default_config
16
+ c = YAML::load(File.read(default_config))
17
+ CONFIG.merge!(c)
18
+ end
19
+
20
+ CONFIG['delete_users'] = []
21
+ CONFIG['add_users'] = []
22
+
23
+ OptionParser.new do |opts|
24
+ opts.on('-v', '--verbose', "Verbose.") do
25
+ CONFIG['verbose'] = true
26
+ end
27
+
28
+ opts.on('-l', '--list', "List users.") do
29
+ CONFIG['list'] = true
30
+ end
31
+
32
+ opts.on('-d', '--delete=user', String, "Delete user. May be used multiple times.") do |u|
33
+ CONFIG['delete_users'] << u
34
+ end
35
+
36
+ opts.on('-a', '--add=user:pass', String, "Add user. May be used multiple times.") do |u|
37
+ CONFIG['add_users'] << u.split(':')
38
+ end
39
+
40
+ opts.on('-f', '--file=file', String, "File to operate on.") do |f|
41
+ CONFIG['file'] = f
42
+ end
43
+
44
+ opts.on('-u', '--user=user', String, 'The user to edit this file.') do |u|
45
+ CONFIG['user'] = u
46
+ end
47
+
48
+ opts.on('--set=file', String, "Set the contents of this encrypted file to the given file.") do |f|
49
+ CONFIG['set'] = f
50
+ end
51
+
52
+ opts.on('-o', '--out=file', String, "Read and print the file contents to here. - is stdout.") do |f|
53
+ CONFIG['out'] = f
54
+ end
55
+ end.parse! ARGV
56
+
57
+ if CONFIG['verbose']
58
+ require 'pp'
59
+ pp CONFIG
60
+ end
61
+
62
+ CONFIG['delete_users'].each do |u|
63
+ f = Ssbx::File.new
64
+ File.open(CONFIG['file'], 'r') { |io| f.read(io) }
65
+ bx = Ssbx::Box.new(f)
66
+ bx.remove_user(u)
67
+ File.open(CONFIG['file'], 'wb') { |io| f.write(io) }
68
+ end
69
+
70
+ if CONFIG['add_users']
71
+ f = Ssbx::File.new
72
+ bx = Ssbx::Box.new(f)
73
+ data = File.open(CONFIG['file'], 'rb') do |io|
74
+ bx.read(io, CONFIG['user'], CONFIG['pass'])
75
+ end
76
+
77
+ CONFIG['add_users'].each do |u|
78
+ u, p = u[0], u[1] || u[0]
79
+
80
+ File.open(CONFIG['file'], 'wb') do |io|
81
+ bx.write(io, u, p, data)
82
+ end
83
+ end
84
+ end
85
+
86
+ if CONFIG['set']
87
+ f = Ssbx::File.new
88
+ bx = Ssbx::Box.new(f)
89
+ data = File.read(CONFIG['set'])
90
+ File.open(CONFIG['file'], 'wb') do |io|
91
+ bx.write(io, CONFIG['user'], CONFIG['pass'], data)
92
+ end
93
+ end
94
+
95
+ if CONFIG['out']
96
+ f = Ssbx::File.new
97
+ bx = Ssbx::Box.new(f)
98
+ data = File.open(CONFIG['file'], 'rb') do |io|
99
+ bx.read(io, CONFIG['user'], CONFIG['pass'])
100
+ end
101
+
102
+ if CONFIG['out'] == '-'
103
+ STDOUT.write(data)
104
+ else
105
+ File.open(CONFIG['out'], 'wb') do |io|
106
+ io.write(data)
107
+ end
108
+ end
109
+ end
110
+
111
+ if CONFIG['list']
112
+ f = Ssbx::File.new
113
+ bx = Ssbx::Box.new(f)
114
+ File.open(CONFIG['file'], 'rb') do |io|
115
+ f.read(io)
116
+ end
117
+
118
+ bx.list.each do |u|
119
+ puts u
120
+ end
121
+ end
data/lib/ssbx/box.rb ADDED
@@ -0,0 +1,112 @@
1
+ require 'ssbx/file'
2
+
3
+ require 'openssl'
4
+
5
+ module Ssbx
6
+
7
+ # This is the encryption logic.
8
+ # This uses File to read and write data, but the actual encryption logic is in here.
9
+ class Box
10
+
11
+ # Create a new box.
12
+ def initialize(file)
13
+ if file.is_a? IO
14
+ @file = File.new
15
+ @file.read(file)
16
+ elsif file.is_a? Ssbx::File
17
+ @file = file
18
+ else
19
+ raise Exception.new("Unsupported input type #{file.class}. Try Ssbx::File or IO.")
20
+ end
21
+ end
22
+
23
+ # Write the data given the userid and password.
24
+ # If the user is not in the file, they are added.
25
+ def write(outstream, userid, password, data)
26
+ userrecord = @file.keys.select { |k| k[0] == userid }
27
+
28
+ # Find or get our user record.
29
+ if userrecord.length == 0
30
+ privkey = OpenSSL::PKey::RSA.new(2048)
31
+
32
+ userrecord = [ userid ]
33
+ userrecord << privkey.to_pem(OpenSSL::Cipher::AES.new('128-CBC'), password)
34
+ userrecord << privkey.public_key.to_pem
35
+ userrecord << '' # Private Key
36
+ userrecord << '' # Initialization Vector.
37
+
38
+ @file.keys << userrecord
39
+ else
40
+ userrecord = userrecord[0]
41
+
42
+ privkey = OpenSSL::PKey::RSA.new(2048)
43
+
44
+ # Rotate user record entries.
45
+ userrecord[1] = privkey.to_pem(OpenSSL::Cipher::AES.new('128-CBC'), password)
46
+ userrecord[2] = privkey.public_key.to_pem
47
+ userrecord[3] = '' # Private Key
48
+ userrecord[4] = '' # Initialization Vector.
49
+ end
50
+
51
+ # Make semetric cipher.
52
+ cipher = OpenSSL::Cipher.new('AES-256-CBC')
53
+ cipher.encrypt
54
+
55
+ key = cipher.random_key
56
+ iv = cipher.random_iv
57
+
58
+ # Encrypt data with key.
59
+ ciphertext = cipher.update(data)
60
+ ciphertext += cipher.final
61
+ @file.data = ciphertext
62
+
63
+ # Now update all key records with the new key and iv.
64
+ @file.keys.each do |key_rec|
65
+ # k[0 id, 1 priv, 2 pub, 3 key, 4 iv]
66
+ k = OpenSSL::PKey::RSA.new(key_rec[2])
67
+ key_rec[3] = k.public_encrypt(key)
68
+ key_rec[4] = iv
69
+ end
70
+
71
+ @file.write(outstream)
72
+ end
73
+
74
+ # Decrypt the data user the given user ID and password.
75
+ def read(instream, userid, password)
76
+ @file.read(instream)
77
+ userrecord = @file.keys.select { |k| k[0] == userid }
78
+
79
+ # Find or get our user record.
80
+ if userrecord.length == 0
81
+ raise Exception.new("User #{userid} not found in the file.")
82
+ else
83
+ userrecord = userrecord[0]
84
+ end
85
+
86
+ privkey = OpenSSL::PKey::RSA.new(userrecord[1], password)
87
+
88
+ cipher = OpenSSL::Cipher.new('AES-256-CBC')
89
+ cipher.decrypt
90
+
91
+ cipher.key = privkey.private_decrypt(userrecord[3])
92
+ cipher.iv = userrecord[4]
93
+
94
+ data = cipher.update(@file.data)
95
+ data += cipher.final
96
+
97
+ data
98
+ end
99
+
100
+ # Remove a user from the memory representation of this file.
101
+ # You must do a valid write of the file to force credential rotation and remove the user
102
+ # from the file on disk.
103
+ def remove_user(user)
104
+ @file.keys.reject! { |k| k[0] == user }
105
+ end
106
+
107
+ def list
108
+ @file.keys.map { |r| r[0] }
109
+ end
110
+
111
+ end
112
+ end
data/lib/ssbx/file.rb ADDED
@@ -0,0 +1,58 @@
1
+ module Ssbx
2
+
3
+ # How data gets to disk and back.
4
+ class File
5
+ # Key tuples.
6
+ #
7
+ # This is an array of arrays.
8
+ # The inner-arrays are key-tuples.
9
+ # They consist of an id represented as a string,
10
+ # a public key, an ecrypted private key, and the
11
+ # decryption key for the data ecrypted with the public key.
12
+ #
13
+ # This means you need to decrypt the private key to decrypt the data key.
14
+ attr_accessor :keys
15
+
16
+ # The file data.
17
+ attr_accessor :data
18
+
19
+ def initialize
20
+ @keys = []
21
+ @data = ''
22
+ end
23
+
24
+ def write(out)
25
+ # How many key records.
26
+ out.write([@keys.length].pack('N'))
27
+ @keys.each do |key_rec|
28
+ # Write the key record columns.
29
+ out.write([key_rec.length].pack('N'))
30
+
31
+ key_rec.each do |col|
32
+ out.write([col.length].pack('N'))
33
+ out.write(col)
34
+ end
35
+ end
36
+
37
+ out.write([@data.length].pack('N'))
38
+ out.write(@data)
39
+ end
40
+
41
+ def read(istream)
42
+ @keys = []
43
+ # Read the key record length...
44
+ istream.read(4).unpack('N')[0].times do
45
+ @keys << []
46
+ # Read the key record length...
47
+ istream.read(4).unpack('N')[0].times do
48
+ col_len = istream.read(4).unpack('N')[0]
49
+ @keys[-1] << istream.read(col_len)
50
+ end
51
+ end
52
+
53
+ sz = istream.read(4).unpack('N')[0]
54
+ @data = istream.read(sz)
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Ssbx
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ssbx.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "ssbx/version"
2
+ require "ssbx/file"
3
+ require "ssbx/box"
4
+
5
+ module Ssbx
6
+ class Error < StandardError; end
7
+
8
+
9
+ end
data/ssbx.gemspec ADDED
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ssbx/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ssbx"
8
+ spec.version = Ssbx::VERSION
9
+ spec.authors = ["Sam"]
10
+ spec.email = ["sam.baskinger@gmail.com"]
11
+
12
+ spec.summary = %q{Shared encrypted file.}
13
+ spec.description = %q{Shared encrypted file.}
14
+ spec.homepage = "https://gitlab.com/basking2/ssbx"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://gitlab.com/basking2/ssbx"
24
+ # spec.metadata["changelog_uri"] = "https://gitlab.com/basking2/ssbx"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 2.0"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssbx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Shared encrypted file.
42
+ email:
43
+ - sam.baskinger@gmail.com
44
+ executables:
45
+ - ssbx
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".gitlab-ci.yml"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - exe/ssbx
59
+ - lib/ssbx.rb
60
+ - lib/ssbx/box.rb
61
+ - lib/ssbx/file.rb
62
+ - lib/ssbx/version.rb
63
+ - ssbx.gemspec
64
+ homepage: https://gitlab.com/basking2/ssbx
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ allowed_push_host: https://rubygems.org
69
+ homepage_uri: https://gitlab.com/basking2/ssbx
70
+ source_code_uri: https://gitlab.com/basking2/ssbx
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.7.6
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Shared encrypted file.
91
+ test_files: []