openssl_util.h revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// found in the LICENSE file.
4513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
54a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#ifndef BASE_OPENSSL_UTIL_H_
64a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#define BASE_OPENSSL_UTIL_H_
7513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#pragma once
8513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
9513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "base/basictypes.h"
104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "base/tracked.h"
11513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
12513209b27ff55e2841eac0e4120199c23acce758Ben Murdochnamespace base {
13513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
144a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// A helper class that takes care of destroying OpenSSL objects when it goes out
154a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// of scope.
164a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochtemplate <typename T, void (*destructor)(T*)>
174a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochclass ScopedOpenSSL {
184a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch public:
19201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  ScopedOpenSSL() : ptr_(NULL) { }
20201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  explicit ScopedOpenSSL(T* ptr) : ptr_(ptr) { }
214a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  ~ScopedOpenSSL() { if (ptr_) (*destructor)(ptr_); }
224a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
234a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  T* get() const { return ptr_; }
24201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  void reset(T* ptr) {
25201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    if (ptr != ptr_) {
26201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch      if (ptr_) (*destructor)(ptr_);
27201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch      ptr_ = ptr;
28201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    }
29201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  }
304a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
314a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch private:
324a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  T* ptr_;
33201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
34201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSL);
354a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch};
364a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
37513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
38513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
39513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// of the our base wrapper APIs.
40513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// This allows the library to write directly to the caller's buffer if it is of
41513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// sufficient size, but if not it will write to temporary |min_sized_buffer_|
42513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// of required size and then its content is automatically copied out on
43513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// destruction, with truncation as appropriate.
44513209b27ff55e2841eac0e4120199c23acce758Ben Murdochtemplate<int MIN_SIZE>
45513209b27ff55e2841eac0e4120199c23acce758Ben Murdochclass ScopedOpenSSLSafeSizeBuffer {
46513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch public:
47513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len)
48513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch      : output_(output),
49513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch        output_len_(output_len) {
50513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  }
51513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
52513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  ~ScopedOpenSSLSafeSizeBuffer() {
53513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch    if (output_len_ < MIN_SIZE) {
54513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch      // Copy the temporary buffer out, truncating as needed.
55513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch      memcpy(output_, min_sized_buffer_, output_len_);
56513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch    }
57513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch    // else... any writing already happened directly into |output_|.
58513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  }
59513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
60513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  unsigned char* safe_buffer() {
61513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch    return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_;
62513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  }
63513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
64513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch private:
65513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  // Pointer to the caller's data area and it's associated size, where data
66513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  // written via safe_buffer() will [eventually] end up.
67513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  unsigned char* output_;
68513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  size_t output_len_;
69513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
70513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  // Temporary buffer writen into in the case where the caller's
71513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  // buffer is not of sufficient size.
72513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch  unsigned char min_sized_buffer_[MIN_SIZE];
734a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
744a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer);
75513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch};
76513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
774a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Initialize OpenSSL if it isn't already initialized. This must be called
784a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// before any other OpenSSL functions.
794a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// This function is thread-safe, and OpenSSL will only ever be initialized once.
804a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// OpenSSL will be properly shut down on program exit.
814a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochvoid EnsureOpenSSLInit();
824a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
834a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Drains the OpenSSL ERR_get_error stack. On a debug build the error codes
84201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// are send to VLOG(1), on a release build they are disregarded. In most
85201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// cases you should pass FROM_HERE as the |location|.
86201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochvoid ClearOpenSSLERRStack(const tracked_objects::Location& location);
87201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
88201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// Place an instance of this class on the call stack to automatically clear
89201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// the OpenSSL error stack on function exit.
90201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochclass OpenSSLErrStackTracer {
91201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch public:
92201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // Pass FROM_HERE as |location|, to help track the source of OpenSSL error
93201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // messages. Note any diagnostic emitted will be tagged with the location of
94201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // the constructor call as it's not possible to trace a destructor's callsite.
95201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  explicit OpenSSLErrStackTracer(const tracked_objects::Location& location)
96201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch      : location_(location) {
97201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    EnsureOpenSSLInit();
98201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  }
99201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  ~OpenSSLErrStackTracer() {
100201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    ClearOpenSSLERRStack(location_);
101201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  }
102201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
103201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch private:
104201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  const tracked_objects::Location location_;
105201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
106201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer);
107201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch};
1084a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
109513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch}  // namespace base
110513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch
1114a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#endif  // BASE_OPENSSL_UTIL_H_
112