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