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