blob: 246c1738322ecc0bb2475f8d8ff5cc2f2e6a75f0 [file] [log] [blame]
[email protected]130686a2012-11-06 18:22:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/x509_util.h"
6
7#include "base/time.h"
8#include "net/base/x509_certificate.h"
9
10namespace net {
11
12namespace x509_util {
13
14bool ClientCertSorter::operator()(
15 const scoped_refptr<X509Certificate>& a,
16 const scoped_refptr<X509Certificate>& b) const {
17 // Certificates that are null are sorted last.
18 if (!a.get() || !b.get())
19 return a.get() && !b.get();
20
21 // Certificates that are expired/not-yet-valid are sorted last.
22 base::Time now = base::Time::Now();
23 bool a_is_valid = now >= a->valid_start() && now <= a->valid_expiry();
24 bool b_is_valid = now >= b->valid_start() && now <= b->valid_expiry();
25 if (a_is_valid && !b_is_valid)
26 return true;
27
28 // Certificates with longer expirations appear as higher priority (less
29 // than) certificates with shorter expirations.
30 if (a->valid_expiry() != b->valid_expiry())
31 return a->valid_expiry() > b->valid_expiry();
32
33 // If the expiration dates are equivalent, certificates that were issued
34 // more recently should be prioritized over older certificates.
35 if (a->valid_start() != b->valid_start())
36 return a->valid_start() > b->valid_start();
37
38 // Otherwise, prefer client certificates with shorter chains.
39 const X509Certificate::OSCertHandles& a_intermediates =
40 a->GetIntermediateCertificates();
41 const X509Certificate::OSCertHandles& b_intermediates =
42 b->GetIntermediateCertificates();
43 return a_intermediates.size() < b_intermediates.size();
44}
45
46} // namespace x509_util
47
48} // namespace net