account.cpp 9.91 KB
Newer Older
Mark Haines's avatar
Mark Haines committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Copyright 2015 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
15
#include "olm/account.hh"
16
#include "olm/base64.hh"
17
#include "olm/pickle.hh"
18

19
20
21
22
23
olm::Account::Account(
) : next_one_time_key_id(0),
    last_error(olm::ErrorCode::SUCCESS) {
}

24

25
olm::OneTimeKey const * olm::Account::lookup_key(
26
    olm::Curve25519PublicKey const & public_key
27
) {
28
    for (olm::OneTimeKey const & key : one_time_keys) {
29
30
31
        if (0 == memcmp(key.key.public_key, public_key.public_key, 32)) {
            return &key;
        }
32
33
34
35
    }
    return 0;
}

36
std::size_t olm::Account::remove_key(
37
    olm::Curve25519PublicKey const & public_key
38
) {
39
    OneTimeKey * i;
40
    for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) {
41
42
        if (0 == memcmp(i->key.public_key, public_key.public_key, 32)) {
            std::uint32_t id = i->id;
43
44
45
46
47
48
            one_time_keys.erase(i);
            return id;
        }
    }
    return std::size_t(-1);
}
Mark Haines's avatar
Mark Haines committed
49

50
std::size_t olm::Account::new_account_random_length() {
51
    return 2 * 32;
Mark Haines's avatar
Mark Haines committed
52
53
}

54
std::size_t olm::Account::new_account(
Mark Haines's avatar
Mark Haines committed
55
56
57
    uint8_t const * random, std::size_t random_length
) {
    if (random_length < new_account_random_length()) {
58
        last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
59
        return std::size_t(-1);
Mark Haines's avatar
Mark Haines committed
60
61
    }

62
    olm::ed25519_generate_key(random, identity_keys.ed25519_key);
Mark Haines's avatar
Mark Haines committed
63
    random += 32;
64
    olm::curve25519_generate_key(random, identity_keys.curve25519_key);
Mark Haines's avatar
Mark Haines committed
65
66
67
68
69
    random += 32;

    return 0;
}

70
71
namespace {

72
73
74
75
76
77

namespace {
uint8_t KEY_JSON_ED25519[] = "\"ed25519\":";
uint8_t KEY_JSON_CURVE25519[] = "\"curve25519\":";
}

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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

std::size_t count_digits(
    std::uint64_t value
) {
    std::size_t digits = 0;
    do {
        digits++;
        value /= 10;
    } while (value);
    return digits;
}

template<typename T>
std::uint8_t * write_string(
    std::uint8_t * pos,
    T const & value
) {
    std::memcpy(pos, value, sizeof(T) - 1);
    return pos + (sizeof(T) - 1);
}

std::uint8_t * write_string(
    std::uint8_t * pos,
    std::uint8_t const * value, std::size_t value_length
) {
    std::memcpy(pos, value, value_length);
    return pos + value_length;
}

std::uint8_t * write_digits(
    std::uint8_t * pos,
    std::uint64_t value
) {
    size_t digits = count_digits(value);
    pos += digits;
    do {
        *(--pos) = '0' + (value % 10);
        value /= 10;
    } while (value);
    return pos + digits;
}

}


123
std::size_t olm::Account::get_identity_json_length() {
124
    std::size_t length = 0;
125
126
127
    length += 1; /* { */
    length += sizeof(KEY_JSON_CURVE25519) - 1;
    length += 1; /* " */
128
129
130
    length += olm::encode_base64_length(
        sizeof(identity_keys.curve25519_key.public_key)
    );
131
132
133
    length += 2; /* ", */
    length += sizeof(KEY_JSON_ED25519) - 1;
    length += 1; /* " */
134
135
136
    length += olm::encode_base64_length(
        sizeof(identity_keys.ed25519_key.public_key)
    );
137
    length += 2; /* "} */
138
139
140
141
142
143
144
145
146
    return length;
}


std::size_t olm::Account::get_identity_json(
    std::uint8_t * identity_json, std::size_t identity_json_length
) {
    std::uint8_t * pos = identity_json;
    std::uint8_t signature[64];
147
    size_t expected_length = get_identity_json_length();
148
149
150
151
152
153

    if (identity_json_length < expected_length) {
        last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return std::size_t(-1);
    }

154
155
156
157
158
159
160
    *(pos++) = '{';
    pos = write_string(pos, KEY_JSON_CURVE25519);
    *(pos++) = '\"';
    pos = olm::encode_base64(
        identity_keys.curve25519_key.public_key,
        sizeof(identity_keys.curve25519_key.public_key),
        pos
161
    );
162
163
164
165
166
167
168
169
170
    *(pos++) = '\"'; *(pos++) = ',';
    pos = write_string(pos, KEY_JSON_ED25519);
    *(pos++) = '\"';
    pos = olm::encode_base64(
        identity_keys.ed25519_key.public_key,
        sizeof(identity_keys.ed25519_key.public_key),
        pos
    );
    *(pos++) = '\"'; *(pos++) = '}';
171
172
    return pos - identity_json;
}
Mark Haines's avatar
Mark Haines committed
173

174
175
176
177

std::size_t olm::Account::signature_length(
) {
    return 64;
178
179
}

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

std::size_t olm::Account::sign(
    std::uint8_t const * message, std::size_t message_length,
    std::uint8_t * signature, std::size_t signature_length
) {
    if (signature_length < this->signature_length()) {
        last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return std::size_t(-1);
    }
    olm::ed25519_sign(
        identity_keys.ed25519_key, message, message_length, signature
    );
    return this->signature_length();
}


196
197
198
199
std::size_t olm::Account::get_one_time_keys_json_length(
) {
    std::size_t length = 0;
    for (auto const & key : one_time_keys) {
200
201
202
        if (key.published) {
            continue;
        }
203
204
205
206
207
208
        length += 2; /* {" */
        length += olm::encode_base64_length(olm::pickle_length(key.id));
        length += 3; /* ":" */
        length += olm::encode_base64_length(sizeof(key.key.public_key));
        length += 1; /* " */
    }
209
210
211
    if (length == 0) {
        /* The list was empty. Add a byte for the opening '{' */
        length = 1;
212
    }
213
214
215
    length += 3; /* }{} */
    length += sizeof(KEY_JSON_CURVE25519) - 1;
    return length;
216
217
218
219
220
221
222
223
224
225
226
}


std::size_t olm::Account::get_one_time_keys_json(
    std::uint8_t * one_time_json, std::size_t one_time_json_length
) {
    std::uint8_t * pos = one_time_json;
    if (one_time_json_length < get_one_time_keys_json_length()) {
        last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return std::size_t(-1);
    }
227
228
    *(pos++) = '{';
    pos = write_string(pos, KEY_JSON_CURVE25519);
229
230
    std::uint8_t sep = '{';
    for (auto const & key : one_time_keys) {
231
232
233
        if (key.published) {
            continue;
        }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        *(pos++) = sep;
        *(pos++) = '\"';
        std::uint8_t key_id[olm::pickle_length(key.id)];
        olm::pickle(key_id, key.id);
        pos = olm::encode_base64(key_id, sizeof(key_id), pos);
        *(pos++) = '\"'; *(pos++) = ':'; *(pos++) = '\"';
        pos = olm::encode_base64(
            key.key.public_key, sizeof(key.key.public_key), pos
        );
        *(pos++) = '\"';
        sep = ',';
    }
    if (sep != ',') {
        *(pos++) = sep;
    }
    *(pos++) = '}';
250
    *(pos++) = '}';
251
252
253
254
    return pos - one_time_json;
}


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
290
291
292
293
294
295
296
std::size_t olm::Account::mark_keys_as_published(
) {
    std::size_t count = 0;
    for (auto & key : one_time_keys) {
        if (!key.published) {
            key.published = true;
            count++;
        }
    }
    return count;
}


std::size_t olm::Account::max_number_of_one_time_keys(
) {
    return olm::MAX_ONE_TIME_KEYS;
}

std::size_t olm::Account::generate_one_time_keys_random_length(
    std::size_t number_of_keys
) {
    return 32 * number_of_keys;
}

std::size_t olm::Account::generate_one_time_keys(
    std::size_t number_of_keys,
    std::uint8_t const * random, std::size_t random_length
) {
    if (random_length < generate_one_time_keys_random_length(number_of_keys)) {
        last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
        return std::size_t(-1);
    }
    for (unsigned i = 0; i < number_of_keys; ++i) {
        OneTimeKey & key = *one_time_keys.insert(one_time_keys.begin());
        key.id = ++next_one_time_key_id;
        key.published = false;
        olm::curve25519_generate_key(random, key.key);
        random += 32;
    }
    return number_of_keys;
}

297
namespace olm {
298

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
static std::size_t pickle_length(
    olm::IdentityKeys const & value
) {
    size_t length = 0;
    length += olm::pickle_length(value.ed25519_key);
    length += olm::pickle_length(value.curve25519_key);
    return length;
}


static std::uint8_t * pickle(
    std::uint8_t * pos,
    olm::IdentityKeys const & value
) {
    pos = olm::pickle(pos, value.ed25519_key);
    pos = olm::pickle(pos, value.curve25519_key);
    return pos;
}


static std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
    olm::IdentityKeys & value
) {
    pos = olm::unpickle(pos, end, value.ed25519_key);
    pos = olm::unpickle(pos, end, value.curve25519_key);
    return pos;
}

328
329

static std::size_t pickle_length(
330
    olm::OneTimeKey const & value
331
) {
332
333
334
335
336
    std::size_t length = 0;
    length += olm::pickle_length(value.id);
    length += olm::pickle_length(value.published);
    length += olm::pickle_length(value.key);
    return length;
337
338
339
340
341
}


static std::uint8_t * pickle(
    std::uint8_t * pos,
342
    olm::OneTimeKey const & value
343
) {
344
    pos = olm::pickle(pos, value.id);
345
    pos = olm::pickle(pos, value.published);
346
    pos = olm::pickle(pos, value.key);
347
348
349
350
351
352
    return pos;
}


static std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
353
    olm::OneTimeKey & value
354
) {
355
    pos = olm::unpickle(pos, end, value.id);
356
    pos = olm::unpickle(pos, end, value.published);
357
    pos = olm::unpickle(pos, end, value.key);
358
359
360
    return pos;
}

361
} // namespace olm
362
363


364
365
std::size_t olm::pickle_length(
    olm::Account const & value
366
367
) {
    std::size_t length = 0;
368
    length += olm::pickle_length(value.identity_keys);
369
    length += olm::pickle_length(value.one_time_keys);
370
    length += olm::pickle_length(value.next_one_time_key_id);
371
372
373
374
    return length;
}


375
std::uint8_t * olm::pickle(
376
    std::uint8_t * pos,
377
    olm::Account const & value
378
) {
379
    pos = olm::pickle(pos, value.identity_keys);
380
    pos = olm::pickle(pos, value.one_time_keys);
381
    pos = olm::pickle(pos, value.next_one_time_key_id);
382
383
384
385
    return pos;
}


386
std::uint8_t const * olm::unpickle(
387
    std::uint8_t const * pos, std::uint8_t const * end,
388
    olm::Account & value
389
) {
390
    pos = olm::unpickle(pos, end, value.identity_keys);
391
    pos = olm::unpickle(pos, end, value.one_time_keys);
392
    pos = olm::unpickle(pos, end, value.next_one_time_key_id);
393
394
    return pos;
}