Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Side by Side Diff: base/openssl_util.h

Issue 4963002: Refactor EnsureOpenSSLInit and openssl_util into base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_OPENSSL_UTIL_H_ 5 #ifndef BASE_OPENSSL_UTIL_H_
6 #define BASE_OPENSSL_UTIL_H_ 6 #define BASE_OPENSSL_UTIL_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/tracked.h" 10 #include "base/tracked.h"
11 11
12 namespace base { 12 namespace base {
13 13
14 // A helper class that takes care of destroying OpenSSL objects when it goes out
15 // of scope.
16 template <typename T, void (*destructor)(T*)>
17 class ScopedSSL {
wtc 2010/11/16 15:35:07 This template class should be named ScopedOpenSSL.
joth 2010/11/16 16:07:19 Done.
18 public:
19 explicit ScopedSSL(T* ptr_) : ptr_(ptr_) { }
20 ~ScopedSSL() { if (ptr_) (*destructor)(ptr_); }
21
22 T* get() const { return ptr_; }
23
24 private:
25 T* ptr_;
26 };
27
14 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's 28 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
15 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those 29 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
16 // of the our base wrapper APIs. 30 // of the our base wrapper APIs.
17 // This allows the library to write directly to the caller's buffer if it is of 31 // This allows the library to write directly to the caller's buffer if it is of
18 // sufficient size, but if not it will write to temporary |min_sized_buffer_| 32 // sufficient size, but if not it will write to temporary |min_sized_buffer_|
19 // of required size and then its content is automatically copied out on 33 // of required size and then its content is automatically copied out on
20 // destruction, with truncation as appropriate. 34 // destruction, with truncation as appropriate.
21 template<int MIN_SIZE> 35 template<int MIN_SIZE>
22 class ScopedOpenSSLSafeSizeBuffer { 36 class ScopedOpenSSLSafeSizeBuffer {
23 public: 37 public:
(...skipping 20 matching lines...) Expand all
44 unsigned char* output_; 58 unsigned char* output_;
45 size_t output_len_; 59 size_t output_len_;
46 60
47 // Temporary buffer writen into in the case where the caller's 61 // Temporary buffer writen into in the case where the caller's
48 // buffer is not of sufficient size. 62 // buffer is not of sufficient size.
49 unsigned char min_sized_buffer_[MIN_SIZE]; 63 unsigned char min_sized_buffer_[MIN_SIZE];
50 64
51 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer); 65 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer);
52 }; 66 };
53 67
68 // Initialize OpenSSL if it isn't already initialized. This must be called
69 // before any other OpenSSL functions.
70 // This function is thread-safe, and OpenSSL will only ever be initialized once.
71 // OpenSSL will be properly shut down on program exit.
72 void EnsureOpenSSLInit();
73
54 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes 74 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes
55 // are send to VLOG(1), on a release build they are disregarded. 75 // are send to VLOG(1), on a release build they are disregarded.
56 void ClearOpenSSLERRStack(); 76 void ClearOpenSSLERRStack();
57 77
58 } // namespace base 78 } // namespace base
59 79
60 #endif // BASE_OPENSSL_UTIL_H_ 80 #endif // BASE_OPENSSL_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698