Class: SelfCrypto::Account

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

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
93
94
95
96
97
# File 'ext/self_crypto/account.c', line 11

static VALUE initialize(int argc, VALUE * argv, VALUE self) {
    VALUE pickle, password, seed, opts;
    size_t size;
    OlmAccount * this;

    Data_Get_Struct(self, OlmAccount, this);

    (void) rb_scan_args(argc, argv, "0:", & opts);

    opts = (opts == Qnil) ? rb_hash_new() : opts;

    pickle = rb_hash_aref(opts, ID2SYM(rb_intern("pickle")));
    password = rb_hash_aref(opts, ID2SYM(rb_intern("password")));
    seed = rb_hash_aref(opts, ID2SYM(rb_intern("seed")));

    if (pickle != Qnil) {
        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 (pickle != Qnil) {
        if (self_olm_unpickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == -1) {
            raise_olm_error(self_olm_account_last_error(this));
        }
    } else if (seed != Qnil) {
        uint8_t *ed25519_pk = malloc_or_raise(self_crypto_sign_publickeybytes());
        uint8_t *ed25519_sk = malloc_or_raise(self_crypto_sign_secretkeybytes());
        uint8_t *curve25519_pk = malloc_or_raise(self_crypto_sign_publickeybytes()); // equivalent to crypto_scalarmult_curve25519_BYTES
        uint8_t *curve25519_sk = malloc_or_raise(self_crypto_sign_publickeybytes()); // equivalent to crypto_scalarmult_curve25519_BYTES
        size_t status = self_crypto_sign_seed_keypair(
            ed25519_pk,
            ed25519_sk,
            RSTRING_PTR(seed)
        );

        if (status != 0) {
            raise_olm_error("could not generate key from seed");
        }

        status = self_crypto_sign_ed25519_pk_to_curve25519(
            curve25519_pk,
            ed25519_pk
        );

        if (status != 0) {
            raise_olm_error("could not convert ed25519 public key to curve25519");
        }

        status = self_crypto_sign_ed25519_sk_to_curve25519(
            curve25519_sk,
            ed25519_sk
        );

        if (status != 0) {
            raise_olm_error("could not convert ed25519 secret key to curve25519");
        } 

        status = self_olm_import_account(
            this,
            ed25519_sk,
            ed25519_pk,
            curve25519_sk,
            curve25519_pk
        );

        if (status == -1) {
            raise_olm_error(self_olm_account_last_error(this));
        }
    } else {
        size = self_olm_create_account_random_length(this);

        if (self_olm_create_account(this, RSTRING_PTR(get_random(size)), size) == -1) {
            raise_olm_error(self_olm_account_last_error(this));
        }
    }

    return self;
}

Class Method Details

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

Parameters:

  • pickle (String)

    pickled state

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

    password used to encrypt pickled state

Returns:



10
11
12
# File 'lib/self_crypto/account.rb', line 10

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

.from_seed(seed) ⇒ Object



14
15
16
# File 'lib/self_crypto/account.rb', line 14

def self.from_seed(seed)
  Account.new(seed: Base64.decode64(seed))
end

Instance Method Details

#gen_otk(number = 1) ⇒ Object



18
19
20
# File 'lib/self_crypto/account.rb', line 18

def gen_otk(number=1)
  generate_one_time_keys(number)
end

#generate_one_time_keys(number) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/self_crypto/account.c', line 114

static VALUE generate_one_time_keys(VALUE self, VALUE number) {
    size_t size;
    OlmAccount * this;
    Data_Get_Struct(self, OlmAccount, this);

    size = self_olm_account_generate_one_time_keys_random_length(this, NUM2SIZET(number));

    if (self_olm_account_generate_one_time_keys(this, NUM2SIZET(number), RSTRING_PTR(get_random(size)), size) == -1) {
        raise_olm_error(self_olm_account_last_error(this));
    }

    return self;
}

#identity_keysObject Also known as: ik



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/self_crypto/account.c', line 99

static VALUE identity_keys(VALUE self) {
    OlmAccount * this;
    size_t size;
    Data_Get_Struct(self, OlmAccount, this);

    size = self_olm_account_identity_keys_length(this);
    uint8_t buf[size];

    if (self_olm_account_identity_keys(this, buf, size) != size) {
        raise_olm_error(self_olm_account_last_error(this));
    }

    return rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new((char * ) buf, size));
}

#inbound_session(*args) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
# File 'ext/self_crypto/account.c', line 220

static VALUE inbound_session(int argc, VALUE * argv, VALUE self) {
    VALUE args[] = {
        self,
        Qnil,
        Qnil
    };

    (void) rb_scan_args(argc, argv, "11", & args[1], & args[2]);

    return rb_class_new_instance(sizeof(args) / sizeof( * args), args, rb_eval_string("SelfCrypto::InboundSession"));
}

#last_errorObject



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

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

    return rb_funcall(rb_eval_string("SelfCrypto::OlmError"), rb_intern("from_string"), 1, rb_str_new2((this)));
}

#mark_keys_as_publishedObject Also known as: mark_otk



186
187
188
189
190
191
192
193
# File 'ext/self_crypto/account.c', line 186

static VALUE mark_keys_as_published(VALUE self) {
    OlmAccount * this;
    Data_Get_Struct(self, OlmAccount, this);

    (void) self_olm_account_mark_keys_as_published(this);

    return self;
}

#max_number_of_one_time_keysObject Also known as: max_otk



195
196
197
198
199
200
# File 'ext/self_crypto/account.c', line 195

static VALUE max_number_of_one_time_keys(VALUE self) {
    OlmAccount * this;
    Data_Get_Struct(self, OlmAccount, this);

    return SIZET2NUM((this));
}

#one_time_keysObject Also known as: otk



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'ext/self_crypto/account.c', line 135

static VALUE one_time_keys(VALUE self) {
    VALUE retval;
    size_t size;
    void * ptr;
    OlmAccount * this;
    Data_Get_Struct(self, OlmAccount, this);

    size = self_olm_account_one_time_keys_length(this);

    // add an additional byte to stop this from overflowing
    if ((ptr = malloc(size + 1)) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    if (self_olm_account_one_time_keys(this, ptr, size) != size) {
        free(ptr);
        raise_olm_error(self_olm_account_last_error(this));
    }

    retval = rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new(ptr, size));

    free(ptr);

    return retval;
}

#one_time_keys_sizeObject



128
129
130
131
132
133
# File 'ext/self_crypto/account.c', line 128

static VALUE one_time_keys_size(VALUE self) {
    OlmAccount * this;
    Data_Get_Struct(self, OlmAccount, this);

    return SIZET2NUM((this));
}

#outbound_session(identity_key, pre_key) ⇒ Object



216
217
218
# File 'ext/self_crypto/account.c', line 216

static VALUE outbound_session(VALUE self, VALUE identity_key, VALUE pre_key) {
    return rb_funcall(rb_eval_string("SelfCrypto::OutboundSession"), rb_intern("new"), 3, self, identity_key, pre_key);
}

#remove_one_time_keys(session) ⇒ Object Also known as: update_otk



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'ext/self_crypto/account.c', line 202

static VALUE remove_one_time_keys(VALUE self, VALUE session) {
    OlmAccount * this;
    Data_Get_Struct(self, OlmAccount, this);

    OlmSession * s;
    Data_Get_Struct(session, OlmSession, s);

    if (self_olm_remove_one_time_keys(this, s) == -1) {
        raise_olm_error(self_olm_account_last_error(this));
    }

    return self;
}

#sign(message) ⇒ Object



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/account.c', line 161

static VALUE sign(VALUE self, VALUE message) {
    VALUE retval;
    size_t size;
    void * ptr;
    OlmAccount * this;
    Data_Get_Struct(self, OlmAccount, this);

    size = self_olm_account_signature_length(this);

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

    if (self_olm_account_sign(this, RSTRING_PTR(message), RSTRING_LEN(message), ptr, size) == -1) {
        free(ptr);
        raise_olm_error(self_olm_account_last_error(this));
    }

    retval = rb_str_new(ptr, size);

    free(ptr);

    return retval;
}

#to_pickle(*args) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'ext/self_crypto/account.c', line 232

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

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

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

    size = self_olm_pickle_account_length(this);

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

    if (self_olm_pickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size) {
        free(ptr);
        raise_olm_error(self_olm_account_last_error(this));
    }

    retval = rb_str_new(ptr, size);

    free(ptr);

    return retval;
}