account.hh 3.83 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
16
#ifndef OLM_ACCOUNT_HH_
#define OLM_ACCOUNT_HH_
17

18
19
20
#include "olm/list.hh"
#include "olm/crypto.hh"
#include "olm/error.hh"
21
22
23

#include <cstdint>

24
namespace olm {
25
26


27
28
29
30
31
32
struct IdentityKeys {
    Ed25519KeyPair ed25519_key;
    Curve25519KeyPair curve25519_key;
};

struct OneTimeKey {
33
34
35
36
37
38
39
    std::uint32_t id;
    Curve25519KeyPair key;
};


static std::size_t const MAX_ONE_TIME_KEYS = 100;

40

41
struct Account {
42
43
    IdentityKeys identity_keys;
    List<OneTimeKey, MAX_ONE_TIME_KEYS> one_time_keys;
44
    ErrorCode last_error;
45
46
47
48
49
50

    /** Number of random bytes needed to create a new account */
    std::size_t new_account_random_length();

    /** Create a new account. Returns NOT_ENOUGH_RANDOM if the number of random
     * bytes is too small. */
51
    std::size_t new_account(
52
53
54
        uint8_t const * random, std::size_t random_length
    );

55
56
57
58
59
60
61
62
63
64
65
    /** Number of bytes needed to output the identity keys for this account */
    std::size_t 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
    );

    /** Output the identity keys for this account as JSON in the following
     * format.
     *
66
67
68
     *  14 {"algorithms":
     *  30 ["m.olm.curve25519-aes-sha256"
     *  15 ],"device_id":"
69
     *   ? <device identifier>
70
     *  22 ","keys":{"curve25519:
71
     *   4 <base64 characters>
72
     *   3 ":"
73
     *  43 <base64 characters>
74
     *  11 ","ed25519:
75
     *   4 <base64 characters>
76
     *   3 ":"
77
     *  43 <base64 characters>
78
     *  14 "},"user_id":"
79
     *   ? <user identifier>
80
     *  19 ","valid_after_ts":
81
     *   ? <digits>
82
     *  18 ,"valid_until_ts":
83
     *   ? <digits>
84
     *  16 ,"signatures":{"
85
     *   ? <user identifier>
86
     *   1 /
87
     *   ? <device identifier>
88
     *  12 ":{"ed25519:
89
     *   4 <base64 characters>
90
     *   3 ":"
91
     *  86 <base64 characters>
92
93
94
95
     *   4 "}}}
     *
     * Returns the size of the JSON written or std::size_t(-1) on error.
     * If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL. */
96
97
98
99
100
    std::size_t 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,
        std::uint64_t valid_until_ts,
101
        std::uint8_t * identity_json, std::size_t identity_json_length
102
103
    );

104
105
106
107
108
109
110
111
112
113
114
115
    /** Number of bytes needed to output the one time keys for this account */
    std::size_t get_one_time_keys_json_length();

    /*
     * Returns the size of the JSON written or std::size_t(-1) on error.
     * If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL.
     */
    std::size_t get_one_time_keys_json(
        std::uint8_t * one_time_json, std::size_t one_time_json_length
    );

    /** Lookup a one_time key with the given key-id */
116
    OneTimeKey const * lookup_key(
117
        Curve25519PublicKey const & public_key
118
    );
119
120
121
122

    std::size_t remove_key(
        std::uint32_t id
    );
123
};
124

125

126
127
128
std::size_t pickle_length(
    Account const & value
);
129

130
131
132
133
134
135
136
137
138
139
140

std::uint8_t * pickle(
    std::uint8_t * pos,
    Account const & value
);


std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
    Account & value
);
141
142


143
} // namespace olm
144

145
#endif /* OLM_ACCOUNT_HH_ */