Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: webrtc/base/sslidentity.cc

Issue 2259283002: Refactor certificate stats collection, added SSLCertificateStats. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Another unittest for the case of certificate chains using fake certificates Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 // Handling of certificates and keypairs for SSLStreamAdapter's peer mode. 11 // Handling of certificates and keypairs for SSLStreamAdapter's peer mode.
12 #include "webrtc/base/sslidentity.h" 12 #include "webrtc/base/sslidentity.h"
13 13
14 #include <ctime> 14 #include <ctime>
15 #include <string> 15 #include <string>
16 16
17 #include "webrtc/base/base64.h" 17 #include "webrtc/base/base64.h"
18 #include "webrtc/base/checks.h" 18 #include "webrtc/base/checks.h"
19 #include "webrtc/base/logging.h" 19 #include "webrtc/base/logging.h"
20 #include "webrtc/base/sslconfig.h" 20 #include "webrtc/base/sslconfig.h"
21 #include "webrtc/base/sslfingerprint.h"
21 22
22 #if SSL_USE_OPENSSL 23 #if SSL_USE_OPENSSL
23 24
24 #include "webrtc/base/opensslidentity.h" 25 #include "webrtc/base/opensslidentity.h"
25 26
26 #endif // SSL_USE_OPENSSL 27 #endif // SSL_USE_OPENSSL
27 28
28 namespace rtc { 29 namespace rtc {
29 30
30 const char kPemTypeCertificate[] = "CERTIFICATE"; 31 const char kPemTypeCertificate[] = "CERTIFICATE";
31 const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY"; 32 const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
32 const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY"; 33 const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
33 34
35 SSLCertificateStats::SSLCertificateStats(
36 std::string&& fingerprint,
37 std::string&& fingerprint_algorithm,
38 std::string&& base64_certificate,
39 std::unique_ptr<SSLCertificateStats>&& issuer)
40 : fingerprint(std::move(fingerprint)),
41 fingerprint_algorithm(std::move(fingerprint_algorithm)),
42 base64_certificate(std::move(base64_certificate)),
43 issuer(std::move(issuer)) {
44 }
45
46 SSLCertificateStats::~SSLCertificateStats() {
47 }
48
49 std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats() const {
50 // We have a certificate and optionally a chain of certificates. This forms a
51 // linked list, starting with |this|, then the first element of |chain| and
52 // ending with the last element of |chain|. The "issuer" of a certificate is
53 // the next certificate in the chain. Stats are produced for each certificate
54 // in the list. Here, the "issuer" is the issuer's stats.
55 std::unique_ptr<SSLCertChain> chain = GetChain();
56 std::unique_ptr<SSLCertificateStats> issuer;
57 if (chain) {
58 // The loop runs in reverse so that the |issuer| is known before the
59 // |cert|'s stats.
60 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
61 const SSLCertificate* cert = &chain->Get(i);
62 issuer = cert->GetStats(std::move(issuer));
63 }
64 }
65 return GetStats(std::move(issuer));
66 }
67
68 std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats(
69 std::unique_ptr<SSLCertificateStats> issuer) const {
70 // TODO(bemasc): Move this computation to a helper class that caches these
71 // values to reduce CPU use in |StatsCollector::GetStats|. This will require
72 // adding a fast |SSLCertificate::Equals| to detect certificate changes.
73 std::string digest_algorithm;
74 if (!GetSignatureDigestAlgorithm(&digest_algorithm))
75 return nullptr;
76
77 // |SSLFingerprint::Create| can fail if the algorithm returned by
78 // |SSLCertificate::GetSignatureDigestAlgorithm| is not supported by the
79 // implementation of |SSLCertificate::ComputeDigest|. This currently happens
80 // with MD5- and SHA-224-signed certificates when linked to libNSS.
81 std::unique_ptr<SSLFingerprint> ssl_fingerprint(
82 SSLFingerprint::Create(digest_algorithm, this));
83 if (!ssl_fingerprint)
84 return nullptr;
85 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
86
87 Buffer der_buffer;
88 ToDER(&der_buffer);
89 std::string der_base64;
90 Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(), &der_base64);
91
92 return std::unique_ptr<SSLCertificateStats>(new SSLCertificateStats(
93 std::move(fingerprint),
94 std::move(digest_algorithm),
95 std::move(der_base64),
96 std::move(issuer)));
97 }
98
34 KeyParams::KeyParams(KeyType key_type) { 99 KeyParams::KeyParams(KeyType key_type) {
35 if (key_type == KT_ECDSA) { 100 if (key_type == KT_ECDSA) {
36 type_ = KT_ECDSA; 101 type_ = KT_ECDSA;
37 params_.curve = EC_NIST_P256; 102 params_.curve = EC_NIST_P256;
38 } else if (key_type == KT_RSA) { 103 } else if (key_type == KT_RSA) {
39 type_ = KT_RSA; 104 type_ = KT_RSA;
40 params_.rsa.mod_size = kRsaDefaultModSize; 105 params_.rsa.mod_size = kRsaDefaultModSize;
41 params_.rsa.pub_exp = kRsaDefaultExponent; 106 params_.rsa.pub_exp = kRsaDefaultExponent;
42 } else { 107 } else {
43 RTC_NOTREACHED(); 108 RTC_NOTREACHED();
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 330
266 if (bytes_left != 1) { 331 if (bytes_left != 1) {
267 // Now just Z should remain. Its existence was asserted above. 332 // Now just Z should remain. Its existence was asserted above.
268 return -1; 333 return -1;
269 } 334 }
270 335
271 return TmToSeconds(tm); 336 return TmToSeconds(tm);
272 } 337 }
273 338
274 } // namespace rtc 339 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698