keygenWidget.cc 6.59 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
4
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
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen       *
*                           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
51
#include "salt.hh"

KeyGenWidget::KeyGenWidget(QMainWindow *parent)
    : QMainWindow(parent)
Jan Möbius's avatar
Jan Möbius committed
52
{
Jan Möbius's avatar
Jan Möbius committed
53
54
  setupUi(this);
  connect(generateButton,SIGNAL(clicked()),this,SLOT(slotGenerateButton()));
Jan Möbius's avatar
Jan Möbius committed
55
  
56
57
58
59
60
61
62
63
64
65
66
  
  connect(splitButton,SIGNAL(clicked()),this,SLOT(slotSplit()));
  connect(validButton,SIGNAL(clicked()),this,SLOT(slotValid()));
  
  // Automatically set expire date to current date
  // For security reasons no default span is set here!
  expires->setDate( QDate::currentDate());
  
}

void KeyGenWidget::slotValid() {
Jan Möbius's avatar
Jan Möbius committed
67
  QDate today = QDate::currentDate();
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  today = today.addDays(days->value());
  today = today.addMonths(months->value());
  today = today.addYears(years->value());
  
  expires->setDate(today);
}


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
86
  
Jan Möbius's avatar
Jan Möbius committed
87
}
Jan Möbius's avatar
Jan Möbius committed
88

89

Jan Möbius's avatar
Jan Möbius committed
90
KeyGenWidget::~KeyGenWidget() {
Jan Möbius's avatar
Jan Möbius committed
91
92
93

}

Jan Möbius's avatar
Jan Möbius committed
94
95
void KeyGenWidget::slotGenerateButton() {

Jan Möbius's avatar
Jan Möbius committed
96
97
98
99
  QString inputData = requestData->toPlainText();
  QStringList data = inputData.split('\n',QString::SkipEmptyParts);

  if ( data.size() != 5) {
100
    QMessageBox::critical(this,tr("Wrong request data"),tr("The request has to contain 5 lines of data"));
Jan Möbius's avatar
Jan Möbius committed
101
    return;
Jan Möbius's avatar
Jan Möbius committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  } else {

    // Clean strings
    QString name       = data[0].simplified();
    QString coreHash   = data[1].simplified();
    QString pluginHash = data[2].simplified();
    QString macHash    = data[3].simplified();
    QString requestSig = data[4].simplified();

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

    std::cerr << "Generating key for Plugin : " << name.toStdString()       << std::endl;
    std::cerr << "Core Hash                 : " << coreHash.toStdString()   << std::endl;
    std::cerr << "Plugin Hash               : " << pluginHash.toStdString() << std::endl;
    std::cerr << "macHash is                : " << macHash.toStdString()    << std::endl;
    std::cerr << "requestSignature is       : " << requestSig.toStdString() << std::endl;
    std::cerr << "expiryDate is             : " << expiryDate.toStdString() << std::endl;

    // Get the salts
    QString saltPre;
    ADD_SALT_PRE(saltPre);
    QString saltPost;
    ADD_SALT_POST(saltPost);
Jan Möbius's avatar
Jan Möbius committed
125

Jan Möbius's avatar
Jan Möbius committed
126
127
128
129
    QString keyRequest = saltPre + name + coreHash + pluginHash + macHash + saltPost;
    QString requestSigCheck = QCryptographicHash::hash ( keyRequest.toAscii()  , QCryptographicHash::Sha1 ).toHex();
    
    if ( requestSig != requestSigCheck ) {
130
      QMessageBox::critical(this,tr("Signature of request invalid"),tr("The signature of the request is not valid"));
Jan Möbius's avatar
Jan Möbius committed
131
      return;
Jan Möbius's avatar
Jan Möbius committed
132
    }
Jan Möbius's avatar
Jan Möbius committed
133

Jan Möbius's avatar
Jan Möbius committed
134
135
136
137
    std::cerr << "Writing License file to output : " << name.toStdString() << std::endl;
    QFile outFile(name + ".lic");

    if (!outFile.open(QIODevice::WriteOnly|QIODevice::Text)) {
138
      QMessageBox::critical(this,tr("Unable to open file"),tr("Unable to Open output File"));
Jan Möbius's avatar
Jan Möbius committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
      return;
    }

    QTextStream output(&outFile);

    // Add basic hashes
    output << name         << "\n";
    output << coreHash     << "\n";
    output << pluginHash   << "\n";
    output << macHash      << "\n";
    output << expiryDate << "\n";
    
    // Sign the license file
    QString license = saltPre + name + coreHash + pluginHash + macHash + expiryDate + saltPost;
    QString licenseHash = QCryptographicHash::hash ( license.toAscii()  , QCryptographicHash::Sha1 ).toHex();
    
    output << licenseHash;

    outFile.close();
  }
  
  close();
Jan Möbius's avatar
Jan Möbius committed
161
 
Jan Möbius's avatar
Jan Möbius committed
162
}