pkiutilities/untrustedcertificatedialog/src/untrustedcertificateinfo_symbian.cpp
branchRCL_3
changeset 50 03674e5abf46
parent 49 09b1ac925e3f
child 54 94da73d93b58
equal deleted inserted replaced
49:09b1ac925e3f 50:03674e5abf46
     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 "untrustedcertificateinfo_symbian.h"
       
    19 #include <signed.h>                             // TAlgorithmId
       
    20 #include <x509cert.h>                           // CX509Certificate
       
    21 #include <X509CertNameParser.h>                 // X509CertNameParser
       
    22 #include <hash.h>                               // CMD5
       
    23 
       
    24 
       
    25 // ======== LOCAL FUNCTIONS ========
       
    26 
       
    27 // ----------------------------------------------------------------------------
       
    28 // mapAlgorithm()
       
    29 // ----------------------------------------------------------------------------
       
    30 //
       
    31 UntrustedCertificateInfoBase::Algorithm mapAlgorithm(TAlgorithmId aAlgId)
       
    32 {
       
    33     UntrustedCertificateInfoBase::Algorithm algorithm =
       
    34         UntrustedCertificateInfoBase::Unknown;
       
    35     switch(aAlgId) {
       
    36         case ERSA:
       
    37             algorithm = UntrustedCertificateInfoBase::RSA;
       
    38             break;
       
    39         case EDSA:
       
    40             algorithm = UntrustedCertificateInfoBase::DSA;
       
    41             break;
       
    42         case EDH:
       
    43             algorithm = UntrustedCertificateInfoBase::DH;
       
    44             break;
       
    45         case EMD2:
       
    46             algorithm = UntrustedCertificateInfoBase::MD2;
       
    47             break;
       
    48         case EMD5:
       
    49             algorithm = UntrustedCertificateInfoBase::MD5;
       
    50             break;
       
    51         case ESHA1:
       
    52             algorithm = UntrustedCertificateInfoBase::SHA1;
       
    53             break;
       
    54         case ESHA224:
       
    55             algorithm = UntrustedCertificateInfoBase::SHA224;
       
    56             break;
       
    57         case ESHA256:
       
    58             algorithm = UntrustedCertificateInfoBase::SHA256;
       
    59             break;
       
    60         case ESHA384:
       
    61             algorithm = UntrustedCertificateInfoBase::SHA384;
       
    62             break;
       
    63         case ESHA512:
       
    64             algorithm = UntrustedCertificateInfoBase::SHA512;
       
    65             break;
       
    66         default:
       
    67             break;
       
    68     }
       
    69     return algorithm;
       
    70 }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // convertDateTime()
       
    74 // ----------------------------------------------------------------------------
       
    75 //
       
    76 void convertDateTime(const TTime& aFromTime, QDateTime& aToDateTime)
       
    77 {
       
    78     const TDateTime &symbianDateTime = aFromTime.DateTime();
       
    79 
       
    80     QDate date(symbianDateTime.Year(), symbianDateTime.Month()+1, symbianDateTime.Day()+1);
       
    81     QTime time(symbianDateTime.Hour(), symbianDateTime.Minute(), symbianDateTime.Second());
       
    82     aToDateTime.setDate(date);
       
    83     aToDateTime.setTime(time);
       
    84 }
       
    85 
       
    86 
       
    87 // ======== MEMBER FUNCTIONS ========
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // UntrustedCertificateInfoSymbian::UntrustedCertificateInfoSymbian()
       
    91 // ----------------------------------------------------------------------------
       
    92 //
       
    93 UntrustedCertificateInfoSymbian::UntrustedCertificateInfoSymbian(
       
    94     const QByteArray &aEncodedCert) : UntrustedCertificateInfoBase(),
       
    95     mCert(0), mMd5Fingerprint()
       
    96 {
       
    97     QT_TRAP_THROWING(ConstructL(aEncodedCert));
       
    98 }
       
    99 
       
   100 // ----------------------------------------------------------------------------
       
   101 // UntrustedCertificateInfoSymbian::~UntrustedCertificateInfoSymbian()
       
   102 // ----------------------------------------------------------------------------
       
   103 //
       
   104 UntrustedCertificateInfoSymbian::~UntrustedCertificateInfoSymbian()
       
   105 {
       
   106     delete mCert;
       
   107 }
       
   108 
       
   109 // ----------------------------------------------------------------------------
       
   110 // UntrustedCertificateInfoSymbian::commonNameMatches()
       
   111 // ----------------------------------------------------------------------------
       
   112 //
       
   113 bool UntrustedCertificateInfoSymbian::commonNameMatches(const QString &siteName) const
       
   114 {
       
   115     bool matches = false;
       
   116     QT_TRAP_THROWING(matches = CommonNameMatchesL(siteName));
       
   117     return matches;
       
   118 }
       
   119 
       
   120 // ----------------------------------------------------------------------------
       
   121 // UntrustedCertificateInfoSymbian::certificateDetails()
       
   122 // ----------------------------------------------------------------------------
       
   123 //
       
   124 QString UntrustedCertificateInfoSymbian::certificateDetails(const QString &siteName) const
       
   125 {
       
   126     // TODO: localized UI string needed
       
   127     QString details = tr("Service:\n%1\n\nIssuer:\n%2\n\nSubject:\n%3\n\n"
       
   128         "Valid from:\n%4\n\nValid until:\n%5\n\nCertificate format:\n%6\n\n"
       
   129         "Algorithm:\n%7\n\nSerial number:\n%8\n\n"
       
   130         "Fingerprint (SHA1):\n%9\n\nFingerprint (MD5):\n%10")
       
   131         .arg(siteName)                                  // %1
       
   132         .arg(issuerName())                              // %2
       
   133         .arg(subjectName())                             // %3
       
   134         .arg(validFrom().toString())                    // %4
       
   135         .arg(validTo().toString())                      // %5
       
   136         .arg(format())                                  // %6
       
   137         .arg(combinedAlgorithmName())                   // %7
       
   138         .arg(formattedSerialNumber(serialNumber()))     // %8
       
   139         .arg(formattedFingerprint(fingerprint()))       // %9
       
   140         .arg(formattedFingerprint(mMd5Fingerprint));    // %10
       
   141     return details;
       
   142 }
       
   143 
       
   144 // ----------------------------------------------------------------------------
       
   145 // UntrustedCertificateInfoSymbian::ConstructL()
       
   146 // ----------------------------------------------------------------------------
       
   147 //
       
   148 void UntrustedCertificateInfoSymbian::ConstructL(const QByteArray &aEncodedCert)
       
   149 {
       
   150     TPtrC8 encodedCert( reinterpret_cast<const TText8*>( aEncodedCert.constData() ),
       
   151         aEncodedCert.length() );
       
   152 
       
   153     ASSERT( mCert == 0 );
       
   154     mCert = CX509Certificate::NewL( encodedCert );
       
   155 
       
   156     HBufC16* subjectBuf = NULL;
       
   157     X509CertNameParser::SubjectFullNameL( *mCert, subjectBuf );
       
   158     CleanupStack::PushL( subjectBuf );
       
   159     QT_TRYCATCH_LEAVING( mSubjectName =
       
   160         QString::fromUtf16(subjectBuf->Ptr(), subjectBuf->Length()) );
       
   161     CleanupStack::PopAndDestroy( subjectBuf );
       
   162 
       
   163     HBufC16* issuerBuf = NULL;
       
   164     X509CertNameParser::IssuerFullNameL( *mCert, issuerBuf );
       
   165     CleanupStack::PushL( issuerBuf );
       
   166     QT_TRYCATCH_LEAVING( mIssuerName =
       
   167         QString::fromUtf16(issuerBuf->Ptr(), issuerBuf->Length()));
       
   168     CleanupStack::PopAndDestroy( issuerBuf );
       
   169 
       
   170     TPtrC8 fingerprint = mCert->Fingerprint();
       
   171     QT_TRYCATCH_LEAVING( mFingerprint = QByteArray::fromRawData(
       
   172         reinterpret_cast<const char*>(fingerprint.Ptr()), fingerprint.Length()) );
       
   173 
       
   174     mMd5Fingerprint = Md5FingerprintL( mCert->Encoding() );
       
   175 
       
   176     TPtrC8 serialNumber = mCert->SerialNumber();
       
   177     QT_TRYCATCH_LEAVING( mSerialNumber = QByteArray::fromRawData(
       
   178         reinterpret_cast<const char*>(serialNumber.Ptr()), serialNumber.Length()) );
       
   179 
       
   180     const CValidityPeriod& validityPeriod = mCert->ValidityPeriod();
       
   181     convertDateTime(validityPeriod.Start(), mValidFrom);
       
   182     convertDateTime(validityPeriod.Finish(), mValidTo);
       
   183 
       
   184     mFormat = X509Certificate;
       
   185 
       
   186     const CSigningAlgorithmIdentifier& alg = mCert->SigningAlgorithm();
       
   187     mDigestAlgorithm = mapAlgorithm(alg.DigestAlgorithm().Algorithm());
       
   188     mAsymmetricAlgorithm = mapAlgorithm(alg.AsymmetricAlgorithm().Algorithm());
       
   189 }
       
   190 
       
   191 // ----------------------------------------------------------------------------
       
   192 // UntrustedCertificateInfoSymbian::CommonNameMatchesL()
       
   193 // ----------------------------------------------------------------------------
       
   194 //
       
   195 bool UntrustedCertificateInfoSymbian::CommonNameMatchesL(const QString &siteName) const
       
   196 {
       
   197     bool matches = false;
       
   198     const CX500DistinguishedName& distinguishedName = mCert->SubjectName();
       
   199     HBufC* commonNameSymbian = distinguishedName.ExtractFieldL( KX520CommonName );
       
   200     if (commonNameSymbian) {
       
   201         CleanupStack::PushL(commonNameSymbian);
       
   202         QString commonName = QString::fromRawData(
       
   203             reinterpret_cast<const QChar*>(commonNameSymbian->Ptr()),
       
   204             commonNameSymbian->Length());
       
   205         matches = ( commonName == siteName );       // TODO: accept '*' chars in commonName?
       
   206         CleanupStack::PopAndDestroy(commonNameSymbian);
       
   207     }
       
   208     return matches;
       
   209 }
       
   210 
       
   211 // ----------------------------------------------------------------------------
       
   212 // UntrustedCertificateInfoSymbian::Md5FingerprintL()
       
   213 // ----------------------------------------------------------------------------
       
   214 //
       
   215 QByteArray UntrustedCertificateInfoSymbian::Md5FingerprintL( const TDesC8& aEncodedCert ) const
       
   216 {
       
   217     CMD5* md5 = CMD5::NewL();
       
   218     CleanupStack::PushL( md5 );
       
   219 
       
   220     const TPtrC8 fingerprintSymbian = md5->Hash( aEncodedCert );
       
   221     QByteArray fingerprint( reinterpret_cast<const char*>( fingerprintSymbian.Ptr() ),
       
   222         fingerprintSymbian.Length() );
       
   223 
       
   224     CleanupStack::PopAndDestroy( md5 );
       
   225     return fingerprint;
       
   226 }
       
   227