OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_OPENNSSL_UTIL_H_ |
| 6 #define BASE_OPENNSSL_UTIL_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 |
| 11 namespace base { |
| 12 |
| 13 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's |
| 14 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those |
| 15 // of the our base wrapper APIs. |
| 16 // This allows the library to write directly to the caller's buffer if it is of |
| 17 // sufficient size, but if not it will write to temporary |min_sized_buffer_| |
| 18 // of required size and then its content is automatically copied out on |
| 19 // destruction, with truncation as appropriate. |
| 20 template<int MIN_SIZE> |
| 21 class ScopedOpenSSLSafeSizeBuffer { |
| 22 public: |
| 23 ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len) |
| 24 : output_(output), |
| 25 output_len_(output_len) { |
| 26 } |
| 27 |
| 28 ~ScopedOpenSSLSafeSizeBuffer() { |
| 29 if (output_len_ < MIN_SIZE) { |
| 30 // Copy the temporary buffer out, truncating as needed. |
| 31 memcpy(output_, min_sized_buffer_, output_len_); |
| 32 } |
| 33 // else... any writing already happened directly into |output_|. |
| 34 } |
| 35 |
| 36 unsigned char* safe_buffer() { |
| 37 return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_; |
| 38 } |
| 39 |
| 40 private: |
| 41 // Pointer to the caller's data area and it's associated size, where data |
| 42 // written via safe_buffer() will [eventually] end up. |
| 43 unsigned char* output_; |
| 44 size_t output_len_; |
| 45 |
| 46 // Temporary buffer writen into in the case where the caller's |
| 47 // buffer is not of sufficient size. |
| 48 unsigned char min_sized_buffer_[MIN_SIZE]; |
| 49 }; |
| 50 |
| 51 } // namespace base |
| 52 |
| 53 #endif // BASE_NSS_UTIL_H_ |
OLD | NEW |