You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef _TALK_BASE_CRYPTSTRING_H_
  28. #define _TALK_BASE_CRYPTSTRING_H_
  29. #include <cstring>
  30. #include <string>
  31. #include <vector>
  32. #include "talk/base/linked_ptr.h"
  33. #include "talk/base/scoped_ptr.h"
  34. namespace talk_base {
  35. class CryptStringImpl {
  36. public:
  37. virtual ~CryptStringImpl() {}
  38. virtual size_t GetLength() const = 0;
  39. virtual void CopyTo(char * dest, bool nullterminate) const = 0;
  40. virtual std::string UrlEncode() const = 0;
  41. virtual CryptStringImpl * Copy() const = 0;
  42. virtual void CopyRawTo(std::vector<unsigned char> * dest) const = 0;
  43. };
  44. class EmptyCryptStringImpl : public CryptStringImpl {
  45. public:
  46. virtual ~EmptyCryptStringImpl() {}
  47. virtual size_t GetLength() const { return 0; }
  48. virtual void CopyTo(char * dest, bool nullterminate) const {
  49. if (nullterminate) {
  50. *dest = '\0';
  51. }
  52. }
  53. virtual std::string UrlEncode() const { return ""; }
  54. virtual CryptStringImpl * Copy() const { return new EmptyCryptStringImpl(); }
  55. virtual void CopyRawTo(std::vector<unsigned char> * dest) const {
  56. dest->clear();
  57. }
  58. };
  59. class CryptString {
  60. public:
  61. CryptString() : impl_(new EmptyCryptStringImpl()) {}
  62. size_t GetLength() const { return impl_->GetLength(); }
  63. void CopyTo(char * dest, bool nullterminate) const { impl_->CopyTo(dest, nullterminate); }
  64. CryptString(const CryptString & other) : impl_(other.impl_->Copy()) {}
  65. explicit CryptString(const CryptStringImpl & impl) : impl_(impl.Copy()) {}
  66. CryptString & operator=(const CryptString & other) {
  67. if (this != &other) {
  68. impl_.reset(other.impl_->Copy());
  69. }
  70. return *this;
  71. }
  72. void Clear() { impl_.reset(new EmptyCryptStringImpl()); }
  73. std::string UrlEncode() const { return impl_->UrlEncode(); }
  74. void CopyRawTo(std::vector<unsigned char> * dest) const {
  75. return impl_->CopyRawTo(dest);
  76. }
  77. private:
  78. scoped_ptr<const CryptStringImpl> impl_;
  79. };
  80. // Used for constructing strings where a password is involved and we
  81. // need to ensure that we zero memory afterwards
  82. class FormatCryptString {
  83. public:
  84. FormatCryptString() {
  85. storage_ = new char[32];
  86. capacity_ = 32;
  87. length_ = 0;
  88. storage_[0] = 0;
  89. }
  90. void Append(const std::string & text) {
  91. Append(text.data(), text.length());
  92. }
  93. void Append(const char * data, size_t length) {
  94. EnsureStorage(length_ + length + 1);
  95. memcpy(storage_ + length_, data, length);
  96. length_ += length;
  97. storage_[length_] = '\0';
  98. }
  99. void Append(const CryptString * password) {
  100. size_t len = password->GetLength();
  101. EnsureStorage(length_ + len + 1);
  102. password->CopyTo(storage_ + length_, true);
  103. length_ += len;
  104. }
  105. size_t GetLength() {
  106. return length_;
  107. }
  108. const char * GetData() {
  109. return storage_;
  110. }
  111. // Ensures storage of at least n bytes
  112. void EnsureStorage(size_t n) {
  113. if (capacity_ >= n) {
  114. return;
  115. }
  116. size_t old_capacity = capacity_;
  117. char * old_storage = storage_;
  118. for (;;) {
  119. capacity_ *= 2;
  120. if (capacity_ >= n)
  121. break;
  122. }
  123. storage_ = new char[capacity_];
  124. if (old_capacity) {
  125. memcpy(storage_, old_storage, length_);
  126. // zero memory in a way that an optimizer won't optimize it out
  127. old_storage[0] = 0;
  128. for (size_t i = 1; i < old_capacity; i++) {
  129. old_storage[i] = old_storage[i - 1];
  130. }
  131. delete[] old_storage;
  132. }
  133. }
  134. ~FormatCryptString() {
  135. if (capacity_) {
  136. storage_[0] = 0;
  137. for (size_t i = 1; i < capacity_; i++) {
  138. storage_[i] = storage_[i - 1];
  139. }
  140. }
  141. delete[] storage_;
  142. }
  143. private:
  144. char * storage_;
  145. size_t capacity_;
  146. size_t length_;
  147. };
  148. class InsecureCryptStringImpl : public CryptStringImpl {
  149. public:
  150. std::string& password() { return password_; }
  151. const std::string& password() const { return password_; }
  152. virtual ~InsecureCryptStringImpl() {}
  153. virtual size_t GetLength() const { return password_.size(); }
  154. virtual void CopyTo(char * dest, bool nullterminate) const {
  155. memcpy(dest, password_.data(), password_.size());
  156. if (nullterminate) dest[password_.size()] = 0;
  157. }
  158. virtual std::string UrlEncode() const { return password_; }
  159. virtual CryptStringImpl * Copy() const {
  160. InsecureCryptStringImpl * copy = new InsecureCryptStringImpl;
  161. copy->password() = password_;
  162. return copy;
  163. }
  164. virtual void CopyRawTo(std::vector<unsigned char> * dest) const {
  165. dest->resize(password_.size());
  166. memcpy(&dest->front(), password_.data(), password_.size());
  167. }
  168. private:
  169. std::string password_;
  170. };
  171. }
  172. #endif // _TALK_BASE_CRYPTSTRING_H_