2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2012 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef WEBRTC_BASE_FAKESSLIDENTITY_H_
|
|
|
|
|
#define WEBRTC_BASE_FAKESSLIDENTITY_H_
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
2016-04-26 03:13:22 -07:00
|
|
|
#include <memory>
|
2014-05-13 18:00:26 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
#include "webrtc/base/common.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
#include "webrtc/base/messagedigest.h"
|
|
|
|
|
#include "webrtc/base/sslidentity.h"
|
|
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
class FakeSSLCertificate : public rtc::SSLCertificate {
|
|
|
|
|
public:
|
|
|
|
|
// SHA-1 is the default digest algorithm because it is available in all build
|
|
|
|
|
// configurations used for unit testing.
|
|
|
|
|
explicit FakeSSLCertificate(const std::string& data)
|
2015-12-09 05:26:49 -08:00
|
|
|
: data_(data), digest_algorithm_(DIGEST_SHA_1), expiration_time_(-1) {}
|
2014-05-13 18:00:26 +00:00
|
|
|
explicit FakeSSLCertificate(const std::vector<std::string>& certs)
|
2015-12-09 05:26:49 -08:00
|
|
|
: data_(certs.front()),
|
|
|
|
|
digest_algorithm_(DIGEST_SHA_1),
|
|
|
|
|
expiration_time_(-1) {
|
2014-05-13 18:00:26 +00:00
|
|
|
std::vector<std::string>::const_iterator it;
|
|
|
|
|
// Skip certs[0].
|
|
|
|
|
for (it = certs.begin() + 1; it != certs.end(); ++it) {
|
|
|
|
|
certs_.push_back(FakeSSLCertificate(*it));
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-29 06:09:15 -07:00
|
|
|
FakeSSLCertificate* GetReference() const override {
|
2014-05-13 18:00:26 +00:00
|
|
|
return new FakeSSLCertificate(*this);
|
|
|
|
|
}
|
2016-04-29 06:09:15 -07:00
|
|
|
std::string ToPEMString() const override {
|
2014-05-13 18:00:26 +00:00
|
|
|
return data_;
|
|
|
|
|
}
|
2016-04-29 06:09:15 -07:00
|
|
|
void ToDER(Buffer* der_buffer) const override {
|
2014-05-13 18:00:26 +00:00
|
|
|
std::string der_string;
|
|
|
|
|
VERIFY(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string));
|
|
|
|
|
der_buffer->SetData(der_string.c_str(), der_string.size());
|
|
|
|
|
}
|
2015-12-09 05:26:49 -08:00
|
|
|
int64_t CertificateExpirationTime() const override {
|
|
|
|
|
return expiration_time_;
|
|
|
|
|
}
|
|
|
|
|
void SetCertificateExpirationTime(int64_t expiration_time) {
|
|
|
|
|
expiration_time_ = expiration_time;
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
void set_digest_algorithm(const std::string& algorithm) {
|
|
|
|
|
digest_algorithm_ = algorithm;
|
|
|
|
|
}
|
2016-04-29 06:09:15 -07:00
|
|
|
bool GetSignatureDigestAlgorithm(std::string* algorithm) const override {
|
2014-05-13 18:00:26 +00:00
|
|
|
*algorithm = digest_algorithm_;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-04-29 06:09:15 -07:00
|
|
|
bool ComputeDigest(const std::string& algorithm,
|
|
|
|
|
unsigned char* digest,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t* length) const override {
|
2014-05-13 18:00:26 +00:00
|
|
|
*length = rtc::ComputeDigest(algorithm, data_.c_str(), data_.size(),
|
|
|
|
|
digest, size);
|
|
|
|
|
return (*length != 0);
|
|
|
|
|
}
|
2016-04-29 06:09:15 -07:00
|
|
|
std::unique_ptr<SSLCertChain> GetChain() const override {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (certs_.empty())
|
2016-03-15 12:53:24 -07:00
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
std::vector<SSLCertificate*> new_certs(certs_.size());
|
|
|
|
|
std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert);
|
2016-04-26 03:13:22 -07:00
|
|
|
std::unique_ptr<SSLCertChain> chain(new SSLCertChain(new_certs));
|
2014-08-11 14:32:13 +00:00
|
|
|
std::for_each(new_certs.begin(), new_certs.end(), DeleteCert);
|
2016-03-15 12:53:24 -07:00
|
|
|
return chain;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static FakeSSLCertificate* DupCert(FakeSSLCertificate cert) {
|
|
|
|
|
return cert.GetReference();
|
|
|
|
|
}
|
2014-08-11 14:32:13 +00:00
|
|
|
static void DeleteCert(SSLCertificate* cert) { delete cert; }
|
2014-05-13 18:00:26 +00:00
|
|
|
std::string data_;
|
|
|
|
|
std::vector<FakeSSLCertificate> certs_;
|
|
|
|
|
std::string digest_algorithm_;
|
2015-12-09 05:26:49 -08:00
|
|
|
// Expiration time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC).
|
|
|
|
|
int64_t expiration_time_;
|
2014-05-13 18:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FakeSSLIdentity : public rtc::SSLIdentity {
|
|
|
|
|
public:
|
|
|
|
|
explicit FakeSSLIdentity(const std::string& data) : cert_(data) {}
|
|
|
|
|
explicit FakeSSLIdentity(const FakeSSLCertificate& cert) : cert_(cert) {}
|
|
|
|
|
virtual FakeSSLIdentity* GetReference() const {
|
|
|
|
|
return new FakeSSLIdentity(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual const FakeSSLCertificate& certificate() const { return cert_; }
|
2016-04-28 05:14:21 -07:00
|
|
|
virtual std::string PrivateKeyToPEMString() const {
|
|
|
|
|
RTC_NOTREACHED(); // Not implemented.
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
virtual std::string PublicKeyToPEMString() const {
|
|
|
|
|
RTC_NOTREACHED(); // Not implemented.
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
virtual bool operator==(const SSLIdentity& other) const {
|
|
|
|
|
RTC_NOTREACHED(); // Not implemented.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
private:
|
|
|
|
|
FakeSSLCertificate cert_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|
|
|
|
|
|
|
|
|
|
#endif // WEBRTC_BASE_FAKESSLIDENTITY_H_
|