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