PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/qdigidoc-0.4.1/client/AccessCert.cpp

#
C++ | 214 lines | 171 code | 23 blank | 20 comment | 21 complexity | 25a1c4fae578a86770c3685a2a44ab2d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * QDigiDocClient
  3. *
  4. * Copyright (C) 2009-2010 Estonian Informatics Centre
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include "AccessCert.h"
  22. #include "DigiDoc.h"
  23. #include "QSigner.h"
  24. #include "common/SslCertificate.h"
  25. #include "common/sslConnect.h"
  26. #include <QDateTime>
  27. #include <QDesktopServices>
  28. #include <QDir>
  29. #include <QDomElement>
  30. #include <QDomNodeList>
  31. #include <QFile>
  32. #include <QMessageBox>
  33. #include <QUrl>
  34. AccessCert::AccessCert( QWidget *parent )
  35. : QObject( parent )
  36. , m_parent( parent )
  37. {
  38. m_cert = DigiDoc::getConfValue( DigiDoc::PKCS12Cert );
  39. m_pass = DigiDoc::getConfValue( DigiDoc::PKCS12Pass );
  40. }
  41. AccessCert::~AccessCert()
  42. {
  43. DigiDoc::setConfValue( DigiDoc::PKCS12Cert, m_cert );
  44. DigiDoc::setConfValue( DigiDoc::PKCS12Pass, m_pass );
  45. }
  46. bool AccessCert::download( QSigner *signer, const QString &card, const QString &filename )
  47. {
  48. signer->lock();
  49. SSLConnect *ssl = new SSLConnect();
  50. ssl->setPKCS11( DigiDoc::getConfValue( DigiDoc::PKCS11Module ), false );
  51. ssl->setCard( card );
  52. bool retry = false;
  53. do
  54. {
  55. retry = false;
  56. ssl->waitForFinished( SSLConnect::AccessCert );
  57. switch( ssl->error() )
  58. {
  59. case SSLConnect::PinCanceledError:
  60. delete ssl;
  61. signer->unlock();
  62. return false;
  63. case SSLConnect::PinInvalidError:
  64. showWarning( ssl->errorString() );
  65. retry = true;
  66. break;
  67. default:
  68. if( !ssl->errorString().isEmpty() )
  69. {
  70. showWarning( tr("Error downloading server access certificate!\n%1").arg( ssl->errorString() ) );
  71. delete ssl;
  72. signer->unlock();
  73. return false;
  74. }
  75. break;
  76. }
  77. }
  78. while( retry );
  79. QByteArray result = ssl->result();
  80. delete ssl;
  81. signer->unlock();
  82. if( result.isEmpty() )
  83. {
  84. showWarning( tr("Empty result!") );
  85. return false;
  86. }
  87. QDomDocument domDoc;
  88. if( !domDoc.setContent( QString::fromUtf8( result ) ) )
  89. {
  90. showWarning( tr("Error parsing server access certificate result!") );
  91. return false;
  92. }
  93. QDomElement e = domDoc.documentElement();
  94. QDomNodeList status = e.elementsByTagName( "StatusCode" );
  95. if( status.isEmpty() )
  96. {
  97. showWarning( tr("Error parsing server access certificate result!") );
  98. return false;
  99. }
  100. switch( status.item(0).toElement().text().toInt() )
  101. {
  102. case 1: //need to order cert manually from SK web
  103. QDesktopServices::openUrl( QUrl( "http://www.sk.ee/toend/" ) );
  104. return false;
  105. case 2: //got error, show message from MessageToDisplay element
  106. showWarning( tr("Error downloading server access certificate!\n%1")
  107. .arg( e.elementsByTagName( "MessageToDisplay" ).item(0).toElement().text() ) );
  108. return false;
  109. default: break; //ok
  110. }
  111. QString cert = e.elementsByTagName( "TokenData" ).item(0).toElement().text();
  112. if ( cert.isEmpty() )
  113. {
  114. showWarning( tr("Error reading server access certificate - empty content!") );
  115. return false;
  116. }
  117. QString path = QDesktopServices::storageLocation( QDesktopServices::DataLocation );
  118. if ( !QDir( path ).exists() )
  119. QDir().mkpath( path );
  120. QFile f( QString( "%1/%2.p12" ).arg( path ).arg( filename ) );
  121. if ( !f.open( QIODevice::WriteOnly|QIODevice::Truncate ) )
  122. {
  123. showWarning( tr("Failed to save server access certificate file to %1!\n%2")
  124. .arg( f.fileName() )
  125. .arg( f.errorString() ) );
  126. return false;
  127. }
  128. f.write( QByteArray::fromBase64( cert.toLatin1() ) );
  129. DigiDoc::setConfValue( DigiDoc::PKCS12Cert, m_cert = f.fileName() );
  130. DigiDoc::setConfValue( DigiDoc::PKCS12Pass, m_pass = e.elementsByTagName( "TokenPassword" ).item(0).toElement().text() );
  131. return true;
  132. }
  133. void AccessCert::showWarning( const QString &msg )
  134. { QMessageBox::warning( m_parent, tr( "Server access certificate" ), msg ); }
  135. bool AccessCert::showWarning2( const QString &msg )
  136. {
  137. return QMessageBox::No == QMessageBox::warning( m_parent, tr( "Server access certificate" ),
  138. msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );
  139. }
  140. bool AccessCert::validate()
  141. {
  142. m_cert = DigiDoc::getConfValue( DigiDoc::PKCS12Cert );
  143. m_pass = DigiDoc::getConfValue( DigiDoc::PKCS12Pass );
  144. QFile f( m_cert );
  145. if( !f.exists() )
  146. {
  147. if( showWarning2( tr("Did not find any server access certificate!\nStart downloading?") ) )
  148. {
  149. DigiDoc::setConfValue( DigiDoc::PKCS12Cert, QVariant() );
  150. DigiDoc::setConfValue( DigiDoc::PKCS12Pass, QVariant() );
  151. return true;
  152. }
  153. }
  154. else if( !f.open( QIODevice::ReadOnly ) )
  155. {
  156. if( showWarning2( tr("Failed to read server access certificate!\nStart downloading?") ) )
  157. {
  158. DigiDoc::setConfValue( DigiDoc::PKCS12Cert, QVariant() );
  159. DigiDoc::setConfValue( DigiDoc::PKCS12Pass, QVariant() );
  160. return true;
  161. }
  162. }
  163. else
  164. {
  165. PKCS12Certificate p12Cert( &f, m_pass.toLatin1() );
  166. if( p12Cert.error() == PKCS12Certificate::InvalidPassword )
  167. {
  168. if( showWarning2( tr("Server access certificate password is not valid!\nStart downloading?") ) )
  169. {
  170. DigiDoc::setConfValue( DigiDoc::PKCS12Cert, QVariant() );
  171. DigiDoc::setConfValue( DigiDoc::PKCS12Pass, QVariant() );
  172. return true;
  173. }
  174. }
  175. else if( !p12Cert.certificate().isValid() )
  176. {
  177. if( showWarning2( tr("Server access certificate is not valid!\nStart downloading?") ) )
  178. {
  179. DigiDoc::setConfValue( DigiDoc::PKCS12Cert, QVariant() );
  180. DigiDoc::setConfValue( DigiDoc::PKCS12Pass, QVariant() );
  181. return true;
  182. }
  183. }
  184. else if( p12Cert.certificate().expiryDate() < QDateTime::currentDateTime().addDays( 8 ) &&
  185. !showWarning2( tr("Server access certificate is about to expire!\nStart downloading?") ) )
  186. return false;
  187. else
  188. return true;
  189. }
  190. return false;
  191. }