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