blob: 577749e77813ddb03b871f4d709d1d115ddb3a31 [file] [log] [blame]
[email protected]cf211882012-07-11 07:19:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]70372d42010-10-22 13:12:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_OPENSSL_UTIL_H_
6#define CRYPTO_OPENSSL_UTIL_H_
[email protected]70372d42010-10-22 13:12:347
8#include "base/basictypes.h"
[email protected]c62dd9d2011-09-21 18:05:419#include "base/location.h"
[email protected]fb10ca02011-10-06 19:17:3610#include "crypto/crypto_export.h"
[email protected]70372d42010-10-22 13:12:3411
[email protected]4b559b4d2011-04-14 17:37:1412namespace crypto {
[email protected]70372d42010-10-22 13:12:3413
[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]32765f82010-12-16 00:01:3721 ~ScopedOpenSSL() {
22 reset(NULL);
23 }
[email protected]313834722010-11-17 09:57:1824
25 T* get() const { return ptr_; }
[email protected]be796bb2010-11-18 15:43:4326 void reset(T* ptr) {
27 if (ptr != ptr_) {
28 if (ptr_) (*destructor)(ptr_);
29 ptr_ = ptr;
30 }
31 }
[email protected]313834722010-11-17 09:57:1832
33 private:
34 T* ptr_;
[email protected]fbef13932010-11-23 12:38:5335
36 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSL);
[email protected]313834722010-11-17 09:57:1837};
38
[email protected]70372d42010-10-22 13:12:3439// Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
40// SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
41// of the our base wrapper APIs.
42// This allows the library to write directly to the caller's buffer if it is of
43// sufficient size, but if not it will write to temporary |min_sized_buffer_|
44// of required size and then its content is automatically copied out on
45// destruction, with truncation as appropriate.
46template<int MIN_SIZE>
47class ScopedOpenSSLSafeSizeBuffer {
48 public:
49 ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len)
50 : output_(output),
51 output_len_(output_len) {
52 }
53
54 ~ScopedOpenSSLSafeSizeBuffer() {
55 if (output_len_ < MIN_SIZE) {
56 // Copy the temporary buffer out, truncating as needed.
57 memcpy(output_, min_sized_buffer_, output_len_);
58 }
59 // else... any writing already happened directly into |output_|.
60 }
61
62 unsigned char* safe_buffer() {
63 return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_;
64 }
65
66 private:
[email protected]a2c92a1c2012-04-03 12:32:1467 // Pointer to the caller's data area and its associated size, where data
[email protected]70372d42010-10-22 13:12:3468 // written via safe_buffer() will [eventually] end up.
69 unsigned char* output_;
70 size_t output_len_;
71
72 // Temporary buffer writen into in the case where the caller's
73 // buffer is not of sufficient size.
74 unsigned char min_sized_buffer_[MIN_SIZE];
[email protected]ac0f8be2010-11-12 12:03:5475
76 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer);
77};
78
[email protected]313834722010-11-17 09:57:1879// Initialize OpenSSL if it isn't already initialized. This must be called
80// before any other OpenSSL functions.
81// This function is thread-safe, and OpenSSL will only ever be initialized once.
82// OpenSSL will be properly shut down on program exit.
[email protected]fb10ca02011-10-06 19:17:3683void CRYPTO_EXPORT EnsureOpenSSLInit();
[email protected]313834722010-11-17 09:57:1884
[email protected]ac0f8be2010-11-12 12:03:5485// Drains the OpenSSL ERR_get_error stack. On a debug build the error codes
[email protected]be796bb2010-11-18 15:43:4386// are send to VLOG(1), on a release build they are disregarded. In most
87// cases you should pass FROM_HERE as the |location|.
88void ClearOpenSSLERRStack(const tracked_objects::Location& location);
89
90// Place an instance of this class on the call stack to automatically clear
91// the OpenSSL error stack on function exit.
92class OpenSSLErrStackTracer {
93 public:
94 // Pass FROM_HERE as |location|, to help track the source of OpenSSL error
95 // messages. Note any diagnostic emitted will be tagged with the location of
96 // the constructor call as it's not possible to trace a destructor's callsite.
97 explicit OpenSSLErrStackTracer(const tracked_objects::Location& location)
98 : location_(location) {
99 EnsureOpenSSLInit();
100 }
101 ~OpenSSLErrStackTracer() {
102 ClearOpenSSLERRStack(location_);
103 }
104
105 private:
106 const tracked_objects::Location location_;
107
108 DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer);
109};
[email protected]ac0f8be2010-11-12 12:03:54110
[email protected]4b559b4d2011-04-14 17:37:14111} // namespace crypto
[email protected]70372d42010-10-22 13:12:34112
[email protected]4b559b4d2011-04-14 17:37:14113#endif // CRYPTO_OPENSSL_UTIL_H_