-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSRUP.h
115 lines (86 loc) · 3.33 KB
/
SRUP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Created by AJ Poulter on 27/04/2016.
//
#ifndef C2_TEST_SRUP_H
#define C2_TEST_SRUP_H
#include "SRUP_Crypto.h"
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <openssl/rsa.h>
#include <openssl/pem.h>
// We'll start with an abstract base-class
// The protocol elements common to all three message types are:
// * Version
// * Message Type
// * Signature
// * Token
// * Sequence ID ==> New to version 0x02...
// * Sender ID ==> New to version 0x03...
//
// So we'll include those in the base-class.
// Note that we need to treat the Sequence ID as a (long long) integer for comparisons – so we want a 64-bit unsigned int
// We can't assume that sizeof(unsigned long long int) will be 8-bytes (C99 specifies "at least 8-bytes") - so we
// must use a uint64_t... which of course means we have to include stdint.h (or cstdint as we're using C++)
// For the same reasons we're using uint8_t too instead of unsigned char's...
//
// Remember the library class doesn't (can't) do the sequence ID checking : this is a task for the device / server application.
//
// All of the classes will also need to be able to Serialize / Deserialize - so we'll include those methods too...
// ...but since they're specific to the fields that are in use: we'll declare those to be virtual in the base-class
// All of the derived classes will need to implement their own version of PreSign() and Sign() too.
namespace SRUP
{
// Now we have added both the Sequence ID & Sender ID we have incremented the version to 0x03
// Removal of the group messages, and of the explicit target ID in the update init message is a breaking change
// implemented as version number of 0x04...
// Addition of syndication messages moves use to version 0x05
static const uint8_t SRUP_VERSION = 0x05;
}
class SRUP_MSG
{
public:
SRUP_MSG();
virtual ~SRUP_MSG();
// C++11 only - but disable copy constructor & copy-assign constructor
//void operator =(SRUP_MSG const &x) = delete;
//SRUP_MSG(const SRUP_MSG&) = delete;
virtual uint8_t * Serialized()=0;
virtual bool DeSerialize(const uint8_t *)=0;
virtual uint32_t SerializedLength()=0;
const uint8_t* version();
const uint8_t* msgtype();
const uint64_t* sequenceID();
bool sequenceID(const uint64_t*);
const uint64_t* senderID();
bool senderID(const uint64_t*);
static uint8_t getByteVal(uint64_t, uint8_t);
const uint8_t* signature();
bool token(const uint8_t*, uint16_t);
uint16_t token_length() const;
const uint8_t* token();
virtual bool SignF(char*);
virtual bool VerifyF(char*);
virtual bool Sign(char*);
virtual bool Verify(char*);
protected:
uint8_t * m_version;
uint8_t * m_msgtype;
uint64_t* m_sequence_ID;
uint64_t* m_sender_ID;
uint8_t* m_signature;
uint16_t m_sig_len;
uint8_t* m_token;
uint16_t m_token_len;
bool m_is_serialized;
uint8_t * m_serialized;
uint8_t * m_unsigned_message;
uint32_t m_serial_length;
uint32_t m_unsigned_length;
static uint16_t decodeLength(const uint8_t *);
static void encodeLength(uint8_t *, uint8_t *, uint16_t);
virtual bool DataCheck()=0; // A virtual helper function for Sign()...
virtual bool Serialize(bool)=0; // A virtual helper function for Serialized()...
};
#endif //C2_TEST_SRUP_H