]> WPIA git - cassiopeia.git/blob - src/record.h
add: simple, signer-side record handling
[cassiopeia.git] / src / record.h
1 #pragma once
2
3 #include <inttypes.h>
4
5 #include <memory>
6 #include <string>
7
8 #include "bios.h"
9
10 #define RECORD_HEADER_SIZE 17
11
12 class RecordHeader {
13 public:
14     enum class SignerCommand : uint16_t {
15         SET_CSR = 0x01,
16         SET_SPKAC = 0x02,
17         SET_SIGNATURE_TYPE = 0x10,
18         SET_PROFILE = 0x11,
19         ADD_SAN = 0x18,
20         ADD_AVA = 0x19,
21         ADD_PROOF_LINE = 0x40,
22         SIGN = 0x80,
23         LOG_SAVED = 0x81,
24         GET_TIMESTAMP = 0xC0,
25         GET_STATUS_REPORT = 0xD0
26     };
27
28     enum class SignerResult : uint16_t {
29         SAVE_LOG = 0x80,
30         CERTIFICATE = 0x81
31     };
32
33 public:
34     uint16_t command;
35     char flags;
36     uint32_t sessid;
37     uint16_t command_count;
38     uint32_t totalLength;
39     uint16_t offset;
40     uint16_t payloadLength;
41
42     RecordHeader() :
43         command( 0 ), flags( 0 ), sessid( 0 ), command_count( 0 ), totalLength( 0 ), offset( 0 ), payloadLength( 0 ) {
44     }
45
46     template <class T>
47     void append( std::string& str, T val ) {
48         str.append( ( char* ) &val, sizeof( T ) );
49     }
50
51     template <class T>
52     void read( std::string::iterator& it, T& val ) {
53         char* data = ( char* ) &val;
54
55         for( int i = 0; i < sizeof( T ); i++ ) {
56             data[i] = *it;
57             it++;
58         }
59     }
60
61     std::string packToString() {
62         std::string res;
63         res.reserve( RECORD_HEADER_SIZE );
64         append( res, command );
65         append( res, flags );
66         append( res, sessid );
67         append( res, command_count );
68         append( res, totalLength );
69         append( res, offset );
70         append( res, payloadLength );
71         return res;
72     }
73
74     void unpackFromString( std::string str ) {
75         if( str.size() != RECORD_HEADER_SIZE ) {
76             throw "Invalid string length";
77         }
78
79         auto it =  str.begin();
80         read( it, command );
81         read( it, flags );
82         read( it, sessid );
83         read( it, command_count );
84         read( it, totalLength );
85         read( it, offset );
86         read( it, payloadLength );
87     }
88
89 };
90
91 std::string parseCommand( RecordHeader& head, const std::string& input );
92
93 void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio );