2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2012 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/ssl_fingerprint.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2022-03-17 15:47:49 +01:00
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <cstdint>
|
2019-09-17 17:06:18 +02:00
|
|
|
#include <memory>
|
2014-05-13 18:00:26 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2019-03-25 13:48:30 -07:00
|
|
|
#include "absl/algorithm/container.h"
|
2022-03-17 15:47:49 +01:00
|
|
|
#include "absl/strings/string_view.h"
|
2022-03-31 10:36:48 +02:00
|
|
|
#include "api/array_view.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/message_digest.h"
|
|
|
|
|
#include "rtc_base/rtc_certificate.h"
|
|
|
|
|
#include "rtc_base/ssl_certificate.h"
|
|
|
|
|
#include "rtc_base/ssl_identity.h"
|
|
|
|
|
#include "rtc_base/string_encode.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
2022-03-17 15:47:49 +01:00
|
|
|
SSLFingerprint* SSLFingerprint::Create(absl::string_view algorithm,
|
2018-10-15 14:18:03 +00:00
|
|
|
const rtc::SSLIdentity* identity) {
|
2018-10-15 19:27:44 -07:00
|
|
|
return CreateUnique(algorithm, *identity).release();
|
|
|
|
|
}
|
2018-10-11 11:15:48 +00:00
|
|
|
|
2018-10-15 19:27:44 -07:00
|
|
|
std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUnique(
|
2022-03-17 15:47:49 +01:00
|
|
|
absl::string_view algorithm,
|
2018-10-15 19:27:44 -07:00
|
|
|
const rtc::SSLIdentity& identity) {
|
|
|
|
|
return Create(algorithm, identity.certificate());
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-15 19:27:44 -07:00
|
|
|
std::unique_ptr<SSLFingerprint> SSLFingerprint::Create(
|
2022-03-17 15:47:49 +01:00
|
|
|
absl::string_view algorithm,
|
2018-10-15 19:27:44 -07:00
|
|
|
const rtc::SSLCertificate& cert) {
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint8_t digest_val[64];
|
2014-05-13 18:00:26 +00:00
|
|
|
size_t digest_len;
|
2018-10-15 19:27:44 -07:00
|
|
|
bool ret = cert.ComputeDigest(algorithm, digest_val, sizeof(digest_val),
|
|
|
|
|
&digest_len);
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!ret) {
|
2017-02-27 14:06:41 -08:00
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
2019-09-17 17:06:18 +02:00
|
|
|
return std::make_unique<SSLFingerprint>(
|
2018-10-15 19:27:44 -07:00
|
|
|
algorithm, ArrayView<const uint8_t>(digest_val, digest_len));
|
2018-10-11 13:22:38 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-15 14:18:03 +00:00
|
|
|
SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
|
2022-03-17 15:47:49 +01:00
|
|
|
absl::string_view algorithm,
|
|
|
|
|
absl::string_view fingerprint) {
|
2018-10-15 19:27:44 -07:00
|
|
|
return CreateUniqueFromRfc4572(algorithm, fingerprint).release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUniqueFromRfc4572(
|
2022-03-17 15:47:49 +01:00
|
|
|
absl::string_view algorithm,
|
|
|
|
|
absl::string_view fingerprint) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
|
2017-02-27 14:06:41 -08:00
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
if (fingerprint.empty())
|
2017-02-27 14:06:41 -08:00
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
char value[rtc::MessageDigest::kMaxSize];
|
2022-03-31 10:36:48 +02:00
|
|
|
size_t value_len =
|
|
|
|
|
rtc::hex_decode_with_delimiter(ArrayView<char>(value), fingerprint, ':');
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!value_len)
|
2017-02-27 14:06:41 -08:00
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2019-09-17 17:06:18 +02:00
|
|
|
return std::make_unique<SSLFingerprint>(
|
2018-10-15 19:27:44 -07:00
|
|
|
algorithm,
|
|
|
|
|
ArrayView<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-15 19:27:44 -07:00
|
|
|
std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate(
|
|
|
|
|
const RTCCertificate& cert) {
|
2017-01-20 21:20:51 -08:00
|
|
|
std::string digest_alg;
|
2018-10-25 01:16:26 -07:00
|
|
|
if (!cert.GetSSLCertificate().GetSignatureDigestAlgorithm(&digest_alg)) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR)
|
|
|
|
|
<< "Failed to retrieve the certificate's digest algorithm";
|
2017-01-20 21:20:51 -08:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 19:27:44 -07:00
|
|
|
std::unique_ptr<SSLFingerprint> fingerprint =
|
|
|
|
|
CreateUnique(digest_alg, *cert.identity());
|
2017-01-20 21:20:51 -08:00
|
|
|
if (!fingerprint) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
|
|
|
|
|
<< digest_alg;
|
2017-01-20 21:20:51 -08:00
|
|
|
}
|
|
|
|
|
return fingerprint;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 15:47:49 +01:00
|
|
|
SSLFingerprint::SSLFingerprint(absl::string_view algorithm,
|
2018-10-15 19:27:44 -07:00
|
|
|
ArrayView<const uint8_t> digest_view)
|
|
|
|
|
: algorithm(algorithm), digest(digest_view.data(), digest_view.size()) {}
|
|
|
|
|
|
2022-03-17 15:47:49 +01:00
|
|
|
SSLFingerprint::SSLFingerprint(absl::string_view algorithm,
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
const uint8_t* digest_in,
|
|
|
|
|
size_t digest_len)
|
2018-10-15 19:27:44 -07:00
|
|
|
: SSLFingerprint(algorithm, MakeArrayView(digest_in, digest_len)) {}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
|
2018-06-19 15:03:05 +02:00
|
|
|
return algorithm == other.algorithm && digest == other.digest;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string SSLFingerprint::GetRfc4572Fingerprint() const {
|
2022-03-31 10:36:48 +02:00
|
|
|
std::string fingerprint = rtc::hex_encode_with_delimiter(
|
|
|
|
|
absl::string_view(digest.data<char>(), digest.size()), ':');
|
2019-03-25 13:48:30 -07:00
|
|
|
absl::c_transform(fingerprint, fingerprint.begin(), ::toupper);
|
2014-05-13 18:00:26 +00:00
|
|
|
return fingerprint;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 20:20:54 -07:00
|
|
|
std::string SSLFingerprint::ToString() const {
|
2014-05-13 18:00:26 +00:00
|
|
|
std::string fp_str = algorithm;
|
|
|
|
|
fp_str.append(" ");
|
|
|
|
|
fp_str.append(GetRfc4572Fingerprint());
|
|
|
|
|
return fp_str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|