robust_client_socket 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.en.md +2 -2
- data/README.md +2 -2
- data/lib/robust_client_socket/ciphers/base.rb +44 -0
- data/lib/robust_client_socket/ciphers/ecdh.rb +61 -0
- data/lib/robust_client_socket/ciphers/x25519.rb +15 -0
- data/lib/robust_client_socket/ciphers/x448.rb +15 -0
- data/lib/robust_client_socket/configuration.rb +1 -1
- data/lib/robust_client_socket/http/helpers.rb +1 -1
- data/lib/robust_client_socket.rb +2 -1
- data/lib/version.rb +1 -1
- data/spec/lib/robust_client_socket/configuration_spec.rb +1 -1
- data/spec/lib/robust_client_socket/http/helpers_spec.rb +1 -1
- metadata +5 -2
- data/lib/robust_client_socket/token_cipher.rb +0 -112
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0e12655b56d656504508d1722c003bf175f684479609091f24d8ae7d7a21c4a1
|
|
4
|
+
data.tar.gz: 6a0fb316719d97d904bd228f9452dc80abbb3cdccc0a5d9dbcdcd2d3dfd9e907
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: edf7e99c4de9fb69494c93d2ed6a4698100f4a02510d7e0025296cd71a6ae28fa07da82567c1eb09f2943df4b720bd9f6ca1dcc15d77723aee2c041e3bad8e09
|
|
7
|
+
data.tar.gz: d740828d6ab51ed7be3f84c48dcfcc4d33a48bd6a0b08ce97380582b46e1fc038efeb3d76e98b48c4921bf191c80979b3e4f9c3ca5b5308f710894e24617bf2f
|
data/README.en.md
CHANGED
|
@@ -85,7 +85,7 @@ RobustClientSocket provides secure communication between microservices:
|
|
|
85
85
|
- **Scheme**: `c.cipher`, default `:x25519`, `:x448` is built in. A per-service `cipher:` overrides it
|
|
86
86
|
- **X25519 / X448**: a fresh ephemeral key per token; the service keeps one static key pair of the same scheme
|
|
87
87
|
- **AES-256-GCM**: authenticated encryption, a tampered token is rejected
|
|
88
|
-
- **Your own scheme**:
|
|
88
|
+
- **Your own scheme**: a `Base` subclass calls `register_cipher :name`. X25519 and X448 inherit `Ecdh`
|
|
89
89
|
- **Key validation**: the key must match the scheme. RSA and a private key are rejected
|
|
90
90
|
|
|
91
91
|
### 3. TLS/SSL Protection
|
|
@@ -134,7 +134,7 @@ RobustClientSocket.configure do |c|
|
|
|
134
134
|
c.client_name = 'core'
|
|
135
135
|
|
|
136
136
|
# OPTIONAL: token scheme. Default :x25519, :x448 is built in.
|
|
137
|
-
# Your own:
|
|
137
|
+
# Your own: a Base subclass calls register_cipher :name.
|
|
138
138
|
c.cipher = :x25519
|
|
139
139
|
|
|
140
140
|
# OPTIONAL: Header name for token (default: 'Secure-Token')
|
data/README.md
CHANGED
|
@@ -85,7 +85,7 @@ RobustClientSocket обеспечивает защищённую коммуни
|
|
|
85
85
|
- **Выбор схемы**: `c.cipher`, по умолчанию `:x25519`, встроен ещё `:x448`. На сервисе ключ `cipher:` перекрывает общий
|
|
86
86
|
- **X25519 / X448**: эфемерный ключ на каждый токен, сервер хранит только статическую пару той же схемы
|
|
87
87
|
- **AES-256-GCM**: аутентифицированное шифрование, подмена токена отвергается
|
|
88
|
-
- **Своя схема**:
|
|
88
|
+
- **Своя схема**: наследник `Base` вызывает `register_cipher :name`. X25519 и X448 наследуют `Ecdh`
|
|
89
89
|
- **Валидация ключей**: ключ обязан совпадать со схемой. RSA и приватный ключ отвергаются
|
|
90
90
|
|
|
91
91
|
### 3. TLS/SSL защита
|
|
@@ -134,7 +134,7 @@ RobustClientSocket.configure do |c|
|
|
|
134
134
|
c.client_name = 'core'
|
|
135
135
|
|
|
136
136
|
# ОПЦИОНАЛЬНО: схема токена. :x25519 по умолчанию, встроен ещё :x448.
|
|
137
|
-
# Своя схема:
|
|
137
|
+
# Своя схема: класс-наследник Base вызывает register_cipher :name.
|
|
138
138
|
c.cipher = :x25519
|
|
139
139
|
|
|
140
140
|
# ОПЦИОНАЛЬНО: Имя заголовка для токена (default: 'Secure-Token')
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RobustClientSocket
|
|
4
|
+
module Ciphers
|
|
5
|
+
class Base
|
|
6
|
+
UnknownCipher = Class.new(SecurityError)
|
|
7
|
+
CIPHERS = []
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
def ciphers
|
|
11
|
+
CIPHERS
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def register_cipher(name)
|
|
15
|
+
raise ArgumentError, 'cipher name must be a Symbol' unless name.is_a?(Symbol)
|
|
16
|
+
|
|
17
|
+
CIPHERS.delete(self)
|
|
18
|
+
@cipher_name = name
|
|
19
|
+
CIPHERS << self
|
|
20
|
+
name
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def cipher_name
|
|
24
|
+
@cipher_name
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def fetch(name)
|
|
28
|
+
found = ciphers.find { |cipher| cipher.cipher_name == name.to_sym }
|
|
29
|
+
return found if found
|
|
30
|
+
|
|
31
|
+
raise UnknownCipher, "Unknown cipher: #{name}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def seal(_plaintext, _public_key_pem)
|
|
35
|
+
raise NotImplementedError
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def read_public_key(_pem)
|
|
39
|
+
raise NotImplementedError
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openssl'
|
|
4
|
+
require 'base64'
|
|
5
|
+
require_relative 'base'
|
|
6
|
+
|
|
7
|
+
module RobustClientSocket
|
|
8
|
+
module Ciphers
|
|
9
|
+
class Ecdh < Base
|
|
10
|
+
KEY_LENGTH = 32
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def seal(plaintext, public_key_pem)
|
|
14
|
+
recipient = read_public_key(public_key_pem)
|
|
15
|
+
ephemeral = OpenSSL::PKey.generate_key(self::ALGORITHM)
|
|
16
|
+
blob = ephemeral.public_to_der.b + gcm_seal(derive_key(ephemeral.derive(recipient)), plaintext)
|
|
17
|
+
|
|
18
|
+
::Base64.strict_encode64(blob)
|
|
19
|
+
rescue OpenSSL::PKey::PKeyError, OpenSSL::Cipher::CipherError => e
|
|
20
|
+
raise SecurityError, "Encryption failed: #{e.message}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def read_public_key(pem)
|
|
24
|
+
key = OpenSSL::PKey.read(pem)
|
|
25
|
+
raise SecurityError, 'Invalid public key' unless public_key?(key)
|
|
26
|
+
|
|
27
|
+
key
|
|
28
|
+
rescue OpenSSL::PKey::PKeyError => e
|
|
29
|
+
raise SecurityError, "Invalid public key: #{e.message}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def public_key?(key)
|
|
35
|
+
key.oid == self::ALGORITHM && !holds_private_key?(key)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def holds_private_key?(key)
|
|
39
|
+
!key.raw_private_key.nil?
|
|
40
|
+
rescue OpenSSL::PKey::PKeyError
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def derive_key(shared_secret)
|
|
45
|
+
OpenSSL::KDF.hkdf(shared_secret, salt: '', info: self::INFO, length: KEY_LENGTH, hash: 'SHA256')
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def gcm_seal(key, plaintext)
|
|
49
|
+
cipher = OpenSSL::Cipher.new('aes-256-gcm')
|
|
50
|
+
cipher.encrypt
|
|
51
|
+
cipher.key = key
|
|
52
|
+
iv = cipher.random_iv
|
|
53
|
+
cipher.auth_data = self::INFO
|
|
54
|
+
ciphertext = cipher.update(plaintext) + cipher.final
|
|
55
|
+
|
|
56
|
+
iv + cipher.auth_tag + ciphertext
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'ecdh'
|
|
4
|
+
|
|
5
|
+
module RobustClientSocket
|
|
6
|
+
module Ciphers
|
|
7
|
+
class X25519 < Ecdh
|
|
8
|
+
ALGORITHM = 'X25519'
|
|
9
|
+
EPHEMERAL_DER_LENGTH = 44
|
|
10
|
+
INFO = 'robust-socket-token-v1-x25519'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
X25519.register_cipher :x25519
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'ecdh'
|
|
4
|
+
|
|
5
|
+
module RobustClientSocket
|
|
6
|
+
module Ciphers
|
|
7
|
+
class X448 < Ecdh
|
|
8
|
+
ALGORITHM = 'X448'
|
|
9
|
+
EPHEMERAL_DER_LENGTH = 68
|
|
10
|
+
INFO = 'robust-socket-token-v1-x448'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
X448.register_cipher :x448
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -30,7 +30,7 @@ module RobustClientSocket
|
|
|
30
30
|
configuration.services.each do |service_name, creds|
|
|
31
31
|
next unless creds[:public_key]
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
Ciphers::Base.fetch(creds.fetch(:cipher, configuration.cipher)).read_public_key(creds[:public_key])
|
|
34
34
|
rescue SecurityError => e
|
|
35
35
|
raise SecurityError, "Invalid public key for #{service_name}: #{e.message}"
|
|
36
36
|
end
|
data/lib/robust_client_socket.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'version'
|
|
4
4
|
require_relative 'robust_client_socket/configuration'
|
|
5
|
+
require_relative 'robust_client_socket/ciphers/x25519'
|
|
6
|
+
require_relative 'robust_client_socket/ciphers/x448'
|
|
5
7
|
|
|
6
8
|
module RobustClientSocket
|
|
7
9
|
extend RobustClientSocket::Configuration
|
|
@@ -16,7 +18,6 @@ module RobustClientSocket
|
|
|
16
18
|
require 'base64'
|
|
17
19
|
require 'oj'
|
|
18
20
|
|
|
19
|
-
require_relative 'robust_client_socket/token_cipher'
|
|
20
21
|
require_relative 'robust_client_socket/http/httparty_overrides'
|
|
21
22
|
require_relative 'robust_client_socket/http/helpers'
|
|
22
23
|
require_relative 'robust_client_socket/http/client'
|
data/lib/version.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
require './lib/robust_client_socket/configuration.rb'
|
|
3
|
-
require './lib/robust_client_socket/
|
|
3
|
+
require './lib/robust_client_socket/ciphers/x25519'
|
|
4
4
|
|
|
5
5
|
RSpec.describe RobustClientSocket::Configuration do
|
|
6
6
|
let(:dummy_class) { Class.new { extend RobustClientSocket::Configuration } }
|
|
@@ -3,7 +3,7 @@ require 'openssl'
|
|
|
3
3
|
require 'base64'
|
|
4
4
|
require './lib/version.rb'
|
|
5
5
|
require './lib/robust_client_socket/http/helpers.rb'
|
|
6
|
-
require './lib/robust_client_socket/
|
|
6
|
+
require './lib/robust_client_socket/ciphers/x25519'
|
|
7
7
|
|
|
8
8
|
RSpec.describe RobustClientSocket::HTTP::Helpers do
|
|
9
9
|
let(:dummy_class) do
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: robust_client_socket
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.7.
|
|
4
|
+
version: 0.7.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Taras Zhuk
|
|
@@ -96,11 +96,14 @@ files:
|
|
|
96
96
|
- img.png
|
|
97
97
|
- img_1.png
|
|
98
98
|
- lib/robust_client_socket.rb
|
|
99
|
+
- lib/robust_client_socket/ciphers/base.rb
|
|
100
|
+
- lib/robust_client_socket/ciphers/ecdh.rb
|
|
101
|
+
- lib/robust_client_socket/ciphers/x25519.rb
|
|
102
|
+
- lib/robust_client_socket/ciphers/x448.rb
|
|
99
103
|
- lib/robust_client_socket/configuration.rb
|
|
100
104
|
- lib/robust_client_socket/http/client.rb
|
|
101
105
|
- lib/robust_client_socket/http/helpers.rb
|
|
102
106
|
- lib/robust_client_socket/http/httparty_overrides.rb
|
|
103
|
-
- lib/robust_client_socket/token_cipher.rb
|
|
104
107
|
- lib/version.rb
|
|
105
108
|
- robust_client_socket.gemspec
|
|
106
109
|
- spec/lib/payrent_client_socket.rb
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'openssl'
|
|
4
|
-
require 'base64'
|
|
5
|
-
|
|
6
|
-
module RobustClientSocket
|
|
7
|
-
# A named asymmetric token scheme. Built-ins are X25519 and X448.
|
|
8
|
-
# Register your own with CipherRegistry.register(:name, scheme).
|
|
9
|
-
# A scheme answers seal(plaintext, public_key_pem) and read_public_key(pem).
|
|
10
|
-
module CipherRegistry
|
|
11
|
-
UnknownCipher = Class.new(SecurityError)
|
|
12
|
-
|
|
13
|
-
@schemes = {}
|
|
14
|
-
|
|
15
|
-
class << self
|
|
16
|
-
def register(name, scheme)
|
|
17
|
-
raise ArgumentError, 'cipher name must be a Symbol' unless name.is_a?(Symbol)
|
|
18
|
-
raise ArgumentError, 'cipher must answer seal and read_public_key' unless scheme.respond_to?(:seal) && scheme.respond_to?(:read_public_key)
|
|
19
|
-
|
|
20
|
-
@schemes[name] = scheme
|
|
21
|
-
name
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def fetch(name)
|
|
25
|
-
@schemes.fetch(name.to_sym) { raise UnknownCipher, "Unknown cipher: #{name}" }
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def registered?(name)
|
|
29
|
-
@schemes.key?(name.to_sym)
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
module EcdhCipher
|
|
35
|
-
IV_LENGTH = 12
|
|
36
|
-
TAG_LENGTH = 16
|
|
37
|
-
KEY_LENGTH = 32
|
|
38
|
-
|
|
39
|
-
def self.build(algorithm, ephemeral_der_length)
|
|
40
|
-
Module.new do
|
|
41
|
-
extend self
|
|
42
|
-
|
|
43
|
-
const_set(:ALGORITHM, algorithm)
|
|
44
|
-
const_set(:EPHEMERAL_DER_LENGTH, ephemeral_der_length)
|
|
45
|
-
const_set(:INFO, "robust-socket-token-v1-#{algorithm.downcase}")
|
|
46
|
-
|
|
47
|
-
def seal(plaintext, public_key_pem)
|
|
48
|
-
recipient = read_public_key(public_key_pem)
|
|
49
|
-
ephemeral = OpenSSL::PKey.generate_key(self::ALGORITHM)
|
|
50
|
-
blob = ephemeral.public_to_der.b + gcm_seal(derive_key(ephemeral.derive(recipient)), plaintext)
|
|
51
|
-
|
|
52
|
-
::Base64.strict_encode64(blob)
|
|
53
|
-
rescue OpenSSL::PKey::PKeyError, OpenSSL::Cipher::CipherError => e
|
|
54
|
-
raise SecurityError, "Encryption failed: #{e.message}"
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def read_public_key(pem)
|
|
58
|
-
key = OpenSSL::PKey.read(pem)
|
|
59
|
-
raise SecurityError, 'Invalid public key' unless public_key?(key)
|
|
60
|
-
|
|
61
|
-
key
|
|
62
|
-
rescue OpenSSL::PKey::PKeyError => e
|
|
63
|
-
raise SecurityError, "Invalid public key: #{e.message}"
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def public_key?(key)
|
|
67
|
-
key.oid == self::ALGORITHM && !holds_private_key?(key)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
def holds_private_key?(key)
|
|
71
|
-
!key.raw_private_key.nil?
|
|
72
|
-
rescue OpenSSL::PKey::PKeyError
|
|
73
|
-
false
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def derive_key(shared_secret)
|
|
77
|
-
OpenSSL::KDF.hkdf(shared_secret, salt: '', info: self::INFO, length: EcdhCipher::KEY_LENGTH, hash: 'SHA256')
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
def gcm_seal(key, plaintext)
|
|
81
|
-
cipher = OpenSSL::Cipher.new('aes-256-gcm')
|
|
82
|
-
cipher.encrypt
|
|
83
|
-
cipher.key = key
|
|
84
|
-
iv = cipher.random_iv
|
|
85
|
-
cipher.auth_data = self::INFO
|
|
86
|
-
ciphertext = cipher.update(plaintext) + cipher.final
|
|
87
|
-
|
|
88
|
-
iv + cipher.auth_tag + ciphertext
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
X25519 = EcdhCipher.build('X25519', 44)
|
|
95
|
-
X448 = EcdhCipher.build('X448', 68)
|
|
96
|
-
|
|
97
|
-
CipherRegistry.register(:x25519, X25519)
|
|
98
|
-
CipherRegistry.register(:x448, X448)
|
|
99
|
-
|
|
100
|
-
# Default cipher. TokenCipher.seal keeps the previous call shape.
|
|
101
|
-
module TokenCipher
|
|
102
|
-
module_function
|
|
103
|
-
|
|
104
|
-
def seal(plaintext, public_key_pem, cipher: :x25519)
|
|
105
|
-
CipherRegistry.fetch(cipher).seal(plaintext, public_key_pem)
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def read_public_key(pem, cipher: :x25519)
|
|
109
|
-
CipherRegistry.fetch(cipher).read_public_key(pem)
|
|
110
|
-
end
|
|
111
|
-
end
|
|
112
|
-
end
|