/* * CERT_LicenseFile.cpp * * Created by Andrew Salamon on 9/15/08. * Copyright 2008 Machine Perception Laboratory, University of California San Diego. All rights reserved. * */ #include "CERT_LicenseFile.h" #include #include namespace CERT { LicenseFile::LicenseFile( const std::string &privateKeyPath ) : verifier(privateKeyPath), valid(false) { } bool LicenseFile::parseFile( const std::string &filename ) { std::ifstream ifstr( filename.c_str() ); if( ifstr.good() ) { return parseStream( ifstr ); } return false; } bool LicenseFile::parseStream( std::istream &istr ) { std::string line1; std::string line2; std::string cyphertext; unsigned int dur = 0; valid = false; getline( istr, line1 ); getline( istr, line2 ); istr >> dur; getline( istr, cyphertext ); // need to skip the newline that the previous line didn't use. getline( istr, cyphertext ); if( (line1.length() > 0) && (line2.length() > 0) && (dur > 0) ) { license.setName( line1 ); license.setStartDate( License::stringToDate( line2 ) ); license.setDuration( dur ); valid = validateLicense( cyphertext ); return true; } return false; } bool LicenseFile::validateLicense( const std::string &cyphertext ) { std::string plaintext( verifier.decrypt( cyphertext ) ); std::string licStr( license.toString() ); return (plaintext == licStr); } bool LicenseFile::writeToFile( const std::string &filename ) { std::ofstream ofstr( filename.c_str() ); if( ofstr.good() ) { return writeToStream( ofstr ); } return false; } bool LicenseFile::writeToStream( std::ostream &ostr ) { std::string licStr( license.toString() ); std::string cyphertext = verifier.encrypt( licStr ); ostr << licStr; ostr << cyphertext; return true; } License & LicenseFile::getLicense() { return license; } void LicenseFile::setLicense( License &_license ) { license = _license; } } // end namespace CERT