keygenWidget.cc 7.93 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

Matthias Möller's avatar
Matthias Möller committed
44 45 46 47 48 49 50

#if QT_VERSION >= 0x050000 
  #include <QtWidgets>
#else
  #include <QtGui>
#endif

Jan Möbius's avatar
Jan Möbius committed
51
#include "keygenWidget.hh"
Jan Möbius's avatar
Jan Möbius committed
52
#include <iostream>
Jan Möbius's avatar
Jan Möbius committed
53

Jan Möbius's avatar
Jan Möbius committed
54 55 56
#include "salt.hh"

KeyGenWidget::KeyGenWidget(QMainWindow *parent)
Jan Möbius's avatar
Jan Möbius committed
57 58
    : QMainWindow(parent),
    valid_(false)
Jan Möbius's avatar
Jan Möbius committed
59
{
Jan Möbius's avatar
Jan Möbius committed
60 61
  setupUi(this);
  connect(generateButton,SIGNAL(clicked()),this,SLOT(slotGenerateButton()));
Jan Möbius's avatar
Jan Möbius committed
62
  
63 64
  
  connect(splitButton,SIGNAL(clicked()),this,SLOT(slotSplit()));
Jan Möbius's avatar
Jan Möbius committed
65 66 67 68 69 70 71
  
  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()));
72 73 74 75 76 77 78
  
  // 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
79
void KeyGenWidget::slotDate() {
Jan Möbius's avatar
Jan Möbius committed
80
  QDate today = QDate::currentDate();
81 82 83 84 85 86 87
  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
88 89 90 91 92 93
void KeyGenWidget::slotAnalyze() {
  
  // Convert to text and split to elements
  QString inputData = requestData->toPlainText();
  QStringList data = inputData.split('\n',QString::SkipEmptyParts);
  
94
  // This is never a valid request!
Jan Möbius's avatar
Jan Möbius committed
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 144 145 146 147 148 149
  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;
150 151
    if (! ignoreSigBox->isChecked() )
      return;
Jan Möbius's avatar
Jan Möbius committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    
  } 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_;
  
179
  std::cerr << "Full license : \n" << license_.toStdString() << std::endl;
Jan Möbius's avatar
Jan Möbius committed
180
  
181 182 183
  // 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
184 185 186 187
  
  licenseFileName_ = name;
}

188 189 190 191 192 193 194 195 196 197 198

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
199
  
Jan Möbius's avatar
Jan Möbius committed
200
}
Jan Möbius's avatar
Jan Möbius committed
201

Jan Möbius's avatar
Jan Möbius committed
202
KeyGenWidget::~KeyGenWidget() {
Jan Möbius's avatar
Jan Möbius committed
203 204 205

}

206

Jan Möbius's avatar
Jan Möbius committed
207 208
void KeyGenWidget::slotGenerateButton() {

209 210 211 212 213 214 215
  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
216 217 218 219
  }
  
  std::cerr << "Writing License file to output : " << licenseFileName_.toStdString() << std::endl;
  QFile outFile(licenseFileName_ + ".lic");
Jan Möbius's avatar
Jan Möbius committed
220

Jan Möbius's avatar
Jan Möbius committed
221 222
  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
223
    return;
Jan Möbius's avatar
Jan Möbius committed
224
  }
Jan Möbius's avatar
Jan Möbius committed
225

Jan Möbius's avatar
Jan Möbius committed
226 227 228
  QTextStream output(&outFile);
  
  output << license_;
Jan Möbius's avatar
Jan Möbius committed
229

Jan Möbius's avatar
Jan Möbius committed
230
  outFile.close();
Jan Möbius's avatar
Jan Möbius committed
231
  
Jan Möbius's avatar
Jan Möbius committed
232
}