aboutsummaryrefslogtreecommitdiff
path: root/util/crypto/openssl_util.h
blob: 14cb138fc787f0ef39a2fa75af72e7d86a1b69c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UTIL_CRYPTO_OPENSSL_UTIL_H_
#define UTIL_CRYPTO_OPENSSL_UTIL_H_

#include <openssl/ssl.h>
#include <stddef.h>

#include <cstring>

#include "platform/base/error.h"
#include "platform/base/location.h"
#include "platform/base/macros.h"

namespace openscreen {
// Initialize OpenSSL if it isn't already initialized. This must be called
// before any other OpenSSL functions though it is safe and cheap to call this
// multiple times.
// This function is thread-safe, and OpenSSL will only ever be initialized once.
// OpenSSL will be properly shut down on program exit.
// Multiple sequential calls to EnsureOpenSSLInit or EnsureOpenSSLCleanup are
// ignored by OpenSSL itself.
void EnsureOpenSSLInit();
void EnsureOpenSSLCleanup();

// 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 CURRENT_LOCATION as the |location|.
void ClearOpenSSLERRStack(const Location& location);

Error GetSSLError(const SSL* ssl, int return_code);

// Place an instance of this class on the call stack to automatically clear
// the OpenSSL error stack on function exit.
class OpenSSLErrStackTracer {
 public:
  // Pass CURRENT_LOCATION 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.
  explicit OpenSSLErrStackTracer(const Location& location)
      : location_(location) {
    EnsureOpenSSLInit();
  }
  ~OpenSSLErrStackTracer() { ClearOpenSSLERRStack(location_); }

 private:
  const Location location_;

  OSP_DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer);
};

}  // namespace openscreen

#endif  // UTIL_CRYPTO_OPENSSL_UTIL_H_