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