]> WPIA git - cassiopeia.git/blob - src/io/record.h
fmt: Whitespace
[cassiopeia.git] / src / io / 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         SET_WISH_FROM = 0x12,
20         SET_WISH_TO = 0x13,
21         ADD_SAN = 0x18,
22         ADD_AVA = 0x19,
23         ADD_PROOF_LINE = 0x40,
24         SIGN = 0x80,
25         LOG_SAVED = 0x81,
26         REVOKE = 0x100,
27         GET_FULL_CRL = 0x101,
28         ADD_SERIAL = 0x102,
29         GET_TIMESTAMP = 0xC0,
30         GET_STATUS_REPORT = 0xD0
31     };
32
33     enum class SignerResult : uint16_t {
34         REVOKED = 0x100,
35         FULL_CRL = 0x101,
36         SAVE_LOG = 0x80,
37         CERTIFICATE = 0x81,
38         SIGNING_CA = 0x82,
39     };
40
41 public:
42     uint16_t command;
43     char flags;
44     uint32_t sessid;
45     uint16_t command_count;
46     uint32_t totalLength;
47     uint16_t offset;
48     uint16_t payloadLength;
49
50     RecordHeader() :
51         command( 0 ), flags( 0 ), sessid( 0 ), command_count( 0 ), totalLength( 0 ), offset( 0 ), payloadLength( 0 ) {
52     }
53
54     template <class T>
55     static void append( std::string& str, T val ) {
56         str.append( ( char* ) &val, sizeof( T ) );
57     }
58
59     template <class T>
60     static void read( std::string::const_iterator& it, T& val ) {
61         union typeConversion {
62             char buf[sizeof( T )];
63             T value;
64
65             typeConversion( const T& v ) : value( v ) {}
66         };
67
68         typeConversion data( 0 );
69
70         for( size_t i = 0; i < sizeof( T ); i++ ) {
71             data.buf[i] = *it++;
72         }
73
74         val = data.value;
75     }
76
77     std::string packToString() {
78         std::string res;
79         res.reserve( RECORD_HEADER_SIZE );
80         append( res, command );
81         append( res, flags );
82         append( res, sessid );
83         append( res, command_count );
84         append( res, totalLength );
85         append( res, offset );
86         append( res, payloadLength );
87         return res;
88     }
89
90     void unpackFromString( const std::string& str ) {
91         if( str.size() != RECORD_HEADER_SIZE ) {
92             throw "Invalid string length";
93         }
94
95         auto it =  str.cbegin();
96         read( it, command );
97         read( it, flags );
98         read( it, sessid );
99         read( it, command_count );
100         read( it, totalLength );
101         read( it, offset );
102         read( it, payloadLength );
103     }
104
105 };
106
107 std::string parseCommand( RecordHeader& head, const std::string& input, std::shared_ptr<std::ostream> log );
108
109 void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio, std::shared_ptr<std::ostream> log );