rb_mumble_protocol 0.2.1 → 0.3.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/ext/rb_mumble_protocol/src/lib.rs +82 -26
- data/lib/rb_mumble_protocol/crypt_state.rb +13 -2
- data/lib/rb_mumble_protocol/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53a24a4721b3a21ec02c022b203db3f44935b89e45df558711a39cf76c390d27
|
4
|
+
data.tar.gz: 47fac63e468a754daafe67bd1c26516e5d3f99335c5527d8881922b2a90d99f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f182dc904194306a67f4d815b4373637b646b115fb5b61f2e51e2df47e8bfd6d44885e827ebbb86687b6cc81df18fc2dcb60301edd0359f49a8a30fad1c4a0cf
|
7
|
+
data.tar.gz: 0bcba759478da33bd4b289212c7025c6d5826f19a68eb20581c069c8ed19ace99878ce92ca5e762691d148ae1e1c71b40c25b348206399c21d4f6e85caa97140
|
@@ -27,9 +27,55 @@ static BASE_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
|
27
27
|
|
28
28
|
pub mod crypt_state;
|
29
29
|
|
30
|
-
|
30
|
+
use crypt_state::{DecryptError};
|
31
|
+
|
32
|
+
impl Into<u8> for DecryptError {
|
33
|
+
fn into(self: DecryptError) -> u8 {
|
34
|
+
match self {
|
35
|
+
DecryptError::Repeat => 1,
|
36
|
+
DecryptError::Late => 2,
|
37
|
+
DecryptError::Mac => 3,
|
38
|
+
DecryptError::Eof => 4
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
#[magnus::wrap(class = "RbMumbleProtocol::CryptState", size)]
|
31
44
|
struct CryptStateRef(RefCell<crypt_state::CryptState>);
|
32
45
|
|
46
|
+
#[magnus::wrap(class = "RbMumbleProtocol::DecryptResult", free_immediately, size)]
|
47
|
+
struct DecryptResult {
|
48
|
+
buffer: RefCell<Vec<u8>>,
|
49
|
+
reason_raw: u8
|
50
|
+
}
|
51
|
+
|
52
|
+
impl DecryptResult {
|
53
|
+
pub fn new(buffer: Vec<u8>, result: Result<(), DecryptError>) -> Self {
|
54
|
+
let reason_raw =
|
55
|
+
match result {
|
56
|
+
Ok(()) => 0,
|
57
|
+
Err(e) => e.into()
|
58
|
+
};
|
59
|
+
|
60
|
+
Self {
|
61
|
+
buffer: RefCell::new(buffer),
|
62
|
+
reason_raw: reason_raw.into()
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
pub fn is_success(&self) -> bool {
|
67
|
+
self.reason_raw == 0
|
68
|
+
}
|
69
|
+
|
70
|
+
pub fn data(&self) -> Vec<u8> {
|
71
|
+
self.buffer.borrow().to_vec()
|
72
|
+
}
|
73
|
+
|
74
|
+
pub fn reason_raw_value(&self) -> u8 {
|
75
|
+
self.reason_raw
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
33
79
|
impl CryptStateRef {
|
34
80
|
pub fn new() -> Self {
|
35
81
|
Self(RefCell::new(crypt_state::CryptState::generate_new()))
|
@@ -64,6 +110,23 @@ impl CryptStateRef {
|
|
64
110
|
Self(RefCell::new(new_state))
|
65
111
|
}
|
66
112
|
|
113
|
+
pub fn set_decrypt_nonce(&self, nonce: Vec<u8>) -> Result<(), Error> {
|
114
|
+
match self.0.try_borrow_mut() {
|
115
|
+
Ok(mut ref_) => {
|
116
|
+
match &nonce.clone().try_into() {
|
117
|
+
Ok(array) => Ok(ref_.set_decrypt_nonce(array)),
|
118
|
+
Err(_e) => {
|
119
|
+
let msg = format!("Expected a Decrypt nonce of length {}", 16);
|
120
|
+
let err = Error::new(get_ruby().get_inner(&BASE_ERROR), msg);
|
121
|
+
|
122
|
+
Err(err)
|
123
|
+
}
|
124
|
+
}
|
125
|
+
},
|
126
|
+
Err(_e) => { Err(Error::new(get_ruby().get_inner(&BASE_ERROR), "borrow error")) }
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
67
130
|
pub fn key(&self) -> Result<Vec<u8>, Error> {
|
68
131
|
match self.0.try_borrow() {
|
69
132
|
Ok(ref_) => { Ok(ref_.get_key().to_vec()) },
|
@@ -114,27 +177,14 @@ impl CryptStateRef {
|
|
114
177
|
}
|
115
178
|
}
|
116
179
|
|
117
|
-
pub fn decrypt(&self, encrypted: Vec<u8>) -> Result<
|
180
|
+
pub fn decrypt(&self, encrypted: Vec<u8>) -> Result<DecryptResult, Error> {
|
118
181
|
match self.0.try_borrow_mut() {
|
119
182
|
Ok(mut state) => {
|
120
183
|
let mut buffer = BytesMut::new();
|
121
184
|
buffer.extend_from_slice(&encrypted);
|
185
|
+
let result = state.decrypt(&mut buffer);
|
122
186
|
|
123
|
-
|
124
|
-
Ok(_) => Ok(buffer.to_vec()),
|
125
|
-
Err(crypt_state::DecryptError::Repeat) => {
|
126
|
-
Err(Error::new(get_ruby().get_inner(&BASE_ERROR), "DecryptError::Repeat"))
|
127
|
-
},
|
128
|
-
Err(crypt_state::DecryptError::Late) => {
|
129
|
-
Err(Error::new(get_ruby().get_inner(&BASE_ERROR), "DecryptError::Late"))
|
130
|
-
},
|
131
|
-
Err(crypt_state::DecryptError::Mac) => {
|
132
|
-
Err(Error::new(get_ruby().get_inner(&BASE_ERROR), "DecryptError::Mac"))
|
133
|
-
},
|
134
|
-
Err(crypt_state::DecryptError::Eof) => {
|
135
|
-
Err(Error::new(get_ruby().get_inner(&BASE_ERROR), "DecryptError::Eof"))
|
136
|
-
}
|
137
|
-
}
|
187
|
+
Ok(DecryptResult::new(buffer.to_vec(), result))
|
138
188
|
},
|
139
189
|
Err(_e) => { Err(Error::new(get_ruby().get_inner(&BASE_ERROR), "borrow error")) }
|
140
190
|
}
|
@@ -149,18 +199,24 @@ fn get_ruby() -> Ruby {
|
|
149
199
|
#[magnus::init]
|
150
200
|
fn init() -> Result<(), Error> {
|
151
201
|
let module = define_module("RbMumbleProtocol")?;
|
152
|
-
let
|
202
|
+
let class1 = module.define_class("CryptState", class::object())?;
|
203
|
+
|
204
|
+
class1.define_singleton_method("new", function!(CryptStateRef::new, 0))?;
|
205
|
+
class1.define_singleton_method("new_from", function!(CryptStateRef::new_from, 3))?;
|
153
206
|
|
154
|
-
|
155
|
-
|
207
|
+
class1.define_method("key", method!(CryptStateRef::key, 0))?;
|
208
|
+
class1.define_method("encrypt_nonce", method!(CryptStateRef::encrypt_nonce, 0))?;
|
209
|
+
class1.define_method("decrypt_nonce", method!(CryptStateRef::decrypt_nonce, 0))?;
|
210
|
+
class1.define_method("stats", method!(CryptStateRef::stats, 0))?;
|
211
|
+
class1.define_method("set_decrypt_nonce", method!(CryptStateRef::set_decrypt_nonce, 1))?;
|
156
212
|
|
157
|
-
|
158
|
-
|
159
|
-
class.define_method("decrypt_nonce", method!(CryptStateRef::decrypt_nonce, 0))?;
|
160
|
-
class.define_method("stats", method!(CryptStateRef::stats, 0))?;
|
213
|
+
class1.define_method("encrypt", method!(CryptStateRef::encrypt, 1))?;
|
214
|
+
class1.define_method("decrypt", method!(CryptStateRef::decrypt, 1))?;
|
161
215
|
|
162
|
-
|
163
|
-
|
216
|
+
let class2 = module.define_class("DecryptResult", class::object())?;
|
217
|
+
class2.define_method("success?", method!(DecryptResult::is_success, 0))?;
|
218
|
+
class2.define_method("data", method!(DecryptResult::data, 0))?;
|
219
|
+
class2.define_method("reason_raw_value", method!(DecryptResult::reason_raw_value, 0))?;
|
164
220
|
|
165
221
|
Ok(())
|
166
222
|
}
|
@@ -1,9 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module RbMumbleProtocol
|
4
|
-
class
|
4
|
+
class CryptState
|
5
5
|
end
|
6
6
|
|
7
|
-
class
|
7
|
+
class DecryptResult
|
8
|
+
REASONS = {
|
9
|
+
0 => :ok,
|
10
|
+
1 => :repeat,
|
11
|
+
2 => :late,
|
12
|
+
3 => :mac,
|
13
|
+
4 => :eof
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
def reason
|
17
|
+
REASONS[reason_raw_value]
|
18
|
+
end
|
8
19
|
end
|
9
20
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb_mumble_protocol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Odebe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|