blob: 4f1ae8f7a59dc1abf68a3b738580028e36cdbc85 [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) {
24 if (!identity) {
25 return nullptr;
26 }
27
28 return Create(algorithm, &(identity->certificate()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029}
30
Mirko Bonadei6932fb22018-10-15 14:18:03 +000031SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm,
32 const rtc::SSLCertificate* cert) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020033 uint8_t digest_val[64];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034 size_t digest_len;
Mirko Bonadei6932fb22018-10-15 14:18:03 +000035 bool ret = cert->ComputeDigest(algorithm, digest_val, sizeof(digest_val),
36 &digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037 if (!ret) {
deadbeef37f5ecf2017-02-27 14:06:41 -080038 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 }
Mirko Bonadei6932fb22018-10-15 14:18:03 +000040
41 return new SSLFingerprint(algorithm, digest_val, digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042}
43
44SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
Yves Gerey665174f2018-06-19 15:03:05 +020045 const std::string& algorithm,
46 const std::string& fingerprint) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
deadbeef37f5ecf2017-02-27 14:06:41 -080048 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
50 if (fingerprint.empty())
deadbeef37f5ecf2017-02-27 14:06:41 -080051 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052
Mirko Bonadei6932fb22018-10-15 14:18:03 +000053 size_t value_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054 char value[rtc::MessageDigest::kMaxSize];
Mirko Bonadei6932fb22018-10-15 14:18:03 +000055 value_len = rtc::hex_decode_with_delimiter(
Yves Gerey665174f2018-06-19 15:03:05 +020056 value, sizeof(value), fingerprint.c_str(), fingerprint.length(), ':');
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 if (!value_len)
deadbeef37f5ecf2017-02-27 14:06:41 -080058 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
Mirko Bonadei6932fb22018-10-15 14:18:03 +000060 return new SSLFingerprint(algorithm, reinterpret_cast<uint8_t*>(value),
61 value_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062}
63
Mirko Bonadei6932fb22018-10-15 14:18:03 +000064SSLFingerprint* SSLFingerprint::CreateFromCertificate(
65 const RTCCertificate* cert) {
deadbeef8662f942017-01-20 21:20:51 -080066 std::string digest_alg;
Mirko Bonadei6932fb22018-10-15 14:18:03 +000067 if (!cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_alg)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010068 RTC_LOG(LS_ERROR)
69 << "Failed to retrieve the certificate's digest algorithm";
deadbeef8662f942017-01-20 21:20:51 -080070 return nullptr;
71 }
72
Mirko Bonadei6932fb22018-10-15 14:18:03 +000073 SSLFingerprint* fingerprint = Create(digest_alg, cert->identity());
deadbeef8662f942017-01-20 21:20:51 -080074 if (!fingerprint) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010075 RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
76 << digest_alg;
deadbeef8662f942017-01-20 21:20:51 -080077 }
78 return fingerprint;
79}
80
Peter Boström0c4e06b2015-10-07 12:23:21 +020081SSLFingerprint::SSLFingerprint(const std::string& algorithm,
82 const uint8_t* digest_in,
83 size_t digest_len)
Mirko Bonadei6932fb22018-10-15 14:18:03 +000084 : algorithm(algorithm) {
85 digest.SetData(digest_in, digest_len);
86}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087
88SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
89 : algorithm(from.algorithm), digest(from.digest) {}
90
91bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
Yves Gerey665174f2018-06-19 15:03:05 +020092 return algorithm == other.algorithm && digest == other.digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093}
94
95std::string SSLFingerprint::GetRfc4572Fingerprint() const {
96 std::string fingerprint =
Karl Wiberg94784372015-04-20 14:03:07 +020097 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
Yves Gerey665174f2018-06-19 15:03:05 +020098 std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(),
99 ::toupper);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 return fingerprint;
101}
102
mikescarlette7748672016-04-29 20:20:54 -0700103std::string SSLFingerprint::ToString() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 std::string fp_str = algorithm;
105 fp_str.append(" ");
106 fp_str.append(GetRfc4572Fingerprint());
107 return fp_str;
108}
109
110} // namespace rtc