ubiq-security 1.0.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +15 -14
- data/lib/ubiq-security.rb +5 -6
- data/lib/ubiq/algo.rb +65 -40
- data/lib/ubiq/auth.rb +77 -71
- data/lib/ubiq/credentials.rb +80 -64
- data/lib/ubiq/decrypt.rb +231 -211
- data/lib/ubiq/encrypt.rb +158 -146
- data/lib/ubiq/host.rb +4 -1
- data/lib/ubiq/version.rb +1 -2
- metadata +3 -2
data/lib/ubiq/encrypt.rb
CHANGED
@@ -13,154 +13,163 @@
|
|
13
13
|
#
|
14
14
|
# https://ubiqsecurity.com/legal
|
15
15
|
#
|
16
|
+
|
17
|
+
# frozen_string_literal: true
|
18
|
+
|
16
19
|
require 'rb-readline'
|
17
20
|
require 'byebug'
|
18
21
|
require 'httparty'
|
19
|
-
require
|
22
|
+
require 'active_support/all'
|
20
23
|
require_relative './auth.rb'
|
21
24
|
require_relative './algo.rb'
|
22
25
|
require 'webrick'
|
23
26
|
|
27
|
+
# Ubiq Security Modules for encrypting / decrypting data
|
24
28
|
module Ubiq
|
29
|
+
# Ubiq Encryption object
|
30
|
+
# This object represents a single data encryption key and can be used to
|
31
|
+
# encrypt several separate plain texts using the same key
|
32
|
+
class Encryption
|
33
|
+
def initialize(creds, uses)
|
34
|
+
raise 'Some of your credentials are missing, please check!' unless validate_creds(creds)
|
25
35
|
|
26
|
-
#
|
27
|
-
|
28
|
-
class Encryption
|
29
|
-
def initialize(creds, uses)
|
30
|
-
|
31
|
-
raise RuntimeError, 'Some of your credentials are missing, please check!' if !validate_creds(creds)
|
36
|
+
# Set host, either the default or the one given by caller
|
37
|
+
@host = creds.host.blank? ? UBIQ_HOST : creds.host
|
32
38
|
|
33
|
-
|
34
|
-
|
39
|
+
# Set the credentials in instance varibales to be used among methods
|
40
|
+
# The client's public API key (used to identify the client to the server
|
41
|
+
@papi = creds.access_key_id
|
35
42
|
|
36
|
-
|
37
|
-
|
38
|
-
@papi = creds.access_key_id
|
43
|
+
# The client's secret API key (used to authenticate HTTP requests)
|
44
|
+
@sapi = creds.secret_signing_key
|
39
45
|
|
40
|
-
|
41
|
-
|
46
|
+
# The client's secret RSA encryption key/password (used to decrypt the
|
47
|
+
# client's RSA key from the server). This key is not retained by this object.
|
48
|
+
@srsa = creds.secret_crypto_access_key
|
42
49
|
|
43
|
-
|
44
|
-
|
50
|
+
# Build the endpoint URL
|
51
|
+
url = endpoint_base + '/encryption/key'
|
45
52
|
|
46
|
-
|
47
|
-
|
53
|
+
# Build the Request Body with the number of uses of key
|
54
|
+
query = { uses: uses }
|
48
55
|
|
49
|
-
|
50
|
-
|
56
|
+
# Retrieve the necessary headers to make the request using Auth Object
|
57
|
+
headers = Auth.build_headers(@papi, @sapi, endpoint, query, @host, 'post')
|
51
58
|
|
52
|
-
|
53
|
-
|
59
|
+
@encryption_started = false
|
60
|
+
@encryption_ready = true
|
54
61
|
|
55
|
-
|
56
|
-
|
62
|
+
# Request a new encryption key from the server. if the request
|
63
|
+
# fails, the function raises a HTTPError indicating
|
64
|
+
# the status code returned by the server. this exception is
|
65
|
+
# propagated back to the caller
|
57
66
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
begin
|
68
|
+
response = HTTParty.post(
|
69
|
+
url,
|
70
|
+
body: query.to_json,
|
71
|
+
headers: headers
|
72
|
+
)
|
73
|
+
rescue HTTParty::Error
|
74
|
+
raise 'Cant reach server'
|
75
|
+
end
|
76
|
+
|
77
|
+
# Response status is 201 Created
|
78
|
+
if response.code == WEBrick::HTTPStatus::RC_CREATED
|
79
|
+
# The code below largely assumes that the server returns
|
80
|
+
# a json object that contains the members and is formatted
|
81
|
+
# according to the Ubiq REST specification.
|
82
|
+
|
83
|
+
# Build the key object
|
84
|
+
@key = {}
|
85
|
+
@key['id'] = response['key_fingerprint']
|
86
|
+
@key['session'] = response['encryption_session']
|
87
|
+
@key['security_model'] = response['security_model']
|
88
|
+
@key['algorithm'] = response['security_model']['algorithm'].downcase
|
89
|
+
@key['max_uses'] = response['max_uses']
|
90
|
+
@key['uses'] = 0
|
91
|
+
@key['encrypted'] = Base64.strict_decode64(response['encrypted_data_key'])
|
92
|
+
|
93
|
+
# Get encrypted private key from response body
|
94
|
+
encrypted_private_key = response['encrypted_private_key']
|
95
|
+
# Get wrapped data key from response body
|
96
|
+
wrapped_data_key = response['wrapped_data_key']
|
97
|
+
# Decrypt the encryped private key using @srsa supplied
|
98
|
+
private_key = OpenSSL::PKey::RSA.new(encrypted_private_key, @srsa)
|
99
|
+
# Decode WDK from base64 format
|
100
|
+
wdk = Base64.strict_decode64(wrapped_data_key)
|
101
|
+
# Use private key to decrypt the wrapped data key
|
102
|
+
dk = private_key.private_decrypt(wdk, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
|
103
|
+
@key['raw'] = dk
|
104
|
+
# Build the algorithm object
|
105
|
+
@algo = Algo.new.get_algo(@key['algorithm'])
|
106
|
+
else
|
107
|
+
# Raise the error if response is not 201
|
108
|
+
raise "HTTPError Response: Expected 201, got #{response.code}"
|
109
|
+
end
|
71
110
|
end
|
72
111
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
#
|
77
|
-
#
|
78
|
-
|
79
|
-
#
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
@key['
|
86
|
-
|
87
|
-
@key['
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
#
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
# Use private key to decrypt the wrapped data key
|
98
|
-
dk = private_key.private_decrypt(wdk,OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
|
99
|
-
@key['raw'] = dk
|
100
|
-
# Build the algorithm object
|
101
|
-
@algo = Algo.new.get_algo(@key['algorithm'])
|
102
|
-
else
|
103
|
-
# Raise the error if response is not 201
|
104
|
-
raise RuntimeError, "HTTPError Response: Expected 201, got #{response.code}"
|
112
|
+
def begin
|
113
|
+
# Begin the encryption process
|
114
|
+
|
115
|
+
# When this function is called, the encryption object increments
|
116
|
+
# the number of uses of the key and creates a new internal context
|
117
|
+
# to be used to encrypt the data.
|
118
|
+
# If the encryption object is not yet ready to be used, throw an error
|
119
|
+
raise 'Encryption not ready' unless @encryption_ready
|
120
|
+
|
121
|
+
# if Encryption cipher context already exists
|
122
|
+
raise 'Encryption already in progress' if @encryption_started
|
123
|
+
# If max uses > uses
|
124
|
+
raise 'Maximum key uses exceeded' if @key['uses'] >= @key['max_uses']
|
125
|
+
|
126
|
+
@key['uses'] += 1
|
127
|
+
# create a new Encryption context and initialization vector
|
128
|
+
@enc, @iv = Algo.new.encryptor(@algo, @key['raw'])
|
129
|
+
|
130
|
+
# Pack the result into bytes to get a byte string
|
131
|
+
struct = [0, Algo::UBIQ_HEADER_V0_FLAG_AAD, @algo[:id], @iv.length, @key['encrypted'].length].pack('CCCCn')
|
132
|
+
|
133
|
+
@enc.auth_data = struct + @iv + @key['encrypted']
|
134
|
+
@encryption_started = true
|
135
|
+
return struct + @iv + @key['encrypted']
|
105
136
|
end
|
106
137
|
|
107
|
-
|
108
|
-
|
109
|
-
def begin
|
110
|
-
# Begin the encryption process
|
111
|
-
|
112
|
-
# When this function is called, the encryption object increments
|
113
|
-
# the number of uses of the key and creates a new internal context
|
114
|
-
# to be used to encrypt the data.
|
115
|
-
# If the encryption object is not yet ready to be used, throw an error
|
116
|
-
raise RuntimeError, 'Encryption not ready' if !@encryption_ready
|
117
|
-
|
118
|
-
# if Encryption cipher context already exists
|
119
|
-
raise RuntimeError, 'Encryption already in progress' if @encryption_started
|
120
|
-
# If max uses > uses
|
121
|
-
raise RuntimeError, 'Maximum key uses exceeded' if @key['uses'] >= @key['max_uses']
|
122
|
-
@key['uses'] += 1
|
123
|
-
# create a new Encryption context and initialization vector
|
124
|
-
@enc , @iv = Algo.new.encryptor(@algo, @key['raw'])
|
125
|
-
|
126
|
-
# Pack the result into bytes to get a byte string
|
127
|
-
struct = [0, 0, @algo[:id], @iv.length, @key['encrypted'].length].pack('CCCCn')
|
128
|
-
@encryption_started = true
|
129
|
-
return struct + @iv + @key['encrypted']
|
130
|
-
end
|
138
|
+
def update(data)
|
139
|
+
raise 'Encryption is not Started' unless @encryption_started
|
131
140
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
@enc.update(data)
|
137
|
-
end
|
141
|
+
# Encryption of some plain text is perfomed here
|
142
|
+
# Any cipher text produced by the operation is returned
|
143
|
+
@enc.update(data)
|
144
|
+
end
|
138
145
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
146
|
+
def end
|
147
|
+
raise 'Encryption is not Started' unless @encryption_started
|
148
|
+
|
149
|
+
# This function finalizes the encryption (producing the final
|
150
|
+
# cipher text for the encryption, if necessary) and adds any
|
151
|
+
# authentication information (if required by the algorithm).
|
152
|
+
# Any data produced is returned by the function.
|
153
|
+
|
154
|
+
# Finalize an encryption
|
155
|
+
res = @enc.final
|
156
|
+
if @algo[:tag_length] != 0
|
157
|
+
# Add the tag to the cipher text
|
158
|
+
res += @enc.auth_tag
|
159
|
+
end
|
160
|
+
@encryption_started = false
|
161
|
+
# Return the encrypted result
|
162
|
+
return res
|
151
163
|
end
|
152
|
-
@encryption_started = false
|
153
|
-
# Return the encrypted result
|
154
|
-
return res
|
155
|
-
end
|
156
164
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
165
|
+
def close
|
166
|
+
raise 'Encryption currently running' if @encryption_started
|
167
|
+
|
168
|
+
# If the key was used less times than was requested, send an update to the server
|
169
|
+
if @key['uses'] < @key['max_uses']
|
170
|
+
query_url = "#{endpoint}/#{@key['id']}/#{@key['session']}"
|
162
171
|
url = "#{endpoint_base}/encryption/key/#{@key['id']}/#{@key['session']}"
|
163
|
-
query = {actual: @key['uses'], requested: @key['max_uses']}
|
172
|
+
query = { actual: @key['uses'], requested: @key['max_uses'] }
|
164
173
|
headers = Auth.build_headers(@papi, @sapi, query_url, query, @host, 'patch')
|
165
174
|
response = HTTParty.patch(
|
166
175
|
url,
|
@@ -168,40 +177,43 @@ class Encryption
|
|
168
177
|
headers: headers
|
169
178
|
)
|
170
179
|
remove_instance_variable(:@key)
|
171
|
-
|
180
|
+
@encryption_ready = false
|
181
|
+
end
|
172
182
|
end
|
173
|
-
end
|
174
183
|
|
175
|
-
|
176
|
-
|
177
|
-
|
184
|
+
def endpoint_base
|
185
|
+
@host + '/api/v0'
|
186
|
+
end
|
178
187
|
|
179
|
-
|
180
|
-
|
188
|
+
def endpoint
|
189
|
+
'/api/v0/encryption/key'
|
190
|
+
end
|
191
|
+
|
192
|
+
def validate_creds(credentials)
|
193
|
+
# This method checks for the presence of the credentials
|
194
|
+
!credentials.access_key_id.blank? &&
|
195
|
+
!credentials.secret_signing_key.blank? &&
|
196
|
+
!credentials.secret_crypto_access_key.blank?
|
197
|
+
end
|
181
198
|
end
|
182
199
|
|
183
200
|
def validate_creds(credentials)
|
184
201
|
# This method checks for the presence of the credentials
|
185
|
-
!credentials.access_key_id.blank?
|
202
|
+
!credentials.access_key_id.blank? &&
|
203
|
+
!credentials.secret_signing_key.blank? &&
|
204
|
+
!credentials.secret_crypto_access_key.blank?
|
186
205
|
end
|
187
206
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
res = enc.begin() + enc.update(data) + enc.end()
|
199
|
-
enc.close()
|
200
|
-
rescue
|
201
|
-
enc.close() if enc
|
202
|
-
raise
|
207
|
+
def encrypt(creds, data)
|
208
|
+
begin
|
209
|
+
enc = Encryption.new(creds, 1)
|
210
|
+
res = enc.begin + enc.update(data) + enc.end
|
211
|
+
enc.close
|
212
|
+
rescue StandardError
|
213
|
+
enc&.close
|
214
|
+
raise
|
215
|
+
end
|
216
|
+
return res
|
203
217
|
end
|
204
|
-
return res
|
205
218
|
end
|
206
219
|
|
207
|
-
end
|
data/lib/ubiq/host.rb
CHANGED
data/lib/ubiq/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ubiq-security
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ubiq Security, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rb-readline
|
@@ -58,6 +58,7 @@ executables: []
|
|
58
58
|
extensions: []
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
|
+
- CHANGELOG.md
|
61
62
|
- CODE_OF_CONDUCT.md
|
62
63
|
- Gemfile
|
63
64
|
- LICENSE.txt
|