Home | History | Annotate | Download | only in base
      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_OPENSSL_UTIL_H_
      6 #define BASE_OPENSSL_UTIL_H_
      7 #pragma once
      8 
      9 #include "base/basictypes.h"
     10 #include "base/compiler_specific.h"
     11 #include "base/tracked.h"
     12 
     13 namespace base {
     14 
     15 // A helper class that takes care of destroying OpenSSL objects when it goes out
     16 // of scope.
     17 template <typename T, void (*destructor)(T*)>
     18 class ScopedOpenSSL {
     19  public:
     20   ScopedOpenSSL() : ptr_(NULL) { }
     21   explicit ScopedOpenSSL(T* ptr) : ptr_(ptr) { }
     22   ~ScopedOpenSSL() {
     23     reset(NULL);
     24   }
     25 
     26   T* get() const { return ptr_; }
     27   void reset(T* ptr) {
     28     if (ptr != ptr_) {
     29       if (ptr_) (*destructor)(ptr_);
     30       ptr_ = ptr;
     31     }
     32   }
     33   T* release() WARN_UNUSED_RESULT {
     34     T* result = ptr_;
     35     ptr_ = NULL;
     36     return result;
     37   }
     38 
     39  private:
     40   T* ptr_;
     41 
     42   DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSL);
     43 };
     44 
     45 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
     46 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
     47 // of the our base wrapper APIs.
     48 // This allows the library to write directly to the caller's buffer if it is of
     49 // sufficient size, but if not it will write to temporary |min_sized_buffer_|
     50 // of required size and then its content is automatically copied out on
     51 // destruction, with truncation as appropriate.
     52 template<int MIN_SIZE>
     53 class ScopedOpenSSLSafeSizeBuffer {
     54  public:
     55   ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len)
     56       : output_(output),
     57         output_len_(output_len) {
     58   }
     59 
     60   ~ScopedOpenSSLSafeSizeBuffer() {
     61     if (output_len_ < MIN_SIZE) {
     62       // Copy the temporary buffer out, truncating as needed.
     63       memcpy(output_, min_sized_buffer_, output_len_);
     64     }
     65     // else... any writing already happened directly into |output_|.
     66   }
     67 
     68   unsigned char* safe_buffer() {
     69     return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_;
     70   }
     71 
     72  private:
     73   // Pointer to the caller's data area and it's associated size, where data
     74   // written via safe_buffer() will [eventually] end up.
     75   unsigned char* output_;
     76   size_t output_len_;
     77 
     78   // Temporary buffer writen into in the case where the caller's
     79   // buffer is not of sufficient size.
     80   unsigned char min_sized_buffer_[MIN_SIZE];
     81 
     82   DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer);
     83 };
     84 
     85 // Initialize OpenSSL if it isn't already initialized. This must be called
     86 // before any other OpenSSL functions.
     87 // This function is thread-safe, and OpenSSL will only ever be initialized once.
     88 // OpenSSL will be properly shut down on program exit.
     89 void EnsureOpenSSLInit();
     90 
     91 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes
     92 // are send to VLOG(1), on a release build they are disregarded. In most
     93 // cases you should pass FROM_HERE as the |location|.
     94 void ClearOpenSSLERRStack(const tracked_objects::Location& location);
     95 
     96 // Place an instance of this class on the call stack to automatically clear
     97 // the OpenSSL error stack on function exit.
     98 class OpenSSLErrStackTracer {
     99  public:
    100   // Pass FROM_HERE as |location|, to help track the source of OpenSSL error
    101   // messages. Note any diagnostic emitted will be tagged with the location of
    102   // the constructor call as it's not possible to trace a destructor's callsite.
    103   explicit OpenSSLErrStackTracer(const tracked_objects::Location& location)
    104       : location_(location) {
    105     EnsureOpenSSLInit();
    106   }
    107   ~OpenSSLErrStackTracer() {
    108     ClearOpenSSLERRStack(location_);
    109   }
    110 
    111  private:
    112   const tracked_objects::Location location_;
    113 
    114   DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer);
    115 };
    116 
    117 }  // namespace base
    118 
    119 #endif  // BASE_OPENSSL_UTIL_H_
    120