2015-06-22 18:50:56 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2016-05-17 13:13:57 +02:00
|
|
|
#include "olm/olm.h"
|
2015-06-27 01:15:23 +02:00
|
|
|
#include "olm/session.hh"
|
|
|
|
#include "olm/account.hh"
|
2016-05-16 17:25:09 +02:00
|
|
|
#include "olm/cipher.h"
|
2016-05-17 19:48:16 +02:00
|
|
|
#include "olm/pickle_encoding.h"
|
2015-07-24 15:29:52 +02:00
|
|
|
#include "olm/utility.hh"
|
2015-06-27 01:15:23 +02:00
|
|
|
#include "olm/base64.hh"
|
2015-07-10 19:29:14 +02:00
|
|
|
#include "olm/memory.hh"
|
2015-06-15 18:49:20 +02:00
|
|
|
|
|
|
|
#include <new>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
static OlmAccount * to_c(olm::Account * account) {
|
|
|
|
return reinterpret_cast<OlmAccount *>(account);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:29:52 +02:00
|
|
|
static OlmSession * to_c(olm::Session * session) {
|
|
|
|
return reinterpret_cast<OlmSession *>(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OlmUtility * to_c(olm::Utility * utility) {
|
|
|
|
return reinterpret_cast<OlmUtility *>(utility);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
static olm::Account * from_c(OlmAccount * account) {
|
|
|
|
return reinterpret_cast<olm::Account *>(account);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:29:52 +02:00
|
|
|
static olm::Session * from_c(OlmSession * session) {
|
|
|
|
return reinterpret_cast<olm::Session *>(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
static olm::Utility * from_c(OlmUtility * utility) {
|
|
|
|
return reinterpret_cast<olm::Utility *>(utility);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::uint8_t * from_c(void * bytes) {
|
|
|
|
return reinterpret_cast<std::uint8_t *>(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::uint8_t const * from_c(void const * bytes) {
|
|
|
|
return reinterpret_cast<std::uint8_t const *>(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t b64_output_length(
|
|
|
|
size_t raw_length
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
return olm::encode_base64_length(raw_length);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::uint8_t * b64_output_pos(
|
|
|
|
std::uint8_t * output,
|
|
|
|
size_t raw_length
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
return output + olm::encode_base64_length(raw_length) - raw_length;
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t b64_output(
|
|
|
|
std::uint8_t * output, size_t raw_length
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t base64_length = olm::encode_base64_length(raw_length);
|
2015-06-15 18:49:20 +02:00
|
|
|
std::uint8_t * raw_output = output + base64_length - raw_length;
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::encode_base64(raw_output, raw_length, output);
|
2015-06-15 18:49:20 +02:00
|
|
|
return base64_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t b64_input(
|
|
|
|
std::uint8_t * input, size_t b64_length,
|
2016-05-16 13:08:45 +02:00
|
|
|
OlmErrorCode & last_error
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
std::size_t raw_length = olm::decode_base64_length(b64_length);
|
2015-06-15 18:49:20 +02:00
|
|
|
if (raw_length == std::size_t(-1)) {
|
2016-05-16 13:08:45 +02:00
|
|
|
last_error = OlmErrorCode::OLM_INVALID_BASE64;
|
2015-06-15 18:49:20 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::decode_base64(input, b64_length, input);
|
2015-06-15 18:49:20 +02:00
|
|
|
return raw_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2016-09-01 14:35:23 +02:00
|
|
|
void olm_get_library_version(uint8_t *major, uint8_t *minor, uint8_t *patch) {
|
|
|
|
if (major != NULL) *major = OLMLIB_VERSION_MAJOR;
|
|
|
|
if (minor != NULL) *minor = OLMLIB_VERSION_MINOR;
|
|
|
|
if (patch != NULL) *patch = OLMLIB_VERSION_PATCH;
|
|
|
|
}
|
2015-06-15 18:49:20 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_error() {
|
2015-06-15 18:49:20 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
const char * olm_account_last_error(
|
2015-07-15 14:24:26 +02:00
|
|
|
OlmAccount * account
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2016-05-16 17:47:49 +02:00
|
|
|
auto error = from_c(account)->last_error;
|
|
|
|
return _olm_error_to_string(error);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
const char * olm_session_last_error(
|
|
|
|
OlmSession * session
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2016-05-16 17:47:49 +02:00
|
|
|
auto error = from_c(session)->last_error;
|
|
|
|
return _olm_error_to_string(error);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:29:52 +02:00
|
|
|
const char * olm_utility_last_error(
|
|
|
|
OlmUtility * utility
|
|
|
|
) {
|
2016-05-16 17:47:49 +02:00
|
|
|
auto error = from_c(utility)->last_error;
|
|
|
|
return _olm_error_to_string(error);
|
2015-07-24 15:29:52 +02:00
|
|
|
}
|
2015-06-15 18:49:20 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_account_size() {
|
|
|
|
return sizeof(olm::Account);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_session_size() {
|
|
|
|
return sizeof(olm::Session);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:29:52 +02:00
|
|
|
size_t olm_utility_size() {
|
|
|
|
return sizeof(olm::Utility);
|
|
|
|
}
|
2015-06-15 18:49:20 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
OlmAccount * olm_account(
|
2015-06-15 18:49:20 +02:00
|
|
|
void * memory
|
|
|
|
) {
|
2015-07-10 19:29:14 +02:00
|
|
|
olm::unset(memory, sizeof(olm::Account));
|
2015-06-27 01:15:23 +02:00
|
|
|
return to_c(new(memory) olm::Account());
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
OlmSession * olm_session(
|
2015-06-15 18:49:20 +02:00
|
|
|
void * memory
|
|
|
|
) {
|
2015-07-10 19:29:14 +02:00
|
|
|
olm::unset(memory, sizeof(olm::Session));
|
2015-06-27 01:15:23 +02:00
|
|
|
return to_c(new(memory) olm::Session());
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-24 15:29:52 +02:00
|
|
|
OlmUtility * olm_utility(
|
|
|
|
void * memory
|
|
|
|
) {
|
|
|
|
olm::unset(memory, sizeof(olm::Utility));
|
|
|
|
return to_c(new(memory) olm::Utility());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-10 19:29:14 +02:00
|
|
|
size_t olm_clear_account(
|
2015-07-15 14:24:26 +02:00
|
|
|
OlmAccount * account
|
2015-07-10 19:29:14 +02:00
|
|
|
) {
|
|
|
|
/* Clear the memory backing the account */
|
|
|
|
olm::unset(account, sizeof(olm::Account));
|
|
|
|
/* Initialise a fresh account object in case someone tries to use it */
|
|
|
|
new(account) olm::Account();
|
|
|
|
return sizeof(olm::Account);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_clear_session(
|
|
|
|
OlmSession * session
|
|
|
|
) {
|
|
|
|
/* Clear the memory backing the session */
|
|
|
|
olm::unset(session, sizeof(olm::Session));
|
|
|
|
/* Initialise a fresh session object in case someone tries to use it */
|
|
|
|
new(session) olm::Session();
|
|
|
|
return sizeof(olm::Session);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-24 15:29:52 +02:00
|
|
|
size_t olm_clear_utility(
|
|
|
|
OlmUtility * utility
|
|
|
|
) {
|
|
|
|
/* Clear the memory backing the session */
|
|
|
|
olm::unset(utility, sizeof(olm::Utility));
|
|
|
|
/* Initialise a fresh session object in case someone tries to use it */
|
|
|
|
new(utility) olm::Utility();
|
|
|
|
return sizeof(olm::Utility);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_pickle_account_length(
|
|
|
|
OlmAccount * account
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2016-05-17 19:48:16 +02:00
|
|
|
return _olm_enc_output_length(pickle_length(*from_c(account)));
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_pickle_session_length(
|
|
|
|
OlmSession * session
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2016-05-17 19:48:16 +02:00
|
|
|
return _olm_enc_output_length(pickle_length(*from_c(session)));
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_pickle_account(
|
|
|
|
OlmAccount * account,
|
2015-06-15 18:49:20 +02:00
|
|
|
void const * key, size_t key_length,
|
|
|
|
void * pickled, size_t pickled_length
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::Account & object = *from_c(account);
|
2015-06-15 18:49:20 +02:00
|
|
|
std::size_t raw_length = pickle_length(object);
|
2016-05-17 19:48:16 +02:00
|
|
|
if (pickled_length < _olm_enc_output_length(raw_length)) {
|
2016-05-16 13:08:45 +02:00
|
|
|
object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-06-15 18:49:20 +02:00
|
|
|
return size_t(-1);
|
|
|
|
}
|
2016-05-17 19:48:16 +02:00
|
|
|
pickle(_olm_enc_output_pos(from_c(pickled), raw_length), object);
|
|
|
|
return _olm_enc_output(from_c(key), key_length, from_c(pickled), raw_length);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_pickle_session(
|
|
|
|
OlmSession * session,
|
2015-06-15 18:49:20 +02:00
|
|
|
void const * key, size_t key_length,
|
|
|
|
void * pickled, size_t pickled_length
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::Session & object = *from_c(session);
|
2015-06-15 18:49:20 +02:00
|
|
|
std::size_t raw_length = pickle_length(object);
|
2016-05-17 19:48:16 +02:00
|
|
|
if (pickled_length < _olm_enc_output_length(raw_length)) {
|
2016-05-16 13:08:45 +02:00
|
|
|
object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-06-15 18:49:20 +02:00
|
|
|
return size_t(-1);
|
|
|
|
}
|
2016-05-17 19:48:16 +02:00
|
|
|
pickle(_olm_enc_output_pos(from_c(pickled), raw_length), object);
|
|
|
|
return _olm_enc_output(from_c(key), key_length, from_c(pickled), raw_length);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_unpickle_account(
|
|
|
|
OlmAccount * account,
|
2015-06-15 18:49:20 +02:00
|
|
|
void const * key, size_t key_length,
|
|
|
|
void * pickled, size_t pickled_length
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::Account & object = *from_c(account);
|
2015-06-15 18:49:20 +02:00
|
|
|
std::uint8_t * const pos = from_c(pickled);
|
2016-05-17 19:48:16 +02:00
|
|
|
std::size_t raw_length = _olm_enc_input(
|
|
|
|
from_c(key), key_length, pos, pickled_length, &object.last_error
|
2015-06-15 18:49:20 +02:00
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
std::uint8_t * const end = pos + raw_length;
|
2015-07-10 19:00:18 +02:00
|
|
|
/* On success unpickle will return (pos + raw_length). If unpickling
|
|
|
|
* terminates too soon then it will return a pointer before
|
|
|
|
* (pos + raw_length). On error unpickle will return (pos + raw_length + 1).
|
|
|
|
*/
|
|
|
|
if (end != unpickle(pos, end + 1, object)) {
|
2016-05-16 13:08:45 +02:00
|
|
|
if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
|
|
|
|
object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
|
2015-07-10 19:00:18 +02:00
|
|
|
}
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-06-15 18:49:20 +02:00
|
|
|
return pickled_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_unpickle_session(
|
|
|
|
OlmSession * session,
|
2015-06-15 18:49:20 +02:00
|
|
|
void const * key, size_t key_length,
|
|
|
|
void * pickled, size_t pickled_length
|
|
|
|
) {
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::Session & object = *from_c(session);
|
2015-06-15 18:49:20 +02:00
|
|
|
std::uint8_t * const pos = from_c(pickled);
|
2016-05-17 19:48:16 +02:00
|
|
|
std::size_t raw_length = _olm_enc_input(
|
|
|
|
from_c(key), key_length, pos, pickled_length, &object.last_error
|
2015-06-15 18:49:20 +02:00
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-07-10 19:00:18 +02:00
|
|
|
|
2015-07-14 12:32:11 +02:00
|
|
|
std::uint8_t * const end = pos + raw_length;
|
2015-07-10 19:00:18 +02:00
|
|
|
/* On success unpickle will return (pos + raw_length). If unpickling
|
|
|
|
* terminates too soon then it will return a pointer before
|
|
|
|
* (pos + raw_length). On error unpickle will return (pos + raw_length + 1).
|
|
|
|
*/
|
|
|
|
if (end != unpickle(pos, end + 1, object)) {
|
2016-05-16 13:08:45 +02:00
|
|
|
if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
|
|
|
|
object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
|
2015-07-10 19:00:18 +02:00
|
|
|
}
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-06-15 18:49:20 +02:00
|
|
|
return pickled_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_create_account_random_length(
|
|
|
|
OlmAccount * account
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
|
|
|
return from_c(account)->new_account_random_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_create_account(
|
|
|
|
OlmAccount * account,
|
2015-07-10 19:29:14 +02:00
|
|
|
void * random, size_t random_length
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2015-07-10 19:29:14 +02:00
|
|
|
size_t result = from_c(account)->new_account(from_c(random), random_length);
|
|
|
|
olm::unset(random, random_length);
|
|
|
|
return result;
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_account_identity_keys_length(
|
2015-07-10 12:57:53 +02:00
|
|
|
OlmAccount * account
|
|
|
|
) {
|
|
|
|
return from_c(account)->get_identity_json_length();
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_account_identity_keys(
|
|
|
|
OlmAccount * account,
|
2015-06-15 18:49:20 +02:00
|
|
|
void * identity_keys, size_t identity_key_length
|
|
|
|
) {
|
2015-07-07 17:42:03 +02:00
|
|
|
return from_c(account)->get_identity_json(
|
|
|
|
from_c(identity_keys), identity_key_length
|
|
|
|
);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
size_t olm_account_signature_length(
|
|
|
|
OlmAccount * account
|
|
|
|
) {
|
|
|
|
return b64_output_length(from_c(account)->signature_length());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_account_sign(
|
|
|
|
OlmAccount * account,
|
|
|
|
void const * message, size_t message_length,
|
|
|
|
void * signature, size_t signature_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = from_c(account)->signature_length();
|
|
|
|
if (signature_length < b64_output_length(raw_length)) {
|
|
|
|
from_c(account)->last_error =
|
2016-05-16 13:08:45 +02:00
|
|
|
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-07-10 12:57:53 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
from_c(account)->sign(
|
|
|
|
from_c(message), message_length,
|
|
|
|
b64_output_pos(from_c(signature), raw_length), raw_length
|
|
|
|
);
|
|
|
|
return b64_output(from_c(signature), raw_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_account_one_time_keys_length(
|
|
|
|
OlmAccount * account
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2015-07-08 16:30:34 +02:00
|
|
|
return from_c(account)->get_one_time_keys_json_length();
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_account_one_time_keys(
|
|
|
|
OlmAccount * account,
|
2015-07-08 16:30:34 +02:00
|
|
|
void * one_time_keys_json, size_t one_time_key_json_length
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2015-07-08 16:30:34 +02:00
|
|
|
return from_c(account)->get_one_time_keys_json(
|
|
|
|
from_c(one_time_keys_json), one_time_key_json_length
|
|
|
|
);
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-09 19:35:54 +02:00
|
|
|
size_t olm_account_mark_keys_as_published(
|
|
|
|
OlmAccount * account
|
|
|
|
) {
|
|
|
|
return from_c(account)->mark_keys_as_published();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_account_max_number_of_one_time_keys(
|
|
|
|
OlmAccount * account
|
|
|
|
) {
|
|
|
|
return from_c(account)->max_number_of_one_time_keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_account_generate_one_time_keys_random_length(
|
|
|
|
OlmAccount * account,
|
|
|
|
size_t number_of_keys
|
|
|
|
) {
|
|
|
|
return from_c(account)->generate_one_time_keys_random_length(number_of_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_account_generate_one_time_keys(
|
|
|
|
OlmAccount * account,
|
|
|
|
size_t number_of_keys,
|
2015-07-10 19:29:14 +02:00
|
|
|
void * random, size_t random_length
|
2015-07-09 19:35:54 +02:00
|
|
|
) {
|
2015-07-10 19:29:14 +02:00
|
|
|
size_t result = from_c(account)->generate_one_time_keys(
|
2015-07-09 19:35:54 +02:00
|
|
|
number_of_keys,
|
|
|
|
from_c(random), random_length
|
|
|
|
);
|
2015-07-10 19:29:14 +02:00
|
|
|
olm::unset(random, random_length);
|
|
|
|
return result;
|
2015-07-09 19:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_create_outbound_session_random_length(
|
|
|
|
OlmSession * session
|
2015-06-16 16:15:40 +02:00
|
|
|
) {
|
|
|
|
return from_c(session)->new_outbound_session_random_length();
|
|
|
|
}
|
|
|
|
|
2015-07-09 19:35:54 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_create_outbound_session(
|
|
|
|
OlmSession * session,
|
|
|
|
OlmAccount * account,
|
2015-06-15 18:49:20 +02:00
|
|
|
void const * their_identity_key, size_t their_identity_key_length,
|
|
|
|
void const * their_one_time_key, size_t their_one_time_key_length,
|
2015-07-10 19:29:14 +02:00
|
|
|
void * random, size_t random_length
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2015-08-19 18:18:09 +02:00
|
|
|
std::uint8_t const * id_key = from_c(their_identity_key);
|
|
|
|
std::uint8_t const * ot_key = from_c(their_one_time_key);
|
|
|
|
std::size_t id_key_length = their_identity_key_length;
|
|
|
|
std::size_t ot_key_length = their_one_time_key_length;
|
|
|
|
|
2016-09-02 16:11:14 +02:00
|
|
|
if (olm::decode_base64_length(id_key_length) != CURVE25519_KEY_LENGTH
|
|
|
|
|| olm::decode_base64_length(ot_key_length) != CURVE25519_KEY_LENGTH
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
2016-05-16 13:08:45 +02:00
|
|
|
from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
|
2015-06-15 18:49:20 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_public_key identity_key;
|
|
|
|
_olm_curve25519_public_key one_time_key;
|
2015-06-15 18:49:20 +02:00
|
|
|
|
2015-08-19 18:18:09 +02:00
|
|
|
olm::decode_base64(id_key, id_key_length, identity_key.public_key);
|
|
|
|
olm::decode_base64(ot_key, ot_key_length, one_time_key.public_key);
|
2015-06-15 18:49:20 +02:00
|
|
|
|
2015-07-10 19:29:14 +02:00
|
|
|
size_t result = from_c(session)->new_outbound_session(
|
2015-06-15 18:49:20 +02:00
|
|
|
*from_c(account), identity_key, one_time_key,
|
|
|
|
from_c(random), random_length
|
|
|
|
);
|
2015-07-10 19:29:14 +02:00
|
|
|
olm::unset(random, random_length);
|
|
|
|
return result;
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_create_inbound_session(
|
|
|
|
OlmSession * session,
|
|
|
|
OlmAccount * account,
|
2015-06-15 18:49:20 +02:00
|
|
|
void * one_time_key_message, size_t message_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = b64_input(
|
|
|
|
from_c(one_time_key_message), message_length, from_c(session)->last_error
|
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
return from_c(session)->new_inbound_session(
|
2015-07-16 11:45:10 +02:00
|
|
|
*from_c(account), nullptr, from_c(one_time_key_message), raw_length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_create_inbound_session_from(
|
|
|
|
OlmSession * session,
|
|
|
|
OlmAccount * account,
|
|
|
|
void const * their_identity_key, size_t their_identity_key_length,
|
|
|
|
void * one_time_key_message, size_t message_length
|
|
|
|
) {
|
2015-08-19 18:18:09 +02:00
|
|
|
std::uint8_t const * id_key = from_c(their_identity_key);
|
|
|
|
std::size_t id_key_length = their_identity_key_length;
|
|
|
|
|
2016-09-02 16:11:14 +02:00
|
|
|
if (olm::decode_base64_length(id_key_length) != CURVE25519_KEY_LENGTH) {
|
2016-05-16 13:08:45 +02:00
|
|
|
from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
|
2015-07-16 11:45:10 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_public_key identity_key;
|
2015-08-19 18:18:09 +02:00
|
|
|
olm::decode_base64(id_key, id_key_length, identity_key.public_key);
|
2015-07-16 11:45:10 +02:00
|
|
|
|
|
|
|
std::size_t raw_length = b64_input(
|
|
|
|
from_c(one_time_key_message), message_length, from_c(session)->last_error
|
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
return from_c(session)->new_inbound_session(
|
|
|
|
*from_c(account), &identity_key,
|
|
|
|
from_c(one_time_key_message), raw_length
|
2015-06-15 18:49:20 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-16 12:45:20 +02:00
|
|
|
size_t olm_session_id_length(
|
|
|
|
OlmSession * session
|
|
|
|
) {
|
|
|
|
return b64_output_length(from_c(session)->session_id_length());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t olm_session_id(
|
|
|
|
OlmSession * session,
|
|
|
|
void * id, size_t id_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = from_c(session)->session_id_length();
|
|
|
|
if (id_length < b64_output_length(raw_length)) {
|
|
|
|
from_c(session)->last_error =
|
2016-05-16 13:08:45 +02:00
|
|
|
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-07-16 12:45:20 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
std::size_t result = from_c(session)->session_id(
|
|
|
|
b64_output_pos(from_c(id), raw_length), raw_length
|
|
|
|
);
|
|
|
|
if (result == std::size_t(-1)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return b64_output(from_c(id), raw_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-05 00:34:24 +02:00
|
|
|
int olm_session_has_received_message(
|
|
|
|
OlmSession * session
|
|
|
|
) {
|
|
|
|
return from_c(session)->received_message;
|
|
|
|
}
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_matches_inbound_session(
|
|
|
|
OlmSession * session,
|
2015-06-15 18:49:20 +02:00
|
|
|
void * one_time_key_message, size_t message_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = b64_input(
|
|
|
|
from_c(one_time_key_message), message_length, from_c(session)->last_error
|
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
bool matches = from_c(session)->matches_inbound_session(
|
2015-07-16 11:45:10 +02:00
|
|
|
nullptr, from_c(one_time_key_message), raw_length
|
|
|
|
);
|
|
|
|
return matches ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_matches_inbound_session_from(
|
|
|
|
OlmSession * session,
|
|
|
|
void const * their_identity_key, size_t their_identity_key_length,
|
|
|
|
void * one_time_key_message, size_t message_length
|
|
|
|
) {
|
2015-08-19 18:18:09 +02:00
|
|
|
std::uint8_t const * id_key = from_c(their_identity_key);
|
|
|
|
std::size_t id_key_length = their_identity_key_length;
|
|
|
|
|
2016-09-02 16:11:14 +02:00
|
|
|
if (olm::decode_base64_length(id_key_length) != CURVE25519_KEY_LENGTH) {
|
2016-05-16 13:08:45 +02:00
|
|
|
from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
|
2015-07-16 11:45:10 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_public_key identity_key;
|
2015-08-19 18:18:09 +02:00
|
|
|
olm::decode_base64(id_key, id_key_length, identity_key.public_key);
|
2015-07-16 11:45:10 +02:00
|
|
|
|
|
|
|
std::size_t raw_length = b64_input(
|
|
|
|
from_c(one_time_key_message), message_length, from_c(session)->last_error
|
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
bool matches = from_c(session)->matches_inbound_session(
|
|
|
|
&identity_key, from_c(one_time_key_message), raw_length
|
2015-06-15 18:49:20 +02:00
|
|
|
);
|
|
|
|
return matches ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_remove_one_time_keys(
|
|
|
|
OlmAccount * account,
|
|
|
|
OlmSession * session
|
2015-06-22 12:02:42 +02:00
|
|
|
) {
|
|
|
|
size_t result = from_c(account)->remove_key(
|
2015-07-08 17:00:08 +02:00
|
|
|
from_c(session)->bob_one_time_key
|
2015-06-22 12:02:42 +02:00
|
|
|
);
|
|
|
|
if (result == std::size_t(-1)) {
|
2016-05-16 13:08:45 +02:00
|
|
|
from_c(account)->last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
|
2015-06-22 12:02:42 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_encrypt_message_type(
|
|
|
|
OlmSession * session
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
|
|
|
return size_t(from_c(session)->encrypt_message_type());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_encrypt_random_length(
|
|
|
|
OlmSession * session
|
2015-06-15 18:49:20 +02:00
|
|
|
) {
|
|
|
|
return from_c(session)->encrypt_random_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_encrypt_message_length(
|
|
|
|
OlmSession * session,
|
2015-06-15 18:49:20 +02:00
|
|
|
size_t plaintext_length
|
|
|
|
) {
|
|
|
|
return b64_output_length(
|
|
|
|
from_c(session)->encrypt_message_length(plaintext_length)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_encrypt(
|
|
|
|
OlmSession * session,
|
2015-06-15 18:49:20 +02:00
|
|
|
void const * plaintext, size_t plaintext_length,
|
2015-07-10 19:29:14 +02:00
|
|
|
void * random, size_t random_length,
|
2015-06-15 18:49:20 +02:00
|
|
|
void * message, size_t message_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = from_c(session)->encrypt_message_length(
|
|
|
|
plaintext_length
|
|
|
|
);
|
2015-07-10 12:57:53 +02:00
|
|
|
if (message_length < b64_output_length(raw_length)) {
|
2015-06-15 18:49:20 +02:00
|
|
|
from_c(session)->last_error =
|
2016-05-16 13:08:45 +02:00
|
|
|
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-06-15 18:49:20 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2015-07-16 12:45:20 +02:00
|
|
|
std::size_t result = from_c(session)->encrypt(
|
2015-06-15 18:49:20 +02:00
|
|
|
from_c(plaintext), plaintext_length,
|
|
|
|
from_c(random), random_length,
|
|
|
|
b64_output_pos(from_c(message), raw_length), raw_length
|
|
|
|
);
|
2015-07-10 19:29:14 +02:00
|
|
|
olm::unset(random, random_length);
|
2015-07-16 12:45:20 +02:00
|
|
|
if (result == std::size_t(-1)) {
|
|
|
|
return result;
|
|
|
|
}
|
2015-06-15 18:49:20 +02:00
|
|
|
return b64_output(from_c(message), raw_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_decrypt_max_plaintext_length(
|
|
|
|
OlmSession * session,
|
2015-06-15 18:49:20 +02:00
|
|
|
size_t message_type,
|
|
|
|
void * message, size_t message_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = b64_input(
|
|
|
|
from_c(message), message_length, from_c(session)->last_error
|
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
return from_c(session)->decrypt_max_plaintext_length(
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::MessageType(message_type), from_c(message), raw_length
|
2015-06-15 18:49:20 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
size_t olm_decrypt(
|
|
|
|
OlmSession * session,
|
2015-06-15 18:49:20 +02:00
|
|
|
size_t message_type,
|
|
|
|
void * message, size_t message_length,
|
|
|
|
void * plaintext, size_t max_plaintext_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = b64_input(
|
|
|
|
from_c(message), message_length, from_c(session)->last_error
|
|
|
|
);
|
|
|
|
if (raw_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
return from_c(session)->decrypt(
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::MessageType(message_type), from_c(message), raw_length,
|
2015-06-15 18:49:20 +02:00
|
|
|
from_c(plaintext), max_plaintext_length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-24 15:29:52 +02:00
|
|
|
|
|
|
|
size_t olm_sha256_length(
|
|
|
|
OlmUtility * utility
|
|
|
|
) {
|
|
|
|
return b64_output_length(from_c(utility)->sha256_length());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_sha256(
|
|
|
|
OlmUtility * utility,
|
|
|
|
void const * input, size_t input_length,
|
|
|
|
void * output, size_t output_length
|
|
|
|
) {
|
|
|
|
std::size_t raw_length = from_c(utility)->sha256_length();
|
|
|
|
if (output_length < b64_output_length(raw_length)) {
|
|
|
|
from_c(utility)->last_error =
|
2016-05-16 13:08:45 +02:00
|
|
|
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
|
2015-07-24 15:29:52 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
std::size_t result = from_c(utility)->sha256(
|
|
|
|
from_c(input), input_length,
|
|
|
|
b64_output_pos(from_c(output), raw_length), raw_length
|
|
|
|
);
|
|
|
|
if (result == std::size_t(-1)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return b64_output(from_c(output), raw_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t olm_ed25519_verify(
|
|
|
|
OlmUtility * utility,
|
|
|
|
void const * key, size_t key_length,
|
|
|
|
void const * message, size_t message_length,
|
|
|
|
void * signature, size_t signature_length
|
|
|
|
) {
|
2016-09-02 16:11:14 +02:00
|
|
|
if (olm::decode_base64_length(key_length) != CURVE25519_KEY_LENGTH) {
|
2016-05-16 13:08:45 +02:00
|
|
|
from_c(utility)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
|
2015-07-24 15:29:52 +02:00
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_ed25519_public_key verify_key;
|
2015-08-19 18:18:09 +02:00
|
|
|
olm::decode_base64(from_c(key), key_length, verify_key.public_key);
|
2015-07-24 15:29:52 +02:00
|
|
|
std::size_t raw_signature_length = b64_input(
|
|
|
|
from_c(signature), signature_length, from_c(utility)->last_error
|
|
|
|
);
|
|
|
|
if (raw_signature_length == std::size_t(-1)) {
|
|
|
|
return std::size_t(-1);
|
|
|
|
}
|
|
|
|
return from_c(utility)->ed25519_verify(
|
|
|
|
verify_key,
|
|
|
|
from_c(message), message_length,
|
|
|
|
from_c(signature), raw_signature_length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-15 18:49:20 +02:00
|
|
|
}
|