blob: a85b7a09ceaff0254a28d112cf21d91656f3b71d [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 09:11:00 -080011#include "rtc_base/ssl_fingerprint.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <ctype.h>
Ali Tofigh7fa90572022-03-17 15:47:49 +010014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020016#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include <string>
18
Steve Anton2acd1632019-03-25 13:48:30 -070019#include "absl/algorithm/container.h"
Ali Tofigh7fa90572022-03-17 15:47:49 +010020#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/message_digest.h"
23#include "rtc_base/rtc_certificate.h"
24#include "rtc_base/ssl_certificate.h"
25#include "rtc_base/ssl_identity.h"
26#include "rtc_base/string_encode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
28namespace rtc {
29
Ali Tofigh7fa90572022-03-17 15:47:49 +010030SSLFingerprint* SSLFingerprint::Create(absl::string_view algorithm,
Mirko Bonadei6932fb22018-10-15 14:18:03 +000031 const rtc::SSLIdentity* identity) {
Steve Anton4905edb2018-10-15 19:27:44 -070032 return CreateUnique(algorithm, *identity).release();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033}
34
Steve Anton4905edb2018-10-15 19:27:44 -070035std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUnique(
Ali Tofigh7fa90572022-03-17 15:47:49 +010036 absl::string_view algorithm,
Steve Anton4905edb2018-10-15 19:27:44 -070037 const rtc::SSLIdentity& identity) {
38 return Create(algorithm, identity.certificate());
39}
40
41std::unique_ptr<SSLFingerprint> SSLFingerprint::Create(
Ali Tofigh7fa90572022-03-17 15:47:49 +010042 absl::string_view algorithm,
Steve Anton4905edb2018-10-15 19:27:44 -070043 const rtc::SSLCertificate& cert) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020044 uint8_t digest_val[64];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045 size_t digest_len;
Steve Anton4905edb2018-10-15 19:27:44 -070046 bool ret = cert.ComputeDigest(algorithm, digest_val, sizeof(digest_val),
47 &digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 if (!ret) {
deadbeef37f5ecf2017-02-27 14:06:41 -080049 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050 }
Mirko Bonadei317a1f02019-09-17 17:06:18 +020051 return std::make_unique<SSLFingerprint>(
Steve Anton4905edb2018-10-15 19:27:44 -070052 algorithm, ArrayView<const uint8_t>(digest_val, digest_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053}
54
55SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
Ali Tofigh7fa90572022-03-17 15:47:49 +010056 absl::string_view algorithm,
57 absl::string_view fingerprint) {
Steve Anton4905edb2018-10-15 19:27:44 -070058 return CreateUniqueFromRfc4572(algorithm, fingerprint).release();
59}
60
61std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUniqueFromRfc4572(
Ali Tofigh7fa90572022-03-17 15:47:49 +010062 absl::string_view algorithm,
63 absl::string_view fingerprint) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
deadbeef37f5ecf2017-02-27 14:06:41 -080065 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
67 if (fingerprint.empty())
deadbeef37f5ecf2017-02-27 14:06:41 -080068 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 char value[rtc::MessageDigest::kMaxSize];
Steve Anton4905edb2018-10-15 19:27:44 -070071 size_t value_len = rtc::hex_decode_with_delimiter(
Ali Tofigh7fa90572022-03-17 15:47:49 +010072 value, sizeof(value), fingerprint.data(), fingerprint.length(), ':');
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 if (!value_len)
deadbeef37f5ecf2017-02-27 14:06:41 -080074 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075
Mirko Bonadei317a1f02019-09-17 17:06:18 +020076 return std::make_unique<SSLFingerprint>(
Steve Anton4905edb2018-10-15 19:27:44 -070077 algorithm,
78 ArrayView<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079}
80
Steve Anton4905edb2018-10-15 19:27:44 -070081std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate(
82 const RTCCertificate& cert) {
deadbeef8662f942017-01-20 21:20:51 -080083 std::string digest_alg;
Benjamin Wright6c6c9df2018-10-25 01:16:26 -070084 if (!cert.GetSSLCertificate().GetSignatureDigestAlgorithm(&digest_alg)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010085 RTC_LOG(LS_ERROR)
86 << "Failed to retrieve the certificate's digest algorithm";
deadbeef8662f942017-01-20 21:20:51 -080087 return nullptr;
88 }
89
Steve Anton4905edb2018-10-15 19:27:44 -070090 std::unique_ptr<SSLFingerprint> fingerprint =
91 CreateUnique(digest_alg, *cert.identity());
deadbeef8662f942017-01-20 21:20:51 -080092 if (!fingerprint) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010093 RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
94 << digest_alg;
deadbeef8662f942017-01-20 21:20:51 -080095 }
96 return fingerprint;
97}
98
Ali Tofigh7fa90572022-03-17 15:47:49 +010099SSLFingerprint::SSLFingerprint(absl::string_view algorithm,
Steve Anton4905edb2018-10-15 19:27:44 -0700100 ArrayView<const uint8_t> digest_view)
101 : algorithm(algorithm), digest(digest_view.data(), digest_view.size()) {}
102
Ali Tofigh7fa90572022-03-17 15:47:49 +0100103SSLFingerprint::SSLFingerprint(absl::string_view algorithm,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 const uint8_t* digest_in,
105 size_t digest_len)
Steve Anton4905edb2018-10-15 19:27:44 -0700106 : SSLFingerprint(algorithm, MakeArrayView(digest_in, digest_len)) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200109 return algorithm == other.algorithm && digest == other.digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110}
111
112std::string SSLFingerprint::GetRfc4572Fingerprint() const {
113 std::string fingerprint =
Karl Wiberg94784372015-04-20 14:03:07 +0200114 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
Steve Anton2acd1632019-03-25 13:48:30 -0700115 absl::c_transform(fingerprint, fingerprint.begin(), ::toupper);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 return fingerprint;
117}
118
mikescarlette7748672016-04-29 20:20:54 -0700119std::string SSLFingerprint::ToString() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 std::string fp_str = algorithm;
121 fp_str.append(" ");
122 fp_str.append(GetRfc4572Fingerprint());
123 return fp_str;
124}
125
126} // namespace rtc