]> WPIA git - cassiopeia.git/blob - src/io/record.h
add: Implement signing based on requested "wish time"
[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     void append( std::string& str, T val ) {
56         str.append( ( char* ) &val, sizeof( T ) );
57     }
58
59     template <class T>
60     void read( std::string::iterator& it, T& val ) {
61         char* data = ( char* ) &val;
62
63         for( size_t i = 0; i < sizeof( T ); i++ ) {
64             data[i] = *it;
65             it++;
66         }
67     }
68
69     std::string packToString() {
70         std::string res;
71         res.reserve( RECORD_HEADER_SIZE );
72         append( res, command );
73         append( res, flags );
74         append( res, sessid );
75         append( res, command_count );
76         append( res, totalLength );
77         append( res, offset );
78         append( res, payloadLength );
79         return res;
80     }
81
82     void unpackFromString( std::string str ) {
83         if( str.size() != RECORD_HEADER_SIZE ) {
84             throw "Invalid string length";
85         }
86
87         auto it =  str.begin();
88         read( it, command );
89         read( it, flags );
90         read( it, sessid );
91         read( it, command_count );
92         read( it, totalLength );
93         read( it, offset );
94         read( it, payloadLength );
95     }
96
97 };
98
99 std::string parseCommand( RecordHeader& head, const std::string input, std::shared_ptr<std::ostream> log );
100
101 void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio, std::shared_ptr<std::ostream> log );