Skip to content

Commit db2e063

Browse files
committed
Move ProProof into session_protocol.hpp
Pro proof is part of the session protocol and is required by some of the higher-level helper functions in the crypto layer. Because of this we were having some circular dependency issues, Debian 12 ARM build was complaining that session_protocol.cpp was calling out to functions in session/config/pro.h which sits in a layer above us. This created a circular dependency and upon re-assessment the pro proof structures really belong in the protocol layer. What does belong in the config section is the top-level pro config object that contains the Session Pro rotating key pair and the proof itself.
1 parent a0fbd43 commit db2e063

File tree

10 files changed

+511
-530
lines changed

10 files changed

+511
-530
lines changed

include/session/config/pro.h

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -9,132 +9,13 @@ extern "C" {
99
#include <stdint.h>
1010

1111
#include "../export.h"
12-
#include "../types.h"
13-
14-
typedef enum PRO_STATUS { // See session::ProStatus
15-
PRO_STATUS_NIL,
16-
PRO_STATUS_INVALID_PRO_BACKEND_SIG,
17-
PRO_STATUS_INVALID_USER_SIG,
18-
PRO_STATUS_VALID,
19-
PRO_STATUS_EXPIRED,
20-
} PRO_STATUS;
21-
22-
typedef struct pro_signed_message {
23-
span_u8 sig;
24-
span_u8 msg;
25-
} pro_signed_message;
26-
27-
typedef struct pro_proof {
28-
uint8_t version;
29-
uint8_t gen_index_hash[32];
30-
uint8_t rotating_pubkey[32];
31-
uint64_t expiry_unix_ts;
32-
uint8_t sig[64];
33-
} pro_proof;
12+
#include "session/session_protocol.h"
3413

3514
typedef struct pro_config {
3615
uint8_t rotating_privkey[64];
3716
pro_proof proof;
3817
} pro_pro_config;
3918

40-
/// API: pro/pro_proof_hash
41-
///
42-
/// Generate the 32 byte hash that is to be signed by the rotating key or Session Pro Backend key to
43-
/// embed in the envelope or proof respectively which other clients use to authenticate the validity
44-
/// of a proof.
45-
///
46-
/// Inputs:
47-
/// - `proof` -- Proof to calculate the hash from
48-
///
49-
/// Outputs:
50-
/// - `bytes32` -- The 32 byte hash calculated from the proof
51-
LIBSESSION_EXPORT bytes32 pro_proof_hash(pro_proof const* proof);
52-
53-
/// API: pro/pro_proof_verify_signature
54-
///
55-
/// Verify the proof was signed by the `verify_pubkey`
56-
///
57-
/// Inputs:
58-
/// - `proof` -- Proof to verify
59-
/// - `verify_pubkey` -- Array of bytes containing the public key to (typically the Session Pro
60-
/// Backend public key) verify the proof against.
61-
/// - `verify_pubkey_len` -- Length of the `verify_pubkey` this must be 32 bytes, but is
62-
/// parameterised to detect errors about incorrectly sized arrays by the caller.
63-
///
64-
/// Outputs:
65-
/// - `bool` -- True if verified, false otherwise
66-
LIBSESSION_EXPORT bool pro_proof_verify_signature(
67-
pro_proof const* proof, uint8_t const* verify_pubkey, size_t verify_pubkey_len);
68-
69-
/// API: pro/pro_proof_verify_message
70-
///
71-
/// Check if the `rotating_pubkey` in the proof was the signatory of the message and signature
72-
/// passed in. This function throws if an signature is passed in that isn't 64 bytes.
73-
///
74-
/// Inputs:
75-
/// - `proof` -- Proof to verify
76-
/// - `sig` -- Signature to verify with the `rotating_pubkey`. The signature should have
77-
/// originally been signed over `msg` passed in.
78-
/// - `sig_len` -- Length of the signature, should be 64 bytes
79-
/// - `msg` -- Message that the signature signed over with. It will be verified using the
80-
/// embedded `rotating_pubkey`.
81-
/// - `msg_len` -- Length of the message
82-
///
83-
/// Outputs:
84-
/// - `bool` -- True if verified, false otherwise (bad signature, or, invalid arguments).
85-
LIBSESSION_EXPORT bool pro_proof_verify_message(
86-
pro_proof const* proof,
87-
uint8_t const* sig,
88-
size_t sig_len,
89-
uint8_t const* msg,
90-
size_t msg_len);
91-
92-
/// API: pro/pro_proof_is_active
93-
///
94-
/// Check if the Pro proof is currently entitled to Pro given the `unix_ts` with respect to the
95-
/// proof's `expiry_unix_ts`
96-
///
97-
/// Inputs:
98-
/// - `proof` -- Proof to verify
99-
/// - `unix_ts_s` -- The unix timestamp in seconds to check the proof expiry time against
100-
///
101-
/// Outputs:
102-
/// - `bool` -- True if expired, false otherwise
103-
LIBSESSION_EXPORT bool pro_proof_is_active(pro_proof const* proof, uint64_t unix_ts_s);
104-
105-
/// API: pro/pro_proof_status
106-
///
107-
/// Evaluate the status of the pro proof by checking it is signed by the `verify_pubkey`, it has
108-
/// not expired via `unix_ts_s` and optionally verify that the `signed_msg` was signed by the
109-
/// `rotating_pubkey` embedded in the proof.
110-
///
111-
/// Internally this function calls `pro_proof_verify_signature`, `pro_proof_verify_message` and
112-
/// optionally `pro_proof_is_active` in sequence. This function fails if an invalidly sized public
113-
/// key or signature are passed in. They must be 32 and 64 bytes respectively, the appropriate
114-
/// invalid status will be returned.
115-
///
116-
/// Inputs:
117-
/// - `proof` -- Proof to verify
118-
/// - `verify_pubkey` -- 32 byte Ed25519 public key of the corresponding secret key to check if
119-
/// they are the original signatory of the proof.
120-
/// - `verify_pubkey_len` -- Length of the `verify_pubkey` should be 32 bytes
121-
/// they are the original signatory of the proof.
122-
/// - `unix_ts` -- Unix timestamp in seconds to compared against the embedded `expiry_unix_ts`
123-
/// to determine if the proof has expired or not
124-
/// - `signed_msg` -- Optionally set the payload to the message with the signature to verify if
125-
/// the embedded `rotating_pubkey` in the proof signed the given message.
126-
///
127-
/// Outputs:
128-
/// - `status` - The derived status given the components of the message. If `signed_msg` is
129-
/// not set then this function can never return `PRO_STATUS_INVALID_USER_SIG` from the set of
130-
/// possible enum values. Otherwise this funtion can return all possible values.
131-
LIBSESSION_EXPORT PRO_STATUS pro_proof_status(
132-
pro_proof const* proof,
133-
const uint8_t* verify_pubkey,
134-
size_t verify_pubkey_len,
135-
uint64_t unix_ts_s,
136-
const pro_signed_message* signed_msg);
137-
13819
/// API: pro/pro_verify
13920
///
14021
/// Verify the proof was signed by the `verify_pubkey` and that the `rotating_privkey` in the `pro`

include/session/config/pro.hpp

Lines changed: 10 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,22 @@
11
#pragma once
22

3-
#include <session/config/pro.h>
4-
5-
#include <chrono>
6-
#include <cstdint>
73
#include <session/config.hpp>
84
#include <session/config/base.hpp>
9-
#include <session/sodium_array.hpp>
10-
#include <session/types.hpp>
5+
#include <session/session_protocol.hpp>
116

127
namespace session::config {
138

14-
enum ProProofVersion { ProProofVersion_v0 };
15-
16-
enum class ProStatus {
17-
// Pro proof sig was not signed by the Pro backend key
18-
InvalidProBackendSig = PRO_STATUS_INVALID_PRO_BACKEND_SIG,
19-
// Pro sig in the envelope was not signed by the Rotating key
20-
InvalidUserSig = PRO_STATUS_INVALID_USER_SIG,
21-
Valid = PRO_STATUS_VALID, // Proof is verified; has not expired
22-
Expired = PRO_STATUS_EXPIRED, // Proof is verified; has expired
23-
};
24-
25-
struct ProSignedMessage {
26-
std::span<const uint8_t> sig;
27-
std::span<const uint8_t> msg;
28-
};
29-
30-
/// keys used currently or in the past (so that we don't reuse):
31-
///
32-
/// @ - version
33-
/// g - gen_index_hash
34-
/// r - rotating ed25519 pubkey
35-
/// e - expiry unix timestamp (in seconds)
36-
/// s - proof signature, signed by the Session Pro Backend's ed25519 key
37-
class ProProof {
38-
public:
39-
/// Version of the proof set by the Session Pro Backend
40-
std::uint8_t version;
41-
42-
/// Hash of the generation index set by the Session Pro Backend
43-
array_uc32 gen_index_hash;
44-
45-
/// The public key that the Session client registers their Session Pro entitlement under.
46-
/// Session clients must sign messages with this key along side the sending of this proof for
47-
/// the network to authenticate their usage of the proof
48-
array_uc32 rotating_pubkey;
49-
50-
/// Unix epoch timestamp to which this proof's entitlement to Session Pro features is valid to
51-
std::chrono::sys_seconds expiry_unix_ts;
52-
53-
/// Signature over the contents of the proof. It is signed by the Session Pro Backend key which
54-
/// is the entity responsible for issueing tamper-proof Sesison Pro certificates for Session
55-
/// clients.
56-
array_uc64 sig;
57-
58-
/// API: pro/Proof::verify_signature
59-
///
60-
/// Verify that the proof's contents was not tampered with by hashing the proof and checking
61-
/// that the hash was signed by the secret key of the given Ed25519 public key.
62-
///
63-
/// For Session Pro intents and purposes, we expect proofs to be signed by the Session Pro
64-
/// Backend public key. This function throws if an incorrectly sized key is passed in.
65-
///
66-
/// Inputs:
67-
/// - `verify_pubkey` -- 32 byte Ed25519 public key of the corresponding secret key to check if
68-
/// they are the original signatory of the proof.
69-
///
70-
/// Outputs:
71-
/// - `bool` - True if the given key was the signatory of the proof, false otherwise
72-
bool verify_signature(const std::span<const uint8_t>& verify_pubkey) const;
73-
74-
/// API: pro/Proof::verify_message
75-
///
76-
/// Check if the `rotating_pubkey` in the proof was the signatory of the message and signature
77-
/// passed in. This function throws if an signature is passed in that isn't 64 bytes.
78-
///
79-
/// Inputs:
80-
/// - `sig` -- Signature to verify with the `rotating_pubkey`. The signature should have
81-
/// originally been signed over `msg` passed in.
82-
/// - `msg` -- Message that the signature signed over with. It will be verified using the
83-
/// embedded `rotating_pubkey`.
84-
///
85-
/// Outputs:
86-
/// - `bool` - True if the message was signed by the embedded `rotating_pubkey` false otherwise.
87-
bool verify_message(std::span<const uint8_t> sig, const std::span<const uint8_t> msg) const;
88-
89-
/// API: pro/Proof::is_active
90-
///
91-
/// Check if Pro proof is currently entitled to Pro given the `unix_ts` with respect to the
92-
/// proof's `expiry_unix_ts`
93-
///
94-
/// Inputs:
95-
/// - `unix_ts` -- Unix timestamp in seconds to compared against the embedded `expiry_unix_ts`
96-
/// to determine if the proof has expired or not
97-
///
98-
/// Outputs:
99-
/// - `bool` - True if proof is active (i.e. has not expired), false otherwise.
100-
bool is_active(std::chrono::sys_seconds unix_ts) const;
101-
102-
/// API: pro/Proof::status
103-
///
104-
/// Evaluate the status of the pro proof by checking it is signed by the `verify_pubkey`, it has
105-
/// not expired via `unix_ts` and optionally verify that the `signed_msg` was signed by the
106-
/// `rotating_pubkey` embedded in the proof.
107-
///
108-
/// Internally this function calls `verify_signature`, `verify_message` and optionally
109-
/// `is_active` in sequence. This function throws if an invalidly sized public key or signature
110-
/// are passed in. They must be 32 and 64 bytes respectively.
111-
///
112-
/// Inputs:
113-
/// - `verify_pubkey` -- 32 byte Ed25519 public key of the corresponding secret key to check if
114-
/// they are the original signatory of the proof.
115-
/// - `unix_ts` -- Unix timestamp in seconds to compared against the embedded `expiry_unix_ts`
116-
/// to determine if the proof has expired or not
117-
/// - `signed_msg` -- Optionally set the payload to the message with the signature to verify if
118-
/// the embedded `rotating_pubkey` in the proof signed the given message.
119-
///
120-
/// Outputs:
121-
/// - `ProStatus` - The derived status given the components of the message. If `signed_msg` is
122-
/// not set then this function can never return `ProStatus::InvalidUserSig` from the set of
123-
/// possible enum values. Otherwise this funtion can return all possible values.
124-
ProStatus status(
125-
std::span<const uint8_t> verify_pubkey,
126-
std::chrono::sys_seconds unix_ts,
127-
const std::optional<ProSignedMessage>& signed_msg);
128-
129-
/// API: pro/Proof::hash
130-
///
131-
/// Create a 32-byte hash from the proof. This hash is the payload that is signed in the proof.
132-
array_uc32 hash() const;
133-
134-
bool load(const dict& root);
135-
};
136-
1379
/// keys used currently or in the past (so that we don't reuse):
13810
///
139-
/// r - rotating ed25519 privkey
140-
/// p - proof
11+
/// p + pro data
12+
/// |
13+
/// +-- @ - version
14+
/// +-- g - gen_index_hash
15+
/// +-- r - rotating ed25519 pubkey
16+
/// +-- e - expiry unix timestamp (in seconds)
17+
/// +-- s - proof signature, signed by the Session Pro Backend's ed25519 key
18+
/// +-- r - rotating ed25519 privkey
19+
/// +-- p - proof
14120
class ProConfig {
14221
public:
14322
/// Private key for the public key key specified in the proof. This is synced between clients

include/session/pro_backend.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

3-
#include <session/config/pro.hpp>
3+
#include <chrono>
44
#include <session/types.hpp>
5+
#include <string>
56

67
namespace session::pro_backend {
78

include/session/session_encrypt.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <stdint.h>
4+
35
#include <optional>
46
#include <span>
57
#include <string>

0 commit comments

Comments
 (0)