~osomon/oxide/ensure-files-exist

« back to all changes in this revision

Viewing changes to shared/browser/oxide_security_status.cc

  • Committer: Olivier Tilloy
  • Date: 2014-09-25 16:27:09 UTC
  • mfrom: (677.1.95 oxide)
  • Revision ID: olivier.tilloy@canonical.com-20140925162709-h8bai0f1nfaf7cfy
Merge the latest changes from trunk.

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 "oxide_security_status.h"
 
19
 
 
20
#include "base/logging.h"
 
21
#include "content/public/browser/cert_store.h"
 
22
#include "content/public/common/security_style.h"
 
23
#include "net/cert/cert_status_flags.h"
 
24
#include "net/cert/x509_certificate.h"
 
25
 
 
26
#include "shared/base/oxide_enum_flags.h"
 
27
 
 
28
namespace oxide {
 
29
 
 
30
namespace {
 
31
 
 
32
OXIDE_MAKE_ENUM_BITWISE_OPERATORS(content::SSLStatus::ContentStatusFlags)
 
33
OXIDE_MAKE_ENUM_BITWISE_OPERATORS(CertStatus)
 
34
 
 
35
inline SecurityLevel CalculateSecurityLevel(
 
36
    const content::SSLStatus& ssl_status,
 
37
    net::X509Certificate* cert) {
 
38
  if (ssl_status.security_style == content::SECURITY_STYLE_UNKNOWN ||
 
39
      ssl_status.security_style == content::SECURITY_STYLE_UNAUTHENTICATED) {
 
40
    return SECURITY_LEVEL_NONE;
 
41
  }
 
42
 
 
43
  if (ssl_status.security_style ==
 
44
      content::SECURITY_STYLE_AUTHENTICATION_BROKEN) {
 
45
    return SECURITY_LEVEL_ERROR;
 
46
  }
 
47
 
 
48
  DCHECK_EQ(ssl_status.security_style, content::SECURITY_STYLE_AUTHENTICATED);
 
49
  CHECK(!(ssl_status.content_status &
 
50
          content::SSLStatus::RAN_INSECURE_CONTENT)) <<
 
51
      "Invalid SSLStatus - RAN_INSECURE_CONTENT and SECURITY_STYLE_AUTHENTICATED "
 
52
      "are meant to be mutually exclusive!";
 
53
 
 
54
  if (ssl_status.content_status &
 
55
      content::SSLStatus::DISPLAYED_INSECURE_CONTENT) {
 
56
    return SECURITY_LEVEL_WARNING;
 
57
  }
 
58
 
 
59
  DCHECK_EQ(
 
60
      static_cast<content::SSLStatus::ContentStatusFlags>(
 
61
        ssl_status.content_status),
 
62
      content::SSLStatus::NORMAL_CONTENT);
 
63
 
 
64
  if (net::IsCertStatusError(ssl_status.cert_status)) {
 
65
    CHECK(net::IsCertStatusMinorError(ssl_status.cert_status)) <<
 
66
        "Invalid SSLStatus - Non-minor cert status error and "
 
67
        "SECURITY_STYLE_AUTHENTICATED are meant to be mutually exclusive!";
 
68
    return SECURITY_LEVEL_WARNING;
 
69
  }
 
70
 
 
71
  if ((ssl_status.cert_status & net::CERT_STATUS_IS_EV) && cert) {
 
72
    return SECURITY_LEVEL_SECURE_EV;
 
73
  }
 
74
 
 
75
  return SECURITY_LEVEL_SECURE;
 
76
}
 
77
 
 
78
inline CertStatus CalculateCertStatus(net::CertStatus cert_status,
 
79
                                      net::X509Certificate* cert) {
 
80
  CertStatus rv = CERT_STATUS_OK;
 
81
 
 
82
  // Handle flags that have a direct mapping to CertErrorStatus first
 
83
  if (cert_status & net::CERT_STATUS_COMMON_NAME_INVALID) {
 
84
    rv |= CERT_STATUS_BAD_IDENTITY;
 
85
    cert_status &= ~net::CERT_STATUS_COMMON_NAME_INVALID;
 
86
  }
 
87
  if (cert_status & net::CERT_STATUS_DATE_INVALID) {
 
88
    if (cert && cert->HasExpired()) {
 
89
      rv |= CERT_STATUS_EXPIRED;
 
90
    } else {
 
91
      // The date could be in the future or issuer certificates could
 
92
      // have expired. In the latter case, perhaps make this
 
93
      // CERT_STATUS_EXPIRED too?
 
94
      rv |= CERT_STATUS_DATE_INVALID;
 
95
    }
 
96
    cert_status &= ~net::CERT_STATUS_DATE_INVALID;
 
97
  }
 
98
  if (cert_status & net::CERT_STATUS_AUTHORITY_INVALID) {
 
99
    rv |= CERT_STATUS_AUTHORITY_INVALID;
 
100
    cert_status &= ~net::CERT_STATUS_AUTHORITY_INVALID;
 
101
  }
 
102
  if (cert_status & net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION) {
 
103
    rv |= CERT_STATUS_REVOCATION_CHECK_FAILED;
 
104
    cert_status &= ~net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
 
105
  }
 
106
  if (cert_status & net::CERT_STATUS_REVOKED) {
 
107
    rv |= CERT_STATUS_REVOKED;
 
108
    cert_status &= ~net::CERT_STATUS_REVOKED;
 
109
  }
 
110
  if (cert_status & net::CERT_STATUS_INVALID) {
 
111
    rv |= CERT_STATUS_INVALID;
 
112
    cert_status &= ~net::CERT_STATUS_INVALID;
 
113
  }
 
114
  if (cert_status & net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM) {
 
115
    rv |= CERT_STATUS_INSECURE;
 
116
    cert_status &= ~net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM;
 
117
  }
 
118
  if (cert_status & net::CERT_STATUS_WEAK_KEY) {
 
119
    rv |= CERT_STATUS_INSECURE;
 
120
    cert_status &= ~net::CERT_STATUS_WEAK_KEY;
 
121
  }
 
122
 
 
123
  // For flags that don't have a direct mapping to CertErrorStatus,
 
124
  // set the generic flag if any non-minor error bits are set
 
125
  if (net::IsCertStatusError(cert_status) &&
 
126
      !net::IsCertStatusMinorError(cert_status)) {
 
127
    rv |= CERT_STATUS_GENERIC_ERROR;
 
128
  }
 
129
 
 
130
  return rv;
 
131
}
 
132
 
 
133
}
 
134
 
 
135
SecurityStatus::SecurityStatus()
 
136
    : security_level_(SECURITY_LEVEL_NONE),
 
137
      content_status_(content::SSLStatus::NORMAL_CONTENT),
 
138
      cert_status_(CERT_STATUS_OK) {}
 
139
 
 
140
SecurityStatus::SecurityStatus(const content::SSLStatus& ssl_status)
 
141
    : security_level_(SECURITY_LEVEL_NONE),
 
142
      content_status_(content::SSLStatus::NORMAL_CONTENT),
 
143
      cert_status_(CERT_STATUS_OK) {
 
144
  Update(ssl_status);
 
145
}
 
146
 
 
147
SecurityStatus::~SecurityStatus() {}
 
148
 
 
149
void SecurityStatus::Update(const content::SSLStatus& ssl_status) {
 
150
  cert_ = NULL;
 
151
  content::CertStore::GetInstance()->RetrieveCert(ssl_status.cert_id,
 
152
                                                  &cert_);
 
153
 
 
154
  security_level_ = CalculateSecurityLevel(ssl_status, cert_.get());
 
155
  content_status_ = static_cast<content::SSLStatus::ContentStatusFlags>(
 
156
      ssl_status.content_status);
 
157
  cert_status_ = CalculateCertStatus(ssl_status.cert_status, cert_.get());
 
158
}
 
159
 
 
160
scoped_refptr<net::X509Certificate> SecurityStatus::cert() const {
 
161
  return cert_;
 
162
}
 
163
 
 
164
} // namespace oxide