pkcs7-cryptographer 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -2
- data/Gemfile.lock +15 -8
- data/README.md +72 -9
- data/bin/console +1 -0
- data/lib/pkcs7/cryptographer.rb +3 -20
- data/lib/pkcs7/cryptographer/entity.rb +65 -0
- data/lib/pkcs7/cryptographer/initializers.rb +28 -0
- data/lib/pkcs7/cryptographer/version.rb +1 -1
- data/pkcs7-cryptographer.gemspec +3 -1
- metadata +36 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a33a97c4086f5f9780f1098df7d5983802301bcd6566b42030572871450cd304
|
4
|
+
data.tar.gz: d6257773adc3d2c0fd25383ad1b243c04317d091fee682e477b8dc384201dfec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a58da6fe867dfe729490b054a6ba53d1a07a5ccfde2812acdbd2d892af10e4f29f7345741b2c9f51bcf4af400d8f7cb19392b95d27a981850afdc47d76c816f
|
7
|
+
data.tar.gz: 7a30d3664b52f8789fc118d9b8b5b00a74187dec24db6edff6b850fddeee31e0058ce008d56734c2a8e1b9e7a081780fb99dfcfc21f15e50cef0f3857a0d6c73
|
data/.rubocop.yml
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rake
|
3
|
+
- rubocop-rspec
|
4
|
+
|
1
5
|
AllCops:
|
2
6
|
NewCops: enable
|
3
|
-
SuggestExtensions: false
|
4
7
|
Style/StringLiterals:
|
5
8
|
Enabled: true
|
6
9
|
EnforcedStyle: double_quotes
|
@@ -13,4 +16,10 @@ Layout/LineLength:
|
|
13
16
|
Max: 80
|
14
17
|
|
15
18
|
Metrics/BlockLength:
|
16
|
-
IgnoredMethods: ['describe', 'context']
|
19
|
+
IgnoredMethods: ['describe', 'context']
|
20
|
+
|
21
|
+
RSpec/MultipleMemoizedHelpers:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
RSpec/NestedGroups:
|
25
|
+
Max: 4
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
pkcs7-cryptographer (0.1
|
4
|
+
pkcs7-cryptographer (0.2.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -33,19 +33,24 @@ GEM
|
|
33
33
|
diff-lcs (>= 1.2.0, < 2.0)
|
34
34
|
rspec-support (~> 3.10.0)
|
35
35
|
rspec-support (3.10.2)
|
36
|
-
rubocop (
|
36
|
+
rubocop (1.12.0)
|
37
37
|
parallel (~> 1.10)
|
38
|
-
parser (>=
|
38
|
+
parser (>= 3.0.0.0)
|
39
39
|
rainbow (>= 2.2.2, < 4.0)
|
40
|
-
regexp_parser (>= 1.8)
|
40
|
+
regexp_parser (>= 1.8, < 3.0)
|
41
41
|
rexml
|
42
|
-
rubocop-ast (>=
|
42
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
43
43
|
ruby-progressbar (~> 1.7)
|
44
|
-
unicode-display_width (>= 1.4.0, <
|
44
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
45
45
|
rubocop-ast (1.4.1)
|
46
46
|
parser (>= 2.7.1.5)
|
47
|
+
rubocop-rake (0.5.1)
|
48
|
+
rubocop
|
49
|
+
rubocop-rspec (2.2.0)
|
50
|
+
rubocop (~> 1.0)
|
51
|
+
rubocop-ast (>= 1.1.0)
|
47
52
|
ruby-progressbar (1.11.0)
|
48
|
-
unicode-display_width (
|
53
|
+
unicode-display_width (2.0.0)
|
49
54
|
|
50
55
|
PLATFORMS
|
51
56
|
x86_64-darwin-19
|
@@ -56,7 +61,9 @@ DEPENDENCIES
|
|
56
61
|
pry
|
57
62
|
rake (~> 13.0)
|
58
63
|
rspec (~> 3.2)
|
59
|
-
rubocop (
|
64
|
+
rubocop (= 1.12.0)
|
65
|
+
rubocop-rake (= 0.5.1)
|
66
|
+
rubocop-rspec (= 2.2.0)
|
60
67
|
|
61
68
|
BUNDLED WITH
|
62
69
|
2.2.3
|
data/README.md
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
# PKCS7::Cryptographer
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/pkcs7-cryptographer.svg)](https://badge.fury.io/rb/pkcs7-cryptographer)
|
4
|
+
![main workflow](https://github.com/dmuneras/pkcs7-cryptographer/actions/workflows/main.yml/badge.svg)
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
Cryptographer is an small utility to encrypt and decrypt messages
|
4
9
|
using PKCS7.
|
5
10
|
|
6
|
-
PKCS7 is used to store signed and encrypted data.
|
7
|
-
as chipher in the encryption process. If you want to read more
|
8
|
-
the involved data structures and theory around this,
|
11
|
+
PKCS7 is used to store signed and encrypted data.This specific implementation
|
12
|
+
uses aes-256-cbc as chipher in the encryption process. If you want to read more
|
13
|
+
information about the involved data structures and theory around this,
|
14
|
+
please visit:
|
9
15
|
|
10
16
|
- https://ruby-doc.org/stdlib-3.0.0/libdoc/openssl/rdoc/OpenSSL.html
|
11
17
|
- https://tools.ietf.org/html/rfc5652
|
@@ -20,30 +26,36 @@ gem 'pkcs7-cryptographer'
|
|
20
26
|
|
21
27
|
And then execute:
|
22
28
|
|
29
|
+
```sh
|
23
30
|
$ bundle install
|
31
|
+
```
|
24
32
|
|
25
33
|
Or install it yourself as:
|
26
34
|
|
35
|
+
```sh
|
27
36
|
$ gem install pkcs7-cryptographer
|
28
|
-
|
37
|
+
```
|
29
38
|
## Usage
|
30
39
|
|
31
40
|
After installing the gem you will have the `PKCS7::Cryptographer` available.
|
32
41
|
|
33
|
-
`PKCS7::Cryptographer` is a class that provides
|
42
|
+
`PKCS7::Cryptographer` is a class that provides two public methods:
|
34
43
|
|
35
44
|
- `sign_and_encrypt`
|
36
45
|
- `decrypt_and_verify`
|
37
46
|
|
38
|
-
Read the following
|
47
|
+
Read the following examples to get a better undertanding:
|
48
|
+
|
49
|
+
|
50
|
+
### Using bare PKCS7::Cryptographer
|
39
51
|
|
40
52
|
```ruby
|
41
53
|
|
42
54
|
# This script assumes you have a read_file method to read the certificates and
|
43
55
|
# keys.
|
44
56
|
|
45
|
-
# What we are going to do is
|
46
|
-
# and read it from the Client:
|
57
|
+
# What we are going to do is signing an encrypting a message from the CA
|
58
|
+
# Authority and read it from the Client:
|
47
59
|
|
48
60
|
# Certificate Authority PKI data
|
49
61
|
CA_KEY = read_file("ca.key")
|
@@ -62,6 +74,8 @@ Read the following example to get a better undertanding:
|
|
62
74
|
# It could be read if the CA_STORE of the reader has certificate of the
|
63
75
|
# CA that signed the client certificate as trusted.
|
64
76
|
|
77
|
+
cryptographer = PKCS7::Cryptographer.new
|
78
|
+
|
65
79
|
# Client <------------------------- CA Authority API
|
66
80
|
encrypted_data = cryptographer.sign_and_encrypt(
|
67
81
|
data: "Atletico Nacional de Medellin",
|
@@ -87,6 +101,55 @@ Read the following example to get a better undertanding:
|
|
87
101
|
# decrypted_data returns: "Atletico Nacional de Medellin"
|
88
102
|
```
|
89
103
|
|
104
|
+
### Using PKCS7::Cryptographer::Entity
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
|
108
|
+
# This script assumes you have a read_file method to read the certificates and
|
109
|
+
# keys. If you have any question about how to generate the keys/certificates
|
110
|
+
# check this post: https://mariadb.com/kb/en/certificate-creation-with-openssl/
|
111
|
+
|
112
|
+
# What we are going to do is sending a message from the CA Authority and read
|
113
|
+
# it from the Client:
|
114
|
+
|
115
|
+
# Certificate Authority PKI data
|
116
|
+
CA_KEY = read_file("ca.key")
|
117
|
+
CA_CERTIFICATE = read_file("ca.crt")
|
118
|
+
|
119
|
+
# Client PKI data
|
120
|
+
CLIENT_CERTIFICATE = read_file("client.crt")
|
121
|
+
CLIENT_KEY = read_file("client.key")
|
122
|
+
|
123
|
+
CA_STORE = OpenSSL::X509::Store.new
|
124
|
+
CA_STORE.add_cert(OpenSSL::X509::Certificate.new(CA_CERTIFICATE))
|
125
|
+
|
126
|
+
ca_entity = PKCS7::Cryptographer::Entity.new(
|
127
|
+
key: CA_KEY,
|
128
|
+
certificate: CA_CERTIFICATE,
|
129
|
+
ca_store: CA_STORE
|
130
|
+
)
|
131
|
+
|
132
|
+
client_entity = PKCS7::Cryptographer::Entity.new(
|
133
|
+
key: CLIENT_KEY,
|
134
|
+
certificate: CLIENT_CERTIFICATE,
|
135
|
+
ca_store: CA_STORE
|
136
|
+
)
|
137
|
+
|
138
|
+
# SEND MESSAGE TO THE CLIENT
|
139
|
+
# ----------------------------------------------------------------------------
|
140
|
+
data = "Victor Ibarbo"
|
141
|
+
encrypted_data = ca_entity.encrypt_data(data: data, to: client_entity)
|
142
|
+
|
143
|
+
# READ MESSAGE IN CLIENT
|
144
|
+
# ----------------------------------------------------------------------------
|
145
|
+
decrypted_data = client_entity.decrypt_data(
|
146
|
+
data: encrypted_data,
|
147
|
+
from: ca_entity
|
148
|
+
)
|
149
|
+
|
150
|
+
# decrypted_data returns: "Victor Ibarbo"
|
151
|
+
```
|
152
|
+
|
90
153
|
## Development
|
91
154
|
|
92
155
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
data/bin/console
CHANGED
data/lib/pkcs7/cryptographer.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "openssl"
|
4
4
|
require_relative "cryptographer/version"
|
5
|
+
require_relative "cryptographer/initializers"
|
5
6
|
|
6
7
|
module PKCS7
|
7
8
|
###
|
@@ -14,6 +15,8 @@ module PKCS7
|
|
14
15
|
# - https://tools.ietf.org/html/rfc5652
|
15
16
|
###
|
16
17
|
class Cryptographer
|
18
|
+
include PKCS7::Cryptographer::Initializers
|
19
|
+
|
17
20
|
# PUBLIC METHODS
|
18
21
|
# --------------------------------------------------------------------------
|
19
22
|
|
@@ -75,25 +78,5 @@ module PKCS7
|
|
75
78
|
|
76
79
|
signed_data.data
|
77
80
|
end
|
78
|
-
|
79
|
-
# PRIVATE METHODS
|
80
|
-
# --------------------------------------------------------------------------
|
81
|
-
private
|
82
|
-
|
83
|
-
def x509_certificate(certificate)
|
84
|
-
wrap_in_class_or_return(certificate, OpenSSL::X509::Certificate)
|
85
|
-
end
|
86
|
-
|
87
|
-
def rsa_key(key)
|
88
|
-
wrap_in_class_or_return(key, OpenSSL::PKey::RSA)
|
89
|
-
end
|
90
|
-
|
91
|
-
def pkcs7(pkcs7)
|
92
|
-
wrap_in_class_or_return(pkcs7, OpenSSL::PKCS7)
|
93
|
-
end
|
94
|
-
|
95
|
-
def wrap_in_class_or_return(data, klass)
|
96
|
-
data.instance_of?(klass) ? data : klass.new(data)
|
97
|
-
end
|
98
81
|
end
|
99
82
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "initializers"
|
4
|
+
|
5
|
+
module PKCS7
|
6
|
+
class Cryptographer
|
7
|
+
###
|
8
|
+
# Define an entity abel to decrypt or encrypt messages to send them to other
|
9
|
+
# entities. It uses a Cryptographer to do the dirty work and just provide a
|
10
|
+
# more human readable way to read an pass messages between trustable
|
11
|
+
# entities.
|
12
|
+
###
|
13
|
+
class Entity
|
14
|
+
include PKCS7::Cryptographer::Initializers
|
15
|
+
|
16
|
+
attr_reader :certificate
|
17
|
+
|
18
|
+
# PUBLIC METHODS
|
19
|
+
# ------------------------------------------------------------------------
|
20
|
+
def initialize(key:, certificate:, ca_store: OpenSSL::X509::Store.new)
|
21
|
+
@key = rsa_key(key)
|
22
|
+
@certificate = x509_certificate(certificate)
|
23
|
+
@cryptographer = PKCS7::Cryptographer.new
|
24
|
+
@ca_store = ca_store
|
25
|
+
end
|
26
|
+
|
27
|
+
def trustable_entity?(entity)
|
28
|
+
@ca_store.verify(entity.certificate)
|
29
|
+
end
|
30
|
+
|
31
|
+
def encrypt_data(data:, to:)
|
32
|
+
perform_safely(to) do
|
33
|
+
@cryptographer.sign_and_encrypt(
|
34
|
+
data: data,
|
35
|
+
key: @key,
|
36
|
+
certificate: @certificate,
|
37
|
+
public_certificate: to.certificate
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def decrypt_data(data:, from:)
|
43
|
+
perform_safely(from) do
|
44
|
+
@cryptographer.decrypt_and_verify(
|
45
|
+
data: data,
|
46
|
+
key: @key,
|
47
|
+
certificate: @certificate,
|
48
|
+
public_certificate: from.certificate,
|
49
|
+
ca_store: @ca_store
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# PRIVATE METHODS
|
55
|
+
# ------------------------------------------------------------------------
|
56
|
+
private
|
57
|
+
|
58
|
+
def perform_safely(entity)
|
59
|
+
return false unless trustable_entity?(entity)
|
60
|
+
|
61
|
+
yield
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PKCS7
|
4
|
+
class Cryptographer
|
5
|
+
###
|
6
|
+
# Provides a set of methods to initialize OpenSSL objects if necessary. It
|
7
|
+
# allow consumers to pass either the OpenSSL ruby objects or the
|
8
|
+
# certificate, key or encrypted message string.
|
9
|
+
###
|
10
|
+
module Initializers
|
11
|
+
def x509_certificate(certificate)
|
12
|
+
wrap_in_class_or_return(certificate, OpenSSL::X509::Certificate)
|
13
|
+
end
|
14
|
+
|
15
|
+
def rsa_key(key)
|
16
|
+
wrap_in_class_or_return(key, OpenSSL::PKey::RSA)
|
17
|
+
end
|
18
|
+
|
19
|
+
def pkcs7(pkcs7)
|
20
|
+
wrap_in_class_or_return(pkcs7, OpenSSL::PKCS7)
|
21
|
+
end
|
22
|
+
|
23
|
+
def wrap_in_class_or_return(data, klass)
|
24
|
+
data.instance_of?(klass) ? data : klass.new(data)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/pkcs7-cryptographer.gemspec
CHANGED
@@ -31,5 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.add_development_dependency "pry"
|
32
32
|
spec.add_development_dependency "rake", "~> 13.0"
|
33
33
|
spec.add_development_dependency "rspec", "~> 3.2"
|
34
|
-
spec.add_development_dependency "rubocop", "
|
34
|
+
spec.add_development_dependency "rubocop", "1.12.0"
|
35
|
+
spec.add_development_dependency "rubocop-rake", "0.5.1"
|
36
|
+
spec.add_development_dependency "rubocop-rspec", "2.2.0"
|
35
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pkcs7-cryptographer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Munera Sanchez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
11
|
+
date: 2021-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,16 +70,44 @@ dependencies:
|
|
70
70
|
name: rubocop
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '='
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 1.12.0
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.12.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.5.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.5.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 2.2.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
81
109
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
110
|
+
version: 2.2.0
|
83
111
|
description: Utility to encrypt and decrypt messages using OpenSSL::PKCS7
|
84
112
|
email:
|
85
113
|
- dmunera119@gmail.com
|
@@ -100,6 +128,8 @@ files:
|
|
100
128
|
- bin/console
|
101
129
|
- bin/setup
|
102
130
|
- lib/pkcs7/cryptographer.rb
|
131
|
+
- lib/pkcs7/cryptographer/entity.rb
|
132
|
+
- lib/pkcs7/cryptographer/initializers.rb
|
103
133
|
- lib/pkcs7/cryptographer/version.rb
|
104
134
|
- pkcs7-cryptographer.gemspec
|
105
135
|
homepage: https://github.com/dmuneras/pkcs7-cryptographer
|