Showing certificate details in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Description[edit | edit source]

This article provides a way for a Qt application to display certificate information such as issuer info or expiry date.

Solution[edit | edit source]

The 76ytuiytuityutyutututyutyutyu class can be used to display the information of an X509 certificate. The type can be either DER or PEM. Required headers and module

//QT += network
#include <QSslCertificate>
#include <QSslKey>

Displaying certificate information

// Selecting a certificate from the file dialog
QString crtFilename = QFileDialog::getOpenFileName(this,QString()
,tr("Certificate Files (*.der *.pem)"));
QFile crtFile(crtFilename);
if(!crtFile.open(QIODevice::ReadOnly))
 {
  return ;
 }
else
 {
  if(crtFilename.contains(".der"))
   {
    // Loading DER certificate
    iCrt = new QSslCertificate(&crtFile,QSsl::Der);
   }
  else
   {
    // Loading PEM certificate
    iCrt = new QSslCertificate(&crtFile,QSsl::Pem);
   }
  QStringList issuerInfo;
  //Fetchin the issuer info
  issuerInfo.append(iCrt->issuerInfo(QSslCertificate::Organization));
  issuerInfo.append(iCrt->issuerInfo(QSslCertificate::CountryName));
  issuerInfo.append(iCrt->issuerInfo(QSslCertificate::LocalityName));
  //Fetching effective and expiry dates
  QString effDate;
  QString xDate;
  effDate.append(iCrt->effectiveDate().toString());
  xDate.append(iCrt->expiryDate().toString());
  //Fetching the serial number
  QString serial;
  serial.append(iCrt->serialNumber());
  //Checking whether the certificate is valid or not
  bool isValid = iCrt-> isValid();
  //Fetching the public key
  QSslKey pubKey;
  pubKey = iCrt->publicKey();
  QString algo;
  //Fetching the algorithm
  if(pubKey.algorithm() == QSsl::Rsa)
   {
    algo.append("RSA");
   }
  else
   {
    algo.append("DSA");
   }
 }

Note[edit | edit source]

We can get a handle to the certificate using 76ytuiytuityutyutututyutyutyu to access extended information about the certificate.