keygen.cc 3.75 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
#include <qapplication.h>
#include <QtGui>
#include <QFile>
Jan Möbius's avatar
Jan Möbius committed
4
#include <QMessageBox>
Jan Möbius's avatar
Jan Möbius committed
5
#include <iostream>
Jan Möbius's avatar
Jan Möbius committed
6
#include <OpenFlipper/LicenseManager/keyGen/keygenWidget.hh>
Jan Möbius's avatar
Jan Möbius committed
7
#include "salt.hh"
Jan Möbius's avatar
Jan Möbius committed
8 9 10

int main(int argc, char **argv)
{	
Jan Möbius's avatar
Jan Möbius committed
11 12
  QApplication a( argc, argv );

Jan Möbius's avatar
Jan Möbius committed
13 14 15 16 17 18
  // Get license information
  QString name;
  QString coreHash;
  QString pluginHash;
  QString macHash;
  QString requestSig;
Jan Möbius's avatar
Jan Möbius committed
19
  QString expiryDate;
Jan Möbius's avatar
Jan Möbius committed
20

Jan Möbius's avatar
Jan Möbius committed
21 22 23
  if (argc == 3) {
  
    QFile file(argv[1]);
Jan Möbius's avatar
Jan Möbius committed
24

Jan Möbius's avatar
Jan Möbius committed
25 26 27 28 29 30
    if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
      std::cerr << "Unable to open file " << std::endl;
      std::cerr  << " Call ./keygen <InfoFile> <Expiry date>" << std::endl;
      std::cerr << "Date is of the form: YYYY-MM-DD" << std::endl;
      return 1;
    }
Jan Möbius's avatar
Jan Möbius committed
31

Jan Möbius's avatar
Jan Möbius committed
32 33 34 35 36 37
    // Get license information
    name       = file.readLine().simplified();
    coreHash   = file.readLine().simplified();
    pluginHash = file.readLine().simplified();
    macHash    = file.readLine().simplified();
    requestSig = file.readLine().simplified();
Jan Möbius's avatar
Jan Möbius committed
38
    expiryDate = argv[2];
Jan Möbius's avatar
Jan Möbius committed
39

Jan Möbius's avatar
Jan Möbius committed
40
    file.close();
Jan Möbius's avatar
Jan Möbius committed
41

Jan Möbius's avatar
Jan Möbius committed
42
  } else {
Jan Möbius's avatar
Jan Möbius committed
43

Jan Möbius's avatar
Jan Möbius committed
44
    KeyGenWidget* keyGenWidget = new KeyGenWidget(0);
Jan Möbius's avatar
Jan Möbius committed
45

Jan Möbius's avatar
Jan Möbius committed
46 47 48 49 50 51
    // Automatically set expire date to current date + 1 Year
    QDate today = QDate::currentDate();
    keyGenWidget->expires->setDate(today.addYears(1));

    // Show the widget
    keyGenWidget->show();
Jan Möbius's avatar
Jan Möbius committed
52 53 54
    a.exec();


Jan Möbius's avatar
Jan Möbius committed
55 56
    QString inputData = keyGenWidget->requestData->toPlainText();
    QStringList data = inputData.split('\n',QString::SkipEmptyParts);
Jan Möbius's avatar
Jan Möbius committed
57 58 59 60 61 62 63 64 65 66 67 68
    
    if ( data.size() != 5 ) {
      std::cerr << "Request data has to containe 5 lines!" << std::endl;
      exit(1);
    }
  
    // Clean strings
    name       = data[0].simplified();
    coreHash   = data[1].simplified();
    pluginHash = data[2].simplified();
    macHash    = data[3].simplified();
    requestSig = data[4].simplified();
Jan Möbius's avatar
Jan Möbius committed
69 70 71

    expiryDate = keyGenWidget->expires->date().toString(Qt::ISODate);
    
Jan Möbius's avatar
Jan Möbius committed
72 73 74 75 76 77 78
  } 

  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;
Jan Möbius's avatar
Jan Möbius committed
79
  std::cerr << "expiryDate is             : " << expiryDate.toStdString() << std::endl;
Jan Möbius's avatar
Jan Möbius committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93

  // Get the salts
  QString saltPre;
  ADD_SALT_PRE(saltPre);
  QString saltPost;
  ADD_SALT_POST(saltPost);

  QString keyRequest = saltPre + name + coreHash + pluginHash + macHash + saltPost;
  QString requestSigCheck = QCryptographicHash::hash ( keyRequest.toAscii()  , QCryptographicHash::Sha1 ).toHex();
  
  if ( requestSig != requestSigCheck ) {
    QMessageBox::critical(0,"Signature of request invalid","The signature of the request is not valid");
    return 1;
  }
Jan Möbius's avatar
Jan Möbius committed
94 95 96

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

Jan Möbius's avatar
Jan Möbius committed
98 99 100 101 102 103
  if (!outFile.open(QIODevice::WriteOnly|QIODevice::Text)) {
    std::cerr << "Unable to open file " << std::endl;
    return 1;
  }

  QTextStream output(&outFile);
Jan Möbius's avatar
Jan Möbius committed
104 105 106 107 108 109 110 111

  // Add basic hashes
  output << name         << "\n";
  output << coreHash     << "\n";
  output << pluginHash   << "\n";
  output << macHash      << "\n";

  // Add expiryDate
Jan Möbius's avatar
Jan Möbius committed
112
  QDate date = QDate::fromString(expiryDate,Qt::ISODate);
Jan Möbius's avatar
Jan Möbius committed
113 114

  output << date.toString(Qt::ISODate) << "\n";
Jan Möbius's avatar
Jan Möbius committed
115
  
Jan Möbius's avatar
Jan Möbius committed
116

Jan Möbius's avatar
Jan Möbius committed
117

Jan Möbius's avatar
Jan Möbius committed
118 119
  // Sign the license file
  QString license = saltPre + name + coreHash + pluginHash + macHash + date.toString(Qt::ISODate) + saltPost;
Jan Möbius's avatar
Jan Möbius committed
120 121 122

  license = saltPre + license + saltPost;
  QString licenseHash = QCryptographicHash::hash ( license.toAscii()  , QCryptographicHash::Sha1 ).toHex();
Jan Möbius's avatar
Jan Möbius committed
123
  
Jan Möbius's avatar
Jan Möbius committed
124 125 126 127
  output << licenseHash;

  outFile.close();

Jan Möbius's avatar
Jan Möbius committed
128
  return 0;
Jan Möbius's avatar
Jan Möbius committed
129 130 131

}