1
// vim:expandtab:shiftwidth=2:tabstop=2:
2
// Copyright (C) 2014 Canonical Ltd.
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
// Lesser General Public License for more details.
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this library; if not, write to the Free Software
16
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
#include "oxideqsslcertificate.h"
19
#include "oxideqsslcertificate_p.h"
25
#include "base/logging.h"
26
#include "base/time/time.h"
27
#include "net/base/hash_value.h"
28
#include "net/cert/x509_certificate.h"
29
#include "net/cert/x509_cert_types.h"
31
static QStringList GetPrincipalValue(const net::CertPrincipal& principal,
32
OxideQSslCertificate::PrincipalAttr attr) {
34
case OxideQSslCertificate::PrincipalAttrOrganizationName: {
36
for (size_t i = 0; i < principal.organization_names.size(); ++i) {
37
rv.push_back(QString::fromStdString(principal.organization_names[i]));
41
case OxideQSslCertificate::PrincipalAttrCommonName:
42
return QStringList(QString::fromStdString(principal.common_name));
43
case OxideQSslCertificate::PrincipalAttrLocalityName:
44
return QStringList(QString::fromStdString(principal.locality_name));
45
case OxideQSslCertificate::PrincipalAttrOrganizationUnitName: {
47
for (size_t i = 0; i < principal.organization_unit_names.size(); ++i) {
48
rv.push_back(QString::fromStdString(principal.organization_unit_names[i]));
52
case OxideQSslCertificate::PrincipalAttrCountryName:
53
return QStringList(QString::fromStdString(principal.country_name));
54
case OxideQSslCertificate::PrincipalAttrStateOrProvinceName:
55
return QStringList(QString::fromStdString(principal.state_or_province_name));
62
static QDateTime ToQDateTime(const base::Time& time) {
63
int64_t ms = (time - base::Time()).InMilliseconds();
64
return QDateTime::fromMSecsSinceEpoch(ms);
67
OxideQSslCertificatePrivate::OxideQSslCertificatePrivate(
68
const scoped_refptr<net::X509Certificate>& cert)
71
OxideQSslCertificatePrivate::~OxideQSslCertificatePrivate() {}
74
OxideQSslCertificate* OxideQSslCertificatePrivate::Create(
75
const scoped_refptr<net::X509Certificate>& cert,
77
return new OxideQSslCertificate(
78
*new OxideQSslCertificatePrivate(cert),
82
OxideQSslCertificate::OxideQSslCertificate(OxideQSslCertificatePrivate& dd,
87
OxideQSslCertificate::~OxideQSslCertificate() {}
89
QString OxideQSslCertificate::serialNumber() const {
90
Q_D(const OxideQSslCertificate);
92
const std::string& serial_number = d->x509_cert_->serial_number();
93
QByteArray ba(serial_number.data(), int(serial_number.size()));
95
return QString::fromUtf8(ba.toHex());
98
QString OxideQSslCertificate::subjectDisplayName() const {
99
Q_D(const OxideQSslCertificate);
101
return QString::fromStdString(d->x509_cert_->subject().GetDisplayName());
104
QString OxideQSslCertificate::issuerDisplayName() const {
105
Q_D(const OxideQSslCertificate);
107
return QString::fromStdString(d->x509_cert_->issuer().GetDisplayName());
110
QStringList OxideQSslCertificate::getSubjectInfo(PrincipalAttr attr) const {
111
Q_D(const OxideQSslCertificate);
113
return GetPrincipalValue(d->x509_cert_->subject(), attr);
116
QStringList OxideQSslCertificate::getIssuerInfo(PrincipalAttr attr) const {
117
Q_D(const OxideQSslCertificate);
119
return GetPrincipalValue(d->x509_cert_->issuer(), attr);
122
QDateTime OxideQSslCertificate::effectiveDate() const {
123
Q_D(const OxideQSslCertificate);
125
return ToQDateTime(d->x509_cert_->valid_start());
128
QDateTime OxideQSslCertificate::expiryDate() const {
129
Q_D(const OxideQSslCertificate);
131
return ToQDateTime(d->x509_cert_->valid_expiry());
134
QString OxideQSslCertificate::fingerprintSHA1() const {
135
Q_D(const OxideQSslCertificate);
137
const net::SHA1HashValue& hash = d->x509_cert_->fingerprint();
138
QByteArray ba(reinterpret_cast<const char *>(hash.data), sizeof(hash.data));
140
return QString::fromUtf8(ba.toHex());
143
bool OxideQSslCertificate::isExpired() const {
144
Q_D(const OxideQSslCertificate);
146
return d->x509_cert_->HasExpired();
149
OxideQSslCertificate* OxideQSslCertificate::issuer() const {
150
Q_D(const OxideQSslCertificate);
153
return d->issuer_.get();
156
const net::X509Certificate::OSCertHandles& handles =
157
d->x509_cert_->GetIntermediateCertificates();
158
if (handles.empty()) {
162
net::X509Certificate::OSCertHandle handle = handles[0];
164
net::X509Certificate::OSCertHandles intermediates;
165
for (size_t i = 1; i < handles.size(); ++i) {
166
intermediates.push_back(handles[i]);
169
scoped_refptr<net::X509Certificate> cert =
170
net::X509Certificate::CreateFromHandle(handle, intermediates);
171
d->issuer_.reset(OxideQSslCertificatePrivate::Create(cert));
173
return d->issuer_.get();
176
OxideQSslCertificate* OxideQSslCertificate::copy() const {
177
Q_D(const OxideQSslCertificate);
179
return OxideQSslCertificatePrivate::Create(d->x509_cert_);
182
QString OxideQSslCertificate::toPem() const {
183
Q_D(const OxideQSslCertificate);
186
if (!net::X509Certificate::GetPEMEncoded(d->x509_cert_->os_cert_handle(),
191
return QString::fromStdString(pem);