blob: b651a3d6285205b55a2fb03677104b15a97b7662 [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/helpers.h"
17#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
Yves Gerey665174f2018-06-19 15:03:05 +020023SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm,
24 const rtc::SSLIdentity* identity) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025 if (!identity) {
deadbeef37f5ecf2017-02-27 14:06:41 -080026 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027 }
28
29 return Create(algorithm, &(identity->certificate()));
30}
31
Yves Gerey665174f2018-06-19 15:03:05 +020032SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm,
33 const rtc::SSLCertificate* cert) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020034 uint8_t digest_val[64];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 size_t digest_len;
Yves Gerey665174f2018-06-19 15:03:05 +020036 bool ret = cert->ComputeDigest(algorithm, digest_val, sizeof(digest_val),
37 &digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 if (!ret) {
deadbeef37f5ecf2017-02-27 14:06:41 -080039 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 }
41
42 return new SSLFingerprint(algorithm, digest_val, digest_len);
43}
44
45SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
Yves Gerey665174f2018-06-19 15:03:05 +020046 const std::string& algorithm,
47 const std::string& fingerprint) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
deadbeef37f5ecf2017-02-27 14:06:41 -080049 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51 if (fingerprint.empty())
deadbeef37f5ecf2017-02-27 14:06:41 -080052 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053
54 size_t value_len;
55 char value[rtc::MessageDigest::kMaxSize];
Yves Gerey665174f2018-06-19 15:03:05 +020056 value_len = rtc::hex_decode_with_delimiter(
57 value, sizeof(value), fingerprint.c_str(), fingerprint.length(), ':');
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 if (!value_len)
deadbeef37f5ecf2017-02-27 14:06:41 -080059 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060
Peter Boström0c4e06b2015-10-07 12:23:21 +020061 return new SSLFingerprint(algorithm, reinterpret_cast<uint8_t*>(value),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 value_len);
63}
64
deadbeef8662f942017-01-20 21:20:51 -080065SSLFingerprint* SSLFingerprint::CreateFromCertificate(
66 const RTCCertificate* cert) {
67 std::string digest_alg;
68 if (!cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_alg)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 RTC_LOG(LS_ERROR)
70 << "Failed to retrieve the certificate's digest algorithm";
deadbeef8662f942017-01-20 21:20:51 -080071 return nullptr;
72 }
73
74 SSLFingerprint* fingerprint = Create(digest_alg, cert->identity());
75 if (!fingerprint) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010076 RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
77 << digest_alg;
deadbeef8662f942017-01-20 21:20:51 -080078 }
79 return fingerprint;
80}
81
Peter Boström0c4e06b2015-10-07 12:23:21 +020082SSLFingerprint::SSLFingerprint(const std::string& algorithm,
83 const uint8_t* digest_in,
84 size_t digest_len)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 : algorithm(algorithm) {
86 digest.SetData(digest_in, digest_len);
87}
88
89SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
90 : algorithm(from.algorithm), digest(from.digest) {}
91
92bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
Yves Gerey665174f2018-06-19 15:03:05 +020093 return algorithm == other.algorithm && digest == other.digest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094}
95
96std::string SSLFingerprint::GetRfc4572Fingerprint() const {
97 std::string fingerprint =
Karl Wiberg94784372015-04-20 14:03:07 +020098 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
Yves Gerey665174f2018-06-19 15:03:05 +020099 std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(),
100 ::toupper);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 return fingerprint;
102}
103
mikescarlette7748672016-04-29 20:20:54 -0700104std::string SSLFingerprint::ToString() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 std::string fp_str = algorithm;
106 fp_str.append(" ");
107 fp_str.append(GetRfc4572Fingerprint());
108 return fp_str;
109}
110
111} // namespace rtc