Home | History | Annotate | Download | only in base
      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/common.h"
     18 #include "webrtc/base/messagedigest.h"
     19 #include "webrtc/base/sslidentity.h"
     20 
     21 namespace rtc {
     22 
     23 class FakeSSLCertificate : public rtc::SSLCertificate {
     24  public:
     25   // SHA-1 is the default digest algorithm because it is available in all build
     26   // configurations used for unit testing.
     27   explicit FakeSSLCertificate(const std::string& data)
     28       : data_(data), digest_algorithm_(DIGEST_SHA_1), expiration_time_(-1) {}
     29   explicit FakeSSLCertificate(const std::vector<std::string>& certs)
     30       : data_(certs.front()),
     31         digest_algorithm_(DIGEST_SHA_1),
     32         expiration_time_(-1) {
     33     std::vector<std::string>::const_iterator it;
     34     // Skip certs[0].
     35     for (it = certs.begin() + 1; it != certs.end(); ++it) {
     36       certs_.push_back(FakeSSLCertificate(*it));
     37     }
     38   }
     39   virtual FakeSSLCertificate* GetReference() const {
     40     return new FakeSSLCertificate(*this);
     41   }
     42   virtual std::string ToPEMString() const {
     43     return data_;
     44   }
     45   virtual void ToDER(Buffer* der_buffer) const {
     46     std::string der_string;
     47     VERIFY(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
     48     der_buffer->SetData(der_string.c_str(), der_string.size());
     49   }
     50   int64_t CertificateExpirationTime() const override {
     51     return expiration_time_;
     52   }
     53   void SetCertificateExpirationTime(int64_t expiration_time) {
     54     expiration_time_ = expiration_time;
     55   }
     56   void set_digest_algorithm(const std::string& algorithm) {
     57     digest_algorithm_ = algorithm;
     58   }
     59   virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const {
     60     *algorithm = digest_algorithm_;
     61     return true;
     62   }
     63   virtual bool ComputeDigest(const std::string& algorithm,
     64                              unsigned char* digest,
     65                              size_t size,
     66                              size_t* length) const {
     67     *length = rtc::ComputeDigest(algorithm, data_.c_str(), data_.size(),
     68                                        digest, size);
     69     return (*length != 0);
     70   }
     71   virtual bool GetChain(SSLCertChain** chain) const {
     72     if (certs_.empty())
     73       return false;
     74     std::vector<SSLCertificate*> new_certs(certs_.size());
     75     std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
     76     *chain = new SSLCertChain(new_certs);
     77     std::for_each(new_certs.begin(), new_certs.end(), DeleteCert);
     78     return true;
     79   }
     80 
     81  private:
     82   static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
     83     return cert.GetReference();
     84   }
     85   static void DeleteCert(SSLCertificate* cert) { delete cert; }
     86   std::string data_;
     87   std::vector<FakeSSLCertificate> certs_;
     88   std::string digest_algorithm_;
     89   // Expiration time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC).
     90   int64_t expiration_time_;
     91 };
     92 
     93 class FakeSSLIdentity : public rtc::SSLIdentity {
     94  public:
     95   explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
     96   explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
     97   virtual FakeSSLIdentity* GetReference() const {
     98     return new FakeSSLIdentity(*this);
     99   }
    100   virtual const FakeSSLCertificate& certificate() const { return cert_; }
    101  private:
    102   FakeSSLCertificate cert_;
    103 };
    104 
    105 }  // namespace rtc
    106 
    107 #endif  // WEBRTC_BASE_FAKESSLIDENTITY_H_
    108