account.cpp 11 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
olm::OneTimeKey const * olm::Account::lookup_key(
21
    olm::Curve25519PublicKey const & public_key
22
) {
23
    for (olm::OneTimeKey const & key : one_time_keys) {
24
25
26
        if (0 == memcmp(key.key.public_key, public_key.public_key, 32)) {
            return &key;
        }
27
28
29
30
    }
    return 0;
}

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

45
std::size_t olm::Account::new_account_random_length() {
46
    return 12 * 32;
Mark Haines's avatar
Mark Haines committed
47
48
}

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

    unsigned id = 0;

59
    olm::ed25519_generate_key(random, identity_keys.ed25519_key);
Mark Haines's avatar
Mark Haines committed
60
    random += 32;
61
    olm::curve25519_generate_key(random, identity_keys.curve25519_key);
Mark Haines's avatar
Mark Haines committed
62
63
    random += 32;

64
    for (unsigned i = 0; i < 10; ++i) {
65
        OneTimeKey & key = *one_time_keys.insert(one_time_keys.end());
Mark Haines's avatar
Mark Haines committed
66
        key.id = ++id;
67
        olm::curve25519_generate_key(random, key.key);
Mark Haines's avatar
Mark Haines committed
68
69
70
71
72
73
        random += 32;
    }

    return 0;
}

74
75
76
77
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
namespace {

static const uint8_t IDENTITY_JSON_PART_0[] =
    "{\"algorithms\":"
    "[\"m.olm.curve25519-aes-sha256\""
    "],\"device_id\":\"";
static const uint8_t IDENTITY_JSON_PART_1[] = "\",\"keys\":{\"curve25519:";
static const uint8_t IDENTITY_JSON_PART_2[] = "\":\"";
static const uint8_t IDENTITY_JSON_PART_3[] = "\",\"ed25519:";
static const uint8_t IDENTITY_JSON_PART_4[] = "\":\"";
static const uint8_t IDENTITY_JSON_PART_5[] = "\"},\"user_id\":\"";
static const uint8_t IDENTITY_JSON_PART_6[] = "\",\"valid_after_ts\":";
static const uint8_t IDENTITY_JSON_PART_7[] = ",\"valid_until_ts\":";
static const uint8_t IDENTITY_JSON_PART_8[] = ",\"signatures\":{\"";
static const uint8_t IDENTITY_JSON_PART_9[] = "/";
static const uint8_t IDENTITY_JSON_PART_A[] = "\":{\"ed25519:";
static const uint8_t IDENTITY_JSON_PART_B[] = "\":\"";
static const uint8_t IDENTITY_JSON_PART_C[] = "\"}}}";

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;
}

}


std::size_t olm::Account::get_identity_json_length(
    std::size_t user_id_length,
    std::size_t device_id_length,
    std::uint64_t valid_after_ts,
    std::uint64_t valid_until_ts
) {
    std::size_t length = 0;
    length += sizeof(IDENTITY_JSON_PART_0) - 1;
    length += device_id_length;
    length += sizeof(IDENTITY_JSON_PART_1) - 1;
147
    length += olm::encode_base64_length(3);
148
    length += sizeof(IDENTITY_JSON_PART_2) - 1;
149
150
151
    length += olm::encode_base64_length(
        sizeof(identity_keys.curve25519_key.public_key)
    );
152
    length += sizeof(IDENTITY_JSON_PART_3) - 1;
153
    length += olm::encode_base64_length(3);
154
    length += sizeof(IDENTITY_JSON_PART_4) - 1;
155
156
157
    length += olm::encode_base64_length(
        sizeof(identity_keys.ed25519_key.public_key)
    );
158
159
160
161
162
163
164
165
166
167
168
    length += sizeof(IDENTITY_JSON_PART_5) - 1;
    length += user_id_length;
    length += sizeof(IDENTITY_JSON_PART_6) - 1;
    length += count_digits(valid_after_ts);
    length += sizeof(IDENTITY_JSON_PART_7) - 1;
    length += count_digits(valid_until_ts);
    length += sizeof(IDENTITY_JSON_PART_8) - 1;
    length += user_id_length;
    length += sizeof(IDENTITY_JSON_PART_9) - 1;
    length += device_id_length;
    length += sizeof(IDENTITY_JSON_PART_A) - 1;
169
    length += olm::encode_base64_length(3);
170
    length += sizeof(IDENTITY_JSON_PART_B) - 1;
171
    length += olm::encode_base64_length(64);
172
173
174
175
176
177
178
179
180
    length += sizeof(IDENTITY_JSON_PART_C) - 1;
    return length;
}


std::size_t olm::Account::get_identity_json(
    std::uint8_t const * user_id, std::size_t user_id_length,
    std::uint8_t const * device_id, std::size_t device_id_length,
    std::uint64_t valid_after_ts,
181
    std::uint64_t valid_until_ts,
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    std::uint8_t * identity_json, std::size_t identity_json_length
) {
    std::uint8_t * pos = identity_json;
    std::uint8_t signature[64];
    size_t expected_length = get_identity_json_length(
            user_id_length, device_id_length, valid_after_ts, valid_until_ts
    );

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

    pos = write_string(pos, IDENTITY_JSON_PART_0);
    pos = write_string(pos, device_id, device_id_length);
    pos = write_string(pos, IDENTITY_JSON_PART_1);
198
    pos = encode_base64(identity_keys.curve25519_key.public_key, 3, pos);
199
    pos = write_string(pos, IDENTITY_JSON_PART_2);
200
    pos = encode_base64(identity_keys.curve25519_key.public_key, 32, pos);
201
    pos = write_string(pos, IDENTITY_JSON_PART_3);
202
    pos = encode_base64(identity_keys.ed25519_key.public_key, 3, pos);
203
    pos = write_string(pos, IDENTITY_JSON_PART_4);
204
    pos = encode_base64(identity_keys.ed25519_key.public_key, 32, pos);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    pos = write_string(pos, IDENTITY_JSON_PART_5);
    pos = write_string(pos, user_id, user_id_length);
    pos = write_string(pos, IDENTITY_JSON_PART_6);
    pos = write_digits(pos, valid_after_ts);
    pos = write_string(pos, IDENTITY_JSON_PART_7);
    pos = write_digits(pos, valid_until_ts);
    *pos = '}';
    // Sign the JSON up to written up to this point.
    ed25519_sign(
        identity_keys.ed25519_key,
        identity_json, 1 + pos - identity_json,
        signature
    );
    // Append the signature to the end of the JSON.
    pos = write_string(pos, IDENTITY_JSON_PART_8);
    pos = write_string(pos, user_id, user_id_length);
    pos = write_string(pos, IDENTITY_JSON_PART_9);
    pos = write_string(pos, device_id, device_id_length);
    pos = write_string(pos, IDENTITY_JSON_PART_A);
224
    pos = encode_base64(identity_keys.ed25519_key.public_key, 3, pos);
225
    pos = write_string(pos, IDENTITY_JSON_PART_B);
226
    pos = encode_base64(signature, 64, pos);
227
228
229
    pos = write_string(pos, IDENTITY_JSON_PART_C);
    return pos - identity_json;
}
Mark Haines's avatar
Mark Haines committed
230

231
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
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
namespace {
uint8_t ONE_TIME_KEY_JSON_ALG[] = "curve25519";
}

std::size_t olm::Account::get_one_time_keys_json_length(
) {
    std::size_t length = 0;
    for (auto const & key : one_time_keys) {
        length += 2; /* {" */
        length += sizeof(ONE_TIME_KEY_JSON_ALG) - 1;
        length += 1; /* : */
        length += olm::encode_base64_length(olm::pickle_length(key.id));
        length += 3; /* ":" */
        length += olm::encode_base64_length(sizeof(key.key.public_key));
        length += 1; /* " */
    }
    if (length) {
        return length + 1; /* } */
    } else {
        return 2; /* {} */
    }
}


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);
    }
    std::uint8_t sep = '{';
    for (auto const & key : one_time_keys) {
        *(pos++) = sep;
        *(pos++) = '\"';
        pos = write_string(pos, ONE_TIME_KEY_JSON_ALG);
        *(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++) = '}';
    return pos - one_time_json;
}


287
namespace olm {
288

289
290
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
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;
}

318
319

static std::size_t pickle_length(
320
    olm::OneTimeKey const & value
321
) {
322
    return olm::pickle_length(value.id) + olm::pickle_length(value.key);
323
324
325
326
327
}


static std::uint8_t * pickle(
    std::uint8_t * pos,
328
    olm::OneTimeKey const & value
329
) {
330
331
    pos = olm::pickle(pos, value.id);
    pos = olm::pickle(pos, value.key);
332
333
334
335
336
337
    return pos;
}


static std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
338
    olm::OneTimeKey & value
339
) {
340
341
    pos = olm::unpickle(pos, end, value.id);
    pos = olm::unpickle(pos, end, value.key);
342
343
344
    return pos;
}

345
} // namespace olm
346
347


348
349
std::size_t olm::pickle_length(
    olm::Account const & value
350
351
) {
    std::size_t length = 0;
352
    length += olm::pickle_length(value.identity_keys);
353
    length += olm::pickle_length(value.one_time_keys);
354
355
356
357
    return length;
}


358
std::uint8_t * olm::pickle(
359
    std::uint8_t * pos,
360
    olm::Account const & value
361
) {
362
    pos = olm::pickle(pos, value.identity_keys);
363
    pos = olm::pickle(pos, value.one_time_keys);
364
365
366
367
    return pos;
}


368
std::uint8_t const * olm::unpickle(
369
    std::uint8_t const * pos, std::uint8_t const * end,
370
    olm::Account & value
371
) {
372
    pos = olm::unpickle(pos, end, value.identity_keys);
373
    pos = olm::unpickle(pos, end, value.one_time_keys);
374
375
    return pos;
}