Class: SelfCrypto::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/self_crypto/session.rb,
ext/self_crypto/session.c

Direct Known Subclasses

InboundSession, OutboundSession

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'ext/self_crypto/session.c', line 12

static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE pickle, password;
    OlmSession *this;
    Data_Get_Struct(self, OlmSession, this);

    (void)rb_scan_args(argc, argv, "11", &pickle, &password);

    if(rb_obj_is_kind_of(pickle, rb_cString) != Qtrue){

        rb_raise(rb_eTypeError, "pickle must be kind of String");
    }

    if(password != Qnil){

        if(rb_obj_is_kind_of(password, rb_cString) != Qtrue){

            rb_raise(rb_eTypeError, "password must be kind of String");
        }
    }
    else{

        password = rb_str_new2("");
    }

    if(olm_unpickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == olm_error()){

        raise_olm_error(olm_session_last_error(this));
    }

    return self;
}

Class Method Details

.from_pickle(pickle, password = "") ⇒ Session

Parameters:

  • pickle (String)

    pickled state

  • password (String) (defaults to: "")

    password used to encrypt pickled state

Returns:



8
9
10
# File 'lib/self_crypto/session.rb', line 8

def self.from_pickle(pickle, password="")
  Session.new(pickle, password)
end

Instance Method Details

#decrypt(cipher) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/self_crypto/session.c', line 241

static VALUE session_decrypt(VALUE self, VALUE cipher)
{
    size_t plain_size, plain_max, type;
    void *ptr;
    OlmSession *this;
    VALUE retval, data;
    Data_Get_Struct(self, OlmSession, this);

    if(rb_obj_is_kind_of(cipher, rb_eval_string("SelfCrypto::Message")) == Qtrue){

        type = OLM_MESSAGE_TYPE_MESSAGE;
    }
    else if(rb_obj_is_kind_of(cipher, rb_eval_string("SelfCrypto::PreKeyMessage")) == Qtrue){

        type = OLM_MESSAGE_TYPE_PRE_KEY;
    }
    else{

        rb_raise(rb_eTypeError, "cipher must be kind of Message or PreKeyMessage");
    }

    data = rb_funcall(cipher, rb_intern("to_s"), 0);

    if((plain_max = olm_decrypt_max_plaintext_length(this, type, RSTRING_PTR(dup_string(data)), RSTRING_LEN(data))) == olm_error()){

        raise_olm_error(olm_session_last_error(this));
    }

    /* size of output will be less than size of input */
    if((ptr = malloc(plain_max)) == NULL){

        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    if((plain_size = olm_decrypt(this,
            type,
            RSTRING_PTR(dup_string(data)), RSTRING_LEN(data),
            ptr, plain_max)) == olm_error())
    {
        free(ptr);
        raise_olm_error(olm_session_last_error(this));
    }

    retval = rb_str_new(ptr, plain_size);

    free(ptr);

    return retval;
}

#encrypt(plain) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/self_crypto/session.c', line 208

static VALUE session_encrypt(VALUE self, VALUE plain)
{
    size_t cipher_size, random_size;
    void *ptr;
    OlmSession *this;
    VALUE retval, type;
    Data_Get_Struct(self, OlmSession, this);

    cipher_size = olm_encrypt_message_length(this, RSTRING_LEN(plain));
    random_size = olm_encrypt_random_length(this);

    if((ptr = malloc(cipher_size)) == NULL){

        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    type = message_type(self);

    if(olm_encrypt(this, RSTRING_PTR(plain), RSTRING_LEN(plain),
            RSTRING_PTR(get_random(random_size)), random_size,
            ptr, cipher_size
            ) == olm_error()){
        free(ptr);
        raise_olm_error(olm_session_last_error(this));
    }

    retval = rb_funcall(type, rb_intern("new"), 1, rb_str_new(ptr, cipher_size));

    free(ptr);

    return retval;
}

#has_received_messageObject Also known as: has_received?



122
123
124
125
126
127
128
# File 'ext/self_crypto/session.c', line 122

static VALUE has_received_message(VALUE self)
{
    OlmSession *this;
    Data_Get_Struct(self, OlmSession, this);

    return (olm_session_has_received_message(this) == 0) ? Qfalse : Qtrue;
}

#idObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/self_crypto/session.c', line 130

static VALUE get_session_id(VALUE self)
{
    OlmSession *this;
    Data_Get_Struct(self, OlmSession, this);

    size_t size = olm_session_id_length(this);
    uint8_t buf[size];

    if(olm_session_id(this, buf, size) != size){

        raise_olm_error(olm_session_last_error(this));
    }

    return rb_str_new((char *)buf, size);
}

#last_errorObject



4
5
6
7
8
9
10
# File 'ext/self_crypto/session.c', line 4

static VALUE last_error(VALUE self)
{
    OlmSession *this;
    Data_Get_Struct(self, OlmSession, this);

    return rb_str_new2(olm_session_last_error(this));
}

#to_pickle(*args) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/self_crypto/session.c', line 291

static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
{
    VALUE password, retval;
    OlmSession *this;
    void *ptr;
    size_t size;
    Data_Get_Struct(self, OlmSession, this);

    (void)rb_scan_args(argc, argv, "01", &password);

    password = (password == Qnil) ? rb_str_new2("") : password;

    size = olm_pickle_session_length(this);

    if((ptr = malloc(size)) == NULL){

        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    if(olm_pickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size){

        free(ptr);
        raise_olm_error(olm_session_last_error(this));
    }

    retval = rb_str_new(ptr, size);

    free(ptr);

    return retval;
}

#will_receive?(*args) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/self_crypto/session.c', line 146

static VALUE will_receive(int argc, VALUE *argv, VALUE self)
{
    VALUE one_time_message, identity;
    size_t result;
    OlmSession *this;
    Data_Get_Struct(self, OlmSession, this);

    identity = Qnil;

    (void)rb_scan_args(argc, argv, "11", &one_time_message, &identity);

    if(rb_obj_is_kind_of(one_time_message, rb_eval_string("SelfCrypto::PreKeyMessage")) != Qtrue){

        rb_raise(rb_eTypeError, "one_time_message must be kind of PreKeyMessage");
    }

    one_time_message = rb_funcall(one_time_message, rb_intern("to_s"), 0);

    if(identity == Qnil){

        result = olm_matches_inbound_session(this,
                RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
        );
    }
    else{

        result = olm_matches_inbound_session_from(this,
                RSTRING_PTR(identity), RSTRING_LEN(identity),
                RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
        );
    }

    if(result == olm_error()){

        raise_olm_error(olm_session_last_error(this));
    }

    return (result == 1) ? Qtrue : Qfalse;
}