Skip to content

Commit

Permalink
Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
denblackstache committed Jul 17, 2021
0 parents commit 461ef65
Show file tree
Hide file tree
Showing 34 changed files with 1,055 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.env
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.3
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

source 'https://rubygems.org'

ruby '2.7.3'

gem 'os', '~> 1.1'

gem 'tty-prompt', '~> 0.23.1'

group :development, :test do
gem 'rubocop', '~> 1.18', require: false
end
51 changes: 51 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
os (1.1.1)
parallel (1.20.1)
parser (3.0.2.0)
ast (~> 2.4.1)
pastel (0.8.0)
tty-color (~> 0.5)
rainbow (3.0.0)
regexp_parser (2.1.1)
rexml (3.2.5)
rubocop (1.18.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.7.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.8.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
tty-color (0.6.0)
tty-cursor (0.7.1)
tty-prompt (0.23.1)
pastel (~> 0.8)
tty-reader (~> 0.8)
tty-reader (0.9.0)
tty-cursor (~> 0.7)
tty-screen (~> 0.8)
wisper (~> 2.0)
tty-screen (0.8.1)
unicode-display_width (2.0.0)
wisper (2.0.1)

PLATFORMS
ruby

DEPENDENCIES
os (~> 1.1)
rubocop (~> 1.18)
tty-prompt (~> 0.23.1)

RUBY VERSION
ruby 2.7.3p183

BUNDLED WITH
2.1.4
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Denis Semenenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# iCSP

An interactive CryptoPro CSP shell that tries to imitate its GUI counterpart on the Windows platform.
Built for macOS and Linux daily use by wrapping a set of CryptoPro CLI tools: cryptcp, certmgr, csptest, cpconfig, etc.

The project goal is only to simplify my daily life with CryptoPro CSP on *nix systems.
Not everything is polished. Feel free to submit a PR if you need to add extra commands/options or fix bugs.

See:
* https://www.cryptopro.ru/products/other/cryptcp
* https://www.cryptopro.ru/sites/default/files/products/cryptcp/cryptcp_5.0.x.pdf
* https://www.cryptopro.ru/sites/default/files/docs/certmgr.pdf

## Usage

Management commands:

* certificate — Manage certificates
* container — Manage containers
* hardware — Manage hardware
* license — Manage licenses

CSP commands:

* create_signature
* verify_signature
* create_hash
* verify_hash
* encrypt_file
* decrypt_file


## Examples

List all key containers

```bash
./bin/icsp container list
```

Create a signature for the file

```bash
./bin/icsp create_signature document.txt
```

## License

[MIT](./LICENSE)
55 changes: 55 additions & 0 deletions bin/icsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'logger'
require 'optionparser'
require 'ostruct'
require 'yaml'

require 'os'
require 'tty-prompt'

require_relative '../config/config'
require_relative '../config/environment'
require_relative '../src/csp_option_parser'
require_relative '../src/commands/base_command'
require_relative '../src/shell'
require_relative '../src/shell_result'

require_relative '../src/commands/certificate/delete'
require_relative '../src/commands/certificate/install'
require_relative '../src/commands/certificate/list'
require_relative '../src/commands/certificate/view'
require_relative '../src/commands/container/check'
require_relative '../src/commands/container/list'
require_relative '../src/commands/hardware/list'
require_relative '../src/commands/license/view'
require_relative '../src/commands/license/set'
require_relative '../src/commands/create_hash'
require_relative '../src/commands/verify_hash'
require_relative '../src/commands/encrypt_file'
require_relative '../src/commands/decrypt_file'
require_relative '../src/commands/create_signature'
require_relative '../src/commands/verify_signature'

# CryptoPro CSP
module CSP
def self.to_camel_case(string)
string.to_s.split('_').map(&:capitalize).join
end

def self.run
parsed = ::CSP::Services::OptionParserService.new.parse
command_parts = %w[::CSP Commands]
command_parts << to_camel_case(parsed.command) if parsed.command
command_parts << to_camel_case(parsed.subcommand) if parsed.subcommand
command = Object.const_get(command_parts.join('::'))
.new(config: config, options: parsed.options, arguments: parsed.arguments)
command.respond_to?(:print) ? command.print : command.execute
rescue StandardError => e
puts 'Application aborted with error:'
raise e
end

run
end
48 changes: 48 additions & 0 deletions config/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

class Config
def initialize
@bin = %i[certmgr cryptcp csptest].freeze
@sbin = [:cpconfig].freeze
end

def csp_path
@csp_path ||= ENV.fetch('CSP_PATH', '/opt/cprocsp')
end

def build_path(binary_name)
File.join(csp_path, category_path(binary_name), arch_path, binary_name.to_s)
end

def category_path(binary_name)
@sbin.include?(binary_name) ? 'sbin' : 'bin'
end

def arch_path
return '' if OS.mac?

OS.bits == 64 ? 'amd64' : 'ia32'
end

def log_level
@log_level ||= ENV.fetch('LOG_LEVEL', 'warn').upcase
end

private

def method_missing(symbol, *args)
if @bin.include?(symbol) || @sbin.include?(symbol)
build_path(symbol)
else
super
end
end

def respond_to_missing?(symbol, *args)
if @bin.include?(symbol) || @sbin.include?(symbol)
true
else
super
end
end
end
11 changes: 11 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module CSP
def self.config
@config ||= Config.new
end

def self.logger
@logger ||= Logger.new($stdout, level: Object.const_get("Logger::Severity::#{config.log_level}"))
end
end
19 changes: 19 additions & 0 deletions src/commands/base_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module CSP
module Commands
class BaseCommand
def initialize(config:, options:, arguments:)
@config = config
@options = options
@arguments = arguments
@prompt = TTY::Prompt.new
::CSP.logger.debug('Command initialized with:')
::CSP.logger.debug("arguments: #{@arguments.inspect}")
::CSP.logger.debug("options: #{@options.inspect}")
end

attr_reader :config, :options, :arguments, :prompt
end
end
end
25 changes: 25 additions & 0 deletions src/commands/certificate/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module CSP
module Commands
module Certificate
class Delete < ::CSP::Commands::BaseCommand
def certmgr
@certmgr ||= @config.certmgr
end

def execute
store = prompt.select('Select store:', available_stores)
result = ::CSP::Shell.new("#{certmgr} -delete -store #{store}", fork: false).execute
exit(result.exit_code) unless result.ok

puts result
end

def available_stores
%w[uMy root ca]
end
end
end
end
end
48 changes: 48 additions & 0 deletions src/commands/certificate/install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module CSP
module Commands
module Certificate
class Install < ::CSP::Commands::BaseCommand
def certmgr
@certmgr ||= @config.certmgr
end

def execute
unless options[:file]
install_from_container
return
end

install_from_file
end

def install_from_container
result = ::CSP::Shell.new("#{certmgr} -inst -cont '#{selected_container}'", convert_to_utf8: false).execute
exit(result.exit_code) unless result.ok

puts result
end

def install_from_file
store = prompt.select('Select store:', available_stores)
not_crl = prompt.no?('Is it CRL?')
result = ::CSP::Shell.new(
"#{certmgr} -inst -file '#{options[:file]}' -store #{store} #{not_crl ? '' : '-crl'}", convert_to_utf8: false
).execute
exit(result.exit_code) unless result.ok

puts result
end

def selected_container
::CSP::Commands::Container::List.new(config: config, options: options, arguments: arguments).select
end

def available_stores
%w[uMy root ca]
end
end
end
end
end
Loading

0 comments on commit 461ef65

Please sign in to comment.