Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /crypto /openssl_util.h
blob: f1a95e64fc3a958057cf0db9f8015fdec60fef0b [file] [log] [blame] [edit]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#ifndef CRYPTO_OPENSSL_UTIL_H_
#define CRYPTO_OPENSSL_UTIL_H_
#include<stddef.h>
#include<string.h>
#include"base/location.h"
#include"base/memory/raw_ptr.h"
#include"crypto/crypto_export.h"
namespace crypto{
// Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
// SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
// of our base wrapper APIs.
// This allows the library to write directly to the caller's buffer if it is of
// sufficient size, but if not it will write to temporary |min_sized_buffer_|
// of required size and then its content is automatically copied out on
// destruction, with truncation as appropriate.
template<int MIN_SIZE>
classScopedOpenSSLSafeSizeBuffer{
public:
ScopedOpenSSLSafeSizeBuffer(unsignedchar* output,size_t output_len)
: output_(output),
output_len_(output_len){
}
ScopedOpenSSLSafeSizeBuffer(constScopedOpenSSLSafeSizeBuffer&)=delete;
ScopedOpenSSLSafeSizeBuffer&operator=(constScopedOpenSSLSafeSizeBuffer&)=
delete;
~ScopedOpenSSLSafeSizeBuffer(){
if(output_len_< MIN_SIZE){
// Copy the temporary buffer out, truncating as needed.
memcpy(output_, min_sized_buffer_, output_len_);
}
// else... any writing already happened directly into |output_|.
}
unsignedchar* safe_buffer(){
return output_len_< MIN_SIZE? min_sized_buffer_: output_.get();
}
private:
// Pointer to the caller's data area and its associated size, where data
// written via safe_buffer() will [eventually] end up.
raw_ptr<unsignedchar> output_;
size_t output_len_;
// Temporary buffer writen into in the case where the caller's
// buffer is not of sufficient size.
unsignedchar min_sized_buffer_[MIN_SIZE];
};
// Drains the OpenSSL ERR_get_error stack. On a debug build the error codes
// are send to VLOG(1), on a release build they are disregarded. In most
// cases you should pass FROM_HERE as the |location|.
CRYPTO_EXPORTvoidClearOpenSSLERRStack(constbase::Location& location);
// Place an instance of this class on the call stack to automatically clear
// the OpenSSL error stack on function exit.
classOpenSSLErrStackTracer{
public:
OpenSSLErrStackTracer()=delete;
// Pass FROM_HERE as |location|, to help track the source of OpenSSL error
// messages. Note any diagnostic emitted will be tagged with the location of
// the constructor call as it's not possible to trace a destructor's callsite.
explicitOpenSSLErrStackTracer(constbase::Location& location)
: location_(location){}
OpenSSLErrStackTracer(constOpenSSLErrStackTracer&)=delete;
OpenSSLErrStackTracer&operator=(constOpenSSLErrStackTracer&)=delete;
~OpenSSLErrStackTracer(){
ClearOpenSSLERRStack(location_);
}
private:
constbase::Location location_;
};
}// namespace crypto
#endif// CRYPTO_OPENSSL_UTIL_H_

[8]ページ先頭

©2009-2025 Movatter.jp