blob: 03a627f54619d6edd428944583af6c537acffc1e [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
tzikd0be0022018-10-18 15:10:02 +090016#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/logging.h"
18#include "rtc_base/messagedigest.h"
19#include "rtc_base/stringencode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
21namespace rtc {
22
Henrik Grunell2b156262018-10-11 11:15:48 +000023SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm,
Mirko Bonadei6932fb22018-10-15 14:18:03 +000024 const rtc::SSLIdentity* identity) {
Steve Anton4905edb2018-10-15 19:27:44 -070025 return CreateUnique(algorithm, *identity).release();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026}
27
Steve Anton4905edb2018-10-15 19:27:44 -070028std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUnique(
29 const std::string& algorithm,
30 const rtc::SSLIdentity& identity) {
31 return Create(algorithm, identity.certificate());
32}
33
34std::unique_ptr<SSLFingerprint> SSLFingerprint::Create(
35 const std::string& algorithm,
36 const rtc::SSLCertificate& cert) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020037 uint8_t digest_val[64];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 size_t digest_len;
Steve Anton4905edb2018-10-15 19:27:44 -070039 bool ret = cert.ComputeDigest(algorithm, digest_val, sizeof(digest_val),
40 &digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 if (!ret) {
deadbeef37f5ecf2017-02-27 14:06:41 -080042 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 }
Steve Anton4905edb2018-10-15 19:27:44 -070044 return absl::make_unique<SSLFingerprint>(
45 algorithm, ArrayView<const uint8_t>(digest_val, digest_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046}
47
48SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
Yves Gerey665174f2018-06-19 15:03:05 +020049 const std::string& algorithm,
50 const std::string& fingerprint) {
Steve Anton4905edb2018-10-15 19:27:44 -070051 return CreateUniqueFromRfc4572(algorithm, fingerprint).release();
52}
53
54std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUniqueFromRfc4572(
55 const std::string& algorithm,
56 const std::string& fingerprint) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
deadbeef37f5ecf2017-02-27 14:06:41 -080058 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
60 if (fingerprint.empty())
deadbeef37f5ecf2017-02-27 14:06:41 -080061 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 char value[rtc::MessageDigest::kMaxSize];
Steve Anton4905edb2018-10-15 19:27:44 -070064 size_t value_len = rtc::hex_decode_with_delimiter(
Yves Gerey665174f2018-06-19 15:03:05 +020065 value, sizeof(value), fingerprint.c_str(), fingerprint.length(), ':');
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 if (!value_len)
deadbeef37f5ecf2017-02-27 14:06:41 -080067 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
Steve Anton4905edb2018-10-15 19:27:44 -070069 return absl::make_unique<SSLFingerprint>(
70 algorithm,
71 ArrayView<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072}
73
Steve Anton4905edb2018-10-15 19:27:44 -070074std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate(
75 const RTCCertificate& cert) {
deadbeef8662f942017-01-20 21:20:51 -080076 std::string digest_alg;
Steve Anton4905edb2018-10-15 19:27:44 -070077 if (!cert.ssl_certificate().GetSignatureDigestAlgorithm(&digest_alg)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010078 RTC_LOG(LS_ERROR)
79 << "Failed to retrieve the certificate's digest algorithm";
deadbeef8662f942017-01-20 21:20:51 -080080 return nullptr;
81 }
82
Steve Anton4905edb2018-10-15 19:27:44 -070083 std::unique_ptr<SSLFingerprint> fingerprint =
84 CreateUnique(digest_alg, *cert.identity());
deadbeef8662f942017-01-20 21:20:51 -080085 if (!fingerprint) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
87 << digest_alg;
deadbeef8662f942017-01-20 21:20:51 -080088 }
89 return fingerprint;
90}
91
Peter Boström0c4e06b2015-10-07 12:23:21 +020092SSLFingerprint::SSLFingerprint(const std::string& algorithm,
Steve Anton4905edb2018-10-15 19:27:44 -070093 ArrayView<const uint8_t> digest_view)
94 : algorithm(algorithm), digest(digest_view.data(), digest_view.size()) {}
95
96SSLFingerprint::SSLFingerprint(const std::string& algorithm,
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 const uint8_t* digest_in,
98 size_t digest_len)
Steve Anton4905edb2018-10-15 19:27:44 -070099 : SSLFingerprint(algorithm, MakeArrayView(digest_in, digest_len)) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100
101SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
102 : algorithm(from.algorithm), digest(from.digest) {}
103
104bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200105 return algorithm == other.algorithm && digest == other.digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106}
107
108std::string SSLFingerprint::GetRfc4572Fingerprint() const {
109 std::string fingerprint =
Karl Wiberg94784372015-04-20 14:03:07 +0200110 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
Yves Gerey665174f2018-06-19 15:03:05 +0200111 std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(),
112 ::toupper);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 return fingerprint;
114}
115
mikescarlette7748672016-04-29 20:20:54 -0700116std::string SSLFingerprint::ToString() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 std::string fp_str = algorithm;
118 fp_str.append(" ");
119 fp_str.append(GetRfc4572Fingerprint());
120 return fp_str;
121}
122
123} // namespace rtc