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 ) {
30 std::string filename = keydir;
31 if( 0 != mkdir( filename.c_str(), 0755 ) ) {
36 if( 0 != mkdir( filename.c_str(), 0755 ) ) {
41 if( serial.length() < 3 ) {
44 first = serial.substr( 0, serial.length() - 3 );
47 filename += "/" + first;
48 if( 0 != mkdir( filename.c_str(), 0755 ) ) {
51 filename += "/" + serial + ".crt";
52 writeFile( filename, cert );
57 bool isDigit( char c ) {
58 return ( c >= '0' ) && ( c <= '9' );
61 std::pair<bool, time_t> parseDate( const std::string& date ) {
62 if( date.size() != 10 || date[4] != '-' || date[7] != '-' ) {
63 return std::pair<bool, time_t>( false, 0 );
66 if( !isDigit( date[0] )
67 || !isDigit( date[1] )
68 || !isDigit( date[2] )
69 || !isDigit( date[3] )
70 || !isDigit( date[5] )
71 || !isDigit( date[6] )
72 || !isDigit( date[8] )
73 || !isDigit( date[9] ) ) {
74 return std::pair<bool, time_t>( false, 0 );
81 t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
82 t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
83 t.tm_mday = std::stoi( date.substr( 8, 2 ) );
84 setenv( "TZ", "UTC", 1 );
86 std::time_t res = mktime( &t );
88 std::size_t siz = strftime( check, 11, "%Y-%m-%d", &t );
91 return std::pair<bool, time_t>( false, 0 ); // NO-COVERAGE (by contract of strftime)
94 std::string checkS( check, siz );
96 if( checkS != date ) {
97 return std::pair<bool, time_t>( false, 0 );
100 return std::pair<bool, time_t>( true, res );
103 std::pair<bool, time_t> addMonths( std::time_t t, int32_t count ) {
104 std::tm* parsed = gmtime( &t );
106 if( !parsed || count <= 0 || count > 24 ) { // FIXED MAX-Validity-Length
107 return std::pair<bool, time_t>( false, 0 );
110 parsed->tm_mon += count;
111 int oldday = parsed->tm_mday;
112 setenv( "TZ", "UTC", 1 );
114 std::time_t res = mktime( parsed );
116 if( parsed->tm_mday != oldday ) {
118 res = mktime( parsed );
121 return std::pair<bool, time_t>( true, res );
125 std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& date ) {
126 if( date[date.size() - 1] != 'm' ) {
127 return std::pair<bool, time_t>( false, 0 );
132 int num = std::stoi( date.substr( 0, date.size() - 1 ) , &end );
134 if( end != date.size() - 1 ) {
135 return std::pair<bool, time_t>( false, 0 );
138 return addMonths( t, num );
139 } catch( const std::invalid_argument& a ) {
140 return std::pair<bool, time_t>( false, 0 );
141 } catch( const std::out_of_range& a ) {
142 return std::pair<bool, time_t>( false, 0 );
145 std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& date ) {
146 if( date[date.size() - 1] != 'y' ) {
147 return std::pair<bool, time_t>( false, 0 );
152 int num = std::stoi( date.substr( 0, date.size() - 1 ), &end );
154 if( end != date.size() - 1 ) {
155 return std::pair<bool, time_t>( false, 0 );
158 return addMonths( t, num * 12 );
159 } catch( std::invalid_argument& a ) {
160 return std::pair<bool, time_t>( false, 0 );
161 } catch( std::out_of_range& a ) {
162 return std::pair<bool, time_t>( false, 0 );