blob: 5b261e0f53f945692327ebda68a87d560aa21b62 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/ssl_fingerprint.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
13#include <ctype.h>
Yves Gerey988cc082018-10-23 10:03:0114#include <cstdint>
Mirko Bonadei317a1f02019-09-17 15:06:1815#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:2616#include <string>
17
Steve Anton2acd1632019-03-25 20:48:3018#include "absl/algorithm/container.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/message_digest.h"
21#include "rtc_base/rtc_certificate.h"
22#include "rtc_base/ssl_certificate.h"
23#include "rtc_base/ssl_identity.h"
24#include "rtc_base/string_encode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2625
26namespace rtc {
27
Henrik Grunell2b1562632018-10-11 11:15:4828SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm,
Mirko Bonadei6932fb22018-10-15 14:18:0329 const rtc::SSLIdentity* identity) {
Steve Anton4905edb2018-10-16 02:27:4430 return CreateUnique(algorithm, *identity).release();
henrike@webrtc.orgf0488722014-05-13 18:00:2631}
32
Steve Anton4905edb2018-10-16 02:27:4433std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUnique(
34 const std::string& algorithm,
35 const rtc::SSLIdentity& identity) {
36 return Create(algorithm, identity.certificate());
37}
38
39std::unique_ptr<SSLFingerprint> SSLFingerprint::Create(
40 const std::string& algorithm,
41 const rtc::SSLCertificate& cert) {
Peter Boström0c4e06b2015-10-07 10:23:2142 uint8_t digest_val[64];
henrike@webrtc.orgf0488722014-05-13 18:00:2643 size_t digest_len;
Steve Anton4905edb2018-10-16 02:27:4444 bool ret = cert.ComputeDigest(algorithm, digest_val, sizeof(digest_val),
45 &digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:2646 if (!ret) {
deadbeef37f5ecf2017-02-27 22:06:4147 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2648 }
Mirko Bonadei317a1f02019-09-17 15:06:1849 return std::make_unique<SSLFingerprint>(
Steve Anton4905edb2018-10-16 02:27:4450 algorithm, ArrayView<const uint8_t>(digest_val, digest_len));
henrike@webrtc.orgf0488722014-05-13 18:00:2651}
52
53SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
Yves Gerey665174f2018-06-19 13:03:0554 const std::string& algorithm,
55 const std::string& fingerprint) {
Steve Anton4905edb2018-10-16 02:27:4456 return CreateUniqueFromRfc4572(algorithm, fingerprint).release();
57}
58
59std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUniqueFromRfc4572(
60 const std::string& algorithm,
61 const std::string& fingerprint) {
henrike@webrtc.orgf0488722014-05-13 18:00:2662 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
deadbeef37f5ecf2017-02-27 22:06:4163 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2664
65 if (fingerprint.empty())
deadbeef37f5ecf2017-02-27 22:06:4166 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2667
henrike@webrtc.orgf0488722014-05-13 18:00:2668 char value[rtc::MessageDigest::kMaxSize];
Steve Anton4905edb2018-10-16 02:27:4469 size_t value_len = rtc::hex_decode_with_delimiter(
Yves Gerey665174f2018-06-19 13:03:0570 value, sizeof(value), fingerprint.c_str(), fingerprint.length(), ':');
henrike@webrtc.orgf0488722014-05-13 18:00:2671 if (!value_len)
deadbeef37f5ecf2017-02-27 22:06:4172 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2673
Mirko Bonadei317a1f02019-09-17 15:06:1874 return std::make_unique<SSLFingerprint>(
Steve Anton4905edb2018-10-16 02:27:4475 algorithm,
76 ArrayView<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len));
henrike@webrtc.orgf0488722014-05-13 18:00:2677}
78
Steve Anton4905edb2018-10-16 02:27:4479std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate(
80 const RTCCertificate& cert) {
deadbeef8662f942017-01-21 05:20:5181 std::string digest_alg;
Benjamin Wright6c6c9df2018-10-25 08:16:2682 if (!cert.GetSSLCertificate().GetSignatureDigestAlgorithm(&digest_alg)) {
Mirko Bonadei675513b2017-11-09 10:09:2583 RTC_LOG(LS_ERROR)
84 << "Failed to retrieve the certificate's digest algorithm";
deadbeef8662f942017-01-21 05:20:5185 return nullptr;
86 }
87
Steve Anton4905edb2018-10-16 02:27:4488 std::unique_ptr<SSLFingerprint> fingerprint =
89 CreateUnique(digest_alg, *cert.identity());
deadbeef8662f942017-01-21 05:20:5190 if (!fingerprint) {
Mirko Bonadei675513b2017-11-09 10:09:2591 RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
92 << digest_alg;
deadbeef8662f942017-01-21 05:20:5193 }
94 return fingerprint;
95}
96
Peter Boström0c4e06b2015-10-07 10:23:2197SSLFingerprint::SSLFingerprint(const std::string& algorithm,
Steve Anton4905edb2018-10-16 02:27:4498 ArrayView<const uint8_t> digest_view)
99 : algorithm(algorithm), digest(digest_view.data(), digest_view.size()) {}
100
101SSLFingerprint::SSLFingerprint(const std::string& algorithm,
Peter Boström0c4e06b2015-10-07 10:23:21102 const uint8_t* digest_in,
103 size_t digest_len)
Steve Anton4905edb2018-10-16 02:27:44104 : SSLFingerprint(algorithm, MakeArrayView(digest_in, digest_len)) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26105
106SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
107 : algorithm(from.algorithm), digest(from.digest) {}
108
109bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
Yves Gerey665174f2018-06-19 13:03:05110 return algorithm == other.algorithm && digest == other.digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26111}
112
113std::string SSLFingerprint::GetRfc4572Fingerprint() const {
114 std::string fingerprint =
Karl Wiberg94784372015-04-20 12:03:07115 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
Steve Anton2acd1632019-03-25 20:48:30116 absl::c_transform(fingerprint, fingerprint.begin(), ::toupper);
henrike@webrtc.orgf0488722014-05-13 18:00:26117 return fingerprint;
118}
119
mikescarlette7748672016-04-30 03:20:54120std::string SSLFingerprint::ToString() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26121 std::string fp_str = algorithm;
122 fp_str.append(" ");
123 fp_str.append(GetRfc4572Fingerprint());
124 return fp_str;
125}
126
127} // namespace rtc