blob: db6537eef070290bded82e71d64d5e2071c98c3f [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
avidd373b8b2015-12-21 21:34:438#include <stddef.h>
Erik Chenc073e802020-04-22 21:21:549#include <string.h>
avidd373b8b2015-12-21 21:34:4310
[email protected]c62dd9d2011-09-21 18:05:4111#include "base/location.h"
avidd373b8b2015-12-21 21:34:4312#include "base/macros.h"
[email protected]fb10ca02011-10-06 19:17:3613#include "crypto/crypto_export.h"
[email protected]70372d42010-10-22 13:12:3414
[email protected]4b559b4d2011-04-14 17:37:1415namespace crypto {
[email protected]70372d42010-10-22 13:12:3416
17// Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
18// SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
eroman01db75e2016-08-25 22:15:3819// of our base wrapper APIs.
[email protected]70372d42010-10-22 13:12:3420// This allows the library to write directly to the caller's buffer if it is of
21// sufficient size, but if not it will write to temporary |min_sized_buffer_|
22// of required size and then its content is automatically copied out on
23// destruction, with truncation as appropriate.
24template<int MIN_SIZE>
25class ScopedOpenSSLSafeSizeBuffer {
26 public:
27 ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len)
28 : output_(output),
29 output_len_(output_len) {
30 }
31
32 ~ScopedOpenSSLSafeSizeBuffer() {
33 if (output_len_ < MIN_SIZE) {
34 // Copy the temporary buffer out, truncating as needed.
35 memcpy(output_, min_sized_buffer_, output_len_);
36 }
37 // else... any writing already happened directly into |output_|.
38 }
39
40 unsigned char* safe_buffer() {
41 return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_;
42 }
43
44 private:
[email protected]a2c92a1c2012-04-03 12:32:1445 // Pointer to the caller's data area and its associated size, where data
[email protected]70372d42010-10-22 13:12:3446 // written via safe_buffer() will [eventually] end up.
47 unsigned char* output_;
48 size_t output_len_;
49
50 // Temporary buffer writen into in the case where the caller's
51 // buffer is not of sufficient size.
52 unsigned char min_sized_buffer_[MIN_SIZE];
[email protected]ac0f8be2010-11-12 12:03:5453
54 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer);
55};
56
[email protected]313834722010-11-17 09:57:1857// Initialize OpenSSL if it isn't already initialized. This must be called
[email protected]c8a80e92014-05-17 16:02:0858// before any other OpenSSL functions though it is safe and cheap to call this
59// multiple times.
[email protected]313834722010-11-17 09:57:1860// This function is thread-safe, and OpenSSL will only ever be initialized once.
61// OpenSSL will be properly shut down on program exit.
bauerb01c4fff2016-01-23 02:18:0462CRYPTO_EXPORT void EnsureOpenSSLInit();
[email protected]313834722010-11-17 09:57:1863
[email protected]ac0f8be2010-11-12 12:03:5464// Drains the OpenSSL ERR_get_error stack. On a debug build the error codes
[email protected]be796bb2010-11-18 15:43:4365// are send to VLOG(1), on a release build they are disregarded. In most
66// cases you should pass FROM_HERE as the |location|.
Brett Wilson1c990022017-09-12 20:11:1567CRYPTO_EXPORT void ClearOpenSSLERRStack(const base::Location& location);
[email protected]be796bb2010-11-18 15:43:4368
69// Place an instance of this class on the call stack to automatically clear
70// the OpenSSL error stack on function exit.
71class OpenSSLErrStackTracer {
72 public:
73 // Pass FROM_HERE as |location|, to help track the source of OpenSSL error
74 // messages. Note any diagnostic emitted will be tagged with the location of
75 // the constructor call as it's not possible to trace a destructor's callsite.
Brett Wilson1c990022017-09-12 20:11:1576 explicit OpenSSLErrStackTracer(const base::Location& location)
[email protected]be796bb2010-11-18 15:43:4377 : location_(location) {
78 EnsureOpenSSLInit();
79 }
80 ~OpenSSLErrStackTracer() {
81 ClearOpenSSLERRStack(location_);
82 }
83
84 private:
Brett Wilson1c990022017-09-12 20:11:1585 const base::Location location_;
[email protected]be796bb2010-11-18 15:43:4386
87 DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer);
88};
[email protected]ac0f8be2010-11-12 12:03:5489
[email protected]4b559b4d2011-04-14 17:37:1490} // namespace crypto
[email protected]70372d42010-10-22 13:12:3491
[email protected]4b559b4d2011-04-14 17:37:1492#endif // CRYPTO_OPENSSL_UTIL_H_