1/*
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    std::for_each(new_certs.begin(), new_certs.end(), DeleteCert);
69    return true;
70  }
71
72 private:
73  static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
74    return cert.GetReference();
75  }
76  static void DeleteCert(SSLCertificate* cert) { delete cert; }
77  std::string data_;
78  std::vector<FakeSSLCertificate> certs_;
79  std::string digest_algorithm_;
80};
81
82class FakeSSLIdentity : public rtc::SSLIdentity {
83 public:
84  explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
85  explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
86  virtual FakeSSLIdentity* GetReference() const {
87    return new FakeSSLIdentity(*this);
88  }
89  virtual const FakeSSLCertificate& certificate() const { return cert_; }
90 private:
91  FakeSSLCertificate cert_;
92};
93
94}  // namespace rtc
95
96#endif  // WEBRTC_BASE_FAKESSLIDENTITY_H_
97