blob: ef466545afa854f9e2b65ff243063e05eb332218 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/sslfingerprint.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <ctype.h>
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/logging.h"
17#include "rtc_base/messagedigest.h"
18#include "rtc_base/stringencode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
Henrik Grunell2b156262018-10-11 11:15:48 +000022SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm,
Mirko Bonadei6932fb22018-10-15 14:18:03 +000023 const rtc::SSLIdentity* identity) {
Steve Anton4905edb2018-10-15 19:27:44 -070024 return CreateUnique(algorithm, *identity).release();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025}
26
Steve Anton4905edb2018-10-15 19:27:44 -070027std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUnique(
28 const std::string& algorithm,
29 const rtc::SSLIdentity& identity) {
30 return Create(algorithm, identity.certificate());
31}
32
33std::unique_ptr<SSLFingerprint> SSLFingerprint::Create(
34 const std::string& algorithm,
35 const rtc::SSLCertificate& cert) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020036 uint8_t digest_val[64];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037 size_t digest_len;
Steve Anton4905edb2018-10-15 19:27:44 -070038 bool ret = cert.ComputeDigest(algorithm, digest_val, sizeof(digest_val),
39 &digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 if (!ret) {
deadbeef37f5ecf2017-02-27 14:06:41 -080041 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042 }
Steve Anton4905edb2018-10-15 19:27:44 -070043 return absl::make_unique<SSLFingerprint>(
44 algorithm, ArrayView<const uint8_t>(digest_val, digest_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045}
46
47SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
Yves Gerey665174f2018-06-19 15:03:05 +020048 const std::string& algorithm,
49 const std::string& fingerprint) {
Steve Anton4905edb2018-10-15 19:27:44 -070050 return CreateUniqueFromRfc4572(algorithm, fingerprint).release();
51}
52
53std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUniqueFromRfc4572(
54 const std::string& algorithm,
55 const std::string& fingerprint) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
deadbeef37f5ecf2017-02-27 14:06:41 -080057 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058
59 if (fingerprint.empty())
deadbeef37f5ecf2017-02-27 14:06:41 -080060 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 char value[rtc::MessageDigest::kMaxSize];
Steve Anton4905edb2018-10-15 19:27:44 -070063 size_t value_len = rtc::hex_decode_with_delimiter(
Yves Gerey665174f2018-06-19 15:03:05 +020064 value, sizeof(value), fingerprint.c_str(), fingerprint.length(), ':');
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 if (!value_len)
deadbeef37f5ecf2017-02-27 14:06:41 -080066 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
Steve Anton4905edb2018-10-15 19:27:44 -070068 return absl::make_unique<SSLFingerprint>(
69 algorithm,
70 ArrayView<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071}
72
Steve Anton4905edb2018-10-15 19:27:44 -070073std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate(
74 const RTCCertificate& cert) {
deadbeef8662f942017-01-20 21:20:51 -080075 std::string digest_alg;
Steve Anton4905edb2018-10-15 19:27:44 -070076 if (!cert.ssl_certificate().GetSignatureDigestAlgorithm(&digest_alg)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_ERROR)
78 << "Failed to retrieve the certificate's digest algorithm";
deadbeef8662f942017-01-20 21:20:51 -080079 return nullptr;
80 }
81
Steve Anton4905edb2018-10-15 19:27:44 -070082 std::unique_ptr<SSLFingerprint> fingerprint =
83 CreateUnique(digest_alg, *cert.identity());
deadbeef8662f942017-01-20 21:20:51 -080084 if (!fingerprint) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010085 RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
86 << digest_alg;
deadbeef8662f942017-01-20 21:20:51 -080087 }
88 return fingerprint;
89}
90
Peter Boström0c4e06b2015-10-07 12:23:21 +020091SSLFingerprint::SSLFingerprint(const std::string& algorithm,
Steve Anton4905edb2018-10-15 19:27:44 -070092 ArrayView<const uint8_t> digest_view)
93 : algorithm(algorithm), digest(digest_view.data(), digest_view.size()) {}
94
95SSLFingerprint::SSLFingerprint(const std::string& algorithm,
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 const uint8_t* digest_in,
97 size_t digest_len)
Steve Anton4905edb2018-10-15 19:27:44 -070098 : SSLFingerprint(algorithm, MakeArrayView(digest_in, digest_len)) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099
100SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
101 : algorithm(from.algorithm), digest(from.digest) {}
102
103bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200104 return algorithm == other.algorithm && digest == other.digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105}
106
107std::string SSLFingerprint::GetRfc4572Fingerprint() const {
108 std::string fingerprint =
Karl Wiberg94784372015-04-20 14:03:07 +0200109 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
Yves Gerey665174f2018-06-19 15:03:05 +0200110 std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(),
111 ::toupper);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 return fingerprint;
113}
114
mikescarlette7748672016-04-29 20:20:54 -0700115std::string SSLFingerprint::ToString() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 std::string fp_str = algorithm;
117 fp_str.append(" ");
118 fp_str.append(GetRfc4572Fingerprint());
119 return fp_str;
120}
121
122} // namespace rtc