cryptopro 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,5 +19,9 @@ h1. Использование
19
19
 
20
20
  Cryptopro::Csr.issue_test_certificate(csr)
21
21
 
22
+ Получение информации по сертификату:
23
+
24
+ Cryptopro::Certificate.info(body)
25
+
22
26
  Copyright (c) 2011 divineforest, evrone.com
23
27
  This project rocks and uses MIT-LICENSE.
@@ -1,5 +1,7 @@
1
+ require 'cryptopro/base'
1
2
  require 'cryptopro/csr'
2
3
  require 'cryptopro/signature'
4
+ require 'cryptopro/certificate'
3
5
 
4
6
  module Cryptopro
5
7
  end
@@ -0,0 +1,39 @@
1
+ require 'tmpdir'
2
+ require 'digest/md5'
3
+ require 'cocaine'
4
+
5
+ module Cryptopro
6
+ class Base
7
+ CERTIFICATE_FILE_NAME = "certificate.cer"
8
+ CERTIFICATE_LINE_LENGTH = 64
9
+
10
+ def self.create_temp_dir
11
+ uniq_name = Digest::MD5.hexdigest("#{rand(1_000_000)}#{Time.now}")
12
+ full_name = "#{Dir.tmpdir}/cryptcp/#{uniq_name}"
13
+ FileUtils.mkdir_p(full_name)
14
+ end
15
+
16
+ def self.create_temp_file(dir_name, file_name, content)
17
+ full_path = "#{dir_name}/#{file_name}"
18
+ File.open(full_path, "w") { |file| file.write(content) }
19
+ full_path
20
+ end
21
+
22
+ # Добавляет -----BEGIN CERTIFICATE----- / -----END CERTIFICATE-----, если их нет.
23
+ # Так же делит длинную строку Base64 на строки по 64 символа.
24
+ # Это требование cryptcp к файл с сертификатом.
25
+ def self.add_container_to_certificate(certificate)
26
+ return certificate if certificate.downcase.include?("begin")
27
+
28
+ parts = certificate.scan(/.{1,#{CERTIFICATE_LINE_LENGTH}}/)
29
+ certificate_with_container = "-----BEGIN CERTIFICATE-----\n#{parts.join("\n")}\n-----END CERTIFICATE-----"
30
+ end
31
+
32
+ def self.create_temp_certificate_file(content)
33
+ tmp_dir = create_temp_dir
34
+ certificate_with_container = add_container_to_certificate(content)
35
+ create_temp_file(tmp_dir, CERTIFICATE_FILE_NAME, certificate_with_container)
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,61 @@
1
+ module Cryptopro
2
+ class Certificate < Cryptopro::Base
3
+
4
+ def self.info(certificate_body)
5
+ certificate_file_path = create_temp_certificate_file(certificate_body)
6
+ cryptopro_answer = get_info(certificate_file_path)
7
+ convert_from_raw_to_hashes(cryptopro_answer)
8
+ end
9
+
10
+ private
11
+
12
+ def self.get_info(certificate_file_path)
13
+ Cocaine::CommandLine.path = ["/opt/cprocsp/bin/amd64", "/opt/cprocsp/bin/ia32"]
14
+ line = Cocaine::CommandLine.new("certmgr", "-list -f :certificate",
15
+ :certificate => certificate_file_path
16
+ )
17
+ begin
18
+ line.run
19
+ rescue Cocaine::ExitStatusError
20
+ false
21
+ rescue Cocaine::CommandNotFoundError => e
22
+ raise "Command certmgr was not found"
23
+ end
24
+ end
25
+
26
+ def self.raw_certificates(cryptopro_answer)
27
+ cleaned_answer = clean_answer(cryptopro_answer)
28
+ cleaned_answer.split("=============================================================================")
29
+ end
30
+
31
+ def self.clean_answer(cryptopro_answer)
32
+ cleaned = []
33
+ cleaned = cryptopro_answer.split("\n")[4..-4]
34
+ cleaned.join("\n")
35
+ end
36
+
37
+ def self.certificate_extract_info(raw_certificate)
38
+ info = {}
39
+ raw_certificate.split("\n").each do |certificate_line|
40
+ if certificate_line.include?(":")
41
+ name, value = certificate_line.split(":").map(&:strip)
42
+ name.gsub!(/\s/, "_")
43
+ name.downcase!
44
+ info[name.to_sym] = value
45
+ end
46
+ end
47
+ info
48
+ end
49
+
50
+ def self.convert_from_raw_to_hashes(cryptopro_answer)
51
+ container_certificates = []
52
+
53
+ raw_certificates(cryptopro_answer).each do |raw_certificate|
54
+ container_certificates << certificate_extract_info(raw_certificate)
55
+ end
56
+
57
+ container_certificates
58
+ end
59
+
60
+ end
61
+ end
@@ -1,15 +1,8 @@
1
- require 'tmpdir'
2
- require 'digest/md5'
3
- require 'cocaine'
4
-
5
1
  module Cryptopro
6
- class Signature
2
+ class Signature < Cryptopro::Base
7
3
  MESSAGE_FILE_NAME = "message.txt"
8
4
  # Должен называться как файл с сообщением, только расширение .sgn
9
5
  SIGNATURE_FILE_NAME = "message.txt.sgn"
10
- CERTIFICATE_FILE_NAME = "certificate.cer"
11
-
12
- CERTIFICATE_LINE_LENGTH = 64
13
6
 
14
7
  # Options: message, signature, certificate
15
8
  def self.verify(options)
@@ -17,6 +10,8 @@ module Cryptopro
17
10
  raise "Signature required" if (options[:signature].nil? || options[:signature].empty?)
18
11
  raise "Certificate required" if (options[:certificate].nil? || options[:certificate].empty?)
19
12
 
13
+ # Для работы с cryptcp требуется, чтобы сообщение, полпись и сертификат были в виде файлов
14
+ # Создаётся временная уникальная папка для каждой проверки
20
15
  tmp_dir = create_temp_dir
21
16
  create_temp_files(tmp_dir, options)
22
17
  valid = execute(tmp_dir)
@@ -24,14 +19,6 @@ module Cryptopro
24
19
 
25
20
  private
26
21
 
27
- # Для работы с cryptcp требуется, чтобы сообщение, полпись и сертификат были в виде файлов
28
- # Создаётся временная уникальная папка для каждой проверки
29
- def self.create_temp_dir
30
- uniq_name = Digest::MD5.hexdigest("#{rand(1_000_000)}#{Time.now}")
31
- full_name = "#{Dir.tmpdir}/cryptcp/#{uniq_name}"
32
- FileUtils.mkdir_p(full_name)
33
- end
34
-
35
22
  def self.create_temp_files(tmp_dir, options)
36
23
  # Создать файл сообщения
37
24
  create_temp_file(tmp_dir, MESSAGE_FILE_NAME, options[:message])
@@ -42,10 +29,6 @@ module Cryptopro
42
29
  create_temp_file(tmp_dir, CERTIFICATE_FILE_NAME, certificate_with_container)
43
30
  end
44
31
 
45
- def self.create_temp_file(dir_name, file_name, content)
46
- File.open("#{dir_name}/#{file_name}", "w") { |file| file.write(content) }
47
- end
48
-
49
32
  # Обсуждение формата использования: http://www.cryptopro.ru/forum2/Default.aspx?g=posts&t=1516
50
33
  # Пример вызова утилиты cryptcp:
51
34
  # cryptcp -vsignf -dir /home/user/signs -f certificate.cer message.txt
@@ -67,16 +50,5 @@ module Cryptopro
67
50
  end
68
51
  end
69
52
 
70
- # Добавляет -----BEGIN CERTIFICATE----- / -----END CERTIFICATE-----, если их нет.
71
- # Так же делит длинную строку Base64 на строки по 64 символа.
72
- # Это требование cryptcp к файл с сертификатом.
73
- def self.add_container_to_certificate(certificate)
74
- return certificate if certificate.downcase.include?("begin")
75
-
76
- parts = certificate.scan(/.{1,#{CERTIFICATE_LINE_LENGTH}}/)
77
- certificate_with_container = "-----BEGIN CERTIFICATE-----\n#{parts.join("\n")}\n-----END CERTIFICATE-----"
78
- end
79
-
80
53
  end
81
-
82
54
  end
@@ -1,3 +1,3 @@
1
1
  module Cryptopro
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryptopro
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - divineforest
@@ -15,18 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-11-14 00:00:00 +03:00
17
+ date: 2011-11-17 00:00:00 +04:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: cocaine
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
25
  - - ">="
28
26
  - !ruby/object:Gem::Version
29
- hash: 3
30
27
  segments:
31
28
  - 0
32
29
  version: "0"
@@ -49,6 +46,8 @@ files:
49
46
  - Rakefile
50
47
  - cryptopro.gemspec
51
48
  - lib/cryptopro.rb
49
+ - lib/cryptopro/base.rb
50
+ - lib/cryptopro/certificate.rb
52
51
  - lib/cryptopro/csr.rb
53
52
  - lib/cryptopro/signature.rb
54
53
  - lib/cryptopro/version.rb
@@ -62,27 +61,23 @@ rdoc_options: []
62
61
  require_paths:
63
62
  - lib
64
63
  required_ruby_version: !ruby/object:Gem::Requirement
65
- none: false
66
64
  requirements:
67
65
  - - ">="
68
66
  - !ruby/object:Gem::Version
69
- hash: 3
70
67
  segments:
71
68
  - 0
72
69
  version: "0"
73
70
  required_rubygems_version: !ruby/object:Gem::Requirement
74
- none: false
75
71
  requirements:
76
72
  - - ">="
77
73
  - !ruby/object:Gem::Version
78
- hash: 3
79
74
  segments:
80
75
  - 0
81
76
  version: "0"
82
77
  requirements: []
83
78
 
84
79
  rubyforge_project: cryptopro
85
- rubygems_version: 1.6.2
80
+ rubygems_version: 1.3.6
86
81
  signing_key:
87
82
  specification_version: 3
88
83
  summary: CryptoPro ruby-wrapper for linux