pkiutilities/untrustedcertificatedialog/src/untrustedcertificateinfobase.cpp
changeset 26 aad866c37519
child 30 cc1cea6aabaf
equal deleted inserted replaced
22:6b63ca65093a 26:aad866c37519
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Certificate info class for TLS untrusted certificate dialog.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "UntrustedCertificateInfoBase.h"
       
    19 
       
    20 const char *KHexNumberFormatSimple = "%X";
       
    21 const char *KHexNumberFormatTwoDigitsWithLeadingZeroes = "%02X";
       
    22 const int KCharsPerBlock = 2;
       
    23 const char KBlockSeparator = ' ';
       
    24 
       
    25 
       
    26 // ======== MEMBER FUNCTIONS ========
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // UntrustedCertificateInfoBase::UntrustedCertificateInfoBase()
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 UntrustedCertificateInfoBase::UntrustedCertificateInfoBase() : QObject(),
       
    33     mSubjectName(), mIssuerName(), mFingerprint(), mSerialNumber(), mValidFrom(), mValidTo(),
       
    34     mFormat(UnknownCertificate), mDigestAlgorithm(Unknown), mAsymmetricAlgorithm(Unknown)
       
    35 {
       
    36 }
       
    37 
       
    38 // ----------------------------------------------------------------------------
       
    39 // UntrustedCertificateInfoBase::~UntrustedCertificateInfoBase()
       
    40 // ----------------------------------------------------------------------------
       
    41 //
       
    42 UntrustedCertificateInfoBase::~UntrustedCertificateInfoBase()
       
    43 {
       
    44 }
       
    45 
       
    46 // ----------------------------------------------------------------------------
       
    47 // UntrustedCertificateInfoBase::subjectName()
       
    48 // ----------------------------------------------------------------------------
       
    49 //
       
    50 const QString UntrustedCertificateInfoBase::subjectName() const
       
    51 {
       
    52     return mSubjectName;
       
    53 }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // UntrustedCertificateInfoBase::issuerName()
       
    57 // ----------------------------------------------------------------------------
       
    58 //
       
    59 const QString UntrustedCertificateInfoBase::issuerName() const
       
    60 {
       
    61     return mIssuerName;
       
    62 }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // UntrustedCertificateInfoBase::fingerprint()
       
    66 // ----------------------------------------------------------------------------
       
    67 //
       
    68 const QByteArray UntrustedCertificateInfoBase::fingerprint() const
       
    69 {
       
    70     return mFingerprint;
       
    71 }
       
    72 
       
    73 // ----------------------------------------------------------------------------
       
    74 // UntrustedCertificateInfoBase::formattedFingerprint()
       
    75 // ----------------------------------------------------------------------------
       
    76 //
       
    77 const QString UntrustedCertificateInfoBase::formattedFingerprint() const
       
    78 {
       
    79     QString formatted;
       
    80     QByteArray fp = fingerprint();
       
    81 
       
    82     QString number;
       
    83     int blockIndex = 0;
       
    84     int count = fp.count();
       
    85     for (int index = 0; index < count; ++index) {
       
    86         if (blockIndex == KCharsPerBlock) {
       
    87             formatted.append(KBlockSeparator);
       
    88             blockIndex = 0;
       
    89         }
       
    90         number.sprintf(KHexNumberFormatTwoDigitsWithLeadingZeroes,
       
    91             static_cast<unsigned char>(fp.at(index)));
       
    92         formatted.append(number);
       
    93         ++blockIndex;
       
    94     }
       
    95 
       
    96     return formatted;
       
    97 }
       
    98 
       
    99 // ----------------------------------------------------------------------------
       
   100 // UntrustedCertificateInfoBase::serialNumber()
       
   101 // ----------------------------------------------------------------------------
       
   102 //
       
   103 const QByteArray UntrustedCertificateInfoBase::serialNumber() const
       
   104 {
       
   105     return mSerialNumber;
       
   106 }
       
   107 
       
   108 // ----------------------------------------------------------------------------
       
   109 // UntrustedCertificateInfoBase::formattedSerialNumber()
       
   110 // ----------------------------------------------------------------------------
       
   111 //
       
   112 const QString UntrustedCertificateInfoBase::formattedSerialNumber() const
       
   113 {
       
   114     QString formatted;
       
   115     QByteArray sn = serialNumber();
       
   116 
       
   117     QString number;
       
   118     int count = sn.count();
       
   119     for (int index = 0; index < count; ++index) {
       
   120         number.sprintf(KHexNumberFormatSimple,
       
   121             static_cast<unsigned char>(sn.at(index)));
       
   122         formatted.append(number);
       
   123     }
       
   124 
       
   125     return formatted;
       
   126 }
       
   127 
       
   128 // ----------------------------------------------------------------------------
       
   129 // UntrustedCertificateInfoBase::validFrom()
       
   130 // ----------------------------------------------------------------------------
       
   131 //
       
   132 const QDateTime UntrustedCertificateInfoBase::validFrom() const
       
   133 {
       
   134     return mValidFrom;
       
   135 }
       
   136 
       
   137 // ----------------------------------------------------------------------------
       
   138 // UntrustedCertificateInfoBase::validTo()
       
   139 // ----------------------------------------------------------------------------
       
   140 //
       
   141 const QDateTime UntrustedCertificateInfoBase::validTo() const
       
   142 {
       
   143     return mValidTo;
       
   144 }
       
   145 
       
   146 // ----------------------------------------------------------------------------
       
   147 // UntrustedCertificateInfoBase::format()
       
   148 // ----------------------------------------------------------------------------
       
   149 //
       
   150 const QString UntrustedCertificateInfoBase::format() const
       
   151 {
       
   152     QString format;
       
   153     switch (mFormat) {
       
   154         case X509Certificate:
       
   155             //: Type name for X509 certificates displayed in certificate details.
       
   156             // TODO: localised UI string
       
   157             format = tr("X509");
       
   158             break;
       
   159         default:
       
   160             //: Type name for unknown certificates displayed in certificate details.
       
   161             // TODO: localised UI string
       
   162             format = tr("Unknown");
       
   163             break;
       
   164     }
       
   165     return format;
       
   166 }
       
   167 
       
   168 // ----------------------------------------------------------------------------
       
   169 // UntrustedCertificateInfoBase::digestAlgorithm()
       
   170 // ----------------------------------------------------------------------------
       
   171 //
       
   172 const QString UntrustedCertificateInfoBase::digestAlgorithm() const
       
   173 {
       
   174     return algorithmName(mDigestAlgorithm);
       
   175 }
       
   176 
       
   177 // ----------------------------------------------------------------------------
       
   178 // UntrustedCertificateInfoBase::AsymmetricAlgorithm()
       
   179 // ----------------------------------------------------------------------------
       
   180 //
       
   181 const QString UntrustedCertificateInfoBase::asymmetricAlgorithm() const
       
   182 {
       
   183     return algorithmName(mAsymmetricAlgorithm);
       
   184 }
       
   185 
       
   186 // ----------------------------------------------------------------------------
       
   187 // UntrustedCertificateInfoBase::AsymmetricAlgorithm()
       
   188 // ----------------------------------------------------------------------------
       
   189 //
       
   190 const QString UntrustedCertificateInfoBase::combinedAlgorithmName() const
       
   191 {
       
   192     if (mDigestAlgorithm != Unknown && mAsymmetricAlgorithm != Unknown) {
       
   193         //: Format to combine digest and asymmetric algorithm names.
       
   194         //: %1 is digest (signing) and %2 is asymmetric (public-key) algorithm.
       
   195         // TODO: localised UI string needed
       
   196         return tr("%1%2").arg(digestAlgorithm()).arg(asymmetricAlgorithm());
       
   197     }
       
   198     //: Algorithm name for unknown algorithm.
       
   199     // TODO: localised UI string needed
       
   200     return tr("Unknown");
       
   201 }
       
   202 
       
   203 // ----------------------------------------------------------------------------
       
   204 // UntrustedCertificateInfoBase::isDateValid()
       
   205 // ----------------------------------------------------------------------------
       
   206 //
       
   207 bool UntrustedCertificateInfoBase::isDateValid() const
       
   208 {
       
   209     QDateTime current = QDateTime::currentDateTime();
       
   210     return (mValidFrom <= current && mValidTo >= current);
       
   211 }
       
   212 
       
   213 // ----------------------------------------------------------------------------
       
   214 // UntrustedCertificateInfoBase::algorithmName()
       
   215 // ----------------------------------------------------------------------------
       
   216 //
       
   217 const QString UntrustedCertificateInfoBase::algorithmName(Algorithm algorithm) const
       
   218 {
       
   219     QString name;
       
   220     switch (algorithm) {
       
   221         case RSA:
       
   222             //: Certificate details algorithm name
       
   223             // TODO: localized UI string
       
   224             name = tr("RSA");
       
   225             break;
       
   226         case DSA:
       
   227             //: Certificate details algorithm name
       
   228             // TODO: localized UI string
       
   229             name = tr("DSA");
       
   230             break;
       
   231         case DH:
       
   232             //: Certificate details algorithm name
       
   233             // TODO: localized UI string
       
   234             name = tr("DH");
       
   235             break;
       
   236         case MD2:
       
   237             //: Certificate details algorithm name
       
   238             // TODO: localized UI string
       
   239             name = tr("MD2");
       
   240             break;
       
   241         case MD5:
       
   242             //: Certificate details algorithm name
       
   243             // TODO: localized UI string
       
   244             name = tr("MD5");
       
   245             break;
       
   246         case SHA1:
       
   247             //: Certificate details algorithm name
       
   248             // TODO: localized UI string
       
   249             name = tr("SHA1");
       
   250             break;
       
   251         case SHA224:
       
   252         case SHA256:
       
   253         case SHA384:
       
   254         case SHA512:
       
   255             //: Certificate details algorithm name
       
   256             // TODO: localized UI string
       
   257             name = tr("SHA2");
       
   258             break;
       
   259         case Unknown:
       
   260         default:
       
   261             //: Certificate details algorithm name
       
   262             // TODO: localized UI string
       
   263             name = tr("Unknown");
       
   264             break;
       
   265     }
       
   266     return name;
       
   267 }
       
   268