示例#1
0
void CertificateDialog::updateValue()
{
	const QSslCertificate certificate(m_certificates.value(m_ui->chainItemView->currentIndex().data(Qt::UserRole).toInt()));
	const CertificateField field(static_cast<CertificateField>(m_ui->detailsItemView->currentIndex().data(Qt::UserRole).toInt()));

	m_ui->valueTextEdit->clear();

	switch (field)
	{
		case ValidityField:
		case PublicKeyField:
		case ExtensionsField:
		case DigestField:
			break;
		case VersionField:
			m_ui->valueTextEdit->setPlainText(QString(certificate.version()));

			break;
		case SerialNumberField:
			m_ui->valueTextEdit->setPlainText(formatHex(QString(certificate.serialNumber()), QLatin1Char(':')));

			break;
		case SignatureAlgorithmField:
			m_ui->valueTextEdit->setPlainText(QRegularExpression(QLatin1String("Signature Algorithm:(.+)")).match(certificate.toText()).captured(1).trimmed());

			break;
		case IssuerField:
			{
				const QList<QByteArray> attributes(certificate.issuerInfoAttributes());

				for (int i = 0; i < attributes.count(); ++i)
				{
					m_ui->valueTextEdit->appendPlainText(QStringLiteral("%1 = %2").arg(QString(attributes.at(i))).arg(certificate.issuerInfo(attributes.at(i)).join(QLatin1String(", "))));
				}
			}

			break;
		case ValidityNotBeforeField:
			m_ui->valueTextEdit->setPlainText(certificate.effectiveDate().toString(QLatin1String("yyyy-MM-dd hh:mm:ss t")));

			break;
		case ValidityNotAfterField:
			m_ui->valueTextEdit->setPlainText(certificate.expiryDate().toString(QLatin1String("yyyy-MM-dd hh:mm:ss t")));

			break;
		case SubjectField:
			{
				const QList<QByteArray> attributes(certificate.subjectInfoAttributes());

				for (int i = 0; i < attributes.count(); ++i)
				{
					m_ui->valueTextEdit->appendPlainText(QStringLiteral("%1 = %2").arg(QString(attributes.at(i))).arg(certificate.subjectInfo(attributes.at(i)).join(QLatin1String(", "))));
				}
			}

			break;
		case PublicKeyValueField:
			{
				const QRegularExpression expression(QLatin1String("Public-Key:[.\\s\\S]+Modulus:([.\\s\\S]+)Exponent:(.+)"), QRegularExpression::MultilineOption);
				const QRegularExpressionMatch match(expression.match(certificate.toText()));

				if (match.hasMatch())
				{
					m_ui->valueTextEdit->setPlainText(tr("Modulus:\n%1\n\nExponent: %2").arg(formatHex(match.captured(1).trimmed().mid(3))).arg(match.captured(2).trimmed()));
				}
			}

			break;
		case PublicKeyAlgorithmField:
			m_ui->valueTextEdit->setPlainText(QRegularExpression(QLatin1String("Public Key Algorithm:(.+)")).match(certificate.toText()).captured(1).trimmed());

			break;
		case ExtensionField:
			{
				const QSslCertificateExtension extension(certificate.extensions().value(m_ui->detailsItemView->currentIndex().data(Qt::UserRole + 1).toInt()));

				m_ui->valueTextEdit->setPlainText(extension.isCritical() ? tr("Critical") : tr("Not Critical"));
				m_ui->valueTextEdit->appendPlainText(tr("OID: %1").arg(extension.oid()));

				if (!extension.value().isNull())
				{
					m_ui->valueTextEdit->appendPlainText(tr("Value:"));

					if (extension.value().type() == QVariant::List)
					{
						const QVariantList list(extension.value().toList());

						for (int i = 0; i < list.count(); ++i)
						{
							m_ui->valueTextEdit->appendPlainText(list.at(i).toString());
						}
					}
					else if (extension.value().type() == QVariant::Map)
					{
						const QVariantMap map(extension.value().toMap());
						QVariantMap::const_iterator iterator;

						for (iterator = map.constBegin(); iterator != map.constEnd(); ++iterator)
						{
							m_ui->valueTextEdit->appendPlainText(QStringLiteral("%1 = %2").arg(iterator.key()).arg(iterator.value().toString()));
						}
					}
					else
					{
						m_ui->valueTextEdit->appendPlainText(extension.value().toString());
					}
				}
			}

			break;
		case DigestSha1Field:
			m_ui->valueTextEdit->setPlainText(formatHex(QString(certificate.digest(QCryptographicHash::Sha1).toHex())));

			break;
		case DigestSha256Field:
			m_ui->valueTextEdit->setPlainText(formatHex(QString(certificate.digest(QCryptographicHash::Sha256).toHex())));

			break;
		default:
			break;
	}

	QTextCursor cursor(m_ui->valueTextEdit->textCursor());
	cursor.setPosition(0);

	m_ui->valueTextEdit->setTextCursor(cursor);
}
示例#2
0
void CertificateDialog::updateCertificate()
{
	const QSslCertificate certificate(m_certificates.value(m_ui->chainItemView->currentIndex().data(Qt::UserRole).toInt()));

	m_ui->detailsItemView->getSourceModel()->clear();

	createField(VersionField);
	createField(SerialNumberField);
	createField(SignatureAlgorithmField);
	createField(IssuerField);

	QStandardItem *validityItem(createField(ValidityField));
	validityItem->setFlags(Qt::ItemIsEnabled);

	createField(ValidityNotBeforeField, validityItem);
	createField(ValidityNotAfterField, validityItem);
	createField(SubjectField);

	QStandardItem *publicKeyItem(createField(PublicKeyField));
	publicKeyItem->setFlags(Qt::ItemIsEnabled);

	createField(PublicKeyAlgorithmField, publicKeyItem);
	createField(PublicKeyValueField, publicKeyItem);

	QStandardItem *extensionsItem(createField(ExtensionsField));
	extensionsItem->setFlags(Qt::ItemIsEnabled);
	extensionsItem->setEnabled(certificate.extensions().count() > 0);

	for (int i = 0; i < certificate.extensions().count(); ++i)
	{
		QString title(certificate.extensions().at(i).name());

		if (title == QLatin1String("authorityKeyIdentifier"))
		{
			title = tr("Authority Key Identifier");
		}
		else if (title == QLatin1String("subjectKeyIdentifier"))
		{
			title = tr("Subject Key Identifier");
		}
		else if (title == QLatin1String("keyUsage"))
		{
			title = tr("Key Usage");
		}
		else if (title == QLatin1String("certificatePolicies"))
		{
			title = tr("Certificate Policies");
		}
		else if (title == QLatin1String("policyMappings"))
		{
			title = tr("Policy Mappings");
		}
		else if (title == QLatin1String("subjectAltName"))
		{
			title = tr("Subject Alternative Name");
		}
		else if (title == QLatin1String("issuerAltName"))
		{
			title = tr("Issuer Alternative Name");
		}
		else if (title == QLatin1String("subjectDirectoryAttributes"))
		{
			title = tr("Subject Directory Attributes");
		}
		else if (title == QLatin1String("basicConstraints"))
		{
			title = tr("Basic Constraints");
		}
		else if (title == QLatin1String("nameConstraints"))
		{
			title = tr("Name Constraints");
		}
		else if (title == QLatin1String("policyConstraints"))
		{
			title = tr("Policy Constraints");
		}
		else if (title == QLatin1String("extendedKeyUsage"))
		{
			title = tr("Extended Key Usage");
		}
		else if (title == QLatin1String("crlDistributionPoints"))
		{
			title = tr("CRL Distribution Points");
		}
		else if (title == QLatin1String("inhibitAnyPolicy"))
		{
			title = tr("Inhibit Any Policy");
		}
		else if (title == QLatin1String("freshestCRL"))
		{
			title = tr("Delta CRL Distribution Point");
		}
		else if (title == QLatin1String("authorityInfoAccess"))
		{
			title = tr("Authority Information Access");
		}
		else if (title == QLatin1String("subjectInfoAccess"))
		{
			title = tr("Subject Information Access");
		}

		QMap<int, QVariant> data;
		data[Qt::DisplayRole] = title;
		data[Qt::UserRole + 1] = i;

		createField(ExtensionField, extensionsItem, data);
	}

	QStandardItem *digestItem(createField(DigestField));

	createField(DigestSha256Field, digestItem);
	createField(DigestSha1Field, digestItem);

	m_ui->detailsItemView->expandAll();
	m_ui->valueTextEdit->clear();
}