blob: 717cb6c3be6376d36a7f39b2407da09f3cb7e278 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-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#ifndef WEBRTC_BASE_FAKESSLIDENTITY_H_
12#define WEBRTC_BASE_FAKESSLIDENTITY_H_
13
14#include <algorithm>
15#include <vector>
16
17#include "webrtc/base/messagedigest.h"
18#include "webrtc/base/sslidentity.h"
19
20namespace rtc {
21
22class FakeSSLCertificate : public rtc::SSLCertificate {
23 public:
24 // SHA-1 is the default digest algorithm because it is available in all build
25 // configurations used for unit testing.
26 explicit FakeSSLCertificate(const std::string& data)
27 : data_(data), digest_algorithm_(DIGEST_SHA_1) {}
28 explicit FakeSSLCertificate(const std::vector<std::string>& certs)
29 : data_(certs.front()), digest_algorithm_(DIGEST_SHA_1) {
30 std::vector<std::string>::const_iterator it;
31 // Skip certs[0].
32 for (it = certs.begin() + 1; it != certs.end(); ++it) {
33 certs_.push_back(FakeSSLCertificate(*it));
34 }
35 }
36 virtual FakeSSLCertificate* GetReference() const {
37 return new FakeSSLCertificate(*this);
38 }
39 virtual std::string ToPEMString() const {
40 return data_;
41 }
42 virtual void ToDER(Buffer* der_buffer) const {
43 std::string der_string;
44 VERIFY(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
45 der_buffer->SetData(der_string.c_str(), der_string.size());
46 }
47 void set_digest_algorithm(const std::string& algorithm) {
48 digest_algorithm_ = algorithm;
49 }
50 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const {
51 *algorithm = digest_algorithm_;
52 return true;
53 }
54 virtual bool ComputeDigest(const std::string& algorithm,
55 unsigned char* digest,
56 size_t size,
57 size_t* length) const {
58 *length = rtc::ComputeDigest(algorithm, data_.c_str(), data_.size(),
59 digest, size);
60 return (*length != 0);
61 }
62 virtual bool GetChain(SSLCertChain** chain) const {
63 if (certs_.empty())
64 return false;
65 std::vector<SSLCertificate*> new_certs(certs_.size());
66 std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
67 *chain = new SSLCertChain(new_certs);
68 return true;
69 }
70
71 private:
72 static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
73 return cert.GetReference();
74 }
75 std::string data_;
76 std::vector<FakeSSLCertificate> certs_;
77 std::string digest_algorithm_;
78};
79
80class FakeSSLIdentity : public rtc::SSLIdentity {
81 public:
82 explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
83 explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
84 virtual FakeSSLIdentity* GetReference() const {
85 return new FakeSSLIdentity(*this);
86 }
87 virtual const FakeSSLCertificate& certificate() const { return cert_; }
88 private:
89 FakeSSLCertificate cert_;
90};
91
92} // namespace rtc
93
94#endif // WEBRTC_BASE_FAKESSLIDENTITY_H_