Kea  1.9.9-git
openssl_common.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 namespace isc {
8 namespace cryptolink {
9 namespace ossl {
10 
16 const EVP_MD*
18 
22 template<typename T>
23 class SecBuf {
24 public:
25  typedef typename std::vector<T>::iterator iterator;
26 
27  typedef typename std::vector<T>::const_iterator const_iterator;
28 
29  explicit SecBuf() : vec_() {}
30 
31  explicit SecBuf(size_t n, const T& value = T()) : vec_(n, value) {}
32 
33  SecBuf(iterator first, iterator last) : vec_(first, last) {}
34 
35  SecBuf(const_iterator first, const_iterator last) : vec_(first, last) {}
36 
37  SecBuf(const std::vector<T>& x) : vec_(x) {}
38 
39  ~SecBuf() {
40 #if defined(__has_feature)
41 #if __has_feature(address_sanitizer)
42  // Make the address sanitizer happy assuming this won't reallocate
43  vec_.resize(vec_.capacity());
44 #endif
45 #endif
46  std::memset(&vec_[0], 0, vec_.capacity() * sizeof(T));
47  };
48 
49  iterator begin() {
50  return (vec_.begin());
51  };
52 
53  const_iterator begin() const {
54  return (vec_.begin());
55  };
56 
57  iterator end() {
58  return (vec_.end());
59  };
60 
61  const_iterator end() const {
62  return (vec_.end());
63  };
64 
65  size_t size() const {
66  return (vec_.size());
67  };
68 
69  void resize(size_t sz) {
70  vec_.resize(sz);
71  };
72 
73  void clear() {
74 #if defined(__has_feature)
75 #if __has_feature(address_sanitizer)
76  // Make the address sanitizer happy assuming this won't reallocate
77  vec_.resize(vec_.capacity());
78 #endif
79 #endif
80  std::memset(&vec_[0], 0, vec_.capacity() * sizeof(T));
81  vec_.clear();
82  }
83 
84  SecBuf& operator=(const SecBuf& x) {
85  if (&x != *this) {
86  vec_ = x.vec_;
87  }
88  return (*this);
89  };
90 
91  T& operator[](size_t n) {
92  return (vec_[n]);
93  };
94 
95  const T& operator[](size_t n) const {
96  return (vec_[n]);
97  };
98 
99  // constant time comparison against timing attacks
100  // (same type than XXX::verify() so const void* (vs. const T*) x)
101  bool same(const void* x, size_t len) const {
102  bool ret = true;
103  const T* p = static_cast<const T*>(x);
104  for (size_t i = 0; i < len; ++i)
105  ret = ret && (vec_[i] == p[i]);
106  return ret;
107  };
108 
109 private:
110  std::vector<T> vec_;
111 };
112 
113 } // namespace ossl
114 } // namespace cryptolink
115 } // namespace isc
Defines the logger used by the top-level component of kea-dhcp-ddns.