util/src/network/ssl/qsslerror.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 /*!
       
    44     \class QSslError
       
    45     \brief The QSslError class provides an SSL error.
       
    46     \since 4.3
       
    47 
       
    48     \reentrant
       
    49     \ingroup network
       
    50     \ingroup ssl
       
    51     \inmodule QtNetwork
       
    52 
       
    53     QSslError provides a simple API for managing errors during QSslSocket's
       
    54     SSL handshake.
       
    55 
       
    56     \sa QSslSocket, QSslCertificate, QSslCipher
       
    57 */
       
    58 
       
    59 /*!
       
    60     \enum QSslError::SslError
       
    61 
       
    62     Describes all recognized errors that can occur during an SSL handshake.
       
    63     
       
    64     \value NoError
       
    65     \value UnableToGetIssuerCertificate
       
    66     \value UnableToDecryptCertificateSignature
       
    67     \value UnableToDecodeIssuerPublicKey
       
    68     \value CertificateSignatureFailed
       
    69     \value CertificateNotYetValid
       
    70     \value CertificateExpired
       
    71     \value InvalidNotBeforeField
       
    72     \value InvalidNotAfterField
       
    73     \value SelfSignedCertificate
       
    74     \value SelfSignedCertificateInChain
       
    75     \value UnableToGetLocalIssuerCertificate
       
    76     \value UnableToVerifyFirstCertificate
       
    77     \value CertificateRevoked
       
    78     \value InvalidCaCertificate
       
    79     \value PathLengthExceeded
       
    80     \value InvalidPurpose
       
    81     \value CertificateUntrusted
       
    82     \value CertificateRejected
       
    83     \value SubjectIssuerMismatch
       
    84     \value AuthorityIssuerSerialNumberMismatch
       
    85     \value NoPeerCertificate
       
    86     \value HostNameMismatch
       
    87     \value UnspecifiedError
       
    88     \value NoSslSupport
       
    89 
       
    90     \sa QSslError::errorString()
       
    91 */
       
    92 
       
    93 #include "qsslerror.h"
       
    94 #include "qsslsocket.h"
       
    95 #ifndef QT_NO_DEBUG_STREAM
       
    96 #include <QtCore/qdebug.h>
       
    97 
       
    98 QT_BEGIN_NAMESPACE
       
    99 #endif
       
   100 
       
   101 class QSslErrorPrivate
       
   102 {
       
   103 public:
       
   104     QSslError::SslError error;
       
   105     QSslCertificate certificate;
       
   106 };
       
   107 
       
   108 /*!
       
   109     Constructs a QSslError object with no error and default certificate. 
       
   110 
       
   111 */
       
   112 
       
   113 // RVCT compiler in debug build does not like about default values in const-
       
   114 // So as an workaround we define all constructor overloads here explicitly
       
   115 QSslError::QSslError()
       
   116     : d(new QSslErrorPrivate)
       
   117 {
       
   118     d->error = QSslError::NoError;
       
   119     d->certificate = QSslCertificate();
       
   120 }
       
   121 
       
   122 /*!
       
   123     Constructs a QSslError object. The argument specifies the \a
       
   124     error that occurred.
       
   125 
       
   126 */
       
   127 QSslError::QSslError(SslError error)
       
   128     : d(new QSslErrorPrivate)
       
   129 {
       
   130     d->error = error;
       
   131     d->certificate = QSslCertificate();
       
   132 }
       
   133 
       
   134 /*!
       
   135     Constructs a QSslError object. The two arguments specify the \a
       
   136     error that occurred, and which \a certificate the error relates to.
       
   137 
       
   138     \sa QSslCertificate
       
   139 */
       
   140 QSslError::QSslError(SslError error, const QSslCertificate &certificate)
       
   141     : d(new QSslErrorPrivate)
       
   142 {
       
   143     d->error = error;
       
   144     d->certificate = certificate;
       
   145 }
       
   146 
       
   147 /*!
       
   148     Constructs an identical copy of \a other.
       
   149 */
       
   150 QSslError::QSslError(const QSslError &other)
       
   151     : d(new QSslErrorPrivate)
       
   152 {
       
   153     *d.data() = *other.d.data();
       
   154 }
       
   155 
       
   156 /*!
       
   157     Destroys the QSslError object.
       
   158 */
       
   159 QSslError::~QSslError()
       
   160 {
       
   161 }
       
   162 
       
   163 /*!
       
   164     \since 4.4
       
   165 
       
   166     Assigns the contents of \a other to this error.
       
   167 */
       
   168 QSslError &QSslError::operator=(const QSslError &other)
       
   169 {
       
   170     *d.data() = *other.d.data();
       
   171     return *this;
       
   172 }
       
   173 
       
   174 /*!
       
   175     \since 4.4
       
   176 
       
   177     Returns true if this error is equal to \a other; otherwise returns false.
       
   178 */
       
   179 bool QSslError::operator==(const QSslError &other) const
       
   180 {
       
   181     return d->error == other.d->error
       
   182         && d->certificate == other.d->certificate;
       
   183 }
       
   184 
       
   185 /*!
       
   186     \fn bool QSslError::operator!=(const QSslError &other) const
       
   187     \since 4.4
       
   188 
       
   189     Returns true if this error is not equal to \a other; otherwise returns
       
   190     false.
       
   191 */
       
   192 
       
   193 /*!
       
   194     Returns the type of the error.
       
   195 
       
   196     \sa errorString(), certificate()
       
   197 */
       
   198 QSslError::SslError QSslError::error() const
       
   199 {
       
   200     return d->error;
       
   201 }
       
   202 
       
   203 /*!
       
   204     Returns a short localized human-readable description of the error.
       
   205 
       
   206     \sa error(), certificate()
       
   207 */
       
   208 QString QSslError::errorString() const
       
   209 {
       
   210     QString errStr;
       
   211     switch (d->error) {
       
   212     case NoError:
       
   213         errStr = QSslSocket::tr("No error");
       
   214         break;
       
   215     case UnableToGetIssuerCertificate:
       
   216         errStr = QSslSocket::tr("The issuer certificate could not be found");
       
   217         break;
       
   218     case UnableToDecryptCertificateSignature:
       
   219         errStr = QSslSocket::tr("The certificate signature could not be decrypted");
       
   220         break;
       
   221     case UnableToDecodeIssuerPublicKey:
       
   222         errStr = QSslSocket::tr("The public key in the certificate could not be read");
       
   223         break;
       
   224     case CertificateSignatureFailed:
       
   225         errStr = QSslSocket::tr("The signature of the certificate is invalid");
       
   226         break;
       
   227     case CertificateNotYetValid:
       
   228         errStr = QSslSocket::tr("The certificate is not yet valid");
       
   229         break;
       
   230     case CertificateExpired:
       
   231         errStr = QSslSocket::tr("The certificate has expired");
       
   232         break;
       
   233     case InvalidNotBeforeField:
       
   234         errStr = QSslSocket::tr("The certificate's notBefore field contains an invalid time");
       
   235         break;
       
   236     case InvalidNotAfterField:
       
   237         errStr = QSslSocket::tr("The certificate's notAfter field contains an invalid time");
       
   238         break;
       
   239     case SelfSignedCertificate:
       
   240         errStr = QSslSocket::tr("The certificate is self-signed, and untrusted");
       
   241         break;
       
   242     case SelfSignedCertificateInChain:
       
   243         errStr = QSslSocket::tr("The root certificate of the certificate chain is self-signed, and untrusted");
       
   244         break;
       
   245     case UnableToGetLocalIssuerCertificate:
       
   246         errStr = QSslSocket::tr("The issuer certificate of a locally looked up certificate could not be found");
       
   247         break;
       
   248     case UnableToVerifyFirstCertificate:
       
   249         errStr = QSslSocket::tr("No certificates could be verified");
       
   250         break;
       
   251     case InvalidCaCertificate:
       
   252         errStr = QSslSocket::tr("One of the CA certificates is invalid");
       
   253         break;
       
   254     case PathLengthExceeded:
       
   255         errStr = QSslSocket::tr("The basicConstraints path length parameter has been exceeded");
       
   256         break;
       
   257     case InvalidPurpose:
       
   258         errStr = QSslSocket::tr("The supplied certificate is unsuitable for this purpose");
       
   259         break;
       
   260     case CertificateUntrusted:
       
   261         errStr = QSslSocket::tr("The root CA certificate is not trusted for this purpose");
       
   262         break;
       
   263     case CertificateRejected:
       
   264         errStr = QSslSocket::tr("The root CA certificate is marked to reject the specified purpose");
       
   265         break;
       
   266     case SubjectIssuerMismatch: // hostname mismatch
       
   267         errStr = QSslSocket::tr("The current candidate issuer certificate was rejected because its"
       
   268                                 " subject name did not match the issuer name of the current certificate");
       
   269         break;
       
   270     case AuthorityIssuerSerialNumberMismatch:
       
   271         errStr = QSslSocket::tr("The current candidate issuer certificate was rejected because"
       
   272                              " its issuer name and serial number was present and did not match the"
       
   273                              " authority key identifier of the current certificate");
       
   274         break;
       
   275     case NoPeerCertificate:
       
   276         errStr = QSslSocket::tr("The peer did not present any certificate");
       
   277         break;
       
   278     case HostNameMismatch:
       
   279         errStr = QSslSocket::tr("The host name did not match any of the valid hosts"
       
   280                              " for this certificate");
       
   281         break;
       
   282     case NoSslSupport:
       
   283         break;
       
   284     default:
       
   285         errStr = QSslSocket::tr("Unknown error");
       
   286         break;
       
   287     }
       
   288 
       
   289     return errStr;
       
   290 }
       
   291 
       
   292 /*!
       
   293     Returns the certificate associated with this error, or a null certificate
       
   294     if the error does not relate to any certificate.
       
   295 
       
   296     \sa error(), errorString()
       
   297 */
       
   298 QSslCertificate QSslError::certificate() const
       
   299 {
       
   300     return d->certificate;
       
   301 }
       
   302 
       
   303 #ifndef QT_NO_DEBUG_STREAM
       
   304 //class QDebug;
       
   305 QDebug operator<<(QDebug debug, const QSslError &error)
       
   306 {
       
   307     debug << error.errorString();
       
   308     return debug;
       
   309 }
       
   310 QDebug operator<<(QDebug debug, const QSslError::SslError &error)
       
   311 {
       
   312     debug << QSslError(error).errorString();
       
   313     return debug;
       
   314 }
       
   315 #endif
       
   316 
       
   317 QT_END_NAMESPACE