Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
matrix-org
Olm
Commits
b3db0e6e
Commit
b3db0e6e
authored
May 13, 2016
by
Richard van der Hoff
Browse files
translate logging.cpp to C
parent
48cb5f92
Changes
8
Hide whitespace changes
Inline
Side-by-side
include/olm/logging.h
h
→
include/olm/logging.h
View file @
b3db0e6e
...
...
@@ -13,26 +13,32 @@
* limitations under the License.
*/
#ifndef OLM_LOGGING_H
H
_
#define OLM_LOGGING_H
H
_
#ifndef OLM_LOGGING_H_
#define OLM_LOGGING_H_
namespac
e
olm
{
#includ
e
"
olm
/olm.hh"
const
unsigned
int
LOG_FATAL
=
1
;
const
unsigned
int
LOG_ERROR
=
2
;
const
unsigned
int
LOG_WARNING
=
3
;
const
unsigned
int
LOG_INFO
=
4
;
const
unsigned
int
LOG_DEBUG
=
5
;
const
unsigned
int
LOG_TRACE
=
6
;
#ifdef __cplusplus
extern
"C"
{
#endif
void
set_log_level
(
unsigned
int
log_level
);
#define OLM_LOG_FATAL 1
#define OLM_LOG_ERROR 2
#define OLM_LOG_WARNING 3
#define OLM_LOG_INFO 4
#define OLM_LOG_DEBUG 5
#define OLM_LOG_TRACE 6
bool
log_enabled_for
(
unsigned
int
level
,
const
char
*
category
);
/* returns non-zero if logging is enabled for this level */
int
olm_log_enabled_for
(
unsigned
int
level
,
const
char
*
category
);
__attribute__
((
__format__
(
__printf__
,
3
,
4
)))
void
logf
(
unsigned
int
level
,
const
char
*
category
,
void
olm_
logf
(
unsigned
int
level
,
const
char
*
category
,
const
char
*
format
,
...);
}
// namespace olm
#endif
/* OLM_LOGGING_HH_ */
#ifdef __cplusplus
}
// extern "C"
#endif
#endif
/* OLM_LOGGING_H_ */
include/olm/olm.hh
View file @
b3db0e6e
...
...
@@ -25,9 +25,9 @@ extern "C" {
static
const
size_t
OLM_MESSAGE_TYPE_PRE_KEY
=
0
;
static
const
size_t
OLM_MESSAGE_TYPE_MESSAGE
=
1
;
struc
t
OlmAccount
;
struct
OlmSession
;
struct
OlmUtility
;
typedef
struct
OlmAccoun
t
OlmAccount
;
typedef
struct
OlmSession
OlmSession
;
typedef
struct
OlmUtility
OlmUtility
;
/** The size of an account object in bytes */
size_t
olm_account_size
();
...
...
src/account.cpp
View file @
b3db0e6e
...
...
@@ -14,7 +14,7 @@
*/
#include "olm/account.hh"
#include "olm/base64.hh"
#include "olm/logging.h
h
"
#include "olm/logging.h"
#include "olm/pickle.hh"
#include "olm/memory.hh"
...
...
@@ -45,11 +45,11 @@ std::size_t olm::Account::remove_key(
if
(
olm
::
array_equal
(
i
->
key
.
public_key
,
public_key
.
public_key
))
{
std
::
uint32_t
id
=
i
->
id
;
one_time_keys
.
erase
(
i
);
olm
::
logf
(
olm
::
LOG_INFO
,
LOG_CATEGORY
,
"removed key id %i"
,
id
);
olm
_
logf
(
OLM_
LOG_INFO
,
LOG_CATEGORY
,
"removed key id %i"
,
id
);
return
id
;
}
}
olm
::
logf
(
olm
::
LOG_WARNING
,
LOG_CATEGORY
,
"Couldn't find key to remove"
);
olm
_
logf
(
OLM_
LOG_WARNING
,
LOG_CATEGORY
,
"Couldn't find key to remove"
);
return
std
::
size_t
(
-
1
);
}
...
...
@@ -69,7 +69,7 @@ std::size_t olm::Account::new_account(
random
+=
KEY_LENGTH
;
olm
::
curve25519_generate_key
(
random
,
identity_keys
.
curve25519_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new account"
);
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new account"
);
return
0
;
}
...
...
src/logging.c
pp
→
src/logging.c
View file @
b3db0e6e
...
...
@@ -13,25 +13,23 @@
* limitations under the License.
*/
#include "olm/logging.h
h
"
#include "olm/logging.h"
#include <cstdarg>
#include <cstdio>
namespace
olm
{
#include <stdarg.h>
#include <stdio.h>
static
unsigned
int
log_level
=
1
;
void
set_log_level
(
unsigned
int
level
)
{
void
olm_
set_log_level
(
unsigned
int
level
)
{
log_level
=
level
;
}
bool
log_enabled_for
(
unsigned
int
level
,
const
char
*
category
)
int
olm_
log_enabled_for
(
unsigned
int
level
,
const
char
*
category
)
{
return
level
<=
log_level
;
}
void
logf
(
unsigned
int
level
,
const
char
*
category
,
void
olm_
logf
(
unsigned
int
level
,
const
char
*
category
,
const
char
*
format
,
...)
{
if
(
level
>
log_level
)
{
return
;
...
...
@@ -47,5 +45,3 @@ void logf(unsigned int level, const char *category,
putchar
(
'\n'
);
}
}
// namespace olm
src/message.cpp
View file @
b3db0e6e
...
...
@@ -14,7 +14,7 @@
*/
#include "olm/message.hh"
#include "olm/logging.h
h
"
#include "olm/logging.h"
#include "olm/memory.hh"
namespace
{
...
...
@@ -239,7 +239,7 @@ void olm::decode_message(
}
unknown
=
pos
;
}
olm
::
logf
(
olm
::
LOG_TRACE
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_TRACE
,
LOG_CATEGORY
,
"Decoded message ver=%i ratchet_key=%s chain_idx=%i ciphertext=%s"
,
reader
.
version
,
olm
::
bytes_to_string
(
reader
.
ratchet_key
,
reader
.
ratchet_key_length
).
c_str
(),
...
...
@@ -335,7 +335,7 @@ void olm::decode_one_time_key_message(
unknown
=
pos
;
}
olm
::
logf
(
olm
::
LOG_TRACE
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_TRACE
,
LOG_CATEGORY
,
"Decoded pre-key message ver=%i one_time_key[Eb]=%s "
"base_key[Ea]=%s identity_key[Ia]=%s message=%s"
,
reader
.
version
,
...
...
src/olm.cpp
View file @
b3db0e6e
...
...
@@ -19,7 +19,7 @@
#include "olm/base64.hh"
#include "olm/cipher.hh"
#include "olm/memory.hh"
#include "olm/logging.h
h
"
#include "olm/logging.h"
#include <new>
#include <cstring>
...
...
@@ -819,10 +819,4 @@ size_t olm_ed25519_verify(
);
}
void
olm_set_log_level
(
unsigned
int
level
)
{
olm
::
set_log_level
(
level
);
}
}
src/ratchet.cpp
View file @
b3db0e6e
...
...
@@ -17,7 +17,7 @@
#include "olm/memory.hh"
#include "olm/cipher.hh"
#include "olm/pickle.hh"
#include "olm/logging.h
h
"
#include "olm/logging.h"
#include <cstring>
...
...
@@ -79,7 +79,7 @@ static void advance_chain_key(
new_chain_key
.
key
);
new_chain_key
.
index
=
chain_key
.
index
+
1
;
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Derived chain key C(%i,%i)"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Derived chain key C(%i,%i)"
,
chain_index
,
new_chain_key
.
index
);
}
...
...
@@ -95,7 +95,7 @@ static void create_message_keys(
message_key
.
key
);
message_key
.
index
=
chain_key
.
index
;
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Created message key with chain key C(%i,%i)"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Created message key with chain key C(%i,%i)"
,
chain_index
,
message_key
.
index
);
}
...
...
@@ -176,7 +176,7 @@ static std::size_t verify_mac_and_decrypt_for_new_chain(
new_chain
.
ratchet_key
,
session
.
kdf_info
,
new_root_key
,
new_chain
.
chain_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Calculated new receiver chain R(%i)"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Calculated new receiver chain R(%i)"
,
chain_index
);
std
::
size_t
result
=
verify_mac_and_decrypt_for_existing_chain
(
...
...
@@ -219,7 +219,7 @@ void olm::Ratchet::initialise_as_bob(
receiver_chains
[
0
].
ratchet_key
=
their_ratchet_key
;
chain_index
=
0
;
olm
::
unset
(
derived_secrets
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised receiver chain R(0)"
);
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised receiver chain R(0)"
);
}
...
...
@@ -242,7 +242,7 @@ void olm::Ratchet::initialise_as_alice(
sender_chain
[
0
].
ratchet_key
=
our_ratchet_key
;
chain_index
=
0
;
olm
::
unset
(
derived_secrets
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised sender chain R(0)"
);
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised sender chain R(0)"
);
}
namespace
olm
{
...
...
@@ -450,7 +450,7 @@ std::size_t olm::Ratchet::encrypt(
if
(
sender_chain
.
empty
())
{
sender_chain
.
insert
();
olm
::
curve25519_generate_key
(
random
,
sender_chain
[
0
].
ratchet_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new ratchet key T(%i) %s"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new ratchet key T(%i) %s"
,
chain_index
+
1
,
sender_chain
[
0
].
ratchet_key
.
to_string
().
c_str
());
create_chain_key
(
...
...
@@ -460,7 +460,7 @@ std::size_t olm::Ratchet::encrypt(
kdf_info
,
root_key
,
sender_chain
[
0
].
chain_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised new sender chain R(%i)"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised new sender chain R(%i)"
,
chain_index
+
1
);
chain_index
++
;
}
...
...
@@ -491,7 +491,7 @@ std::size_t olm::Ratchet::encrypt(
output
,
output_length
);
olm
::
logf
(
olm
::
LOG_TRACE
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_TRACE
,
LOG_CATEGORY
,
"Encoded message ver=%i ratchet_key=%s chain_idx=%i ciphertext=%s"
,
PROTOCOL_VERSION
,
olm
::
bytes_to_string
(
writer
.
ratchet_key
,
olm
::
KEY_LENGTH
).
c_str
(),
...
...
@@ -525,7 +525,7 @@ std::size_t olm::Ratchet::decrypt(
std
::
uint8_t
const
*
input
,
std
::
size_t
input_length
,
std
::
uint8_t
*
plaintext
,
std
::
size_t
max_plaintext_length
)
{
olm
::
logf
(
olm
::
LOG_TRACE
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_TRACE
,
LOG_CATEGORY
,
"Decrypting message %s"
,
olm
::
bytes_to_string
(
input
,
input_length
).
c_str
());
...
...
@@ -580,7 +580,7 @@ std::size_t olm::Ratchet::decrypt(
std
::
size_t
result
=
std
::
size_t
(
-
1
);
if
(
!
chain
)
{
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Sender ratchet key does not match known chain; starting new one"
);
result
=
verify_mac_and_decrypt_for_new_chain
(
*
this
,
reader
,
plaintext
,
max_plaintext_length
...
...
@@ -642,7 +642,7 @@ std::size_t olm::Ratchet::decrypt(
olm
::
unset
(
sender_chain
[
0
]);
sender_chain
.
erase
(
sender_chain
.
begin
());
receiver_chain_index
=
++
chain_index
;
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised new receiver chain R(%i)"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised new receiver chain R(%i)"
,
chain_index
);
}
...
...
src/session.cpp
View file @
b3db0e6e
...
...
@@ -16,7 +16,7 @@
#include "olm/cipher.hh"
#include "olm/crypto.hh"
#include "olm/account.hh"
#include "olm/logging.h
h
"
#include "olm/logging.h"
#include "olm/memory.hh"
#include "olm/message.hh"
#include "olm/pickle.hh"
...
...
@@ -68,7 +68,7 @@ std::size_t olm::Session::new_outbound_session(
return
std
::
size_t
(
-
1
);
}
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Creating new outbound session to receiver identity IB %s, "
"receiver ephemeral EB %s"
,
identity_key
.
to_string
().
c_str
(),
one_time_key
.
to_string
().
c_str
()
...
...
@@ -76,12 +76,12 @@ std::size_t olm::Session::new_outbound_session(
olm
::
Curve25519KeyPair
base_key
;
olm
::
curve25519_generate_key
(
random
,
base_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new ephemeral key EA %s"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new ephemeral key EA %s"
,
base_key
.
to_string
().
c_str
());
olm
::
Curve25519KeyPair
ratchet_key
;
olm
::
curve25519_generate_key
(
random
+
olm
::
KEY_LENGTH
,
ratchet_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new ratchet key T(0) %s"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Created new ratchet key T(0) %s"
,
ratchet_key
.
to_string
().
c_str
());
olm
::
Curve25519KeyPair
const
&
alice_identity_key_pair
=
(
...
...
@@ -108,7 +108,7 @@ std::size_t olm::Session::new_outbound_session(
olm
::
unset
(
ratchet_key
);
olm
::
unset
(
secret
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised outbound session"
);
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised outbound session"
);
return
std
::
size_t
(
0
);
}
...
...
@@ -151,7 +151,7 @@ std::size_t olm::Session::new_inbound_session(
their_identity_key
->
public_key
,
reader
.
identity_key
,
olm
::
KEY_LENGTH
);
if
(
!
same
)
{
olm
::
logf
(
olm
::
LOG_INFO
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_INFO
,
LOG_CATEGORY
,
"Identity key on received message is incorrect "
"(expected %s, got %s)"
,
their_identity_key
->
to_string
().
c_str
(),
...
...
@@ -167,7 +167,7 @@ std::size_t olm::Session::new_inbound_session(
olm
::
load_array
(
alice_base_key
.
public_key
,
reader
.
base_key
);
olm
::
load_array
(
bob_one_time_key
.
public_key
,
reader
.
one_time_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Creating new inbound session from sender identity IA %s, "
"sender ephemeral EA %s, our ephemeral EB %s"
,
alice_identity_key
.
to_string
().
c_str
(),
...
...
@@ -189,7 +189,7 @@ std::size_t olm::Session::new_inbound_session(
olm
::
Curve25519PublicKey
ratchet_key
;
olm
::
load_array
(
ratchet_key
.
public_key
,
message_reader
.
ratchet_key
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Received ratchet key T(0) %s"
,
ratchet_key
.
to_string
().
c_str
());
olm
::
OneTimeKey
const
*
our_one_time_key
=
local_account
.
lookup_key
(
...
...
@@ -197,7 +197,7 @@ std::size_t olm::Session::new_inbound_session(
);
if
(
!
our_one_time_key
)
{
olm
::
logf
(
olm
::
LOG_INFO
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_INFO
,
LOG_CATEGORY
,
"Session uses unknown ephemeral key %s"
,
bob_one_time_key
.
to_string
().
c_str
());
last_error
=
olm
::
ErrorCode
::
BAD_MESSAGE_KEY_ID
;
...
...
@@ -221,7 +221,7 @@ std::size_t olm::Session::new_inbound_session(
olm
::
unset
(
secret
);
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised inbound session"
);
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Initialised inbound session"
);
return
std
::
size_t
(
0
);
}
...
...
@@ -320,7 +320,7 @@ std::size_t olm::Session::encrypt(
std
::
uint8_t
const
*
random
,
std
::
size_t
random_length
,
std
::
uint8_t
*
message
,
std
::
size_t
message_length
)
{
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Encrypting '%.*s'"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Encrypting '%.*s'"
,
(
int
)
plaintext_length
,
plaintext
);
if
(
message_length
<
encrypt_message_length
(
plaintext_length
))
{
...
...
@@ -351,7 +351,7 @@ std::size_t olm::Session::encrypt(
message_body
=
writer
.
message
;
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Encoded pre-key message ver=%i one_time_key[Eb]=%s "
"base_key[Ea]=%s identity_key[Ia]=%s"
,
PROTOCOL_VERSION
,
...
...
@@ -373,7 +373,7 @@ std::size_t olm::Session::encrypt(
return
result
;
}
olm
::
logf
(
olm
::
LOG_TRACE
,
LOG_CATEGORY
,
"Encrypted message %s"
,
olm
_
logf
(
OLM_
LOG_TRACE
,
LOG_CATEGORY
,
"Encrypted message %s"
,
olm
::
bytes_to_string
(
message_body
,
result
).
c_str
());
return
result
;
...
...
@@ -417,7 +417,7 @@ std::size_t olm::Session::decrypt(
std
::
uint8_t
const
*
message
,
std
::
size_t
message_length
,
std
::
uint8_t
*
plaintext
,
std
::
size_t
max_plaintext_length
)
{
olm
::
logf
(
olm
::
LOG_TRACE
,
LOG_CATEGORY
,
"Decrypting %smessage"
,
olm
_
logf
(
OLM_
LOG_TRACE
,
LOG_CATEGORY
,
"Decrypting %smessage"
,
message_type
==
olm
::
MessageType
::
MESSAGE
?
""
:
"pre-key "
);
std
::
uint8_t
const
*
message_body
;
...
...
@@ -447,7 +447,7 @@ std::size_t olm::Session::decrypt(
}
received_message
=
true
;
olm
::
logf
(
olm
::
LOG_DEBUG
,
LOG_CATEGORY
,
"Decrypted '%.*s'"
,
olm
_
logf
(
OLM_
LOG_DEBUG
,
LOG_CATEGORY
,
"Decrypted '%.*s'"
,
(
int
)
result
,
plaintext
);
return
result
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment