Class: SelfCrypto::GroupSession

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'ext/self_crypto/omemo.c', line 6

static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE identity;
    GroupSession *this;

    Data_Get_Struct(self, GroupSession, this);

    (void)rb_scan_args(argc, argv, "1", &identity);

    if(identity != Qnil){
        if(rb_obj_is_kind_of(identity, rb_cString) != Qtrue){
            rb_raise(rb_eTypeError, "identity must be kind of String");
        }
    }

    omemo_set_identity(this, RSTRING_PTR(identity));

    return self;
}

Instance Method Details

#add_participant(identity, session) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'ext/self_crypto/omemo.c', line 26

static VALUE add_participant(VALUE self, VALUE identity, VALUE session)
{
    GroupSession *this;
    OlmSession *s;

    Data_Get_Struct(self, GroupSession, this);
    Data_Get_Struct(session, OlmSession, s);

    if(rb_obj_is_kind_of(identity, rb_eval_string("String")) != Qtrue){
        rb_raise(rb_eTypeError, "identity must be kind of String");
    }

    if(
       rb_obj_is_instance_of(session, rb_eval_string("SelfCrypto::Session")) != Qtrue &&
       rb_obj_is_instance_of(session, rb_eval_string("SelfCrypto::InboundSession")) != Qtrue &&
       rb_obj_is_instance_of(session, rb_eval_string("SelfCrypto::OutboundSession")) != Qtrue
     ){
        rb_raise(rb_eTypeError, "session must be an instance of SelfCrypto::Session, SelfCrypto::InboundSession or SelfCrypto::OutboundSession");
    }

    omemo_add_group_participant(this, RSTRING_PTR(identity), s);

    return identity;
}

#decrypt(sender, ciphertext) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
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/omemo.c', line 94

static VALUE group_decrypt(VALUE self, VALUE sender, VALUE ciphertext)
{
    GroupSession *this;
    VALUE plaintext;
    void *ptr;
    size_t plaintext_sz;

    Data_Get_Struct(self, GroupSession, this);

    if(rb_obj_is_kind_of(sender, rb_eval_string("String")) != Qtrue){
        rb_raise(rb_eTypeError, "sender must be kind of String");
    }

    if(rb_obj_is_kind_of(ciphertext, rb_eval_string("String")) != Qtrue){
        rb_raise(rb_eTypeError, "ciphertext must be kind of String");
    }

    plaintext_sz = omemo_decrypted_size(this, RSTRING_PTR(ciphertext), RSTRING_LEN(ciphertext));

    if (plaintext_sz == 0) {
        rb_raise(rb_eTypeError, "could not get size of decrypted message");
    }

    if((ptr = malloc(plaintext_sz)) == NULL){
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    plaintext_sz = omemo_decrypt(
        this,
        RSTRING_PTR(sender),
        ptr,
        plaintext_sz,
        RSTRING_PTR(ciphertext),
        RSTRING_LEN(ciphertext)
    );

    if(plaintext_sz == 0) {
        free(ptr);
        rb_raise(rb_eTypeError, "failed to decrypt");
    }

    plaintext = rb_str_new(ptr, plaintext_sz);

    free(ptr);

    return plaintext;
}

#encrypt(plaintext) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/self_crypto/omemo.c', line 51

static VALUE group_encrypt(VALUE self, VALUE plaintext)
{
    GroupSession *this;
    VALUE ciphertext;
    void *ptr;
    size_t ciphertext_sz;

    Data_Get_Struct(self, GroupSession, this);

    if(rb_obj_is_kind_of(plaintext, rb_eval_string("String")) != Qtrue){
        rb_raise(rb_eTypeError, "plaintext must be kind of String");
    }

    ciphertext_sz = omemo_encrypted_size(this, RSTRING_LEN(plaintext));

    if (ciphertext_sz == 0) {
        rb_raise(rb_eTypeError, "could not get size of encrypted message");
    }

    if((ptr = malloc(ciphertext_sz)) == NULL){
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    ciphertext_sz = omemo_encrypt(
        this,
        RSTRING_PTR(plaintext),
        RSTRING_LEN(plaintext),
        ptr,
        ciphertext_sz
    );

    if(ciphertext_sz == 0) {
        free(ptr);
        rb_raise(rb_eTypeError, "failed to encrypt");
    }

    ciphertext = rb_funcall(rb_eval_string("SelfCrypto::GroupMessage"), rb_intern("new"), 1, rb_str_new(ptr, ciphertext_sz));

    free(ptr);

    return ciphertext;
}