• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.12
  • kdelibs
  • kio
  • kssl
ksslkeygen.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2001 George Staikos <staikos@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 
22 #include "ksslkeygen.h"
23 #include "ksslkeygen_p.h"
24 #include "ui_keygenwizard.h"
25 
26 #include <kdebug.h>
27 #include <klocale.h>
28 #include <kmessagebox.h>
29 #include <kopenssl.h>
30 #include <kprogressdialog.h>
31 #include <kstandarddirs.h>
32 #include <ktemporaryfile.h>
33 #include <kwallet.h>
34 
35 #include <assert.h>
36 
37 KSSLKeyGenWizardPage2::KSSLKeyGenWizardPage2(QWidget* parent)
38  : QWizardPage(parent)
39 {
40  ui2 = new Ui_KGWizardPage2;
41  ui2->setupUi(this);
42  connect(ui2->_password1, SIGNAL(textChanged(QString)), this, SLOT(slotPassChanged()));
43  connect(ui2->_password2, SIGNAL(textChanged(QString)), this, SLOT(slotPassChanged()));
44 }
45 
46 bool KSSLKeyGenWizardPage2::isComplete() const
47 {
48  return ui2->_password1->text() == ui2->_password2->text() && ui2->_password1->text().length() >= 4;
49 }
50 
51 void KSSLKeyGenWizardPage2::slotPassChanged()
52 {
53  emit completeChanged(); // well maybe it hasn't changed, but it might have; QWizard calls isComplete() to find out
54 }
55 
56 QString KSSLKeyGenWizardPage2::password() const
57 {
58  Q_ASSERT(isComplete());
59  return ui2->_password1->text();
60 }
61 
63 
64 class KSSLKeyGenPrivate
65 {
66 public:
67  KSSLKeyGenPrivate()
68  : idx(-1)
69  {
70  }
71  int idx;
72  Ui_KGWizardPage1 *ui1;
73  KSSLKeyGenWizardPage2* page2;
74 };
75 
76 KSSLKeyGen::KSSLKeyGen(QWidget *parent)
77  : QWizard(parent), d(new KSSLKeyGenPrivate)
78 {
79 #ifdef KSSL_HAVE_SSL
80 
81  QWizardPage* page1 = new QWizardPage(this);
82  page1->setTitle(i18n("KDE Certificate Request"));
83  d->ui1 = new Ui_KGWizardPage1;
84  d->ui1->setupUi(page1);
85  addPage(page1);
86  //setHelpEnabled(page1, false);
87 
88  d->page2 = new KSSLKeyGenWizardPage2(this);
89  d->page2->setTitle(i18n("KDE Certificate Request - Password"));
90  addPage(d->page2);
91 #else
92  // tell him he doesn't have SSL
93 #endif
94 }
95 
96 
97 KSSLKeyGen::~KSSLKeyGen() {
98  delete d->ui1;
99  delete d;
100 }
101 
102 bool KSSLKeyGen::validateCurrentPage() {
103  if (currentPage() != d->page2)
104  return true;
105 
106  assert(d->idx >= 0 && d->idx <= 3); // for now
107 
108  // Generate the CSR
109  int bits;
110  switch (d->idx) {
111  case 0:
112  bits = 2048;
113  break;
114  case 1:
115  bits = 1024;
116  break;
117  case 2:
118  bits = 768;
119  break;
120  case 3:
121  bits = 512;
122  break;
123  default:
124  KMessageBox::sorry(this, i18n("Unsupported key size."), i18n("KDE SSL Information"));
125  return false;
126  }
127 
128  KProgressDialog *kpd = new KProgressDialog(this);
129  kpd->setObjectName("progress dialog");
130  kpd->setWindowTitle(i18n("KDE"));
131  kpd->setLabelText(i18n("Please wait while the encryption keys are generated..."));
132  kpd->progressBar()->setValue(0);
133  kpd->show();
134  // FIXME - progress dialog won't show this way
135 
136  int rc = generateCSR("This CSR" /*FIXME */, d->page2->password(), bits, 0x10001 /* This is the traditional exponent used */);
137  if (rc != 0) // error
138  return false;
139 
140  kpd->progressBar()->setValue(100);
141 
142 #if 0 // TODO: implement
143  if (rc == 0 && KWallet::Wallet::isEnabled()) {
144  rc = KMessageBox::questionYesNo(this, i18n("Do you wish to store the passphrase in your wallet file?"), QString(), KGuiItem(i18n("Store")), KGuiItem(i18n("Do Not Store")));
145  if (rc == KMessageBox::Yes) {
146  KWallet::Wallet *w = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(), winId());
147  if (w) {
148  // FIXME: store passphrase in wallet
149  delete w;
150  }
151  }
152  }
153 #endif
154 
155  kpd->deleteLater();
156  return true;
157 }
158 
159 
160 int KSSLKeyGen::generateCSR(const QString& name, const QString& pass, int bits, int e) {
161 #ifdef KSSL_HAVE_SSL
162  KOSSL *kossl = KOSSL::self();
163  int rc;
164 
165  X509_REQ *req = kossl->X509_REQ_new();
166  if (!req) {
167  return -2;
168  }
169 
170  EVP_PKEY *pkey = kossl->EVP_PKEY_new();
171  if (!pkey) {
172  kossl->X509_REQ_free(req);
173  return -4;
174  }
175 
176  RSA *rsakey = kossl->RSA_generate_key(bits, e, NULL, NULL);
177  if (!rsakey) {
178  kossl->X509_REQ_free(req);
179  kossl->EVP_PKEY_free(pkey);
180  return -3;
181  }
182 
183  rc = kossl->EVP_PKEY_assign(pkey, EVP_PKEY_RSA, (char *)rsakey);
184 
185  rc = kossl->X509_REQ_set_pubkey(req, pkey);
186 
187  // Set the subject
188  X509_NAME *n = kossl->X509_NAME_new();
189 
190  kossl->X509_NAME_add_entry_by_txt(n, (char*)LN_countryName, MBSTRING_UTF8, (unsigned char*)name.toLocal8Bit().data(), -1, -1, 0);
191  kossl->X509_NAME_add_entry_by_txt(n, (char*)LN_organizationName, MBSTRING_UTF8, (unsigned char*)name.toLocal8Bit().data(), -1, -1, 0);
192  kossl->X509_NAME_add_entry_by_txt(n, (char*)LN_organizationalUnitName, MBSTRING_UTF8, (unsigned char*)name.toLocal8Bit().data(), -1, -1, 0);
193  kossl->X509_NAME_add_entry_by_txt(n, (char*)LN_localityName, MBSTRING_UTF8, (unsigned char*)name.toLocal8Bit().data(), -1, -1, 0);
194  kossl->X509_NAME_add_entry_by_txt(n, (char*)LN_stateOrProvinceName, MBSTRING_UTF8, (unsigned char*)name.toLocal8Bit().data(), -1, -1, 0);
195  kossl->X509_NAME_add_entry_by_txt(n, (char*)LN_commonName, MBSTRING_UTF8, (unsigned char*)name.toLocal8Bit().data(), -1, -1, 0);
196  kossl->X509_NAME_add_entry_by_txt(n, (char*)LN_pkcs9_emailAddress, MBSTRING_UTF8, (unsigned char*)name.toLocal8Bit().data(), -1, -1, 0);
197 
198  rc = kossl->X509_REQ_set_subject_name(req, n);
199 
200 
201  rc = kossl->X509_REQ_sign(req, pkey, kossl->EVP_md5());
202 
203  // We write it to the database and then the caller can obtain it
204  // back from there. Yes it's inefficient, but it doesn't happen
205  // often and this way things are uniform.
206 
207  KGlobal::dirs()->addResourceType("kssl", "data", "kssl");
208 
209  QString path = KGlobal::dirs()->saveLocation("kssl");
210  KTemporaryFile csrFile;
211  csrFile.setAutoRemove(false);
212  csrFile.setPrefix(path + "csr_");
213  csrFile.setSuffix(".der");
214 
215  if (!csrFile.open()) {
216  kossl->X509_REQ_free(req);
217  kossl->EVP_PKEY_free(pkey);
218  return -5;
219  }
220 
221  KTemporaryFile p8File;
222  p8File.setAutoRemove(false);
223  p8File.setPrefix(path + "pkey_");
224  p8File.setSuffix(".p8");
225 
226  if (!p8File.open()) {
227  kossl->X509_REQ_free(req);
228  kossl->EVP_PKEY_free(pkey);
229  return -5;
230  }
231 
232  FILE *csr_fs = fopen(QFile::encodeName(csrFile.fileName()), "r+");
233  FILE *p8_fs = fopen(QFile::encodeName(p8File.fileName()), "r+");
234 
235  kossl->i2d_X509_REQ_fp(csr_fs, req);
236 
237  kossl->i2d_PKCS8PrivateKey_fp(p8_fs, pkey,
238  kossl->EVP_bf_cbc(), pass.toLocal8Bit().data(),
239  pass.length(), 0L, 0L);
240 
241  // FIXME Write kconfig entry to store the filenames under the md5 hash
242 
243  kossl->X509_REQ_free(req);
244  kossl->EVP_PKEY_free(pkey);
245 
246  fclose(csr_fs);
247  fclose(p8_fs);
248 
249  return 0;
250 #else
251  return -1;
252 #endif
253 }
254 
255 
256 QStringList KSSLKeyGen::supportedKeySizes() {
257  QStringList x;
258 
259 #ifdef KSSL_HAVE_SSL
260  x << i18n("2048 (High Grade)")
261  << i18n("1024 (Medium Grade)")
262  << i18n("768 (Low Grade)")
263  << i18n("512 (Low Grade)");
264 #else
265  x << i18n("No SSL support.");
266 #endif
267 
268  return x;
269 }
270 
271 void KSSLKeyGen::setKeySize(int idx)
272 {
273  d->idx = idx;
274 }
275 
276 #include "ksslkeygen.moc"
277 
278 #include "ksslkeygen_p.moc"
KStandardDirs::saveLocation
QString saveLocation(const char *type, const QString &suffix=QString(), bool create=true) const
i18n
QString i18n(const char *text)
KSSLKeyGenWizardPage2
Definition: ksslkeygen_p.h:27
KProgressDialog::progressBar
QProgressBar * progressBar()
kdebug.h
KWallet::Wallet
KStandardDirs::addResourceType
bool addResourceType(const char *type, const QString &relativename, bool priority=true)
KTemporaryFile::setPrefix
void setPrefix(const QString &prefix)
QWizardPage
QWidget
KGlobal::dirs
KStandardDirs * dirs()
KWallet::Wallet::isEnabled
static bool isEnabled()
ksslkeygen.h
QString
KTemporaryFile
KTemporaryFile::setSuffix
void setSuffix(const QString &suffix)
klocale.h
KWallet::Wallet::openWallet
static Wallet * openWallet(const QString &name, WId w, OpenType ot=Synchronous)
KSSLKeyGen::KSSLKeyGen
KSSLKeyGen(QWidget *parent=0L)
Construct a keygen dialog.
Definition: ksslkeygen.cpp:76
ksslkeygen_p.h
KGuiItem
QStringList
KMessageBox::sorry
static void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
KOSSL
#define KOSSL
Definition: kopenssl.h:25
KSSLKeyGen::supportedKeySizes
static QStringList supportedKeySizes()
List the supported key sizes.
Definition: ksslkeygen.cpp:256
KSSLKeyGenWizardPage2::password
QString password() const
Definition: ksslkeygen.cpp:56
KSSLKeyGen::~KSSLKeyGen
virtual ~KSSLKeyGen()
Destroy this dialog.
Definition: ksslkeygen.cpp:97
KMessageBox::questionYesNo
static int questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
KSSLKeyGenWizardPage2::KSSLKeyGenWizardPage2
KSSLKeyGenWizardPage2(QWidget *parent)
Definition: ksslkeygen.cpp:37
KSSLKeyGen::setKeySize
void setKeySize(int idx)
Set the key size.
Definition: ksslkeygen.cpp:271
kprogressdialog.h
KSSLKeyGen::generateCSR
int generateCSR(const QString &name, const QString &pass, int bits, int e=0x10001)
Generate the certificate signing request.
Definition: ksslkeygen.cpp:160
ktemporaryfile.h
KProgressDialog
kwallet.h
kstandarddirs.h
KProgressDialog::setLabelText
void setLabelText(const QString &text)
fopen
FILE * fopen(const QString &pathname, const char *mode)
KMessageBox::Yes
KSSLKeyGenWizardPage2::isComplete
bool isComplete() const
Definition: ksslkeygen.cpp:46
kopenssl.h
kmessagebox.h
KWallet::Wallet::LocalWallet
static const QString LocalWallet()
QWizard
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal