11 void writeFile( const std::string& name, const std::string& content ) {
19 std::string readFile( const std::string& name ) {
20 std::ifstream t( name );
21 std::string res = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
27 std::string writeBackFile( const std::string& serial, const std::string& cert, const std::string& keydir ) {
28 std::string filename = keydir;
29 mkdir( filename.c_str(), 0755 );
31 mkdir( filename.c_str(), 0755 );
34 if( serial.length() < 3 ) {
37 first = serial.substr( 0, serial.length() - 3 );
40 filename += "/" + first;
41 mkdir( filename.c_str(), 0755 );
42 filename += "/" + serial + ".crt";
43 writeFile( filename, cert );
47 bool isDigit( char c ) {
48 return ( c >= '0' ) && ( c <= '9' );
51 std::pair<bool, time_t> parseDate( const std::string& date ) {
52 if( date.size() != 10 || date[4] != '-' || date[7] != '-' ) {
53 return std::pair<bool, time_t>( false, 0 );
56 if( !isDigit( date[0] )
57 || !isDigit( date[1] )
58 || !isDigit( date[2] )
59 || !isDigit( date[3] )
60 || !isDigit( date[5] )
61 || !isDigit( date[6] )
62 || !isDigit( date[8] )
63 || !isDigit( date[9] ) ) {
64 return std::pair<bool, time_t>( false, 0 );
71 t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
72 t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
73 t.tm_mday = std::stoi( date.substr( 8, 2 ) );
74 setenv( "TZ", "UTC", 1 );
76 std::time_t res = mktime( &t );
78 std::size_t siz = strftime( check, 11, "%Y-%m-%d", &t );
81 return std::pair<bool, time_t>( false, 0 ); // NO-COVERAGE (by contract of strftime)
84 std::string checkS( check, siz );
86 if( checkS != date ) {
87 return std::pair<bool, time_t>( false, 0 );
90 return std::pair<bool, time_t>( true, res );
93 std::pair<bool, time_t> addMonths( std::time_t t, int32_t count ) {
94 std::tm* parsed = gmtime( &t );
96 if( !parsed || count <= 0 || count > 24 ) { // FIXED MAX-Validity-Length
97 return std::pair<bool, time_t>( false, 0 );
100 parsed->tm_mon += count;
101 int oldday = parsed->tm_mday;
102 setenv( "TZ", "UTC", 1 );
104 std::time_t res = mktime( parsed );
106 if( parsed->tm_mday != oldday ) {
108 res = mktime( parsed );
111 return std::pair<bool, time_t>( true, res );
115 std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& date ) {
116 if( date[date.size() - 1] != 'm' ) {
117 return std::pair<bool, time_t>( false, 0 );
122 int num = std::stoi( date.substr( 0, date.size() - 1 ) , &end );
124 if( end != date.size() - 1 ) {
125 return std::pair<bool, time_t>( false, 0 );
128 return addMonths( t, num );
129 } catch( const std::invalid_argument& a ) {
130 return std::pair<bool, time_t>( false, 0 );
131 } catch( const std::out_of_range& a ) {
132 return std::pair<bool, time_t>( false, 0 );
135 std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& date ) {
136 if( date[date.size() - 1] != 'y' ) {
137 return std::pair<bool, time_t>( false, 0 );
142 int num = std::stoi( date.substr( 0, date.size() - 1 ), &end );
144 if( end != date.size() - 1 ) {
145 return std::pair<bool, time_t>( false, 0 );
148 return addMonths( t, num * 12 );
149 } catch( std::invalid_argument& a ) {
150 return std::pair<bool, time_t>( false, 0 );
151 } catch( std::out_of_range& a ) {
152 return std::pair<bool, time_t>( false, 0 );