blob: 1939b4fd0bd8c9cacb33ffef76b89b35bfe736b3 [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
11#include "webrtc/base/sslfingerprint.h"
12
13#include <ctype.h>
14#include <string>
15
16#include "webrtc/base/helpers.h"
17#include "webrtc/base/messagedigest.h"
18#include "webrtc/base/stringencode.h"
19
20namespace rtc {
21
22SSLFingerprint* SSLFingerprint::Create(
23 const std::string& algorithm, const rtc::SSLIdentity* identity) {
24 if (!identity) {
25 return NULL;
26 }
27
28 return Create(algorithm, &(identity->certificate()));
29}
30
31SSLFingerprint* SSLFingerprint::Create(
32 const std::string& algorithm, 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;
35 bool ret = cert->ComputeDigest(
36 algorithm, digest_val, sizeof(digest_val), &digest_len);
37 if (!ret) {
38 return NULL;
39 }
40
41 return new SSLFingerprint(algorithm, digest_val, digest_len);
42}
43
44SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
45 const std::string& algorithm, const std::string& fingerprint) {
46 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
47 return NULL;
48
49 if (fingerprint.empty())
50 return NULL;
51
52 size_t value_len;
53 char value[rtc::MessageDigest::kMaxSize];
54 value_len = rtc::hex_decode_with_delimiter(value, sizeof(value),
55 fingerprint.c_str(),
56 fingerprint.length(),
57 ':');
58 if (!value_len)
59 return NULL;
60
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
Peter Boström0c4e06b2015-10-07 12:23:21 +020065SSLFingerprint::SSLFingerprint(const std::string& algorithm,
66 const uint8_t* digest_in,
67 size_t digest_len)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 : algorithm(algorithm) {
69 digest.SetData(digest_in, digest_len);
70}
71
72SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
73 : algorithm(from.algorithm), digest(from.digest) {}
74
75bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
76 return algorithm == other.algorithm &&
77 digest == other.digest;
78}
79
80std::string SSLFingerprint::GetRfc4572Fingerprint() const {
81 std::string fingerprint =
Karl Wiberg94784372015-04-20 14:03:07 +020082 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 std::transform(fingerprint.begin(), fingerprint.end(),
84 fingerprint.begin(), ::toupper);
85 return fingerprint;
86}
87
88std::string SSLFingerprint::ToString() {
89 std::string fp_str = algorithm;
90 fp_str.append(" ");
91 fp_str.append(GetRfc4572Fingerprint());
92 return fp_str;
93}
94
95} // namespace rtc