robust_server_socket 0.6.0 → 0.6.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 +1 -1
- data/README.md +1 -1
- data/lib/robust_server_socket/ciphers/base.rb +48 -0
- data/lib/robust_server_socket/{token_cipher.rb → ciphers/ecdh.rb} +32 -75
- data/lib/robust_server_socket/ciphers/x25519.rb +15 -0
- data/lib/robust_server_socket/ciphers/x448.rb +15 -0
- data/lib/robust_server_socket/configuration.rb +1 -1
- data/lib/robust_server_socket/secure_token/decrypt.rb +1 -1
- data/lib/robust_server_socket.rb +2 -1
- data/lib/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1c77eac44af0afbbb67432e817f05adc22a1e181371e8745a26f9d9c6824ca0f
|
|
4
|
+
data.tar.gz: 24e89717e46931ab832bfd470dbe66554729a745952d93aa3116600c53ee85b5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e39d06bab2870ae9afac302b71690bb6eda4658991dd96882f521c9624a23737188d117297a64df1abd987c33f67178cdf59258a18090d72675e7544d67192cd
|
|
7
|
+
data.tar.gz: 9787d2103577e6b1f50b0fbd0b4396511cb30a1979fcaa0ed4ce1ec915d4c7ee0494954aa22b0e0a4b9d6478bcb71266309faf66acbe098bcb6773bfcea1fab1
|
data/README.en.md
CHANGED
|
@@ -84,7 +84,7 @@ RobustServerSocket implements a multi-layered protection system for inter-servic
|
|
|
84
84
|
### 1. Cryptographic Protection
|
|
85
85
|
- **Scheme**: `c.cipher`, default `:x25519`, `:x448` is built in. Client and server must pick the same one
|
|
86
86
|
- **X25519 / X448 + AES-256-GCM**: the token is sealed with an ephemeral key to the service's static key pair
|
|
87
|
-
- **Your own scheme**:
|
|
87
|
+
- **Your own scheme**: a `Base` subclass calls `register_cipher :name`. X25519 and X448 inherit `Ecdh`
|
|
88
88
|
- **Key validation**: only a private key of the selected scheme is accepted; RSA is rejected
|
|
89
89
|
|
|
90
90
|
### 2. Access Control
|
data/README.md
CHANGED
|
@@ -84,7 +84,7 @@ RobustServerSocket реализует многоуровневую систем
|
|
|
84
84
|
### 1. Криптографическая защита
|
|
85
85
|
- **Схема**: `c.cipher`, по умолчанию `:x25519`, встроен ещё `:x448`. Клиент и сервер должны выбрать одну
|
|
86
86
|
- **X25519 / X448 + AES-256-GCM**: токен запечатан эфемерным ключом на статическую пару сервиса
|
|
87
|
-
- **Своя схема**:
|
|
87
|
+
- **Своя схема**: наследник `Base` вызывает `register_cipher :name`. X25519 и X448 наследуют `Ecdh`
|
|
88
88
|
- **Валидация ключей**: принимается только приватный ключ выбранной схемы, RSA отвергается
|
|
89
89
|
|
|
90
90
|
### 2. Контроль доступа
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RobustServerSocket
|
|
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 open(_token, _private_key_pem)
|
|
39
|
+
raise NotImplementedError
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def read_private_key(_pem)
|
|
43
|
+
raise NotImplementedError
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -2,50 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
require 'openssl'
|
|
4
4
|
require 'base64'
|
|
5
|
+
require_relative 'base'
|
|
5
6
|
|
|
6
7
|
module RobustServerSocket
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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 open and read_private_key' unless scheme.respond_to?(:open) && scheme.respond_to?(:read_private_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
|
-
MAX_BLOB_BYTES = 1024
|
|
39
|
-
|
|
40
|
-
def self.build(algorithm, ephemeral_der_length)
|
|
41
|
-
Module.new do
|
|
42
|
-
extend self
|
|
43
|
-
|
|
44
|
-
const_set(:ALGORITHM, algorithm)
|
|
45
|
-
const_set(:EPHEMERAL_DER_LENGTH, ephemeral_der_length)
|
|
46
|
-
const_set(:HEADER_LENGTH, ephemeral_der_length + IV_LENGTH + TAG_LENGTH)
|
|
47
|
-
const_set(:INFO, "robust-socket-token-v1-#{algorithm.downcase}")
|
|
48
|
-
|
|
8
|
+
module Ciphers
|
|
9
|
+
class Ecdh < Base
|
|
10
|
+
MAX_BLOB_BYTES = 1024
|
|
11
|
+
IV_LENGTH = 12
|
|
12
|
+
TAG_LENGTH = 16
|
|
13
|
+
KEY_LENGTH = 32
|
|
14
|
+
|
|
15
|
+
class << self
|
|
49
16
|
def seal(plaintext, public_key_pem)
|
|
50
17
|
recipient = OpenSSL::PKey.read(public_key_pem)
|
|
51
18
|
raise SecurityError, 'Invalid public key' unless recipient.oid == self::ALGORITHM
|
|
@@ -57,16 +24,7 @@ module RobustServerSocket
|
|
|
57
24
|
end
|
|
58
25
|
|
|
59
26
|
def open(token, private_key_pem)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
decoded = ::Base64.strict_decode64(token)
|
|
63
|
-
raise SecureToken::InvalidToken, 'Token too large' if decoded.bytesize > EcdhCipher::MAX_BLOB_BYTES
|
|
64
|
-
raise SecureToken::InvalidToken, 'Invalid token' if decoded.bytesize <= self::HEADER_LENGTH
|
|
65
|
-
|
|
66
|
-
ephemeral_der = decoded.byteslice(0, self::EPHEMERAL_DER_LENGTH)
|
|
67
|
-
iv = decoded.byteslice(self::EPHEMERAL_DER_LENGTH, EcdhCipher::IV_LENGTH)
|
|
68
|
-
tag = decoded.byteslice(self::EPHEMERAL_DER_LENGTH + EcdhCipher::IV_LENGTH, EcdhCipher::TAG_LENGTH)
|
|
69
|
-
ciphertext = decoded.byteslice(self::HEADER_LENGTH..-1)
|
|
27
|
+
ephemeral_der, iv, tag, ciphertext = split(token)
|
|
70
28
|
key = derive_key(read_private_key(private_key_pem).derive(read_ephemeral(ephemeral_der)))
|
|
71
29
|
|
|
72
30
|
gcm_open(key, iv, tag, ciphertext)
|
|
@@ -83,6 +41,27 @@ module RobustServerSocket
|
|
|
83
41
|
raise SecurityError, "Invalid private key: #{e.message}"
|
|
84
42
|
end
|
|
85
43
|
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def split(token)
|
|
47
|
+
raise SecureToken::InvalidToken, 'Invalid token format' unless token.is_a?(String) && token.match?(SecureToken::BASE64_REGEXP)
|
|
48
|
+
|
|
49
|
+
decoded = ::Base64.strict_decode64(token)
|
|
50
|
+
raise SecureToken::InvalidToken, 'Token too large' if decoded.bytesize > MAX_BLOB_BYTES
|
|
51
|
+
raise SecureToken::InvalidToken, 'Invalid token' if decoded.bytesize <= header_length
|
|
52
|
+
|
|
53
|
+
[
|
|
54
|
+
decoded.byteslice(0, self::EPHEMERAL_DER_LENGTH),
|
|
55
|
+
decoded.byteslice(self::EPHEMERAL_DER_LENGTH, IV_LENGTH),
|
|
56
|
+
decoded.byteslice(self::EPHEMERAL_DER_LENGTH + IV_LENGTH, TAG_LENGTH),
|
|
57
|
+
decoded.byteslice(header_length..-1)
|
|
58
|
+
]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def header_length
|
|
62
|
+
self::EPHEMERAL_DER_LENGTH + IV_LENGTH + TAG_LENGTH
|
|
63
|
+
end
|
|
64
|
+
|
|
86
65
|
def private_key?(key)
|
|
87
66
|
key.oid == self::ALGORITHM && holds_private_key?(key)
|
|
88
67
|
end
|
|
@@ -101,7 +80,7 @@ module RobustServerSocket
|
|
|
101
80
|
end
|
|
102
81
|
|
|
103
82
|
def derive_key(shared_secret)
|
|
104
|
-
OpenSSL::KDF.hkdf(shared_secret, salt: '', info: self::INFO, length:
|
|
83
|
+
OpenSSL::KDF.hkdf(shared_secret, salt: '', info: self::INFO, length: KEY_LENGTH, hash: 'SHA256')
|
|
105
84
|
end
|
|
106
85
|
|
|
107
86
|
def gcm_seal(key, plaintext)
|
|
@@ -128,26 +107,4 @@ module RobustServerSocket
|
|
|
128
107
|
end
|
|
129
108
|
end
|
|
130
109
|
end
|
|
131
|
-
|
|
132
|
-
X25519 = EcdhCipher.build('X25519', 44)
|
|
133
|
-
X448 = EcdhCipher.build('X448', 68)
|
|
134
|
-
|
|
135
|
-
CipherRegistry.register(:x25519, X25519)
|
|
136
|
-
CipherRegistry.register(:x448, X448)
|
|
137
|
-
|
|
138
|
-
module TokenCipher
|
|
139
|
-
module_function
|
|
140
|
-
|
|
141
|
-
def seal(plaintext, public_key_pem, cipher: :x25519)
|
|
142
|
-
CipherRegistry.fetch(cipher).seal(plaintext, public_key_pem)
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
def open(token, private_key_pem, cipher: :x25519)
|
|
146
|
-
CipherRegistry.fetch(cipher).open(token, private_key_pem)
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
def read_private_key(pem, cipher: :x25519)
|
|
150
|
-
CipherRegistry.fetch(cipher).read_private_key(pem)
|
|
151
|
-
end
|
|
152
|
-
end
|
|
153
110
|
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'ecdh'
|
|
4
|
+
|
|
5
|
+
module RobustServerSocket
|
|
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 RobustServerSocket
|
|
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
|
|
@@ -53,7 +53,7 @@ module RobustServerSocket
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def validate_key_security!
|
|
56
|
-
|
|
56
|
+
Ciphers::Base.fetch(configuration.cipher).read_private_key(configuration.private_key)
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
|
|
@@ -5,7 +5,7 @@ module RobustServerSocket
|
|
|
5
5
|
module Decrypt
|
|
6
6
|
class << self
|
|
7
7
|
def call(token)
|
|
8
|
-
|
|
8
|
+
Ciphers::Base.fetch(RobustServerSocket.configuration.cipher).open(token, private_key_pem)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
# Clear cached private key (useful for hot reloading in development)
|
data/lib/robust_server_socket.rb
CHANGED
|
@@ -7,7 +7,8 @@ require 'connection_pool'
|
|
|
7
7
|
|
|
8
8
|
require_relative 'robust_server_socket/configuration'
|
|
9
9
|
require_relative 'robust_server_socket/secure_token'
|
|
10
|
-
require_relative 'robust_server_socket/
|
|
10
|
+
require_relative 'robust_server_socket/ciphers/x25519'
|
|
11
|
+
require_relative 'robust_server_socket/ciphers/x448'
|
|
11
12
|
require_relative 'robust_server_socket/secure_token/decrypt'
|
|
12
13
|
require_relative 'robust_server_socket/client_token'
|
|
13
14
|
|
data/lib/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: robust_server_socket
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tee_zed
|
|
@@ -96,6 +96,10 @@ files:
|
|
|
96
96
|
- Rakefile
|
|
97
97
|
- lib/robust_server_socket.rb
|
|
98
98
|
- lib/robust_server_socket/cacher.rb
|
|
99
|
+
- lib/robust_server_socket/ciphers/base.rb
|
|
100
|
+
- lib/robust_server_socket/ciphers/ecdh.rb
|
|
101
|
+
- lib/robust_server_socket/ciphers/x25519.rb
|
|
102
|
+
- lib/robust_server_socket/ciphers/x448.rb
|
|
99
103
|
- lib/robust_server_socket/client_token.rb
|
|
100
104
|
- lib/robust_server_socket/configuration.rb
|
|
101
105
|
- lib/robust_server_socket/modules/client_auth_protection.rb
|
|
@@ -104,7 +108,6 @@ files:
|
|
|
104
108
|
- lib/robust_server_socket/rate_limiter.rb
|
|
105
109
|
- lib/robust_server_socket/secure_token.rb
|
|
106
110
|
- lib/robust_server_socket/secure_token/decrypt.rb
|
|
107
|
-
- lib/robust_server_socket/token_cipher.rb
|
|
108
111
|
- lib/version.rb
|
|
109
112
|
- robust_server_socket.gemspec
|
|
110
113
|
licenses:
|