summaryrefslogtreecommitdiff
path: root/libs/libaxolotl/src/session_pre_key.c
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2017-02-13 07:56:33 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2017-02-13 09:09:08 +0300
commit193f645f65ad4ffdec3186e4176b23af10861199 (patch)
treee1b16b48ac74c5f03f99a98798e849f6dd9752cc /libs/libaxolotl/src/session_pre_key.c
parent36c32a13878d3bd94e88bd9c764f1eadb05ea1ed (diff)
libs:
libaxolotl: updated libaxolotl (libsignal-c) from (https://github.com/WhisperSystems/libsignal-protocol-c)
Diffstat (limited to 'libs/libaxolotl/src/session_pre_key.c')
-rw-r--r--libs/libaxolotl/src/session_pre_key.c159
1 files changed, 82 insertions, 77 deletions
diff --git a/libs/libaxolotl/src/session_pre_key.c b/libs/libaxolotl/src/session_pre_key.c
index 333d2df6ce..416ed6a1f5 100644
--- a/libs/libaxolotl/src/session_pre_key.c
+++ b/libs/libaxolotl/src/session_pre_key.c
@@ -4,18 +4,18 @@
#include <string.h>
#include <assert.h>
-#include "axolotl_internal.h"
#include "curve.h"
#include "LocalStorageProtocol.pb-c.h"
+#include "signal_protocol_internal.h"
struct session_pre_key {
- axolotl_type_base base;
+ signal_type_base base;
uint32_t id;
ec_key_pair *key_pair;
};
struct session_signed_pre_key {
- axolotl_type_base base;
+ signal_type_base base;
uint32_t id;
ec_key_pair *key_pair;
uint64_t timestamp;
@@ -24,14 +24,14 @@ struct session_signed_pre_key {
};
struct session_pre_key_bundle {
- axolotl_type_base base;
+ signal_type_base base;
uint32_t registration_id;
int device_id;
uint32_t pre_key_id;
ec_public_key *pre_key_public;
uint32_t signed_pre_key_id;
ec_public_key *signed_pre_key_public;
- axolotl_buffer *signed_pre_key_signature;
+ signal_buffer *signed_pre_key_signature;
ec_public_key *identity_key;
};
@@ -45,33 +45,38 @@ int session_pre_key_create(session_pre_key **pre_key, uint32_t id, ec_key_pair *
result = malloc(sizeof(session_pre_key));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
memset(result, 0, sizeof(session_pre_key));
- AXOLOTL_INIT(result, session_pre_key_destroy);
+ SIGNAL_INIT(result, session_pre_key_destroy);
result->id = id;
- AXOLOTL_REF(key_pair);
+ SIGNAL_REF(key_pair);
result->key_pair = key_pair;
*pre_key = result;
return 0;
}
-int session_pre_key_serialize(axolotl_buffer **buffer, const session_pre_key *pre_key)
+int session_pre_key_serialize(signal_buffer **buffer, const session_pre_key *pre_key)
{
int result = 0;
size_t result_size = 0;
Textsecure__PreKeyRecordStructure record = TEXTSECURE__PRE_KEY_RECORD_STRUCTURE__INIT;
- axolotl_buffer *public_buf = 0;
- axolotl_buffer *private_buf = 0;
- axolotl_buffer *result_buf = 0;
+ signal_buffer *public_buf = 0;
+ signal_buffer *private_buf = 0;
+ signal_buffer *result_buf = 0;
ec_public_key *public_key = 0;
ec_private_key *private_key = 0;
size_t len = 0;
uint8_t *data = 0;
+ if(!pre_key) {
+ result = SG_ERR_INVAL;
+ goto complete;
+ }
+
public_key = ec_key_pair_get_public(pre_key->key_pair);
result = ec_public_key_serialize(&public_buf, public_key);
if(result < 0) {
@@ -88,36 +93,36 @@ int session_pre_key_serialize(axolotl_buffer **buffer, const session_pre_key *pr
record.id = pre_key->id;
record.has_publickey = 1;
- record.publickey.data = axolotl_buffer_data(public_buf);
- record.publickey.len = axolotl_buffer_len(public_buf);
+ record.publickey.data = signal_buffer_data(public_buf);
+ record.publickey.len = signal_buffer_len(public_buf);
record.has_privatekey = 1;
- record.privatekey.data = axolotl_buffer_data(private_buf);
- record.privatekey.len = axolotl_buffer_len(private_buf);
+ record.privatekey.data = signal_buffer_data(private_buf);
+ record.privatekey.len = signal_buffer_len(private_buf);
len = textsecure__pre_key_record_structure__get_packed_size(&record);
- result_buf = axolotl_buffer_alloc(len);
+ result_buf = signal_buffer_alloc(len);
if(!result_buf) {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
- data = axolotl_buffer_data(result_buf);
+ data = signal_buffer_data(result_buf);
result_size = textsecure__pre_key_record_structure__pack(&record, data);
if(result_size != len) {
- axolotl_buffer_free(result_buf);
- result = AX_ERR_INVALID_PROTO_BUF;
+ signal_buffer_free(result_buf);
+ result = SG_ERR_INVALID_PROTO_BUF;
result_buf = 0;
goto complete;
}
complete:
if(public_buf) {
- axolotl_buffer_free(public_buf);
+ signal_buffer_free(public_buf);
}
if(private_buf) {
- axolotl_buffer_free(private_buf);
+ signal_buffer_free(private_buf);
}
if(result >= 0) {
*buffer = result_buf;
@@ -125,7 +130,7 @@ complete:
return result;
}
-int session_pre_key_deserialize(session_pre_key **pre_key, const uint8_t *data, size_t len, axolotl_context *global_context)
+int session_pre_key_deserialize(session_pre_key **pre_key, const uint8_t *data, size_t len, signal_context *global_context)
{
int result = 0;
Textsecure__PreKeyRecordStructure *record = 0;
@@ -136,12 +141,12 @@ int session_pre_key_deserialize(session_pre_key **pre_key, const uint8_t *data,
record = textsecure__pre_key_record_structure__unpack(0, len, data);
if(!record) {
- result = AX_ERR_INVALID_PROTO_BUF;
+ result = SG_ERR_INVALID_PROTO_BUF;
goto complete;
}
if(!record->has_id || !record->has_publickey || !record->has_privatekey) {
- result = AX_ERR_INVALID_KEY;
+ result = SG_ERR_INVALID_KEY;
goto complete;
}
@@ -170,13 +175,13 @@ complete:
textsecure__pre_key_record_structure__free_unpacked(record, 0);
}
if(public_key) {
- AXOLOTL_UNREF(public_key);
+ SIGNAL_UNREF(public_key);
}
if(private_key) {
- AXOLOTL_UNREF(private_key);
+ SIGNAL_UNREF(private_key);
}
if(key_pair) {
- AXOLOTL_UNREF(key_pair);
+ SIGNAL_UNREF(key_pair);
}
if(result >= 0) {
*pre_key = result_pre_key;
@@ -194,12 +199,12 @@ ec_key_pair *session_pre_key_get_key_pair(const session_pre_key *pre_key)
return pre_key->key_pair;
}
-void session_pre_key_destroy(axolotl_type_base *type)
+void session_pre_key_destroy(signal_type_base *type)
{
session_pre_key *pre_key = (session_pre_key *)type;
if(pre_key->key_pair) {
- AXOLOTL_UNREF(pre_key->key_pair);
+ SIGNAL_UNREF(pre_key->key_pair);
}
free(pre_key);
@@ -218,20 +223,20 @@ int session_signed_pre_key_create(session_signed_pre_key **pre_key,
assert(signature_len > 0);
if(signature_len > (SIZE_MAX - sizeof(session_signed_pre_key)) / sizeof(uint8_t)) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
result = malloc(sizeof(session_signed_pre_key) + (sizeof(uint8_t) * signature_len));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
memset(result, 0, sizeof(session_signed_pre_key));
- AXOLOTL_INIT(result, session_signed_pre_key_destroy);
+ SIGNAL_INIT(result, session_signed_pre_key_destroy);
result->id = id;
result->timestamp = timestamp;
- AXOLOTL_REF(key_pair);
+ SIGNAL_REF(key_pair);
result->key_pair = key_pair;
result->signature_len = signature_len;
@@ -242,15 +247,15 @@ int session_signed_pre_key_create(session_signed_pre_key **pre_key,
return 0;
}
-int session_signed_pre_key_serialize(axolotl_buffer **buffer, const session_signed_pre_key *pre_key)
+int session_signed_pre_key_serialize(signal_buffer **buffer, const session_signed_pre_key *pre_key)
{
int result = 0;
size_t result_size = 0;
Textsecure__SignedPreKeyRecordStructure record = TEXTSECURE__SIGNED_PRE_KEY_RECORD_STRUCTURE__INIT;
- axolotl_buffer *public_buf = 0;
- axolotl_buffer *private_buf = 0;
- axolotl_buffer *signature_buf = 0;
- axolotl_buffer *result_buf = 0;
+ signal_buffer *public_buf = 0;
+ signal_buffer *private_buf = 0;
+ signal_buffer *signature_buf = 0;
+ signal_buffer *result_buf = 0;
ec_public_key *public_key = 0;
ec_private_key *private_key = 0;
size_t len = 0;
@@ -268,9 +273,9 @@ int session_signed_pre_key_serialize(axolotl_buffer **buffer, const session_sign
goto complete;
}
- signature_buf = axolotl_buffer_create(pre_key->signature, pre_key->signature_len);
+ signature_buf = signal_buffer_create(pre_key->signature, pre_key->signature_len);
if(!signature_buf) {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
@@ -281,43 +286,43 @@ int session_signed_pre_key_serialize(axolotl_buffer **buffer, const session_sign
record.timestamp = pre_key->timestamp;
record.has_publickey = 1;
- record.publickey.data = axolotl_buffer_data(public_buf);
- record.publickey.len = axolotl_buffer_len(public_buf);
+ record.publickey.data = signal_buffer_data(public_buf);
+ record.publickey.len = signal_buffer_len(public_buf);
record.has_privatekey = 1;
- record.privatekey.data = axolotl_buffer_data(private_buf);
- record.privatekey.len = axolotl_buffer_len(private_buf);
+ record.privatekey.data = signal_buffer_data(private_buf);
+ record.privatekey.len = signal_buffer_len(private_buf);
record.has_signature = 1;
- record.signature.data = axolotl_buffer_data(signature_buf);
- record.signature.len = axolotl_buffer_len(signature_buf);
+ record.signature.data = signal_buffer_data(signature_buf);
+ record.signature.len = signal_buffer_len(signature_buf);
len = textsecure__signed_pre_key_record_structure__get_packed_size(&record);
- result_buf = axolotl_buffer_alloc(len);
+ result_buf = signal_buffer_alloc(len);
if(!result_buf) {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
- data = axolotl_buffer_data(result_buf);
+ data = signal_buffer_data(result_buf);
result_size = textsecure__signed_pre_key_record_structure__pack(&record, data);
if(result_size != len) {
- axolotl_buffer_free(result_buf);
- result = AX_ERR_INVALID_PROTO_BUF;
+ signal_buffer_free(result_buf);
+ result = SG_ERR_INVALID_PROTO_BUF;
result_buf = 0;
goto complete;
}
complete:
if(public_buf) {
- axolotl_buffer_free(public_buf);
+ signal_buffer_free(public_buf);
}
if(private_buf) {
- axolotl_buffer_free(private_buf);
+ signal_buffer_free(private_buf);
}
if(signature_buf) {
- axolotl_buffer_free(signature_buf);
+ signal_buffer_free(signature_buf);
}
if(result >= 0) {
*buffer = result_buf;
@@ -325,7 +330,7 @@ complete:
return result;
}
-int session_signed_pre_key_deserialize(session_signed_pre_key **pre_key, const uint8_t *data, size_t len, axolotl_context *global_context)
+int session_signed_pre_key_deserialize(session_signed_pre_key **pre_key, const uint8_t *data, size_t len, signal_context *global_context)
{
int result = 0;
Textsecure__SignedPreKeyRecordStructure *record = 0;
@@ -336,14 +341,14 @@ int session_signed_pre_key_deserialize(session_signed_pre_key **pre_key, const u
record = textsecure__signed_pre_key_record_structure__unpack(0, len, data);
if(!record) {
- result = AX_ERR_INVALID_PROTO_BUF;
+ result = SG_ERR_INVALID_PROTO_BUF;
goto complete;
}
if(!record->has_id || !record->has_timestamp
|| !record->has_publickey || !record->has_privatekey
|| !record->has_signature) {
- result = AX_ERR_INVALID_KEY;
+ result = SG_ERR_INVALID_KEY;
goto complete;
}
@@ -374,13 +379,13 @@ complete:
textsecure__signed_pre_key_record_structure__free_unpacked(record, 0);
}
if(public_key) {
- AXOLOTL_UNREF(public_key);
+ SIGNAL_UNREF(public_key);
}
if(private_key) {
- AXOLOTL_UNREF(private_key);
+ SIGNAL_UNREF(private_key);
}
if(key_pair) {
- AXOLOTL_UNREF(key_pair);
+ SIGNAL_UNREF(key_pair);
}
if(result >= 0) {
*pre_key = result_pre_key;
@@ -413,12 +418,12 @@ size_t session_signed_pre_key_get_signature_len(const session_signed_pre_key *pr
return pre_key->signature_len;
}
-void session_signed_pre_key_destroy(axolotl_type_base *type)
+void session_signed_pre_key_destroy(signal_type_base *type)
{
session_signed_pre_key *pre_key = (session_signed_pre_key *)type;
if(pre_key->key_pair) {
- AXOLOTL_UNREF(pre_key->key_pair);
+ SIGNAL_UNREF(pre_key->key_pair);
}
free(pre_key);
@@ -438,36 +443,36 @@ int session_pre_key_bundle_create(session_pre_key_bundle **bundle,
result_bundle = malloc(sizeof(session_pre_key_bundle));
if(!result_bundle) {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
memset(result_bundle, 0, sizeof(session_pre_key_bundle));
- AXOLOTL_INIT(result_bundle, session_pre_key_bundle_destroy);
+ SIGNAL_INIT(result_bundle, session_pre_key_bundle_destroy);
result_bundle->registration_id = registration_id;
result_bundle->device_id = device_id;
result_bundle->pre_key_id = pre_key_id;
if(pre_key_public) {
- AXOLOTL_REF(pre_key_public);
+ SIGNAL_REF(pre_key_public);
result_bundle->pre_key_public = pre_key_public;
}
result_bundle->signed_pre_key_id = signed_pre_key_id;
if(signed_pre_key_public) {
- AXOLOTL_REF(signed_pre_key_public);
+ SIGNAL_REF(signed_pre_key_public);
result_bundle->signed_pre_key_public = signed_pre_key_public;
}
if(signed_pre_key_signature_data && signed_pre_key_signature_len > 0) {
- result_bundle->signed_pre_key_signature = axolotl_buffer_create(
+ result_bundle->signed_pre_key_signature = signal_buffer_create(
signed_pre_key_signature_data, signed_pre_key_signature_len);
}
if(identity_key) {
- AXOLOTL_REF(identity_key);
+ SIGNAL_REF(identity_key);
result_bundle->identity_key = identity_key;
}
@@ -477,7 +482,7 @@ complete:
}
else {
if(result_bundle) {
- AXOLOTL_UNREF(result_bundle);
+ SIGNAL_UNREF(result_bundle);
}
}
return result;
@@ -519,7 +524,7 @@ ec_public_key *session_pre_key_bundle_get_signed_pre_key(const session_pre_key_b
return bundle->signed_pre_key_public;
}
-axolotl_buffer *session_pre_key_bundle_get_signed_pre_key_signature(const session_pre_key_bundle *bundle)
+signal_buffer *session_pre_key_bundle_get_signed_pre_key_signature(const session_pre_key_bundle *bundle)
{
assert(bundle);
return bundle->signed_pre_key_signature;
@@ -531,21 +536,21 @@ ec_public_key *session_pre_key_bundle_get_identity_key(const session_pre_key_bun
return bundle->identity_key;
}
-void session_pre_key_bundle_destroy(axolotl_type_base *type)
+void session_pre_key_bundle_destroy(signal_type_base *type)
{
session_pre_key_bundle *bundle = (session_pre_key_bundle *)type;
if(bundle->pre_key_public) {
- AXOLOTL_UNREF(bundle->pre_key_public);
+ SIGNAL_UNREF(bundle->pre_key_public);
}
if(bundle->signed_pre_key_public) {
- AXOLOTL_UNREF(bundle->signed_pre_key_public);
+ SIGNAL_UNREF(bundle->signed_pre_key_public);
}
if(bundle->signed_pre_key_signature) {
- axolotl_buffer_free(bundle->signed_pre_key_signature);
+ signal_buffer_free(bundle->signed_pre_key_signature);
}
if(bundle->identity_key) {
- AXOLOTL_UNREF(bundle->identity_key);
+ SIGNAL_UNREF(bundle->identity_key);
}
free(bundle);