keygenWidget.cc 7.87 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41 42
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
43 44 45

#include <QtGui>
#include "keygenWidget.hh"
Jan Möbius's avatar
Jan Möbius committed
46
#include <iostream>
Jan Möbius's avatar
Jan Möbius committed
47

Jan Möbius's avatar
Jan Möbius committed
48 49 50
#include "salt.hh"

KeyGenWidget::KeyGenWidget(QMainWindow *parent)
Jan Möbius's avatar
Jan Möbius committed
51 52
    : QMainWindow(parent),
    valid_(false)
Jan Möbius's avatar
Jan Möbius committed
53
{
Jan Möbius's avatar
Jan Möbius committed
54 55
  setupUi(this);
  connect(generateButton,SIGNAL(clicked()),this,SLOT(slotGenerateButton()));
Jan Möbius's avatar
Jan Möbius committed
56
  
57 58
  
  connect(splitButton,SIGNAL(clicked()),this,SLOT(slotSplit()));
Jan Möbius's avatar
Jan Möbius committed
59 60 61 62 63 64 65
  
  connect(requestData,SIGNAL(textChanged()),this,SLOT(slotAnalyze()));
  
  // connect spinboxes forexpiry date
  connect(days  ,SIGNAL(valueChanged(int)),this,SLOT(slotDate()));
  connect(months,SIGNAL(valueChanged(int)),this,SLOT(slotDate()));
  connect(years ,SIGNAL(valueChanged(int)),this,SLOT(slotDate()));
66 67 68 69 70 71 72
  
  // Automatically set expire date to current date
  // For security reasons no default span is set here!
  expires->setDate( QDate::currentDate());
  
}

Jan Möbius's avatar
Jan Möbius committed
73
void KeyGenWidget::slotDate() {
Jan Möbius's avatar
Jan Möbius committed
74
  QDate today = QDate::currentDate();
75 76 77 78 79 80 81
  today = today.addDays(days->value());
  today = today.addMonths(months->value());
  today = today.addYears(years->value());
  
  expires->setDate(today);
}

Jan Möbius's avatar
Jan Möbius committed
82 83 84 85 86 87
void KeyGenWidget::slotAnalyze() {
  
  // Convert to text and split to elements
  QString inputData = requestData->toPlainText();
  QStringList data = inputData.split('\n',QString::SkipEmptyParts);
  
88
  // This is never a valid request!
Jan Möbius's avatar
Jan Möbius committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  if ( data.size() < 6 ) {
    
    QPalette p = requestData->palette();
    p.setColor( QPalette::Base, QColor(255,0,0) );
    requestData->setPalette(p);
    
    valid_ = false;
    
    return;
  } else {
    QPalette p = requestData->palette();
    p.setColor( QPalette::Base, QColor(255,255,255) );
    requestData->setPalette(p);
  }
  
  // Get strings
  QString name        = data[0].simplified();
  QString coreHash    = data[1].simplified();
  QString pluginHash  = data[2].simplified();
  QString cpuHash     = data[3].simplified();
  QString productHash = data[4].simplified();
  
  QStringList macHashes;
  for ( int i = 5 ; i < data.size() - 1; ++i)
    macHashes.push_back(data[i].simplified());
  
  QString requestSig = data[data.size() - 1].simplified();

  fileNameBox->setText(name);
  coreHashBox->setText(coreHash);
  pluginHashBox->setText(pluginHash);
  cpuHashBox->setText(cpuHash);
  productIDBox->setText(productHash);
  
  macHashBox->setText(macHashes.join("\n"));
  
  signatureBox->setText(requestSig);
  
  
  // Get the salts
  QString saltPre;
  ADD_SALT_PRE(saltPre);
  QString saltPost;
  ADD_SALT_POST(saltPost);
  

  QString keyRequest      = saltPre + name + coreHash + pluginHash + cpuHash + productHash + macHashes.join("") +  saltPost;
  QString requestSigCheck = QCryptographicHash::hash ( keyRequest.toAscii()  , QCryptographicHash::Sha1 ).toHex();
  
  if ( requestSig != requestSigCheck ) {
    QPalette p = signatureBox->palette();
    p.setColor( QPalette::Base, QColor(255,0,0) );
    signatureBox->setPalette(p);
    
    valid_ = false;
144 145
    if (! ignoreSigBox->isChecked() )
      return;
Jan Möbius's avatar
Jan Möbius committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    
  } else {
    QPalette p = signatureBox->palette();
    p.setColor( QPalette::Base, QColor(0,255,0) );
    signatureBox->setPalette(p);
    
  }

  QString expiryDate = expires->date().toString(Qt::ISODate);

  license_ = "";
  
  // Add basic hashes
  license_ += expiryDate + "\n";
  license_ += name + "\n";
  license_ += coreHash + "\n";
  license_ += pluginHash + "\n";
  license_ += cpuHash + "\n";
  license_ += productHash + "\n";
  license_ += macHashes.join("\n") + "\n";
  
  QString licenseTmp = saltPre + expiryDate + name + coreHash + pluginHash + cpuHash + productHash + macHashes.join("") +  saltPost;
  QString licenseHash = QCryptographicHash::hash ( licenseTmp.toAscii()  , QCryptographicHash::Sha1 ).toHex();
  
  // Prepend signature
  license_ = licenseHash + "\n" + license_;
  
173
  std::cerr << "Full license : \n" << license_.toStdString() << std::endl;
Jan Möbius's avatar
Jan Möbius committed
174
  
175 176 177
  // Only set valid, if the the request and the signature boxes match
  if ( requestSig == requestSigCheck )
    valid_ = true;
Jan Möbius's avatar
Jan Möbius committed
178 179 180 181
  
  licenseFileName_ = name;
}

182 183 184 185 186 187 188 189 190 191 192

void KeyGenWidget::slotSplit() {
  // Get request data
  QString inputData = requestData->toPlainText();
  
  // Split with ;
  QStringList data = inputData.split(";",QString::SkipEmptyParts);
  
  QString newText = data.join("\n");
  
  requestData->setText(newText);
Jan Möbius's avatar
Jan Möbius committed
193
  
Jan Möbius's avatar
Jan Möbius committed
194
}
Jan Möbius's avatar
Jan Möbius committed
195

Jan Möbius's avatar
Jan Möbius committed
196
KeyGenWidget::~KeyGenWidget() {
Jan Möbius's avatar
Jan Möbius committed
197 198 199

}

200

Jan Möbius's avatar
Jan Möbius committed
201 202
void KeyGenWidget::slotGenerateButton() {

203 204 205 206 207 208 209
  if ( ! valid_  ) {
    if (  ! ignoreSigBox->isChecked( ) ) {
      std::cerr << "Invalid Request " << std::endl;
      return;
    } else {
      std::cerr << "Invalid Request but overriding!!" << std::endl;
    }
Jan Möbius's avatar
Jan Möbius committed
210 211 212 213
  }
  
  std::cerr << "Writing License file to output : " << licenseFileName_.toStdString() << std::endl;
  QFile outFile(licenseFileName_ + ".lic");
Jan Möbius's avatar
Jan Möbius committed
214

Jan Möbius's avatar
Jan Möbius committed
215 216
  if (!outFile.open(QIODevice::WriteOnly|QIODevice::Text)) {
    QMessageBox::critical(this,tr("Unable to open file"),tr("Unable to Open output File"));
Jan Möbius's avatar
Jan Möbius committed
217
    return;
Jan Möbius's avatar
Jan Möbius committed
218
  }
Jan Möbius's avatar
Jan Möbius committed
219

Jan Möbius's avatar
Jan Möbius committed
220 221 222
  QTextStream output(&outFile);
  
  output << license_;
Jan Möbius's avatar
Jan Möbius committed
223

Jan Möbius's avatar
Jan Möbius committed
224
  outFile.close();
Jan Möbius's avatar
Jan Möbius committed
225
  
Jan Möbius's avatar
Jan Möbius committed
226
}