~abreu-alexandre/oxide/add-ua-to-downloadrequested

« back to all changes in this revision

Viewing changes to qt/core/api/oxideqsslcertificate.cc

  • Committer: Chris Coulson
  • Date: 2014-09-01 13:49:18 UTC
  • mfrom: (694.1.23 ssl-status-api)
  • Revision ID: chris.coulson@canonical.com-20140901134918-3mqi09aln7uldf2l
Add WebView.securityStatus, WebView.blockedContent, WebView.setCanTemporarilyDisplayInsecureContent, WebView.setCanTemporarilyRunInsecureContent, SecurityStatus, CertificateError and SslCertificate API's. I'm still working on tests for these API's, but this shouldn't block webbrowser-app work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim:expandtab:shiftwidth=2:tabstop=2:
 
2
// Copyright (C) 2014 Canonical Ltd.
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
 
 
18
#include "oxideqsslcertificate.h"
 
19
#include "oxideqsslcertificate_p.h"
 
20
 
 
21
#include <string>
 
22
#include <vector>
 
23
#include <QByteArray>
 
24
 
 
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"
 
30
 
 
31
static QStringList GetPrincipalValue(const net::CertPrincipal& principal,
 
32
                                     OxideQSslCertificate::PrincipalAttr attr) {
 
33
  switch (attr) {
 
34
    case OxideQSslCertificate::PrincipalAttrOrganizationName: {
 
35
      QStringList rv;
 
36
      for (size_t i = 0; i < principal.organization_names.size(); ++i) {
 
37
        rv.push_back(QString::fromStdString(principal.organization_names[i]));
 
38
      }
 
39
      return rv;
 
40
    }
 
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: {
 
46
      QStringList rv;
 
47
      for (size_t i = 0; i < principal.organization_unit_names.size(); ++i) {
 
48
        rv.push_back(QString::fromStdString(principal.organization_unit_names[i]));
 
49
      }
 
50
      return rv;
 
51
    }
 
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));
 
56
    default:
 
57
      NOTREACHED();
 
58
      return QStringList();
 
59
  }
 
60
}
 
61
 
 
62
static QDateTime ToQDateTime(const base::Time& time) {
 
63
  int64_t ms = (time - base::Time()).InMilliseconds();
 
64
  return QDateTime::fromMSecsSinceEpoch(ms);
 
65
}
 
66
 
 
67
OxideQSslCertificatePrivate::OxideQSslCertificatePrivate(
 
68
    const scoped_refptr<net::X509Certificate>& cert)
 
69
    : x509_cert_(cert) {}
 
70
 
 
71
OxideQSslCertificatePrivate::~OxideQSslCertificatePrivate() {}
 
72
 
 
73
// static
 
74
OxideQSslCertificate* OxideQSslCertificatePrivate::Create(
 
75
    const scoped_refptr<net::X509Certificate>& cert,
 
76
    QObject* parent) {
 
77
  return new OxideQSslCertificate(
 
78
      *new OxideQSslCertificatePrivate(cert),
 
79
      parent);
 
80
}
 
81
 
 
82
OxideQSslCertificate::OxideQSslCertificate(OxideQSslCertificatePrivate& dd,
 
83
                                           QObject* parent)
 
84
    : QObject(parent),
 
85
      d_ptr(&dd) {}
 
86
 
 
87
OxideQSslCertificate::~OxideQSslCertificate() {}
 
88
 
 
89
QString OxideQSslCertificate::serialNumber() const {
 
90
  Q_D(const OxideQSslCertificate);
 
91
 
 
92
  const std::string& serial_number = d->x509_cert_->serial_number();
 
93
  QByteArray ba(serial_number.data(), int(serial_number.size()));
 
94
 
 
95
  return QString::fromUtf8(ba.toHex());
 
96
}
 
97
 
 
98
QString OxideQSslCertificate::subjectDisplayName() const {
 
99
  Q_D(const OxideQSslCertificate);
 
100
 
 
101
  return QString::fromStdString(d->x509_cert_->subject().GetDisplayName());
 
102
}
 
103
 
 
104
QString OxideQSslCertificate::issuerDisplayName() const {
 
105
  Q_D(const OxideQSslCertificate);
 
106
 
 
107
  return QString::fromStdString(d->x509_cert_->issuer().GetDisplayName());
 
108
}
 
109
 
 
110
QStringList OxideQSslCertificate::getSubjectInfo(PrincipalAttr attr) const {
 
111
  Q_D(const OxideQSslCertificate);
 
112
 
 
113
  return GetPrincipalValue(d->x509_cert_->subject(), attr);
 
114
}
 
115
 
 
116
QStringList OxideQSslCertificate::getIssuerInfo(PrincipalAttr attr) const {
 
117
  Q_D(const OxideQSslCertificate);
 
118
 
 
119
  return GetPrincipalValue(d->x509_cert_->issuer(), attr);
 
120
}
 
121
 
 
122
QDateTime OxideQSslCertificate::effectiveDate() const {
 
123
  Q_D(const OxideQSslCertificate);
 
124
 
 
125
  return ToQDateTime(d->x509_cert_->valid_start());
 
126
}
 
127
 
 
128
QDateTime OxideQSslCertificate::expiryDate() const {
 
129
  Q_D(const OxideQSslCertificate);
 
130
 
 
131
  return ToQDateTime(d->x509_cert_->valid_expiry());
 
132
}
 
133
 
 
134
QString OxideQSslCertificate::fingerprintSHA1() const {
 
135
  Q_D(const OxideQSslCertificate);
 
136
 
 
137
  const net::SHA1HashValue& hash = d->x509_cert_->fingerprint();
 
138
  QByteArray ba(reinterpret_cast<const char *>(hash.data), sizeof(hash.data));
 
139
 
 
140
  return QString::fromUtf8(ba.toHex());
 
141
}
 
142
 
 
143
bool OxideQSslCertificate::isExpired() const {
 
144
  Q_D(const OxideQSslCertificate);
 
145
 
 
146
  return d->x509_cert_->HasExpired();
 
147
}
 
148
 
 
149
OxideQSslCertificate* OxideQSslCertificate::issuer() const {
 
150
  Q_D(const OxideQSslCertificate);
 
151
 
 
152
  if (d->issuer_) {
 
153
    return d->issuer_.get();
 
154
  }
 
155
 
 
156
  const net::X509Certificate::OSCertHandles& handles =
 
157
      d->x509_cert_->GetIntermediateCertificates();
 
158
  if (handles.empty()) {
 
159
    return NULL;
 
160
  }
 
161
 
 
162
  net::X509Certificate::OSCertHandle handle = handles[0];
 
163
 
 
164
  net::X509Certificate::OSCertHandles intermediates;
 
165
  for (size_t i = 1; i < handles.size(); ++i) {
 
166
    intermediates.push_back(handles[i]);
 
167
  }
 
168
 
 
169
  scoped_refptr<net::X509Certificate> cert =
 
170
      net::X509Certificate::CreateFromHandle(handle, intermediates);
 
171
  d->issuer_.reset(OxideQSslCertificatePrivate::Create(cert));
 
172
 
 
173
  return d->issuer_.get();
 
174
}
 
175
 
 
176
OxideQSslCertificate* OxideQSslCertificate::copy() const {
 
177
  Q_D(const OxideQSslCertificate);
 
178
 
 
179
  return OxideQSslCertificatePrivate::Create(d->x509_cert_);
 
180
}
 
181
 
 
182
QString OxideQSslCertificate::toPem() const {
 
183
  Q_D(const OxideQSslCertificate);
 
184
 
 
185
  std::string pem;
 
186
  if (!net::X509Certificate::GetPEMEncoded(d->x509_cert_->os_cert_handle(),
 
187
                                           &pem)) {
 
188
    return QString();
 
189
  }
 
190
 
 
191
  return QString::fromStdString(pem);
 
192
}