/* * CERT_License.cpp * * Created by Andrew Salamon on 9/15/08. * Copyright 2008 Machine Perception Laboratory, University of California San Diego. All rights reserved. * */ #include "CERT_License.h" #include #include namespace CERT { static const int defaultDuration = 365; const int License::invalid = 0; const int License::internal = 1804289383; const int License::evaluation = 846930886; const int License::licensed = 1681692777; // 1714636915 // 1957747793 static inline bool isValidType( int _type ) { return (License::internal == _type) || (License::evaluation == _type) || (License::licensed == _type); } bg::date License::stringToDate( const std::string &dtStr ) { bg::date dt( bg::from_simple_string(dtStr) ); return dt; } License::License() : type(invalid), start( bg::day_clock::local_day() ), duration(defaultDuration) { } License::License( const std::string &filepath ) { std::ifstream ifstr( filepath.c_str() ); if( ifstr.good() ) { fromStream( ifstr ); } } void License::clear() { type = invalid; name = ""; start = bg::day_clock::local_day(); duration = defaultDuration; } int License::getType() { return type; } void License::setType( int _type ) { if( (invalid != _type) && !isValidType( _type ) ) return; // throw exception type = _type; if( internal == type ) { name = "Machine Perception Lab, UCSD"; start = bg::date(bg::not_a_date_time); duration = 0; } else if( invalid == type ) { name = ""; start = bg::date(bg::not_a_date_time); duration = 0; } else { // Just make sure everything is valid if( start.is_not_a_date() ) start = bg::day_clock::local_day(); if( duration <= 0 ) duration = defaultDuration; if( evaluation == type ) name = ""; } } std::string License::getName() { return name; } void License::setName( const std::string &_name ) { name = _name; } bg::date License::getStartDate() { return start; } void License::setStartDate( const bg::date &dt ) { start = dt; } bg::date License::getEndDate() { return start + bg::days( duration ); } void License::setEndDate( const bg::date &dt ) { if( dt > start ) { bg::days dur( dt - start ); setDuration( dur.days() ); } } unsigned int License::getDuration() { return duration; } void License::setDuration( unsigned int dur ) { duration = dur; } std::string License::toString() { std::ostringstream ostr; ostr << name << '\n'; ostr << start.year() << '-' << start.month() << '-' << start.day() << '\n'; ostr << duration << '\n'; return ostr.str(); } static const std::string headerString( "=====BEGIN CERT LICENSE=====" ); static const std::string footerString( "=====END CERT LICENSE=====" ); void License::fromStream( std::istream &is ) { std::string header; std::string cyphertext; std::string footer; clear(); // Loop through all the lines in the file until we encounter a header string. // That way we can put other information in the License file. Copyrights, limitations, contact info, etc. while( (headerString != header) && !is.eof() ) std::getline( is, header ); std::getline( is, cyphertext ); std::getline( is, footer ); if( (headerString != header) || (footerString != footer) || (0 == cyphertext.length()) ) return; std::string plaintext( verifier.decrypt( cyphertext ) ); std::istringstream istr( plaintext ); int _type; istr >> _type; if( !isValidType( _type ) ) { _type = invalid; } setType( _type ); // vary the inputs we look for based on the type if( invalid != type ) { char ch; istr.get(ch); // skip newline std::getline( istr, name ); if( internal != type ) { if( evaluation != type ) { std::string dateString; std::getline( istr, dateString ); start = stringToDate( dateString ); } istr >> duration; } } } void License::toStream( std::ostream &os ) { std::ostringstream ostr; // vary the outputs we generate based on the type ostr << type << std::endl; if( invalid != type ) { ostr << name << std::endl; if( internal != type ) { if( evaluation != type ) ostr << start.year() << '-' << start.month() << '-' << start.day() << std::endl; ostr << duration << std::endl; } } std::string cyphertext = verifier.encrypt( ostr.str() ); os << headerString << std::endl; os << cyphertext << std::endl; os << footerString << std::endl; } void License::setPrivateKeyPath( const std::string &path ) { verifier.loadPrivateKey( path ); } int License::daysLeft() { bg::date today = bg::day_clock::local_day(); if( today < start ) return 0; // If the current date is before the start something is wrong bg::date endDate = getEndDate(); bg::days daysLeft = endDate - today; return daysLeft.days(); } } // end namespace CERT std::istream &operator>>( std::istream &is, CERT::License &license ) { license.fromStream( is ); return is; } std::ostream &operator<<( std::ostream &os, CERT::License &license ) { license.toStream( os ); return os; }