1 // Copyright (c) 2011 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.
5 #ifndef CRYPTO_OPENSSL_UTIL_H_
6 #define CRYPTO_OPENSSL_UTIL_H_
9 #include "base/basictypes.h"
10 #include "base/location.h"
11 #include "crypto/crypto_export.h"
15 // A helper class that takes care of destroying OpenSSL objects when it goes out
17 template <typename T
, void (*destructor
)(T
*)>
20 ScopedOpenSSL() : ptr_(NULL
) { }
21 explicit ScopedOpenSSL(T
* ptr
) : ptr_(ptr
) { }
26 T
* get() const { return ptr_
; }
29 if (ptr_
) (*destructor
)(ptr_
);
37 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSL
);
40 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
41 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
42 // of the our base wrapper APIs.
43 // This allows the library to write directly to the caller's buffer if it is of
44 // sufficient size, but if not it will write to temporary |min_sized_buffer_|
45 // of required size and then its content is automatically copied out on
46 // destruction, with truncation as appropriate.
47 template<int MIN_SIZE
>
48 class ScopedOpenSSLSafeSizeBuffer
{
50 ScopedOpenSSLSafeSizeBuffer(unsigned char* output
, size_t output_len
)
52 output_len_(output_len
) {
55 ~ScopedOpenSSLSafeSizeBuffer() {
56 if (output_len_
< MIN_SIZE
) {
57 // Copy the temporary buffer out, truncating as needed.
58 memcpy(output_
, min_sized_buffer_
, output_len_
);
60 // else... any writing already happened directly into |output_|.
63 unsigned char* safe_buffer() {
64 return output_len_
< MIN_SIZE
? min_sized_buffer_
: output_
;
68 // Pointer to the caller's data area and it's associated size, where data
69 // written via safe_buffer() will [eventually] end up.
70 unsigned char* output_
;
73 // Temporary buffer writen into in the case where the caller's
74 // buffer is not of sufficient size.
75 unsigned char min_sized_buffer_
[MIN_SIZE
];
77 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer
);
80 // Initialize OpenSSL if it isn't already initialized. This must be called
81 // before any other OpenSSL functions.
82 // This function is thread-safe, and OpenSSL will only ever be initialized once.
83 // OpenSSL will be properly shut down on program exit.
84 void CRYPTO_EXPORT
EnsureOpenSSLInit();
86 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes
87 // are send to VLOG(1), on a release build they are disregarded. In most
88 // cases you should pass FROM_HERE as the |location|.
89 void ClearOpenSSLERRStack(const tracked_objects::Location
& location
);
91 // Place an instance of this class on the call stack to automatically clear
92 // the OpenSSL error stack on function exit.
93 class OpenSSLErrStackTracer
{
95 // Pass FROM_HERE as |location|, to help track the source of OpenSSL error
96 // messages. Note any diagnostic emitted will be tagged with the location of
97 // the constructor call as it's not possible to trace a destructor's callsite.
98 explicit OpenSSLErrStackTracer(const tracked_objects::Location
& location
)
99 : location_(location
) {
102 ~OpenSSLErrStackTracer() {
103 ClearOpenSSLERRStack(location_
);
107 const tracked_objects::Location location_
;
109 DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer
);
112 } // namespace crypto
114 #endif // CRYPTO_OPENSSL_UTIL_H_