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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'ext/self_crypto/session.c', line 11

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 (self_olm_unpickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == -1) {
        raise_olm_error(self_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

#last_errorObject



4
5
6
7
8
9
# 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(self_olm_session_last_error(this));
}

#to_pickle(*args) ⇒ Object



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
185
186
187
# File 'ext/self_crypto/session.c', line 158

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 = self_olm_pickle_session_length(this);

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

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

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

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

    retval = rb_str_new(ptr, size);

    free(ptr);

    return retval;
}

#will_receive?(*args) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/self_crypto/session.c', line 107

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 = self_olm_matches_inbound_session(this,
            RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
        );
    } else {
        result = self_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 == -1) {

        raise_olm_error(self_olm_session_last_error(this));
    }

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